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 UAA08823; Wed, 21 May 2003 20:46:13 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 UAA09119 for ; Wed, 21 May 2003 20:46:12 +0200 (MET DST) Received: from avalon.ens-lsh.fr (mailhost.ens-lsh.fr [193.51.131.2]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h4LIkBT19267 for ; Wed, 21 May 2003 20:46:11 +0200 (MET DST) Received: from avalon.ens-lsh.fr (localhost [127.0.0.1]) by avalon.ens-lsh.fr (8.12.8/8.12.8) with ESMTP id h4LJjMQk008603 for ; Wed, 21 May 2003 20:45:22 +0100 (WEST) Received: from ens-notes1.ens-lsh.fr (ens-notes1.ens-lsh.fr [10.2.1.44]) by avalon.ens-lsh.fr (8.12.8/8.12.8) with ESMTP id h4LJjMV9008600 for ; Wed, 21 May 2003 20:45:22 +0100 (WEST) Received: from mostha ([10.3.20.3]) by ens-notes1.ens-lsh.fr (Lotus Domino Release 5.0.10) with SMTP id 2003052120460964:24965 ; Wed, 21 May 2003 20:46:09 +0200 Date: Wed, 21 May 2003 20:42:30 +0200 From: Damien To: caml-list@inria.fr Subject: Re: [Caml-list] Scanf and objects Message-Id: <20030521204230.0c4ee422.Damien.Pous@ens-lyon.fr> In-Reply-To: <20030521.184812.24056832.debian00@tiscali.be> References: <20030521.184812.24056832.debian00@tiscali.be> Organization: mosthagloups X-Mailer: Sylpheed version 0.8.11claws141 (GTK+ 1.2.10; i386-debian-linux-gnu) Mime-Version: 1.0 X-MIMETrack: Itemize by SMTP Server on ens-notes1/ENS-LSH(Release 5.0.10 |March 22, 2002) at 21/05/2003 20:46:09, Serialize by Router on ens-notes1/ENS-LSH(Release 5.0.10 |March 22, 2002) at 21/05/2003 20:46:10 Content-Type: multipart/mixed; boundary="Multipart_Wed__21_May_2003_20:42:30_+0200_085ccf98" X-Spam: no; 0.00; damien:01 ens-lyon:01 caml-list:01 scanf:01 troestler:01 workarounds:01 iterator:01 fmt:01 bscanf:01 val:01 scanbuf:01 0.,:01 truncate:01 beeing:01 'a-:01 X-Attachments: type="application/octet-stream" name="scanf.ml" name="scanf.ml" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk --Multipart_Wed__21_May_2003_20:42:30_+0200_085ccf98 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII On Wed, 21 May 2003 18:48:12 +0200 (CEST) Christophe TROESTLER wrote: > Hi, > [...] > Now, is it possible to build objects through a format process? More > precisely, I'd like to have a function that takes (something like) a > format string, say "%i %f", and creates an object like > > object > method fold : ('a -> int -> float -> 'a) -> 'a -> 'a > end > > It does not look so easy to me since on one hand you would have to > parametrize the object to be able to build its type incrementally > (e.g. with a (int -> float -> 'a, 'a) type) but that would then forbid > the fold method to be fully polymorphic (not really nice...). Is this > correct? Are there workarounds? it seems to me that's the same problem as # let id x = x in id 5, id "1";; - : int * string = (5, "1") # let err id = id 3, id "3";; Characters 20-23: let err id = id 3, id "3";; ^^^ This expression has type string but is here used with type int let's define the iterator : # let mk ib fmt f = let rec aux acc = try aux (Scanf.bscanf ib fmt (f acc)) with End_of_file -> acc in aux;; val mk : Scanf.Scanning.scanbuf -> ('a, Scanf.Scanning.scanbuf, 'b) format -> ('b -> 'a) -> 'b ->'b= then use it : # let buf() = Scanf.Scanning.from_string "3 4. 5 6. ";; val buf : unit -> Scanf.Scanning.scanbuf = # let fmt: (_,_,_) format = "%i %f " in mk (buf()) fmt (fun acc i f -> float i +. f +. acc) 0., mk (buf()) fmt (fun acc i f -> i + (truncate f) + acc) 0;; - : float * int = (18., 18) # let err ib fmt f g = mk ib fmt f 0, mk ib fmt g "";; Characters 48-50: let err ib fmt f g = mk ib fmt f 0, mk ib fmt g "";; ^^ This expression has type string but is here used with type int then when one try to turn it into an object, we get the same problem : # class ['a] folder ib fmt = object method fold : 'b. ('b -> 'a -> 'b) -> 'b -> 'b = fun f -> mk ib fmt f end;; Characters 85-109: This method has type ('a -> 'b -> 'a) -> 'a -> 'a which is less general than 'c. ('c -> 'b -> 'c) -> 'c -> 'c "fmt" beeing a parameter for the class "folder", he's not able to keep its fully polymorphic type inside the class, and fails to have type "('a->'b,_,'b) format" for all 'b using a functor, I obtained the attached working code but I'm not sure this will really be useful : using the 'mk' function is far simpler... damien --Multipart_Wed__21_May_2003_20:42:30_+0200_085ccf98 Content-Type: application/octet-stream; name="scanf.ml" Content-Disposition: attachment; filename="scanf.ml" Content-Transfer-Encoding: base64 bW9kdWxlIE1LKE06IHNpZyB0eXBlICdhIHQgdmFsIGZtdDogKCdhIHQsXywnYSkgZm9ybWF0IGVu ZCkgPQpzdHJ1Y3QKICBjbGFzcyBmb2xkZXIgaWIgPSBvYmplY3QKICAgIG1ldGhvZCBmb2xkIDog J2IuICgnYiAtPiAnYiBNLnQpIC0+ICdiIC0+ICdiID0gCiAgICAgIGZ1biBmIC0+IAoJbGV0IHJl YyBhdXggYWNjID0gCgkgIHRyeSBhdXggKFNjYW5mLmJzY2FuZiBpYiBNLmZtdCAoZiBhY2MpKQoJ ICB3aXRoIEVuZF9vZl9maWxlIC0+IGFjYwoJaW4gYXV4CQogIGVuZAplbmQKOzsKCm1vZHVsZSBG PU1LKHN0cnVjdCB0eXBlICdhIHQgPSBpbnQgLT4gZmxvYXQgLT4gJ2EgbGV0IChmbXQ6ICgnYSB0 LF8sJ2EpIGZvcm1hdCkgPSAiJWkgJWYgIiBlbmQpCjs7CgpsZXQgYnVmKCkgPSBTY2FuZi5TY2Fu bmluZy5mcm9tX3N0cmluZyAiMyA0LiA1IDYuICIKOzsKCmxldCBvID0gbmV3IEYuZm9sZGVyIChi dWYoKSkgaW4KICBvI2ZvbGQgKGZ1biBhY2MgaSBmIC0+IGZsb2F0IGkgKy4gZiArLiBhY2MpIDAu LAogIG8jZm9sZCAoZnVuIGFjYyBpIGYgLT4gaSArICh0cnVuY2F0ZSBmKSArIGFjYykgMAo7Owo= --Multipart_Wed__21_May_2003_20:42:30_+0200_085ccf98-- ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners