author | haftmann |
Fri, 18 Jan 2008 08:30:12 +0100 | |
changeset 25924 | f974a1c64348 |
parent 25865 | a141d6bfd398 |
child 25935 | ce3cd5f0c4ee |
permissions | -rw-r--r-- |
24590 | 1 |
(* Title: Tools/nbe.ML |
24155 | 2 |
ID: $Id$ |
3 |
Authors: Klaus Aehlig, LMU Muenchen; Tobias Nipkow, Florian Haftmann, TU Muenchen |
|
4 |
||
24839 | 5 |
Normalization by evaluation, based on generic code generator. |
24155 | 6 |
*) |
7 |
||
8 |
signature NBE = |
|
9 |
sig |
|
25101 | 10 |
val norm_conv: cterm -> thm |
11 |
val norm_term: theory -> term -> term |
|
12 |
||
25204 | 13 |
datatype Univ = |
24381 | 14 |
Const of string * Univ list (*named (uninterpreted) constants*) |
25924 | 15 |
| Free of string * Univ list (*free (uninterpreted) variables*) |
16 |
| DFree of string * int (*free (uninterpreted) dictionary parameters*) |
|
24155 | 17 |
| BVar of int * Univ list |
18 |
| Abs of (int * (Univ list -> Univ)) * Univ list; |
|
25924 | 19 |
val apps: Univ -> Univ list -> Univ (*explicit applications*) |
25101 | 20 |
val abs: int -> (Univ list -> Univ) -> Univ |
25924 | 21 |
(*abstractions as closures*) |
24155 | 22 |
|
25204 | 23 |
val univs_ref: (unit -> Univ list -> Univ list) option ref |
25924 | 24 |
val norm_invoke: theory -> CodeThingol.code -> term |
25 |
-> CodeThingol.typscheme * CodeThingol.iterm -> string list -> thm |
|
24155 | 26 |
val trace: bool ref |
25924 | 27 |
|
24155 | 28 |
val setup: theory -> theory |
29 |
end; |
|
30 |
||
31 |
structure Nbe: NBE = |
|
32 |
struct |
|
33 |
||
34 |
(* generic non-sense *) |
|
35 |
||
36 |
val trace = ref false; |
|
37 |
fun tracing f x = if !trace then (Output.tracing (f x); x) else x; |
|
38 |
||
39 |
||
40 |
(** the semantical universe **) |
|
41 |
||
42 |
(* |
|
43 |
Functions are given by their semantical function value. To avoid |
|
44 |
trouble with the ML-type system, these functions have the most |
|
45 |
generic type, that is "Univ list -> Univ". The calling convention is |
|
46 |
that the arguments come as a list, the last argument first. In |
|
47 |
other words, a function call that usually would look like |
|
48 |
||
49 |
f x_1 x_2 ... x_n or f(x_1,x_2, ..., x_n) |
|
50 |
||
51 |
would be in our convention called as |
|
52 |
||
53 |
f [x_n,..,x_2,x_1] |
|
54 |
||
55 |
Moreover, to handle functions that are still waiting for some |
|
56 |
arguments we have additionally a list of arguments collected to far |
|
57 |
and the number of arguments we're still waiting for. |
|
58 |
*) |
|
59 |
||
25204 | 60 |
datatype Univ = |
24381 | 61 |
Const of string * Univ list (*named (uninterpreted) constants*) |
24155 | 62 |
| Free of string * Univ list (*free variables*) |
25924 | 63 |
| DFree of string * int (*free (uninterpreted) dictionary parameters*) |
24155 | 64 |
| BVar of int * Univ list (*bound named variables*) |
65 |
| Abs of (int * (Univ list -> Univ)) * Univ list |
|
24381 | 66 |
(*abstractions as closures*); |
24155 | 67 |
|
68 |
(* constructor functions *) |
|
69 |
||
25101 | 70 |
fun abs n f = Abs ((n, f), []); |
25924 | 71 |
fun apps (Abs ((n, f), xs)) ys = let val k = n - length ys in |
72 |
if k = 0 then f (ys @ xs) |
|
73 |
else if k < 0 then |
|
74 |
let val (zs, ws) = chop (~ k) ys |
|
75 |
in apps (f (ws @ xs)) zs end |
|
76 |
else Abs ((k, f), ys @ xs) end (*note: reverse convention also for apps!*) |
|
77 |
| apps (Const (name, xs)) ys = Const (name, ys @ xs) |
|
78 |
| apps (Free (name, xs)) ys = Free (name, ys @ xs) |
|
79 |
| apps (BVar (name, xs)) ys = BVar (name, ys @ xs); |
|
24155 | 80 |
|
25101 | 81 |
(* universe graph *) |
24155 | 82 |
|
25101 | 83 |
type univ_gr = Univ option Graph.T; |
84 |
val compiled : univ_gr -> string -> bool = can o Graph.get_node; |
|
24839 | 85 |
|
24155 | 86 |
|
87 |
(** assembling and compiling ML code from terms **) |
|
88 |
||
89 |
(* abstract ML syntax *) |
|
90 |
||
91 |
infix 9 `$` `$$`; |
|
92 |
fun e1 `$` e2 = "(" ^ e1 ^ " " ^ e2 ^ ")"; |
|
25101 | 93 |
fun e `$$` [] = e |
94 |
| e `$$` es = "(" ^ e ^ " " ^ space_implode " " es ^ ")"; |
|
24590 | 95 |
fun ml_abs v e = "(fn " ^ v ^ " => " ^ e ^ ")"; |
24155 | 96 |
|
97 |
fun ml_cases t cs = |
|
98 |
"(case " ^ t ^ " of " ^ space_implode " | " (map (fn (p, t) => p ^ " => " ^ t) cs) ^ ")"; |
|
99 |
fun ml_Let ds e = "let\n" ^ space_implode "\n" ds ^ " in " ^ e ^ " end"; |
|
100 |
||
101 |
fun ml_list es = "[" ^ commas es ^ "]"; |
|
102 |
||
103 |
fun ml_fundefs ([(name, [([], e)])]) = |
|
104 |
"val " ^ name ^ " = " ^ e ^ "\n" |
|
105 |
| ml_fundefs (eqs :: eqss) = |
|
106 |
let |
|
107 |
fun fundef (name, eqs) = |
|
108 |
let |
|
109 |
fun eqn (es, e) = name ^ " " ^ space_implode " " es ^ " = " ^ e |
|
110 |
in space_implode "\n | " (map eqn eqs) end; |
|
111 |
in |
|
112 |
(prefix "fun " o fundef) eqs :: map (prefix "and " o fundef) eqss |
|
113 |
|> space_implode "\n" |
|
114 |
|> suffix "\n" |
|
115 |
end; |
|
116 |
||
117 |
(* nbe specific syntax *) |
|
118 |
||
119 |
local |
|
120 |
val prefix = "Nbe."; |
|
121 |
val name_const = prefix ^ "Const"; |
|
122 |
val name_abs = prefix ^ "abs"; |
|
25924 | 123 |
val name_apps = prefix ^ "apps"; |
24155 | 124 |
in |
125 |
||
25924 | 126 |
fun nbe_fun' c = "c_" ^ translate_string (fn "." => "_" | c => c) c; |
127 |
val nbe_fun = nbe_fun'; (*FIXME!*) |
|
25101 | 128 |
fun nbe_dict v n = "d_" ^ v ^ "_" ^ string_of_int n; |
24155 | 129 |
fun nbe_bound v = "v_" ^ v; |
25924 | 130 |
val nbe_value = ""; |
24155 | 131 |
|
25924 | 132 |
(*note: these three are the "turning spots" where proper argument order is established!*) |
133 |
fun nbe_apps t [] = t |
|
134 |
| nbe_apps t ts = name_apps `$$` [t, ml_list (rev ts)]; |
|
135 |
fun nbe_apps_local c ts = nbe_fun c `$` ml_list (rev ts); |
|
136 |
fun nbe_apps_constr c ts = |
|
137 |
name_const `$` ("(" ^ ML_Syntax.print_string c ^ ", " ^ ml_list (rev ts) ^ ")"); |
|
138 |
||
24155 | 139 |
|
140 |
fun nbe_abss 0 f = f `$` ml_list [] |
|
25101 | 141 |
| nbe_abss n f = name_abs `$$` [string_of_int n, f]; |
24155 | 142 |
|
143 |
end; |
|
144 |
||
24219 | 145 |
open BasicCodeThingol; |
24155 | 146 |
|
25190 | 147 |
(* sandbox communication *) |
148 |
||
149 |
val univs_ref = ref (NONE : (unit -> Univ list -> Univ list) option); |
|
150 |
||
25204 | 151 |
val compile = |
25190 | 152 |
tracing (fn s => "\n--- code to be evaluated:\n" ^ s) |
25204 | 153 |
#> ML_Context.evaluate |
25190 | 154 |
(Output.tracing o enclose "\n---compiler echo:\n" "\n---\n", |
155 |
Output.tracing o enclose "\n--- compiler echo (with error):\n" "\n---\n") |
|
25204 | 156 |
(!trace) ("Nbe.univs_ref", univs_ref); |
25190 | 157 |
|
25865 | 158 |
(* code generation *) |
24155 | 159 |
|
25924 | 160 |
datatype const_kind = Local of int | Global | Constr; |
25101 | 161 |
|
25924 | 162 |
fun assemble_constapp kind c dss ts = |
163 |
let |
|
164 |
val ts' = (maps o map) (assemble_idict kind) dss @ ts; |
|
165 |
in case kind c |
|
166 |
of Local n => if n <= length ts' |
|
167 |
then let val (ts1, ts2) = chop n ts' |
|
168 |
in nbe_apps (nbe_apps_local c ts1) ts2 |
|
169 |
end else nbe_apps (nbe_abss n (nbe_fun c)) ts' |
|
170 |
| Global => nbe_apps (nbe_fun c) ts' |
|
171 |
| Constr => nbe_apps_constr c ts' |
|
172 |
end |
|
173 |
and assemble_idict kind (DictConst (inst, dss)) = |
|
174 |
assemble_constapp kind inst dss [] |
|
175 |
| assemble_idict kind (DictVar (supers, (v, (n, _)))) = |
|
176 |
fold_rev (fn super => assemble_constapp kind super [] o single) supers (nbe_dict v n); |
|
177 |
||
178 |
fun assemble_iterm kind = |
|
24155 | 179 |
let |
180 |
fun of_iterm t = |
|
181 |
let |
|
24219 | 182 |
val (t', ts) = CodeThingol.unfold_app t |
25924 | 183 |
in of_iapp t' (fold_rev (cons o of_iterm) ts []) end |
184 |
and of_iapp (IConst (c, (dss, _))) ts = assemble_constapp kind c dss ts |
|
24347 | 185 |
| of_iapp (IVar v) ts = nbe_apps (nbe_bound v) ts |
186 |
| of_iapp ((v, _) `|-> t) ts = |
|
24155 | 187 |
nbe_apps (nbe_abss 1 (ml_abs (ml_list [nbe_bound v]) (of_iterm t))) ts |
24347 | 188 |
| of_iapp (ICase (((t, _), cs), t0)) ts = |
24155 | 189 |
nbe_apps (ml_cases (of_iterm t) (map (pairself of_iterm) cs |
190 |
@ [("_", of_iterm t0)])) ts |
|
191 |
in of_iterm end; |
|
192 |
||
25924 | 193 |
fun assemble_eqns kind (c, (vs, eqns)) = |
24155 | 194 |
let |
25924 | 195 |
val dict_args = maps (fn (v, sort) => map_index (nbe_dict v o fst) sort) vs; |
196 |
val assemble_arg = assemble_iterm (K Constr); |
|
197 |
val assemble_rhs = assemble_iterm kind; |
|
24155 | 198 |
fun assemble_eqn (args, rhs) = |
25924 | 199 |
([ml_list (rev (dict_args @ map assemble_arg args))], assemble_rhs rhs); |
200 |
val default_args = dict_args @ map nbe_bound (Name.invent_list [] "a" ((length o fst o hd) eqns)); |
|
201 |
val default_eqn = ([ml_list (rev default_args)], nbe_apps_constr c default_args); |
|
202 |
in (nbe_fun' c, map assemble_eqn eqns @ [default_eqn]) end; |
|
24155 | 203 |
|
25190 | 204 |
fun assemble_eqnss gr deps [] = ([], ("", [])) |
205 |
| assemble_eqnss gr deps eqnss = |
|
24155 | 206 |
let |
207 |
val cs = map fst eqnss; |
|
25101 | 208 |
val num_args = cs ~~ map (fn (_, (vs, (args, rhs) :: _)) => |
209 |
length (maps snd vs) + length args) eqnss; |
|
25924 | 210 |
fun kind c = case AList.lookup (op =) num_args c |
211 |
of SOME n => Local n |
|
212 |
| NONE => if (is_some o Option.join o try (Graph.get_node gr)) c |
|
213 |
then Global else Constr; |
|
25190 | 214 |
val deps' = filter (is_some o Option.join o try (Graph.get_node gr)) deps; |
25924 | 215 |
val bind_deps = ml_list (map nbe_fun' deps'); |
216 |
val bind_locals = ml_fundefs (map (assemble_eqns kind) eqnss); |
|
25190 | 217 |
val result = ml_list (map (fn (c, n) => nbe_abss n (nbe_fun c)) num_args); |
218 |
val arg_deps = map (the o Graph.get_node gr) deps'; |
|
219 |
in (cs, (ml_abs bind_deps (ml_Let [bind_locals] result), arg_deps)) end; |
|
220 |
||
221 |
fun compile_eqnss gr deps eqnss = case assemble_eqnss gr deps eqnss |
|
222 |
of ([], _) => [] |
|
223 |
| (cs, (s, deps)) => cs ~~ compile s deps; |
|
24155 | 224 |
|
25101 | 225 |
fun eqns_of_stmt (_, CodeThingol.Fun (_, [])) = |
226 |
[] |
|
227 |
| eqns_of_stmt (const, CodeThingol.Fun ((vs, _), eqns)) = |
|
228 |
[(const, (vs, map fst eqns))] |
|
229 |
| eqns_of_stmt (_, CodeThingol.Datatypecons _) = |
|
230 |
[] |
|
231 |
| eqns_of_stmt (_, CodeThingol.Datatype _) = |
|
232 |
[] |
|
233 |
| eqns_of_stmt (class, CodeThingol.Class (v, (superclasses, classops))) = |
|
234 |
let |
|
235 |
val names = map snd superclasses @ map fst classops; |
|
236 |
val params = Name.invent_list [] "d" (length names); |
|
237 |
fun mk (k, name) = |
|
238 |
(name, ([(v, [])], |
|
239 |
[([IConst (class, ([], [])) `$$ map IVar params], IVar (nth params k))])); |
|
240 |
in map_index mk names end |
|
241 |
| eqns_of_stmt (_, CodeThingol.Classrel _) = |
|
242 |
[] |
|
243 |
| eqns_of_stmt (_, CodeThingol.Classparam _) = |
|
244 |
[] |
|
245 |
| eqns_of_stmt (inst, CodeThingol.Classinst ((class, (_, arities)), (superinsts, instops))) = |
|
246 |
[(inst, (arities, [([], IConst (class, ([], [])) `$$ |
|
247 |
map (fn (_, (_, (inst, dicts))) => IConst (inst, (dicts, []))) superinsts |
|
248 |
@ map (IConst o snd o fst) instops)]))]; |
|
24155 | 249 |
|
25101 | 250 |
fun compile_stmts stmts_deps = |
251 |
let |
|
252 |
val names = map (fst o fst) stmts_deps; |
|
253 |
val names_deps = map (fn ((name, _), deps) => (name, deps)) stmts_deps; |
|
254 |
val eqnss = maps (eqns_of_stmt o fst) stmts_deps; |
|
25190 | 255 |
val compiled_deps = names_deps |
256 |
|> maps snd |
|
257 |
|> distinct (op =) |
|
258 |
|> subtract (op =) names; |
|
259 |
fun compile gr = eqnss |
|
260 |
|> compile_eqnss gr compiled_deps |
|
261 |
|> rpair gr; |
|
25101 | 262 |
in |
263 |
fold (fn name => Graph.new_node (name, NONE)) names |
|
264 |
#> fold (fn (name, deps) => fold (curry Graph.add_edge name) deps) names_deps |
|
265 |
#> compile |
|
266 |
#-> fold (fn (name, univ) => Graph.map_node name (K (SOME univ))) |
|
267 |
end; |
|
24155 | 268 |
|
25101 | 269 |
fun ensure_stmts code = |
270 |
let |
|
271 |
fun add_stmts names gr = if exists (compiled gr) names then gr else gr |
|
272 |
|> compile_stmts (map (fn name => ((name, Graph.get_node code name), |
|
273 |
Graph.imm_succs code name)) names); |
|
274 |
in fold_rev add_stmts (Graph.strong_conn code) end; |
|
24155 | 275 |
|
25924 | 276 |
fun eval_term gr deps ((vs, ty), t) = |
277 |
let |
|
278 |
val frees = CodeThingol.fold_unbound_varnames (insert (op =)) t [] |
|
279 |
val frees' = map (fn v => Free (v, [])) frees; |
|
280 |
val dict_frees = maps (fn (v, sort) => map_index (curry DFree v o fst) sort) vs; |
|
281 |
in |
|
282 |
(nbe_value, (vs, [(map IVar frees, t)])) |
|
283 |
|> singleton (compile_eqnss gr deps) |
|
284 |
|> snd |
|
285 |
|> (fn t => apps t (rev (dict_frees @ frees'))) |
|
286 |
end; |
|
24155 | 287 |
|
288 |
||
25101 | 289 |
(** evaluation **) |
24155 | 290 |
|
24839 | 291 |
(* reification *) |
24155 | 292 |
|
293 |
fun term_of_univ thy t = |
|
294 |
let |
|
25101 | 295 |
fun take_until f [] = [] |
296 |
| take_until f (x::xs) = if f x then [] else x :: take_until f xs; |
|
297 |
fun is_dict (Const (c, _)) = |
|
298 |
(is_some o CodeName.class_rev thy) c |
|
299 |
orelse (is_some o CodeName.classrel_rev thy) c |
|
300 |
orelse (is_some o CodeName.instance_rev thy) c |
|
301 |
| is_dict (DFree _) = true |
|
302 |
| is_dict _ = false; |
|
24155 | 303 |
fun of_apps bounds (t, ts) = |
304 |
fold_map (of_univ bounds) ts |
|
305 |
#>> (fn ts' => list_comb (t, rev ts')) |
|
306 |
and of_univ bounds (Const (name, ts)) typidx = |
|
307 |
let |
|
25101 | 308 |
val ts' = take_until is_dict ts; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
309 |
val SOME c = CodeName.const_rev thy name; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
310 |
val T = Code.default_typ thy c; |
24155 | 311 |
val T' = map_type_tvar (fn ((v, i), S) => TypeInfer.param (typidx + i) (v, S)) T; |
312 |
val typidx' = typidx + maxidx_of_typ T' + 1; |
|
25101 | 313 |
in of_apps bounds (Term.Const (c, T'), ts') typidx' end |
24155 | 314 |
| of_univ bounds (Free (name, ts)) typidx = |
315 |
of_apps bounds (Term.Free (name, dummyT), ts) typidx |
|
316 |
| of_univ bounds (BVar (name, ts)) typidx = |
|
317 |
of_apps bounds (Bound (bounds - name - 1), ts) typidx |
|
318 |
| of_univ bounds (t as Abs _) typidx = |
|
319 |
typidx |
|
25924 | 320 |
|> of_univ (bounds + 1) (apps t [BVar (bounds, [])]) |
24155 | 321 |
|-> (fn t' => pair (Term.Abs ("u", dummyT, t'))) |
322 |
in of_univ 0 t 0 |> fst end; |
|
323 |
||
25101 | 324 |
(* function store *) |
325 |
||
326 |
structure Nbe_Functions = CodeDataFun |
|
327 |
( |
|
328 |
type T = univ_gr; |
|
329 |
val empty = Graph.empty; |
|
330 |
fun merge _ = Graph.merge (K true); |
|
331 |
fun purge _ NONE _ = Graph.empty |
|
332 |
| purge NONE _ _ = Graph.empty |
|
333 |
| purge (SOME thy) (SOME cs) gr = |
|
334 |
let |
|
335 |
val cs_exisiting = |
|
336 |
map_filter (CodeName.const_rev thy) (Graph.keys gr); |
|
337 |
val dels = (Graph.all_preds gr |
|
338 |
o map (CodeName.const thy) |
|
339 |
o filter (member (op =) cs_exisiting) |
|
340 |
) cs; |
|
341 |
in Graph.del_nodes dels gr end; |
|
342 |
); |
|
343 |
||
344 |
(* compilation, evaluation and reification *) |
|
345 |
||
346 |
fun compile_eval thy code vs_ty_t deps = |
|
25190 | 347 |
vs_ty_t |
348 |
|> eval_term (Nbe_Functions.change thy (ensure_stmts code)) deps |
|
25101 | 349 |
|> term_of_univ thy; |
350 |
||
24155 | 351 |
(* evaluation with type reconstruction *) |
352 |
||
24381 | 353 |
fun eval thy code t vs_ty_t deps = |
24155 | 354 |
let |
24347 | 355 |
val ty = type_of t; |
24155 | 356 |
fun subst_Frees [] = I |
357 |
| subst_Frees inst = |
|
358 |
Term.map_aterms (fn (t as Term.Free (s, _)) => the_default t (AList.lookup (op =) inst s) |
|
359 |
| t => t); |
|
360 |
val anno_vars = |
|
361 |
subst_Frees (map (fn (s, T) => (s, Term.Free (s, T))) (Term.add_frees t [])) |
|
362 |
#> subst_Vars (map (fn (ixn, T) => (ixn, Var (ixn, T))) (Term.add_vars t [])) |
|
24347 | 363 |
fun constrain t = |
24680 | 364 |
singleton (Syntax.check_terms (ProofContext.init thy)) (TypeInfer.constrain ty t); |
24155 | 365 |
fun check_tvars t = if null (Term.term_tvars t) then t else |
366 |
error ("Illegal schematic type variables in normalized term: " |
|
367 |
^ setmp show_types true (Sign.string_of_term thy) t); |
|
25167 | 368 |
val string_of_term = setmp show_types true (Sign.string_of_term thy); |
24155 | 369 |
in |
25101 | 370 |
compile_eval thy code vs_ty_t deps |
25167 | 371 |
|> tracing (fn t => "Normalized:\n" ^ string_of_term t) |
24155 | 372 |
|> anno_vars |
25167 | 373 |
|> tracing (fn t => "Vars typed:\n" ^ string_of_term t) |
24155 | 374 |
|> constrain |
25167 | 375 |
|> tracing (fn t => "Types inferred:\n" ^ string_of_term t) |
25101 | 376 |
|> tracing (fn t => "---\n") |
24155 | 377 |
|> check_tvars |
378 |
end; |
|
379 |
||
380 |
(* evaluation oracle *) |
|
381 |
||
24839 | 382 |
exception Norm of CodeThingol.code * term |
24381 | 383 |
* (CodeThingol.typscheme * CodeThingol.iterm) * string list; |
24155 | 384 |
|
24839 | 385 |
fun norm_oracle (thy, Norm (code, t, vs_ty_t, deps)) = |
24381 | 386 |
Logic.mk_equals (t, eval thy code t vs_ty_t deps); |
24155 | 387 |
|
24839 | 388 |
fun norm_invoke thy code t vs_ty_t deps = |
389 |
Thm.invoke_oracle_i thy "HOL.norm" (thy, Norm (code, t, vs_ty_t, deps)); |
|
24283 | 390 |
(*FIXME get rid of hardwired theory name*) |
24155 | 391 |
|
24839 | 392 |
fun norm_conv ct = |
24155 | 393 |
let |
394 |
val thy = Thm.theory_of_cterm ct; |
|
24381 | 395 |
fun conv code vs_ty_t deps ct = |
24155 | 396 |
let |
397 |
val t = Thm.term_of ct; |
|
24839 | 398 |
in norm_invoke thy code t vs_ty_t deps end; |
24219 | 399 |
in CodePackage.eval_conv thy conv ct end; |
24155 | 400 |
|
24839 | 401 |
fun norm_term thy = |
402 |
let |
|
403 |
fun invoke code vs_ty_t deps t = |
|
404 |
eval thy code t vs_ty_t deps; |
|
405 |
in CodePackage.eval_term thy invoke #> Code.postprocess_term thy end; |
|
406 |
||
24155 | 407 |
(* evaluation command *) |
408 |
||
409 |
fun norm_print_term ctxt modes t = |
|
410 |
let |
|
411 |
val thy = ProofContext.theory_of ctxt; |
|
24839 | 412 |
val t' = norm_term thy t; |
413 |
val ty' = Term.type_of t'; |
|
24634 | 414 |
val p = PrintMode.with_modes modes (fn () => |
24920 | 415 |
Pretty.block [Pretty.quote (Syntax.pretty_term ctxt t'), Pretty.fbrk, |
416 |
Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt ty')]) (); |
|
24155 | 417 |
in Pretty.writeln p end; |
418 |
||
419 |
||
420 |
(** Isar setup **) |
|
421 |
||
24508
c8b82fec6447
replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents:
24493
diff
changeset
|
422 |
fun norm_print_term_cmd (modes, s) state = |
24155 | 423 |
let val ctxt = Toplevel.context_of state |
24508
c8b82fec6447
replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents:
24493
diff
changeset
|
424 |
in norm_print_term ctxt modes (Syntax.read_term ctxt s) end; |
24155 | 425 |
|
24839 | 426 |
val setup = Theory.add_oracle ("norm", norm_oracle) |
24155 | 427 |
|
428 |
local structure P = OuterParse and K = OuterKeyword in |
|
429 |
||
430 |
val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) []; |
|
431 |
||
24867 | 432 |
val _ = |
24155 | 433 |
OuterSyntax.improper_command "normal_form" "normalize term by evaluation" K.diag |
434 |
(opt_modes -- P.typ >> (Toplevel.keep o norm_print_term_cmd)); |
|
435 |
||
436 |
end; |
|
437 |
||
438 |
end; |