User Tools

Site Tools


Typing Haskell in ML

This post presents a quick translation of the haskell code of the article Typing Haskell in Haskell by Mark P. Jones to OCaml.


I did this translation to experiment with type-class capable type inference for my Photon compiler project. I decided upon OCaml because:

  • It has good LLVM bindings.
  • I find it easier when I have to mix mutable state, error management and I/O (I am not quite at ease with monad transformers).

I share it because:

  • It could be of some use for someone else.
  • It can serve as a comparison point between the two languages (although I don't pretend my OCaml version is pretty).


While I intend to rewrite this code completely from scratch in my project (indeed, my type system is not even the same as Haskell), I kept this translation nearly as close to the original as possible to:

  • allow for easy comparison;
  • allow to follow the article with the OCaml code (and vice-versa).

One thing I changed is switching from monads to exceptions, as OCaml has no syntactic support for monads. I even used exceptions when I could have used the option type for the same reason. Maybe values in the haskell code are used monadically.

There are also some re-ordering, as OCaml process files linearly (i.e. values must be defined before being used). I even had to fuse sections 11.3 to 11.6 due to mutual recursion.

The type inference monad has been replaced by mutable state which is passed explicitely such that a function which had the haskell type

f :: SomeType -> TI OtherType

has the OCaml type

val f : ti -> someType -> otherType

The Code


Enter your comment. Wiki syntax is allowed:
blog/2010/08/04/typing_haskell_in_ml.txt · Last modified: 2011/02/20 20:28 (external edit)