* [Caml-list] camlp4 and let binding...
@ 2002-11-26 4:29 Pietro Abate
2002-11-26 11:56 ` Daniel de Rauglaudre
0 siblings, 1 reply; 3+ messages in thread
From: Pietro Abate @ 2002-11-26 4:29 UTC (permalink / raw)
To: caml-list
hi all,
I'm having few problems writing an camlp4 grammar for my project.
I would like to rewrite something something like
let rule_1 = x & y ; z => x + y ; z ;;
in a piece of ocaml code like
let rule_r =
let pattern =
let p1 = And(x,y) in
let p2 = Star in
(p1,p2)
in
let action s =
match s with
|And(x,y)::z -> Plus(x,y)::z
|_ -> failwith ("error")
in new rule(pattern,action)
I wrote something that, of course, doesn't work. My major problems are about
the inner let definitions. How can I chain all let defintions in the list ?
EXTEND
str_item: [[
"let"; r = LIDENT; "="; n = complexnum "=>"; d = complexnum ->
let pref = "rule" ^ r in
let definition = n @ (genaction n d) in (****)
<str_item:<
value $lid:pref$ = $list:definition$ in
new rule(pattern,action)
>>
]];;
complexnum: [[
n = LIST1 numerator SEP ';' -> (<:patt< "pattern" >>, n)
]];
numerator: [[
pel = LIST1 pattern SEP ';' -> pel >>
]];
pattern: [[
n = LIDENT -> (<:patt< $genid$>, <expr:< $n$ >) (***)
n1 = LIDENT; '&'; n2 = LIDENT ->
(<:patt< $genid$>, <expr:< And( $n1$ , $n2$ ) > )
n1 = LIDENT; '+'; n2 = LIDENT ->
(<:patt< $genid$>, <expr:< Plus( $n1$ , $n2$ ) > )
]];
END
(***) genid is a simple function that generates unique identifiers...
(****) genaction should be a function that generates the action
(but I still have no idea how to write it...)
tnx for your help,
p
--
pgp key: 1024D/8A091922 2000-10-18 Pietro Abate <abate@discus.anu.edu.au>
Key fingerprint = 5111 D91B 5E0C 5CE6 FDA3 5EF4 6120 E18E 8A09 1922
public key avalaible via public key server at wwwkeys.eu.pgp.net
"Who says that something cannot be done should not interrupt a man who is
doing it." --- Old Chinese Proverb
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] camlp4 and let binding...
2002-11-26 4:29 [Caml-list] camlp4 and let binding Pietro Abate
@ 2002-11-26 11:56 ` Daniel de Rauglaudre
0 siblings, 0 replies; 3+ messages in thread
From: Daniel de Rauglaudre @ 2002-11-26 11:56 UTC (permalink / raw)
To: caml-list
Hi,
On Tue, Nov 26, 2002 at 03:29:09PM +1100, Pietro Abate wrote:
>
> I wrote something that, of course, doesn't work. My major problems are about
> the inner let definitions. How can I chain all let defintions in the list ?
I am not sure I understand what you want to do but this:
> <str_item:<
> value $lid:pref$ = $list:definition$ in
> new rule(pattern,action)
> >>
is syntactically incorrect: you must not put "in" after a "value". If
you want to make a let..in inside (like in your example), you have to
make the variable "definition" a list of (patt * expr) and you can
write:
<str_item:<
value $lid:pref$ =
let $list:definition$ in
new rule(pattern,action)
>>
If "definition" is, for example, the list: [(p1, e1); (p2; e2)] it is
equivalent to:
<str_item:<
value $lid:pref$ =
let $p1$ = $e1$
and $p2$ = $e2$ in
new rule(pattern,action)
>>
If you prefer a "let..in let.. in" instead of "let .. and .. in", you
have to write it explicitely:
<str_item:<
value $lid:pref$ =
let $p1$ = $e1$ in
let $p2$ = $e2$ in
new rule(pattern,action)
>>
If you want that, but you have a list of let..in with an unknown
number of elements, you have to do it with an iterator, e.g.:
List.fold_right (fun (p1, e1) e -> <:expr< let $p1$ = $e1$ in $e$ >>)
list <:expr< new rule(pattern, action) >>
--
Daniel de RAUGLAUDRE
http://cristal.inria.fr/~ddr/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Caml-list] camlp4 and let binding...
@ 2002-11-26 0:07 Pietro Abate
0 siblings, 0 replies; 3+ messages in thread
From: Pietro Abate @ 2002-11-26 0:07 UTC (permalink / raw)
To: caml-list
hi all,
I'm having few problems writing an camlp4 grammar for my project.
I would like to rewrite something something like
let rule_1 = x & y ; z => x + y ; z ;;
in a piece of ocaml code like
let rule_r =
let pattern =
let p1 = And(x,y) in
let p2 = Star in
(p1,p2)
in
let action s =
match s with
|And(x,y)::z -> Plus(x,y)::z
|_ -> failwith ("error")
in new rule(pattern,action)
I wrote something that, of course, doesn't work. My major problems are about
the inner let definitions. How can I chain all let defintions in the list ?
EXTEND
str_item: [[
"let"; r = LIDENT; "="; n = complexnum "=>"; d = complexnum ->
let pref = "rule" ^ r in
let definition = n @ (genaction n d) in (****)
<str_item:<
value $lid:pref$ = $list:definition$ in
new rule(pattern,action)
>>
]];;
complexnum: [[
n = LIST1 numerator SEP ';' -> (<:patt< "pattern" >>, n)
]];
numerator: [[
pel = LIST1 pattern SEP ';' -> pel >>
]];
pattern: [[
n = LIDENT -> (<:patt< $genid$>, <expr:< $n$ >) (***)
n1 = LIDENT; '&'; n2 = LIDENT ->
(<:patt< $genid$>, <expr:< And( $n1$ , $n2$ ) > )
n1 = LIDENT; '+'; n2 = LIDENT ->
(<:patt< $genid$>, <expr:< Plus( $n1$ , $n2$ ) > )
]];
END
(***) genid is a simple function that generates unique identifiers...
(****) genaction should be a function that generates the action
(but I still have no idea how to write it...)
tnx for your help,
p
--
pgp key: 1024D/8A091922 2000-10-18 Pietro Abate <abate@discus.anu.edu.au>
Key fingerprint = 5111 D91B 5E0C 5CE6 FDA3 5EF4 6120 E18E 8A09 1922
public key avalaible via public key server at wwwkeys.eu.pgp.net
"Who says that something cannot be done should not interrupt a man who is
doing it." --- Old Chinese Proverb
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2002-11-26 11:56 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-26 4:29 [Caml-list] camlp4 and let binding Pietro Abate
2002-11-26 11:56 ` Daniel de Rauglaudre
-- strict thread matches above, loose matches on Subject: below --
2002-11-26 0:07 Pietro Abate
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox