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