What are some ways that I can accomplish what Haskell's typeclasses do in OCaml? Basically, I want to write a polymorphic function without writing too much code. The typical way to do polymorphism is to supply an additional argument telling the function is what type it is currently working on. For example, let's say I want to sort a list of ints, I have to pass an additional comparator to the function.

you could use the OO feature of OCaml for that purpose, although there are limits on how far you can emulate haskell typeclasses.
–
didiercFeb 18 '13 at 13:40

@didierc thanks for that link! I'm now wondering if a language extension to OCaml can overcome those limits.
–
Jason YeoFeb 18 '13 at 18:46

1

OO in ocaml is already very powerful, the difference is really that dispatch on return type feature which distinguish haskell.
–
didiercFeb 18 '13 at 21:15

1

Although modules is the traditional way to mimic type classes, class types as interfaces over object classes are interesting and they are not well documented. Check my working example below.
–
Gabriel RibaFeb 19 '13 at 16:17

1

Even in the Haskell type class way of solving this problem you still have to write a comparator function for every type that you want to sort. You just don't have to select which comparator function you want to use on every invocation of sort (and many common types have comparator functions already written for you in the standard library, but I imagine that would be the case in OCaml too).
–
BenNov 13 '13 at 1:45

This is explained in detail in "Modular Type Classes" by Derek Dreyer, Robert Harper, and Manuel M. T. Chakravarty. In Proceedings of The 34th Annual ACM SIGPLAN - SIGACT Symposium on Principles of Programming Languages, ACM Press, 2007. From the abstract:

ML modules and Haskell type classes have proven to be highly effective tools for program structuring. Modules emphasize explicit configuration of program components and the use of data abstraction. Type classes emphasize implicit program construction and ad hoc polymorphism. In this paper, we show how the implicitly-typed style of type class programming may be supported within the framework of an explicitly-typed module language by viewing type classes as a particular mode of use of modules. This view offers a harmonious integration of modules and type classes, where type class features, such as class hierarchies and associated types, arise naturally as uses of existing module-language constructs, such as module hierarchies and type components. In addition, programmers have explicit control over which type class instances are available for use by type inference in a given scope. We formalize our approach as a Harper-Stone-style elaboration relation, and provide a sound type inference algorithm as a guide to implementation.

Basically, as @Thomas showed, type classes in Haskell become module types in OCaml with a type (the type implementing the type class) and a bunch of values using that type.

Then, corresponding to an "instance" of the type class in Haskell, you have a module in OCaml that implements the module type, with the type being the type of the instance, and the values being the implementation of the values in the type class.

And then, every time you have a function that "uses" a type constrained by that type class in Haskell, you need to wrap that function inside a module in OCaml. This module (actually a functor) will take an argument module which corresponds to the instance of the type class we are using.

And every time you use that function, you will need to first create an appropriate module using that functor and passing it the right instance of the type class.

You will notice that a big difference in the Haskell and OCaml ways of doing it, is that in Haskell, when you use that last function, the compiler infers the proper instance of the type class for you; whereas with the OCaml way of doing it, the user must explicitly specify the instance to use.

this looks the closest to what I wanna achieve! But, what does the BatList module do?
–
Jason YeoFeb 19 '13 at 16:28

I tried BatList from batteries because I wanted to use a list generator from BatLazyList named range wich acts as [from..to] in haskell, but I dismissed it ; you can change open BatList with open List and it works too.
–
Gabriel RibaFeb 19 '13 at 16:36