caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dirk Thierbach <dthierbach@gmx.de>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Unexpected restriction in "let rec" expressions
Date: Wed, 27 Feb 2008 13:02:48 +0100	[thread overview]
Message-ID: <20080227120248.GA8000@feanor> (raw)
In-Reply-To: <6f9f8f4a0802270143o68c58cbfh2ea059cda5c0a744@mail.gmail.com>

On Wed, Feb 27, 2008 at 10:43:18AM +0100, Loup Vaillant wrote:
> 2008/2/26, Nicolas Pouillard <nicolas.pouillard@gmail.com>:
> > A picture can helps...

Fixed names to be consistent with

loop :: ((a,c) -> (b,c)) -> a -> b
loop f a = b where (b,c) = f (a,c)

>>
>>      +---------+
>>   a>-|         |->b
>>      |    x    |
>>   c>-|         |->c
>>      +---------+
>>
>>      +---------+
>>   a>-|         |->b
>>      |    y    |
>>  +->-|         |->-+
>>  |   +---------+   |
>>  +--------c--------+
>>

> I saw this image before, but despite of its clarity, it hasn't solved
> my problem: the chicken and egg one. See, the looping "c" in y looks
> like it has to be produced out of thin air. 

It's easier to think this in Haskell, because then one hasn't to deal
with lazyness explicitely. Consider

f (a,c) = (c, a : map (+a) c)

That's a perfectly non-recursive function. Now, what happens when
we apply "loop" to it?

*Main> take 10 $ loop f 1 
[1,2,3,4,5,6,7,8,9,10]
*Main> take 10 $ loop f 2
[2,4,6,8,10,12,14,16,18,20]

Ah, so it actually does produce values out of thin air :-) How did that
happen? Well, inside f, we used the argument c before it was really
defined, but in doing so, we added information *before* feeding it back 
through the loop. One can picture the "incomplete" list going round
and round the loop, and with each step gaining more information
(here for a = 1):

?
1,?
1,2,?
1,2,3,?

and so on. Of course that's not what really happens during the
computation -- there lazy evaluation takes care that the unkown part
isn't actually evaluated before it has been defined. With strict
evaluation, the complete argument is evaluated on function call, so
this doesn't work.

BTW, the "canonical" fixpoint operator, also called Y-combinator,

  fix : (a -> a) -> a 

which has already been mentioned can be thought of in a similar way.

Does that explanation help?

And a semantic explanation with domains (a special kind of partial
orders) can make the "gain more information" idea concrete, and also
make a bit more clear why it is called a "fixpoint". Google for those
if you want to learn more.

- Dirk


  reply	other threads:[~2008-02-27 13:29 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-26 12:24 Loup Vaillant
2008-02-26 14:04 ` [Caml-list] " Jean-Christophe Filliâtre
2008-02-26 14:18 ` Damien Doligez
2008-02-26 14:34   ` Loup Vaillant
2008-02-26 14:51     ` Gabriel Kerneis
2008-02-26 14:56     ` blue storm
2008-02-26 17:48     ` Nicolas Pouillard
2008-02-26 14:57 ` Dirk Thierbach
2008-02-27  8:53 ` Andrej Bauer
2008-02-27  9:43   ` Loup Vaillant
2008-02-27 12:02     ` Dirk Thierbach [this message]
2008-02-27 14:04       ` Loup Vaillant
2008-02-27 16:41         ` Dirk Thierbach
2008-02-27 23:32           ` Loup Vaillant
2008-02-27 19:03 ` Pal-Kristian Engstad
2008-02-27 23:46   ` Loup Vaillant

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=20080227120248.GA8000@feanor \
    --to=dthierbach@gmx.de \
    --cc=caml-list@yquem.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).