<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2009/10/e9ad3c0b25d3628d64545506e6804dda"
  from="Till Varoquaux &lt;till@p...&gt;"
  author="Till Varoquaux"
  date="2009-10-16T15:22:41"
  subject="Re: [Caml-list] How to add () to function parameters"
  prev="2009/10/4def6cb1b0cb5eb0931e86bb5de2c977"
  next="2009/10/29b852a760ba278359c879c0d99a315e"
  prev-in-thread="2009/10/4def6cb1b0cb5eb0931e86bb5de2c977"
  prev-thread="2009/10/6d8097dd50dc4197e62e77c76987d947"
  next-thread="2009/10/05e52ba550a618e67da7434c75334d50"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="How to add () to function parameters">
<msg 
  url="2009/10/67acf39883f97441f08e61ecd49a77e2"
  from="Till Crueger &lt;Till.Crueger@g...&gt;"
  author="Till Crueger"
  date="2009-10-14T10:40:58"
  subject="How to add () to function parameters">
<msg 
  url="2009/10/4def6cb1b0cb5eb0931e86bb5de2c977"
  from="Till Varoquaux &lt;till@p...&gt;"
  author="Till Varoquaux"
  date="2009-10-16T15:15:08"
  subject="Re: [Caml-list] How to add () to function parameters">
<msg 
  url="2009/10/e9ad3c0b25d3628d64545506e6804dda"
  from="Till Varoquaux &lt;till@p...&gt;"
  author="Till Varoquaux"
  date="2009-10-16T15:22:41"
  subject="Re: [Caml-list] How to add () to function parameters">
</msg>
</msg>
</msg>
</thread>

<contents>
Oh, and I nearly forgot: In practice you shouldn't really have that
many functions taking more than 5 unlabeled arguments lying around so
I would bite the bullet and define cps1 through 5 the straightforward
way....

Till

On Fri, Oct 16, 2009 at 11:15 AM, Till Varoquaux &lt;till@pps.jussieu.fr&gt; wrote:
&gt; Well I can basically see two solutions (plus countless complications
&gt; that I won't go into.)
&gt;
&gt; We want to define a function
&gt;
&gt; val cps3: f:('a -&gt; 'b -&gt; 'c -&gt; 'd) -&gt; ('d -&gt; 'e) -&gt; 'a -&gt; 'b -&gt; 'c -&gt; 'e =
&gt;
&gt; that takes a three argument function a returns the same function in CPS style.
&gt;
&gt; The functional unparsing/danvy way [1]:
&gt;
&gt;&gt; let (++) f g = fun x -&gt; f (g x)
&gt;&gt; let i k f arg = k (f arg)
&gt;&gt; let cps ty ~f k = ty k f
&gt;&gt; let cps3 ~f = cps (i++i++i) ~f
&gt;
&gt; brute force style:
&gt;
&gt;&gt; let e acc ~f cont = acc cont f
&gt;&gt; let i = fun acc g -&gt; g (fun cont v arg -&gt; acc cont (v arg))
&gt;&gt; let cps = fun z -&gt;
&gt;&gt;   let acc = (fun cont x -&gt; cont x) in
&gt;&gt;   z acc
&gt;&gt; let cps3 ~f = cps i i i e ~f
&gt;
&gt; The first style is an acquired taste quite the same way that monad
&gt; are. With some getting use to and abstracting your types in a sensible
&gt; way you can enclose things quite nicely and define elegant
&gt; printf/scanf kind of functions.
&gt;
&gt; I strongly discourage you to use the second style. It is a very
&gt; reworked mlton.fold [2] style solution. mlton's fold is a lot more
&gt; esoteric and leads to types that I have never been able to abstract
&gt; properly.
&gt;
&gt; Till
&gt;
&gt; [1] http://www.brics.dk/RS/98/12/
&gt; [2] http://mlton.org/Fold
&gt;
&gt; On Wed, Oct 14, 2009 at 6:44 AM, Till Crueger &lt;Till.Crueger@gmx.net&gt; wrote:
&gt;&gt; Hi,
&gt;&gt;
&gt;&gt; I am looking for a way to add a unit parameter to a function that takes an
&gt;&gt; arbitrary number of parameters. If the number of parameters is known this is
&gt;&gt; fairly easy and I can just do:
&gt;&gt;
&gt;&gt; let lift1 f a =
&gt;&gt;   fun () -&gt;
&gt;&gt;      f a;;
&gt;&gt;
&gt;&gt; let lift2 f a b =
&gt;&gt;   fun () -&gt;
&gt;&gt;     f a b;;
&gt;&gt;
&gt;&gt; (all these create one closure per lifting)
&gt;&gt; etc...
&gt;&gt;
&gt;&gt; However it is a bit of a hassle to have to code each of these lifts... So
&gt;&gt; what I am looking for is a way to extend this pattern to all numbers. So far
&gt;&gt; I got to the point that I can do the following:
&gt;&gt;
&gt;&gt; let lift_once f a =
&gt;&gt;   fun () -&gt;
&gt;&gt;      f a;;
&gt;&gt;
&gt;&gt; let lift_more f a =
&gt;&gt;   fun () -&gt;
&gt;&gt;      f () a;;
&gt;&gt;
&gt;&gt; So for a function f taking two parameters a and b I can do
&gt;&gt;
&gt;&gt; lift_more (lift_once f a) b
&gt;&gt; (two closures created)
&gt;&gt;
&gt;&gt; and for a function taking the parameters a, b and c I can do
&gt;&gt;
&gt;&gt; lift_more (lift_more (lift_once f a) b) c
&gt;&gt; (three closures created)
&gt;&gt;
&gt;&gt; to get the lifted functions.
&gt;&gt;
&gt;&gt; However this solution gets quite ugly with all the parentheses. Also there
&gt;&gt; are a lot of closures being produced and evaluated for any single lifting. I
&gt;&gt; had a look at the Jane Street blog post about variable argument functions
&gt;&gt; (http://ocaml.janestcapital.com/?q=node/22), which seems to do similar
&gt;&gt; things. However I have never been really good with CPS, so I don't know if
&gt;&gt; those techniques can be applied to this problem.
&gt;&gt;
&gt;&gt; Is there any way to do this, which does not get this ugly. Also the
&gt;&gt; resulting lifted function should not contain too many closures.
&gt;&gt;
&gt;&gt; Thanks for your help,
&gt;&gt;   Till
&gt;&gt;
&gt;&gt; _______________________________________________
&gt;&gt; Caml-list mailing list. Subscription management:
&gt;&gt; http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
&gt;&gt; Archives: http://caml.inria.fr
&gt;&gt; Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
&gt;&gt; Bug reports: http://caml.inria.fr/bin/caml-bugs
&gt;&gt;
&gt;

</contents>

</message>

