author | huffman |
Mon, 26 Apr 2010 10:57:04 -0700 | |
changeset 36366 | 886b94b1bed7 |
parent 36328 | 4d9deabf6474 |
child 36429 | 9d6b3be996d4 |
permissions | -rw-r--r-- |
2956 | 1 |
(* Title: Pure/sorts.ML |
2 |
Author: Markus Wenzel and Stefan Berghofer, TU Muenchen |
|
3 |
||
19514 | 4 |
The order-sorted algebra of type classes. |
19529 | 5 |
|
6 |
Classes denote (possibly empty) collections of types that are |
|
7 |
partially ordered by class inclusion. They are represented |
|
8 |
symbolically by strings. |
|
9 |
||
10 |
Sorts are intersections of finitely many classes. They are represented |
|
11 |
by lists of classes. Normal forms of sorts are sorted lists of |
|
12 |
minimal classes (wrt. current class inclusion). |
|
2956 | 13 |
*) |
14 |
||
15 |
signature SORTS = |
|
16 |
sig |
|
28623 | 17 |
val make: sort list -> sort OrdList.T |
28374 | 18 |
val subset: sort OrdList.T * sort OrdList.T -> bool |
28354 | 19 |
val union: sort OrdList.T -> sort OrdList.T -> sort OrdList.T |
20 |
val subtract: sort OrdList.T -> sort OrdList.T -> sort OrdList.T |
|
21 |
val remove_sort: sort -> sort OrdList.T -> sort OrdList.T |
|
22 |
val insert_sort: sort -> sort OrdList.T -> sort OrdList.T |
|
23 |
val insert_typ: typ -> sort OrdList.T -> sort OrdList.T |
|
24 |
val insert_typs: typ list -> sort OrdList.T -> sort OrdList.T |
|
25 |
val insert_term: term -> sort OrdList.T -> sort OrdList.T |
|
26 |
val insert_terms: term list -> sort OrdList.T -> sort OrdList.T |
|
19645 | 27 |
type algebra |
36328
4d9deabf6474
replaced Sorts.rep_algebra by slightly more abstract selectors classes_of/arities_of;
wenzelm
parents:
36105
diff
changeset
|
28 |
val classes_of: algebra -> serial Graph.T |
4d9deabf6474
replaced Sorts.rep_algebra by slightly more abstract selectors classes_of/arities_of;
wenzelm
parents:
36105
diff
changeset
|
29 |
val arities_of: algebra -> (class * (class * sort list)) list Symtab.table |
21933
819ef284720b
classes: more direct way to achieve topological sorting;
wenzelm
parents:
21926
diff
changeset
|
30 |
val all_classes: algebra -> class list |
19645 | 31 |
val super_classes: algebra -> class -> class list |
32 |
val class_less: algebra -> class * class -> bool |
|
33 |
val class_le: algebra -> class * class -> bool |
|
34 |
val sort_eq: algebra -> sort * sort -> bool |
|
35 |
val sort_le: algebra -> sort * sort -> bool |
|
36 |
val sorts_le: algebra -> sort list * sort list -> bool |
|
37 |
val inter_sort: algebra -> sort * sort -> sort |
|
24732 | 38 |
val minimize_sort: algebra -> sort -> sort |
39 |
val complete_sort: algebra -> sort -> sort |
|
28623 | 40 |
val minimal_sorts: algebra -> sort list -> sort OrdList.T |
19645 | 41 |
val certify_class: algebra -> class -> class (*exception TYPE*) |
42 |
val certify_sort: algebra -> sort -> sort (*exception TYPE*) |
|
43 |
val add_class: Pretty.pp -> class * class list -> algebra -> algebra |
|
44 |
val add_classrel: Pretty.pp -> class * class -> algebra -> algebra |
|
45 |
val add_arities: Pretty.pp -> string * (class * sort list) list -> algebra -> algebra |
|
46 |
val empty_algebra: algebra |
|
47 |
val merge_algebra: Pretty.pp -> algebra * algebra -> algebra |
|
30060 | 48 |
val subalgebra: Pretty.pp -> (class -> bool) -> (class * string -> sort list option) |
22181
39104d1c43ca
added explicit query function for arities to subalgebra projection
haftmann
parents:
21933
diff
changeset
|
49 |
-> algebra -> (sort -> sort) * algebra |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
50 |
type class_error |
26639 | 51 |
val class_error: Pretty.pp -> class_error -> string |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
52 |
exception CLASS_ERROR of class_error |
19645 | 53 |
val mg_domain: algebra -> string -> sort -> sort list (*exception CLASS_ERROR*) |
28665 | 54 |
val meet_sort: algebra -> typ * sort |
55 |
-> sort Vartab.table -> sort Vartab.table (*exception CLASS_ERROR*) |
|
56 |
val meet_sort_typ: algebra -> typ * sort -> typ -> typ (*exception CLASS_ERROR*) |
|
19645 | 57 |
val of_sort: algebra -> typ * sort -> bool |
32791 | 58 |
val of_sort_derivation: algebra -> |
36102
a51d1d154c71
of_sort_derivation: pass-through full type information -- following the version by krauss/schropp;
wenzelm
parents:
35975
diff
changeset
|
59 |
{class_relation: typ -> 'a * class -> class -> 'a, |
a51d1d154c71
of_sort_derivation: pass-through full type information -- following the version by krauss/schropp;
wenzelm
parents:
35975
diff
changeset
|
60 |
type_constructor: string * typ list -> ('a * class) list list -> class -> 'a, |
22570
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents:
22364
diff
changeset
|
61 |
type_variable: typ -> ('a * class) list} -> |
19584 | 62 |
typ * sort -> 'a list (*exception CLASS_ERROR*) |
35961
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
63 |
val classrel_derivation: algebra -> |
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
64 |
('a * class -> class -> 'a) -> 'a * class -> class -> 'a (*exception CLASS_ERROR*) |
31946
99ac0321cd47
witness_sorts: proper type witnesses for hyps, not invented "'hyp" variables;
wenzelm
parents:
30242
diff
changeset
|
65 |
val witness_sorts: algebra -> string list -> (typ * sort) list -> sort list -> (typ * sort) list |
2956 | 66 |
end; |
67 |
||
20573 | 68 |
structure Sorts: SORTS = |
2956 | 69 |
struct |
70 |
||
19514 | 71 |
|
19529 | 72 |
(** ordered lists of sorts **) |
14782
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents:
9281
diff
changeset
|
73 |
|
35408 | 74 |
val make = OrdList.make Term_Ord.sort_ord; |
75 |
val subset = OrdList.subset Term_Ord.sort_ord; |
|
76 |
val union = OrdList.union Term_Ord.sort_ord; |
|
77 |
val subtract = OrdList.subtract Term_Ord.sort_ord; |
|
14782
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents:
9281
diff
changeset
|
78 |
|
35408 | 79 |
val remove_sort = OrdList.remove Term_Ord.sort_ord; |
80 |
val insert_sort = OrdList.insert Term_Ord.sort_ord; |
|
14782
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents:
9281
diff
changeset
|
81 |
|
16598
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
82 |
fun insert_typ (TFree (_, S)) Ss = insert_sort S Ss |
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
83 |
| insert_typ (TVar (_, S)) Ss = insert_sort S Ss |
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
84 |
| insert_typ (Type (_, Ts)) Ss = insert_typs Ts Ss |
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
85 |
and insert_typs [] Ss = Ss |
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
86 |
| insert_typs (T :: Ts) Ss = insert_typs Ts (insert_typ T Ss); |
14782
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents:
9281
diff
changeset
|
87 |
|
16598
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
88 |
fun insert_term (Const (_, T)) Ss = insert_typ T Ss |
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
89 |
| insert_term (Free (_, T)) Ss = insert_typ T Ss |
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
90 |
| insert_term (Var (_, T)) Ss = insert_typ T Ss |
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
91 |
| insert_term (Bound _) Ss = Ss |
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
92 |
| insert_term (Abs (_, T, t)) Ss = insert_term t (insert_typ T Ss) |
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
93 |
| insert_term (t $ u) Ss = insert_term t (insert_term u Ss); |
14782
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents:
9281
diff
changeset
|
94 |
|
16598
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
95 |
fun insert_terms [] Ss = Ss |
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents:
16400
diff
changeset
|
96 |
| insert_terms (t :: ts) Ss = insert_terms ts (insert_term t Ss); |
14782
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents:
9281
diff
changeset
|
97 |
|
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents:
9281
diff
changeset
|
98 |
|
19529 | 99 |
|
100 |
(** order-sorted algebra **) |
|
2956 | 101 |
|
102 |
(* |
|
14782
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents:
9281
diff
changeset
|
103 |
classes: graph representing class declarations together with proper |
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents:
9281
diff
changeset
|
104 |
subclass relation, which needs to be transitive and acyclic. |
2956 | 105 |
|
14782
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents:
9281
diff
changeset
|
106 |
arities: table of association lists of all type arities; (t, ars) |
19531 | 107 |
means that type constructor t has the arities ars; an element |
108 |
(c, (c0, Ss)) of ars represents the arity t::(Ss)c being derived |
|
109 |
via c0 <= c. "Coregularity" of the arities structure requires |
|
110 |
that for any two declarations t::(Ss1)c1 and t::(Ss2)c2 such that |
|
111 |
c1 <= c2 holds Ss1 <= Ss2. |
|
2956 | 112 |
*) |
113 |
||
19645 | 114 |
datatype algebra = Algebra of |
20573 | 115 |
{classes: serial Graph.T, |
19645 | 116 |
arities: (class * (class * sort list)) list Symtab.table}; |
117 |
||
36328
4d9deabf6474
replaced Sorts.rep_algebra by slightly more abstract selectors classes_of/arities_of;
wenzelm
parents:
36105
diff
changeset
|
118 |
fun classes_of (Algebra {classes, ...}) = classes; |
4d9deabf6474
replaced Sorts.rep_algebra by slightly more abstract selectors classes_of/arities_of;
wenzelm
parents:
36105
diff
changeset
|
119 |
fun arities_of (Algebra {arities, ...}) = arities; |
19645 | 120 |
|
121 |
fun make_algebra (classes, arities) = |
|
122 |
Algebra {classes = classes, arities = arities}; |
|
123 |
||
124 |
fun map_classes f (Algebra {classes, arities}) = make_algebra (f classes, arities); |
|
125 |
fun map_arities f (Algebra {classes, arities}) = make_algebra (classes, f arities); |
|
126 |
||
127 |
||
128 |
(* classes *) |
|
129 |
||
21933
819ef284720b
classes: more direct way to achieve topological sorting;
wenzelm
parents:
21926
diff
changeset
|
130 |
fun all_classes (Algebra {classes, ...}) = Graph.all_preds classes (Graph.maximals classes); |
819ef284720b
classes: more direct way to achieve topological sorting;
wenzelm
parents:
21926
diff
changeset
|
131 |
|
19645 | 132 |
val super_classes = Graph.imm_succs o classes_of; |
2956 | 133 |
|
134 |
||
19529 | 135 |
(* class relations *) |
2956 | 136 |
|
19645 | 137 |
val class_less = Graph.is_edge o classes_of; |
138 |
fun class_le algebra (c1, c2) = c1 = c2 orelse class_less algebra (c1, c2); |
|
2956 | 139 |
|
140 |
||
19529 | 141 |
(* sort relations *) |
2956 | 142 |
|
19645 | 143 |
fun sort_le algebra (S1, S2) = |
23585 | 144 |
S1 = S2 orelse forall (fn c2 => exists (fn c1 => class_le algebra (c1, c2)) S1) S2; |
2956 | 145 |
|
19645 | 146 |
fun sorts_le algebra (Ss1, Ss2) = |
147 |
ListPair.all (sort_le algebra) (Ss1, Ss2); |
|
2956 | 148 |
|
19645 | 149 |
fun sort_eq algebra (S1, S2) = |
150 |
sort_le algebra (S1, S2) andalso sort_le algebra (S2, S1); |
|
2956 | 151 |
|
152 |
||
19529 | 153 |
(* intersection *) |
2956 | 154 |
|
19645 | 155 |
fun inter_class algebra c S = |
2956 | 156 |
let |
157 |
fun intr [] = [c] |
|
158 |
| intr (S' as c' :: c's) = |
|
19645 | 159 |
if class_le algebra (c', c) then S' |
160 |
else if class_le algebra (c, c') then intr c's |
|
2956 | 161 |
else c' :: intr c's |
162 |
in intr S end; |
|
163 |
||
19645 | 164 |
fun inter_sort algebra (S1, S2) = |
165 |
sort_strings (fold (inter_class algebra) S1 S2); |
|
2956 | 166 |
|
167 |
||
24732 | 168 |
(* normal forms *) |
2956 | 169 |
|
24732 | 170 |
fun minimize_sort _ [] = [] |
171 |
| minimize_sort _ (S as [_]) = S |
|
172 |
| minimize_sort algebra S = |
|
19645 | 173 |
filter (fn c => not (exists (fn c' => class_less algebra (c', c)) S)) S |
19529 | 174 |
|> sort_distinct string_ord; |
2990 | 175 |
|
24732 | 176 |
fun complete_sort algebra = |
177 |
Graph.all_succs (classes_of algebra) o minimize_sort algebra; |
|
178 |
||
28623 | 179 |
fun minimal_sorts algebra raw_sorts = |
180 |
let |
|
181 |
fun le S1 S2 = sort_le algebra (S1, S2); |
|
182 |
val sorts = make (map (minimize_sort algebra) raw_sorts); |
|
183 |
in sorts |> filter_out (fn S => exists (fn S' => le S' S andalso not (le S S')) sorts) end; |
|
184 |
||
2990 | 185 |
|
19645 | 186 |
(* certify *) |
187 |
||
188 |
fun certify_class algebra c = |
|
189 |
if can (Graph.get_node (classes_of algebra)) c then c |
|
190 |
else raise TYPE ("Undeclared class: " ^ quote c, [], []); |
|
191 |
||
24732 | 192 |
fun certify_sort classes = minimize_sort classes o map (certify_class classes); |
19645 | 193 |
|
194 |
||
2956 | 195 |
|
19529 | 196 |
(** build algebras **) |
19514 | 197 |
|
198 |
(* classes *) |
|
199 |
||
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23585
diff
changeset
|
200 |
fun err_dup_class c = error ("Duplicate declaration of class: " ^ quote c); |
19514 | 201 |
|
202 |
fun err_cyclic_classes pp css = |
|
203 |
error (cat_lines (map (fn cs => |
|
204 |
"Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css)); |
|
205 |
||
19645 | 206 |
fun add_class pp (c, cs) = map_classes (fn classes => |
19514 | 207 |
let |
20573 | 208 |
val classes' = classes |> Graph.new_node (c, serial ()) |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23585
diff
changeset
|
209 |
handle Graph.DUP dup => err_dup_class dup; |
19514 | 210 |
val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs) |
211 |
handle Graph.CYCLES css => err_cyclic_classes pp css; |
|
19645 | 212 |
in classes'' end); |
19514 | 213 |
|
214 |
||
215 |
(* arities *) |
|
216 |
||
217 |
local |
|
218 |
||
219 |
fun for_classes _ NONE = "" |
|
220 |
| for_classes pp (SOME (c1, c2)) = |
|
221 |
" for classes " ^ Pretty.string_of_classrel pp [c1, c2]; |
|
222 |
||
223 |
fun err_conflict pp t cc (c, Ss) (c', Ss') = |
|
224 |
error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n " ^ |
|
225 |
Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n " ^ |
|
226 |
Pretty.string_of_arity pp (t, Ss', [c'])); |
|
227 |
||
19645 | 228 |
fun coregular pp algebra t (c, (c0, Ss)) ars = |
19514 | 229 |
let |
19524 | 230 |
fun conflict (c', (_, Ss')) = |
19645 | 231 |
if class_le algebra (c, c') andalso not (sorts_le algebra (Ss, Ss')) then |
19514 | 232 |
SOME ((c, c'), (c', Ss')) |
19645 | 233 |
else if class_le algebra (c', c) andalso not (sorts_le algebra (Ss', Ss)) then |
19514 | 234 |
SOME ((c', c), (c', Ss')) |
235 |
else NONE; |
|
236 |
in |
|
237 |
(case get_first conflict ars of |
|
238 |
SOME ((c1, c2), (c', Ss')) => err_conflict pp t (SOME (c1, c2)) (c, Ss) (c', Ss') |
|
19524 | 239 |
| NONE => (c, (c0, Ss)) :: ars) |
19514 | 240 |
end; |
241 |
||
19645 | 242 |
fun complete algebra (c0, Ss) = map (rpair (c0, Ss)) (c0 :: super_classes algebra c0); |
243 |
||
244 |
fun insert pp algebra t (c, (c0, Ss)) ars = |
|
19514 | 245 |
(case AList.lookup (op =) ars c of |
19645 | 246 |
NONE => coregular pp algebra t (c, (c0, Ss)) ars |
19524 | 247 |
| SOME (_, Ss') => |
19645 | 248 |
if sorts_le algebra (Ss, Ss') then ars |
249 |
else if sorts_le algebra (Ss', Ss) then |
|
250 |
coregular pp algebra t (c, (c0, Ss)) |
|
19524 | 251 |
(filter_out (fn (c'', (_, Ss'')) => c = c'' andalso Ss'' = Ss') ars) |
19514 | 252 |
else err_conflict pp t NONE (c, Ss) (c, Ss')); |
253 |
||
35975
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
254 |
in |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
255 |
|
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
256 |
fun insert_ars pp algebra t = fold_rev (insert pp algebra t); |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
257 |
|
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
258 |
fun insert_complete_ars pp algebra (t, ars) arities = |
19645 | 259 |
let val ars' = |
260 |
Symtab.lookup_list arities t |
|
35975
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
261 |
|> fold_rev (insert_ars pp algebra t) (map (complete algebra) ars); |
19645 | 262 |
in Symtab.update (t, ars') arities end; |
19514 | 263 |
|
35975
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
264 |
fun add_arities pp arg algebra = |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
265 |
algebra |> map_arities (insert_complete_ars pp algebra arg); |
19514 | 266 |
|
19645 | 267 |
fun add_arities_table pp algebra = |
35975
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
268 |
Symtab.fold (fn (t, ars) => insert_complete_ars pp algebra (t, map snd ars)); |
19514 | 269 |
|
270 |
end; |
|
271 |
||
19529 | 272 |
|
19645 | 273 |
(* classrel *) |
274 |
||
275 |
fun rebuild_arities pp algebra = algebra |> map_arities (fn arities => |
|
276 |
Symtab.empty |
|
277 |
|> add_arities_table pp algebra arities); |
|
278 |
||
279 |
fun add_classrel pp rel = rebuild_arities pp o map_classes (fn classes => |
|
280 |
classes |> Graph.add_edge_trans_acyclic rel |
|
281 |
handle Graph.CYCLES css => err_cyclic_classes pp css); |
|
282 |
||
283 |
||
284 |
(* empty and merge *) |
|
285 |
||
286 |
val empty_algebra = make_algebra (Graph.empty, Symtab.empty); |
|
287 |
||
288 |
fun merge_algebra pp |
|
289 |
(Algebra {classes = classes1, arities = arities1}, |
|
290 |
Algebra {classes = classes2, arities = arities2}) = |
|
291 |
let |
|
292 |
val classes' = Graph.merge_trans_acyclic (op =) (classes1, classes2) |
|
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23585
diff
changeset
|
293 |
handle Graph.DUP c => err_dup_class c |
35975
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
294 |
| Graph.CYCLES css => err_cyclic_classes pp css; |
19645 | 295 |
val algebra0 = make_algebra (classes', Symtab.empty); |
35975
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
296 |
val arities' = |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
297 |
(case (pointer_eq (classes1, classes2), pointer_eq (arities1, arities2)) of |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
298 |
(true, true) => arities1 |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
299 |
| (true, false) => (*no completion*) |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
300 |
(arities1, arities2) |> Symtab.join (fn t => fn (ars1, ars2) => |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
301 |
if pointer_eq (ars1, ars2) then raise Symtab.SAME |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
302 |
else insert_ars pp algebra0 t ars2 ars1) |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
303 |
| (false, true) => (*unary completion*) |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
304 |
Symtab.empty |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
305 |
|> add_arities_table pp algebra0 arities1 |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
306 |
| (false, false) => (*binary completion*) |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
307 |
Symtab.empty |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
308 |
|> add_arities_table pp algebra0 arities1 |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
309 |
|> add_arities_table pp algebra0 arities2); |
19645 | 310 |
in make_algebra (classes', arities') end; |
311 |
||
21933
819ef284720b
classes: more direct way to achieve topological sorting;
wenzelm
parents:
21926
diff
changeset
|
312 |
|
28922 | 313 |
(* algebra projections *) |
314 |
||
22181
39104d1c43ca
added explicit query function for arities to subalgebra projection
haftmann
parents:
21933
diff
changeset
|
315 |
fun subalgebra pp P sargs (algebra as Algebra {classes, arities}) = |
19952 | 316 |
let |
24732 | 317 |
val restrict_sort = minimize_sort algebra o filter P o Graph.all_succs classes; |
22181
39104d1c43ca
added explicit query function for arities to subalgebra projection
haftmann
parents:
21933
diff
changeset
|
318 |
fun restrict_arity tyco (c, (_, Ss)) = |
30060 | 319 |
if P c then case sargs (c, tyco) |
320 |
of SOME sorts => SOME (c, (c, Ss |> map2 (curry (inter_sort algebra)) sorts |
|
22181
39104d1c43ca
added explicit query function for arities to subalgebra projection
haftmann
parents:
21933
diff
changeset
|
321 |
|> map restrict_sort)) |
30062 | 322 |
| NONE => NONE |
22181
39104d1c43ca
added explicit query function for arities to subalgebra projection
haftmann
parents:
21933
diff
changeset
|
323 |
else NONE; |
21933
819ef284720b
classes: more direct way to achieve topological sorting;
wenzelm
parents:
21926
diff
changeset
|
324 |
val classes' = classes |> Graph.subgraph P; |
22181
39104d1c43ca
added explicit query function for arities to subalgebra projection
haftmann
parents:
21933
diff
changeset
|
325 |
val arities' = arities |> Symtab.map' (map_filter o restrict_arity); |
21933
819ef284720b
classes: more direct way to achieve topological sorting;
wenzelm
parents:
21926
diff
changeset
|
326 |
in (restrict_sort, rebuild_arities pp (make_algebra (classes', arities'))) end; |
20465 | 327 |
|
19645 | 328 |
|
19529 | 329 |
|
330 |
(** sorts of types **) |
|
331 |
||
35961
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
332 |
(* errors -- performance tuning via delayed message composition *) |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
333 |
|
26639 | 334 |
datatype class_error = |
36105 | 335 |
No_Classrel of class * class | |
336 |
No_Arity of string * class | |
|
337 |
No_Subsort of sort * sort; |
|
19529 | 338 |
|
36105 | 339 |
fun class_error pp (No_Classrel (c1, c2)) = |
22196 | 340 |
"No class relation " ^ Pretty.string_of_classrel pp [c1, c2] |
36105 | 341 |
| class_error pp (No_Arity (a, c)) = |
26326 | 342 |
"No type arity " ^ Pretty.string_of_arity pp (a, [], [c]) |
36105 | 343 |
| class_error pp (No_Subsort (S1, S2)) = |
26994 | 344 |
"Cannot derive subsort relation " ^ Pretty.string_of_sort pp S1 |
345 |
^ " < " ^ Pretty.string_of_sort pp S2; |
|
19529 | 346 |
|
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
347 |
exception CLASS_ERROR of class_error; |
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
348 |
|
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
349 |
|
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
350 |
(* mg_domain *) |
19529 | 351 |
|
19645 | 352 |
fun mg_domain algebra a S = |
19529 | 353 |
let |
19645 | 354 |
val arities = arities_of algebra; |
19529 | 355 |
fun dom c = |
356 |
(case AList.lookup (op =) (Symtab.lookup_list arities a) c of |
|
36105 | 357 |
NONE => raise CLASS_ERROR (No_Arity (a, c)) |
19529 | 358 |
| SOME (_, Ss) => Ss); |
19645 | 359 |
fun dom_inter c Ss = ListPair.map (inter_sort algebra) (dom c, Ss); |
19529 | 360 |
in |
361 |
(case S of |
|
362 |
[] => raise Fail "Unknown domain of empty intersection" |
|
363 |
| c :: cs => fold dom_inter cs (dom c)) |
|
364 |
end; |
|
365 |
||
366 |
||
26639 | 367 |
(* meet_sort *) |
368 |
||
369 |
fun meet_sort algebra = |
|
370 |
let |
|
371 |
fun inters S S' = inter_sort algebra (S, S'); |
|
372 |
fun meet _ [] = I |
|
373 |
| meet (TFree (_, S)) S' = |
|
374 |
if sort_le algebra (S, S') then I |
|
36105 | 375 |
else raise CLASS_ERROR (No_Subsort (S, S')) |
26639 | 376 |
| meet (TVar (v, S)) S' = |
377 |
if sort_le algebra (S, S') then I |
|
378 |
else Vartab.map_default (v, S) (inters S') |
|
379 |
| meet (Type (a, Ts)) S = fold2 meet Ts (mg_domain algebra a S); |
|
380 |
in uncurry meet end; |
|
381 |
||
28665 | 382 |
fun meet_sort_typ algebra (T, S) = |
383 |
let |
|
384 |
val tab = meet_sort algebra (T, S) Vartab.empty; |
|
385 |
in Term.map_type_tvar (fn (v, _) => |
|
386 |
TVar (v, (the o Vartab.lookup tab) v)) |
|
387 |
end; |
|
388 |
||
26639 | 389 |
|
19529 | 390 |
(* of_sort *) |
391 |
||
19645 | 392 |
fun of_sort algebra = |
19529 | 393 |
let |
394 |
fun ofS (_, []) = true |
|
19645 | 395 |
| ofS (TFree (_, S), S') = sort_le algebra (S, S') |
396 |
| ofS (TVar (_, S), S') = sort_le algebra (S, S') |
|
19529 | 397 |
| ofS (Type (a, Ts), S) = |
19645 | 398 |
let val Ss = mg_domain algebra a S in |
19529 | 399 |
ListPair.all ofS (Ts, Ss) |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
400 |
end handle CLASS_ERROR _ => false; |
19529 | 401 |
in ofS end; |
402 |
||
403 |
||
27498 | 404 |
(* animating derivations *) |
405 |
||
32791 | 406 |
fun of_sort_derivation algebra {class_relation, type_constructor, type_variable} = |
19529 | 407 |
let |
27555 | 408 |
val arities = arities_of algebra; |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
409 |
|
36104
fecb587a1d0e
of_sort_derivation: weaken bypasses derivation for identical sort -- accomodate proof terms by krauss/schropp, for example;
wenzelm
parents:
36103
diff
changeset
|
410 |
fun weaken T D1 S2 = |
fecb587a1d0e
of_sort_derivation: weaken bypasses derivation for identical sort -- accomodate proof terms by krauss/schropp, for example;
wenzelm
parents:
36103
diff
changeset
|
411 |
let val S1 = map snd D1 in |
fecb587a1d0e
of_sort_derivation: weaken bypasses derivation for identical sort -- accomodate proof terms by krauss/schropp, for example;
wenzelm
parents:
36103
diff
changeset
|
412 |
if S1 = S2 then map fst D1 |
fecb587a1d0e
of_sort_derivation: weaken bypasses derivation for identical sort -- accomodate proof terms by krauss/schropp, for example;
wenzelm
parents:
36103
diff
changeset
|
413 |
else |
fecb587a1d0e
of_sort_derivation: weaken bypasses derivation for identical sort -- accomodate proof terms by krauss/schropp, for example;
wenzelm
parents:
36103
diff
changeset
|
414 |
S2 |> map (fn c2 => |
fecb587a1d0e
of_sort_derivation: weaken bypasses derivation for identical sort -- accomodate proof terms by krauss/schropp, for example;
wenzelm
parents:
36103
diff
changeset
|
415 |
(case D1 |> find_first (fn (_, c1) => class_le algebra (c1, c2)) of |
fecb587a1d0e
of_sort_derivation: weaken bypasses derivation for identical sort -- accomodate proof terms by krauss/schropp, for example;
wenzelm
parents:
36103
diff
changeset
|
416 |
SOME d1 => class_relation T d1 c2 |
36105 | 417 |
| NONE => raise CLASS_ERROR (No_Subsort (S1, S2)))) |
36104
fecb587a1d0e
of_sort_derivation: weaken bypasses derivation for identical sort -- accomodate proof terms by krauss/schropp, for example;
wenzelm
parents:
36103
diff
changeset
|
418 |
end; |
19529 | 419 |
|
36103 | 420 |
fun derive (_, []) = [] |
421 |
| derive (T as Type (a, Us), S) = |
|
19529 | 422 |
let |
19645 | 423 |
val Ss = mg_domain algebra a S; |
36103 | 424 |
val dom = map2 (fn U => fn S => derive (U, S) ~~ S) Us Ss; |
19529 | 425 |
in |
426 |
S |> map (fn c => |
|
427 |
let |
|
428 |
val (c0, Ss') = the (AList.lookup (op =) (Symtab.lookup_list arities a) c); |
|
36102
a51d1d154c71
of_sort_derivation: pass-through full type information -- following the version by krauss/schropp;
wenzelm
parents:
35975
diff
changeset
|
429 |
val dom' = map (fn ((U, d), S') => weaken U d S' ~~ S') ((Us ~~ dom) ~~ Ss'); |
a51d1d154c71
of_sort_derivation: pass-through full type information -- following the version by krauss/schropp;
wenzelm
parents:
35975
diff
changeset
|
430 |
in class_relation T (type_constructor (a, Us) dom' c0, c0) c end) |
19529 | 431 |
end |
36103 | 432 |
| derive (T, S) = weaken T (type_variable T) S; |
433 |
in derive end; |
|
19529 | 434 |
|
35961
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
435 |
fun classrel_derivation algebra class_relation = |
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
436 |
let |
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
437 |
fun path (x, c1 :: c2 :: cs) = path (class_relation (x, c1) c2, c2 :: cs) |
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
438 |
| path (x, _) = x; |
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
439 |
in |
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
440 |
fn (x, c1) => fn c2 => |
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
441 |
(case Graph.irreducible_paths (classes_of algebra) (c1, c2) of |
36105 | 442 |
[] => raise CLASS_ERROR (No_Classrel (c1, c2)) |
35961
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
443 |
| cs :: _ => path (x, cs)) |
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
444 |
end; |
00e48e1d9afd
Sorts.of_sort_derivation: do not use slow Graph.irreducible_paths here, which not always needed (SUBTLE CHANGE IN SEMANTICS);
wenzelm
parents:
35408
diff
changeset
|
445 |
|
19529 | 446 |
|
447 |
(* witness_sorts *) |
|
448 |
||
19645 | 449 |
fun witness_sorts algebra types hyps sorts = |
19529 | 450 |
let |
19645 | 451 |
fun le S1 S2 = sort_le algebra (S1, S2); |
31946
99ac0321cd47
witness_sorts: proper type witnesses for hyps, not invented "'hyp" variables;
wenzelm
parents:
30242
diff
changeset
|
452 |
fun get S2 (T, S1) = if le S1 S2 then SOME (T, S2) else NONE; |
19645 | 453 |
fun mg_dom t S = SOME (mg_domain algebra t S) handle CLASS_ERROR _ => NONE; |
19529 | 454 |
|
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
455 |
fun witn_sort _ [] solved_failed = (SOME (propT, []), solved_failed) |
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
456 |
| witn_sort path S (solved, failed) = |
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
457 |
if exists (le S) failed then (NONE, (solved, failed)) |
19529 | 458 |
else |
31946
99ac0321cd47
witness_sorts: proper type witnesses for hyps, not invented "'hyp" variables;
wenzelm
parents:
30242
diff
changeset
|
459 |
(case get_first (get S) solved of |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
460 |
SOME w => (SOME w, (solved, failed)) |
19529 | 461 |
| NONE => |
31946
99ac0321cd47
witness_sorts: proper type witnesses for hyps, not invented "'hyp" variables;
wenzelm
parents:
30242
diff
changeset
|
462 |
(case get_first (get S) hyps of |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
463 |
SOME w => (SOME w, (w :: solved, failed)) |
19584 | 464 |
| NONE => witn_types path types S (solved, failed))) |
19529 | 465 |
|
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
466 |
and witn_sorts path x = fold_map (witn_sort path) x |
19529 | 467 |
|
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
468 |
and witn_types _ [] S (solved, failed) = (NONE, (solved, S :: failed)) |
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
469 |
| witn_types path (t :: ts) S solved_failed = |
19529 | 470 |
(case mg_dom t S of |
471 |
SOME SS => |
|
472 |
(*do not descend into stronger args (achieving termination)*) |
|
473 |
if exists (fn D => le D S orelse exists (le D) path) SS then |
|
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
474 |
witn_types path ts S solved_failed |
19529 | 475 |
else |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
476 |
let val (ws, (solved', failed')) = witn_sorts (S :: path) SS solved_failed in |
19529 | 477 |
if forall is_some ws then |
478 |
let val w = (Type (t, map (#1 o the) ws), S) |
|
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
479 |
in (SOME w, (w :: solved', failed')) end |
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
480 |
else witn_types path ts S (solved', failed') |
19529 | 481 |
end |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
482 |
| NONE => witn_types path ts S solved_failed); |
19529 | 483 |
|
19584 | 484 |
in map_filter I (#1 (witn_sorts [] sorts ([], []))) end; |
19529 | 485 |
|
19514 | 486 |
end; |