haftmann@29947
|
1 |
(* Title: Tools/code/code_wellsorted.ML
|
haftmann@29947
|
2 |
Author: Florian Haftmann, TU Muenchen
|
haftmann@29947
|
3 |
|
haftmann@29947
|
4 |
Retrieving, well-sorting and structuring code equations in graph
|
haftmann@29947
|
5 |
with explicit dependencies -- the Waisenhaus algorithm.
|
haftmann@29947
|
6 |
*)
|
haftmann@29947
|
7 |
|
haftmann@29947
|
8 |
signature CODE_FUNCGR =
|
haftmann@29947
|
9 |
sig
|
haftmann@29947
|
10 |
type T
|
haftmann@29947
|
11 |
val eqns: T -> string -> (thm * bool) list
|
haftmann@29947
|
12 |
val typ: T -> string -> (string * sort) list * typ
|
haftmann@29947
|
13 |
val all: T -> string list
|
haftmann@29947
|
14 |
val pretty: theory -> T -> Pretty.T
|
haftmann@29947
|
15 |
val make: theory -> string list
|
haftmann@29947
|
16 |
-> ((sort -> sort) * Sorts.algebra) * T
|
haftmann@29947
|
17 |
val eval_conv: theory
|
haftmann@29947
|
18 |
-> (term -> term * (((sort -> sort) * Sorts.algebra) -> T -> thm)) -> cterm -> thm
|
haftmann@29947
|
19 |
val eval_term: theory
|
haftmann@29947
|
20 |
-> (term -> term * (((sort -> sort) * Sorts.algebra) -> T -> 'a)) -> term -> 'a
|
haftmann@29947
|
21 |
end
|
haftmann@29947
|
22 |
|
haftmann@29947
|
23 |
structure Code_Funcgr : CODE_FUNCGR =
|
haftmann@29947
|
24 |
struct
|
haftmann@29947
|
25 |
|
haftmann@29947
|
26 |
(** the equation graph type **)
|
haftmann@29947
|
27 |
|
haftmann@29947
|
28 |
type T = (((string * sort) list * typ) * (thm * bool) list) Graph.T;
|
haftmann@29947
|
29 |
|
haftmann@29947
|
30 |
fun eqns eqngr = these o Option.map snd o try (Graph.get_node eqngr);
|
haftmann@29947
|
31 |
fun typ eqngr = fst o Graph.get_node eqngr;
|
haftmann@29947
|
32 |
fun all eqngr = Graph.keys eqngr;
|
haftmann@29947
|
33 |
|
haftmann@29947
|
34 |
fun pretty thy eqngr =
|
haftmann@29947
|
35 |
AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
|
haftmann@29947
|
36 |
|> (map o apfst) (Code_Unit.string_of_const thy)
|
haftmann@29947
|
37 |
|> sort (string_ord o pairself fst)
|
haftmann@29947
|
38 |
|> map (fn (s, thms) =>
|
haftmann@29947
|
39 |
(Pretty.block o Pretty.fbreaks) (
|
haftmann@29947
|
40 |
Pretty.str s
|
haftmann@29947
|
41 |
:: map (Display.pretty_thm o fst) thms
|
haftmann@29947
|
42 |
))
|
haftmann@29947
|
43 |
|> Pretty.chunks;
|
haftmann@29947
|
44 |
|
haftmann@29947
|
45 |
|
haftmann@29947
|
46 |
(** the Waisenhaus algorithm **)
|
haftmann@29947
|
47 |
|
haftmann@29947
|
48 |
(* auxiliary *)
|
haftmann@29947
|
49 |
|
haftmann@29947
|
50 |
fun complete_proper_sort thy =
|
haftmann@29947
|
51 |
Sign.complete_sort thy #> filter (can (AxClass.get_info thy));
|
haftmann@29947
|
52 |
|
haftmann@29966
|
53 |
fun inst_params thy tyco =
|
haftmann@29947
|
54 |
map (fn (c, _) => AxClass.param_of_inst thy (c, tyco))
|
haftmann@29966
|
55 |
o maps (#params o AxClass.get_info thy);
|
haftmann@29947
|
56 |
|
haftmann@29947
|
57 |
fun consts_of thy eqns = [] |> (fold o fold o fold_aterms)
|
haftmann@29947
|
58 |
(fn Const (c, ty) => insert (op =) (c, Sign.const_typargs thy (c, Logic.unvarifyT ty)) | _ => I)
|
haftmann@29947
|
59 |
(map (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of o fst) eqns);
|
haftmann@29947
|
60 |
|
haftmann@29947
|
61 |
fun tyscm_rhss_of thy c eqns =
|
haftmann@29947
|
62 |
let
|
haftmann@29947
|
63 |
val tyscm = case eqns of [] => Code.default_typscheme thy c
|
haftmann@29947
|
64 |
| ((thm, _) :: _) => (snd o Code_Unit.head_eqn thy) thm;
|
haftmann@29947
|
65 |
val rhss = consts_of thy eqns;
|
haftmann@29947
|
66 |
in (tyscm, rhss) end;
|
haftmann@29947
|
67 |
|
haftmann@29947
|
68 |
|
haftmann@29947
|
69 |
(* data structures *)
|
haftmann@29947
|
70 |
|
haftmann@29947
|
71 |
datatype const = Fun of string | Inst of class * string;
|
haftmann@29947
|
72 |
|
haftmann@29947
|
73 |
fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
|
haftmann@29947
|
74 |
| const_ord (Inst class_tyco1, Inst class_tyco2) =
|
haftmann@29947
|
75 |
prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
|
haftmann@29947
|
76 |
| const_ord (Fun _, Inst _) = LESS
|
haftmann@29947
|
77 |
| const_ord (Inst _, Fun _) = GREATER;
|
haftmann@29947
|
78 |
|
haftmann@29947
|
79 |
type var = const * int;
|
haftmann@29947
|
80 |
|
haftmann@29947
|
81 |
structure Vargraph =
|
haftmann@29947
|
82 |
GraphFun(type key = var val ord = prod_ord const_ord int_ord);
|
haftmann@29947
|
83 |
|
haftmann@29991
|
84 |
datatype styp = Tyco of string * styp list | Var of var | Free;
|
haftmann@29947
|
85 |
|
haftmann@29991
|
86 |
fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys)
|
haftmann@29991
|
87 |
| styp_of c_lhs (TFree (v, _)) = case c_lhs
|
haftmann@29991
|
88 |
of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs)
|
haftmann@29991
|
89 |
| NONE => Free;
|
haftmann@29947
|
90 |
|
haftmann@29991
|
91 |
type vardeps_data = ((string * styp list) list * class list) Vargraph.T
|
haftmann@29991
|
92 |
* (((string * sort) list * (thm * bool) list) Symtab.table
|
haftmann@29991
|
93 |
* (class * string) list);
|
haftmann@29991
|
94 |
|
haftmann@29991
|
95 |
val empty_vardeps_data : vardeps_data =
|
haftmann@29991
|
96 |
(Vargraph.empty, (Symtab.empty, []));
|
haftmann@29991
|
97 |
|
haftmann@29991
|
98 |
(* retrieving equations and instances from the background context *)
|
haftmann@29947
|
99 |
|
haftmann@29947
|
100 |
fun obtain_eqns thy eqngr c =
|
haftmann@29947
|
101 |
case try (Graph.get_node eqngr) c
|
haftmann@29947
|
102 |
of SOME ((lhs, _), eqns) => ((lhs, []), eqns)
|
haftmann@29947
|
103 |
| NONE => let
|
haftmann@29947
|
104 |
val eqns = Code.these_eqns thy c
|
haftmann@29947
|
105 |
|> burrow_fst (Code_Unit.norm_args thy)
|
haftmann@29947
|
106 |
|> burrow_fst (Code_Unit.norm_varnames thy Code_Name.purify_tvar Code_Name.purify_var);
|
haftmann@29947
|
107 |
val ((lhs, _), rhss) = tyscm_rhss_of thy c eqns;
|
haftmann@29947
|
108 |
in ((lhs, rhss), eqns) end;
|
haftmann@29947
|
109 |
|
haftmann@29947
|
110 |
fun obtain_instance thy arities (inst as (class, tyco)) =
|
haftmann@29947
|
111 |
case AList.lookup (op =) arities inst
|
haftmann@29947
|
112 |
of SOME classess => (classess, ([], []))
|
haftmann@29947
|
113 |
| NONE => let
|
haftmann@29966
|
114 |
val all_classes = complete_proper_sort thy [class];
|
haftmann@29966
|
115 |
val superclasses = remove (op =) class all_classes
|
haftmann@29947
|
116 |
val classess = map (complete_proper_sort thy)
|
haftmann@29947
|
117 |
(Sign.arity_sorts thy tyco [class]);
|
haftmann@29966
|
118 |
val inst_params = inst_params thy tyco all_classes;
|
haftmann@29947
|
119 |
in (classess, (superclasses, inst_params)) end;
|
haftmann@29947
|
120 |
|
haftmann@29991
|
121 |
|
haftmann@29991
|
122 |
(* computing instantiations *)
|
haftmann@29991
|
123 |
|
haftmann@29947
|
124 |
fun add_classes thy arities eqngr c_k new_classes vardeps_data =
|
haftmann@29947
|
125 |
let
|
haftmann@29947
|
126 |
val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k;
|
haftmann@29947
|
127 |
val diff_classes = new_classes |> subtract (op =) old_classes;
|
haftmann@29947
|
128 |
in if null diff_classes then vardeps_data
|
haftmann@29947
|
129 |
else let
|
haftmann@29947
|
130 |
val c_ks = Vargraph.imm_succs (fst vardeps_data) c_k |> insert (op =) c_k;
|
haftmann@29947
|
131 |
in
|
haftmann@29947
|
132 |
vardeps_data
|
haftmann@29947
|
133 |
|> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes)
|
haftmann@29991
|
134 |
|> fold (fn styp => fold (assert_typmatch_inst thy arities eqngr styp) new_classes) styps
|
haftmann@29947
|
135 |
|> fold (fn c_k => add_classes thy arities eqngr c_k diff_classes) c_ks
|
haftmann@29947
|
136 |
end end
|
haftmann@29947
|
137 |
and add_styp thy arities eqngr c_k tyco_styps vardeps_data =
|
haftmann@29947
|
138 |
let
|
haftmann@29947
|
139 |
val (old_styps, classes) = Vargraph.get_node (fst vardeps_data) c_k;
|
haftmann@29947
|
140 |
in if member (op =) old_styps tyco_styps then vardeps_data
|
haftmann@29947
|
141 |
else
|
haftmann@29947
|
142 |
vardeps_data
|
haftmann@29947
|
143 |
|> (apfst o Vargraph.map_node c_k o apfst) (cons tyco_styps)
|
haftmann@29991
|
144 |
|> fold (assert_typmatch_inst thy arities eqngr tyco_styps) classes
|
haftmann@29947
|
145 |
end
|
haftmann@29947
|
146 |
and add_dep thy arities eqngr c_k c_k' vardeps_data =
|
haftmann@29947
|
147 |
let
|
haftmann@29947
|
148 |
val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k;
|
haftmann@29947
|
149 |
in
|
haftmann@29947
|
150 |
vardeps_data
|
haftmann@29947
|
151 |
|> add_classes thy arities eqngr c_k' classes
|
haftmann@29947
|
152 |
|> apfst (Vargraph.add_edge (c_k, c_k'))
|
haftmann@29947
|
153 |
end
|
haftmann@29991
|
154 |
and assert_typmatch_inst thy arities eqngr (tyco, styps) class vardeps_data =
|
haftmann@29947
|
155 |
if can (Sign.arity_sorts thy tyco) [class]
|
haftmann@29947
|
156 |
then vardeps_data
|
haftmann@29991
|
157 |
|> assert_inst thy arities eqngr (class, tyco)
|
haftmann@29947
|
158 |
|> fold_index (fn (k, styp) =>
|
haftmann@29991
|
159 |
assert_typmatch thy arities eqngr styp (Inst (class, tyco), k)) styps
|
haftmann@29947
|
160 |
else vardeps_data (*permissive!*)
|
haftmann@29991
|
161 |
and assert_inst thy arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) =
|
haftmann@29991
|
162 |
if member (op =) insts inst then vardeps_data
|
haftmann@29991
|
163 |
else let
|
haftmann@29947
|
164 |
val (classess, (superclasses, inst_params)) =
|
haftmann@29947
|
165 |
obtain_instance thy arities inst;
|
haftmann@29947
|
166 |
in
|
haftmann@29947
|
167 |
vardeps_data
|
haftmann@29991
|
168 |
|> (apsnd o apsnd) (insert (op =) inst)
|
haftmann@29991
|
169 |
|> fold (fn superclass => assert_inst thy arities eqngr (superclass, tyco)) superclasses
|
haftmann@29991
|
170 |
|> fold (assert_fun thy arities eqngr) inst_params
|
haftmann@29947
|
171 |
|> fold_index (fn (k, classes) =>
|
haftmann@29947
|
172 |
apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))
|
haftmann@29947
|
173 |
#> add_classes thy arities eqngr (Inst (class, tyco), k) classes
|
haftmann@29947
|
174 |
#> fold (fn superclass =>
|
haftmann@29947
|
175 |
add_dep thy arities eqngr (Inst (superclass, tyco), k)
|
haftmann@29947
|
176 |
(Inst (class, tyco), k)) superclasses
|
haftmann@29947
|
177 |
#> fold (fn inst_param =>
|
haftmann@29947
|
178 |
add_dep thy arities eqngr (Fun inst_param, k)
|
haftmann@29947
|
179 |
(Inst (class, tyco), k)
|
haftmann@29947
|
180 |
) inst_params
|
haftmann@29947
|
181 |
) classess
|
haftmann@29947
|
182 |
end
|
haftmann@29991
|
183 |
and assert_typmatch thy arities eqngr (Tyco tyco_styps) c_k vardeps_data =
|
haftmann@29991
|
184 |
vardeps_data
|
haftmann@29991
|
185 |
|> add_styp thy arities eqngr c_k tyco_styps
|
haftmann@29991
|
186 |
| assert_typmatch thy arities eqngr (Var c_k') c_k vardeps_data =
|
haftmann@29991
|
187 |
vardeps_data
|
haftmann@29991
|
188 |
|> add_dep thy arities eqngr c_k c_k'
|
haftmann@29991
|
189 |
| assert_typmatch thy arities eqngr Free c_k vardeps_data =
|
haftmann@29991
|
190 |
vardeps_data
|
haftmann@29991
|
191 |
and assert_rhs thy arities eqngr (c', styps) vardeps_data =
|
haftmann@29991
|
192 |
vardeps_data
|
haftmann@29991
|
193 |
|> assert_fun thy arities eqngr c'
|
haftmann@29991
|
194 |
|> fold_index (fn (k, styp) =>
|
haftmann@29991
|
195 |
assert_typmatch thy arities eqngr styp (Fun c', k)) styps
|
haftmann@29991
|
196 |
and assert_fun thy arities eqngr c (vardeps_data as (_, (eqntab, _))) =
|
haftmann@29991
|
197 |
if Symtab.defined eqntab c then vardeps_data
|
haftmann@29991
|
198 |
else let
|
haftmann@29947
|
199 |
val ((lhs, rhss), eqns) = obtain_eqns thy eqngr c;
|
haftmann@29991
|
200 |
val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss;
|
haftmann@29947
|
201 |
in
|
haftmann@29947
|
202 |
vardeps_data
|
haftmann@29991
|
203 |
|> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns)))
|
haftmann@29947
|
204 |
|> fold_index (fn (k, (_, sort)) =>
|
haftmann@29947
|
205 |
apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))
|
haftmann@29947
|
206 |
#> add_classes thy arities eqngr (Fun c, k) (complete_proper_sort thy sort)) lhs
|
haftmann@29991
|
207 |
|> fold (assert_rhs thy arities eqngr) rhss'
|
haftmann@29991
|
208 |
end;
|
haftmann@29947
|
209 |
|
haftmann@29947
|
210 |
|
haftmann@29947
|
211 |
(* applying instantiations *)
|
haftmann@29947
|
212 |
|
haftmann@29947
|
213 |
fun build_algebra thy arities =
|
haftmann@29947
|
214 |
let
|
haftmann@29947
|
215 |
val pp = Syntax.pp_global thy;
|
haftmann@29947
|
216 |
val thy_algebra = Sign.classes_of thy;
|
haftmann@29947
|
217 |
val is_proper = can (AxClass.get_info thy);
|
haftmann@29947
|
218 |
val classrels = Sorts.classrels_of thy_algebra
|
haftmann@29947
|
219 |
|> filter (is_proper o fst)
|
haftmann@29947
|
220 |
|> (map o apsnd) (filter is_proper);
|
haftmann@29947
|
221 |
val instances = Sorts.instances_of thy_algebra
|
haftmann@29991
|
222 |
|> filter (is_proper o snd)
|
haftmann@29991
|
223 |
|> map swap (*FIXME*)
|
haftmann@29947
|
224 |
fun add_class (class, superclasses) algebra =
|
haftmann@29947
|
225 |
Sorts.add_class pp (class, Sorts.minimize_sort algebra superclasses) algebra;
|
haftmann@29991
|
226 |
fun add_arity (class, tyco) algebra = Sorts.add_arities pp
|
haftmann@29991
|
227 |
(tyco, [(class, ((map (Sorts.minimize_sort algebra) o the
|
haftmann@29991
|
228 |
o AList.lookup (op =) arities) (class, tyco)))]) algebra;
|
haftmann@29947
|
229 |
in
|
haftmann@29947
|
230 |
Sorts.empty_algebra
|
haftmann@29947
|
231 |
|> fold add_class classrels
|
haftmann@29947
|
232 |
|> fold add_arity instances
|
haftmann@29947
|
233 |
end;
|
haftmann@29947
|
234 |
|
haftmann@29947
|
235 |
fun dicts_of thy (proj_sort, algebra) (T, sort) =
|
haftmann@29947
|
236 |
let
|
haftmann@29947
|
237 |
fun class_relation (x, _) _ = x;
|
haftmann@29947
|
238 |
fun type_constructor tyco xs class =
|
haftmann@29991
|
239 |
inst_params thy tyco (Sorts.complete_sort algebra [class])
|
haftmann@29991
|
240 |
@ (maps o maps) fst xs;
|
haftmann@29947
|
241 |
fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort);
|
haftmann@29947
|
242 |
in
|
haftmann@29947
|
243 |
flat (Sorts.of_sort_derivation (Syntax.pp_global thy) algebra
|
haftmann@29947
|
244 |
{ class_relation = class_relation, type_constructor = type_constructor,
|
haftmann@29947
|
245 |
type_variable = type_variable } (T, proj_sort sort)
|
haftmann@29947
|
246 |
handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
|
haftmann@29947
|
247 |
end;
|
haftmann@29947
|
248 |
|
haftmann@29991
|
249 |
fun add_arity thy vardeps (class, tyco) =
|
haftmann@29991
|
250 |
AList.update (op =)
|
haftmann@29991
|
251 |
((class, tyco), map (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k))
|
haftmann@29991
|
252 |
(0 upto Sign.arity_number thy tyco - 1));
|
haftmann@29947
|
253 |
|
haftmann@29991
|
254 |
fun add_eqs thy (proj_sort, algebra) vardeps
|
haftmann@29991
|
255 |
(c, (proto_lhs, proto_eqns)) (rhss, eqngr) =
|
haftmann@29947
|
256 |
let
|
haftmann@29947
|
257 |
val lhs = map_index (fn (k, (v, _)) =>
|
haftmann@29947
|
258 |
(v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
|
haftmann@29947
|
259 |
val inst_tab = Vartab.empty |> fold (fn (v, sort) =>
|
haftmann@29947
|
260 |
Vartab.update ((v, 0), sort)) lhs;
|
haftmann@29947
|
261 |
val eqns = proto_eqns
|
haftmann@29947
|
262 |
|> (map o apfst) (Code_Unit.inst_thm thy inst_tab);
|
haftmann@29991
|
263 |
val (tyscm, rhss') = tyscm_rhss_of thy c eqns;
|
haftmann@29991
|
264 |
val eqngr' = Graph.new_node (c, (tyscm, eqns)) eqngr;
|
haftmann@29991
|
265 |
in (map (pair c) rhss' @ rhss, eqngr') end;
|
haftmann@29947
|
266 |
|
haftmann@29991
|
267 |
fun extend_arities_eqngr thy cs cs_rhss (arities, eqngr) =
|
haftmann@29947
|
268 |
let
|
haftmann@29991
|
269 |
val cs_rhss' = (map o apsnd o map) (styp_of NONE) cs_rhss;
|
haftmann@29991
|
270 |
val (vardeps, (eqntab, insts)) = empty_vardeps_data
|
haftmann@29991
|
271 |
|> fold (assert_fun thy arities eqngr) cs
|
haftmann@29991
|
272 |
|> fold (assert_rhs thy arities eqngr) cs_rhss';
|
haftmann@29991
|
273 |
val arities' = fold (add_arity thy vardeps) insts arities;
|
haftmann@29947
|
274 |
val algebra = build_algebra thy arities';
|
haftmann@29947
|
275 |
val proj_sort = complete_proper_sort thy #> Sorts.minimize_sort algebra;
|
haftmann@29991
|
276 |
val (rhss, eqngr') = Symtab.fold
|
haftmann@29991
|
277 |
(add_eqs thy (proj_sort, algebra) vardeps) eqntab ([], eqngr);
|
haftmann@29991
|
278 |
fun deps_of (c, rhs) = c ::
|
haftmann@29991
|
279 |
maps (dicts_of thy (proj_sort, algebra))
|
haftmann@29991
|
280 |
(rhs ~~ (map snd o fst o fst o Graph.get_node eqngr') c);
|
haftmann@29991
|
281 |
val eqngr'' = fold (fn (c, rhs) => fold
|
haftmann@29991
|
282 |
(curry Graph.add_edge c) (deps_of rhs)) rhss eqngr';
|
haftmann@29991
|
283 |
in ((proj_sort, algebra), (arities', eqngr'')) end;
|
haftmann@29947
|
284 |
|
haftmann@29947
|
285 |
|
haftmann@29947
|
286 |
(** retrieval interfaces **)
|
haftmann@29947
|
287 |
|
haftmann@29947
|
288 |
fun proto_eval thy cterm_of evaluator_lift evaluator proto_ct arities_eqngr =
|
haftmann@29947
|
289 |
let
|
haftmann@29947
|
290 |
val ct = cterm_of proto_ct;
|
haftmann@29947
|
291 |
val _ = Sign.no_vars (Syntax.pp_global thy) (Thm.term_of ct);
|
haftmann@29947
|
292 |
val _ = Term.fold_types (Type.no_tvars #> K I) (Thm.term_of ct) ();
|
haftmann@29947
|
293 |
fun consts_of t =
|
haftmann@29947
|
294 |
fold_aterms (fn Const c_ty => cons c_ty | _ => I) t [];
|
haftmann@29947
|
295 |
val thm = Code.preprocess_conv thy ct;
|
haftmann@29947
|
296 |
val ct' = Thm.rhs_of thm;
|
haftmann@29947
|
297 |
val t' = Thm.term_of ct';
|
haftmann@29991
|
298 |
val (t'', evaluator_eqngr) = evaluator t';
|
haftmann@29947
|
299 |
val consts = map fst (consts_of t');
|
haftmann@29947
|
300 |
val consts' = consts_of t'';
|
haftmann@29991
|
301 |
val const_matches' = fold (fn (c, ty) =>
|
haftmann@29991
|
302 |
insert (op =) (c, Sign.const_typargs thy (c, Logic.unvarifyT ty))) consts' [];
|
haftmann@29991
|
303 |
val (algebra', arities_eqngr') =
|
haftmann@29991
|
304 |
extend_arities_eqngr thy consts const_matches' arities_eqngr;
|
haftmann@29991
|
305 |
in
|
haftmann@29991
|
306 |
(evaluator_lift (evaluator_eqngr algebra') thm (snd arities_eqngr'),
|
haftmann@29991
|
307 |
arities_eqngr')
|
haftmann@29991
|
308 |
end;
|
haftmann@29947
|
309 |
|
haftmann@29947
|
310 |
fun proto_eval_conv thy =
|
haftmann@29947
|
311 |
let
|
haftmann@29947
|
312 |
fun evaluator_lift evaluator thm1 eqngr =
|
haftmann@29947
|
313 |
let
|
haftmann@29947
|
314 |
val thm2 = evaluator eqngr;
|
haftmann@29947
|
315 |
val thm3 = Code.postprocess_conv thy (Thm.rhs_of thm2);
|
haftmann@29947
|
316 |
in
|
haftmann@29947
|
317 |
Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
|
haftmann@29947
|
318 |
error ("could not construct evaluation proof:\n"
|
haftmann@29947
|
319 |
^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
|
haftmann@29947
|
320 |
end;
|
haftmann@29947
|
321 |
in proto_eval thy I evaluator_lift end;
|
haftmann@29947
|
322 |
|
haftmann@29947
|
323 |
fun proto_eval_term thy =
|
haftmann@29947
|
324 |
let
|
haftmann@29947
|
325 |
fun evaluator_lift evaluator _ eqngr = evaluator eqngr;
|
haftmann@29947
|
326 |
in proto_eval thy (Thm.cterm_of thy) evaluator_lift end;
|
haftmann@29947
|
327 |
|
haftmann@29947
|
328 |
structure Wellsorted = CodeDataFun
|
haftmann@29947
|
329 |
(
|
haftmann@29947
|
330 |
type T = ((string * class) * sort list) list * T;
|
haftmann@29947
|
331 |
val empty = ([], Graph.empty);
|
haftmann@29947
|
332 |
fun purge thy cs (arities, eqngr) =
|
haftmann@29947
|
333 |
let
|
haftmann@29947
|
334 |
val del_cs = ((Graph.all_preds eqngr
|
haftmann@29947
|
335 |
o filter (can (Graph.get_node eqngr))) cs);
|
haftmann@29987
|
336 |
val del_arities = del_cs
|
haftmann@29987
|
337 |
|> map_filter (AxClass.inst_of_param thy)
|
haftmann@29987
|
338 |
|> maps (fn (c, tyco) =>
|
haftmann@29987
|
339 |
(map (rpair tyco) o Sign.complete_sort thy o the_list
|
haftmann@29987
|
340 |
o AxClass.class_of_param thy) c);
|
haftmann@29947
|
341 |
val arities' = fold (AList.delete (op =)) del_arities arities;
|
haftmann@29947
|
342 |
val eqngr' = Graph.del_nodes del_cs eqngr;
|
haftmann@29947
|
343 |
in (arities', eqngr') end;
|
haftmann@29947
|
344 |
);
|
haftmann@29947
|
345 |
|
haftmann@29987
|
346 |
fun make thy cs = apsnd snd
|
haftmann@29987
|
347 |
(Wellsorted.change_yield thy (extend_arities_eqngr thy cs []));
|
haftmann@29947
|
348 |
|
haftmann@29947
|
349 |
fun eval_conv thy f =
|
haftmann@29947
|
350 |
fst o Wellsorted.change_yield thy o proto_eval_conv thy f;
|
haftmann@29947
|
351 |
|
haftmann@29947
|
352 |
fun eval_term thy f =
|
haftmann@29947
|
353 |
fst o Wellsorted.change_yield thy o proto_eval_term thy f;
|
haftmann@29947
|
354 |
|
haftmann@29947
|
355 |
|
haftmann@29947
|
356 |
(** diagnostic commands **)
|
haftmann@29947
|
357 |
|
haftmann@29947
|
358 |
fun code_depgr thy consts =
|
haftmann@29947
|
359 |
let
|
haftmann@29947
|
360 |
val (_, eqngr) = make thy consts;
|
haftmann@29947
|
361 |
val select = Graph.all_succs eqngr consts;
|
haftmann@29947
|
362 |
in
|
haftmann@29947
|
363 |
eqngr
|
haftmann@29947
|
364 |
|> not (null consts) ? Graph.subgraph (member (op =) select)
|
haftmann@29947
|
365 |
|> Graph.map_nodes ((apsnd o map o apfst) (AxClass.overload thy))
|
haftmann@29947
|
366 |
end;
|
haftmann@29947
|
367 |
|
haftmann@29947
|
368 |
fun code_thms thy = Pretty.writeln o pretty thy o code_depgr thy;
|
haftmann@29947
|
369 |
|
haftmann@29947
|
370 |
fun code_deps thy consts =
|
haftmann@29947
|
371 |
let
|
haftmann@29947
|
372 |
val eqngr = code_depgr thy consts;
|
haftmann@29947
|
373 |
fun mk_entry (const, (_, (_, parents))) =
|
haftmann@29947
|
374 |
let
|
haftmann@29947
|
375 |
val name = Code_Unit.string_of_const thy const;
|
haftmann@29947
|
376 |
val nameparents = map (Code_Unit.string_of_const thy) parents;
|
haftmann@29947
|
377 |
in { name = name, ID = name, dir = "", unfold = true,
|
haftmann@29947
|
378 |
path = "", parents = nameparents }
|
haftmann@29947
|
379 |
end;
|
haftmann@29947
|
380 |
val prgr = Graph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) eqngr [];
|
haftmann@29947
|
381 |
in Present.display_graph prgr end;
|
haftmann@29947
|
382 |
|
haftmann@29947
|
383 |
local
|
haftmann@29947
|
384 |
|
haftmann@29947
|
385 |
structure P = OuterParse
|
haftmann@29947
|
386 |
and K = OuterKeyword
|
haftmann@29947
|
387 |
|
haftmann@29947
|
388 |
fun code_thms_cmd thy = code_thms thy o op @ o Code_Name.read_const_exprs thy;
|
haftmann@29947
|
389 |
fun code_deps_cmd thy = code_deps thy o op @ o Code_Name.read_const_exprs thy;
|
haftmann@29947
|
390 |
|
haftmann@29947
|
391 |
in
|
haftmann@29947
|
392 |
|
haftmann@29947
|
393 |
val _ =
|
haftmann@29961
|
394 |
OuterSyntax.improper_command "code_thms" "print system of code equations for code" OuterKeyword.diag
|
haftmann@29947
|
395 |
(Scan.repeat P.term_group
|
haftmann@29947
|
396 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
|
haftmann@29947
|
397 |
o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of)));
|
haftmann@29947
|
398 |
|
haftmann@29947
|
399 |
val _ =
|
haftmann@29961
|
400 |
OuterSyntax.improper_command "code_deps" "visualize dependencies of code equations for code" OuterKeyword.diag
|
haftmann@29947
|
401 |
(Scan.repeat P.term_group
|
haftmann@29947
|
402 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
|
haftmann@29947
|
403 |
o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of)));
|
haftmann@29947
|
404 |
|
haftmann@29947
|
405 |
end;
|
haftmann@29947
|
406 |
|
haftmann@29947
|
407 |
end; (*struct*)
|