1 (* Goalfile Abstract Syntax Tree
2 * Copyright (C) 2019 Richard W.M. Jones
3 * Copyright (C) 2019 Red Hat Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 include Map.Make (String)
29 List.fold_left (fun env (k, v) -> add k v env) env (bindings env')
32 type loc = position * position
33 let noloc = dummy_pos, dummy_pos
35 let string_loc () loc =
37 sprintf "%s:%d:%d" pos.pos_fname pos.pos_lnum (pos.pos_cnum - pos.pos_bol)
38 let print_loc fp loc =
39 fprintf fp "%s" (string_loc () loc)
43 | PTactic of loc * id * substs list
45 | EGoalDefn of loc * goal
46 | EFuncDefn of loc * func
47 | ETacticDefn of loc * tactic
48 | ECall of loc * id * expr list
49 | ETacticCtor of loc * id * expr list
51 | EList of loc * expr list
52 | ESubsts of loc * substs
53 | EConstant of loc * constant
56 and goal = param_decl list * pattern list * expr list * code option
57 and func = param_decl list * code
58 and tactic = param_decl list * code
62 and substs = subst list
67 let getvar env loc name =
70 failwithf "%a: variable ‘%s’ not found" string_loc loc name
72 let getgoal env loc name =
76 failwithf "%a: goal ‘%s’ not found" string_loc loc name in
79 | EGoalDefn (loc, goal) -> goal
81 failwithf "%a: tried to call ‘%s’ which is not a goal"
82 string_loc loc name in
85 let getfunc env loc name =
89 failwithf "%a: func ‘%s’ not found" string_loc loc name in
92 | EFuncDefn (loc, func) -> func
94 failwithf "%a: tried to call ‘%s’ which is not a function"
95 string_loc loc name in
98 let gettactic env loc name =
99 assert (name.[0] = '*');
101 try Env.find name env
103 failwithf "%a: tactic ‘%s’ not found" string_loc loc name in
106 | ETacticDefn (loc, tactic) -> tactic
108 failwithf "%a: tried to call ‘%s’ which is not a tactic"
109 string_loc loc name in
112 module Substs = struct
114 mutable elems : subst list; (* built in reverse order *)
115 curr : Buffer.t; (* current string *)
118 let create () = { elems = []; curr = Buffer.create 13 }
121 if Buffer.length t.curr > 0 then
122 t.elems <- SString (Buffer.contents t.curr) :: t.elems;
125 let get t = finalize t; List.rev t.elems
127 let add_char { curr } = Buffer.add_char curr
128 let add_string { curr} = Buffer.add_string curr
129 let add_var t id = finalize t; t.elems <- SVar id :: t.elems
133 : out_channel -> (out_channel -> 'a -> unit) -> 'a list -> unit =
135 let comma = ref false in
138 if !comma then fprintf fp ", ";
143 let rec string_env () env =
144 let env = Env.bindings env in
145 String.concat "" (List.map (string_def ()) env)
147 and print_env fp env = output_string fp (string_env () env)
149 and string_def () (name, expr) =
151 | EGoalDefn (loc, goal) -> string_goal () (Some name, goal) ^ "\n"
152 | EFuncDefn (loc, func) -> string_func () (Some name, func) ^ "\n"
153 | ETacticDefn (loc, tactic) -> string_tactic () (Some name, tactic) ^ "\n"
154 | expr -> sprintf "let %s = %a\n" name string_expr expr;
156 and print_def fp name expr = output_string fp (string_def () (name, expr))
158 and string_goal () (name, (param_decls, patterns, exprs, code)) =
159 sprintf "goal%s (%s) = %s : %s%s"
160 (match name with None -> "" | Some name -> " " ^ name)
161 (String.concat ", " (List.map (string_param_decl ()) param_decls))
162 (String.concat ", " (List.map (string_pattern ()) patterns))
163 (String.concat ", " (List.map (string_expr ()) exprs))
164 (match code with None -> "" | Some code -> " = { ... }")
166 and string_func () (name, (param_decls, code)) =
167 sprintf "function%s (%s) = { ... }"
168 (match name with None -> "" | Some name -> " " ^ name)
169 (String.concat ", " (List.map (string_param_decl ()) param_decls))
171 and string_tactic () (name, (param_decls, code)) =
172 sprintf "tactic%s (%s) = { ... }"
173 (match name with None -> "" | Some name -> " " ^ name)
174 (String.concat ", " (List.map (string_param_decl ()) param_decls))
176 and string_param_decl () name = name
178 and string_pattern () = function
179 | PTactic (loc, name, params) ->
180 sprintf "%s (%s)" name (String.concat ", "
181 (List.map (string_substs ()) params))
183 and print_pattern fp p = output_string fp (string_pattern () p)
185 and string_expr () = function
186 | EGoalDefn (loc, goal) -> string_goal () (None, goal)
187 | EFuncDefn (loc, func) -> string_func () (None, func)
188 | ETacticDefn (loc, goal) -> string_tactic () (None, goal)
189 | ECall (loc, name, params) ->
191 name (String.concat ", " (List.map (string_expr ()) params))
192 | ETacticCtor (loc, name, params) ->
194 name (String.concat ", " (List.map (string_expr ()) params))
195 | EVar (loc, var) -> var
197 sprintf "[%s]" (String.concat ", " (List.map (string_expr ()) xs))
198 | ESubsts (loc, s) -> string_substs () s
199 | EConstant (loc, c) -> string_constant () c
201 and print_expr fp expr = output_string fp (string_expr () expr)
203 and string_constant () = function
204 | CString s -> sprintf "%S" s
206 and print_constant fp c = output_string fp (string_constant () c)
208 and print_id = output_string
210 and string_substs () ss =
214 | SString s -> sprintf "%S" s
217 (String.concat "+" ss)
219 and print_substs fp ss = output_string fp (string_substs () ss)
221 and print_code fp xs =
224 | SString s -> fprintf fp "%s" s
225 | SVar id -> fprintf fp "%%%s" id