caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] question: restrictions on `let rec`
@ 2016-07-13 13:56 Hongbo Zhang (BLOOMBERG/ 731 LEX)
  0 siblings, 0 replies; 4+ messages in thread
From: Hongbo Zhang (BLOOMBERG/ 731 LEX) @ 2016-07-13 13:56 UTC (permalink / raw)
  To: caml-list, milanst

[-- Attachment #1: Type: text/plain, Size: 1058 bytes --]

Hi , I found another work around, thanks for your suggestion!

From: milanst@gmail.com At: 07/12/16 18:18:08
To: Hongbo Zhang (BLOOMBERG/ 731 LEX), caml-list@inria.fr
Subject: Re: [Caml-list] question: restrictions on `let rec`


What are you trying to accomplish exactly?
What type do you want fib to have? 
 I usually use lazy and force to get around let rec limitations and that usually allows for only local uglyness, the lazy types usually don't escape.
>
> On Jul 12, 2016 5:37 PM, "Hongbo Zhang (BLOOMBERG/ 731 LEX)" <hzhang295@bloomberg.net> 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


[-- Attachment #2: Type: text/html, Size: 2685 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] question: restrictions on `let rec`
  2016-07-12 21:37 Hongbo Zhang (BLOOMBERG/ 731 LEX)
       [not found] ` <CAKR7PS-2wF19FXo0Qo=x1SM-cuMDUpS-333d10fJzzDBR8XoTg@mail.gmail.com>
@ 2016-07-14  9:07 ` Goswin von Brederlow
  1 sibling, 0 replies; 4+ messages in thread
From: Goswin von Brederlow @ 2016-07-14  9:07 UTC (permalink / raw)
  To: Hongbo Zhang (BLOOMBERG/ 731 LEX); +Cc: caml-list

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] question: restrictions on `let rec`
       [not found]     ` <CAKR7PS_0=p9mLSVo0vE0YN=DDH4PhCoHboEE_1NWPAf+opq_jg@mail.gmail.com>
@ 2016-07-12 22:17       ` Milan Stanojević
  0 siblings, 0 replies; 4+ messages in thread
From: Milan Stanojević @ 2016-07-12 22:17 UTC (permalink / raw)
  To: Hongbo Zhang, Caml List

[-- Attachment #1: Type: text/plain, Size: 827 bytes --]

What are you trying to accomplish exactly?
What type do you want fib to have?

I usually use lazy and force to get around let rec limitations and that
usually allows for only local uglyness, the lazy types usually don't escape.
>
> On Jul 12, 2016 5:37 PM, "Hongbo Zhang (BLOOMBERG/ 731 LEX)" <
hzhang295@bloomberg.net> 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

[-- Attachment #2: Type: text/html, Size: 1128 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Caml-list] question: restrictions on `let rec`
@ 2016-07-12 21:37 Hongbo Zhang (BLOOMBERG/ 731 LEX)
       [not found] ` <CAKR7PS-2wF19FXo0Qo=x1SM-cuMDUpS-333d10fJzzDBR8XoTg@mail.gmail.com>
  2016-07-14  9:07 ` Goswin von Brederlow
  0 siblings, 2 replies; 4+ messages in thread
From: Hongbo Zhang (BLOOMBERG/ 731 LEX) @ 2016-07-12 21:37 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1168 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 1683 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-07-14  9:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-13 13:56 [Caml-list] question: restrictions on `let rec` Hongbo Zhang (BLOOMBERG/ 731 LEX)
  -- strict thread matches above, loose matches on Subject: below --
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 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).