From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 7D3C5BC69 for ; Wed, 4 Jul 2007 09:32:11 +0200 (CEST) Received: from dorado.vub.ac.be (smtp.vub.ac.be [134.184.129.10]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l647WB75018794 for ; Wed, 4 Jul 2007 09:32:11 +0200 Received: from exchange.etro.vub.ac.be (etro10.vub.ac.be [134.184.2.10]) by dorado.vub.ac.be (Postfix) with ESMTP id ACBD94B5 for ; Wed, 4 Jul 2007 09:32:10 +0200 (CEST) Received: from ssel.vub.ac.be ([10.0.4.37]) by exchange.etro.vub.ac.be with Microsoft SMTPSVC(6.0.3790.3959); Wed, 4 Jul 2007 09:32:10 +0200 Received: from localhost (localhost.localdomain [127.0.0.1]) by ssel.vub.ac.be (Postfix) with ESMTP id 67DD6246891E; Wed, 4 Jul 2007 09:32:10 +0200 (CEST) X-Virus-Scanned: amavisd-new at ssel.vub.ac.be Received: from ssel.vub.ac.be ([127.0.0.1]) by localhost (ssel.vub.ac.be [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 9lVmXeT95jtn; Wed, 4 Jul 2007 09:32:04 +0200 (CEST) Received: from [192.168.2.2] (cable-87-244-185-212.upc.chello.be [87.244.185.212]) by ssel.vub.ac.be (Postfix) with ESMTP id F33C6246891D; Wed, 4 Jul 2007 09:32:02 +0200 (CEST) In-Reply-To: <9b415f950707031259p55c7874bq2bf05725d0bb1526@mail.gmail.com> References: <9b415f950707031259p55c7874bq2bf05725d0bb1526@mail.gmail.com> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: Cc: Caml-list Content-Transfer-Encoding: 7bit From: Bruno De Fraine Subject: Re: [Caml-list] problems with the new camlp4 (again) Date: Wed, 4 Jul 2007 09:31:56 +0200 To: Benedikt Grundmann X-Mailer: Apple Mail (2.752.3) X-OriginalArrivalTime: 04 Jul 2007 07:32:10.0517 (UTC) FILETIME=[6EBE5C50:01C7BE0D] X-Miltered: at concorde with ID 468B4CFB.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; camlp:01 grundmann:01 expr:01 expr:01 trivial:01 bool:01 universiteit:01 vub:98 2007,:98 vub:98 char:01 char:01 wrote:01 rec:01 caml-list:01 On 03 Jul 2007, at 21:59, Benedikt Grundmann wrote: > description would be ("Bar", 2) > > generated functions: > > let wrap (a, b) = Bar (a,b) > > let unwrap (Bar (a, b)) = (a, b) Given a function to generate variable names: let var_name = let b = int_of_char 'a' in fun i -> String.make 1 (char_of_int (b+i)) ;; and some copy-pasted auxiliary functions: let rec fold_range f accu l u = if l < u then let u = u - 1 in fold_range f (f accu u) l u else accu ;; let lid_patt _loc n = <:patt< $lid:n$ >> ;; let lid_expr _loc n = <:expr< $lid:n$ >> ;; let tuple_patt _loc = function | [] -> <:patt< () >> | [p] -> p | p::ps -> <:patt< ($p$,$list:ps$) >> ;; let expr_of_cons _loc n es = List.fold_left (fun acc e -> <:expr< $acc$ $e$ >> ) <:expr< $uid:n$ >> es ;; This is quite trivial: let gen_wrap _loc (n,i) = let var_names = fold_range (fun l c -> (var_name c)::l) [] 0 i in let patt = tuple_patt _loc (List.map (lid_patt _loc) var_names) and expr = expr_of_cons _loc n (List.map (lid_expr _loc) var_names) in <:expr< fun $patt$ -> $expr$ >> ;; Test: # let _loc = Loc.ghost in (gen_wrap _loc ("Bar",2) = <:expr< fun (a,b) -> Bar a b >>) ;; - : bool = true I'll leave "gen_unwrap" as an exercise. Regards, Bruno -- Bruno De Fraine Vrije Universiteit Brussel Faculty of Applied Sciences, DINF - SSEL Room 4K208, Pleinlaan 2, B-1050 Brussels tel: +32 (0)2 629 29 75 fax: +32 (0)2 629 28 70 e-mail: Bruno.De.Fraine@vub.ac.be