caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alain Frisch <Alain.Frisch@inria.fr>
To: Jeremy Yallop <jeremy.yallop@ed.ac.uk>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Polymorphic recursion
Date: Wed, 04 Apr 2007 07:27:28 +0200	[thread overview]
Message-ID: <46133740.3070305@inria.fr> (raw)
In-Reply-To: <46128CEE.2090704@ed.ac.uk>

Jeremy Yallop wrote:
> Right.  You can write polymorphic-recursive functions if you wrap them
> in recursive modules, though:
> 
>   module rec Size : sig val size : 'a seq -> int end = struct
>     let rec size = function
>       | Unit -> 0
>       | Seq (_, b) -> 1 + 2 * Size.size b
>   end

Note that you don't even need the "rec" in this example, and that the
same idiom would support mutually recursive functions. To make an
automatic translation simpler, you can simply "open Size" at the
beginning of the structure to avoid rewriting self-references in the
function's body. To support local definitions, you can of course rely on
local modules:

let rec f : 'a 'b. t = E1 in E2

becomes:

let module X = struct
  module rec Y : sig val f : t end = struct
    open Y
    let f = E1
  end
  open Y
  let v = E2
end in
X.v

However, this encoding has an important drawback: you cannot use type
variables currently in scope in t, E1, E2 (as a consequence, we don't
need to explicitly quantify over variables in the function prototype,
the encoding forces all the variables in the function's type to be
universally quantified). By changing the encoding, you can allow
references to those type variables in E2:

let f =
 let module X = struct
   module rec Y : sig val f : t end = struct
     open Y
     let f = E1
   end
 end in
 X.Y.f
in
E2


-- Alain


  reply	other threads:[~2007-04-04  5:27 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-03 16:59 Loup Vaillant
2007-04-03 17:20 ` [Caml-list] " Jeremy Yallop
2007-04-04  5:27   ` Alain Frisch [this message]
2007-04-04 12:54     ` Loup Vaillant
2007-04-03 17:35 ` Till Varoquaux
2007-04-03 20:00   ` brogoff
2007-04-04  1:27     ` skaller
2007-04-04  1:40       ` skaller
2007-04-04 13:49 ` Roland Zumkeller
2007-04-04 15:13   ` Alain Frisch
2007-04-04 15:20     ` Alain Frisch
2007-04-04 16:45       ` Roland Zumkeller
2007-04-04 19:58         ` Alain Frisch
2007-04-04 20:13           ` brogoff
2007-04-05  9:33           ` Roland Zumkeller
2007-04-05  9:54             ` Alain Frisch
2007-04-05 10:07               ` Daniel de Rauglaudre
2007-04-05  9:46           ` Francois Maurel
2007-04-04 15:50     ` Stefan Monnier
2007-04-04 23:36 ` [Caml-list] " Brian Hurt
2007-04-05  8:17   ` Loup Vaillant
  -- strict thread matches above, loose matches on Subject: below --
2008-05-12 21:55 polymorphic recursion Jacques Le Normand
2008-05-12 22:16 ` [Caml-list] " Christophe TROESTLER
2003-08-24 18:01 [Caml-list] Polymorphic recursion Lukasz Stafiniak
2003-08-25  0:30 ` Jacques Garrigue
2003-08-25  0:43   ` Jacques Garrigue

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=46133740.3070305@inria.fr \
    --to=alain.frisch@inria.fr \
    --cc=caml-list@yquem.inria.fr \
    --cc=jeremy.yallop@ed.ac.uk \
    /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).