From: "Benedikt Grundmann" <benedikt-grundmann@web.de>
To: Caml-list <caml-list@inria.fr>
Subject: problems with the new camlp4 (again)
Date: Tue, 3 Jul 2007 21:59:46 +0200 [thread overview]
Message-ID: <9b415f950707031259p55c7874bq2bf05725d0bb1526@mail.gmail.com> (raw)
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)
next reply other threads:[~2007-07-03 19:59 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-07-03 19:59 Benedikt Grundmann [this message]
2007-07-04 7:31 ` [Caml-list] " Bruno De Fraine
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=9b415f950707031259p55c7874bq2bf05725d0bb1526@mail.gmail.com \
--to=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