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.1 required=5.0 tests=AWL,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 D1E9BBC6B for ; Fri, 23 Mar 2007 10:59:29 +0100 (CET) Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.245]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l2N9xSaB009448 for ; Fri, 23 Mar 2007 10:59:29 +0100 Received: by an-out-0708.google.com with SMTP id c24so1151399ana for ; Fri, 23 Mar 2007 02:59:28 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=CjiadEtgqMjKgfKdRVJ81azpD2HC4l51Thu5J3oKneJzvSAqlJiHtDfO7aiKixokGPGVArawtVYL4GyNuVqLh845OpftLeKMsdc29fcYeQF+i27LKA50aBERuzwIe+If+eOnoIQdDQgFVYahYkNh0qE3Rmoj3b110JaYG17YCsk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=ofKl4edsLIzgFx4scNfRakgxZAnet3q247nuLTSDSArb1EgzkhkkxI+PjAZ9d47pwLp25VDnp1DPU304uMN/0iosUkcABGeYxaXlQZDY3MzANccrSthkhRg70ABblZ5hs1od31Vew3V7gRQ381oglB6K6pPmw4xHaiGQFcw9OzU= Received: by 10.100.125.5 with SMTP id x5mr2458183anc.1174643967132; Fri, 23 Mar 2007 02:59:27 -0700 (PDT) Received: by 10.100.111.13 with HTTP; Fri, 23 Mar 2007 02:59:27 -0700 (PDT) Message-ID: <6f9f8f4a0703230259m1807996dib8509d6e5647400@mail.gmail.com> Date: Fri, 23 Mar 2007 10:59:27 +0100 From: "Loup Vaillant" To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Where are the AST specs? In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <6f9f8f4a0703181022q2b9d7f8bt33919e07f3799c65@mail.gmail.com> <95513600703190145h5a0c6202t518ff61fcde83655@mail.gmail.com> <6f9f8f4a0703190217o5a975a78s16fe2be77825af01@mail.gmail.com> <6f9f8f4a0703190353p4d38ea32k30e1ad2a00f70f4@mail.gmail.com> <6f9f8f4a0703190742w3cd42978ib23a78163542729f@mail.gmail.com> <6f9f8f4a0703190908j5e57d1b4l7d1d67ac98a93243@mail.gmail.com> X-j-chkmail-Score: MSGID : 4603A500.000 on discorde : j-chkmail score : X : 0/20 1 0.000 -> 1 X-Miltered: at discorde with ID 4603A500.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; emacs:01 buffer:01 associative:01 associative:01 ocaml:01 syntax:01 camlp:01 camlp:01 cmo:01 def:01 def:01 expr:01 expr:01 cmo:01 wrote:01 Here is the promised snipet of code. this is emacs lisp. One can evaluate these from the *scratch* buffer. (C-xC-e at the end of the expression). Of course, I assume here that '+' accept only two arguments, or else these macros are useless. (Actually, they are.) ; This macro is right associative (defmacro plus (first &rest tail) (if (null tail) first `(+ ,first (plus ,@tail)))) ; This one is left associative (defmacro sulp (first &rest tail) (if (null tail) first `(sulp (+ ,first ,(car tail)) ,@(cdr tail)))) ; some examples (plus 1 2 3 4 5) (sulp 1 2 3 4 5) I have not defined the macro definfing macros (I am no expert), yet I belive they do not take more than six lines each. Note that I will need read macros as well as regulars ones. That is the kind of power I want from Ocaml. If I succeed, I will consider solved (at least for my personnal usage) many syntax problems I read about in this list. 2007/3/19, Nicolas Pouillard : > On 3/19/07, Loup Vaillant wrote: > > 2007/3/19, Nicolas Pouillard : > > > [...camlp4code...] > > > $ cat test_macros.ml > > > let cons x xs = x :: xs;; > > > > > > foldl(( + ), 1, 2, 3, 4);; > > > foldr(cons, 1, 2, 3, []);; > > > > > > $ camlp4o ./macros.cmo test_macros.ml > > > let cons x xs = x :: xs > > > let _ = ((1 + 2) + 3) + 4 > > > let _ = cons 1 (cons 2 (cons 3 [])) > > > > Impressive. > > If it is possible, It would be best would be to be able to write something like: > > (def_foldl + plus) > > > > and then just > > (+ x y z) > > (+ x y z w) > > ... > > The same for cons: > > (def_foldr :: cons) > > > > (:: x y z L) > > (:: x y z t []) > > $ cat macros.ml > open Camlp4.PreCast;; > let foldr_funs = ref [];; > let foldl_funs = ref [];; > AstFilters.register_str_item_filter begin > Ast.map_expr begin function > | <:expr@loc< def_foldr $lid:name$ $e$ >> -> > foldr_funs := (name, e) :: !foldr_funs; <:expr@loc<()>> > | <:expr@loc< def_foldl $lid:name$ $e$ >> -> > foldl_funs := (name, e) :: !foldl_funs; <:expr@loc<()>> > | e -> e > end > end#str_item;; > AstFilters.register_str_item_filter begin > Ast.map_expr begin function > | <:expr@loc< $lid:name$($tup:e$) >> when List.mem_assoc name !foldl_funs -> > let op = List.assoc name !foldl_funs in > let rec foldl = > function > | [] -> assert false > | [x] -> x > | x :: xs -> <:expr@loc< $op$ $foldl xs$ $x$ >> > in foldl (List.rev (Ast.list_of_expr e [])) > | <:expr@loc< $lid:name$($tup:e$) >> when List.mem_assoc name !foldr_funs -> > let op = List.assoc name !foldr_funs in > let rec foldr = > function > | [] -> assert false > | [x] -> x > | x :: xs -> <:expr@loc< $op$ $x$ $foldr xs$ >> > in foldr (Ast.list_of_expr e []) > | e -> e > end > end#str_item;; > > $ cat test_macros.ml > let cons x xs = x :: xs;; > > def_foldl ( !+ ) ( + );; > def_foldr ( !:: ) cons;; > > !+ (1, 2, 3, 4);; > !:: (1, 2, 3, []);; > > $ camlp4of ./macros.cmo test_macros.ml > let cons x xs = x :: xs > let _ = () > let _ = () > let _ = ((1 + 2) + 3) + 4 > let _ = cons 1 (cons 2 (cons 3 [])) > > > > What's wrong with the current anti quotation system? > > Err, actually, nothing... I just found the '$' ugly. But it is one > > character, and I may as well use it "as is". > > I don't like the sacrifice of $ for that purpose but that's just a > matter of taste. > > -- > Nicolas Pouillard >