2 * Copyright (C) 2013 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 (* For general information about camlp4, see:
20 * http://brion.inria.fr/gallium/index.php/Camlp4
22 * For information about quotations, see:
23 * http://brion.inria.fr/gallium/index.php/Quotation
32 let locfail _loc msg = Loc.raise _loc (Failure msg)
34 (* 'expr' is a function expression (RHS of a binding). It has the
35 * form 'fun a -> fun b -> ...'. Return the parameters and the body of
38 let rec function_parameters = function
39 (* fun patt -> expr *)
40 | ExFun (_loc, McArr (_ploc, param, ExNil _, expr)) ->
41 let params, body = function_parameters expr in
42 ((_ploc, param) :: params), body
43 (* patt when expr -> expr *)
44 | ExFun (_loc, McArr (_, _, _, expr)) ->
45 locfail _loc "not supported: goal function uses when-clause"
48 (* Define one or more 'let [rec] goal ... [and ...]' expressions.
50 * 'r' is Some _ if the rec keyword was defined. 'lets' is the list
53 let generate_let_goal _loc (r : rec_flag) (lets : binding) =
54 let autopublish = ref [] in
56 (* lets might be a single binding, or multiple bindings using BiAnd
57 * ('let .. and'). Rewrite each individual goal in the list.
59 let rec rewrite = function
60 | BiNil _loc -> BiNil _loc
62 (* let goal left = ... and right = ... *)
63 | BiAnd (_loc, left, right) ->
64 BiAnd (_loc, rewrite left, rewrite right)
66 (* let goal name = expr *)
67 | BiEq (_loc, PaId (_, (IdLid (_, name))), expr) ->
68 (* Rename the function to goal_<name>. *)
69 let gname = "goal_" ^ name in
71 (* Split the function into parameters and body. *)
72 let params, body = function_parameters expr in
75 locfail _loc "goal must have some parameters; you probably want to put '()' here";
77 (* Is it a "zero-parameters" automatically published goal? What
78 * this really means is it has exactly one unit parameter.
81 | [ _, PaId (_, IdUid (_, "()")) ] ->
82 autopublish := name :: !autopublish
86 (* Put a try-clause around the body. *)
88 (* Define a goal name which the body may use. *)
89 let goalname = $str:name$ in
90 (* Define onsuccess, onrun, onfail functions that the body may call. *)
92 let _on fns f = fns := f :: !fns in
93 let _call_on fns a = List.iter (fun f -> f a) !fns in
96 let onfail, _call_onfails =
97 let fns = ref [] in (_on fns), (_call_on fns)
99 let onrun, _call_onruns =
100 let fns = ref [] in (_on fns), (_call_on fns)
102 let onsuccess, _call_onsuccesses =
103 let fns = ref [] in (_on fns), (_call_on fns)
109 _call_onsuccesses ();
111 (* Avoid a compiler warning: *)
114 (* target() within the body may raise Goal_OK meaning that the
115 * goal should be short-circuited. We return as if it's an
116 * ordinary function exit.
118 | Goal_result Goal_OK ->
119 _call_onsuccesses ();
126 (* Recreate the function with parameters. *)
129 fun (_ploc, param) rest ->
130 ExFun (_loc, McArr (_ploc, param, ExNil _ploc, rest))
133 <:binding< $lid:gname$ = $expr$ >>
135 locfail _loc "cannot parse 'let goal' expression"
137 let lets = rewrite lets in
139 (* let [rec] ... and ... in () *)
140 let stmts = Ast.StVal (_loc, r, lets) in
142 (* Auto-published goals. *)
146 let gname = "goal_" ^ name in
148 let () = publish $str:name$ (
151 Goaljobs.require $lid:gname$
153 failwith (Printf.sprintf "goal '%s' does not take any arguments"
157 StSem (_loc, stmt, publish_name)
160 (* Rewrite 'require (name args...)' as 'require (fun () -> goal_name args)'.
161 * 'expr' is a function call.
163 let generate_require _loc expr =
164 (* Note that 'f a b c' is parsed as '((f a) b) c' so the actually
165 * function name is buried deeply in the tree. Rewrite the name.
167 let rec rewrite = function
168 | ExApp (_loc, ExId (_loc1, IdLid (_loc2, name)), right) ->
169 let gname = "goal_" ^ name in
170 ExApp (_loc, ExId (_loc1, IdLid (_loc2, gname)), right)
171 | ExApp (_loc, (ExApp _ as expr), right) ->
172 ExApp (_loc, rewrite expr, right)
174 locfail _loc "require (...) expression must contain a call to a goal"
176 let expr = rewrite expr in
177 <:expr< Goaljobs.require (fun () -> $expr$) >>
182 GLOBAL: expr str_item;
184 (* Rewrite 'require (name args...)'. *)
185 expr: LEVEL "apply" [
186 [ "require"; e = expr ->
187 generate_require _loc e ]
190 (* "str_item" is a top level statement in an OCaml program. *)
191 str_item: LEVEL "top" [
192 [ "let"; r = opt_rec; "goal"; ls = binding ->
193 generate_let_goal _loc r ls ]