Version française
Home     About     Download     Resources     Contact us    
Browse thread
Camlp4 documentation
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christophe TROESTLER <Christophe.Troestler+ocaml@u...>
Subject: Re: Re : [Caml-list] Camlp4 documentation
On Sat, 23 May 2009 13:13:23 +0000, Matthieu Wipliez wrote:
> 
> A tuple is represented as <:expr< ($e$) >> 

Well, at the moment, the better is to complement the information on
the wiki with personal experiments in the toploop (and occasionally
read the Camlp4 sources).  For example, for the above assertion:

  #directory "+camlp4";;
  #load "dynlink.cma";;  (* OCaml >= 3.11.0 *)
  #load "camlp4of.cma";;
  open Camlp4.PreCast;;
  let _loc = Loc.ghost;;
  
  open Syntax;;

  # let f e = <:expr< ($e$) >>;;
  val f : 'a -> 'a = <fun>

With such a signature, you can imagine that f is just the identity
function and you can confirm it with a few experiments.  To match a
tuple, first look how they are represented:

  let e = <:expr< x,y,z >>;;
  val e : Camlp4.PreCast.Syntax.Ast.expr =
    Camlp4.PreCast.Syntax.Ast.ExTup (<abstr>,
     Camlp4.PreCast.Syntax.Ast.ExCom (<abstr>,
      Camlp4.PreCast.Syntax.Ast.ExId (<abstr>,
       Camlp4.PreCast.Syntax.Ast.IdLid (<abstr>, "x")),
      Camlp4.PreCast.Syntax.Ast.ExCom (<abstr>,
       Camlp4.PreCast.Syntax.Ast.ExId (<abstr>,
        Camlp4.PreCast.Syntax.Ast.IdLid (<abstr>, "y")),
       Camlp4.PreCast.Syntax.Ast.ExId (<abstr>,
        Camlp4.PreCast.Syntax.Ast.IdLid (<abstr>, "z")))))
  
Tuples are thus constructed with "ExTup" which is matched by $tup$ :

  # match e with <:expr< $tup:t$ >> -> t;;
  - : Camlp4.PreCast.Syntax.Ast.expr =
  Camlp4.PreCast.Syntax.Ast.ExCom (<abstr>,
   Camlp4.PreCast.Syntax.Ast.ExId (<abstr>,
    Camlp4.PreCast.Syntax.Ast.IdLid (<abstr>, "x")),
   Camlp4.PreCast.Syntax.Ast.ExCom (<abstr>,
    Camlp4.PreCast.Syntax.Ast.ExId (<abstr>,
     Camlp4.PreCast.Syntax.Ast.IdLid (<abstr>, "y")),
    Camlp4.PreCast.Syntax.Ast.ExId (<abstr>,
     Camlp4.PreCast.Syntax.Ast.IdLid (<abstr>, "z"))))

Then you probably want the individual components that are separated by
comas.  You can easily write your own function but looking at the
Camlp4 sources, you will find that it is already provided :

  # match e with <:expr< $tup:t$ >> -> Ast.list_of_expr t [];;
  - : Camlp4.PreCast.Syntax.Ast.expr list =
  [Camlp4.PreCast.Syntax.Ast.ExId (<abstr>,
    Camlp4.PreCast.Syntax.Ast.IdLid (<abstr>, "x"));
   Camlp4.PreCast.Syntax.Ast.ExId (<abstr>,
    Camlp4.PreCast.Syntax.Ast.IdLid (<abstr>, "y"));
   Camlp4.PreCast.Syntax.Ast.ExId (<abstr>,
    Camlp4.PreCast.Syntax.Ast.IdLid (<abstr>, "z"))]

Hope it helps,
C.