| author | wenzelm | 
| Wed, 30 Sep 2009 22:27:20 +0200 | |
| changeset 32789 | d89327de0b3c | 
| parent 31946 | 99ac0321cd47 | 
| child 32791 | e6d47ce70d27 | 
| 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 | 
| 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: 
21926diff
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 | |
| 28623 | 41 | val minimal_sorts: algebra -> sort list -> sort OrdList.T | 
| 19645 | 42 | val certify_class: algebra -> class -> class (*exception TYPE*) | 
| 43 | val certify_sort: algebra -> sort -> sort (*exception TYPE*) | |
| 44 | val add_class: Pretty.pp -> class * class list -> algebra -> algebra | |
| 45 | val add_classrel: Pretty.pp -> class * class -> algebra -> algebra | |
| 46 | val add_arities: Pretty.pp -> string * (class * sort list) list -> algebra -> algebra | |
| 47 | val empty_algebra: algebra | |
| 48 | val merge_algebra: Pretty.pp -> algebra * algebra -> algebra | |
| 30060 | 49 | val subalgebra: Pretty.pp -> (class -> bool) -> (class * string -> sort list option) | 
| 22181 
39104d1c43ca
added explicit query function for arities to subalgebra projection
 haftmann parents: 
21933diff
changeset | 50 | -> algebra -> (sort -> sort) * algebra | 
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 51 | type class_error | 
| 26639 | 52 | val class_error: Pretty.pp -> class_error -> string | 
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 53 | exception CLASS_ERROR of class_error | 
| 19645 | 54 | val mg_domain: algebra -> string -> sort -> sort list (*exception CLASS_ERROR*) | 
| 28665 | 55 | val meet_sort: algebra -> typ * sort | 
| 56 | -> sort Vartab.table -> sort Vartab.table (*exception CLASS_ERROR*) | |
| 57 | val meet_sort_typ: algebra -> typ * sort -> typ -> typ (*exception CLASS_ERROR*) | |
| 19645 | 58 | val of_sort: algebra -> typ * sort -> bool | 
| 27555 | 59 |   val weaken: algebra -> ('a * class -> class -> 'a) -> 'a * class -> class -> 'a
 | 
| 19645 | 60 | val of_sort_derivation: Pretty.pp -> algebra -> | 
| 22570 
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
 wenzelm parents: 
