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