caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Good examples for a Camlp4 beginner?
@ 2003-05-30 18:52 Matt Gushee
  2003-05-30 19:13 ` Alexander V. Voinov
  2003-05-30 20:13 ` Basile STARYNKEVITCH
  0 siblings, 2 replies; 4+ messages in thread
From: Matt Gushee @ 2003-05-30 18:52 UTC (permalink / raw)
  To: caml-list

Hello, all--

Over the past couple of weeks I have been learning about the various
OCaml parsing and lexing tools, with an emphasis on Camlp4. It's
fascinating, and I've learned a lot, but I am still having trouble
grasping how the different components fit together. I think what I need
now is to look at some examples of working, real-world code that use
Camlp4 ... something non-trivial, but not enormously complex. Can anyone
suggest a good place to start?

Thanks in advance for any suggestions.

-- 
Matt Gushee                 When a nation follows the Way,
Englewood, Colorado, USA    Horses bear manure through
mgushee@havenrock.com           its fields;
http://www.havenrock.com/   When a nation ignores the Way,
                            Horses bear soldiers through
                                its streets.
                                
                            --Lao Tzu (Peter Merel, trans.)

-------------------
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] 4+ messages in thread

* Re: [Caml-list] Good examples for a Camlp4 beginner?
  2003-05-30 18:52 [Caml-list] Good examples for a Camlp4 beginner? Matt Gushee
@ 2003-05-30 19:13 ` Alexander V. Voinov
  2003-05-30 19:27   ` Matt Gushee
  2003-05-30 20:13 ` Basile STARYNKEVITCH
  1 sibling, 1 reply; 4+ messages in thread
From: Alexander V. Voinov @ 2003-05-30 19:13 UTC (permalink / raw)
  To: Matt Gushee; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 807 bytes --]

Hi Matt,

See a simple extension, which adds some sugar on top of the List module 
functionality. My purpose in this case was to give close analogs to 
Python's loops over sequences (e.g. lists), to facilitate transition. An 
example is attached as well.

Alexander

Matt Gushee wrote:

>Hello, all--
>
>Over the past couple of weeks I have been learning about the various
>OCaml parsing and lexing tools, with an emphasis on Camlp4. It's
>fascinating, and I've learned a lot, but I am still having trouble
>grasping how the different components fit together. I think what I need
>now is to look at some examples of working, real-world code that use
>Camlp4 ... something non-trivial, but not enormously complex. Can anyone
>suggest a good place to start?
>
>Thanks in advance for any suggestions.
>
>  
>


[-- Attachment #2: listsugar.ml --]
[-- Type: text/plain, Size: 957 bytes --]

open Pcaml;;

EXTEND
  GLOBAL: expr;

  expr: 
    [ [ "map"; list = expr; "with"; OPT "|"; clauses = LIST1 clause SEP "|" ->
	  <:expr< List.map (fun [ $list:clauses$ ]) $list$ >> ]

    | [ "iterate"; list = expr; "with"; OPT "|"; clauses = LIST1 clause SEP "|" ->
	  <:expr< List.iter (fun [ $list:clauses$ ]) $list$ >> ]
	
    | [ "foldr"; list = expr; "from"; initval = expr; "with"; OPT "|"; 
	clauses = LIST1 clause SEP "|" ->
	  <:expr< List.fold_right (fun a b -> 
				     match (a, b) with [ $list:clauses$ ]) 
	                           $list$ $initval$ >> ]

    | [ "foldl"; list = expr; "from"; initval = expr; "with"; OPT "|"; 
	clauses = LIST1 clause SEP "|" ->
	  <:expr< List.fold_left (fun a b -> 
				     match (a, b) with [ $list:clauses$ ]) 
	                           $initval$ $list$ >> ]
    ];

  clause:
    [[ p = patt; w = OPT when_expr; "->"; e = expr -> (p, w, e)]];

  when_expr:
    [[ "when"; e = expr -> e ]];
END;;



[-- Attachment #3: testsugar.ml --]
[-- Type: text/plain, Size: 973 bytes --]

open Printf

let _ =
  let values = [1; 2; 3; 4; 5; 6; 7] in

  let vals2 = 
    map values with
      | 1 | 3 | 7 -> 10
      | v when v mod 2 == 0 -> v + 1 
      | v -> v - 1
  in

  let valfmtd = String.concat ", " (map vals2 with v -> sprintf "<%d>" v) in

  let suml, sumsquares, nelem = 
    foldl values from 0.0, 0.0, 0 with 
	(suml0, sumsq0, n), value -> 
	  let fv = float value in 
	  (suml0 +. fv, sumsq0 +. fv *. fv, n + 1) 
  in
  let mean = suml /. float nelem in
  let sdev = sqrt ((sumsquares -. float nelem *. mean *. mean) 
		   /. (float nelem -. 1.0)) in

  let sumr = 
    foldr values from 0.0 with 
	value, sum0 -> (
	  printf "found value = %d, sum0 = %5.2f\n" value sum0;
	  sum0 +. float value
	) 
  in

  printf "%s\n" valfmtd;
  printf "mean: %g, sdev: %g\n" mean sdev;
  printf "sumr: %g\n" sumr;

  iterate values with
    | 1 -> printf "one\n"
    | v when v mod 2 == 0 -> printf "%d is even\n" v
    | v -> printf "%d is odd\n" v


      

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Good examples for a Camlp4 beginner?
  2003-05-30 19:13 ` Alexander V. Voinov
