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