caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: caml-list@inria.fr
Subject: ANN: pa_polyrec: syntax for polymorphic recursion
Date: Mon, 28 Sep 2009 21:57:26 +0100	[thread overview]
Message-ID: <4AC12336.1040703@gmail.com> (raw)

I'm pleased to announce the initial release of pa_polyrec, a syntax extension 
for polymorphic recursion in OCaml.

    https://forge.ocamlcore.org/projects/pa-polyrec/

There are several methods for encoding polymorphic-recursive functions in OCaml; 
this extension allows them to be written directly, using a natural syntax.  For 
example, given the following type of perfectly-balanced trees we might wish to 
write a function for summing the leaves.

   type 'a perfect = Zero of 'a | Succ of ('a * 'a) perfect

In standard OCaml such a function can be written as follows:

   let sump f =
     (object (o)
         method sump : 'a. ('a -> int) -> 'a perfect -> int =
           fun f -> function
            | Zero x -> f x
            | Succ x -> o#sump (fun (a, b) -> f a + f b) x
      end)#sump f

   let sum_perfect = sump id

Using pa_polyrec one can write the function in the following less obfuscated style:

   let rec sump : 'a. ('a -> int) -> 'a perfect -> int =
     fun f -> function
      | Zero x -> f x
      | Succ x -> sump (fun (a, b) -> f a + f b) x

   let sum_perfect = sump id

Note that the type variable 'a in the type of the function is quantified: this 
is what differentiates polymorphic-recursive functions from standard OCaml 
recursive function definitions.

More complex usage is supported, including mutual recursion.  A number of 
examples are included in the distribution, including several modules from Chris 
Okasaki's thesis "Purely Functional Data Structures" and code from Richard Bird 
and Ross Paterson's paper "de Bruijn notation as a nested datatype".


             reply	other threads:[~2009-09-28 20:56 UTC|newest]

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

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=4AC12336.1040703@gmail.com \
    --to=yallop@gmail.com \
    --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).