22364diff
changeset | 61 |     {class_relation: 'a * class -> class -> 'a,
 | 
| 
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
 wenzelm parents: 
22364diff
changeset | 62 |      type_constructor: string -> ('a * class) list list -> class -> 'a,
 | 
| 
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
 wenzelm parents: 
22364diff
changeset | 63 |      type_variable: typ -> ('a * class) list} ->
 | 
| 19584 | 64 | typ * sort -> 'a list (*exception CLASS_ERROR*) | 
| 31946 
99ac0321cd47
witness_sorts: proper type witnesses for hyps, not invented "'hyp" variables;
 wenzelm parents: 
30242diff
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: 
9281diff
changeset | 73 | |
| 29269 
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
 wenzelm parents: 
28922diff
changeset | 74 | val make = OrdList.make TermOrd.sort_ord; | 
| 
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
 wenzelm parents: 
28922diff
changeset | 75 | val op subset = OrdList.subset TermOrd.sort_ord; | 
| 
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
 wenzelm parents: 
28922diff
changeset | 76 | val op union = OrdList.union TermOrd.sort_ord; | 
| 
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
 wenzelm parents: 
28922diff
changeset | 77 | val subtract = OrdList.subtract TermOrd.sort_ord; | 
| 14782 
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
 wenzelm parents: 
9281diff
changeset | 78 | |
| 29269 
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
 wenzelm parents: 
28922diff
changeset | 79 | val remove_sort = OrdList.remove TermOrd.sort_ord; | 
| 
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
 wenzelm parents: 
28922diff
changeset | 80 | val insert_sort = OrdList.insert TermOrd.sort_ord; | 
| 14782 
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
 wenzelm parents: 
9281diff
changeset | 81 | |
| 16598 
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
 wenzelm parents: 
16400diff
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: 
16400diff
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: 
16400diff
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: 
16400diff
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: 
16400diff
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: 
9281diff
changeset | 87 | |
| 16598 
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
 wenzelm parents: 
16400diff
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: 
16400diff
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: 
16400diff
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: 
16400diff
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: 
16400diff
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: 
16400diff
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: 
9281diff
changeset | 94 | |
| 16598 
59381032be14
removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
 wenzelm parents: 
16400diff
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: 
16400diff
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: 
9281diff
changeset | 97 | |
| 
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
 wenzelm parents: 
9281diff
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: 
9281diff
changeset | 103 | classes: graph representing class declarations together with proper | 
| 
d6ce35a1c386
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
 wenzelm parents: 
9281diff
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: 
9281diff
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 | ||
| 118 | fun rep_algebra (Algebra args) = args; | |
| 119 | ||
| 120 | val classes_of = #classes o rep_algebra; | |
| 121 | val arities_of = #arities o rep_algebra; | |
| 122 | ||
| 123 | fun make_algebra (classes, arities) = | |
| 124 |   Algebra {classes = classes, arities = arities};
 | |
| 125 | ||
| 126 | fun map_classes f (Algebra {classes, arities}) = make_algebra (f classes, arities);
 | |
| 127 | fun map_arities f (Algebra {classes, arities}) = make_algebra (classes, f arities);
 | |
| 128 | ||
| 129 | ||
| 130 | (* classes *) | |
| 131 | ||
| 21933 
819ef284720b
classes: more direct way to achieve topological sorting;
 wenzelm parents: 
21926diff
changeset | 132 | fun all_classes (Algebra {classes, ...}) = Graph.all_preds classes (Graph.maximals classes);
 | 
| 
819ef284720b
classes: more direct way to achieve topological sorting;
 wenzelm parents: 
21926diff
changeset | 133 | |
| 19645 | 134 | val super_classes = Graph.imm_succs o classes_of; | 
| 2956 | 135 | |
| 136 | ||
| 19529 | 137 | (* class relations *) | 
| 2956 | 138 | |
| 19645 | 139 | val class_less = Graph.is_edge o classes_of; | 
| 140 | fun class_le algebra (c1, c2) = c1 = c2 orelse class_less algebra (c1, c2); | |
| 2956 | 141 | |
| 142 | ||
| 19529 | 143 | (* sort relations *) | 
| 2956 | 144 | |
| 19645 | 145 | fun sort_le algebra (S1, S2) = | 
| 23585 | 146 | S1 = S2 orelse forall (fn c2 => exists (fn c1 => class_le algebra (c1, c2)) S1) S2; | 
| 2956 | 147 | |
| 19645 | 148 | fun sorts_le algebra (Ss1, Ss2) = | 
| 149 | ListPair.all (sort_le algebra) (Ss1, Ss2); | |
| 2956 | 150 | |
| 19645 | 151 | fun sort_eq algebra (S1, S2) = | 
| 152 | sort_le algebra (S1, S2) andalso sort_le algebra (S2, S1); | |
| 2956 | 153 | |
| 154 | ||
| 19529 | 155 | (* intersection *) | 
| 2956 | 156 | |
| 19645 | 157 | fun inter_class algebra c S = | 
| 2956 | 158 | let | 
| 159 | fun intr [] = [c] | |
| 160 | | intr (S' as c' :: c's) = | |
| 19645 | 161 | if class_le algebra (c', c) then S' | 
| 162 | else if class_le algebra (c, c') then intr c's | |
| 2956 | 163 | else c' :: intr c's | 
| 164 | in intr S end; | |
| 165 | ||
| 19645 | 166 | fun inter_sort algebra (S1, S2) = | 
| 167 | sort_strings (fold (inter_class algebra) S1 S2); | |
| 2956 | 168 | |
| 169 | ||
| 24732 | 170 | (* normal forms *) | 
| 2956 | 171 | |
| 24732 | 172 | fun minimize_sort _ [] = [] | 
| 173 | | minimize_sort _ (S as [_]) = S | |
| 174 | | minimize_sort algebra S = | |
| 19645 | 175 | filter (fn c => not (exists (fn c' => class_less algebra (c', c)) S)) S | 
| 19529 | 176 | |> sort_distinct string_ord; | 
| 2990 | 177 | |
| 24732 | 178 | fun complete_sort algebra = | 
| 179 | Graph.all_succs (classes_of algebra) o minimize_sort algebra; | |
| 180 | ||
| 28623 | 181 | fun minimal_sorts algebra raw_sorts = | 
| 182 | let | |
| 183 | fun le S1 S2 = sort_le algebra (S1, S2); | |
| 184 | val sorts = make (map (minimize_sort algebra) raw_sorts); | |
| 185 | in sorts |> filter_out (fn S => exists (fn S' => le S' S andalso not (le S S')) sorts) end; | |
| 186 | ||
| 2990 | 187 | |
| 19645 | 188 | (* certify *) | 
| 189 | ||
| 190 | fun certify_class algebra c = | |
| 191 | if can (Graph.get_node (classes_of algebra)) c then c | |
| 192 |   else raise TYPE ("Undeclared class: " ^ quote c, [], []);
 | |
| 193 | ||
| 24732 | 194 | fun certify_sort classes = minimize_sort classes o map (certify_class classes); | 
| 19645 | 195 | |
| 196 | ||
| 2956 | 197 | |
| 19529 | 198 | (** build algebras **) | 
| 19514 | 199 | |
| 200 | (* classes *) | |
| 201 | ||
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
23585diff
changeset | 202 | fun err_dup_class c = error ("Duplicate declaration of class: " ^ quote c);
 | 
| 19514 | 203 | |
| 204 | fun err_cyclic_classes pp css = | |
| 205 | error (cat_lines (map (fn cs => | |
| 206 | "Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css)); | |
| 207 | ||
| 19645 | 208 | fun add_class pp (c, cs) = map_classes (fn classes => | 
| 19514 | 209 | let | 
| 20573 | 210 | val classes' = classes |> Graph.new_node (c, serial ()) | 
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
23585diff
changeset | 211 | handle Graph.DUP dup => err_dup_class dup; | 
| 19514 | 212 | val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs) | 
| 213 | handle Graph.CYCLES css => err_cyclic_classes pp css; | |
| 19645 | 214 | in classes'' end); | 
| 19514 | 215 | |
| 216 | ||
| 217 | (* arities *) | |
| 218 | ||
| 219 | local | |
| 220 | ||
| 221 | fun for_classes _ NONE = "" | |
| 222 | | for_classes pp (SOME (c1, c2)) = | |
| 223 | " for classes " ^ Pretty.string_of_classrel pp [c1, c2]; | |
| 224 | ||
| 225 | fun err_conflict pp t cc (c, Ss) (c', Ss') = | |
| 226 |   error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n  " ^
 | |
| 227 | Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n " ^ | |
| 228 | Pretty.string_of_arity pp (t, Ss', [c'])); | |
| 229 | ||
| 19645 | 230 | fun coregular pp algebra t (c, (c0, Ss)) ars = | 
| 19514 | 231 | let | 
| 19524 | 232 | fun conflict (c', (_, Ss')) = | 
| 19645 | 233 | if class_le algebra (c, c') andalso not (sorts_le algebra (Ss, Ss')) then | 
| 19514 | 234 | SOME ((c, c'), (c', Ss')) | 
| 19645 | 235 | else if class_le algebra (c', c) andalso not (sorts_le algebra (Ss', Ss)) then | 
| 19514 | 236 | SOME ((c', c), (c', Ss')) | 
| 237 | else NONE; | |
| 238 | in | |
| 239 | (case get_first conflict ars of | |
| 240 | SOME ((c1, c2), (c', Ss')) => err_conflict pp t (SOME (c1, c2)) (c, Ss) (c', Ss') | |
| 19524 | 241 | | NONE => (c, (c0, Ss)) :: ars) | 
| 19514 | 242 | end; | 
| 243 | ||
| 19645 | 244 | fun complete algebra (c0, Ss) = map (rpair (c0, Ss)) (c0 :: super_classes algebra c0); | 
| 245 | ||
| 246 | fun insert pp algebra t (c, (c0, Ss)) ars = | |
| 19514 | 247 | (case AList.lookup (op =) ars c of | 
| 19645 | 248 | NONE => coregular pp algebra t (c, (c0, Ss)) ars | 
| 19524 | 249 | | SOME (_, Ss') => | 
| 19645 | 250 | if sorts_le algebra (Ss, Ss') then ars | 
| 251 | else if sorts_le algebra (Ss', Ss) then | |
| 252 | coregular pp algebra t (c, (c0, Ss)) | |
| 19524 | 253 | (filter_out (fn (c'', (_, Ss'')) => c = c'' andalso Ss'' = Ss') ars) | 
| 19514 | 254 | else err_conflict pp t NONE (c, Ss) (c, Ss')); | 
| 255 | ||
| 19645 | 256 | fun insert_ars pp algebra (t, ars) arities = | 
| 257 | let val ars' = | |
| 258 | Symtab.lookup_list arities t | |
| 259 | |> fold_rev (fold_rev (insert pp algebra t)) (map (complete algebra) ars) | |
| 260 | in Symtab.update (t, ars') arities end; | |
| 19514 | 261 | |
| 262 | in | |
| 263 | ||
| 19645 | 264 | fun add_arities pp arg algebra = algebra |> map_arities (insert_ars pp algebra arg); | 
| 19514 | 265 | |
| 19645 | 266 | fun add_arities_table pp algebra = | 
| 267 | Symtab.fold (fn (t, ars) => insert_ars pp algebra (t, map snd ars)); | |
| 19514 | 268 | |
| 269 | end; | |
| 270 | ||
| 19529 | 271 | |
| 19645 | 272 | (* classrel *) | 
| 273 | ||
| 274 | fun rebuild_arities pp algebra = algebra |> map_arities (fn arities => | |
| 275 | Symtab.empty | |
| 276 | |> add_arities_table pp algebra arities); | |
| 277 | ||
| 278 | fun add_classrel pp rel = rebuild_arities pp o map_classes (fn classes => | |
| 279 | classes |> Graph.add_edge_trans_acyclic rel | |
| 280 | handle Graph.CYCLES css => err_cyclic_classes pp css); | |
| 281 | ||
| 282 | ||
| 283 | (* empty and merge *) | |
| 284 | ||
| 285 | val empty_algebra = make_algebra (Graph.empty, Symtab.empty); | |
| 286 | ||
| 287 | fun merge_algebra pp | |
| 288 |    (Algebra {classes = classes1, arities = arities1},
 | |
| 289 |     Algebra {classes = classes2, arities = arities2}) =
 | |
| 290 | let | |
| 291 | val classes' = Graph.merge_trans_acyclic (op =) (classes1, classes2) | |
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
23585diff
changeset | 292 | handle Graph.DUP c => err_dup_class c | 
| 19645 | 293 | | Graph.CYCLES css => err_cyclic_classes pp css; | 
| 294 | val algebra0 = make_algebra (classes', Symtab.empty); | |
| 295 | val arities' = Symtab.empty | |
| 296 | |> add_arities_table pp algebra0 arities1 | |
| 297 | |> add_arities_table pp algebra0 arities2; | |
| 298 | in make_algebra (classes', arities') end; | |
| 299 | ||
| 21933 
819ef284720b
classes: more direct way to achieve topological sorting;
 wenzelm parents: 
21926diff
changeset | 300 | |
| 28922 | 301 | (* algebra projections *) | 
| 302 | ||
| 22181 
39104d1c43ca
added explicit query function for arities to subalgebra projection
 haftmann parents: 
21933diff
changeset | 303 | fun subalgebra pp P sargs (algebra as Algebra {classes, arities}) =
 | 
| 19952 | 304 | let | 
| 24732 | 305 | 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: 
21933diff
changeset | 306 | fun restrict_arity tyco (c, (_, Ss)) = | 
| 30060 | 307 | if P c then case sargs (c, tyco) | 
| 308 | 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: 
21933diff
changeset | 309 | |> map restrict_sort)) | 
| 30062 | 310 | | NONE => NONE | 
| 22181 
39104d1c43ca
added explicit query function for arities to subalgebra projection
 haftmann parents: 
21933diff
changeset | 311 | else NONE; | 
| 21933 
819ef284720b
classes: more direct way to achieve topological sorting;
 wenzelm parents: 
21926diff
changeset | 312 | val classes' = classes |> Graph.subgraph P; | 
| 22181 
39104d1c43ca
added explicit query function for arities to subalgebra projection
 haftmann parents: 
21933diff
changeset | 313 | val arities' = arities |> Symtab.map' (map_filter o restrict_arity); | 
| 21933 
819ef284720b
classes: more direct way to achieve topological sorting;
 wenzelm parents: 
21926diff
changeset | 314 | in (restrict_sort, rebuild_arities pp (make_algebra (classes', arities'))) end; | 
| 20465 | 315 | |
| 19645 | 316 | |
| 19529 | 317 | |
| 318 | (** sorts of types **) | |
| 319 | ||
| 26639 | 320 | (* errors -- delayed message composition *) | 
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 321 | |
| 26639 | 322 | datatype class_error = | 
| 323 | NoClassrel of class * class | | |
| 324 | NoArity of string * class | | |
| 325 | NoSubsort of sort * sort; | |
| 19529 | 326 | |
| 26639 | 327 | fun class_error pp (NoClassrel (c1, c2)) = | 
| 22196 | 328 | "No class relation " ^ Pretty.string_of_classrel pp [c1, c2] | 
| 26639 | 329 | | class_error pp (NoArity (a, c)) = | 
| 26326 | 330 | "No type arity " ^ Pretty.string_of_arity pp (a, [], [c]) | 
| 26994 | 331 | | class_error pp (NoSubsort (S1, S2)) = | 
| 332 | "Cannot derive subsort relation " ^ Pretty.string_of_sort pp S1 | |
| 333 | ^ " < " ^ Pretty.string_of_sort pp S2; | |
| 19529 | 334 | |
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 335 | exception CLASS_ERROR of class_error; | 
| 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 336 | |
| 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 337 | |
| 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 338 | (* mg_domain *) | 
| 19529 | 339 | |
| 19645 | 340 | fun mg_domain algebra a S = | 
| 19529 | 341 | let | 
| 19645 | 342 | val arities = arities_of algebra; | 
| 19529 | 343 | fun dom c = | 
| 344 | (case AList.lookup (op =) (Symtab.lookup_list arities a) c of | |
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 345 | NONE => raise CLASS_ERROR (NoArity (a, c)) | 
| 19529 | 346 | | SOME (_, Ss) => Ss); | 
| 19645 | 347 | fun dom_inter c Ss = ListPair.map (inter_sort algebra) (dom c, Ss); | 
| 19529 | 348 | in | 
| 349 | (case S of | |
| 350 | [] => raise Fail "Unknown domain of empty intersection" | |
| 351 | | c :: cs => fold dom_inter cs (dom c)) | |
| 352 | end; | |
| 353 | ||
| 354 | ||
| 26639 | 355 | (* meet_sort *) | 
| 356 | ||
| 357 | fun meet_sort algebra = | |
| 358 | let | |
| 359 | fun inters S S' = inter_sort algebra (S, S'); | |
| 360 | fun meet _ [] = I | |
| 361 | | meet (TFree (_, S)) S' = | |
| 362 | if sort_le algebra (S, S') then I | |
| 363 | else raise CLASS_ERROR (NoSubsort (S, S')) | |
| 364 | | meet (TVar (v, S)) S' = | |
| 365 | if sort_le algebra (S, S') then I | |
| 366 | else Vartab.map_default (v, S) (inters S') | |
| 367 | | meet (Type (a, Ts)) S = fold2 meet Ts (mg_domain algebra a S); | |
| 368 | in uncurry meet end; | |
| 369 | ||
| 28665 | 370 | fun meet_sort_typ algebra (T, S) = | 
| 371 | let | |
| 372 | val tab = meet_sort algebra (T, S) Vartab.empty; | |
| 373 | in Term.map_type_tvar (fn (v, _) => | |
| 374 | TVar (v, (the o Vartab.lookup tab) v)) | |
| 375 | end; | |
| 376 | ||
| 26639 | 377 | |
| 19529 | 378 | (* of_sort *) | 
| 379 | ||
| 19645 | 380 | fun of_sort algebra = | 
| 19529 | 381 | let | 
| 382 | fun ofS (_, []) = true | |
| 19645 | 383 | | ofS (TFree (_, S), S') = sort_le algebra (S, S') | 
| 384 | | ofS (TVar (_, S), S') = sort_le algebra (S, S') | |
| 19529 | 385 | | ofS (Type (a, Ts), S) = | 
| 19645 | 386 | let val Ss = mg_domain algebra a S in | 
| 19529 | 387 | ListPair.all ofS (Ts, Ss) | 
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 388 | end handle CLASS_ERROR _ => false; | 
| 19529 | 389 | in ofS end; | 
| 390 | ||
| 391 | ||
| 27498 | 392 | (* animating derivations *) | 
| 393 | ||
| 27555 | 394 | fun weaken algebra class_relation = | 
| 395 | let | |
| 396 | fun path (x, c1 :: c2 :: cs) = path (class_relation (x, c1) c2, c2 :: cs) | |
| 397 | | path (x, _) = x; | |
| 398 | in fn (x, c1) => fn c2 => | |
| 399 | (case Graph.irreducible_paths (classes_of algebra) (c1, c2) of | |
| 400 | [] => raise CLASS_ERROR (NoClassrel (c1, c2)) | |
| 401 | | cs :: _ => path (x, cs)) | |
| 402 | end; | |
| 19529 | 403 | |
| 22570 
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
 wenzelm parents: 
22364diff
changeset | 404 | fun of_sort_derivation pp algebra {class_relation, type_constructor, type_variable} =
 | 
| 19529 | 405 | let | 
| 27555 | 406 | val weaken = weaken algebra class_relation; | 
| 407 | val arities = arities_of algebra; | |
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 408 | |
| 19529 | 409 | fun weakens S1 S2 = S2 |> map (fn c2 => | 
| 19645 | 410 | (case S1 |> find_first (fn (_, c1) => class_le algebra (c1, c2)) of | 
| 19529 | 411 | SOME d1 => weaken d1 c2 | 
| 26994 | 412 | | NONE => raise CLASS_ERROR (NoSubsort (map #2 S1, S2)))); | 
| 19529 | 413 | |
| 414 | fun derive _ [] = [] | |
| 415 | | derive (Type (a, Ts)) S = | |
| 416 | let | |
| 19645 | 417 | val Ss = mg_domain algebra a S; | 
| 19529 | 418 | val dom = map2 (fn T => fn S => derive T S ~~ S) Ts Ss; | 
| 419 | in | |
| 420 | S |> map (fn c => | |
| 421 | let | |
| 422 | val (c0, Ss') = the (AList.lookup (op =) (Symtab.lookup_list arities a) c); | |
| 423 | 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: 
22364diff
changeset | 424 | in weaken (type_constructor a dom' c0, c0) c end) | 
| 19529 | 425 | end | 
| 22570 
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
 wenzelm parents: 
22364diff
changeset | 426 | | derive T S = weakens (type_variable T) S; | 
| 19529 | 427 | in uncurry derive end; | 
| 428 | ||
| 429 | ||
| 430 | (* witness_sorts *) | |
| 431 | ||
| 19645 | 432 | fun witness_sorts algebra types hyps sorts = | 
| 19529 | 433 | let | 
| 19645 | 434 | fun le S1 S2 = sort_le algebra (S1, S2); | 
| 31946 
99ac0321cd47
witness_sorts: proper type witnesses for hyps, not invented "'hyp" variables;
 wenzelm parents: 
30242diff
changeset | 435 | fun get S2 (T, S1) = if le S1 S2 then SOME (T, S2) else NONE; | 
| 19645 | 436 | fun mg_dom t S = SOME (mg_domain algebra t S) handle CLASS_ERROR _ => NONE; | 
| 19529 | 437 | |
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 438 | fun witn_sort _ [] solved_failed = (SOME (propT, []), solved_failed) | 
| 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 439 | | witn_sort path S (solved, failed) = | 
| 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 440 | if exists (le S) failed then (NONE, (solved, failed)) | 
| 19529 | 441 | else | 
| 31946 
99ac0321cd47
witness_sorts: proper type witnesses for hyps, not invented "'hyp" variables;
 wenzelm parents: 
30242diff
changeset | 442 | (case get_first (get S) solved of | 
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 443 | SOME w => (SOME w, (solved, failed)) | 
| 19529 | 444 | | NONE => | 
| 31946 
99ac0321cd47
witness_sorts: proper type witnesses for hyps, not invented "'hyp" variables;
 wenzelm parents: 
30242diff
changeset | 445 | (case get_first (get S) hyps of | 
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 446 | SOME w => (SOME w, (w :: solved, failed)) | 
| 19584 | 447 | | NONE => witn_types path types S (solved, failed))) | 
| 19529 | 448 | |
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 449 | and witn_sorts path x = fold_map (witn_sort path) x | 
| 19529 | 450 | |
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 451 | and witn_types _ [] S (solved, failed) = (NONE, (solved, S :: failed)) | 
| 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 452 | | witn_types path (t :: ts) S solved_failed = | 
| 19529 | 453 | (case mg_dom t S of | 
| 454 | SOME SS => | |
| 455 | (*do not descend into stronger args (achieving termination)*) | |
| 456 | 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: 
19531diff
changeset | 457 | witn_types path ts S solved_failed | 
| 19529 | 458 | else | 
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 459 | let val (ws, (solved', failed')) = witn_sorts (S :: path) SS solved_failed in | 
| 19529 | 460 | if forall is_some ws then | 
| 461 | let val w = (Type (t, map (#1 o the) ws), S) | |
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 462 | in (SOME w, (w :: solved', failed')) end | 
| 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 463 | else witn_types path ts S (solved', failed') | 
| 19529 | 464 | end | 
| 19578 
f93b7637a5e6
added class_error and exception CLASS_ERROR (supercedes DOMAIN);
 wenzelm parents: 
19531diff
changeset | 465 | | NONE => witn_types path ts S solved_failed); | 
| 19529 | 466 | |
| 19584 | 467 | in map_filter I (#1 (witn_sorts [] sorts ([], []))) end; | 
| 19529 | 468 | |
| 19514 | 469 | end; |