English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Upgrading sexplib-2.7.0 to camlp4 3.10
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-05-01 (13:21)
From: Joel Reymont <joelr1@g...>
Subject: Re: Upgrading sexplib-2.7.0 to camlp4 3.10

Thank you for your patience!

As per your suggestion, I converted this

     | <:ctyp< ( $list:tys$ ) >> -> tupl _loc (List.map aux tys)

into this

     | <:ctyp< ( $tup:tys$ ) >> -> tupl _loc (List.map aux  
(list_of_ctyp tys []))

where tupl is actually sexp_of_tuple below.

The ultimate goal is to convert ( t1 * ... * tN ) into a Lisp-style  
(t1, ..., tN).

My next issue is an error in sex_of_tuple:

This expression has type
   (Camlp4.PreCast.Syntax.Ast.patt * Camlp4.PreCast.Syntax.Ast.expr)  
but is here used with type Camlp4.PreCast.Syntax.Ast.binding list

This points at $list:bindings$. I attached mk_bindings at the end, in  
case it's of any use.

How should I rewrite $list:bindings$ here?

I don't understand why $list:bindings$ is assumed to be of type  
binding list when bindings itself is [(patt, expr)].

	Thanks, Joel


   (* Conversion of tuples *)
   let sexp_of_tuple _loc fps =
     let bindings, patts, vars = Gen.mk_bindings _loc fps in
     let expr =
         let $list:bindings$ in
         Sexplib.Sexp.List $Gen.mk_expr_lst _loc vars$
     let matching =
         <:patt< ( $list:patts$ ) >>,
     `Match [matching]

   (* [mk_rev_bindings _loc fps] takes a list of values of the form
      [`Fun fun_expr] and [`Match matching].  [fun_expr] is an  
      denoting a function, and [matching] is a list of bindings. @return
      the tuple [(bindings, patts, var_exprs)], where [bindings] is a  
      of [(pattern, expression)] tuples, [patts] is the list of those
      patterns, and [var_exprs] is the expression (variable) associated
      with each of those patterns.  The resulting lists are reversed. *)
   let mk_rev_bindings _loc fps =
     let coll (i, bindings, patts, vars) fp =
       let name = "v" ^ string_of_int i in
       let var_expr = ide _loc name in
       let expr =
         match fp with
         | `Fun fun_expr -> <:expr< $fun_expr$ $var_expr$ >>
         | `Match matchings ->
             <:expr< match $var_expr$ with [ $list:matchings$ ] >>
       let patt = idp _loc name in
       i - 1, (patt, expr) :: bindings, patt :: patts, var_expr ::  
vars in
     let n = List.length fps in
     let _, bindings, patts, vars =
       List.fold_left coll (n, [], [], []) fps in
     bindings, patts, vars

   (* [mk_bindings _loc fps] same as [mk_rev_bindings] but the resulting
      lists are in order. *)
   let mk_bindings _loc fps = mk_rev_bindings _loc (List.rev fps)