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
|
|
29 |
(struct
|
|
30 |
val name = "Pure/nbe";
|
|
31 |
type T = thm list * thm list
|
|
32 |
|
|
33 |
val empty = ([],[])
|
|
34 |
val copy = I;
|
|
35 |
val extend = I;
|
|
36 |
|
|
37 |
fun merge _ ((pres1,posts1), (pres2,posts2)) =
|
|
38 |
(Library.merge eq_thm (pres1,pres2), Library.merge eq_thm (posts1,posts2))
|
|
39 |
|
|
40 |
fun print _ _ = ()
|
|
41 |
end);
|
|
42 |
|
21068
|
43 |
val _ =
|
|
44 |
let
|
|
45 |
fun map_data f = Context.mapping (NBE_Rewrite.map f) I;
|
|
46 |
fun attr_pre (thy, thm) =
|
|
47 |
((map_data o apfst) (insert eq_thm thm) thy, thm)
|
|
48 |
fun attr_post (thy, thm) =
|
|
49 |
((map_data o apsnd) (insert eq_thm thm) thy, thm)
|
|
50 |
val attr = Attrib.syntax (Scan.lift (Args.$$$ "pre" >> K attr_pre
|
|
51 |
|| Args.$$$ "post" >> K attr_post));
|
|
52 |
in
|
|
53 |
Context.add_setup (
|
|
54 |
NBE_Rewrite.init
|
|
55 |
#> Attrib.add_attributes
|
|
56 |
[("normal", attr, "declare rewrite theorems for normalization")]
|
|
57 |
)
|
|
58 |
end;
|
20920
|
59 |
|
|
60 |
fun consts_of_pres thy =
|
21068
|
61 |
let
|
21506
|
62 |
val ctxt = ProofContext.init thy;
|
21068
|
63 |
val pres = fst (NBE_Rewrite.get thy);
|
21506
|
64 |
val rhss = map (snd o Logic.dest_equals o prop_of o LocalDefs.meta_rewrite_rule ctxt) pres;
|
21068
|
65 |
in
|
|
66 |
(fold o fold_aterms)
|
|
67 |
(fn Const c => insert (op =) (CodegenConsts.norm_of_typ thy c) | _ => I)
|
|
68 |
rhss []
|
|
69 |
end;
|
20920
|
70 |
|
|
71 |
fun apply_pres thy =
|
21068
|
72 |
let
|
21506
|
73 |
val ctxt = ProofContext.init thy;
|
|
74 |
val pres = (map (LocalDefs.meta_rewrite_rule ctxt) o fst) (NBE_Rewrite.get thy)
|
20920
|
75 |
in map (CodegenData.rewrite_func pres) end
|
|
76 |
|
|
77 |
fun apply_posts thy =
|
21068
|
78 |
let
|
21506
|
79 |
val ctxt = ProofContext.init thy;
|
|
80 |
val posts = (map (LocalDefs.meta_rewrite_rule ctxt) o snd) (NBE_Rewrite.get thy)
|
21708
|
81 |
in MetaSimplifier.rewrite false posts end
|
20920
|
82 |
|
|
83 |
|
21068
|
84 |
(* code store *)
|
|
85 |
|
20846
|
86 |
structure NBE_Data = CodeDataFun
|
19147
|
87 |
(struct
|
19962
|
88 |
val name = "Pure/NBE"
|
|
89 |
type T = NBE_Eval.Univ Symtab.table
|
|
90 |
val empty = Symtab.empty
|
|
91 |
fun merge _ = Symtab.merge (K true)
|
20937
|
92 |
fun purge _ _ _ = Symtab.empty
|
19147
|
93 |
end);
|
|
94 |
|
19968
|
95 |
val _ = Context.add_setup NBE_Data.init;
|
19795
|
96 |
|
|
97 |
|
21124
|
98 |
(** norm by eval **)
|
21068
|
99 |
|
19968
|
100 |
(* sandbox communication *)
|
19147
|
101 |
|
19177
|
102 |
val tab : NBE_Eval.Univ Symtab.table ref = ref Symtab.empty;
|
|
103 |
fun lookup s = (the o Symtab.lookup (!tab)) s;
|
|
104 |
fun update sx = (tab := Symtab.update sx (!tab));
|
|
105 |
|
20846
|
106 |
local
|
|
107 |
|
21124
|
108 |
(* function generation *)
|
20846
|
109 |
|
|
110 |
fun generate thy funs =
|
|
111 |
let
|
21124
|
112 |
(* FIXME better turn this into a function
|
|
113 |
NBE_Eval.Univ Symtab.table -> NBE_Eval.Univ Symtab.table
|
|
114 |
with implicit side effect *)
|
|
115 |
fun use_code NONE = ()
|
|
116 |
| use_code (SOME s) =
|
|
117 |
(tracing (fn () => "\n---generated code:\n" ^ s);
|
|
118 |
use_text (Output.tracing o enclose "\n---compiler echo:\n" "\n---\n",
|
|
119 |
Output.tracing o enclose "\n--- compiler echo (with error!):\n" "\n---\n")
|
|
120 |
(!trace) s);
|
|
121 |
|
|
122 |
val _ = tracing (fn () => "new definitions: " ^ (commas o maps (map fst)) funs);
|
20846
|
123 |
val _ = tab := NBE_Data.get thy;;
|
|
124 |
val _ = Library.seq (use_code o NBE_Codegen.generate thy
|
|
125 |
(fn s => Symtab.defined (!tab) s)) funs;
|
|
126 |
in NBE_Data.change thy (K (!tab)) end;
|
|
127 |
|
21124
|
128 |
fun ensure_funs thy t =
|
20846
|
129 |
let
|
21124
|
130 |
val consts = CodegenConsts.consts_of thy t;
|
20920
|
131 |
val pre_consts = consts_of_pres thy;
|
|
132 |
val consts' = pre_consts @ consts;
|
21124
|
133 |
val funcgr = CodegenFuncgr.make thy consts';
|
20846
|
134 |
val nbe_tab = NBE_Data.get thy;
|
|
135 |
val all_consts =
|
21124
|
136 |
(pre_consts :: CodegenFuncgr.deps funcgr consts')
|
20846
|
137 |
|> (map o filter_out) (Symtab.defined nbe_tab o CodegenNames.const thy)
|
|
138 |
|> filter_out null;
|
|
139 |
val funs = (map o map)
|
21124
|
140 |
(fn c => (CodegenNames.const thy c, apply_pres thy (CodegenFuncgr.funcs funcgr c))) all_consts;
|
|
141 |
in generate thy funs end;
|
|
142 |
|
|
143 |
(* term evaluation *)
|
20846
|
144 |
|
21124
|
145 |
fun eval_term thy t =
|
|
146 |
let
|
|
147 |
fun subst_Frees [] = I
|
|
148 |
| subst_Frees inst =
|
|
149 |
Term.map_aterms (fn (t as Free(s, _)) => the_default t (AList.lookup (op =) inst s)
|
|
150 |
| t => t);
|
|
151 |
val anno_vars =
|
|
152 |
subst_Frees (map (fn (s, T) => (s, Free (s, T))) (Term.add_frees t []))
|
|
153 |
#> subst_Vars (map (fn (ixn, T) => (ixn, Var(ixn,T))) (Term.add_vars t []))
|
|
154 |
fun check_tvars t = if null (Term.term_tvars t) then t else
|
|
155 |
error ("Illegal schematic type variables in normalized term: "
|
|
156 |
^ setmp show_types true (Sign.string_of_term thy) t);
|
|
157 |
val ty = type_of t;
|
|
158 |
fun constrain t = Sign.infer_types (Sign.pp thy) thy (Sign.consts_of thy)
|
|
159 |
(K NONE) (K NONE) Name.context false ([t], ty) |> fst;
|
|
160 |
val _ = ensure_funs thy t;
|
|
161 |
in
|
|
162 |
t
|
|
163 |
|> tracing (fn t => "Input:\n" ^ Display.raw_string_of_term t)
|
|
164 |
|> NBE_Eval.eval thy (!tab)
|
|
165 |
|> tracing (fn nt => "Normalized:\n" ^ NBE_Eval.string_of_nterm nt)
|
|
166 |
|> NBE_Codegen.nterm_to_term thy
|
|
167 |
|> tracing (fn t =>"Converted back:\n" ^ Display.raw_string_of_term t)
|
|
168 |
|> anno_vars
|
|
169 |
|> tracing (fn t =>"Vars typed:\n" ^ Display.raw_string_of_term t)
|
|
170 |
|> constrain
|
|
171 |
|> check_tvars
|
|
172 |
end;
|
19795
|
173 |
|
21124
|
174 |
(* evaluation oracle *)
|
|
175 |
|
|
176 |
exception Normalization of term;
|
19795
|
177 |
|
21124
|
178 |
fun normalization_oracle (thy, Normalization t) =
|
|
179 |
Logic.mk_equals (t, eval_term thy t);
|
|
180 |
|
|
181 |
fun normalization_invoke thy t =
|
|
182 |
Thm.invoke_oracle_i thy "Pure.normalization" (thy, Normalization t);
|
19968
|
183 |
|
20846
|
184 |
in
|
19968
|
185 |
|
21124
|
186 |
(* interface *)
|
|
187 |
|
|
188 |
fun normalization_conv ct =
|
20846
|
189 |
let
|
21124
|
190 |
val thy = Thm.theory_of_cterm ct;
|
21129
|
191 |
fun mk drop_classes ct thm1 =
|
|
192 |
let
|
|
193 |
val t = Thm.term_of ct;
|
|
194 |
val thm2 = normalization_invoke thy t;
|
|
195 |
val thm3 = apply_posts thy ((snd o Drule.dest_equals o Thm.cprop_of) thm2);
|
|
196 |
val thm23 = drop_classes (Thm.transitive thm2 thm3);
|
|
197 |
in
|
|
198 |
Thm.transitive thm1 thm23 handle THM _ =>
|
|
199 |
error ("normalization_conv - could not construct proof:\n"
|
|
200 |
^ (cat_lines o map string_of_thm) [thm1, thm2, thm3])
|
|
201 |
end;
|
|
202 |
in fst (CodegenFuncgr.make_term thy mk ct) end;
|
20846
|
203 |
|
|
204 |
fun norm_print_term ctxt modes t =
|
|
205 |
let
|
|
206 |
val thy = ProofContext.theory_of ctxt;
|
21124
|
207 |
val ct = Thm.cterm_of thy t;
|
|
208 |
val (_, t') = (Logic.dest_equals o Thm.prop_of o normalization_conv) ct;
|
20846
|
209 |
val ty = Term.type_of t';
|
|
210 |
val p = Library.setmp print_mode (modes @ ! print_mode) (fn () =>
|
|
211 |
Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t'), Pretty.fbrk,
|
|
212 |
Pretty.str "::", Pretty.brk 1, Pretty.quote (ProofContext.pretty_typ ctxt ty)]) ();
|
|
213 |
in Pretty.writeln p end;
|
|
214 |
|
|
215 |
fun norm_print_term_e (modes, raw_t) state =
|
21506
|
216 |
let val ctxt = Toplevel.context_of state
|
20846
|
217 |
in norm_print_term ctxt modes (ProofContext.read_term ctxt raw_t) end;
|
|
218 |
|
20602
|
219 |
val _ = Context.add_setup
|
20677
|
220 |
(Theory.add_oracle ("normalization", normalization_oracle));
|
19177
|
221 |
|
21124
|
222 |
end; (*local*)
|
|
223 |
|
20846
|
224 |
|
19968
|
225 |
(* Isar setup *)
|
|
226 |
|
|
227 |
local structure P = OuterParse and K = OuterKeyword in
|
19147
|
228 |
|
20846
|
229 |
val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
|
|
230 |
|
19147
|
231 |
val nbeP =
|
20846
|
232 |
OuterSyntax.improper_command "normal_form" "normalize term by evaluation" K.diag
|
|
233 |
(opt_modes -- P.typ >> (Toplevel.keep o norm_print_term_e));
|
19968
|
234 |
|
|
235 |
end;
|
19147
|
236 |
|
|
237 |
val _ = OuterSyntax.add_parsers [nbeP];
|
|
238 |
|
19177
|
239 |
end;
|