author | haftmann |
Thu, 24 May 2007 08:37:43 +0200 | |
changeset 23090 | eb3000a5c478 |
parent 22902 | ac833b4bb7ee |
child 23136 | 5a0378eada70 |
permissions | -rw-r--r-- |
20856 | 1 |
(* Title: Pure/nbe.ML |
2 |
ID: $Id$ |
|
19147 | 3 |
Author: Tobias Nipkow, Florian Haftmann, TU Muenchen |
4 |
||
19177 | 5 |
Toplevel theory interface for "normalization by evaluation" |
19147 | 6 |
*) |
7 |
||
19177 | 8 |
signature NBE = |
19147 | 9 |
sig |
20856 | 10 |
(*preconditions: no Vars/TVars in term*) |
20602 | 11 |
val normalization_conv: cterm -> thm |
19962 | 12 |
val lookup: string -> NBE_Eval.Univ |
19795 | 13 |
val update: string * NBE_Eval.Univ -> unit |
20846 | 14 |
val trace: bool ref |
19147 | 15 |
end; |
16 |
||
19177 | 17 |
structure NBE: NBE = |
19147 | 18 |
struct |
19 |
||
20846 | 20 |
val trace = ref false; |
21124 | 21 |
fun tracing f x = if !trace then (Output.tracing (f x); x) else x; |
19968 | 22 |
|
21068 | 23 |
|
24 |
(** data setup **) |
|
25 |
||
26 |
(* preproc and postproc attributes *) |
|
19968 | 27 |
|
20920 | 28 |
structure NBE_Rewrite = TheoryDataFun |
22846 | 29 |
( |
20920 | 30 |
type T = thm list * thm list |
22846 | 31 |
val empty = ([], []); |
20920 | 32 |
val copy = I; |
33 |
val extend = I; |
|
34 |
fun merge _ ((pres1,posts1), (pres2,posts2)) = |
|
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
22213
diff
changeset
|
35 |
(Library.merge Thm.eq_thm (pres1,pres2), Library.merge Thm.eq_thm (posts1,posts2)) |
22846 | 36 |
); |
20920 | 37 |
|
21068 | 38 |
val _ = |
39 |
let |
|
40 |
fun map_data f = Context.mapping (NBE_Rewrite.map f) I; |
|
41 |
fun attr_pre (thy, thm) = |
|
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
22213
diff
changeset
|
42 |
((map_data o apfst) (insert Thm.eq_thm thm) thy, thm) |
21068 | 43 |
fun attr_post (thy, thm) = |
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
22213
diff
changeset
|
44 |
((map_data o apsnd) (insert Thm.eq_thm thm) thy, thm) |
21068 | 45 |
val attr = Attrib.syntax (Scan.lift (Args.$$$ "pre" >> K attr_pre |
46 |
|| Args.$$$ "post" >> K attr_post)); |
|
47 |
in |
|
48 |
Context.add_setup ( |
|
22846 | 49 |
Attrib.add_attributes |
21068 | 50 |
[("normal", attr, "declare rewrite theorems for normalization")] |
51 |
) |
|
52 |
end; |
|
20920 | 53 |
|
22213 | 54 |
fun the_pres thy = |
21068 | 55 |
let |
21506 | 56 |
val ctxt = ProofContext.init thy; |
57 |
val pres = (map (LocalDefs.meta_rewrite_rule ctxt) o fst) (NBE_Rewrite.get thy) |
|
22213 | 58 |
in pres end |
20920 | 59 |
|
60 |
fun apply_posts thy = |
|
21068 | 61 |
let |
21506 | 62 |
val ctxt = ProofContext.init thy; |
63 |
val posts = (map (LocalDefs.meta_rewrite_rule ctxt) o snd) (NBE_Rewrite.get thy) |
|
21708 | 64 |
in MetaSimplifier.rewrite false posts end |
20920 | 65 |
|
22213 | 66 |
(* theorem store *) |
67 |
||
68 |
structure Funcgr = CodegenFuncgrRetrieval ( |
|
69 |
val name = "Pure/nbe_thms"; |
|
70 |
val rewrites = the_pres; |
|
71 |
); |
|
20920 | 72 |
|
21068 | 73 |
(* code store *) |
74 |
||
20846 | 75 |
structure NBE_Data = CodeDataFun |
19147 | 76 |
(struct |
22213 | 77 |
val name = "Pure/mbe"; |
78 |
type T = NBE_Eval.Univ Symtab.table; |
|
79 |
val empty = Symtab.empty; |
|
80 |
fun merge _ = Symtab.merge (K true); |
|
81 |
fun purge _ _ _ = Symtab.empty; |
|
19147 | 82 |
end); |
83 |
||
22213 | 84 |
val _ = Context.add_setup (Funcgr.init #> NBE_Data.init); |
19795 | 85 |
|
86 |
||
21124 | 87 |
(** norm by eval **) |
21068 | 88 |
|
19968 | 89 |
(* sandbox communication *) |
19147 | 90 |
|
19177 | 91 |
val tab : NBE_Eval.Univ Symtab.table ref = ref Symtab.empty; |
92 |
fun lookup s = (the o Symtab.lookup (!tab)) s; |
|
93 |
fun update sx = (tab := Symtab.update sx (!tab)); |
|
94 |
||
20846 | 95 |
local |
96 |
||
21124 | 97 |
(* function generation *) |
20846 | 98 |
|
99 |
fun generate thy funs = |
|
100 |
let |
|
21124 | 101 |
(* FIXME better turn this into a function |
102 |
NBE_Eval.Univ Symtab.table -> NBE_Eval.Univ Symtab.table |
|
103 |
with implicit side effect *) |
|
104 |
fun use_code NONE = () |
|
105 |
| use_code (SOME s) = |
|
21991 | 106 |
(tracing (fn () => "\n---generated code:\n" ^ s) (); |
22144 | 107 |
use_text "" (Output.tracing o enclose "\n---compiler echo:\n" "\n---\n", |
21124 | 108 |
Output.tracing o enclose "\n--- compiler echo (with error!):\n" "\n---\n") |
109 |
(!trace) s); |
|
21991 | 110 |
val _ = tracing (fn () => "new definitions: " ^ (commas o maps (map fst)) funs) (); |
20846 | 111 |
val _ = tab := NBE_Data.get thy;; |
112 |
val _ = Library.seq (use_code o NBE_Codegen.generate thy |
|
113 |
(fn s => Symtab.defined (!tab) s)) funs; |
|
114 |
in NBE_Data.change thy (K (!tab)) end; |
|
115 |
||
22213 | 116 |
fun ensure_funs thy funcgr t = |
20846 | 117 |
let |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22360
diff
changeset
|
118 |
fun consts_of thy t = |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22360
diff
changeset
|
119 |
fold_aterms (fn Const c => cons (CodegenConsts.const_of_cexpr thy c) | _ => I) t [] |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22360
diff
changeset
|
120 |
val consts = consts_of thy t; |
20846 | 121 |
val nbe_tab = NBE_Data.get thy; |
22213 | 122 |
in |
123 |
CodegenFuncgr.deps funcgr consts |
|
124 |
|> (map o filter_out) (Symtab.defined nbe_tab o CodegenNames.const thy) |
|
125 |
|> filter_out null |
|
126 |
|> (map o map) (fn c => (CodegenNames.const thy c, CodegenFuncgr.funcs funcgr c)) |
|
127 |
|> generate thy |
|
128 |
end; |
|
21124 | 129 |
|
130 |
(* term evaluation *) |
|
20846 | 131 |
|
22213 | 132 |
fun eval_term thy funcgr t = |
21124 | 133 |
let |
134 |
fun subst_Frees [] = I |
|
135 |
| subst_Frees inst = |
|
23090 | 136 |
Term.map_aterms (fn (t as Free (s, _)) => the_default t (AList.lookup (op =) inst s) |
137 |
| t => t); |
|
21124 | 138 |
val anno_vars = |
139 |
subst_Frees (map (fn (s, T) => (s, Free (s, T))) (Term.add_frees t [])) |
|
23090 | 140 |
#> subst_Vars (map (fn (ixn, T) => (ixn, Var (ixn, T))) (Term.add_vars t [])) |
21124 | 141 |
fun check_tvars t = if null (Term.term_tvars t) then t else |
142 |
error ("Illegal schematic type variables in normalized term: " |
|
143 |
^ setmp show_types true (Sign.string_of_term thy) t); |
|
144 |
val ty = type_of t; |
|
22728 | 145 |
fun constrain t = |
23090 | 146 |
singleton (ProofContext.infer_types_pats (ProofContext.init thy)) (TypeInfer.constrain t ty); |
22213 | 147 |
val _ = ensure_funs thy funcgr t; |
21124 | 148 |
in |
149 |
t |
|
150 |
|> tracing (fn t => "Input:\n" ^ Display.raw_string_of_term t) |
|
151 |
|> NBE_Eval.eval thy (!tab) |
|
152 |
|> tracing (fn nt => "Normalized:\n" ^ NBE_Eval.string_of_nterm nt) |
|
153 |
|> NBE_Codegen.nterm_to_term thy |
|
23090 | 154 |
|> tracing (fn t => "Converted back:\n" ^ setmp show_types true Display.raw_string_of_term t) |
155 |
|> tracing (fn _ => "Term type:\n" ^ Display.raw_string_of_typ ty) |
|
21124 | 156 |
|> anno_vars |
23090 | 157 |
|> tracing (fn t => "Vars typed:\n" ^ setmp show_types true Display.raw_string_of_term t) |
21124 | 158 |
|> constrain |
23090 | 159 |
|> tracing (fn t => "Types inferred:\n" ^ setmp show_types true Display.raw_string_of_term t) |
21124 | 160 |
|> check_tvars |
161 |
end; |
|
19795 | 162 |
|
21124 | 163 |
(* evaluation oracle *) |
164 |
||
22213 | 165 |
exception Normalization of CodegenFuncgr.T * term; |
19795 | 166 |
|
22213 | 167 |
fun normalization_oracle (thy, Normalization (funcgr, t)) = |
168 |
Logic.mk_equals (t, eval_term thy funcgr t); |
|
21124 | 169 |
|
22213 | 170 |
fun normalization_invoke thy funcgr t = |
171 |
Thm.invoke_oracle_i thy "Pure.normalization" (thy, Normalization (funcgr, t)); |
|
19968 | 172 |
|
20846 | 173 |
in |
19968 | 174 |
|
21124 | 175 |
(* interface *) |
176 |
||
177 |
fun normalization_conv ct = |
|
20846 | 178 |
let |
21124 | 179 |
val thy = Thm.theory_of_cterm ct; |
22213 | 180 |
fun mk funcgr drop_classes ct thm1 = |
21129 | 181 |
let |
182 |
val t = Thm.term_of ct; |
|
22213 | 183 |
val thm2 = normalization_invoke thy funcgr t; |
22902
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents:
22846
diff
changeset
|
184 |
val thm3 = apply_posts thy (Thm.rhs_of thm2); |
21129 | 185 |
val thm23 = drop_classes (Thm.transitive thm2 thm3); |
186 |
in |
|
187 |
Thm.transitive thm1 thm23 handle THM _ => |
|
188 |
error ("normalization_conv - could not construct proof:\n" |
|
189 |
^ (cat_lines o map string_of_thm) [thm1, thm2, thm3]) |
|
190 |
end; |
|
22213 | 191 |
in fst (Funcgr.make_term thy mk ct) end; |
20846 | 192 |
|
193 |
fun norm_print_term ctxt modes t = |
|
194 |
let |
|
195 |
val thy = ProofContext.theory_of ctxt; |
|
21124 | 196 |
val ct = Thm.cterm_of thy t; |
197 |
val (_, t') = (Logic.dest_equals o Thm.prop_of o normalization_conv) ct; |
|
20846 | 198 |
val ty = Term.type_of t'; |
199 |
val p = Library.setmp print_mode (modes @ ! print_mode) (fn () => |
|
200 |
Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t'), Pretty.fbrk, |
|
201 |
Pretty.str "::", Pretty.brk 1, Pretty.quote (ProofContext.pretty_typ ctxt ty)]) (); |
|
202 |
in Pretty.writeln p end; |
|
203 |
||
204 |
fun norm_print_term_e (modes, raw_t) state = |
|
21506 | 205 |
let val ctxt = Toplevel.context_of state |
20846 | 206 |
in norm_print_term ctxt modes (ProofContext.read_term ctxt raw_t) end; |
207 |
||
20602 | 208 |
val _ = Context.add_setup |
20677 | 209 |
(Theory.add_oracle ("normalization", normalization_oracle)); |
19177 | 210 |
|
21124 | 211 |
end; (*local*) |
212 |
||
20846 | 213 |
|
19968 | 214 |
(* Isar setup *) |
215 |
||
216 |
local structure P = OuterParse and K = OuterKeyword in |
|
19147 | 217 |
|
20846 | 218 |
val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) []; |
219 |
||
19147 | 220 |
val nbeP = |
20846 | 221 |
OuterSyntax.improper_command "normal_form" "normalize term by evaluation" K.diag |
222 |
(opt_modes -- P.typ >> (Toplevel.keep o norm_print_term_e)); |
|
19968 | 223 |
|
224 |
end; |
|
19147 | 225 |
|
226 |
val _ = OuterSyntax.add_parsers [nbeP]; |
|
227 |
||
19177 | 228 |
end; |