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=1.4 required=5.0 tests=SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id AB59EBC69 for ; Tue, 3 Jul 2007 21:59:48 +0200 (CEST) Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.173]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l63JxmFu027801 for ; Tue, 3 Jul 2007 21:59:48 +0200 Received: by ug-out-1314.google.com with SMTP id o2so155207uge for ; Tue, 03 Jul 2007 12:59:48 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=googlemail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition:x-google-sender-auth; b=iTFtNjYhZFJbw8KvXlH636dxZPUEq/lLoSzx1AtNpqgrVmyKVDEHwLu68HqQENxAGZiMth293vMw080SarFg9ksm6TU8ZDVAjX07HLLEpQapYMHUjQ01XLn14abvPMGJ3JmOP6J9ZykfAxyGaHPtRwbE8OdI1fJAEz6wtlQw7gs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=beta; h=received:message-id:date:from:sender:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition:x-google-sender-auth; b=A7o6fMbfpjZkcIr22mRnT3gFDyyOrFfQeWitAoORwyesx6Wdb5fksAkwe/nF0+EhcV/OAKj5bJVSvUGg+6S4yNWziYdtM6uMFfX4zKLuu87Cp43++2Ly4mB69FAekMVFpiRnOhL46jOm5yykiaj08sYc1Jwp2qJhVI9zaKKK2dQ= Received: by 10.78.140.16 with SMTP id n16mr3768686hud.1183492786959; Tue, 03 Jul 2007 12:59:46 -0700 (PDT) Received: by 10.78.192.19 with HTTP; Tue, 3 Jul 2007 12:59:46 -0700 (PDT) Message-ID: <9b415f950707031259p55c7874bq2bf05725d0bb1526@mail.gmail.com> Date: Tue, 3 Jul 2007 21:59:46 +0200 From: "Benedikt Grundmann" Sender: benedikt.grundmann@googlemail.com To: Caml-list Subject: problems with the new camlp4 (again) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Google-Sender-Auth: aaaf6653618931cf X-Miltered: at discorde with ID 468AAAB4.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; grundmann:01 camlp:01 foo:01 arity:01 expr:01 expr:01 syntax:01 cheers:01 calvin:98 calvin:98 assert:01 assert:01 functions:01 constructor:01 constructor:01 Hi everybody, Sorry to bother you again. Here my next problem: Given a description of a constructor with arguments (name and number of arguments), I want to generate a function which given a tuple of that many elements returns the constructor applied to the arguments and another function which given a value constructed by that constructor returns the arguments as a tuple. Example: type foo = Bar of t1 * t2 description would be ("Bar", 2) generated functions: let wrap (a, b) = Bar (a,b) let unwrap (Bar (a, b)) = (a, b) Right now I do that by writing lots of special cases (one for each arity up to a limit). But how can I do it in a generic way? Current solution: let gen_wrap c = let _loc = c.cons_loc in let n = List.length c.cons_args in match n with | 1 -> <:expr< fun a -> $uid:c.cons_name$ a >> | 2 -> <:expr< fun (a, b) -> $uid:c.cons_name$ a b >> | 3 -> <:expr< fun (a, b, c) -> $uid:c.cons_name$ a b c >> | 4 -> <:expr< fun (a, b, c, d) -> $uid:c.cons_name$ a b c d >> | _ -> assert false let gen_unwrap c = let _loc = c.cons_loc in let n = List.length c.cons_args in let case = match n with | 1 -> <:match_case< $uid:c.cons_name$ a -> a >> | 2 -> <:match_case< $uid:c.cons_name$ a b -> (a, b) >> | 3 -> <:match_case< $uid:c.cons_name$ a b c -> (a,b,c) >> | 4 -> <:match_case< $uid:c.cons_name$ a b c d -> (a,b,c,d) >> | _ -> assert false in <:expr< fun c -> match c with [ $case$ | _ -> assert False ] >> You can have a look at the "complete" syntax extension here: http://osprepo.janestcapital.com/trac/osp2007/browser/osp/2007/econcurrency/trunk/src/pa_pickle.ml Thanks in advance for any help :-) Cheers, Bene -- Calvin: I try to make everyone's day a little more surreal. (From Calvin & Hobbes)