English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Camlp4 help
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-04-13 (00:00)
From: Andre Nathan <andre@d...>
Subject: Camlp4 help

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


  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)


  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,