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. For example, suppose I had a function

f that I believed was of type 'a list -> 'a list, but really wasn't.

  1. let f x = 13 :: x (* suspend disbelief -- pretend f is large and complex *)
  2. let l = f ["foo"]

If I feed this to OCaml, I get an error message at the point f is applied saying:

This expression has type string but is here used with type int

I would like to find out what the error in f is that makes it not polymorphic. When I first came to OCaml from SML, I was surprised to find the following did not work.

  1. let f (x : 'a list) = 13 :: x
  2. let l = f ["foo"]

In SML, type variables in expressions are universally quantified (at a point determined by some complex rules), while in OCaml they are not. So, while SML would reject the definition of f, OCaml happily unifies a with int and continues.

In OCaml, one can get universally quantified type variables by using the signature language.

  1. include (struct
  2. let f x = 13 :: x
  3. end : sig
  4. val f : 'a list -> 'a list
  5. end)
  6. let l = f ["foo"]

This fails with a more helpful error message.

  1. Signature mismatch:
  2. Modules do not match:
  3. sig val f : int list -> int list end
  4. is not included in
  5. sig val f : 'a list -> 'a list end
  6. Values do not match:
  7. val f : int list -> int list
  8. is not included in
  9. val f : 'a list -> 'a list

However, it's a lot of syntax to use the signature language, and can be difficult if the function you want is not at the top level. Furthermore, you may not want to write out the full type -- perhaps you only want to add enough of a constraint to catch the error. In SML, I just had to write the constraint on x and I was done. Fortunately, one can approximate the SML solution in OCaml by using a new type that has no values.

  1. type z
  2. let f (x : z list) = 13 :: x

This fails with an error message at the use of x that is quite helpful.

  1. This expression has type z list but is here used with type int list

If f actually were polymorphic, then instantiating the polymorphism with a new type should succeed, and I would get an error later in the code at a use of f. So, using this trick I can now focus on f until I fix all its type errors, at which point I can remove the constraint and type z.