caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* curried fns
@ 1995-11-20 15:54 Jocelyn Serot
  1995-11-20 18:06 ` John Harrison
  0 siblings, 1 reply; 6+ messages in thread
From: Jocelyn Serot @ 1995-11-20 15:54 UTC (permalink / raw)
  To: caml-list


Hello,

Could someone please explain the difference(s) between:

	let f x = function y -> y + x;;

and

	let f x y = y + x;;

Both have the same type (int -> int -> int) but they seem to behave
distinctly wrt evaluation strategy.

For instance, if i use the 1st form and write:

	let h x = let z = fact x in fun y -> y + z;;
	map (h 30) [1;2;3];; (* note 1 *)

fact 30 gets evaluated only once (partial evaluation), while
the use of the 2nd form for the h function:

	let h x y = let z = fact x in y + z;;
	map (h 30) [1;2;3];;
 
causes fact 30 to be evaluated _for each_ element of the list.

Is this normal or do i misunderstand sth about curryfied fns ?..

Thanks for any help

	Jocelyn S.

(* note 1: this example is inspired from a a similar one given by
   X. Leroy in his Research Report INRIA/117 about the Zinc experiment *)

--
E-mail: Jocelyn.Serot@lasmea.univ-bpclermont.fr
S-mail: LASMEA - URA 1793 CNRS, Universite Blaise Pascal, 63177 Aubiere cedex
Tel: (33) 73.40.73.30 - Fax: (33) 73.40.72.62




^ permalink raw reply	[flat|nested] 6+ messages in thread
* Re: curried fns
@ 1995-11-20 18:51 Laurent CHENO
  0 siblings, 0 replies; 6+ messages in thread
From: Laurent CHENO @ 1995-11-20 18:51 UTC (permalink / raw)
  To: caml-list


Jocelyn Serot wrote :

>>Hello,
>>
>>Could someone please explain the difference(s) between:
>>
>>        let f x = function y -> y + x;;
>>
>>and
>>
>>        let f x y = y + x;;
>>
>>Both have the same type (int -> int -> int) but they seem to behave
>>distinctly wrt evaluation strategy.
>>
>>For instance, if i use the 1st form and write:
>>
>>        let h x = let z = fact x in fun y -> y + z;;
>>        map (h 30) [1;2;3];; (* note 1 *)
>>
>>fact 30 gets evaluated only once (partial evaluation), while
>>the use of the 2nd form for the h function:
>>
>>        let h x y = let z = fact x in y + z;;
>>        map (h 30) [1;2;3];;
>>
>>causes fact 30 to be evaluated _for each_ element of the list.
>>
>>Is this normal or do i misunderstand sth about curryfied fns ?..
>>
>>Thanks for any help


1. excuse my poor english, thank's

2. I think this can help (I hope so !)

For my example :

#let fact x = print_int x ; print_newline() ; x ;;
fact : int -> int = <fun>

first version

#let h1 x =
   let z = fact x
   in
   function y -> y + z ;;
h1 : int -> int -> int = <fun>

NB : the result of h1 30 is a function, which has been evaluated in a
environment where
z yields for the result of fact 30

So, we can see the result of print_int in *this* call !

#let a1 = h1 30 ;;
30
a1 : int -> int = <fun>


Now, a1 is all defined : no more print ... the function a1 don't print anything

#map a1 [1 ; 2 ; 3] ;;
- : int list = [31; 32; 33]


second version

#let h2 x y =
   let z = fact x
   in
   y + z ;;
h2 : int -> int -> int = <fun>

there is no call for fact : you have specialized the first (and not last)
argument of h2

#let a2 = h2 30 ;;
a2 : int -> int = <fun>

now the evaluation is complete, and there is calls for print_int

#map a2 [1 ; 2 ; 3] ;;
30
30
30
- : int list = [31; 32; 33]
#

Laurent Chéno

-------------------------------------------------------------------
Laurent CHENO teaching at / enseignant au
        Lycée Louis-le-Grand - 123 rue Saint-Jacques
        75231 PARIS CEDEX 05 - FRANCE
personal phone (33) 1 48 05 16 04 - fax  (33) 1 48 07 80 18
-------------------------------------------------------------------






^ permalink raw reply	[flat|nested] 6+ messages in thread
* Re:  curried fns
@ 1995-11-20 20:32 Fauque UPS
  0 siblings, 0 replies; 6+ messages in thread
From: Fauque UPS @ 1995-11-20 20:32 UTC (permalink / raw)
  To: Jocelyn.Serot, caml-list



I am not a caml expert and the gurus will say if I am wrong, but this
is what I understand:

> Could someone please explain the difference(s) between:
>
>	let f x = function y -> y + x;;
>
> and
>
>	let f x y = y + x;;

there is absolutely no differences between these two expressions: y+x will
be evaluated only when two arguments x and y are given to f.

> Both have the same type (int -> int -> int) but they seem to behave
> distinctly wrt evaluation strategy.
> 
> For instance, if i use the 1st form and write:
> 
> 	let h x = let z = fact x in fun y -> y + z;;
> 	map (h 30) [1;2;3];; (* note 1 *)
> 
> fact 30 gets evaluated only once (partial evaluation), while
> the use of the 2nd form for the h function:
> 
> 	let h x y = let z = fact x in y + z;;
> 	map (h 30) [1;2;3];;
>  
> causes fact 30 to be evaluated _for each_ element of the list.

this is not the same case: the first form should have been:

   let h x = fun y -> let z = fact x in y + z;;

and here fact 30 will be evaluated for each element of the list;
you have done in fact an optimization by putting the fact x before
the fun y ->   and it lets caml compute the fact x without the need
to know the actual value of y.

Hubert Fauque
hubert.fauque@enst-bretagne.fr




^ permalink raw reply	[flat|nested] 6+ messages in thread
* Re: curried fns
@ 1995-11-21  5:17 Tarizzo Martial
  0 siblings, 0 replies; 6+ messages in thread
From: Tarizzo Martial @ 1995-11-21  5:17 UTC (permalink / raw)
  To: caml-list


>For instance, if i use the 1st form and write:
>
>	let h x = let z = fact x in fun y -> y + z;;
>	map (h 30) [1;2;3];; (* note 1 *)
>
>fact 30 gets evaluated only once (partial evaluation), while
>the use of the 2nd form for the h function:
>
>	let h x y = let z = fact x in y + z;;
>	map (h 30) [1;2;3];;
> 
>causes fact 30 to be evaluated _for each_ element of the list.
>
>Is this normal or do i misunderstand sth about curryfied fns ?..

I think it's perfectly normal. One can rewrite the two definitions of h
without the syntactic sugar provided by 'in' :
* for the first one :
let h = 
  fun x ->
    (fun z ->
      (fun y -> y+z))
    (fact x);;
* for the second one :
let h = 
  fun x ->
    fun y ->
      (fun z -> y+z)
      (fact x);;

It's clear that the value returned by h x;; is a function of y.

In the first def, fact x is evaluated when we compute h x;; and this value
is 'captured' in the definition of fun y 

In the second def, fact x is computed in the body of the function of y, i.e
each time this funciotn is called.

It's very easy to see this behavior : try to evaluate 
h 30;; 
with a side effect in the fact function (print its argument for example)
The first definition gives :
let h x = let z = fact x in fun y -> y + z;;
h : int -> int -> int = <fun>
#h 3;;
3
- : int -> int = <fun>

while the second one gives :
let h x y = let z = fact x in y + z;;
h : int -> int -> int = <fun>
#h 3;;
- : int -> int = <fun>
*********************************
 Tarizzo Martial
 Prof. Sc Physiques
 Classes preparatoires
 Lycee J MOULIN
 57600 FORBACH

 Email: tarizzo@world-net.sct.fr
        74014.3307@compuserve.com
 Compuserve : 74014,3307
*********************************





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

end of thread, other threads:[~1995-11-21 10:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-11-20 15:54 curried fns Jocelyn Serot
1995-11-20 18:06 ` John Harrison
1995-11-21 10:48   ` Pierre Weis
1995-11-20 18:51 Laurent CHENO
1995-11-20 20:32 Fauque UPS
1995-11-21  5:17 Tarizzo Martial

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).