Thanks for the comments, Damien.

Damien Guichard wrote:
Problems are :
   1. functions are harder to type
   2. type errors are quite hard to interpret and to locate
      (although *pa-polyrec* certainly helps in this department)
   3. benchmarks show you pay a certain (small) price for the added type
      expressivity

I think that the arguments you make against polymorphic recursion could also be made against other features of OCaml, such as structurally-typed objects and polymorphic variants: they make type errors more complex and can be less efficient than the alternatives. Still, I'm glad that OCaml has those features because they make it possible to write programs that would otherwise be impossible.

I'm glad you raised these points, though, because they highlight the fact that there is a tradeoff involved. If you use nested data types and polymorphic recursion then you may have to work harder to satisfy the compiler. However, in return the compiler will guarantee the absence of certain errors that would otherwise remain undetected until runtime, and perhaps forever.

May be polymorphic recursion is actually a tool for the working ML programmer.
Yet i am still waiting to see the exemple that is more than a nice trick for the ML hacker.

Here is one example: the following paper shows how to use OCaml extended with GADTs to give a precise type to the output of a parser generator:

   Towards efficient, typed LR parsers
   François Pottier and Yann Régis-Gianas
   ACM Workshop on ML, 2006
   http://gallium.inria.fr/~fpottier/publis/fpottier-regis-gianas-typed-lr.pdf

The run and gotoE procedures, which form a central part of the implementation described in that paper, are inherently polymorphic-recursive, just like most other non-trivial functions over GADTs. OCaml doesn't have GADTs, of course, but Oleg recently described a technique that can be used to translate many programs that use GADTs into OCaml:


http://caml.inria.fr/pub/ml-archives/caml-list/2009/07/2984f23799f442d0579faacbf4e6e904.en.html

Polymorphic recursion is an important element of that technique.

In my opinion, when wants the added type expressivity he actually wants dependant types.

That may be true, but since OCaml doesn't support dependent types I think that it's useful to know what can be accomplished without them.

There is an ancillary benefit to pa_polyrec (besides polymorphic recursion): it provides a clear way of ensuring that a function is polymorphic, giving a solution to the problem described in this blog post by Stephen Weeks:

    http://ocaml.janestreet.com/?q=node/29

For example, suppose that you have a function

   let f = fun x -> 13 :: x

and you want to tell the compiler that it has type 'a list -> 'a list, for any type 'a. (Since it actually does *not* have that type, we want the compiler to complain.) This is surprisingly difficult to write straightforwardly in OCaml! You can't write the following

   let f : 'a list -> 'a list = fun x -> 13 :: x

since OCaml will simply unify the variable 'a with the element type of the list, and type-checking will succeed.

That blog post shows how to use an uninhabited type to guarantee polymorphism: you can write something like the following

   type a

   let f = fun x -> 13 :: x
   let (_ : a list -> a list) = f

and the compiler will duly complain that 'a' and 'int' cannot be unified. This is certainly a useful trick; still, it would be nice to be able to ascribe the polymorphic signature directly. Using pa_polyrec one can write

   let rec f : 'a. 'a list -> 'a list = fun x -> 13 :: x

and the compiler will complain that the type of the right hand side is insufficiently general. (Of course, it would be better to be able to omit the 'rec'; perhaps a future release will permit that.)

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to