@ 2003-05-30 19:27   ` Matt Gushee
  0 siblings, 0 replies; 4+ messages in thread
From: Matt Gushee @ 2003-05-30 19:27 UTC (permalink / raw)
  To: caml-list

On Fri, May 30, 2003 at 12:13:37PM -0700, Alexander V. Voinov wrote:
> 
> See a simple extension, which adds some sugar on top of the List module 
> functionality. My purpose in this case was to give close analogs to 
> Python's loops over sequences (e.g. lists), to facilitate transition. An 
> example is attached as well.

Yes, that's the kind of thing I'm looking for. Thanks!

-- 
Matt Gushee                 When a nation follows the Way,
Englewood, Colorado, USA    Horses bear manure through
mgushee@havenrock.com           its fields;
http://www.havenrock.com/   When a nation ignores the Way,
                            Horses bear soldiers through
                                its streets.
                                
                            --Lao Tzu (Peter Merel, trans.)

-------------------
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] 4+ messages in thread

* [Caml-list] Good examples for a Camlp4 beginner?
  2003-05-30 18:52 [Caml-list] Good examples for a Camlp4 beginner? Matt Gushee
  2003-05-30 19:13 ` Alexander V. Voinov
@ 2003-05-30 20:13 ` Basile STARYNKEVITCH
  1 sibling, 0 replies; 4+ messages in thread
From: Basile STARYNKEVITCH @ 2003-05-30 20:13 UTC (permalink / raw)
  To: Matt Gushee; +Cc: caml-list

>>>>> "Matt" == Matt Gushee <mgushee@havenrock.com> writes:

    Matt> Hello, all-- Over the past couple of weeks I have been
    Matt> learning about the various OCaml parsing and lexing tools,
    Matt> with an emphasis on Camlp4. It's fascinating, and I've
    Matt> learned a lot, but I am still having trouble grasping how
    Matt> the different components fit together. I think what I need
    Matt> now is to look at some examples of working, real-world code
    Matt> that use Camlp4 ... something non-trivial, but not
    Matt> enormously complex. Can anyone suggest a good place to
    Matt> start?

Yes, you might look into the tracing facilities of Poesia monitor;

get the following files (under CVS) from
http://www2.poesia-filter.org:8000/cgi-bin/cvsweb.cgi/PoesiaSoft/PoesiaMonIcap/

README.trace
pa_trace.ml

trace.ml
trace.mli

In a few words, the pa_trace.ml preprocessing file transform
expressions like
   trace FOO "x=%d y=%d" x y tracend
into
  begin
    if Trace.flags.foo then
        Trace.tracer Trace.FOO  [filename]  [lineno]  [colno]
          (Printf.sprintf "x=%d y=%d" x y
  end

where [filename] is actually the string of the source filename, eg
"foobar.ml", [lineno] is the line number, eg 43, etc.

Even if I coded some camlp4 stuff, I definitely do not claim to be a
camlp4 expert. In particular, I never coded any pretty printer.

Of course you could have a look into the Coq proof system, which
(IIRC) initated the development of camlp4 and which is probably the
major camlp4 user.

Regards.
-- 

Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faïencerie, 92340 Bourg La Reine, France

-------------------
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] 4+ messages in thread

end of thread, other threads:[~2003-05-30 20:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-30 18:52 [Caml-list] Good examples for a Camlp4 beginner? Matt Gushee
2003-05-30 19:13 ` Alexander V. Voinov
2003-05-30 19:27   ` Matt Gushee
2003-05-30 20:13 ` Basile STARYNKEVITCH

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).