English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
ANN: pa_polyrec: syntax for polymorphic recursion
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-09-29 (22:41)
From: Jeremy Yallop <yallop@g...>
Subject: Re: [Caml-list] ANN: pa_polyrec: syntax for polymorphic recursion
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

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:


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:


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.)