caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: brogoff <brogoff@speakeasy.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] type-checking difficulties in recursive functions
Date: Wed, 27 Apr 2005 13:55:24 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.58.0504271347150.32221@shell1.sea5.speakeasy.net> (raw)
In-Reply-To: <001b01c54b65$b28bbe90$14b2a8c0@wiko>

On Wed, 27 Apr 2005, Andreas Rossberg wrote:
> Mike Hamburg <hamburg@fas.harvard.edu> wrote:
> >
> > let rec foo p x =
> > if p then x
> > else
> > let something = foo false "Hello"
> > in x;;
> > val foo : bool -> string -> string = <fun>
> >
> > Now, foo is actually safe as a function from bool -> 'a -> 'a, but the
> > recursive declaration doesn't generalize and so the checker doesn't
> > realize this.  With magic, I can get the right type relatively safely
>
> What you want is "polymorphic recursion". Unfortunately, type inference for
> polymorphic recursion is undecidable in general. Haskell allows polymorphic
> recursion if you annotate the type, so that it doesn't have to be inferred.
> In OCaml, this is not possible directly, but you can achieve the same effect
> by going through a record and using the more recent feature of polymorphic
> record fields, plus OCaml's let rec "language extension":

It's also possible (and slightly nicer IMO) to use the experimental recursive
module facility to encode polymorphic recursion in OCaml. There are some
restrictions, but they're not horrible.

Speaking of modules, for a while, there was talk of some notion of mixin modules
to address the problems (and more) being addressed by the recursive module
feature. Anyone in the know care to comment on what happened with that?

-- Brian


      reply	other threads:[~2005-04-27 20:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-27 19:14 Mike Hamburg
2005-04-27 20:13 ` [Caml-list] " Andreas Rossberg
2005-04-27 20:55   ` brogoff [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=Pine.LNX.4.58.0504271347150.32221@shell1.sea5.speakeasy.net \
    --to=brogoff@speakeasy.net \
    --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).