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