caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Bruno De Fraine <Bruno.De.Fraine@vub.ac.be>
To: Benedikt Grundmann <benedikt-grundmann@web.de>
Cc: Caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] problems with the new camlp4 (again)
Date: Wed, 4 Jul 2007 09:31:56 +0200	[thread overview]
Message-ID: <CBD1F1D1-60C2-4C1F-9572-955FB2D40B48@vub.ac.be> (raw)
In-Reply-To: <9b415f950707031259p55c7874bq2bf05725d0bb1526@mail.gmail.com>


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



      reply	other threads:[~2007-07-04  7:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-03 19:59 Benedikt Grundmann
2007-07-04  7:31 ` Bruno De Fraine [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CBD1F1D1-60C2-4C1F-9572-955FB2D40B48@vub.ac.be \
    --to=bruno.de.fraine@vub.ac.be \
    --cc=benedikt-grundmann@web.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).