Ignore warnings about immutable strings.
[goaljobs.git] / pa_goal.ml
1 (* goaljobs
2  * Copyright (C) 2013 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  *)
18
19 (* For general information about camlp4, see:
20  * http://brion.inria.fr/gallium/index.php/Camlp4
21  *
22  * For information about quotations, see:
23  * http://brion.inria.fr/gallium/index.php/Quotation
24  *)
25
26 open Printf
27
28 open Camlp4.PreCast
29 open Syntax
30 open Ast
31
32 let locfail _loc msg = Loc.raise _loc (Failure msg)
33
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
36  * the function.
37  *)
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"
46   | body -> [], body
47
48 (* Define one or more 'let [rec] goal ... [and ...]' expressions.
49  *
50  * 'r' is Some _ if the rec keyword was defined.  'lets' is the list
51  * of let statements.
52  *)
53 let generate_let_goal _loc (r : rec_flag) (lets : binding) =
54   let autopublish = ref [] in
55
56   (* lets might be a single binding, or multiple bindings using BiAnd
57    * ('let .. and').  Rewrite each individual goal in the list.
58    *)
59   let rec rewrite = function
60     | BiNil _loc -> BiNil _loc
61
62     (* let goal left = ... and right = ... *)
63     | BiAnd (_loc, left, right) ->
64       BiAnd (_loc, rewrite left, rewrite right)
65
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
70
71       (* Convert loc to string for goalloc. *)
72       let goalloc = Loc.to_string _loc in
73
74       (* Split the function into parameters and body. *)
75       let params, body = function_parameters expr in
76
77       if params = [] then
78         locfail _loc "goal must have some parameters; you probably want to put '()' here";
79
80       (* Is it a "zero-parameters" automatically published goal?  What
81        * this really means is it has exactly one unit parameter.
82        *)
83       (match params with
84       | [ _, PaId (_, IdUid (_, "()")) ] ->
85         autopublish := name :: !autopublish
86       | _ -> ()
87       );
88
89       (* Put a try-clause around the body. *)
90       let body = <:expr<
91         (* Define a goal name which the body may use. *)
92         let goalname = $str:name$ in
93
94         _enter_goal goalname;
95
96         (* Source location. *)
97         let goalloc = $str:goalloc$ in
98
99         (* Define onsuccess, onrun, onfail functions that the body may call. *)
100         let _on, _call_on =
101           let _on fns f = fns := f :: !fns in
102           let _call_on fns a = List.iter (fun f -> f a) !fns in
103           _on, _call_on
104         in
105         let onfail, _call_onfails =
106           let fns = ref [] in (_on fns), (_call_on fns)
107         in
108         let onrun, _call_onruns =
109           let fns = ref [] in (_on fns), (_call_on fns)
110         in
111         let onsuccess, _call_onsuccesses =
112           let fns = ref [] in (_on fns), (_call_on fns)
113         in
114
115         try
116           $body$ ;
117           _call_onruns ();
118           _call_onsuccesses ();
119           _leave_goal goalname;
120
121           (* Avoid a compiler warning: *)
122           ignore (goalname); ignore (goalloc)
123         with
124         (* target() within the body may raise Goal_OK meaning that the
125          * goal should be short-circuited.  We return as if it's an
126          * ordinary function exit.
127          *)
128         | Goal_result Goal_OK ->
129           _leave_goal goalname;
130           _call_onsuccesses ();
131           ()
132         | exn ->
133           _leave_goal goalname;
134           _call_onfails exn;
135           raise exn
136       >> in
137
138       (* Recreate the function with parameters. *)
139       let expr =
140         List.fold_right (
141           fun (_ploc, param) rest ->
142             ExFun (_loc, McArr (_ploc, param, ExNil _ploc, rest))
143         ) params body in
144
145       <:binding< $lid:gname$ = $expr$ >>
146     | _ ->
147       locfail _loc "cannot parse 'let goal' expression"
148   in
149   let lets = rewrite lets in
150
151   (* let [rec] ... and ... in () *)
152   let stmts = Ast.StVal (_loc, r, lets) in
153
154   (* Auto-published goals. *)
155   List.fold_left (
156     fun stmt name ->
157       let publish_name =
158         let gname = "goal_" ^ name in
159         <:str_item<
160           let () = publish $str:name$ (
161             function
162             | [] ->
163               Goaljobs.require $str:name$ $lid:gname$
164             | _ ->
165               failwith (Printf.sprintf "goal '%s' does not take any arguments"
166                           $str:name$);
167           )
168         >> in
169       StSem (_loc, stmt, publish_name)
170   ) stmts !autopublish
171
172 (* Rewrite 'require (name args...)' as 'require (fun () -> goal_name args)'.
173  * 'expr' is a function call.
174  *)
175 let generate_require _loc expr =
176   (* Note that 'f a b c' is parsed as '((f a) b) c' so the actual
177    * function name is buried deeply in the tree.  Rewrite the name.
178    *)
179   let rec rewrite = function
180     | ExApp (_loc, ExId (_loc1, IdLid (_loc2, name)), right) ->
181       let gname = "goal_" ^ name in
182       ExApp (_loc, ExId (_loc1, IdLid (_loc2, gname)), right), name
183
184     | ExApp (_loc, (ExApp _ as expr), right) ->
185       let expr, name = rewrite expr in
186       ExApp (_loc, expr, right), name
187
188     | _ ->
189       locfail _loc "require (...) expression must contain a call to a goal"
190   in
191   let expr, name = rewrite expr in
192   <:expr< Goaljobs.require $str:name$ (fun () -> $expr$) >>
193
194 ;;
195
196 EXTEND Gram
197   GLOBAL: expr str_item;
198
199   (* Rewrite 'require (name args...)'. *)
200   expr: LEVEL "apply" [
201     [ "require"; e = expr ->
202       generate_require _loc e ]
203   ];
204
205   (* "str_item" is a top level statement in an OCaml program. *)
206   str_item: LEVEL "top" [
207     [ "let"; r = opt_rec; "goal"; ls = binding ->
208     generate_let_goal _loc r ls ]
209   ];
210
211 END