caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: Damien Guichard <alphablock@orange.fr>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] ANN: pa_polyrec: syntax for polymorphic recursion
Date: Tue, 29 Sep 2009 23:42:07 +0100	[thread overview]
Message-ID: <4AC28D3F.20709@gmail.com> (raw)
In-Reply-To: <200909291620091407574@orange.fr>

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


      reply	other threads:[~2009-09-29 22:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-28 20:57 Jeremy Yallop
2009-09-29 14:20 ` [Caml-list] " Damien Guichard
2009-09-29 22:42   ` Jeremy Yallop [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4AC28D3F.20709@gmail.com \
    --to=yallop@gmail.com \
    --cc=alphablock@orange.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).