author | haftmann |
Mon, 04 May 2009 14:49:51 +0200 | |
changeset 31036 | 64ff53fc0c0c |
parent 30970 | 3fe2e418a071 |
child 31063 | 88aaab83b6fc |
permissions | -rw-r--r-- |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
1 |
(* Title: Tools/code/code_wellsorted.ML |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
2 |
Author: Florian Haftmann, TU Muenchen |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
3 |
|
30061
c95e8f696b68
clarified status of variables in evaluation terms; tuned header
haftmann
parents:
30058
diff
changeset
|
4 |
Producing well-sorted systems of code equations in a graph |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
5 |
with explicit dependencies -- the Waisenhaus algorithm. |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
6 |
*) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
7 |
|
30202
2775062fd3a9
reduced confusion code_funcgr vs. code_wellsorted
haftmann
parents:
30083
diff
changeset
|
8 |
signature CODE_WELLSORTED = |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
9 |
sig |
30947 | 10 |
type code_algebra |
11 |
type code_graph |
|
12 |
val eqns: code_graph -> string -> (thm * bool) list |
|
13 |
val typ: code_graph -> string -> (string * sort) list * typ |
|
14 |
val all: code_graph -> string list |
|
15 |
val pretty: theory -> code_graph -> Pretty.T |
|
16 |
val obtain: theory -> string list -> term list -> code_algebra * code_graph |
|
17 |
val eval_conv: theory -> (sort -> sort) |
|
18 |
-> (code_algebra -> code_graph -> (string * sort) list -> term -> cterm -> thm) -> cterm -> thm |
|
30970
3fe2e418a071
generic postprocessing scheme for term evaluations
haftmann
parents:
30947
diff
changeset
|
19 |
val eval: theory -> (sort -> sort) -> ((term -> term) -> 'a -> 'a) |
30947 | 20 |
-> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
21 |
end |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
22 |
|
30202
2775062fd3a9
reduced confusion code_funcgr vs. code_wellsorted
haftmann
parents:
30083
diff
changeset
|
23 |
structure Code_Wellsorted : CODE_WELLSORTED = |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
24 |
struct |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
25 |
|
30947 | 26 |
(** the algebra and code equation graph types **) |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
27 |
|
30947 | 28 |
type code_algebra = (sort -> sort) * Sorts.algebra; |
29 |
type code_graph = (((string * sort) list * typ) * (thm * bool) list) Graph.T; |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
30 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
31 |
fun eqns eqngr = these o Option.map snd o try (Graph.get_node eqngr); |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
32 |
fun typ eqngr = fst o Graph.get_node eqngr; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
33 |
fun all eqngr = Graph.keys eqngr; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
34 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
35 |
fun pretty thy eqngr = |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
36 |
AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
37 |
|> (map o apfst) (Code_Unit.string_of_const thy) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
38 |
|> sort (string_ord o pairself fst) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
39 |
|> map (fn (s, thms) => |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
40 |
(Pretty.block o Pretty.fbreaks) ( |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
41 |
Pretty.str s |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
42 |
:: map (Display.pretty_thm o fst) thms |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
43 |
)) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
44 |
|> Pretty.chunks; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
45 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
46 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
47 |
(** the Waisenhaus algorithm **) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
48 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
49 |
(* auxiliary *) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
50 |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
51 |
fun is_proper_class thy = can (AxClass.get_info thy); |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
52 |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
53 |
fun complete_proper_sort thy = |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
54 |
Sign.complete_sort thy #> filter (is_proper_class thy); |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
55 |
|
30029 | 56 |
fun inst_params thy tyco = |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
57 |
map (fn (c, _) => AxClass.param_of_inst thy (c, tyco)) |
30029 | 58 |
o maps (#params o AxClass.get_info thy); |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
59 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
60 |
fun consts_of thy eqns = [] |> (fold o fold o fold_aterms) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
61 |
(fn Const (c, ty) => insert (op =) (c, Sign.const_typargs thy (c, Logic.unvarifyT ty)) | _ => I) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
62 |
(map (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of o fst) eqns); |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
63 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
64 |
fun tyscm_rhss_of thy c eqns = |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
65 |
let |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
66 |
val tyscm = case eqns of [] => Code.default_typscheme thy c |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
67 |
| ((thm, _) :: _) => (snd o Code_Unit.head_eqn thy) thm; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
68 |
val rhss = consts_of thy eqns; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
69 |
in (tyscm, rhss) end; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
70 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
71 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
72 |
(* data structures *) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
73 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
74 |
datatype const = Fun of string | Inst of class * string; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
75 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
76 |
fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
77 |
| const_ord (Inst class_tyco1, Inst class_tyco2) = |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
78 |
prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
79 |
| const_ord (Fun _, Inst _) = LESS |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
80 |
| const_ord (Inst _, Fun _) = GREATER; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
81 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
82 |
type var = const * int; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
83 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
84 |
structure Vargraph = |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
85 |
GraphFun(type key = var val ord = prod_ord const_ord int_ord); |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
86 |
|
30054 | 87 |
datatype styp = Tyco of string * styp list | Var of var | Free; |
88 |
||
89 |
fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys) |
|
90 |
| styp_of c_lhs (TFree (v, _)) = case c_lhs |
|
91 |
of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs) |
|
92 |
| NONE => Free; |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
93 |
|
30054 | 94 |
type vardeps_data = ((string * styp list) list * class list) Vargraph.T |
95 |
* (((string * sort) list * (thm * bool) list) Symtab.table |
|
96 |
* (class * string) list); |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
97 |
|
30054 | 98 |
val empty_vardeps_data : vardeps_data = |
99 |
(Vargraph.empty, (Symtab.empty, [])); |
|
100 |
||
30876 | 101 |
|
30054 | 102 |
(* retrieving equations and instances from the background context *) |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
103 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
104 |
fun obtain_eqns thy eqngr c = |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
105 |
case try (Graph.get_node eqngr) c |
30058 | 106 |
of SOME ((lhs, _), eqns) => ((lhs, []), []) |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
107 |
| NONE => let |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
108 |
val eqns = Code.these_eqns thy c |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
109 |
|> burrow_fst (Code_Unit.norm_args thy) |
31036 | 110 |
|> burrow_fst (Code_Unit.norm_varnames thy); |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
111 |
val ((lhs, _), rhss) = tyscm_rhss_of thy c eqns; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
112 |
in ((lhs, rhss), eqns) end; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
113 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
114 |
fun obtain_instance thy arities (inst as (class, tyco)) = |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
115 |
case AList.lookup (op =) arities inst |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
116 |
of SOME classess => (classess, ([], [])) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
117 |
| NONE => let |
30029 | 118 |
val all_classes = complete_proper_sort thy [class]; |
119 |
val superclasses = remove (op =) class all_classes |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
120 |
val classess = map (complete_proper_sort thy) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
121 |
(Sign.arity_sorts thy tyco [class]); |
30029 | 122 |
val inst_params = inst_params thy tyco all_classes; |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
123 |
in (classess, (superclasses, inst_params)) end; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
124 |
|
30054 | 125 |
|
126 |
(* computing instantiations *) |
|
127 |
||
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
128 |
fun add_classes thy arities eqngr c_k new_classes vardeps_data = |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
129 |
let |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
130 |
val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
131 |
val diff_classes = new_classes |> subtract (op =) old_classes; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
132 |
in if null diff_classes then vardeps_data |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
133 |
else let |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
134 |
val c_ks = Vargraph.imm_succs (fst vardeps_data) c_k |> insert (op =) c_k; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
135 |
in |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
136 |
vardeps_data |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
137 |
|> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes) |
30054 | 138 |
|> fold (fn styp => fold (assert_typmatch_inst thy arities eqngr styp) new_classes) styps |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
139 |
|> fold (fn c_k => add_classes thy arities eqngr c_k diff_classes) c_ks |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
140 |
end end |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
141 |
and add_styp thy arities eqngr c_k tyco_styps vardeps_data = |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
142 |
let |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
143 |
val (old_styps, classes) = Vargraph.get_node (fst vardeps_data) c_k; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
144 |
in if member (op =) old_styps tyco_styps then vardeps_data |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
145 |
else |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
146 |
vardeps_data |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
147 |
|> (apfst o Vargraph.map_node c_k o apfst) (cons tyco_styps) |
30054 | 148 |
|> fold (assert_typmatch_inst thy arities eqngr tyco_styps) classes |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
149 |
end |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
150 |
and add_dep thy arities eqngr c_k c_k' vardeps_data = |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
151 |
let |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
152 |
val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
153 |
in |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
154 |
vardeps_data |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
155 |
|> add_classes thy arities eqngr c_k' classes |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
156 |
|> apfst (Vargraph.add_edge (c_k, c_k')) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
157 |
end |
30054 | 158 |
and assert_typmatch_inst thy arities eqngr (tyco, styps) class vardeps_data = |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
159 |
if can (Sign.arity_sorts thy tyco) [class] |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
160 |
then vardeps_data |
30054 | 161 |
|> assert_inst thy arities eqngr (class, tyco) |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
162 |
|> fold_index (fn (k, styp) => |
30054 | 163 |
assert_typmatch thy arities eqngr styp (Inst (class, tyco), k)) styps |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
164 |
else vardeps_data (*permissive!*) |
30054 | 165 |
and assert_inst thy arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) = |
166 |
if member (op =) insts inst then vardeps_data |
|
167 |
else let |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
168 |
val (classess, (superclasses, inst_params)) = |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
169 |
obtain_instance thy arities inst; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
170 |
in |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
171 |
vardeps_data |
30054 | 172 |
|> (apsnd o apsnd) (insert (op =) inst) |
30083 | 173 |
|> fold_index (fn (k, _) => |
174 |
apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess |
|
30054 | 175 |
|> fold (fn superclass => assert_inst thy arities eqngr (superclass, tyco)) superclasses |
176 |
|> fold (assert_fun thy arities eqngr) inst_params |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
177 |
|> fold_index (fn (k, classes) => |
30075 | 178 |
add_classes thy arities eqngr (Inst (class, tyco), k) classes |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
179 |
#> fold (fn superclass => |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
180 |
add_dep thy arities eqngr (Inst (superclass, tyco), k) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
181 |
(Inst (class, tyco), k)) superclasses |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
182 |
#> fold (fn inst_param => |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
183 |
add_dep thy arities eqngr (Fun inst_param, k) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
184 |
(Inst (class, tyco), k) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
185 |
) inst_params |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
186 |
) classess |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
187 |
end |
30054 | 188 |
and assert_typmatch thy arities eqngr (Tyco tyco_styps) c_k vardeps_data = |
189 |
vardeps_data |
|
190 |
|> add_styp thy arities eqngr c_k tyco_styps |
|
191 |
| assert_typmatch thy arities eqngr (Var c_k') c_k vardeps_data = |
|
192 |
vardeps_data |
|
193 |
|> add_dep thy arities eqngr c_k c_k' |
|
194 |
| assert_typmatch thy arities eqngr Free c_k vardeps_data = |
|
195 |
vardeps_data |
|
196 |
and assert_rhs thy arities eqngr (c', styps) vardeps_data = |
|
197 |
vardeps_data |
|
198 |
|> assert_fun thy arities eqngr c' |
|
199 |
|> fold_index (fn (k, styp) => |
|
200 |
assert_typmatch thy arities eqngr styp (Fun c', k)) styps |
|
201 |
and assert_fun thy arities eqngr c (vardeps_data as (_, (eqntab, _))) = |
|
202 |
if Symtab.defined eqntab c then vardeps_data |
|
203 |
else let |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
204 |
val ((lhs, rhss), eqns) = obtain_eqns thy eqngr c; |
30054 | 205 |
val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss; |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
206 |
in |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
207 |
vardeps_data |
30054 | 208 |
|> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns))) |
30083 | 209 |
|> fold_index (fn (k, _) => |
210 |
apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
211 |
|> fold_index (fn (k, (_, sort)) => |
30083 | 212 |
add_classes thy arities eqngr (Fun c, k) (complete_proper_sort thy sort)) lhs |
30054 | 213 |
|> fold (assert_rhs thy arities eqngr) rhss' |
214 |
end; |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
215 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
216 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
217 |
(* applying instantiations *) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
218 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
219 |
fun dicts_of thy (proj_sort, algebra) (T, sort) = |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
220 |
let |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
221 |
fun class_relation (x, _) _ = x; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
222 |
fun type_constructor tyco xs class = |
30054 | 223 |
inst_params thy tyco (Sorts.complete_sort algebra [class]) |
224 |
@ (maps o maps) fst xs; |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
225 |
fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort); |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
226 |
in |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
227 |
flat (Sorts.of_sort_derivation (Syntax.pp_global thy) algebra |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
228 |
{ class_relation = class_relation, type_constructor = type_constructor, |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
229 |
type_variable = type_variable } (T, proj_sort sort) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
230 |
handle Sorts.CLASS_ERROR _ => [] (*permissive!*)) |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
231 |
end; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
232 |
|
30054 | 233 |
fun add_arity thy vardeps (class, tyco) = |
30058 | 234 |
AList.default (op =) |
30054 | 235 |
((class, tyco), map (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k)) |
236 |
(0 upto Sign.arity_number thy tyco - 1)); |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
237 |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
238 |
fun add_eqs thy vardeps (c, (proto_lhs, proto_eqns)) (rhss, eqngr) = |
30058 | 239 |
if can (Graph.get_node eqngr) c then (rhss, eqngr) |
240 |
else let |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
241 |
val lhs = map_index (fn (k, (v, _)) => |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
242 |
(v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
243 |
val inst_tab = Vartab.empty |> fold (fn (v, sort) => |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
244 |
Vartab.update ((v, 0), sort)) lhs; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
245 |
val eqns = proto_eqns |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
246 |
|> (map o apfst) (Code_Unit.inst_thm thy inst_tab); |
30054 | 247 |
val (tyscm, rhss') = tyscm_rhss_of thy c eqns; |
248 |
val eqngr' = Graph.new_node (c, (tyscm, eqns)) eqngr; |
|
249 |
in (map (pair c) rhss' @ rhss, eqngr') end; |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
250 |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
251 |
fun extend_arities_eqngr thy cs ts (arities, eqngr) = |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
252 |
let |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
253 |
val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) => |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
254 |
insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts []; |
30054 | 255 |
val (vardeps, (eqntab, insts)) = empty_vardeps_data |
256 |
|> fold (assert_fun thy arities eqngr) cs |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
257 |
|> fold (assert_rhs thy arities eqngr) cs_rhss; |
30054 | 258 |
val arities' = fold (add_arity thy vardeps) insts arities; |
30064 | 259 |
val pp = Syntax.pp_global thy; |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
260 |
val algebra = Sorts.subalgebra pp (is_proper_class thy) |
30064 | 261 |
(AList.lookup (op =) arities') (Sign.classes_of thy); |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
262 |
val (rhss, eqngr') = Symtab.fold (add_eqs thy vardeps) eqntab ([], eqngr); |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
263 |
fun deps_of (c, rhs) = c :: maps (dicts_of thy algebra) |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
264 |
(rhs ~~ (map snd o fst o fst o Graph.get_node eqngr') c); |
30054 | 265 |
val eqngr'' = fold (fn (c, rhs) => fold |
266 |
(curry Graph.add_edge c) (deps_of rhs)) rhss eqngr'; |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
267 |
in (algebra, (arities', eqngr'')) end; |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
268 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
269 |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
270 |
(** store **) |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
271 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
272 |
structure Wellsorted = CodeDataFun |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
273 |
( |
30947 | 274 |
type T = ((string * class) * sort list) list * code_graph; |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
275 |
val empty = ([], Graph.empty); |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
276 |
fun purge thy cs (arities, eqngr) = |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
277 |
let |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
278 |
val del_cs = ((Graph.all_preds eqngr |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
279 |
o filter (can (Graph.get_node eqngr))) cs); |
30050
916a8427f65a
first attempt to solve evaluation bootstrap problem
haftmann
parents:
30029
diff
changeset
|
280 |
val del_arities = del_cs |
916a8427f65a
first attempt to solve evaluation bootstrap problem
haftmann
parents:
30029
diff
changeset
|
281 |
|> map_filter (AxClass.inst_of_param thy) |
916a8427f65a
first attempt to solve evaluation bootstrap problem
haftmann
parents:
30029
diff
changeset
|
282 |
|> maps (fn (c, tyco) => |
916a8427f65a
first attempt to solve evaluation bootstrap problem
haftmann
parents:
30029
diff
changeset
|
283 |
(map (rpair tyco) o Sign.complete_sort thy o the_list |
916a8427f65a
first attempt to solve evaluation bootstrap problem
haftmann
parents:
30029
diff
changeset
|
284 |
o AxClass.class_of_param thy) c); |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
285 |
val arities' = fold (AList.delete (op =)) del_arities arities; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
286 |
val eqngr' = Graph.del_nodes del_cs eqngr; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
287 |
in (arities', eqngr') end; |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
288 |
); |
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
289 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
290 |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
291 |
(** retrieval interfaces **) |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
292 |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
293 |
fun obtain thy cs ts = apsnd snd |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
294 |
(Wellsorted.change_yield thy (extend_arities_eqngr thy cs ts)); |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
295 |
|
30947 | 296 |
fun prepare_sorts prep_sort (Const (c, ty)) = Const (c, map_type_tfree |
297 |
(fn (v, sort) => TFree (v, prep_sort sort)) ty) |
|
298 |
| prepare_sorts prep_sort (t1 $ t2) = |
|
299 |
prepare_sorts prep_sort t1 $ prepare_sorts prep_sort t2 |
|
300 |
| prepare_sorts prep_sort (Abs (v, ty, t)) = |
|
301 |
Abs (v, Type.strip_sorts ty, prepare_sorts prep_sort t) |
|
302 |
| prepare_sorts _ (Term.Free (v, ty)) = Term.Free (v, Type.strip_sorts ty) |
|
303 |
| prepare_sorts _ (t as Bound _) = t; |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
304 |
|
30947 | 305 |
fun gen_eval thy cterm_of conclude_evaluation prep_sort evaluator proto_ct = |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
306 |
let |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
307 |
val ct = cterm_of proto_ct; |
30947 | 308 |
val _ = (Term.map_types Type.no_tvars o Sign.no_vars (Syntax.pp_global thy)) |
309 |
(Thm.term_of ct); |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
310 |
val thm = Code.preprocess_conv thy ct; |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
311 |
val ct' = Thm.rhs_of thm; |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
312 |
val t' = Thm.term_of ct'; |
30947 | 313 |
val vs = Term.add_tfrees t' []; |
314 |
val consts = fold_aterms |
|
315 |
(fn Const (c, _) => insert (op =) c | _ => I) t' []; |
|
316 |
val t'' = prepare_sorts prep_sort t'; |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
317 |
val (algebra', eqngr') = obtain thy consts [t'']; |
30947 | 318 |
in conclude_evaluation (evaluator algebra' eqngr' vs t'' ct') thm end; |
319 |
||
320 |
fun simple_evaluator evaluator algebra eqngr vs t ct = |
|
321 |
evaluator algebra eqngr vs t; |
|
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
322 |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
323 |
fun eval_conv thy = |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
324 |
let |
30947 | 325 |
fun conclude_evaluation thm2 thm1 = |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
326 |
let |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
327 |
val thm3 = Code.postprocess_conv thy (Thm.rhs_of thm2); |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
328 |
in |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
329 |
Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ => |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
330 |
error ("could not construct evaluation proof:\n" |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
331 |
^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3]) |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset
|
332 |
end; |
30947 | 333 |
in gen_eval thy I conclude_evaluation end; |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
334 |
|
30970
3fe2e418a071
generic postprocessing scheme for term evaluations
haftmann
parents:
30947
diff
changeset
|
335 |
fun eval thy prep_sort postproc evaluator = gen_eval thy (Thm.cterm_of thy) |
3fe2e418a071
generic postprocessing scheme for term evaluations
haftmann
parents:
30947
diff
changeset
|
336 |
(K o postproc (Code.postprocess_term thy)) prep_sort (simple_evaluator evaluator); |
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
337 |
|
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset
|
338 |
end; (*struct*) |