20600
|
1 |
(* Title: Pure/Tools/codegen_funcgr.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Florian Haftmann, TU Muenchen
|
|
4 |
|
20855
|
5 |
Retrieving, normalizing and structuring code function theorems
|
|
6 |
in graph with explicit dependencies.
|
20600
|
7 |
*)
|
|
8 |
|
|
9 |
signature CODEGEN_FUNCGR =
|
|
10 |
sig
|
|
11 |
type T;
|
|
12 |
val mk_funcgr: theory -> CodegenConsts.const list -> (string * typ) list -> T
|
20835
|
13 |
val all_deps_of: T -> CodegenConsts.const list -> CodegenConsts.const list list
|
20600
|
14 |
val get_funcs: T -> CodegenConsts.const -> thm list
|
|
15 |
val get_func_typs: T -> (CodegenConsts.const * typ) list
|
20835
|
16 |
val normalize: theory -> thm list -> thm list
|
20600
|
17 |
val print_codethms: theory -> CodegenConsts.const list -> unit
|
20938
|
18 |
structure Constgraph : GRAPH
|
20600
|
19 |
end;
|
|
20 |
|
|
21 |
structure CodegenFuncgr: CODEGEN_FUNCGR =
|
|
22 |
struct
|
|
23 |
|
|
24 |
(** code data **)
|
|
25 |
|
|
26 |
structure Consttab = CodegenConsts.Consttab;
|
|
27 |
structure Constgraph = GraphFun (
|
|
28 |
type key = CodegenConsts.const;
|
|
29 |
val ord = CodegenConsts.const_ord;
|
|
30 |
);
|
|
31 |
|
|
32 |
type T = (typ * thm list) Constgraph.T;
|
|
33 |
|
|
34 |
structure Funcgr = CodeDataFun
|
|
35 |
(struct
|
|
36 |
val name = "Pure/codegen_funcgr";
|
|
37 |
type T = T;
|
|
38 |
val empty = Constgraph.empty;
|
|
39 |
fun merge _ _ = Constgraph.empty;
|
20938
|
40 |
fun purge _ NONE _ = Constgraph.empty
|
|
41 |
| purge _ (SOME cs) funcgr =
|
|
42 |
Constgraph.del_nodes ((Constgraph.all_succs funcgr
|
|
43 |
o filter (can (Constgraph.get_node funcgr))) cs) funcgr;
|
20600
|
44 |
end);
|
|
45 |
|
|
46 |
val _ = Context.add_setup Funcgr.init;
|
|
47 |
|
|
48 |
|
|
49 |
(** theorem purification **)
|
|
50 |
|
|
51 |
fun abs_norm thy thm =
|
|
52 |
let
|
20705
|
53 |
fun eta_expand thm =
|
20600
|
54 |
let
|
20705
|
55 |
val lhs = (fst o Logic.dest_equals o Drule.plain_prop_of) thm;
|
20600
|
56 |
val tys = (fst o strip_type o fastype_of) lhs;
|
20896
|
57 |
val ctxt = Name.make_context (map (fst o fst) (Term.add_vars lhs []));
|
20705
|
58 |
val vs_ct = map (fn (v, ty) => cterm_of thy (Var ((v, 0), ty)))
|
20896
|
59 |
(Name.names ctxt "a" tys);
|
20600
|
60 |
in
|
20705
|
61 |
fold (fn ct => fn thm => Thm.combination thm (Thm.reflexive ct)) vs_ct thm
|
20600
|
62 |
end;
|
|
63 |
fun beta_norm thm =
|
20705
|
64 |
let
|
|
65 |
val rhs = (snd o Logic.dest_equals o Drule.plain_prop_of) thm;
|
|
66 |
val thm' = Thm.beta_conversion true (cterm_of thy rhs);
|
|
67 |
in Thm.transitive thm thm' end;
|
20600
|
68 |
in
|
|
69 |
thm
|
20705
|
70 |
|> eta_expand
|
20600
|
71 |
|> beta_norm
|
|
72 |
end;
|
|
73 |
|
|
74 |
fun canonical_tvars thy thm =
|
|
75 |
let
|
|
76 |
fun mk_inst (v_i as (v, i), (v', sort)) (s as (maxidx, set, acc)) =
|
|
77 |
if v = v' orelse member (op =) set v then s
|
|
78 |
else let
|
|
79 |
val ty = TVar (v_i, sort)
|
|
80 |
in
|
|
81 |
(maxidx + 1, v :: set,
|
|
82 |
(ctyp_of thy ty, ctyp_of thy (TVar ((v', maxidx), sort))) :: acc)
|
|
83 |
end;
|
|
84 |
fun tvars_of thm = (fold_types o fold_atyps)
|
|
85 |
(fn TVar (v_i as (v, i), sort) => cons (v_i, (CodegenNames.purify_var v, sort))
|
|
86 |
| _ => I) (prop_of thm) [];
|
|
87 |
val maxidx = Thm.maxidx_of thm + 1;
|
|
88 |
val (_, _, inst) = fold mk_inst (tvars_of thm) (maxidx + 1, [], []);
|
|
89 |
in Thm.instantiate (inst, []) thm end;
|
|
90 |
|
|
91 |
fun canonical_vars thy thm =
|
|
92 |
let
|
|
93 |
fun mk_inst (v_i as (v, i), (v', ty)) (s as (maxidx, set, acc)) =
|
|
94 |
if v = v' orelse member (op =) set v then s
|
|
95 |
else let
|
|
96 |
val t = if i = ~1 then Free (v, ty) else Var (v_i, ty)
|
|
97 |
in
|
|
98 |
(maxidx + 1, v :: set,
|
|
99 |
(cterm_of thy t, cterm_of thy (Var ((v', maxidx), ty))) :: acc)
|
|
100 |
end;
|
|
101 |
fun vars_of thm = fold_aterms
|
|
102 |
(fn Var (v_i as (v, i), ty) => cons (v_i, (CodegenNames.purify_var v, ty))
|
|
103 |
| _ => I) (prop_of thm) [];
|
|
104 |
val maxidx = Thm.maxidx_of thm + 1;
|
|
105 |
val (_, _, inst) = fold mk_inst (vars_of thm) (maxidx + 1, [], []);
|
|
106 |
in Thm.instantiate ([], inst) thm end;
|
|
107 |
|
20835
|
108 |
fun normalize thy thms =
|
20600
|
109 |
let
|
|
110 |
fun burrow_thms f [] = []
|
|
111 |
| burrow_thms f thms =
|
|
112 |
thms
|
|
113 |
|> Conjunction.intr_list
|
|
114 |
|> f
|
|
115 |
|> Conjunction.elim_list;
|
|
116 |
fun unvarify thms =
|
|
117 |
#2 (#1 (Variable.import true thms (ProofContext.init thy)));
|
|
118 |
in
|
|
119 |
thms
|
|
120 |
|> map (abs_norm thy)
|
|
121 |
|> burrow_thms (
|
|
122 |
canonical_tvars thy
|
|
123 |
#> canonical_vars thy
|
|
124 |
#> Drule.zero_var_indexes
|
|
125 |
)
|
|
126 |
end;
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
(** retrieval **)
|
|
131 |
|
|
132 |
fun get_funcs funcgr (c_tys as (c, _)) =
|
20835
|
133 |
(these o Option.map snd o try (Constgraph.get_node funcgr)) c_tys;
|
20600
|
134 |
|
|
135 |
fun get_func_typs funcgr =
|
|
136 |
AList.make (fst o Constgraph.get_node funcgr) (Constgraph.keys funcgr);
|
|
137 |
|
20835
|
138 |
fun all_deps_of funcgr cs =
|
|
139 |
let
|
|
140 |
val conn = Constgraph.strong_conn funcgr;
|
|
141 |
val order = rev conn;
|
|
142 |
in
|
|
143 |
(map o filter) (member (op =) (Constgraph.all_succs funcgr cs)) order
|
|
144 |
|> filter_out null
|
|
145 |
end;
|
|
146 |
|
20600
|
147 |
local
|
|
148 |
|
|
149 |
fun add_things_of thy f (c, thms) =
|
|
150 |
(fold o fold_aterms)
|
|
151 |
(fn Const c_ty => let
|
|
152 |
val c' = CodegenConsts.norm_of_typ thy c_ty
|
|
153 |
in if CodegenConsts.eq_const (c, c') then I
|
|
154 |
else f (c', c_ty) end
|
|
155 |
| _ => I) (maps (op :: o swap o apfst (snd o strip_comb)
|
|
156 |
o Logic.dest_equals o Drule.plain_prop_of) thms)
|
|
157 |
|
|
158 |
fun rhs_of thy (c, thms) =
|
|
159 |
Consttab.empty
|
|
160 |
|> add_things_of thy (Consttab.update o rpair () o fst) (c, thms)
|
|
161 |
|> Consttab.keys;
|
|
162 |
|
|
163 |
fun rhs_of' thy (c, thms) =
|
|
164 |
add_things_of thy (cons o snd) (c, thms) [];
|
|
165 |
|
|
166 |
fun insts_of thy funcgr (c, ty) =
|
|
167 |
let
|
|
168 |
val tys = Sign.const_typargs thy (c, ty);
|
|
169 |
val c' = CodegenConsts.norm thy (c, tys);
|
20705
|
170 |
val ty_decl = CodegenConsts.disc_typ_of_const thy
|
|
171 |
(fst o Constgraph.get_node funcgr o CodegenConsts.norm thy) (c, tys);
|
20600
|
172 |
val tys_decl = Sign.const_typargs thy (c, ty_decl);
|
|
173 |
val pp = Sign.pp thy;
|
|
174 |
val algebra = Sign.classes_of thy;
|
|
175 |
fun classrel (x, _) _ = x;
|
|
176 |
fun constructor tyco xs class =
|
|
177 |
(tyco, class) :: maps (maps fst) xs;
|
|
178 |
fun variable (TVar (_, sort)) = map (pair []) sort
|
|
179 |
| variable (TFree (_, sort)) = map (pair []) sort;
|
|
180 |
fun mk_inst ty (TVar (_, sort)) = cons (ty, sort)
|
|
181 |
| mk_inst ty (TFree (_, sort)) = cons (ty, sort)
|
|
182 |
| mk_inst (Type (tyco1, tys1)) (Type (tyco2, tys2)) =
|
|
183 |
if tyco1 <> tyco2 then error "bad instance"
|
|
184 |
else fold2 mk_inst tys1 tys2;
|
|
185 |
in
|
|
186 |
flat (maps (Sorts.of_sort_derivation pp algebra
|
|
187 |
{ classrel = classrel, constructor = constructor, variable = variable })
|
|
188 |
(fold2 mk_inst tys tys_decl []))
|
|
189 |
end;
|
|
190 |
|
|
191 |
fun all_classops thy tyco class =
|
|
192 |
maps (AxClass.params_of thy)
|
|
193 |
(Graph.all_succs ((#classes o Sorts.rep_algebra o Sign.classes_of) thy) [class])
|
20705
|
194 |
|> AList.make (fn c => CodegenConsts.disc_typ_of_classop thy (c, [Type (tyco, [])]))
|
20600
|
195 |
(*typ_of_classop is very liberal in its type arguments*)
|
|
196 |
|> map (CodegenConsts.norm_of_typ thy);
|
|
197 |
|
|
198 |
fun instdefs_of thy insts =
|
|
199 |
let
|
|
200 |
val thy_classes = (#classes o Sorts.rep_algebra o Sign.classes_of) thy;
|
|
201 |
in
|
|
202 |
Symtab.empty
|
|
203 |
|> fold (fn (tyco, class) =>
|
|
204 |
Symtab.map_default (tyco, []) (insert (op =) class)) insts
|
|
205 |
|> (fn tab => Symtab.fold (fn (tyco, classes) => append (maps (all_classops thy tyco)
|
|
206 |
(Graph.all_succs thy_classes classes))) tab [])
|
|
207 |
end;
|
|
208 |
|
|
209 |
fun insts_of_thms thy funcgr c_thms =
|
|
210 |
let
|
|
211 |
val insts = add_things_of thy (fn (_, c_ty) => fold (insert (op =))
|
|
212 |
(insts_of thy funcgr c_ty)) c_thms [];
|
|
213 |
in instdefs_of thy insts end;
|
|
214 |
|
|
215 |
fun ensure_const thy funcgr c auxgr =
|
|
216 |
if can (Constgraph.get_node funcgr) c
|
|
217 |
then (NONE, auxgr)
|
|
218 |
else if can (Constgraph.get_node auxgr) c
|
|
219 |
then (SOME c, auxgr)
|
|
220 |
else if is_some (CodegenData.get_datatype_of_constr thy c) then
|
|
221 |
auxgr
|
|
222 |
|> Constgraph.new_node (c, [])
|
|
223 |
|> pair (SOME c)
|
|
224 |
else let
|
20835
|
225 |
val thms = normalize thy (CodegenData.these_funcs thy c);
|
20600
|
226 |
val rhs = rhs_of thy (c, thms);
|
|
227 |
in
|
|
228 |
auxgr
|
|
229 |
|> Constgraph.new_node (c, thms)
|
|
230 |
|> fold_map (ensure_const thy funcgr) rhs
|
|
231 |
|-> (fn rhs' => fold (fn SOME c' => Constgraph.add_edge (c, c')
|
|
232 |
| NONE => I) rhs')
|
|
233 |
|> pair (SOME c)
|
|
234 |
end;
|
|
235 |
|
|
236 |
fun specialize_typs thy funcgr eqss =
|
|
237 |
let
|
|
238 |
fun max k [] = k
|
|
239 |
| max k (l::ls) = max (if k < l then l else k) ls;
|
|
240 |
fun typscheme_of (c, ty) =
|
|
241 |
try (Constgraph.get_node funcgr) (CodegenConsts.norm_of_typ thy (c, ty))
|
|
242 |
|> Option.map fst;
|
|
243 |
fun incr_indices (c, thms) maxidx =
|
|
244 |
let
|
|
245 |
val thms' = map (Thm.incr_indexes maxidx) thms;
|
|
246 |
val maxidx' = Int.max
|
|
247 |
(maxidx, max ~1 (map Thm.maxidx_of thms') + 1);
|
|
248 |
in ((c, thms'), maxidx') end;
|
|
249 |
val tsig = Sign.tsig_of thy;
|
|
250 |
fun unify_const thms (c, ty) (env, maxidx) =
|
|
251 |
case typscheme_of (c, ty)
|
|
252 |
of SOME ty_decl => let
|
|
253 |
val ty_decl' = Logic.incr_tvar maxidx ty_decl;
|
|
254 |
val maxidx' = Int.max (Term.maxidx_of_typ ty_decl' + 1, maxidx);
|
|
255 |
in Type.unify tsig (ty_decl', ty) (env, maxidx')
|
|
256 |
handle TUNIFY => error ("Failed to instantiate\n"
|
|
257 |
^ (Sign.string_of_typ thy o Envir.norm_type env) ty_decl' ^ "\nto\n"
|
|
258 |
^ (Sign.string_of_typ thy o Envir.norm_type env) ty ^ ",\n"
|
|
259 |
^ "in function theorems\n"
|
|
260 |
^ cat_lines (map string_of_thm thms))
|
|
261 |
end
|
|
262 |
| NONE => (env, maxidx);
|
|
263 |
fun apply_unifier unif (c, []) = (c, [])
|
|
264 |
| apply_unifier unif (c, thms as thm :: _) =
|
|
265 |
let
|
|
266 |
val ty = CodegenData.typ_func thy thm;
|
|
267 |
val ty' = Envir.norm_type unif ty;
|
|
268 |
val env = Type.typ_match (Sign.tsig_of thy) (ty, ty') Vartab.empty;
|
|
269 |
val inst = Thm.instantiate (Vartab.fold (fn (x_i, (sort, ty)) =>
|
|
270 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [], []);
|
|
271 |
in (c, map (Drule.zero_var_indexes o inst) thms) end;
|
|
272 |
val (eqss', maxidx) =
|
|
273 |
fold_map incr_indices eqss 0;
|
|
274 |
val (unif, _) =
|
|
275 |
fold (fn (c, thms) => fold (unify_const thms) (rhs_of' thy (c, thms)))
|
|
276 |
eqss' (Vartab.empty, maxidx);
|
|
277 |
val eqss'' =
|
|
278 |
map (apply_unifier unif) eqss';
|
|
279 |
in eqss'' end;
|
|
280 |
|
|
281 |
fun merge_eqsyss thy raw_eqss funcgr =
|
|
282 |
let
|
|
283 |
val eqss = specialize_typs thy funcgr raw_eqss;
|
20938
|
284 |
val tys = map (CodegenData.typ_funcs thy) eqss;
|
20600
|
285 |
val rhss = map (rhs_of thy) eqss;
|
|
286 |
in
|
|
287 |
funcgr
|
|
288 |
|> fold2 (fn (c, thms) => fn ty => Constgraph.new_node (c, (ty, thms))) eqss tys
|
|
289 |
|> `(fn funcgr => map (insts_of_thms thy funcgr) eqss)
|
|
290 |
|-> (fn rhs_insts => fold2 (fn (c, _) => fn rhs_inst =>
|
|
291 |
ensure_consts thy rhs_inst #> fold (curry Constgraph.add_edge c) rhs_inst) eqss rhs_insts)
|
|
292 |
|> fold2 (fn (c, _) => fn rhs => fold (curry Constgraph.add_edge c) rhs) eqss rhss
|
|
293 |
end
|
20705
|
294 |
and merge_new_eqsyss thy raw_eqss funcgr =
|
|
295 |
if exists (member CodegenConsts.eq_const (Constgraph.keys funcgr)) (map fst raw_eqss)
|
|
296 |
then funcgr
|
|
297 |
else merge_eqsyss thy raw_eqss funcgr
|
20600
|
298 |
and ensure_consts thy cs funcgr =
|
|
299 |
fold (snd oo ensure_const thy funcgr) cs Constgraph.empty
|
20705
|
300 |
|> (fn auxgr => fold (merge_new_eqsyss thy)
|
20600
|
301 |
(map (AList.make (Constgraph.get_node auxgr))
|
|
302 |
(rev (Constgraph.strong_conn auxgr))) funcgr);
|
|
303 |
|
|
304 |
in
|
|
305 |
|
|
306 |
val ensure_consts = ensure_consts;
|
|
307 |
|
|
308 |
fun mk_funcgr thy consts cs =
|
|
309 |
Funcgr.change thy (
|
|
310 |
ensure_consts thy consts
|
|
311 |
#> (fn funcgr => ensure_consts thy
|
|
312 |
(instdefs_of thy (fold (fold (insert (op =)) o insts_of thy funcgr) cs [])) funcgr)
|
|
313 |
);
|
|
314 |
|
|
315 |
end; (*local*)
|
|
316 |
|
|
317 |
fun print_funcgr thy funcgr =
|
|
318 |
AList.make (snd o Constgraph.get_node funcgr) (Constgraph.keys funcgr)
|
|
319 |
|> (map o apfst) (CodegenConsts.string_of_const thy)
|
|
320 |
|> sort (string_ord o pairself fst)
|
|
321 |
|> map (fn (s, thms) =>
|
|
322 |
(Pretty.block o Pretty.fbreaks) (
|
|
323 |
Pretty.str s
|
|
324 |
:: map Display.pretty_thm thms
|
|
325 |
))
|
|
326 |
|> Pretty.chunks
|
|
327 |
|> Pretty.writeln;
|
|
328 |
|
|
329 |
fun print_codethms thy consts =
|
|
330 |
mk_funcgr thy consts [] |> print_funcgr thy;
|
|
331 |
|
|
332 |
fun print_codethms_e thy cs =
|
|
333 |
print_codethms thy (map (CodegenConsts.read_const thy) cs);
|
|
334 |
|
|
335 |
|
|
336 |
(** Isar **)
|
|
337 |
|
|
338 |
structure P = OuterParse;
|
|
339 |
|
|
340 |
val print_codethmsK = "print_codethms";
|
|
341 |
|
|
342 |
val print_codethmsP =
|
|
343 |
OuterSyntax.improper_command print_codethmsK "print code theorems of this theory" OuterKeyword.diag
|
|
344 |
(Scan.option (P.$$$ "(" |-- Scan.repeat P.term --| P.$$$ ")")
|
|
345 |
>> (fn NONE => CodegenData.print_thms
|
|
346 |
| SOME cs => fn thy => print_codethms_e thy cs)
|
|
347 |
>> (fn f => Toplevel.no_timing o Toplevel.unknown_theory
|
|
348 |
o Toplevel.keep (f o Toplevel.theory_of)));
|
|
349 |
|
|
350 |
val _ = OuterSyntax.add_parsers [print_codethmsP];
|
|
351 |
|
|
352 |
end; (*struct*)
|