From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA07412; Wed, 7 Mar 2001 17:00:11 +0100 (MET) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA07362 for caml-list@pauillac.inria.fr; Wed, 7 Mar 2001 17:00:11 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA29907 for ; Tue, 6 Mar 2001 11:50:34 +0100 (MET) Received: from batman.labri.u-bordeaux.fr (batman.labri.u-bordeaux.fr [147.210.8.5]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f26AoSD17026 for ; Tue, 6 Mar 2001 11:50:28 +0100 (MET) Received: from serveur1-1.labri.u-bordeaux.fr (root@serveur1-1 [147.210.8.170]) by batman.labri.u-bordeaux.fr (8.8.7/8.8.7) with ESMTP id LAA24870; Tue, 6 Mar 2001 11:54:25 +0100 (MET) Received: (from vanicat@localhost) by serveur1-1.labri.u-bordeaux.fr (8.9.3/8.8.8/Debian/GNU) id LAA06890; Tue, 6 Mar 2001 11:50:21 +0100 X-Authentication-Warning: serveur1-1.labri.u-bordeaux.fr: vanicat set sender to vanicat@labri.u-bordeaux.fr using -f To: Chris Hecker Cc: caml-list@inria.fr Subject: Re: [Caml-list] currying... References: <4.3.2.7.2.20010306012957.00c7cf00@shell16.ba.best.com> From: Remi VANICAT In-Reply-To: Chris Hecker's message of "Tue, 06 Mar 2001 01:39:33 -0800" Date: 06 Mar 2001 11:50:21 +0100 Message-ID: User-Agent: Gnus/5.0807 (Gnus v5.8.7) Emacs/20.7 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Chris Hecker writes: > How does caml know when to call a function? For example, say I have: > > val f: int -> int -> int -> unit > > and the definition of f is > > let f x y = Printf.printf "%d %d" x y;Printf.printf "%d" > > so f actually takes two ints, prints them, and then returns a > function that takes an int and returns unit. From the val > declaration above in a .cmi file, how can caml tell the difference > between that f and this one: > > let f x y z = Printf.printf "%d %d %d" x y z > > How does it know "when" to call f, since you need a different number > of parameters for the different definitions? The top f prints x y > when it's called with two parms, so it doesn't wait until all three > parms have been passed. there is a problem here : Printf.printf is a strange function, it process argument one by one. A better vision of the problem can be see by using print_int : # let f x y = print_int x; print_char ' '; print_int y; print_int;; val f : int -> int -> int -> unit = # f 1;; - : int -> int -> unit = # f 1 2;; 1 2- : int -> unit = # f 1 2 3;; 1 23- : unit = () # let f x y z = print_int x; print_char ' '; print_int y; print_int z;; val f : int -> int -> int -> unit = # f 1;; - : int -> int -> unit = # f 1 2;; - : int -> unit = # f 1 2 3;; 1 23- : unit = () in fact, when caml create a function, it build a closure, and the closure contain the information of how many argument it need to be evaluate. > > I have a feeling I'm missing something fundamental here, or else the > definition of a function internally has a field for its arity and it > just partially applies until it reaches the total arity. I thought > I remembered seeing some documentation on this months ago, but I > can't find it now... yes, there is such a field. I've never seen a documentation about it, but it is clear when you read the output of ocaml -dinstr and the byterun/interp.c file of the source code > > It doesn't seem to partially evaluate the function or anything > insane like that. it mostly true, with the remarkable exception of printf -- Rémi Vanicat vanicat@labri.u-bordeaux.fr http://dept-info.labri.u-bordeaux.fr/~vanicat ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr