Here's a little trick that I find useful when I get a type error due to a function that I believe is polymorphic, but isn't due to some bug.
Here's a type-checking problem I ran into today. I had a module with a variant type matching a signature that exposed the variant type.
module type S = sig type t = A | B end module M : S = struct type t = A | B end
I wanted to extend the module with some new functions, and match a new signature that extended the original signature. Easy, right?
Here's another puzzle:
Is it possible in OCaml to define a variable-argument function? For
example, can one define a function f and values a and z such
that the following assertions hold:
assert (f z = 0); assert (f a z = 1); assert (f a a z = 2); assert (f a a a z = 3); ...
In OCaml, referring to constructors defined in other modules can be somewhat awkward. Suppose we have a module like the following.
module Example = struct type t = Foo | Bar | Baz end
To write a function that pattern matches on values of type
Example.t we could directly refer to the variants as follows.
let f e = match e with | Example.Foo -> ... | Example.Bar -> ... | Example.Baz -> ...
That is pretty verbose.
The Unix module defines the Unix_error exception constructor.
module Unix : sig exception Unix_error of error * string * string ... end
Suppose you want to create your own My_unix module that defines some
Unix utility functions and exports the same Unix_error. How would
you do it?
Is it possible in OCaml to implement a universal type, into which any other type can be embedded? More concretely, is possible to implement the following signature?
module type Univ = sig type t val embed: unit -> ('a -> t) * (t -> 'a option) end