English 中文(简体)
ocaml record type and null
原标题:

I m trying to define type like:

type aaa = NULL | {a: int; b: int};;

But the compiler does not allow to do it. I m not sure of the reason why we can t mix record type with anything else.

I need to match a value if it is the record type or null record and I m tired of creating dummy record like {a = -999; b = -999}.

is there a better way ?

最佳回答

The "record" part of the definition has to be done in a separate type. Then you can wrap that in an "option" type if you want to express "None" or "Some value".

type aaa = {a: int; b: int}
type bbb = aaa option
问题回答

First problem: in the Objective Caml approach, you can t have clean union types without constructors. Consider the following conundrum :

type test = Null | {a : int ; b: int }

let value = { a: 0 ; b : 42 } in print_int value.a

The second line is incorrect, because value is of an union type, and might therefore be Null, which has no member a. This would introduce an implicit assumption about the value of an union type, which Objective Caml avoids at all costs. This means you need a constructor.

But even that would be enough, because then you d have an anonymous record type:

type test = Null | Pair of { a : int ; b : int }

match Pair { a : 0 ; b : 42 } with
  | Null -> 0
  | Pair p -> p.a

What would be the type of p here? This could certainly be solved by allowing anonymous record types into the language, but it s not a simple addition, as such types are notoriously difficult to handle with a type unification approach and require a lot of additional constructs for support (for instance, the < ; ... >, #type and value :> type constructs for handling objects).

Language designers took the easy way out, and required that all record types receive a name. Of course, if you have an exceedingly simple record, you can use a tuple:

type test = Null | Pair of int * int




相关问题
ocamlc, module compilation

I wrote an app in ocaml. It consist of several modules: Util (util.ml) Work1 (work1.ml) -- open Util Work2 (work2.ml) -- open Util, too Main (main.ml) -- open all of them. When i compile its, using ...

How can I simplify this ocaml pattern-matching code?

I m writing a simple little ocaml program that reads an algebraic statement in from a file, parses it into an AST using ocamllex/ocamlyacc, reduces it, and then prints it. The part where I m reducing ...

How can I create a type with multiple parameters in OCaml?

I m trying to create a type that has multiple type parameters. I know how to make a type with one parameter: type a foo = a * int But I need to have two parameters, so that I can parameterize the ...

Hashtable indexed on several fields

I m currently programming an OCaml module defining a type corresponding to a CPU register. The interface of this module is the following : (* * Defines a type which represents a R3000 register. *) ...

Extending an existing type in OCaml

I ve been doing some OCaml programming lately to learn the language and to get more acquainted with functional programming. Recently, I ve started to think that I d like to be able to extend an ...

Ocaml Syntax Error

What s wrong with this code? I can t figure it out: let parent (rules : grammar) (symbol1 : string) (symbol2 : string) : (SymbolSet.t) = try SymbolSet.singleton (getParent [symbol1; symbol2] ...

热门标签