caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: "Hongbo Zhang (BLOOMBERG/ 731 LEX)" <hzhang295@bloomberg.net>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] question: restrictions on `let rec`
Date: Thu, 14 Jul 2016 11:07:59 +0200	[thread overview]
Message-ID: <20160714090759.GC21053@frosties> (raw)
In-Reply-To: <5785631702290072003908A6_0_132218@p057>

On Tue, Jul 12, 2016 at 09:37:27PM -0000, Hongbo Zhang (BLOOMBERG/ 731 LEX) wrote:
> Hi all,
>    I have a question simplified as below:
> 
>  let rec fib = fun x -> fib (x - 1);;                                                                                                                       
> val fib : int -> 'a = <fun>                                                                                                                                  
> # let rec fib = (Obj.magic (fun x -> fib (x - 1)) : int -> 'a);;                                                                                             
> Characters 15-47:                                                                                                                                            
>   let rec fib = (Obj.magic (fun x -> fib (x - 1)) : int -> 'a);;                                                                                             
>                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                            
> Error: This kind of expression is not allowed as right-hand side of `let rec'  
> 
> I know `fib` is a legal expression, is there any way to work around it?
>  Thanks -- Hongbo

The problem is in the evaluation order:

    let rec fib = fun x -> fib (x - 1);;

is a function that will be evaluated when a parameter is passed. At
that later date "fib" is well defined.

    let rec fib = (Obj.magic (fun x -> fib (x - 1)) : int -> 'a);;

Here fib is a value and Obj.magic will be evaluated right there. At
this point "fib" is not yet well defined, you are still defining it.
"fib" is not legal there.

What you have to do is make it a function again:

# let rec fib = fun x -> (Obj.magic (fun x -> fib (x - 1)) : int -> 'a) x;;
val fib : int -> 'a = <fun>

MfG
	Goswin

  parent reply	other threads:[~2016-07-14  9:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-12 21:37 Hongbo Zhang (BLOOMBERG/ 731 LEX)
     [not found] ` <CAKR7PS-2wF19FXo0Qo=x1SM-cuMDUpS-333d10fJzzDBR8XoTg@mail.gmail.com>
     [not found]   ` <CAKR7PS94hePcfyMUKGq0wH-VUZQYdu=hN2jcCXD9z3jcFSwHiw@mail.gmail.com>
     [not found]     ` <CAKR7PS_0=p9mLSVo0vE0YN=DDH4PhCoHboEE_1NWPAf+opq_jg@mail.gmail.com>
2016-07-12 22:17       ` Milan Stanojević
2016-07-14  9:07 ` Goswin von Brederlow [this message]
2016-07-13 13:56 Hongbo Zhang (BLOOMBERG/ 731 LEX)

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=20160714090759.GC21053@frosties \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    --cc=hzhang295@bloomberg.net \
    /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).