caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Camlp4 help
@ 2009-04-13  0:05 Andre Nathan
  2009-04-13 19:08 ` [Caml-list] " Andre Nathan
  0 siblings, 1 reply; 11+ messages in thread
From: Andre Nathan @ 2009-04-13  0:05 UTC (permalink / raw)
  To: caml-list

Hello

I'm adding support for property testing in OSpec. Currently you can
write a specification like

  forall (list_of int) (fun l -> (List.rev (List.rev l)) should = l)

and it's also possible to add a constraint as in

  forall (list_of int) ~given:(fun l -> List.length l > 0)
         (fun l -> l should match x::xs)

This automatically generates lists of random sizes containing random
elements, and runs the specified property for each of them. I've been
trying to turn this into a syntax extension that would look like

  forall (list_of int) l . (List.rev (List.rev l)) should = l

or

  forall (list_of int) l . List.length l > 0 => l should match x::xs

The best I could to to make this work was forcing parenthesis around the
expression that comes after the dot, with the following rule:

  "forall"; "("; gen = expr; ")"; var = ipatt; "."; OPT "(";
  e1 = expr; OPT ")"; impl = OPT "=>"; e2 = OPT expr ->

With that I can write the two specifications above as

  forall (list_of int) l . ((List.rev (List.rev l)) should = l)

and

  forall (list_of int) l . (List.length l > 0) => l should match x::xs

which is not that bad, but not exactly what I wanted... 

If I simplify the rule above to

  "forall"; "("; gen = expr; ")"; var = ipatt; ".";
  e1 = expr; impl = OPT "=>"; e2 = OPT expr ->

then everything after the dot is bound to e1, even when there's a "=>". 

Is there some other matcher in camlp4 other than "expr" that I could use
for that? If not, is there another way to parse this correctly without
the extra parenthesis?

Thanks in advance,
Andre


^ permalink raw reply	[flat|nested] 11+ messages in thread
* Camlp4 help
@ 2009-03-21  3:41 Andre Nathan
  2009-03-21 15:26 ` [Caml-list] " blue storm
  0 siblings, 1 reply; 11+ messages in thread
From: Andre Nathan @ 2009-03-21  3:41 UTC (permalink / raw)
  To: caml-list

Hello

I'm just beginning with camlp4 here, and I'm stuck with what I think is
a precedence issue. I have the following syntax extension:

open Camlp4.PreCast
open Syntax

let sum = Gram.Entry.mk "sum"

EXTEND Gram
  expr: LEVEL "top"
    [ [ "sum"; "do"; seq = LIST1 sum; "done" ->
        <:expr< do { $list:seq$ } >> ] ]
    ;
  sum:
    [ [ x = expr; "plus"; y = expr ->
        <:expr< $x$ + $y$ >> ] ]
    ;
END

This works fine for something like this:

sum do
  1 plus 2
done

which becomes (1 + 2).

However, it breaks on

sum do
  let a = 1 in
  let b = 2 in
  a plus b
done

because it becomes ((let a = 1 in let b = 2 in a) + b).

How can fix that (allowing "b" to be in scope for the second argument of
"plus")?

Also, sequences of operations don't parse:

sum do
  1 + 2;
  3 + 4
done

gives "Parse error: [sum] or "done" expected (in [expr])"

What am I missing here?

Thanks in advance,
Andre


^ permalink raw reply	[flat|nested] 11+ messages in thread
* camlp4 help
@ 2005-07-15  8:39 Pietro Abate
  2005-07-15 20:24 ` [Caml-list] " Martin Jambon
  2005-07-18 10:12 ` Hendrik Tews
  0 siblings, 2 replies; 11+ messages in thread
From: Pietro Abate @ 2005-07-15  8:39 UTC (permalink / raw)
  To: ocaml ml

Hi all,
I'm having problems (once again) with camlp4...

I'd like to parse and produce a bit of ocaml code out of the
following description :

---------------------
CONNECTIVES
    "_&_",AssocLeft,And ;
    "_v_",AssocLeft,Or
END

TABLEAU

    RULE "And"
    { A & B }
    ----------
      A ; B
    END

END
---------------

The first time around I used quotes around the "{A & B}" and parsed the
expression with a adhoc parser outside camlp4 (and it's all good and
working).  Now I want to remove the quotes tut-cur and do everything
using the pre-processor (quotes make my code quote un-readable).  I've
started writing this code below, but I don't know how to proceed...

The main problem is of course that I've to parse an expression (in rule),
with a generic parser built on the top of the connectives that I've just
declared. Is there a way to tell campl4 : pass everything in between 
RULE and END to a function my_parser ?

I was looking for inspiration in M. Jambon excellent tutorial, but 
without much luck... 

thanks,
p

-----------------------(not tested)
let rule = Grammar.Entry.create Pcaml.gram "rule";;
let connective = Grammar.Entry.create Pcaml.gram "connective";;

type assoc = |AssocLeft |AssocRight |AssocNone ;;

let conntable = Hashtbl.create 15;;

EXTEND
  Pcaml.str_item: [
    "CONNECTIVES"; clist = LIST1 connective SEP ";"; "END" ->
      List.iter Hashtbl.add conntable clist;
      <:str_item< value version = "connectives declared" >>
    |"TABLEAU"; LIST1 rule; "END" ->
        <:str_item< value version = "tableau declared" >>
  ];

  connective: [
    s = STRING; ","; a = UIDENT; ","; r = UIDENT -> (s,a,r)
  ];

  (* how can I write a quotation outside the EXTEND syntax ? *)
  (* how does this quotation looks like ? *)
  (* Quotation.add my_parser ??? *)
  rule: [
    "RULE"; r = my_parser ??? "END" -> do_something r
  ];
  
END
------------------------

-- 
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++ 
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html


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

end of thread, other threads:[~2009-04-13 19:09 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-13  0:05 Camlp4 help Andre Nathan
2009-04-13 19:08 ` [Caml-list] " Andre Nathan
  -- strict thread matches above, loose matches on Subject: below --
2009-03-21  3:41 Andre Nathan
2009-03-21 15:26 ` [Caml-list] " blue storm
2009-03-21 16:14   ` Andre Nathan
2009-03-21 17:24     ` blue storm
2009-03-21 17:26       ` blue storm
2005-07-15  8:39 camlp4 help Pietro Abate
2005-07-15 20:24 ` [Caml-list] " Martin Jambon
2005-07-18 10:12 ` Hendrik Tews
2005-07-19  5:23   ` Pietro Abate
2005-07-20  7:37     ` Hendrik Tews
2005-07-20  9:57       ` Gerd Stolpmann

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