caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Jacques Carette <carette@mcmaster.ca>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] inlining/eta-expansion question
Date: Fri, 24 Feb 2012 10:15:44 +0900	[thread overview]
Message-ID: <4EEA9299-CB06-448B-B3D4-70B6F07ED08D@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <4F468B23.6020006@mcmaster.ca>

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>



      reply	other threads:[~2012-02-24  1:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-23 18:53 Jacques Carette
2012-02-24  1:15 ` Jacques Garrigue [this message]

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=4EEA9299-CB06-448B-B3D4-70B6F07ED08D@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=carette@mcmaster.ca \
    /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).