caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jan Kybic <kybic@fel.cvut.cz>
To: caml-list@pauillac.inria.fr
Subject: [Caml-list] Re:  Correct locations for macro camlp4 extensions
Date: 24 Aug 2004 09:02:29 +0200	[thread overview]
Message-ID: <m23c2d6mt6.fsf_-_@biogw-ii-19.felk.cvut.cz> (raw)
In-Reply-To: <1093281685.412a2795efb43@webmail.fas.harvard.edu>

> 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


  parent reply	other threads:[~2004-08-24 10:27 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-23  9:33 [Caml-list] Q: " Jan Kybic
2004-08-23 10:59 ` skaller
2004-09-03 14:37   ` Yamagata Yoriyuki
2004-09-04  2:12     ` skaller
2004-08-23 17:21 ` Michael Alexander Hamburg
2004-08-23 18:29   ` Richard Jones
2004-08-23 20:33     ` David Brown
2004-08-23 21:53       ` skaller
2004-08-23 23:16     ` Michael Alexander Hamburg
2004-08-24  7:43       ` Marcin 'Qrczak' Kowalczyk
2004-08-24  7:02   ` Jan Kybic [this message]
2004-08-24 16:31     ` [Caml-list] " Michael Alexander Hamburg

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=m23c2d6mt6.fsf_-_@biogw-ii-19.felk.cvut.cz \
    --to=kybic@fel.cvut.cz \
    --cc=caml-list@pauillac.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).