Browse thread
[Caml-list] Q: Correct locations for macro camlp4 extensions
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Jan Kybic <kybic@f...> |
| Subject: | [Caml-list] Re: Correct locations for macro camlp4 extensions |
> Which Haskell operator features does this emulate? I know you can make an
> operator without any camlp4 that does right-associative application, basically
What I had in mind is function decomposition '.', you write
( f . g . h ) x instead of h ( g ( f x ) )
The other operator is '$':
f $ g $ h x instead of f ( g ( h x ) )
both can save a lot of parentheses.
As somebody else on the list asked, I am attaching a source code
syntext.ml and an example for the syntax extensions I mentioned in my
previous email ( operator '$', 'repeat'/'until' cycle, 'where' expression, and
generalized 'for' cycle.) Bear in mind, though, that I have not
written any of them and I understand only in general terms how they
work. I am only posting them here so that they are not lost. (I am not
adding pa_macro, since it is a part of the standard distribution.)
Now, the question remains: How do I instrument the code below so that
the ocaml compiler is informed about the correct line numbers?
Jan
--
-------------------------------------------------------------------------
Jan Kybic <kybic@ieee.org> tel. +420 2 2435 7264
or <kybic@fel.cvut.cz>, http://cmp.felk.cvut.cz/~kybic
(** Syntax extensions for Caml programs. To be compiled with
ocamlc -pp "camlp4o pa_extend.cmo q_MLast.cmo" \
-I /usr/lib/ocaml/camlp4 -c syntext.ml
*)
open Pcaml
#load "pa_extend.cmo";;
#load "q_MLast.cmo";;
let expr_where = Grammar.Entry.create gram "expr_where";;
let let_gen = Grammar.Entry.create gram "let_general";;
let gensym =
let cnt = ref 0 in
fun var ->
let x = incr cnt; !cnt in
var ^ "_gensym" ^ string_of_int x
let gen_for loc v iv wh nx e =
let loop_fun = gensym "iter" in
<:expr<
let rec $lid:loop_fun$ $lid:v$ =
if $wh$ then do { $e$; $lid:loop_fun$ $nx$ } else ()
in $lid:loop_fun$ $iv$ >>
EXTEND
(* infix operator $, functional composition *)
expr: AFTER "apply"
[[ f = expr; "$"; g = expr -> <:expr< fun x -> $f$ ($g$ x) >> ]];
(* repeat/until cycle *)
expr: LEVEL "expr1"
[[ "repeat"; e1 = expr; "until"; e2 = expr ->
<:expr< do { $e1$; while not $e2$ do { $e1$ } } >> ]];
(* where expressions *)
expr: BEFORE "top"
[[ e = SELF; (ifRec,l) = expr_where ->
<:expr< let $rec:ifRec!=None$ $list:l$ in $e$ >> ]];
expr_where:
[["where"; _ = OPT "begin"; (ifRec,l) = let_gen; "end" ->
(ifRec,l)
| "where"; "("; (ifRec,l) = let_gen; ")" -> (ifRec,l) ]];
let_gen: [[r = OPT "rec"; l = LIST1 let_binding SEP "and" -> (r,l)]];
expr: LEVEL "expr1"
[ [ "for"; v = LIDENT; iv = expr LEVEL "simple";
wh = expr LEVEL "simple"; nx = expr LEVEL "simple";
"do"; e = expr; "done" ->
gen_for loc v iv wh nx e ] ]
;
END;;
(* examples, how to use the syntax extensions. To be compiled with:
ocamlc -pp "camlp4o ./syntext.cmo" -I /usr/lib/ocaml/camlp4 test.ml\
-o test
*)
let _ =
repeat print_int (z 3) ; until true
where ( z x = (f $ g ) x where
( f x = x + 3 and g x = x + 7 )
)
let _ = for c 0 (c<10) (c+2) do print_int c; done
(* end of examples *)
-------------------
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