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*)
|
19962
|
11 |
val norm_term: theory -> term -> term
|
20602
|
12 |
val normalization_conv: cterm -> thm
|
19962
|
13 |
val lookup: string -> NBE_Eval.Univ
|
19795
|
14 |
val update: string * NBE_Eval.Univ -> unit
|
20846
|
15 |
val trace: bool ref
|
19147
|
16 |
end;
|
|
17 |
|
19177
|
18 |
structure NBE: NBE =
|
19147
|
19 |
struct
|
|
20 |
|
20846
|
21 |
val trace = ref false;
|
|
22 |
fun tracing f = if !trace then Output.tracing (f ()) else ();
|
|
23 |
|
19968
|
24 |
|
|
25 |
(* theory data setup *)
|
|
26 |
|
20846
|
27 |
structure NBE_Data = CodeDataFun
|
19147
|
28 |
(struct
|
19962
|
29 |
val name = "Pure/NBE"
|
|
30 |
type T = NBE_Eval.Univ Symtab.table
|
|
31 |
val empty = Symtab.empty
|
|
32 |
fun merge _ = Symtab.merge (K true)
|
20846
|
33 |
fun purge _ _ = Symtab.empty
|
19147
|
34 |
end);
|
|
35 |
|
19968
|
36 |
val _ = Context.add_setup NBE_Data.init;
|
19795
|
37 |
|
|
38 |
|
19968
|
39 |
(* sandbox communication *)
|
19147
|
40 |
|
19177
|
41 |
val tab : NBE_Eval.Univ Symtab.table ref = ref Symtab.empty;
|
|
42 |
fun lookup s = (the o Symtab.lookup (!tab)) s;
|
|
43 |
fun update sx = (tab := Symtab.update sx (!tab));
|
|
44 |
|
19147
|
45 |
|
20846
|
46 |
(* norm by eval *)
|
|
47 |
|
|
48 |
local
|
|
49 |
|
|
50 |
(* FIXME better turn this into a function
|
|
51 |
NBE_Eval.Univ Symtab.table -> NBE_Eval.Univ Symtab.table
|
|
52 |
with implicit side effect *)
|
|
53 |
fun use_code NONE = ()
|
|
54 |
| use_code (SOME s) =
|
|
55 |
(tracing (fn () => "\n---generated code:\n" ^ s);
|
|
56 |
use_text (Output.tracing o enclose "\n---compiler echo:\n" "\n---\n",
|
|
57 |
Output.tracing o enclose "\n--- compiler echo (with error!):\n" "\n---\n")
|
|
58 |
(!trace) s);
|
|
59 |
|
|
60 |
fun generate thy funs =
|
|
61 |
let
|
|
62 |
val _ = tab := NBE_Data.get thy;;
|
|
63 |
val _ = Library.seq (use_code o NBE_Codegen.generate thy
|
|
64 |
(fn s => Symtab.defined (!tab) s)) funs;
|
|
65 |
in NBE_Data.change thy (K (!tab)) end;
|
|
66 |
|
|
67 |
fun compile_term thy t =
|
|
68 |
let
|
|
69 |
(*FIXME: proper interfaces in codegen_*)
|
|
70 |
val (consts, cs) = CodegenConsts.consts_of thy t;
|
|
71 |
val funcgr = CodegenFuncgr.mk_funcgr thy consts cs;
|
|
72 |
(*FIXME: proper interfaces in codegen_*)
|
|
73 |
fun const_typ (c, ty) =
|
19795
|
74 |
let
|
20846
|
75 |
val const = CodegenConsts.norm_of_typ thy (c, ty);
|
|
76 |
in case CodegenFuncgr.get_funcs funcgr const
|
|
77 |
of (thm :: _) => CodegenData.typ_func thy thm
|
|
78 |
| [] => Sign.the_const_type thy c
|
|
79 |
end;
|
|
80 |
val (_, ct) = CodegenData.preprocess_cterm thy const_typ (Thm.cterm_of thy t)
|
|
81 |
val t' = Thm.term_of ct;
|
|
82 |
val (consts, cs) = CodegenConsts.consts_of thy t';
|
|
83 |
val funcgr = CodegenFuncgr.mk_funcgr thy consts cs;
|
|
84 |
val nbe_tab = NBE_Data.get thy;
|
|
85 |
val all_consts =
|
|
86 |
CodegenFuncgr.all_deps_of funcgr consts
|
|
87 |
|> (map o filter_out) (Symtab.defined nbe_tab o CodegenNames.const thy)
|
|
88 |
|> filter_out null;
|
|
89 |
val funs = (map o map)
|
|
90 |
(fn c => (CodegenNames.const thy c, CodegenFuncgr.get_funcs funcgr c)) all_consts;
|
|
91 |
val _ = tracing (fn () => "new definitions: " ^ (commas o maps (map fst)) funs);
|
|
92 |
val _ = generate thy funs;
|
20856
|
93 |
val nt = NBE_Eval.eval thy (!tab) t';
|
20846
|
94 |
in nt end;
|
|
95 |
|
|
96 |
fun subst_Frees [] = I
|
|
97 |
| subst_Frees inst =
|
|
98 |
Term.map_aterms (fn (t as Free(s, _)) => the_default t (AList.lookup (op =) inst s)
|
|
99 |
| t => t);
|
19795
|
100 |
|
|
101 |
fun var_tab t = (Term.add_frees t [], Term.add_vars t []);
|
|
102 |
|
19968
|
103 |
fun anno_vars (Ftab, Vtab) =
|
|
104 |
subst_Vars (map (fn (ixn, T) => (ixn, Var(ixn,T))) Vtab) o
|
|
105 |
subst_Frees (map (fn (s, T) => (s, Free(s,T))) Ftab)
|
|
106 |
|
20846
|
107 |
in
|
19968
|
108 |
|
20846
|
109 |
fun norm_term thy t =
|
|
110 |
let
|
|
111 |
val _ = tracing (fn () => "Input:\n" ^ Display.raw_string_of_term t);
|
|
112 |
val nt = compile_term thy t;
|
|
113 |
val vtab = var_tab t;
|
|
114 |
val ty = type_of t;
|
|
115 |
fun constrain ty t = Sign.infer_types (Sign.pp thy) thy (Sign.consts_of thy)
|
|
116 |
(K NONE) (K NONE) Name.context false ([t], ty) |> fst;
|
|
117 |
val _ = tracing (fn () => "Normalized:\n" ^ NBE_Eval.string_of_nterm nt);
|
|
118 |
val t' = NBE_Codegen.nterm_to_term thy nt;
|
|
119 |
val _ = tracing (fn () =>"Converted back:\n" ^ Display.raw_string_of_term t');
|
|
120 |
val t'' = anno_vars vtab t';
|
|
121 |
val _ = tracing (fn () =>"Vars typed:\n" ^ Display.raw_string_of_term t'');
|
|
122 |
val t''' = constrain ty t'';
|
|
123 |
val _ = if null (Term.term_tvars t''') then () else
|
|
124 |
error ("Illegal schematic type variables in normalized term: "
|
|
125 |
^ setmp show_types true (Sign.string_of_term thy) t''');
|
|
126 |
in t''' end;
|
|
127 |
|
|
128 |
fun norm_print_term ctxt modes t =
|
|
129 |
let
|
|
130 |
val thy = ProofContext.theory_of ctxt;
|
|
131 |
val t' = norm_term thy t;
|
|
132 |
val ty = Term.type_of t';
|
|
133 |
val p = Library.setmp print_mode (modes @ ! print_mode) (fn () =>
|
|
134 |
Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t'), Pretty.fbrk,
|
|
135 |
Pretty.str "::", Pretty.brk 1, Pretty.quote (ProofContext.pretty_typ ctxt ty)]) ();
|
|
136 |
in Pretty.writeln p end;
|
|
137 |
|
|
138 |
fun norm_print_term_e (modes, raw_t) state =
|
|
139 |
let
|
|
140 |
val ctxt = (Proof.context_of o Toplevel.enter_forward_proof) state;
|
|
141 |
in norm_print_term ctxt modes (ProofContext.read_term ctxt raw_t) end;
|
|
142 |
|
|
143 |
end; (*local*)
|
19968
|
144 |
|
|
145 |
|
20846
|
146 |
(* normalization oracle *)
|
19962
|
147 |
|
20602
|
148 |
exception Normalization of term;
|
|
149 |
|
|
150 |
fun normalization_oracle (thy, Normalization t) =
|
|
151 |
Logic.mk_equals (t, norm_term thy t);
|
|
152 |
|
|
153 |
fun normalization_conv ct =
|
|
154 |
let val {sign, t, ...} = rep_cterm ct
|
20677
|
155 |
in Thm.invoke_oracle_i sign "Pure.normalization" (sign, Normalization t) end;
|
20602
|
156 |
|
|
157 |
val _ = Context.add_setup
|
20677
|
158 |
(Theory.add_oracle ("normalization", normalization_oracle));
|
19177
|
159 |
|
20846
|
160 |
|
19968
|
161 |
(* Isar setup *)
|
|
162 |
|
|
163 |
local structure P = OuterParse and K = OuterKeyword in
|
19147
|
164 |
|
20846
|
165 |
val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
|
|
166 |
|
19147
|
167 |
val nbeP =
|
20846
|
168 |
OuterSyntax.improper_command "normal_form" "normalize term by evaluation" K.diag
|
|
169 |
(opt_modes -- P.typ >> (Toplevel.keep o norm_print_term_e));
|
19968
|
170 |
|
|
171 |
end;
|
19147
|
172 |
|
|
173 |
val _ = OuterSyntax.add_parsers [nbeP];
|
|
174 |
|
19177
|
175 |
end;
|