From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id A1867BC29 for ; Thu, 10 Aug 2006 00:51:36 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k79MpZep032576 for ; Thu, 10 Aug 2006 00:51:35 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA17350 for ; Thu, 10 Aug 2006 00:51:35 +0200 (MET DST) Received: from hedwig1.umh.ac.be (hedwig2.umh.ac.be [193.190.193.73]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id k79MpYS5001424 for ; Thu, 10 Aug 2006 00:51:34 +0200 Received: from poincare.swapping.umh.ac.be (pri-010.umh.ac.be [10.101.0.10]) by hedwig1.umh.ac.be (8.13.6/8.13.6) with ESMTP id k79Mrkoa2797806; Thu, 10 Aug 2006 00:53:47 +0200 Received: from localhost ([127.0.0.1]) by poincare.swapping.umh.ac.be with esmtp (Exim 4.62) (envelope-from ) id 1GAwtM-0002Pw-LT; Thu, 10 Aug 2006 00:51:12 +0200 Date: Thu, 10 Aug 2006 00:51:12 +0200 (CEST) Message-Id: <20060810.005112.03212332.Christophe.Troestler@umh.ac.be> To: garrigue@math.nagoya-u.ac.jp Cc: caml-list@inria.fr Subject: Re: [Caml-list] format polymorphism & campl4 From: Christophe TROESTLER In-Reply-To: <20060809.164232.15671006.garrigue@math.nagoya-u.ac.jp> References: <20060807.013312.243185382.Christophe.Troestler@umh.ac.be> <20060809.164232.15671006.garrigue@math.nagoya-u.ac.jp> X-Face: #2fb%mPx>rRL@4ff~TVgZ"<[:,oL"`TUEGK/[8/qb58~C>jR(x4A+v/n)7BgpEtIph_neoLKJBq0JBY9:}8v|j Organization: Universite de Mons-Hainaut (http://math.umh.ac.be/an/) X-Spook: NORAD Attorney General Hugo Chavez national information infrastructure Abu Ghraib John Kerry Mahmoud Ahmadinejad rs9512c csystems advisors X-Blessing: Om Ah Hum Vajra Guru Pema Siddhi Hum X-Operating-System: Debian GNU/Linux (http://www.debian.org/) X-Mailer: Mew version 4.2.52 on Emacs 22.0.50 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.1 (www dot roaringpenguin dot com slash mimedefang) X-Miltered: at nez-perce with ID 44DA66F7.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 44DA66F6.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; polymorphism:01 christophe:01 troestler:01 christophe:01 troestler:01 umh:01 fmt:01 fmt:01 printf:01 printf:01 scanf:01 sscanf:01 well-typed:01 scanf:01 camlp:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 On Wed, 09 Aug 2006, Jacques Garrigue wrote: > > > 'a 'b. (string -> 'b, 'a, 'b) format > > It clearly has the second type! Thanks for answering -- I got badly confused by the error message ! (shame on me :) > > type 'a fmt = { fmt: 'b 'c. ('a,'b,'c) format } > > fun (s: _ fmt) -> Printf.printf s.fmt, Scanf.sscanf "string" s.fm > > With your definition of fmt, you are requiring 'c to be independent > from 'a. But If you look at the type of "%s", you see that > 'a = string -> 'c, so that {fmt="%s"} is not well-typed. Well, yes, of course, that's obvious after you stated it! :) > A solution is to define it as > type 'a fmt = {fmt: 'b 'c. ('a -> 'b, 'c, 'b) format} > > The drawback is that only allows formats taking one argument. > One could add definitions for any defined number of arguments, but I > don't see any generic solution. Yeah but I really want to allow as many arguments as the user desires. That's a bit of an annoyance for printf/scanf like schemes: you have to know in advance in how many contexts you may want to simultaneously use an argument of type format in order to put enough type parameters... So I suppose my best luck to do it cleanly is to use camlp4 to make the parameter duplication for the user. Another approach I was thinking about is to define a new type, say type ('a,'b,'c,'d) fmt and have (for the above application) external pr_fmt : (_, _, 'a, 'c) fmt -> ('a, out_channel, 'c) format = "%identity" external sc_fmt : ('a, 'c, _, _) fmt -> ('a, Scanf.Scanning.scanbuf, 'c) format = "%identity" external ( !/ ) : ('a, _, 'b) format -> ('a, 'b, 'a, _) fmt = "%identity" let f s = Printf.printf (pr_fmt s), Scanf.sscanf "string" (sc_fmt s) f !/"%s %u" Now, of course ( !/ ) is not completely doing what I want. If [pr_fmt] and [sc_fmt] are for purely internal use by the library -- thus user only seeing [/!] -- is the above safe? Is there a way using Camlp4 to reinterpret a string in the new type (possibly prefixing it with !/ or another chosen symbol to distinguish it) so that, say, !/"%i %f" becomes (Obj.magic "%i ... %f": (int -> float -> 'a, 'a, int -> float -> 'b, 'b) fmt) ? Thanks for your help, ChriS P.S. Out of curiosity, is there a type system with inference (say, extending the one of OCaml) that can handle this kind of thing cleanly?