author | haftmann |
Tue, 15 Jul 2008 16:02:07 +0200 | |
changeset 27609 | b23c9ad0fe7d |
parent 27103 | d8549f4d900b |
child 28054 | 2b84d34c5d02 |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Tools/code/code_funcgr.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
5 |
Retrieving, normalizing and structuring defining equations in graph |
|
6 |
with explicit dependencies. |
|
7 |
*) |
|
8 |
||
9 |
signature CODE_FUNCGR = |
|
10 |
sig |
|
11 |
type T |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
12 |
val funcs: T -> string -> thm list |
26971 | 13 |
val typ: T -> string -> (string * sort) list * typ |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
14 |
val all: T -> string list |
24219 | 15 |
val pretty: theory -> T -> Pretty.T |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
16 |
val make: theory -> string list -> T |
26740 | 17 |
val eval_conv: theory -> (term -> term * (T -> term -> thm)) -> cterm -> thm |
18 |
val eval_term: theory -> (term -> term * (T -> term -> 'a)) -> term -> 'a |
|
26971 | 19 |
val timing: bool ref |
24219 | 20 |
end |
21 |
||
24283 | 22 |
structure CodeFuncgr : CODE_FUNCGR = |
24219 | 23 |
struct |
24 |
||
25 |
(** the graph type **) |
|
26 |
||
26971 | 27 |
type T = (((string * sort) list * typ) * thm list) Graph.T; |
24219 | 28 |
|
29 |
fun funcs funcgr = |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
30 |
these o Option.map snd o try (Graph.get_node funcgr); |
24219 | 31 |
|
32 |
fun typ funcgr = |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
33 |
fst o Graph.get_node funcgr; |
24219 | 34 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
35 |
fun all funcgr = Graph.keys funcgr; |
24219 | 36 |
|
37 |
fun pretty thy funcgr = |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
38 |
AList.make (snd o Graph.get_node funcgr) (Graph.keys funcgr) |
24219 | 39 |
|> (map o apfst) (CodeUnit.string_of_const thy) |
40 |
|> sort (string_ord o pairself fst) |
|
41 |
|> map (fn (s, thms) => |
|
42 |
(Pretty.block o Pretty.fbreaks) ( |
|
43 |
Pretty.str s |
|
44 |
:: map Display.pretty_thm thms |
|
45 |
)) |
|
46 |
|> Pretty.chunks; |
|
47 |
||
48 |
||
49 |
(** generic combinators **) |
|
50 |
||
51 |
fun fold_consts f thms = |
|
52 |
thms |
|
53 |
|> maps (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of) |
|
54 |
|> (fold o fold_aterms) (fn Const c => f c | _ => I); |
|
55 |
||
56 |
fun consts_of (const, []) = [] |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
57 |
| consts_of (const, thms as _ :: _) = |
24219 | 58 |
let |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
59 |
fun the_const (c, _) = if c = const then I else insert (op =) c |
24219 | 60 |
in fold_consts the_const thms [] end; |
61 |
||
26971 | 62 |
fun insts_of thy algebra tys sorts = |
24219 | 63 |
let |
64 |
fun class_relation (x, _) _ = x; |
|
65 |
fun type_constructor tyco xs class = |
|
26971 | 66 |
(tyco, class) :: (maps o maps) fst xs; |
24219 | 67 |
fun type_variable (TVar (_, sort)) = map (pair []) sort |
68 |
| type_variable (TFree (_, sort)) = map (pair []) sort; |
|
26971 | 69 |
fun of_sort_deriv ty sort = |
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
70 |
Sorts.of_sort_derivation (Syntax.pp_global thy) algebra |
24219 | 71 |
{ class_relation = class_relation, type_constructor = type_constructor, |
72 |
type_variable = type_variable } |
|
26517 | 73 |
(ty, sort) handle Sorts.CLASS_ERROR _ => [] (*permissive!*) |
26971 | 74 |
in (flat o flat) (map2 of_sort_deriv tys sorts) end; |
24219 | 75 |
|
26971 | 76 |
fun meets_of thy algebra = |
24219 | 77 |
let |
26971 | 78 |
fun meet_of ty sort tab = |
79 |
Sorts.meet_sort algebra (ty, sort) tab |
|
80 |
handle Sorts.CLASS_ERROR _ => tab (*permissive!*); |
|
81 |
in fold2 meet_of end; |
|
24219 | 82 |
|
83 |
||
84 |
(** graph algorithm **) |
|
85 |
||
86 |
val timing = ref false; |
|
87 |
||
88 |
local |
|
89 |
||
26971 | 90 |
fun resort_thms thy algebra typ_of thms = |
91 |
let |
|
92 |
val cs = fold_consts (insert (op =)) thms []; |
|
93 |
fun meets (c, ty) = case typ_of c |
|
94 |
of SOME (vs, _) => |
|
95 |
meets_of thy algebra (Sign.const_typargs thy (c, ty)) (map snd vs) |
|
96 |
| NONE => I; |
|
97 |
val tab = fold meets cs Vartab.empty; |
|
98 |
in map (CodeUnit.inst_thm tab) thms end; |
|
24219 | 99 |
|
100 |
fun resort_funcss thy algebra funcgr = |
|
101 |
let |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
102 |
val typ_funcgr = try (fst o Graph.get_node funcgr); |
26971 | 103 |
val resort_dep = apsnd (resort_thms thy algebra typ_funcgr); |
26997
40552bbac005
permissive wrt. instantiation of class operations
haftmann
parents:
26971
diff
changeset
|
104 |
fun resort_rec typ_of (c, []) = (true, (c, [])) |
40552bbac005
permissive wrt. instantiation of class operations
haftmann
parents:
26971
diff
changeset
|
105 |
| resort_rec typ_of (c, thms as thm :: _) = if is_some (AxClass.inst_of_param thy c) |
40552bbac005
permissive wrt. instantiation of class operations
haftmann
parents:
26971
diff
changeset
|
106 |
then (true, (c, thms)) |
40552bbac005
permissive wrt. instantiation of class operations
haftmann
parents:
26971
diff
changeset
|
107 |
else let |
26971 | 108 |
val (_, (vs, ty)) = CodeUnit.head_func thm; |
109 |
val thms' as thm' :: _ = resort_thms thy algebra typ_of thms |
|
110 |
val (_, (vs', ty')) = CodeUnit.head_func thm'; (*FIXME simplify check*) |
|
26997
40552bbac005
permissive wrt. instantiation of class operations
haftmann
parents:
26971
diff
changeset
|
111 |
in (Sign.typ_equiv thy (ty, ty'), (c, thms')) end; |
24219 | 112 |
fun resort_recs funcss = |
113 |
let |
|
26971 | 114 |
fun typ_of c = case these (AList.lookup (op =) funcss c) |
115 |
of thm :: _ => (SOME o snd o CodeUnit.head_func) thm |
|
116 |
| [] => NONE; |
|
117 |
val (unchangeds, funcss') = split_list (map (resort_rec typ_of) funcss); |
|
24219 | 118 |
val unchanged = fold (fn x => fn y => x andalso y) unchangeds true; |
119 |
in (unchanged, funcss') end; |
|
120 |
fun resort_rec_until funcss = |
|
121 |
let |
|
122 |
val (unchanged, funcss') = resort_recs funcss; |
|
123 |
in if unchanged then funcss' else resort_rec_until funcss' end; |
|
124 |
in map resort_dep #> resort_rec_until end; |
|
125 |
||
126 |
fun instances_of thy algebra insts = |
|
127 |
let |
|
128 |
val thy_classes = (#classes o Sorts.rep_algebra o Sign.classes_of) thy; |
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24713
diff
changeset
|
129 |
fun all_classparams tyco class = |
24969 | 130 |
these (try (#params o AxClass.get_info thy) class) |
26517 | 131 |
|> map_filter (fn (c, _) => try (AxClass.param_of_inst thy) (c, tyco)) |
24219 | 132 |
in |
133 |
Symtab.empty |
|
134 |
|> fold (fn (tyco, class) => |
|
135 |
Symtab.map_default (tyco, []) (insert (op =) class)) insts |
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24713
diff
changeset
|
136 |
|> (fn tab => Symtab.fold (fn (tyco, classes) => append (maps (all_classparams tyco) |
24219 | 137 |
(Graph.all_succs thy_classes classes))) tab []) |
138 |
end; |
|
139 |
||
140 |
fun instances_of_consts thy algebra funcgr consts = |
|
141 |
let |
|
26971 | 142 |
fun inst (cexpr as (c, ty)) = insts_of thy algebra |
143 |
(Sign.const_typargs thy (c, ty)) ((map snd o fst) (typ funcgr c)); |
|
24219 | 144 |
in |
145 |
[] |
|
146 |
|> fold (fold (insert (op =)) o inst) consts |
|
147 |
|> instances_of thy algebra |
|
148 |
end; |
|
149 |
||
24283 | 150 |
fun ensure_const' thy algebra funcgr const auxgr = |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
151 |
if can (Graph.get_node funcgr) const |
24219 | 152 |
then (NONE, auxgr) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
153 |
else if can (Graph.get_node auxgr) const |
24219 | 154 |
then (SOME const, auxgr) |
155 |
else if is_some (Code.get_datatype_of_constr thy const) then |
|
156 |
auxgr |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
157 |
|> Graph.new_node (const, []) |
24219 | 158 |
|> pair (SOME const) |
159 |
else let |
|
160 |
val thms = Code.these_funcs thy const |
|
161 |
|> CodeUnit.norm_args |
|
162 |
|> CodeUnit.norm_varnames CodeName.purify_tvar CodeName.purify_var; |
|
163 |
val rhs = consts_of (const, thms); |
|
164 |
in |
|
165 |
auxgr |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
166 |
|> Graph.new_node (const, thms) |
24283 | 167 |
|> fold_map (ensure_const thy algebra funcgr) rhs |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
168 |
|-> (fn rhs' => fold (fn SOME const' => Graph.add_edge (const, const') |
24219 | 169 |
| NONE => I) rhs') |
170 |
|> pair (SOME const) |
|
171 |
end |
|
24283 | 172 |
and ensure_const thy algebra funcgr const = |
24219 | 173 |
let |
174 |
val timeap = if !timing |
|
175 |
then Output.timeap_msg ("time for " ^ CodeUnit.string_of_const thy const) |
|
176 |
else I; |
|
24283 | 177 |
in timeap (ensure_const' thy algebra funcgr const) end; |
24219 | 178 |
|
24283 | 179 |
fun merge_funcss thy algebra raw_funcss funcgr = |
24219 | 180 |
let |
181 |
val funcss = raw_funcss |
|
182 |
|> resort_funcss thy algebra funcgr |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
183 |
|> filter_out (can (Graph.get_node funcgr) o fst); |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
184 |
fun typ_func c [] = Code.default_typ thy c |
26997
40552bbac005
permissive wrt. instantiation of class operations
haftmann
parents:
26971
diff
changeset
|
185 |
| typ_func c (thms as thm :: _) = (snd o CodeUnit.head_func) thm; |
24219 | 186 |
fun add_funcs (const, thms) = |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
187 |
Graph.new_node (const, (typ_func const thms, thms)); |
24219 | 188 |
fun add_deps (funcs as (const, thms)) funcgr = |
189 |
let |
|
190 |
val deps = consts_of funcs; |
|
191 |
val insts = instances_of_consts thy algebra funcgr |
|
192 |
(fold_consts (insert (op =)) thms []); |
|
193 |
in |
|
194 |
funcgr |
|
26997
40552bbac005
permissive wrt. instantiation of class operations
haftmann
parents:
26971
diff
changeset
|
195 |
|> ensure_consts thy algebra insts |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
196 |
|> fold (curry Graph.add_edge const) deps |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
197 |
|> fold (curry Graph.add_edge const) insts |
24219 | 198 |
end; |
199 |
in |
|
200 |
funcgr |
|
201 |
|> fold add_funcs funcss |
|
202 |
|> fold add_deps funcss |
|
203 |
end |
|
26997
40552bbac005
permissive wrt. instantiation of class operations
haftmann
parents:
26971
diff
changeset
|
204 |
and ensure_consts thy algebra cs funcgr = |
24219 | 205 |
let |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
206 |
val auxgr = Graph.empty |
24283 | 207 |
|> fold (snd oo ensure_const thy algebra funcgr) cs; |
24219 | 208 |
in |
209 |
funcgr |
|
24283 | 210 |
|> fold (merge_funcss thy algebra) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
211 |
(map (AList.make (Graph.get_node auxgr)) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
212 |
(rev (Graph.strong_conn auxgr))) |
26997
40552bbac005
permissive wrt. instantiation of class operations
haftmann
parents:
26971
diff
changeset
|
213 |
end; |
24219 | 214 |
|
215 |
in |
|
216 |
||
217 |
(** retrieval interfaces **) |
|
218 |
||
26997
40552bbac005
permissive wrt. instantiation of class operations
haftmann
parents:
26971
diff
changeset
|
219 |
val ensure_consts = ensure_consts; |
24219 | 220 |
|
26740 | 221 |
fun proto_eval thy cterm_of evaluator_fr evaluator proto_ct funcgr = |
24219 | 222 |
let |
26740 | 223 |
val ct = cterm_of proto_ct; |
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
224 |
val _ = Sign.no_vars (Syntax.pp_global thy) (Thm.term_of ct); |
24219 | 225 |
val _ = Term.fold_types (Type.no_tvars #> K I) (Thm.term_of ct) (); |
26740 | 226 |
fun consts_of t = fold_aterms (fn Const c_ty => cons c_ty | _ => I) |
227 |
t []; |
|
228 |
val algebra = Code.coregular_algebra thy; |
|
229 |
val thm = Code.preprocess_conv ct; |
|
230 |
val ct' = Thm.rhs_of thm; |
|
231 |
val t' = Thm.term_of ct'; |
|
232 |
val consts = map fst (consts_of t'); |
|
233 |
val funcgr' = ensure_consts thy algebra consts funcgr; |
|
234 |
val (t'', evaluator') = apsnd evaluator_fr (evaluator t'); |
|
235 |
val consts' = consts_of t''; |
|
236 |
val dicts = instances_of_consts thy algebra funcgr' consts'; |
|
237 |
val funcgr'' = ensure_consts thy algebra dicts funcgr'; |
|
238 |
in (evaluator' thm funcgr'' t'', funcgr'') end; |
|
239 |
||
240 |
fun proto_eval_conv thy = |
|
241 |
let |
|
242 |
fun evaluator evaluator' thm1 funcgr t = |
|
24219 | 243 |
let |
26740 | 244 |
val thm2 = evaluator' funcgr t; |
24219 | 245 |
val thm3 = Code.postprocess_conv (Thm.rhs_of thm2); |
246 |
in |
|
26740 | 247 |
Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ => |
26971 | 248 |
error ("could not construct evaluation proof:\n" |
26928 | 249 |
^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3]) |
24219 | 250 |
end; |
26740 | 251 |
in proto_eval thy I evaluator end; |
24283 | 252 |
|
26740 | 253 |
fun proto_eval_term thy = |
24283 | 254 |
let |
26740 | 255 |
fun evaluator evaluator' _ funcgr t = evaluator' funcgr t; |
256 |
in proto_eval thy (Thm.cterm_of thy) evaluator end; |
|
24219 | 257 |
|
258 |
end; (*local*) |
|
259 |
||
260 |
structure Funcgr = CodeDataFun |
|
24713 | 261 |
( |
24219 | 262 |
type T = T; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
263 |
val empty = Graph.empty; |
27609 | 264 |
fun purge _ cs funcgr = |
265 |
Graph.del_nodes ((Graph.all_preds funcgr |
|
266 |
o filter (can (Graph.get_node funcgr))) cs) funcgr; |
|
24713 | 267 |
); |
24219 | 268 |
|
269 |
fun make thy = |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
270 |
Funcgr.change thy o ensure_consts thy (Code.coregular_algebra thy); |
24219 | 271 |
|
272 |
fun eval_conv thy f = |
|
26740 | 273 |
fst o Funcgr.change_yield thy o proto_eval_conv thy f; |
24283 | 274 |
|
275 |
fun eval_term thy f = |
|
26740 | 276 |
fst o Funcgr.change_yield thy o proto_eval_term thy f; |
24219 | 277 |
|
27103 | 278 |
|
279 |
(** diagnostic commands **) |
|
280 |
||
281 |
fun code_depgr thy [] = make thy [] |
|
282 |
| code_depgr thy consts = |
|
283 |
let |
|
284 |
val gr = make thy consts; |
|
285 |
val select = Graph.all_succs gr consts; |
|
286 |
in |
|
287 |
gr |
|
288 |
|> Graph.subgraph (member (op =) select) |
|
289 |
|> Graph.map_nodes ((apsnd o map) (AxClass.overload thy)) |
|
290 |
end; |
|
291 |
||
292 |
fun code_thms thy = Pretty.writeln o pretty thy o code_depgr thy; |
|
293 |
||
294 |
fun code_deps thy consts = |
|
295 |
let |
|
296 |
val gr = code_depgr thy consts; |
|
297 |
fun mk_entry (const, (_, (_, parents))) = |
|
298 |
let |
|
299 |
val name = CodeUnit.string_of_const thy const; |
|
300 |
val nameparents = map (CodeUnit.string_of_const thy) parents; |
|
301 |
in { name = name, ID = name, dir = "", unfold = true, |
|
302 |
path = "", parents = nameparents } |
|
303 |
end; |
|
304 |
val prgr = Graph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) gr []; |
|
305 |
in Present.display_graph prgr end; |
|
306 |
||
307 |
local |
|
308 |
||
309 |
structure P = OuterParse |
|
310 |
and K = OuterKeyword |
|
311 |
||
312 |
fun code_thms_cmd thy = code_thms thy o op @ o CodeName.read_const_exprs thy; |
|
313 |
fun code_deps_cmd thy = code_deps thy o op @ o CodeName.read_const_exprs thy; |
|
314 |
||
315 |
in |
|
316 |
||
317 |
val _ = |
|
318 |
OuterSyntax.improper_command "code_thms" "print system of defining equations for code" OuterKeyword.diag |
|
319 |
(Scan.repeat P.term |
|
320 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory |
|
321 |
o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of))); |
|
322 |
||
323 |
val _ = |
|
324 |
OuterSyntax.improper_command "code_deps" "visualize dependencies of defining equations for code" OuterKeyword.diag |
|
325 |
(Scan.repeat P.term |
|
326 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory |
|
327 |
o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of))); |
|
328 |
||
329 |
end; |
|
330 |
||
24219 | 331 |
end; (*struct*) |