First published version.
[goaljobs.git] / pa_goal.ml
diff --git a/pa_goal.ml b/pa_goal.ml
new file mode 100644 (file)
index 0000000..06b2ec8
--- /dev/null
@@ -0,0 +1,130 @@
+(* goaljobs
+ * Copyright (C) 2013 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(* For general information about camlp4, see:
+ * http://brion.inria.fr/gallium/index.php/Camlp4
+ *
+ * For information about quotations, see:
+ * http://brion.inria.fr/gallium/index.php/Quotation
+ *)
+
+open Printf
+
+open Camlp4.PreCast
+open Syntax
+open Ast
+
+let locfail _loc msg = Loc.raise _loc (Failure msg)
+
+(* 'expr' is a function expression (RHS of a binding).  It has the
+ * form 'fun a -> fun b -> ...'.  Return the parameters and the body of
+ * the function.
+ *)
+let rec function_parameters = function
+  (* fun patt -> expr *)
+  | ExFun (_loc, McArr (_ploc, param, ExNil _, expr)) ->
+    let params, body = function_parameters expr in
+    ((_ploc, param) :: params), body
+  (* patt when expr -> expr *)
+  | ExFun (_loc, McArr (_, _, _, expr)) ->
+    locfail _loc "not supported: goal function uses when-clause"
+  | body -> [], body
+
+(* Define one or more 'let [rec] goal ... [and ...]' expressions.
+ *
+ * 'r' is Some _ if the rec keyword was defined.  'lets' is the list
+ * of let statements.
+ *)
+let generate_let_goal _loc (r : rec_flag) (lets : binding) =
+  (* lets might be a single binding, or multiple bindings using BiAnd
+   * ('let .. and').  Rewrite each individual goal in the list.
+   *)
+  let rec rewrite = function
+    | BiNil _loc -> BiNil _loc
+
+    (* let goal left = ... and right = ... *)
+    | BiAnd (_loc, left, right) ->
+      BiAnd (_loc, rewrite left, rewrite right)
+
+    (* let goal name = expr *)
+    | BiEq (_loc, PaId (_, (IdLid (_, name))), expr) ->
+      (* Rename the function to goal_<name>. *)
+      let gname = "goal_" ^ name in
+
+      (* Split the function into parameters and body. *)
+      let params, body = function_parameters expr in
+
+      if params = [] then
+        locfail _loc "goal must have some parameters; you probably want to put '()' here";
+
+      (* Put a try-clause around the body. *)
+      let body = <:expr< try $body$ with Goal_result Goal_OK -> () >> in
+
+      (* Recreate the function with parameters. *)
+      let expr =
+        List.fold_right (
+          fun (_ploc, param) rest ->
+            ExFun (_loc, McArr (_ploc, param, ExNil _ploc, rest))
+        ) params body in
+
+      <:binding< $lid:gname$ = $expr$ >>
+    | _ ->
+      locfail _loc "cannot parse 'let goal' expression"
+  in
+  let lets = rewrite lets in
+
+  (* let [rec] ... and ... in () *)
+  Ast.StVal (_loc, r, lets)
+
+(* Rewrite 'require (name args...)' as 'require (goal_name args)'.
+ * 'expr' is a function call.
+ *)
+let generate_require _loc expr =
+  (* Note that 'f a b c' is parsed as '((f a) b) c' so the actually
+   * function name is buried deeply in the tree.  Rewrite the name.
+   *)
+  let rec rewrite = function
+    | ExApp (_loc, ExId (_loc1, IdLid (_loc2, name)), right) ->
+      let gname = "goal_" ^ name in
+      ExApp (_loc, ExId (_loc1, IdLid (_loc2, gname)), right)
+    | ExApp (_loc, (ExApp _ as expr), right) ->
+      ExApp (_loc, rewrite expr, right)
+    | _ ->
+      locfail _loc "require (...) expression must contain a call to a goal"
+  in
+  let expr = rewrite expr in
+  <:expr< Goaljobs.require ($expr$) >>
+
+;;
+
+EXTEND Gram
+  GLOBAL: expr str_item;
+
+  (* Rewrite 'require (name args...)'. *)
+  expr: LEVEL "apply" [
+    [ "require"; e = expr ->
+      generate_require _loc e ]
+  ];
+
+  (* "str_item" is a top level statement in an OCaml program. *)
+  str_item: LEVEL "top" [
+    [ "let"; r = opt_rec; "goal"; ls = binding ->
+    generate_let_goal _loc r ls ]
+  ];
+
+END