caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] inlining/eta-expansion question
@ 2012-02-23 18:53 Jacques Carette
  2012-02-24  1:15 ` Jacques Garrigue
  0 siblings, 1 reply; 2+ messages in thread
From: Jacques Carette @ 2012-02-23 18:53 UTC (permalink / raw)
  To: caml-list

What difference is there, if any, between
   let plus = (+)
and
   let plus = fun a b -> a + b
?

I know that, operationally, these are identical.  What I am wondering 
is, do applications of 'plus' always compile down to the exact same thing?

In code that I am writing, due to interactions with the value 
restriction, I have to eta-expand all my definitions.  So I am wondering 
what cost I will have to pay for the 'genericity' of my code.

Jacques

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

* Re: [Caml-list] inlining/eta-expansion question
  2012-02-23 18:53 [Caml-list] inlining/eta-expansion question Jacques Carette
@ 2012-02-24  1:15 ` Jacques Garrigue
  0 siblings, 0 replies; 2+ messages in thread
From: Jacques Garrigue @ 2012-02-24  1:15 UTC (permalink / raw)
  To: Jacques Carette; +Cc: caml-list

On 2012/02/24, at 3:53, Jacques Carette wrote:

> What difference is there, if any, between
>  let plus = (+)
> and
>  let plus = fun a b -> a + b
> ?
> 
> I know that, operationally, these are identical.  What I am wondering is, do applications of 'plus' always compile down to the exact same thing?
> 
> In code that I am writing, due to interactions with the value restriction, I have to eta-expand all my definitions.  So I am wondering what cost I will have to pay for the 'genericity' of my code.
> 
> Jacques

For your example: they are identical, since (+) is a primitive, and will be eta-expanded anyway if all arguments are not provided.
But this is is not the case for normal functions: in general eta-expansion is not compiled away, as the function itself might have side-effects in partial applications, so the eta-expanded form might have different semantics.
With ocamlopt inlining may solve that at the call point, but not all functions can be inlined…

Jacques Garrigue

> ocaml -dlambda
        OCaml version 3.13.0+dev11 (2012-01-26)

# let plus = (+);;
(let (plus/1038 (function prim/1040 prim/1039 (+ prim/1040 prim/1039)))
  (apply (field 1 (global Toploop!)) "plus" plus/1038))
val plus : int -> int -> int = <fun>
# let plus a b = a+b;;
(let (plus/1041 (function a/1042 b/1043 (+ a/1042 b/1043)))
  (apply (field 1 (global Toploop!)) "plus" plus/1041))
val plus : int -> int -> int = <fun>
# let plus' a b = plus a b;;
(let
  (plus/1041 (apply (field 0 (global Toploop!)) "plus")
   plus'/1044 (function a/1045 b/1046 (apply plus/1041 a/1045 b/1046)))
  (apply (field 1 (global Toploop!)) "plus'" plus'/1044))
val plus' : int -> int -> int = <fun>



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

end of thread, other threads:[~2012-02-24  1:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-23 18:53 [Caml-list] inlining/eta-expansion question Jacques Carette
2012-02-24  1:15 ` Jacques Garrigue

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