caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andrej Bauer <andrej.bauer@andrej.com>
To: Gerd Stolpmann <gerd@gerd-stolpmann.de>
Cc: saptarshi.guha@gmail.com, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] The need to specify 'rec' in a recursive function  defintion
Date: Wed, 10 Feb 2010 08:19:10 +0100	[thread overview]
Message-ID: <7d8707de1002092319j4d6bfca5s169914a992d57be2@mail.gmail.com> (raw)
In-Reply-To: <1265752863.5482.42.camel@flake.lan.gerd-stolpmann.de>

On Tue, Feb 9, 2010 at 11:01 PM, Gerd Stolpmann <gerd@gerd-stolpmann.de> wrote:
> For defining the operational meaning of a recursive function a special
> helper is needed, the Y-combinator. It gets quite complicated here from
> a theoretical point of view because the Y-combinator is not typable. But
> generally, the idea is to have a combinator y that can be applied to a
> function like
>   y (fun f arg -> expr) arg
> and that "runs" this function recursively, where "f" is the recursion.

A small correction: y is typeable. It's a fixed-point operator, so it's type is

('a -> 'a) -> 'a

or if we only care about recursive functions it is:

# let rec y f x = f (y f) x ;;
val y : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b = <fun>

(Yes, I know I used "let rec", it doesn't matter for checking that y
has a type). Let's check that it works:

# let fac = y (fun f n -> if n = 0 then 1 else n * f (n - 1)) ;;
val fac : int -> int = <fun>

# fac 7 ;;
- : int = 5040

What Gerd probably meant was that the usual untyped lambda-calculus
definition of y, which gets us recursion out of nothing isn't typeable
because self-application requires unrestricted recursive types. But
again, it's an intermediate definition that is not typeable, not y
itself (here adapted so that it works for functions in an eager
language):

$ ocaml -rectypes
        Objective Caml version 3.11.1

# let z = fun u v w -> v (u u v) w ;;
val z : ('a -> ('b -> 'c -> 'd) -> 'b as 'a) -> ('b -> 'c -> 'd) -> 'c
-> 'd = <fun>

# let y = z z ;;
val y : (('_a -> '_b) -> '_a -> '_b) -> '_a -> '_b = <fun>

Observe that y has a non-recursive type, it's the auxiliary z that
requires a recursive one. And this y also works:

# let fac = y (fun f n -> if n = 0 then 1 else n * f (n - 1)) ;;
val fac : int -> int = <fun>

# fac 7 ;;
- : int = 5040

Can anyone get rid of the pesky underscores in the type of y above, so
that it becomes truly polymorphic?

With kind regards,

Andrej


  parent reply	other threads:[~2010-02-10  7:19 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-09 20:50 Saptarshi Guha
2010-02-09 21:55 ` [Caml-list] " Guillaume Yziquel
2010-02-09 22:14   ` Saptarshi Guha
2010-02-09 22:01 ` Gerd Stolpmann
2010-02-09 21:58   ` Guillaume Yziquel
2010-02-09 22:34     ` Gerd Stolpmann
2010-02-10  0:07       ` Guillaume Yziquel
2010-02-10  3:10         ` Alain Frisch
2010-02-09 22:16   ` Saptarshi Guha
2010-02-09 23:29   ` Jon Harrop
2010-02-10 10:15     ` rossberg
2010-02-10  7:19   ` Andrej Bauer [this message]
2010-02-10  9:36     ` Francois Maurel
2010-02-10 10:12     ` rossberg
2010-02-09 23:33 ` Jon Harrop
2010-02-09 22:31   ` Saptarshi Guha
2010-02-10  0:12     ` Jon Harrop
2010-02-10 22:01 ` Stefan Monnier
2010-02-10 22:25   ` [Caml-list] " Till Varoquaux
2010-02-11  1:48     ` Jon Harrop
2010-02-15 15:46     ` Stefan Monnier
2010-02-15 17:33       ` [Caml-list] " Jon Harrop
2010-02-15 20:36         ` Stefan Monnier
2010-02-16 14:42           ` Stefan Monnier
2010-02-16 16:21             ` [Caml-list] " Ashish Agarwal

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=7d8707de1002092319j4d6bfca5s169914a992d57be2@mail.gmail.com \
    --to=andrej.bauer@andrej.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=gerd@gerd-stolpmann.de \
    --cc=saptarshi.guha@gmail.com \
    /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).