Write 'goaljobs' script (wrapper around the compiler).
[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   (* lets might be a single binding, or multiple bindings using BiAnd
55    * ('let .. and').  Rewrite each individual goal in the list.
56    *)
57   let rec rewrite = function
58     | BiNil _loc -> BiNil _loc
59
60     (* let goal left = ... and right = ... *)
61     | BiAnd (_loc, left, right) ->
62       BiAnd (_loc, rewrite left, rewrite right)
63
64     (* let goal name = expr *)
65     | BiEq (_loc, PaId (_, (IdLid (_, name))), expr) ->
66       (* Rename the function to goal_<name>. *)
67       let gname = "goal_" ^ name in
68
69       (* Split the function into parameters and body. *)
70       let params, body = function_parameters expr in
71
72       if params = [] then
73         locfail _loc "goal must have some parameters; you probably want to put '()' here";
74
75       (* Put a try-clause around the body. *)
76       let body = <:expr< try $body$ with Goal_result Goal_OK -> () >> in
77
78       (* Recreate the function with parameters. *)
79       let expr =
80         List.fold_right (
81           fun (_ploc, param) rest ->
82             ExFun (_loc, McArr (_ploc, param, ExNil _ploc, rest))
83         ) params body in
84
85       <:binding< $lid:gname$ = $expr$ >>
86     | _ ->
87       locfail _loc "cannot parse 'let goal' expression"
88   in
89   let lets = rewrite lets in
90
91   (* let [rec] ... and ... in () *)
92   Ast.StVal (_loc, r, lets)
93
94 (* Rewrite 'require (name args...)' as 'require (goal_name args)'.
95  * 'expr' is a function call.
96  *)
97 let generate_require _loc expr =
98   (* Note that 'f a b c' is parsed as '((f a) b) c' so the actually
99    * function name is buried deeply in the tree.  Rewrite the name.
100    *)
101   let rec rewrite = function
102     | ExApp (_loc, ExId (_loc1, IdLid (_loc2, name)), right) ->
103       let gname = "goal_" ^ name in
104       ExApp (_loc, ExId (_loc1, IdLid (_loc2, gname)), right)
105     | ExApp (_loc, (ExApp _ as expr), right) ->
106       ExApp (_loc, rewrite expr, right)
107     | _ ->
108       locfail _loc "require (...) expression must contain a call to a goal"
109   in
110   let expr = rewrite expr in
111   <:expr< Goaljobs.require ($expr$) >>
112
113 ;;
114
115 EXTEND Gram
116   GLOBAL: expr str_item;
117
118   (* Rewrite 'require (name args...)'. *)
119   expr: LEVEL "apply" [
120     [ "require"; e = expr ->
121       generate_require _loc e ]
122   ];
123
124   (* "str_item" is a top level statement in an OCaml program. *)
125   str_item: LEVEL "top" [
126     [ "let"; r = opt_rec; "goal"; ls = binding ->
127     generate_let_goal _loc r ls ]
128   ];
129
130 END