caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pal-Kristian Engstad <pal_engstad@naughtydog.com>
To: Loup Vaillant <loup.vaillant@gmail.com>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Unexpected restriction in "let rec" expressions
Date: Wed, 27 Feb 2008 11:03:08 -0800	[thread overview]
Message-ID: <47C5B3EC.1080206@naughtydog.com> (raw)
In-Reply-To: <6f9f8f4a0802260424o5bce2fd9i89fbfa38bb239a6a@mail.gmail.com>

Loup Vaillant wrote:
> loop :: ((a,c) -> (b,c)) -> a -> b
> loop f a = b
>   where (b,c) = f (a,c)
>   
Remember that values in Haskell are lazy, which simply means that they 
are pointers to things that is either a pointer to a function to 
evaluate it, or the cached value. (This works, since all Haskell values 
are immutable.)

So, what we have here is a specification on how to calculate (b,c) from 
(a, c), given a function f :: (a,c) -> (b, c), and a. In order to 
evaluate b, we must evaluate the pair (b, c), i.e. evaluate the function 
call. Here we're using the value c, which is undefined - except that we 
have one constraint. The second result value is the same as c, and since 
a value is always immutable, it means that the second result value must 
be the *same value* as the second input value.

In other words, we're telling the compiler: Given a function f :: (a, c) 
-> (b, c) and a value of type a, loop f a will give the result b by 
evaluating (b, c') = f (a, c) where c' == c always.

An example function is:

g (a, c) = (c a, (*2))

Here' the constraint is telling us that c == (*2), and since we're 
returning the first parameter, the result is [c a == (*2) a == a * 2].

Another:

g (a, c) = (a + c, 2)

Now, c == 2, thus loop g a == a + 2.

The other examples are of the same theme.

g (a, c) = (c, a : map (+a) c), means that c == a : map (+a) c, which 
again is a recursive definition, and since everything is lazy this 
works. The first element in the list is a, when the second element is 
requested, map (+a) c is requested, but that means requesting c again 
which equals a : map (+a) c, so we get map (+a) (a : map (+a) c), which 
requires just the head  of the cons-cell,which is a, so the result is 
(+a) a : map (+a) (map (+a) c), which is (a + a) : (map (+a) $ map (+a) c)

Hope it helps,

PKE

-- 
Pål-Kristian Engstad (engstad@naughtydog.com), 
Lead Graphics & Engine Programmer,
Naughty Dog, Inc., 1601 Cloverfield Blvd, 6000 North,
Santa Monica, CA 90404, USA. Ph.: (310) 633-9112.

"Most of us would do well to remember that there is a reason Carmack
is Carmack, and we are not Carmack.",
                       Jonathan Blow, 2/1/2006, GD Algo Mailing List


  parent reply	other threads:[~2008-02-27 19:06 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
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 [this message]
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=47C5B3EC.1080206@naughtydog.com \
    --to=pal_engstad@naughtydog.com \
    --cc=caml-list@inria.fr \
    --cc=loup.vaillant@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).