author | wenzelm |
Sat, 15 Apr 2023 14:14:30 +0200 | |
changeset 77855 | ff801186ff66 |
parent 77730 | 4a174bea55e2 |
child 77869 | 1156aa9db7f5 |
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 |
|
19645 | 17 |
type algebra |
36328
4d9deabf6474
replaced Sorts.rep_algebra by slightly more abstract selectors classes_of/arities_of;
wenzelm
parents:
36105
diff
changeset
|
18 |
val classes_of: algebra -> serial Graph.T |
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
19 |
val arities_of: algebra -> (class * sort list) list Symtab.table |
21933
819ef284720b
classes: more direct way to achieve topological sorting;
wenzelm
parents:
21926
diff
changeset
|
20 |
val all_classes: algebra -> class list |
19645 | 21 |
val super_classes: algebra -> class -> class list |
22 |
val class_less: algebra -> class * class -> bool |
|
23 |
val class_le: algebra -> class * class -> bool |
|
24 |
val sort_eq: algebra -> sort * sort -> bool |
|
25 |
val sort_le: algebra -> sort * sort -> bool |
|
26 |
val sorts_le: algebra -> sort list * sort list -> bool |
|
27 |
val inter_sort: algebra -> sort * sort -> sort |
|
24732 | 28 |
val minimize_sort: algebra -> sort -> sort |
29 |
val complete_sort: algebra -> sort -> sort |
|
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
48272
diff
changeset
|
30 |
val add_class: Context.generic -> class * class list -> algebra -> algebra |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
48272
diff
changeset
|
31 |
val add_classrel: Context.generic -> class * class -> algebra -> algebra |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
48272
diff
changeset
|
32 |
val add_arities: Context.generic -> string * (class * sort list) list -> algebra -> algebra |
19645 | 33 |
val empty_algebra: algebra |
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
48272
diff
changeset
|
34 |
val merge_algebra: Context.generic -> algebra * algebra -> algebra |
68295 | 35 |
val dest_algebra: algebra list -> algebra -> |
36 |
{classrel: (class * class list) list, |
|
37 |
arities: (string * sort list * class) list} |
|
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
48272
diff
changeset
|
38 |
val subalgebra: Context.generic -> (class -> bool) -> (class * string -> sort list option) |
22181
39104d1c43ca
added explicit query function for arities to subalgebra projection
haftmann
parents:
21933
diff
changeset
|
39 |
-> algebra -> (sort -> sort) * algebra |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
40 |
type class_error |
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
48272
diff
changeset
|
41 |
val class_error: Context.generic -> class_error -> string |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
42 |
exception CLASS_ERROR of class_error |
48272 | 43 |
val has_instance: algebra -> string -> sort -> bool |
19645 | 44 |
val mg_domain: algebra -> string -> sort -> sort list (*exception CLASS_ERROR*) |
28665 | 45 |
val meet_sort: algebra -> typ * sort |
46 |
-> sort Vartab.table -> sort Vartab.table (*exception CLASS_ERROR*) |
|
47 |
val meet_sort_typ: algebra -> typ * sort -> typ -> typ (*exception CLASS_ERROR*) |
|
19645 | 48 |
val of_sort: algebra -> typ * sort -> bool |
32791 | 49 |
val of_sort_derivation: algebra -> |
62538
85ebb645b1a3
provide explicit hint concering uniqueness of derivation
haftmann
parents:
61264
diff
changeset
|
50 |
{class_relation: typ -> bool -> 'a * class -> class -> 'a, |
36102
a51d1d154c71
of_sort_derivation: pass-through full type information -- following the version by krauss/schropp;
wenzelm
parents:
35975
diff
changeset
|
51 |
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
|
52 |
type_variable: typ -> ('a * class) list} -> |
19584 | 53 |
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
|
54 |
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
|
55 |
('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
|
56 |
val witness_sorts: algebra -> string list -> (typ * sort) list -> sort list -> (typ * sort) list |
2956 | 57 |
end; |
58 |
||
20573 | 59 |
structure Sorts: SORTS = |
2956 | 60 |
struct |
61 |
||
19529 | 62 |
(** order-sorted algebra **) |
2956 | 63 |
|
64 |
(* |
|
14782
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents:
9281
diff
changeset
|
65 |
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
|
66 |
subclass relation, which needs to be transitive and acyclic. |
2956 | 67 |
|
14782
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents:
9281
diff
changeset
|
68 |
arities: table of association lists of all type arities; (t, ars) |
19531 | 69 |
means that type constructor t has the arities ars; an element |
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
70 |
(c, Ss) of ars represents the arity t::(Ss)c. "Coregularity" of |
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
71 |
the arities structure requires that for any two declarations |
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
72 |
t::(Ss1)c1 and t::(Ss2)c2 such that c1 <= c2 holds Ss1 <= Ss2. |
2956 | 73 |
*) |
74 |
||
19645 | 75 |
datatype algebra = Algebra of |
20573 | 76 |
{classes: serial Graph.T, |
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
77 |
arities: (class * sort list) list Symtab.table}; |
19645 | 78 |
|
36328
4d9deabf6474
replaced Sorts.rep_algebra by slightly more abstract selectors classes_of/arities_of;
wenzelm
parents:
36105
diff
changeset
|
79 |
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
|
80 |
fun arities_of (Algebra {arities, ...}) = arities; |
19645 | 81 |
|
82 |
fun make_algebra (classes, arities) = |
|
83 |
Algebra {classes = classes, arities = arities}; |
|
84 |
||
85 |
fun map_classes f (Algebra {classes, arities}) = make_algebra (f classes, arities); |
|
86 |
fun map_arities f (Algebra {classes, arities}) = make_algebra (classes, f arities); |
|
87 |
||
88 |
||
89 |
(* classes *) |
|
90 |
||
21933
819ef284720b
classes: more direct way to achieve topological sorting;
wenzelm
parents:
21926
diff
changeset
|
91 |
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
|
92 |
|
44338
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
wenzelm
parents:
43792
diff
changeset
|
93 |
val super_classes = Graph.immediate_succs o classes_of; |
2956 | 94 |
|
95 |
||
19529 | 96 |
(* class relations *) |
2956 | 97 |
|
71526 | 98 |
val class_less : algebra -> class * class -> bool = Graph.is_edge o classes_of; |
19645 | 99 |
fun class_le algebra (c1, c2) = c1 = c2 orelse class_less algebra (c1, c2); |
2956 | 100 |
|
101 |
||
19529 | 102 |
(* sort relations *) |
2956 | 103 |
|
71526 | 104 |
fun sort_le algebra (S1: sort, S2: sort) = |
23585 | 105 |
S1 = S2 orelse forall (fn c2 => exists (fn c1 => class_le algebra (c1, c2)) S1) S2; |
2956 | 106 |
|
19645 | 107 |
fun sorts_le algebra (Ss1, Ss2) = |
108 |
ListPair.all (sort_le algebra) (Ss1, Ss2); |
|
2956 | 109 |
|
19645 | 110 |
fun sort_eq algebra (S1, S2) = |
111 |
sort_le algebra (S1, S2) andalso sort_le algebra (S2, S1); |
|
2956 | 112 |
|
113 |
||
19529 | 114 |
(* intersection *) |
2956 | 115 |
|
19645 | 116 |
fun inter_class algebra c S = |
2956 | 117 |
let |
118 |
fun intr [] = [c] |
|
119 |
| intr (S' as c' :: c's) = |
|
19645 | 120 |
if class_le algebra (c', c) then S' |
121 |
else if class_le algebra (c, c') then intr c's |
|
2956 | 122 |
else c' :: intr c's |
123 |
in intr S end; |
|
124 |
||
19645 | 125 |
fun inter_sort algebra (S1, S2) = |
126 |
sort_strings (fold (inter_class algebra) S1 S2); |
|
2956 | 127 |
|
128 |
||
24732 | 129 |
(* normal forms *) |
2956 | 130 |
|
24732 | 131 |
fun minimize_sort _ [] = [] |
132 |
| minimize_sort _ (S as [_]) = S |
|
133 |
| minimize_sort algebra S = |
|
19645 | 134 |
filter (fn c => not (exists (fn c' => class_less algebra (c', c)) S)) S |
19529 | 135 |
|> sort_distinct string_ord; |
2990 | 136 |
|
24732 | 137 |
fun complete_sort algebra = |
138 |
Graph.all_succs (classes_of algebra) o minimize_sort algebra; |
|
139 |
||
2990 | 140 |
|
2956 | 141 |
|
19529 | 142 |
(** build algebras **) |
19514 | 143 |
|
144 |
(* classes *) |
|
145 |
||
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23585
diff
changeset
|
146 |
fun err_dup_class c = error ("Duplicate declaration of class: " ^ quote c); |
19514 | 147 |
|
61264 | 148 |
fun err_cyclic_classes context css = |
19514 | 149 |
error (cat_lines (map (fn cs => |
61264 | 150 |
"Cycle in class relation: " ^ Syntax.string_of_classrel (Syntax.init_pretty context) cs) css)); |
19514 | 151 |
|
61264 | 152 |
fun add_class context (c, cs) = map_classes (fn classes => |
19514 | 153 |
let |
20573 | 154 |
val classes' = classes |> Graph.new_node (c, serial ()) |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23585
diff
changeset
|
155 |
handle Graph.DUP dup => err_dup_class dup; |
19514 | 156 |
val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs) |
61264 | 157 |
handle Graph.CYCLES css => err_cyclic_classes context css; |
19645 | 158 |
in classes'' end); |
19514 | 159 |
|
160 |
||
161 |
(* arities *) |
|
162 |
||
163 |
local |
|
164 |
||
165 |
fun for_classes _ NONE = "" |
|
42387 | 166 |
| for_classes ctxt (SOME (c1, c2)) = " for classes " ^ Syntax.string_of_classrel ctxt [c1, c2]; |
19514 | 167 |
|
61264 | 168 |
fun err_conflict context t cc (c, Ss) (c', Ss') = |
169 |
let val ctxt = Syntax.init_pretty context in |
|
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46614
diff
changeset
|
170 |
error ("Conflict of type arities" ^ for_classes ctxt cc ^ ":\n " ^ |
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46614
diff
changeset
|
171 |
Syntax.string_of_arity ctxt (t, Ss, [c]) ^ " and\n " ^ |
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46614
diff
changeset
|
172 |
Syntax.string_of_arity ctxt (t, Ss', [c'])) |
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46614
diff
changeset
|
173 |
end; |
19514 | 174 |
|
61264 | 175 |
fun coregular context algebra t (c, Ss) ars = |
19514 | 176 |
let |
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
177 |
fun conflict (c', Ss') = |
19645 | 178 |
if class_le algebra (c, c') andalso not (sorts_le algebra (Ss, Ss')) then |
19514 | 179 |
SOME ((c, c'), (c', Ss')) |
19645 | 180 |
else if class_le algebra (c', c) andalso not (sorts_le algebra (Ss', Ss)) then |
19514 | 181 |
SOME ((c', c), (c', Ss')) |
182 |
else NONE; |
|
183 |
in |
|
184 |
(case get_first conflict ars of |
|
61264 | 185 |
SOME ((c1, c2), (c', Ss')) => err_conflict context t (SOME (c1, c2)) (c, Ss) (c', Ss') |
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
186 |
| NONE => (c, Ss) :: ars) |
19514 | 187 |
end; |
188 |
||
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
189 |
fun complete algebra (c, Ss) = map (rpair Ss) (c :: super_classes algebra c); |
19645 | 190 |
|
61264 | 191 |
fun insert context algebra t (c, Ss) ars = |
19514 | 192 |
(case AList.lookup (op =) ars c of |
61264 | 193 |
NONE => coregular context algebra t (c, Ss) ars |
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
194 |
| SOME Ss' => |
19645 | 195 |
if sorts_le algebra (Ss, Ss') then ars |
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
196 |
else if sorts_le algebra (Ss', Ss) |
61264 | 197 |
then coregular context algebra t (c, Ss) (remove (op =) (c, Ss') ars) |
198 |
else err_conflict context t NONE (c, Ss) (c, Ss')); |
|
19514 | 199 |
|
35975
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
200 |
in |
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
201 |
|
61264 | 202 |
fun insert_ars context algebra t = fold_rev (insert context algebra 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
|
203 |
|
61264 | 204 |
fun insert_complete_ars context algebra (t, ars) arities = |
19645 | 205 |
let val ars' = |
206 |
Symtab.lookup_list arities t |
|
61264 | 207 |
|> fold_rev (insert_ars context algebra t) (map (complete algebra) ars); |
19645 | 208 |
in Symtab.update (t, ars') arities end; |
19514 | 209 |
|
61264 | 210 |
fun add_arities context arg algebra = |
211 |
algebra |> map_arities (insert_complete_ars context algebra arg); |
|
19514 | 212 |
|
61264 | 213 |
fun add_arities_table context algebra = |
214 |
Symtab.fold (fn (t, ars) => insert_complete_ars context algebra (t, ars)); |
|
19514 | 215 |
|
216 |
end; |
|
217 |
||
19529 | 218 |
|
19645 | 219 |
(* classrel *) |
220 |
||
74232 | 221 |
fun rebuild_arities context algebra = algebra |
222 |
|> map_arities (Symtab.build o add_arities_table context algebra); |
|
19645 | 223 |
|
61264 | 224 |
fun add_classrel context rel = rebuild_arities context o map_classes (fn classes => |
19645 | 225 |
classes |> Graph.add_edge_trans_acyclic rel |
61264 | 226 |
handle Graph.CYCLES css => err_cyclic_classes context css); |
19645 | 227 |
|
228 |
||
229 |
(* empty and merge *) |
|
230 |
||
231 |
val empty_algebra = make_algebra (Graph.empty, Symtab.empty); |
|
232 |
||
61264 | 233 |
fun merge_algebra context |
19645 | 234 |
(Algebra {classes = classes1, arities = arities1}, |
235 |
Algebra {classes = classes2, arities = arities2}) = |
|
236 |
let |
|
237 |
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
|
238 |
handle Graph.DUP c => err_dup_class c |
61264 | 239 |
| Graph.CYCLES css => err_cyclic_classes context css; |
19645 | 240 |
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
|
241 |
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
|
242 |
(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
|
243 |
(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
|
244 |
| (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
|
245 |
(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
|
246 |
if pointer_eq (ars1, ars2) then raise Symtab.SAME |
61264 | 247 |
else insert_ars context algebra0 t ars2 ars1) |
35975
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
248 |
| (false, true) => (*unary completion*) |
74232 | 249 |
Symtab.build (add_arities_table context algebra0 arities1) |
35975
cef3c78ace0a
more efficient merge_algebra for important special cases -- tricky due to required completion if class algebra changes;
wenzelm
parents:
35961
diff
changeset
|
250 |
| (false, false) => (*binary completion*) |
74232 | 251 |
Symtab.build |
252 |
(add_arities_table context algebra0 arities1 #> |
|
253 |
add_arities_table context algebra0 arities2)); |
|
19645 | 254 |
in make_algebra (classes', arities') end; |
255 |
||
21933
819ef284720b
classes: more direct way to achieve topological sorting;
wenzelm
parents:
21926
diff
changeset
|
256 |
|
68295 | 257 |
(* destruct *) |
258 |
||
259 |
fun dest_algebra parents (Algebra {classes, arities}) = |
|
260 |
let |
|
261 |
fun new_classrel rel = not (exists (fn algebra => class_less algebra rel) parents); |
|
262 |
val classrel = |
|
74234 | 263 |
build (classes |> Graph.fold (fn (c, (_, (_, ds))) => |
68295 | 264 |
(case filter (fn d => new_classrel (c, d)) (Graph.Keys.dest ds) of |
265 |
[] => I |
|
74234 | 266 |
| ds' => cons (c, sort_strings ds')))) |
68295 | 267 |
|> sort_by #1; |
268 |
||
269 |
fun is_arity t ar algebra = member (op =) (Symtab.lookup_list (arities_of algebra) t) ar; |
|
270 |
fun add_arity t (c, Ss) = not (exists (is_arity t (c, Ss)) parents) ? cons (t, Ss, c); |
|
271 |
val arities = |
|
74234 | 272 |
build (arities |> Symtab.fold (fn (t, ars) => fold_rev (add_arity t) (sort_by #1 ars))) |
68295 | 273 |
|> sort_by #1; |
274 |
in {classrel = classrel, arities = arities} end; |
|
275 |
||
276 |
||
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
277 |
(* algebra projections *) (* FIXME potentially violates abstract type integrity *) |
28922 | 278 |
|
61264 | 279 |
fun subalgebra context P sargs (algebra as Algebra {classes, arities}) = |
19952 | 280 |
let |
24732 | 281 |
val restrict_sort = minimize_sort algebra o filter P o Graph.all_succs classes; |
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
282 |
fun restrict_arity t (c, Ss) = |
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
283 |
if P c then |
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
284 |
(case sargs (c, t) of |
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
285 |
SOME sorts => |
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
286 |
SOME (c, Ss |> map2 (curry (inter_sort algebra)) sorts |> map restrict_sort) |
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
287 |
| NONE => NONE) |
22181
39104d1c43ca
added explicit query function for arities to subalgebra projection
haftmann
parents:
21933
diff
changeset
|
288 |
else NONE; |
46614
165886a4fe64
clarified Graph.restrict (formerly Graph.subgraph) based on public graph operations;
wenzelm
parents:
45595
diff
changeset
|
289 |
val classes' = classes |> Graph.restrict P; |
39020 | 290 |
val arities' = arities |> Symtab.map (map_filter o restrict_arity); |
61264 | 291 |
in (restrict_sort, rebuild_arities context (make_algebra (classes', arities'))) end; |
20465 | 292 |
|
19645 | 293 |
|
19529 | 294 |
|
295 |
(** sorts of types **) |
|
296 |
||
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
|
297 |
(* errors -- performance tuning via delayed message composition *) |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
298 |
|
26639 | 299 |
datatype class_error = |
36105 | 300 |
No_Classrel of class * class | |
301 |
No_Arity of string * class | |
|
302 |
No_Subsort of sort * sort; |
|
19529 | 303 |
|
61264 | 304 |
fun class_error context = |
305 |
let val ctxt = Syntax.init_pretty context in |
|
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46614
diff
changeset
|
306 |
fn No_Classrel (c1, c2) => "No class relation " ^ Syntax.string_of_classrel ctxt [c1, c2] |
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46614
diff
changeset
|
307 |
| No_Arity (a, c) => "No type arity " ^ Syntax.string_of_arity ctxt (a, [], [c]) |
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46614
diff
changeset
|
308 |
| No_Subsort (S1, S2) => |
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46614
diff
changeset
|
309 |
"Cannot derive subsort relation " ^ |
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46614
diff
changeset
|
310 |
Syntax.string_of_sort ctxt S1 ^ " < " ^ Syntax.string_of_sort ctxt S2 |
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46614
diff
changeset
|
311 |
end; |
19529 | 312 |
|
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
313 |
exception CLASS_ERROR of class_error; |
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
314 |
|
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
315 |
|
48272 | 316 |
(* instances *) |
317 |
||
318 |
fun has_instance algebra a = |
|
319 |
forall (AList.defined (op =) (Symtab.lookup_list (arities_of algebra) a)); |
|
19529 | 320 |
|
19645 | 321 |
fun mg_domain algebra a S = |
19529 | 322 |
let |
48272 | 323 |
val ars = Symtab.lookup_list (arities_of algebra) a; |
19529 | 324 |
fun dom c = |
48272 | 325 |
(case AList.lookup (op =) ars c of |
36105 | 326 |
NONE => raise CLASS_ERROR (No_Arity (a, c)) |
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
327 |
| SOME Ss => Ss); |
19645 | 328 |
fun dom_inter c Ss = ListPair.map (inter_sort algebra) (dom c, Ss); |
19529 | 329 |
in |
330 |
(case S of |
|
331 |
[] => raise Fail "Unknown domain of empty intersection" |
|
332 |
| c :: cs => fold dom_inter cs (dom c)) |
|
333 |
end; |
|
334 |
||
335 |
||
26639 | 336 |
(* meet_sort *) |
337 |
||
338 |
fun meet_sort algebra = |
|
339 |
let |
|
340 |
fun inters S S' = inter_sort algebra (S, S'); |
|
341 |
fun meet _ [] = I |
|
342 |
| meet (TFree (_, S)) S' = |
|
343 |
if sort_le algebra (S, S') then I |
|
36105 | 344 |
else raise CLASS_ERROR (No_Subsort (S, S')) |
26639 | 345 |
| meet (TVar (v, S)) S' = |
346 |
if sort_le algebra (S, S') then I |
|
347 |
else Vartab.map_default (v, S) (inters S') |
|
348 |
| meet (Type (a, Ts)) S = fold2 meet Ts (mg_domain algebra a S); |
|
349 |
in uncurry meet end; |
|
350 |
||
28665 | 351 |
fun meet_sort_typ algebra (T, S) = |
74232 | 352 |
let val tab = Vartab.build (meet_sort algebra (T, S)); |
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
353 |
in Term.map_type_tvar (fn (v, _) => TVar (v, (the o Vartab.lookup tab) v)) end; |
28665 | 354 |
|
26639 | 355 |
|
19529 | 356 |
(* of_sort *) |
357 |
||
19645 | 358 |
fun of_sort algebra = |
19529 | 359 |
let |
360 |
fun ofS (_, []) = true |
|
19645 | 361 |
| ofS (TFree (_, S), S') = sort_le algebra (S, S') |
362 |
| ofS (TVar (_, S), S') = sort_le algebra (S, S') |
|
19529 | 363 |
| ofS (Type (a, Ts), S) = |
19645 | 364 |
let val Ss = mg_domain algebra a S in |
19529 | 365 |
ListPair.all ofS (Ts, Ss) |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
366 |
end handle CLASS_ERROR _ => false; |
19529 | 367 |
in ofS end; |
368 |
||
369 |
||
27498 | 370 |
(* animating derivations *) |
371 |
||
32791 | 372 |
fun of_sort_derivation algebra {class_relation, type_constructor, type_variable} = |
19529 | 373 |
let |
27555 | 374 |
val arities = arities_of algebra; |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
375 |
|
36104
fecb587a1d0e
of_sort_derivation: weaken bypasses derivation for identical sort -- accomodate proof terms by krauss/schropp, for example;
wenzelm
parents:
36103
diff
changeset
|
376 |
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
|
377 |
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
|
378 |
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
|
379 |
else |
fecb587a1d0e
of_sort_derivation: weaken bypasses derivation for identical sort -- accomodate proof terms by krauss/schropp, for example;
wenzelm
parents:
36103
diff
changeset
|
380 |
S2 |> map (fn c2 => |
62538
85ebb645b1a3
provide explicit hint concering uniqueness of derivation
haftmann
parents:
61264
diff
changeset
|
381 |
(case D1 |> filter (fn (_, c1) => class_le algebra (c1, c2)) of |
85ebb645b1a3
provide explicit hint concering uniqueness of derivation
haftmann
parents:
61264
diff
changeset
|
382 |
[d1] => class_relation T true d1 c2 |
85ebb645b1a3
provide explicit hint concering uniqueness of derivation
haftmann
parents:
61264
diff
changeset
|
383 |
| (d1 :: _ :: _) => class_relation T false d1 c2 |
85ebb645b1a3
provide explicit hint concering uniqueness of derivation
haftmann
parents:
61264
diff
changeset
|
384 |
| [] => 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
|
385 |
end; |
19529 | 386 |
|
36103 | 387 |
fun derive (_, []) = [] |
44338
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
wenzelm
parents:
43792
diff
changeset
|
388 |
| derive (Type (a, Us), S) = |
19529 | 389 |
let |
19645 | 390 |
val Ss = mg_domain algebra a S; |
36103 | 391 |
val dom = map2 (fn U => fn S => derive (U, S) ~~ S) Us Ss; |
19529 | 392 |
in |
393 |
S |> map (fn c => |
|
394 |
let |
|
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
395 |
val 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
|
396 |
val dom' = map (fn ((U, d), S') => weaken U d S' ~~ S') ((Us ~~ dom) ~~ Ss'); |
37248
8e8e5f9d1441
arities: no need to maintain original codomain (cf. f795c1164708) -- completion happens in axclass.ML;
wenzelm
parents:
36429
diff
changeset
|
397 |
in type_constructor (a, Us) dom' c end) |
19529 | 398 |
end |
36103 | 399 |
| derive (T, S) = weaken T (type_variable T) S; |
400 |
in derive end; |
|
19529 | 401 |
|
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
|
402 |
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
|
403 |
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
|
404 |
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
|
405 |
| 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
|
406 |
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
|
407 |
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
|
408 |
(case Graph.irreducible_paths (classes_of algebra) (c1, c2) of |
36105 | 409 |
[] => 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
|
410 |
| 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
|
411 |
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
|
412 |
|
19529 | 413 |
|
414 |
(* witness_sorts *) |
|
415 |
||
71526 | 416 |
fun witness_sorts algebra ground_types hyps sorts = |
19529 | 417 |
let |
19645 | 418 |
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
|
419 |
fun get S2 (T, S1) = if le S1 S2 then SOME (T, S2) else NONE; |
19645 | 420 |
fun mg_dom t S = SOME (mg_domain algebra t S) handle CLASS_ERROR _ => NONE; |
19529 | 421 |
|
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
422 |
fun witn_sort _ [] solved_failed = (SOME (propT, []), solved_failed) |
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
423 |
| witn_sort path S (solved, failed) = |
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
424 |
if exists (le S) failed then (NONE, (solved, failed)) |
19529 | 425 |
else |
31946
99ac0321cd47
witness_sorts: proper type witnesses for hyps, not invented "'hyp" variables;
wenzelm
parents:
30242
diff
changeset
|
426 |
(case get_first (get S) solved of |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
427 |
SOME w => (SOME w, (solved, failed)) |
19529 | 428 |
| NONE => |
31946
99ac0321cd47
witness_sorts: proper type witnesses for hyps, not invented "'hyp" variables;
wenzelm
parents:
30242
diff
changeset
|
429 |
(case get_first (get S) hyps of |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
430 |
SOME w => (SOME w, (w :: solved, failed)) |
71526 | 431 |
| NONE => witn_types path ground_types S (solved, failed))) |
19529 | 432 |
|
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
433 |
and witn_sorts path x = fold_map (witn_sort path) x |
19529 | 434 |
|
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
435 |
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
|
436 |
| witn_types path (t :: ts) S solved_failed = |
19529 | 437 |
(case mg_dom t S of |
438 |
SOME SS => |
|
439 |
(*do not descend into stronger args (achieving termination)*) |
|
440 |
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
|
441 |
witn_types path ts S solved_failed |
19529 | 442 |
else |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
443 |
let val (ws, (solved', failed')) = witn_sorts (S :: path) SS solved_failed in |
19529 | 444 |
if forall is_some ws then |
445 |
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
|
446 |
in (SOME w, (w :: solved', failed')) end |
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
447 |
else witn_types path ts S (solved', failed') |
19529 | 448 |
end |
19578
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents:
19531
diff
changeset
|
449 |
| NONE => witn_types path ts S solved_failed); |
19529 | 450 |
|
19584 | 451 |
in map_filter I (#1 (witn_sorts [] sorts ([], []))) end; |
19529 | 452 |
|
19514 | 453 |
end; |