--- a/src/Pure/sorts.ML Tue May 16 13:01:27 2006 +0200
+++ b/src/Pure/sorts.ML Tue May 16 13:01:28 2006 +0200
@@ -24,34 +24,36 @@
val insert_typs: typ list -> sort list -> sort list
val insert_term: term -> sort list -> sort list
val insert_terms: term list -> sort list -> sort list
- type classes
- type arities
- val class_eq: classes -> class * class -> bool
- val class_less: classes -> class * class -> bool
- val class_le: classes -> class * class -> bool
- val sort_eq: classes -> sort * sort -> bool
- val sort_le: classes -> sort * sort -> bool
- val sorts_le: classes -> sort list * sort list -> bool
- val inter_sort: classes -> sort * sort -> sort
- val norm_sort: classes -> sort -> sort
- val add_arities: Pretty.pp -> classes -> string * (class * sort list) list -> arities -> arities
- val rebuild_arities: Pretty.pp -> classes -> arities -> arities
- val merge_arities: Pretty.pp -> classes -> arities * arities -> arities
- val add_class: Pretty.pp -> class * class list -> classes -> classes
- val add_classrel: Pretty.pp -> class * class -> classes -> classes
- val merge_classes: Pretty.pp -> classes * classes -> classes
+ type algebra
+ val rep_algebra: algebra ->
+ {classes: stamp Graph.T,
+ arities: (class * (class * sort list)) list Symtab.table}
+ val classes: algebra -> class list
+ val super_classes: algebra -> class -> class list
+ val class_less: algebra -> class * class -> bool
+ val class_le: algebra -> class * class -> bool
+ val sort_eq: algebra -> sort * sort -> bool
+ val sort_le: algebra -> sort * sort -> bool
+ val sorts_le: algebra -> sort list * sort list -> bool
+ val inter_sort: algebra -> sort * sort -> sort
+ val certify_class: algebra -> class -> class (*exception TYPE*)
+ val certify_sort: algebra -> sort -> sort (*exception TYPE*)
+ val add_class: Pretty.pp -> class * class list -> algebra -> algebra
+ val add_classrel: Pretty.pp -> class * class -> algebra -> algebra
+ val add_arities: Pretty.pp -> string * (class * sort list) list -> algebra -> algebra
+ val empty_algebra: algebra
+ val merge_algebra: Pretty.pp -> algebra * algebra -> algebra
type class_error
val class_error: Pretty.pp -> class_error -> 'a
exception CLASS_ERROR of class_error
- val mg_domain: classes * arities -> string -> sort -> sort list (*exception CLASS_ERROR*)
- val of_sort: classes * arities -> typ * sort -> bool
- val of_sort_derivation: Pretty.pp -> classes * arities ->
+ val mg_domain: algebra -> string -> sort -> sort list (*exception CLASS_ERROR*)
+ val of_sort: algebra -> typ * sort -> bool
+ val of_sort_derivation: Pretty.pp -> algebra ->
{classrel: 'a * class -> class -> 'a,
constructor: string -> ('a * class) list list -> class -> 'a,
variable: typ -> ('a * class) list} ->
typ * sort -> 'a list (*exception CLASS_ERROR*)
- val witness_sorts: classes * arities -> string list ->
- sort list -> sort list -> (typ * sort) list
+ val witness_sorts: algebra -> string list -> sort list -> sort list -> (typ * sort) list
end;
structure Sorts: SORTS =
@@ -99,60 +101,84 @@
c1 <= c2 holds Ss1 <= Ss2.
*)
-type classes = stamp Graph.T;
-type arities = (class * (class * sort list)) list Symtab.table;
+datatype algebra = Algebra of
+ {classes: stamp Graph.T,
+ arities: (class * (class * sort list)) list Symtab.table};
+
+fun rep_algebra (Algebra args) = args;
+
+val classes_of = #classes o rep_algebra;
+val arities_of = #arities o rep_algebra;
+
+fun make_algebra (classes, arities) =
+ Algebra {classes = classes, arities = arities};
+
+fun map_classes f (Algebra {classes, arities}) = make_algebra (f classes, arities);
+fun map_arities f (Algebra {classes, arities}) = make_algebra (classes, f arities);
+
+
+(* classes *)
+
+val classes = Graph.keys o classes_of;
+val super_classes = Graph.imm_succs o classes_of;
(* class relations *)
-fun class_eq (_: classes) (c1, c2:class) = c1 = c2;
-val class_less: classes -> class * class -> bool = Graph.is_edge;
-fun class_le classes (c1, c2) = c1 = c2 orelse class_less classes (c1, c2);
+val class_less = Graph.is_edge o classes_of;
+fun class_le algebra (c1, c2) = c1 = c2 orelse class_less algebra (c1, c2);
(* sort relations *)
-fun sort_le classes (S1, S2) =
- forall (fn c2 => exists (fn c1 => class_le classes (c1, c2)) S1) S2;
+fun sort_le algebra (S1, S2) =
+ forall (fn c2 => exists (fn c1 => class_le algebra (c1, c2)) S1) S2;
-fun sorts_le classes (Ss1, Ss2) =
- ListPair.all (sort_le classes) (Ss1, Ss2);
+fun sorts_le algebra (Ss1, Ss2) =
+ ListPair.all (sort_le algebra) (Ss1, Ss2);
-fun sort_eq classes (S1, S2) =
- sort_le classes (S1, S2) andalso sort_le classes (S2, S1);
+fun sort_eq algebra (S1, S2) =
+ sort_le algebra (S1, S2) andalso sort_le algebra (S2, S1);
(* intersection *)
-fun inter_class classes c S =
+fun inter_class algebra c S =
let
fun intr [] = [c]
| intr (S' as c' :: c's) =
- if class_le classes (c', c) then S'
- else if class_le classes (c, c') then intr c's
+ if class_le algebra (c', c) then S'
+ else if class_le algebra (c, c') then intr c's
else c' :: intr c's
in intr S end;
-fun inter_sort classes (S1, S2) =
- sort_strings (fold (inter_class classes) S1 S2);
+fun inter_sort algebra (S1, S2) =
+ sort_strings (fold (inter_class algebra) S1 S2);
-(* normal forms *)
+(* normal form *)
fun norm_sort _ [] = []
| norm_sort _ (S as [_]) = S
- | norm_sort classes S =
- filter (fn c => not (exists (fn c' => class_less classes (c', c)) S)) S
+ | norm_sort algebra S =
+ filter (fn c => not (exists (fn c' => class_less algebra (c', c)) S)) S
|> sort_distinct string_ord;
+(* certify *)
+
+fun certify_class algebra c =
+ if can (Graph.get_node (classes_of algebra)) c then c
+ else raise TYPE ("Undeclared class: " ^ quote c, [], []);
+
+fun certify_sort classes = norm_sort classes o map (certify_class classes);
+
+
(** build algebras **)
(* classes *)
-local
-
fun err_dup_classes cs =
error ("Duplicate declaration of class(es): " ^ commas_quote cs);
@@ -160,26 +186,13 @@
error (cat_lines (map (fn cs =>
"Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css));
-in
-
-fun add_class pp (c, cs) classes =
+fun add_class pp (c, cs) = map_classes (fn classes =>
let
val classes' = classes |> Graph.new_node (c, stamp ())
handle Graph.DUP dup => err_dup_classes [dup];
val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs)
handle Graph.CYCLES css => err_cyclic_classes pp css;
- in classes'' end;
-
-fun add_classrel pp rel classes =
- classes |> Graph.add_edge_trans_acyclic rel
- handle Graph.CYCLES css => err_cyclic_classes pp css;
-
-fun merge_classes pp args : classes =
- Graph.merge_trans_acyclic (op =) args
- handle Graph.DUPS cs => err_dup_classes cs
- | Graph.CYCLES css => err_cyclic_classes pp css;
-
-end;
+ in classes'' end);
(* arities *)
@@ -195,12 +208,12 @@
Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n " ^
Pretty.string_of_arity pp (t, Ss', [c']));
-fun coregular pp C t (c, (c0, Ss)) ars =
+fun coregular pp algebra t (c, (c0, Ss)) ars =
let
fun conflict (c', (_, Ss')) =
- if class_le C (c, c') andalso not (sorts_le C (Ss, Ss')) then
+ if class_le algebra (c, c') andalso not (sorts_le algebra (Ss, Ss')) then
SOME ((c, c'), (c', Ss'))
- else if class_le C (c', c) andalso not (sorts_le C (Ss', Ss)) then
+ else if class_le algebra (c', c) andalso not (sorts_le algebra (Ss', Ss)) then
SOME ((c', c), (c', Ss'))
else NONE;
in
@@ -209,41 +222,63 @@
| NONE => (c, (c0, Ss)) :: ars)
end;
-fun insert pp C t (c, (c0, Ss)) ars =
+fun complete algebra (c0, Ss) = map (rpair (c0, Ss)) (c0 :: super_classes algebra c0);
+
+fun insert pp algebra t (c, (c0, Ss)) ars =
(case AList.lookup (op =) ars c of
- NONE => coregular pp C t (c, (c0, Ss)) ars
+ NONE => coregular pp algebra t (c, (c0, Ss)) ars
| SOME (_, Ss') =>
- if sorts_le C (Ss, Ss') then ars
- else if sorts_le C (Ss', Ss) then
- coregular pp C t (c, (c0, Ss))
+ if sorts_le algebra (Ss, Ss') then ars
+ else if sorts_le algebra (Ss', Ss) then
+ coregular pp algebra t (c, (c0, Ss))
(filter_out (fn (c'', (_, Ss'')) => c = c'' andalso Ss'' = Ss') ars)
else err_conflict pp t NONE (c, Ss) (c, Ss'));
-fun complete C (c0, Ss) = map (rpair (c0, Ss)) (Graph.all_succs C [c0]);
+fun insert_ars pp algebra (t, ars) arities =
+ let val ars' =
+ Symtab.lookup_list arities t
+ |> fold_rev (fold_rev (insert pp algebra t)) (map (complete algebra) ars)
+ in Symtab.update (t, ars') arities end;
in
-fun add_arities pp classes (t, ars) arities =
- let val ars' =
- Symtab.lookup_list arities t
- |> fold_rev (fold_rev (insert pp classes t)) (map (complete classes) ars)
- in Symtab.update (t, ars') arities end;
-
-fun add_arities_table pp classes =
- Symtab.fold (fn (t, ars) => add_arities pp classes (t, map snd ars));
+fun add_arities pp arg algebra = algebra |> map_arities (insert_ars pp algebra arg);
-fun rebuild_arities pp classes arities =
- Symtab.empty
- |> add_arities_table pp classes arities;
-
-fun merge_arities pp classes (arities1, arities2) =
- Symtab.empty
- |> add_arities_table pp classes arities1
- |> add_arities_table pp classes arities2;
+fun add_arities_table pp algebra =
+ Symtab.fold (fn (t, ars) => insert_ars pp algebra (t, map snd ars));
end;
+(* classrel *)
+
+fun rebuild_arities pp algebra = algebra |> map_arities (fn arities =>
+ Symtab.empty
+ |> add_arities_table pp algebra arities);
+
+fun add_classrel pp rel = rebuild_arities pp o map_classes (fn classes =>
+ classes |> Graph.add_edge_trans_acyclic rel
+ handle Graph.CYCLES css => err_cyclic_classes pp css);
+
+
+(* empty and merge *)
+
+val empty_algebra = make_algebra (Graph.empty, Symtab.empty);
+
+fun merge_algebra pp
+ (Algebra {classes = classes1, arities = arities1},
+ Algebra {classes = classes2, arities = arities2}) =
+ let
+ val classes' = Graph.merge_trans_acyclic (op =) (classes1, classes2)
+ handle Graph.DUPS cs => err_dup_classes cs
+ | Graph.CYCLES css => err_cyclic_classes pp css;
+ val algebra0 = make_algebra (classes', Symtab.empty);
+ val arities' = Symtab.empty
+ |> add_arities_table pp algebra0 arities1
+ |> add_arities_table pp algebra0 arities2;
+ in make_algebra (classes', arities') end;
+
+
(** sorts of types **)
@@ -261,13 +296,14 @@
(* mg_domain *)
-fun mg_domain (classes, arities) a S =
+fun mg_domain algebra a S =
let
+ val arities = arities_of algebra;
fun dom c =
(case AList.lookup (op =) (Symtab.lookup_list arities a) c of
NONE => raise CLASS_ERROR (NoArity (a, c))
| SOME (_, Ss) => Ss);
- fun dom_inter c Ss = ListPair.map (inter_sort classes) (dom c, Ss);
+ fun dom_inter c Ss = ListPair.map (inter_sort algebra) (dom c, Ss);
in
(case S of
[] => raise Fail "Unknown domain of empty intersection"
@@ -277,13 +313,13 @@
(* of_sort *)
-fun of_sort (classes, arities) =
+fun of_sort algebra =
let
fun ofS (_, []) = true
- | ofS (TFree (_, S), S') = sort_le classes (S, S')
- | ofS (TVar (_, S), S') = sort_le classes (S, S')
+ | ofS (TFree (_, S), S') = sort_le algebra (S, S')
+ | ofS (TVar (_, S), S') = sort_le algebra (S, S')
| ofS (Type (a, Ts), S) =
- let val Ss = mg_domain (classes, arities) a S in
+ let val Ss = mg_domain algebra a S in
ListPair.all ofS (Ts, Ss)
end handle CLASS_ERROR _ => false;
in ofS end;
@@ -291,8 +327,9 @@
(* of_sort_derivation *)
-fun of_sort_derivation pp (classes, arities) {classrel, constructor, variable} =
+fun of_sort_derivation pp algebra {classrel, constructor, variable} =
let
+ val {classes, arities} = rep_algebra algebra;
fun weaken_path (x, c1 :: c2 :: cs) = weaken_path (classrel (x, c1) c2, c2 :: cs)
| weaken_path (x, _) = x;
fun weaken (x, c1) c2 =
@@ -301,7 +338,7 @@
| cs :: _ => weaken_path (x, cs));
fun weakens S1 S2 = S2 |> map (fn c2 =>
- (case S1 |> find_first (fn (_, c1) => class_le classes (c1, c2)) of
+ (case S1 |> find_first (fn (_, c1) => class_le algebra (c1, c2)) of
SOME d1 => weaken d1 c2
| NONE => error ("Cannot derive subsort relation " ^
Pretty.string_of_sort pp (map #2 S1) ^ " < " ^ Pretty.string_of_sort pp S2)));
@@ -309,7 +346,7 @@
fun derive _ [] = []
| derive (Type (a, Ts)) S =
let
- val Ss = mg_domain (classes, arities) a S;
+ val Ss = mg_domain algebra a S;
val dom = map2 (fn T => fn S => derive T S ~~ S) Ts Ss;
in
S |> map (fn c =>
@@ -324,12 +361,12 @@
(* witness_sorts *)
-fun witness_sorts (classes, arities) types hyps sorts =
+fun witness_sorts algebra types hyps sorts =
let
- fun le S1 S2 = sort_le classes (S1, S2);
+ fun le S1 S2 = sort_le algebra (S1, S2);
fun get_solved S2 (T, S1) = if le S1 S2 then SOME (T, S2) else NONE;
fun get_hyp S2 S1 = if le S1 S2 then SOME (TFree ("'hyp", S1), S2) else NONE;
- fun mg_dom t S = SOME (mg_domain (classes, arities) t S) handle CLASS_ERROR _ => NONE;
+ fun mg_dom t S = SOME (mg_domain algebra t S) handle CLASS_ERROR _ => NONE;
fun witn_sort _ [] solved_failed = (SOME (propT, []), solved_failed)
| witn_sort path S (solved, failed) =