23930
|
1 |
(* Title: Tools/Nbe/Nbe_Eval.ML
|
|
2 |
ID: $Id$
|
|
3 |
Authors: Klaus Aehlig, LMU Muenchen; Tobias Nipkow, Florian Haftmann, TU Muenchen
|
|
4 |
|
|
5 |
Evaluation mechanisms for normalization by evaluation.
|
|
6 |
*)
|
|
7 |
|
|
8 |
(*
|
|
9 |
FIXME:
|
23998
|
10 |
- get rid of BVar (?) - it is only used for terms to be evaluated, not for functions
|
|
11 |
- proper purge operation - preliminary for...
|
|
12 |
- really incremental code generation
|
23930
|
13 |
*)
|
|
14 |
|
|
15 |
signature NBE_EVAL =
|
|
16 |
sig
|
|
17 |
datatype Univ =
|
23998
|
18 |
Const of string * Univ list (*uninterpreted constants*)
|
|
19 |
| Free of string * Univ list (*free (uninterpreted) variables*)
|
23930
|
20 |
| BVar of int * Univ list (*bound named variables*)
|
23998
|
21 |
| Abs of (int * (Univ list -> Univ)) * Univ list
|
|
22 |
(*abstractions as functions*)
|
23930
|
23 |
val apply: Univ -> Univ -> Univ
|
|
24 |
|
23998
|
25 |
val univs_ref: Univ list ref
|
23930
|
26 |
val lookup_fun: CodegenNames.const -> Univ
|
|
27 |
|
|
28 |
(*preconditions: no Vars/TVars in term*)
|
23998
|
29 |
val eval: theory -> term -> term
|
23930
|
30 |
|
|
31 |
val trace: bool ref
|
|
32 |
end;
|
|
33 |
|
|
34 |
structure Nbe_Eval: NBE_EVAL =
|
|
35 |
struct
|
|
36 |
|
|
37 |
|
|
38 |
(* generic non-sense *)
|
|
39 |
|
|
40 |
val trace = ref false;
|
|
41 |
fun tracing f x = if !trace then (Output.tracing (f x); x) else x;
|
|
42 |
|
|
43 |
(** the semantical universe **)
|
|
44 |
|
|
45 |
(*
|
|
46 |
Functions are given by their semantical function value. To avoid
|
|
47 |
trouble with the ML-type system, these functions have the most
|
|
48 |
generic type, that is "Univ list -> Univ". The calling convention is
|
|
49 |
that the arguments come as a list, the last argument first. In
|
|
50 |
other words, a function call that usually would look like
|
|
51 |
|
|
52 |
f x_1 x_2 ... x_n or f(x_1,x_2, ..., x_n)
|
|
53 |
|
|
54 |
would be in our convention called as
|
|
55 |
|
|
56 |
f [x_n,..,x_2,x_1]
|
|
57 |
|
|
58 |
Moreover, to handle functions that are still waiting for some
|
|
59 |
arguments we have additionally a list of arguments collected to far
|
|
60 |
and the number of arguments we're still waiting for.
|
|
61 |
|
|
62 |
(?) Finally, it might happen, that a function does not get all the
|
23998
|
63 |
arguments it needs. In this case the function must provide means to
|
23930
|
64 |
present itself as a string. As this might be a heavy-wight
|
|
65 |
operation, we delay it. (?)
|
|
66 |
*)
|
|
67 |
|
|
68 |
datatype Univ =
|
23998
|
69 |
Const of string * Univ list (*named constructors*)
|
|
70 |
| Free of string * Univ list (*free variables*)
|
23930
|
71 |
| BVar of int * Univ list (*bound named variables*)
|
23998
|
72 |
| Abs of (int * (Univ list -> Univ)) * Univ list
|
23930
|
73 |
(*functions*);
|
|
74 |
|
23998
|
75 |
fun apply (Abs ((1, f), xs)) x = f (x :: xs)
|
|
76 |
| apply (Abs ((n, f), xs)) x = Abs ((n - 1, f), x :: xs)
|
|
77 |
| apply (Const (name, args)) x = Const (name, x :: args)
|
|
78 |
| apply (Free (name, args)) x = Free (name, x :: args)
|
23930
|
79 |
| apply (BVar (name, args)) x = BVar (name, x :: args);
|
|
80 |
|
|
81 |
|
|
82 |
(** global functions **)
|
|
83 |
|
23998
|
84 |
structure Nbe_Functions = CodeDataFun
|
23930
|
85 |
(struct
|
|
86 |
type T = Univ Symtab.table;
|
|
87 |
val empty = Symtab.empty;
|
|
88 |
fun merge _ = Symtab.merge (K true);
|
|
89 |
fun purge _ _ _ = Symtab.empty;
|
|
90 |
end);
|
|
91 |
|
|
92 |
|
|
93 |
(** sandbox communication **)
|
|
94 |
|
23998
|
95 |
val univs_ref = ref [] : Univ list ref;
|
|
96 |
|
|
97 |
local
|
|
98 |
|
|
99 |
val tab_ref = ref NONE : Univ Symtab.table option ref;
|
23930
|
100 |
|
23998
|
101 |
in
|
|
102 |
|
|
103 |
fun lookup_fun s = case ! tab_ref
|
|
104 |
of NONE => error "compile_univs"
|
|
105 |
| SOME tab => (the o Symtab.lookup tab) s;
|
|
106 |
|
|
107 |
fun compile_univs tab ([], _) = []
|
|
108 |
| compile_univs tab (cs, raw_s) =
|
23930
|
109 |
let
|
|
110 |
val _ = univs_ref := [];
|
|
111 |
val s = "Nbe_Eval.univs_ref := " ^ raw_s;
|
|
112 |
val _ = tracing (fn () => "\n---generated code:\n" ^ s) ();
|
23998
|
113 |
val _ = tab_ref := SOME tab; (*FIXME hide proper*)
|
23930
|
114 |
val _ = use_text "" (Output.tracing o enclose "\n---compiler echo:\n" "\n---\n",
|
23998
|
115 |
Output.tracing o enclose "\n--- compiler echo (with error):\n" "\n---\n")
|
23930
|
116 |
(!trace) s;
|
23998
|
117 |
val _ = tab_ref := NONE;
|
23930
|
118 |
val univs = case !univs_ref of [] => error "compile_univs" | univs => univs;
|
23998
|
119 |
in cs ~~ univs end;
|
23930
|
120 |
|
23998
|
121 |
end; (*local*)
|
23930
|
122 |
|
|
123 |
|
|
124 |
(** printing ML syntax **)
|
|
125 |
|
23998
|
126 |
fun ml_app e1 e2 = "(" ^ e1 ^ " " ^ e2 ^ ")";
|
|
127 |
fun ml_apps s ss = Library.foldl (uncurry ml_app) (s, ss);
|
|
128 |
fun ml_abs v e = "(fn" ^ v ^ " => " ^ e ^ ")";
|
23930
|
129 |
|
23998
|
130 |
fun ml_Val v s = "val " ^ v ^ " = " ^ s;
|
|
131 |
fun ml_Let ds e = "let\n" ^ space_implode "\n" ds ^ " in " ^ e ^ " end";
|
23930
|
132 |
|
23998
|
133 |
val ml_string = ML_Syntax.print_string;
|
|
134 |
fun ml_pair e1 e2 = "(" ^ e1 ^ ", " ^ e2 ^ ")";
|
|
135 |
fun ml_list es = "[" ^ commas es ^ "]";
|
23930
|
136 |
|
23998
|
137 |
fun ml_cases t cs =
|
|
138 |
"(case " ^ t ^ " of " ^ space_implode " |" (map (fn (p, t) => p ^ " => " ^ t) cs) ^ ")";
|
23930
|
139 |
|
23998
|
140 |
fun ml_fundefs ([(name, [([], e)])]) =
|
|
141 |
"val " ^ name ^ " = " ^ e ^ "\n"
|
|
142 |
| ml_fundefs (eqs :: eqss) =
|
23930
|
143 |
let
|
23998
|
144 |
fun fundef (name, eqs) =
|
|
145 |
let
|
|
146 |
fun eqn (es, e) = name ^ " " ^ space_implode " " es ^ " = " ^ e
|
|
147 |
in space_implode "\n | " (map eqn eqs) end;
|
|
148 |
in
|
|
149 |
(prefix "fun " o fundef) eqs :: map (prefix "and " o fundef) eqss
|
|
150 |
|> space_implode "\n"
|
|
151 |
|> suffix "\n"
|
|
152 |
end;
|
23930
|
153 |
|
|
154 |
|
23998
|
155 |
(** nbe specific syntax **)
|
23930
|
156 |
|
|
157 |
local
|
23998
|
158 |
val Eval = "Nbe_Eval.";
|
|
159 |
val Eval_Const = Eval ^ "Const";
|
|
160 |
val Eval_Free = Eval ^ "Free";
|
|
161 |
val Eval_apply = Eval ^ "apply";
|
|
162 |
val Eval_Abs = Eval ^ "Abs";
|
|
163 |
val Eval_lookup_fun = Eval ^ "lookup_fun";
|
23930
|
164 |
in
|
|
165 |
|
23998
|
166 |
fun nbe_const c args = ml_app Eval_Const (ml_pair (ml_string c) (ml_list args));
|
23930
|
167 |
|
23998
|
168 |
fun nbe_fun c = "c_" ^ translate_string (fn "." => "_" | c => c) c;
|
23930
|
169 |
|
23998
|
170 |
fun nbe_free v = ml_app Eval_Free (ml_pair (ml_string v) (ml_list []));
|
23930
|
171 |
|
23998
|
172 |
fun nbe_bound v = "v_" ^ v;
|
23930
|
173 |
|
|
174 |
fun nbe_apps e es =
|
23998
|
175 |
Library.foldr (fn (s, e) => ml_app (ml_app Eval_apply e) s) (es, e);
|
23930
|
176 |
|
23998
|
177 |
fun nbe_abss 0 f = ml_app f (ml_list [])
|
|
178 |
| nbe_abss n f = ml_app Eval_Abs (ml_pair (ml_pair (string_of_int n) f) (ml_list []));
|
23930
|
179 |
|
23998
|
180 |
fun nbe_lookup c = ml_Val (nbe_fun c) (ml_app Eval_lookup_fun (ml_string c));
|
23930
|
181 |
|
23998
|
182 |
val nbe_value = "value";
|
23930
|
183 |
|
|
184 |
end;
|
|
185 |
|
|
186 |
|
23998
|
187 |
(** assembling and compiling of terms etc. **)
|
|
188 |
|
|
189 |
open BasicCodegenThingol;
|
23930
|
190 |
|
23998
|
191 |
(* greetings to Tarski *)
|
|
192 |
|
|
193 |
fun assemble_iterm thy is_fun num_args =
|
23930
|
194 |
let
|
23998
|
195 |
fun of_iterm t =
|
23930
|
196 |
let
|
23998
|
197 |
val (t', ts) = CodegenThingol.unfold_app t
|
|
198 |
in of_itermapp t' (fold (cons o of_iterm) ts []) end
|
|
199 |
and of_itermapp (IConst (c, (dss, _))) ts =
|
|
200 |
(case num_args c
|
23930
|
201 |
of SOME n => if n <= length ts
|
|
202 |
then let val (args2, args1) = chop (length ts - n) ts
|
23998
|
203 |
in nbe_apps (ml_app (nbe_fun c) (ml_list args1)) args2
|
|
204 |
end else nbe_const c ts
|
|
205 |
| NONE => if is_fun c then nbe_apps (nbe_fun c) ts
|
|
206 |
else nbe_const c ts)
|
|
207 |
| of_itermapp (IVar v) ts = nbe_apps (nbe_bound v) ts
|
|
208 |
| of_itermapp ((v, _) `|-> t) ts =
|
|
209 |
nbe_apps (nbe_abss 1 (ml_abs (ml_list [nbe_bound v]) (of_iterm t))) ts
|
|
210 |
| of_itermapp (ICase (((t, _), cs), t0)) ts =
|
|
211 |
nbe_apps (ml_cases (of_iterm t) (map (pairself of_iterm) cs
|
|
212 |
@ [("_", of_iterm t0)])) ts
|
|
213 |
in of_iterm end;
|
23930
|
214 |
|
23998
|
215 |
fun assemble_fun thy is_fun num_args (c, eqns) =
|
23930
|
216 |
let
|
23998
|
217 |
val assemble_arg = assemble_iterm thy (K false) (K NONE);
|
|
218 |
val assemble_rhs = assemble_iterm thy is_fun num_args;
|
23930
|
219 |
fun assemble_eqn (args, rhs) =
|
23998
|
220 |
([ml_list (map assemble_arg (rev args))], assemble_rhs rhs);
|
|
221 |
val default_params = map nbe_bound
|
|
222 |
(Name.invent_list [] "a" ((the o num_args) c));
|
|
223 |
val default_eqn = ([ml_list default_params], nbe_const c default_params);
|
23930
|
224 |
in map assemble_eqn eqns @ [default_eqn] end;
|
|
225 |
|
23998
|
226 |
fun assemble_eqnss thy is_fun [] = ([], "")
|
|
227 |
| assemble_eqnss thy is_fun eqnss =
|
23930
|
228 |
let
|
|
229 |
val cs = map fst eqnss;
|
23998
|
230 |
val num_args = cs ~~ map (fn (_, (args, rhs) :: _) => length args) eqnss;
|
|
231 |
val funs = fold (fold (CodegenThingol.fold_constnames
|
|
232 |
(insert (op =))) o map snd o snd) eqnss [];
|
|
233 |
val bind_funs = map nbe_lookup (filter is_fun funs);
|
|
234 |
val bind_locals = ml_fundefs (map nbe_fun cs ~~ map
|
|
235 |
(assemble_fun thy is_fun (AList.lookup (op =) num_args)) eqnss);
|
|
236 |
val result = ml_list (map (fn (c, n) => nbe_abss n (nbe_fun c)) num_args);
|
|
237 |
in (cs, ml_Let (bind_funs @ [bind_locals]) result) end;
|
23930
|
238 |
|
23998
|
239 |
fun assemble_eval thy is_fun t =
|
|
240 |
let
|
|
241 |
val funs = CodegenThingol.fold_constnames (insert (op =)) t [];
|
|
242 |
val frees = CodegenThingol.fold_unbound_varnames (insert (op =)) t [];
|
|
243 |
val bind_funs = map nbe_lookup (filter is_fun funs);
|
|
244 |
val bind_value = ml_fundefs [(nbe_value, [([ml_list (map nbe_bound frees)],
|
|
245 |
assemble_iterm thy is_fun (K NONE) t)])];
|
|
246 |
val result = ml_list [ml_app nbe_value (ml_list (map nbe_free frees))];
|
|
247 |
in ([nbe_value], ml_Let (bind_funs @ [bind_value]) result) end;
|
23930
|
248 |
|
23998
|
249 |
fun eqns_of_stmt (name, CodegenThingol.Fun ([], _)) =
|
|
250 |
NONE
|
|
251 |
| eqns_of_stmt (name, CodegenThingol.Fun (eqns, _)) =
|
|
252 |
SOME (name, eqns)
|
|
253 |
| eqns_of_stmt (_, CodegenThingol.Datatypecons _) =
|
|
254 |
NONE
|
|
255 |
| eqns_of_stmt (_, CodegenThingol.Datatype _) =
|
|
256 |
NONE
|
|
257 |
| eqns_of_stmt (_, CodegenThingol.Class _) =
|
|
258 |
NONE
|
|
259 |
| eqns_of_stmt (_, CodegenThingol.Classrel _) =
|
|
260 |
NONE
|
|
261 |
| eqns_of_stmt (_, CodegenThingol.Classop _) =
|
|
262 |
NONE
|
|
263 |
| eqns_of_stmt (_, CodegenThingol.Classinst _) =
|
|
264 |
NONE;
|
23930
|
265 |
|
23998
|
266 |
fun compile_stmts thy is_fun =
|
|
267 |
map_filter eqns_of_stmt
|
|
268 |
#> assemble_eqnss thy is_fun
|
|
269 |
#> compile_univs (Nbe_Functions.get thy);
|
|
270 |
|
|
271 |
fun eval_term thy is_fun =
|
|
272 |
assemble_eval thy is_fun
|
|
273 |
#> compile_univs (Nbe_Functions.get thy)
|
|
274 |
#> the_single
|
|
275 |
#> snd;
|
23930
|
276 |
|
|
277 |
|
|
278 |
(* ensure global functions *)
|
|
279 |
|
23998
|
280 |
fun ensure_funs thy code =
|
23930
|
281 |
let
|
23998
|
282 |
fun compile' stmts tab =
|
23930
|
283 |
let
|
23998
|
284 |
val compiled = compile_stmts thy (Symtab.defined tab) stmts;
|
|
285 |
in Nbe_Functions.change thy (fold Symtab.update compiled) end;
|
|
286 |
val nbe_tab = Nbe_Functions.get thy;
|
|
287 |
val stmtss =
|
|
288 |
map (AList.make (Graph.get_node code)) (rev (Graph.strong_conn code))
|
|
289 |
|> (map o filter_out) (Symtab.defined nbe_tab o fst)
|
|
290 |
in fold compile' stmtss nbe_tab end;
|
23930
|
291 |
|
|
292 |
|
|
293 |
(* re-conversion *)
|
|
294 |
|
|
295 |
fun term_of_univ thy t =
|
|
296 |
let
|
|
297 |
fun of_apps bounds (t, ts) =
|
|
298 |
fold_map (of_univ bounds) ts
|
|
299 |
#>> (fn ts' => list_comb (t, rev ts'))
|
23998
|
300 |
and of_univ bounds (Const (name, ts)) typidx =
|
23930
|
301 |
let
|
|
302 |
val SOME (const as (c, _)) = CodegenNames.const_rev thy name;
|
|
303 |
val T = CodegenData.default_typ thy const;
|
|
304 |
val T' = map_type_tvar (fn ((v, i), S) => TypeInfer.param (typidx + i) (v, S)) T;
|
|
305 |
val typidx' = typidx + maxidx_of_typ T' + 1;
|
23998
|
306 |
in of_apps bounds (Term.Const (c, T'), ts) typidx' end
|
|
307 |
| of_univ bounds (Free (name, ts)) typidx =
|
|
308 |
of_apps bounds (Term.Free (name, dummyT), ts) typidx
|
23930
|
309 |
| of_univ bounds (BVar (name, ts)) typidx =
|
|
310 |
of_apps bounds (Bound (bounds - name - 1), ts) typidx
|
23998
|
311 |
| of_univ bounds (t as Abs _) typidx =
|
23930
|
312 |
typidx
|
23998
|
313 |
|> of_univ (bounds + 1) (apply t (BVar (bounds, [])))
|
|
314 |
|-> (fn t' => pair (Term.Abs ("u", dummyT, t')))
|
23930
|
315 |
in of_univ 0 t 0 |> fst end;
|
|
316 |
|
|
317 |
|
|
318 |
(* interface *)
|
|
319 |
|
23998
|
320 |
fun eval thy t =
|
23930
|
321 |
let
|
23998
|
322 |
val (code, t') = CodegenPackage.compile_term thy t;
|
|
323 |
val tab = ensure_funs thy code;
|
|
324 |
val u = eval_term thy (Symtab.defined tab) t';
|
23930
|
325 |
in term_of_univ thy u end;;
|
|
326 |
|
|
327 |
end;
|