src/Pure/sorts.ML
author wenzelm
Mon, 03 Jul 2006 19:33:07 +0200
changeset 19977 ac1b062c81ac
parent 19952 eaf2c25654d3
child 20391 d079804d3b82
permissions -rw-r--r--
project_algebra: norm_sort; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/sorts.ML
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
     4
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
     5
The order-sorted algebra of type classes.
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
     6
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
     7
Classes denote (possibly empty) collections of types that are
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
     8
partially ordered by class inclusion. They are represented
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
     9
symbolically by strings.
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
    10
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
    11
Sorts are intersections of finitely many classes. They are represented
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
    12
by lists of classes.  Normal forms of sorts are sorted lists of
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
    13
minimal classes (wrt. current class inclusion).
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    14
*)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    15
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    16
signature SORTS =
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    17
sig
16598
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    18
  val eq_set: sort list * sort list -> bool
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    19
  val union: sort list -> sort list -> sort list
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    20
  val subtract: sort list -> sort list -> sort list
19463
6cb10eea48c3 added remove_sort;
wenzelm
parents: 19408
diff changeset
    21
  val remove_sort: sort -> sort list -> sort list
16598
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    22
  val insert_sort: sort -> sort list -> sort list
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    23
  val insert_typ: typ -> sort list -> sort list
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    24
  val insert_typs: typ list -> sort list -> sort list
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    25
  val insert_term: term -> sort list -> sort list
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    26
  val insert_terms: term list -> sort list -> sort list
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    27
  type algebra
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    28
  val rep_algebra: algebra ->
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    29
   {classes: stamp Graph.T,
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    30
    arities: (class * (class * sort list)) list Symtab.table}
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    31
  val classes: algebra -> class list
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    32
  val super_classes: algebra -> class -> class list
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    33
  val class_less: algebra -> class * class -> bool
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    34
  val class_le: algebra -> class * class -> bool
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    35
  val sort_eq: algebra -> sort * sort -> bool
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    36
  val sort_le: algebra -> sort * sort -> bool
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    37
  val sorts_le: algebra -> sort list * sort list -> bool
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    38
  val inter_sort: algebra -> sort * sort -> sort
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    39
  val certify_class: algebra -> class -> class    (*exception TYPE*)
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    40
  val certify_sort: algebra -> sort -> sort       (*exception TYPE*)
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    41
  val add_class: Pretty.pp -> class * class list -> algebra -> algebra
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    42
  val add_classrel: Pretty.pp -> class * class -> algebra -> algebra
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    43
  val add_arities: Pretty.pp -> string * (class * sort list) list -> algebra -> algebra
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    44
  val empty_algebra: algebra
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    45
  val merge_algebra: Pretty.pp -> algebra * algebra -> algebra
19952
eaf2c25654d3 added class projection
haftmann
parents: 19645
diff changeset
    46
  val project_algebra: Pretty.pp -> (class -> bool) -> algebra -> algebra
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
    47
  type class_error
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
    48
  val class_error: Pretty.pp -> class_error -> 'a
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
    49
  exception CLASS_ERROR of class_error
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    50
  val mg_domain: algebra -> string -> sort -> sort list   (*exception CLASS_ERROR*)
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    51
  val of_sort: algebra -> typ * sort -> bool
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    52
  val of_sort_derivation: Pretty.pp -> algebra ->
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
    53
    {classrel: 'a * class -> class -> 'a,
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
    54
     constructor: string -> ('a * class) list list -> class -> 'a,
19584
wenzelm
parents: 19578
diff changeset
    55
     variable: typ -> ('a * class) list} ->
wenzelm
parents: 19578
diff changeset
    56
    typ * sort -> 'a list   (*exception CLASS_ERROR*)
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
    57
  val witness_sorts: algebra -> string list -> sort list -> sort list -> (typ * sort) list
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    58
end;
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    59
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    60
structure Sorts: SORTS =
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    61
struct
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    62
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
    63
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
    64
(** ordered lists of sorts **)
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    65
16598
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    66
val eq_set = OrdList.eq_set 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
    67
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
    68
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
    69
19463
6cb10eea48c3 added remove_sort;
wenzelm
parents: 19408
diff changeset
    70
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
    71
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
    72
16598
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    73
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
    74
  | 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
    75
  | 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
    76
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
    77
  | 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
    78
16598
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    79
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
    80
  | 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
    81
  | 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
    82
  | 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
    83
  | 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
    84
  | 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
    85
16598
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    86
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
    87
  | 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
    88
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    89
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
    90
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
    91
(** order-sorted algebra **)
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    92
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    93
(*
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    94
  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
    95
    subclass relation, which needs to be transitive and acyclic.
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    96
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    97
  arities: table of association lists of all type arities; (t, ars)
19531
wenzelm
parents: 19529
diff changeset
    98
    means that type constructor t has the arities ars; an element
wenzelm
parents: 19529
diff changeset
    99
    (c, (c0, Ss)) of ars represents the arity t::(Ss)c being derived
wenzelm
parents: 19529
diff changeset
   100
    via c0 <= c.  "Coregularity" of the arities structure requires
wenzelm
parents: 19529
diff changeset
   101
    that for any two declarations t::(Ss1)c1 and t::(Ss2)c2 such that
wenzelm
parents: 19529
diff changeset
   102
    c1 <= c2 holds Ss1 <= Ss2.
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   103
*)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   104
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   105
datatype algebra = Algebra of
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   106
 {classes: stamp Graph.T,
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   107
  arities: (class * (class * sort list)) list Symtab.table};
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   108
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   109
fun rep_algebra (Algebra args) = args;
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   110
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   111
val classes_of = #classes o rep_algebra;
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   112
val arities_of = #arities o rep_algebra;
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   113
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   114
fun make_algebra (classes, arities) =
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   115
  Algebra {classes = classes, arities = arities};
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   116
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   117
fun map_classes f (Algebra {classes, arities}) = make_algebra (f classes, arities);
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   118
fun map_arities f (Algebra {classes, arities}) = make_algebra (classes, f arities);
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   119
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   120
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   121
(* classes *)
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   122
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   123
val classes = Graph.keys o classes_of;
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   124
val super_classes = Graph.imm_succs o classes_of;
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   125
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   126
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   127
(* class relations *)
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   128
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   129
val class_less = Graph.is_edge o classes_of;
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   130
fun class_le algebra (c1, c2) = c1 = c2 orelse class_less algebra (c1, c2);
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   131
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   132
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   133
(* sort relations *)
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   134
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   135
fun sort_le algebra (S1, S2) =
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   136
  forall (fn c2 => exists (fn c1 => class_le algebra (c1, c2)) S1) S2;
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   137
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   138
fun sorts_le algebra (Ss1, Ss2) =
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   139
  ListPair.all (sort_le algebra) (Ss1, Ss2);
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   140
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   141
fun sort_eq algebra (S1, S2) =
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   142
  sort_le algebra (S1, S2) andalso sort_le algebra (S2, S1);
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   143
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   144
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   145
(* intersection *)
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   146
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   147
fun inter_class algebra c S =
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   148
  let
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   149
    fun intr [] = [c]
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   150
      | intr (S' as c' :: c's) =
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   151
          if class_le algebra (c', c) then S'
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   152
          else if class_le algebra (c, c') then intr c's
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   153
          else c' :: intr c's
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   154
  in intr S end;
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   155
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   156
fun inter_sort algebra (S1, S2) =
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   157
  sort_strings (fold (inter_class algebra) S1 S2);
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   158
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   159
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   160
(* normal form *)
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   161
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   162
fun norm_sort _ [] = []
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   163
  | norm_sort _ (S as [_]) = S
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   164
  | norm_sort algebra S =
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   165
      filter (fn c => not (exists (fn c' => class_less algebra (c', c)) S)) S
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   166
      |> sort_distinct string_ord;
2990
271062b8c461 removed least_sort;
wenzelm
parents: 2956
diff changeset
   167
271062b8c461 removed least_sort;
wenzelm
parents: 2956
diff changeset
   168
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   169
(* certify *)
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   170
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   171
fun certify_class algebra c =
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   172
  if can (Graph.get_node (classes_of algebra)) c then c
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   173
  else raise TYPE ("Undeclared class: " ^ quote c, [], []);
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   174
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   175
fun certify_sort classes = norm_sort classes o map (certify_class classes);
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   176
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   177
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   178
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   179
(** build algebras **)
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   180
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   181
(* classes *)
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   182
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   183
fun err_dup_classes cs =
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   184
  error ("Duplicate declaration of class(es): " ^ commas_quote cs);
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   185
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   186
fun err_cyclic_classes pp css =
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   187
  error (cat_lines (map (fn cs =>
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   188
    "Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css));
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   189
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   190
fun add_class pp (c, cs) = map_classes (fn classes =>
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   191
  let
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   192
    val classes' = classes |> Graph.new_node (c, stamp ())
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   193
      handle Graph.DUP dup => err_dup_classes [dup];
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   194
    val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs)
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   195
      handle Graph.CYCLES css => err_cyclic_classes pp css;
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   196
  in classes'' end);
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   197
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   198
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   199
(* arities *)
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   200
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   201
local
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   202
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   203
fun for_classes _ NONE = ""
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   204
  | for_classes pp (SOME (c1, c2)) =
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   205
      " for classes " ^ Pretty.string_of_classrel pp [c1, c2];
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   206
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   207
fun err_conflict pp t cc (c, Ss) (c', Ss') =
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   208
  error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n  " ^
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   209
    Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n  " ^
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   210
    Pretty.string_of_arity pp (t, Ss', [c']));
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   211
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   212
fun coregular pp algebra t (c, (c0, Ss)) ars =
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   213
  let
19524
f795c1164708 arities: maintain original codomain;
wenzelm
parents: 19514
diff changeset
   214
    fun conflict (c', (_, Ss')) =
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   215
      if class_le algebra (c, c') andalso not (sorts_le algebra (Ss, Ss')) then
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   216
        SOME ((c, c'), (c', Ss'))
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   217
      else if class_le algebra (c', c) andalso not (sorts_le algebra (Ss', Ss)) then
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   218
        SOME ((c', c), (c', Ss'))
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   219
      else NONE;
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   220
  in
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   221
    (case get_first conflict ars of
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   222
      SOME ((c1, c2), (c', Ss')) => err_conflict pp t (SOME (c1, c2)) (c, Ss) (c', Ss')
19524
f795c1164708 arities: maintain original codomain;
wenzelm
parents: 19514
diff changeset
   223
    | NONE => (c, (c0, Ss)) :: ars)
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   224
  end;
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   225
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   226
fun complete algebra (c0, Ss) = map (rpair (c0, Ss)) (c0 :: super_classes algebra c0);
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   227
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   228
fun insert pp algebra t (c, (c0, Ss)) ars =
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   229
  (case AList.lookup (op =) ars c of
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   230
    NONE => coregular pp algebra t (c, (c0, Ss)) ars
19524
f795c1164708 arities: maintain original codomain;
wenzelm
parents: 19514
diff changeset
   231
  | SOME (_, Ss') =>
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   232
      if sorts_le algebra (Ss, Ss') then ars
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   233
      else if sorts_le algebra (Ss', Ss) then
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   234
        coregular pp algebra t (c, (c0, Ss))
19524
f795c1164708 arities: maintain original codomain;
wenzelm
parents: 19514
diff changeset
   235
          (filter_out (fn (c'', (_, Ss'')) => c = c'' andalso Ss'' = Ss') ars)
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   236
      else err_conflict pp t NONE (c, Ss) (c, Ss'));
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   237
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   238
fun insert_ars pp algebra (t, ars) arities =
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   239
  let val ars' =
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   240
    Symtab.lookup_list arities t
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   241
    |> fold_rev (fold_rev (insert pp algebra t)) (map (complete algebra) ars)
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   242
  in Symtab.update (t, ars') arities end;
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   243
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   244
in
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   245
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   246
fun add_arities pp arg algebra = algebra |> map_arities (insert_ars pp algebra arg);
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   247
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   248
fun add_arities_table pp algebra =
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   249
  Symtab.fold (fn (t, ars) => insert_ars pp algebra (t, map snd ars));
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   250
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   251
end;
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   252
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   253
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   254
(* classrel *)
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   255
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   256
fun rebuild_arities pp algebra = algebra |> map_arities (fn arities =>
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   257
  Symtab.empty
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   258
  |> add_arities_table pp algebra arities);
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   259
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   260
fun add_classrel pp rel = rebuild_arities pp o map_classes (fn classes =>
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   261
  classes |> Graph.add_edge_trans_acyclic rel
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   262
    handle Graph.CYCLES css => err_cyclic_classes pp css);
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   263
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   264
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   265
(* empty and merge *)
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   266
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   267
val empty_algebra = make_algebra (Graph.empty, Symtab.empty);
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   268
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   269
fun merge_algebra pp
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   270
   (Algebra {classes = classes1, arities = arities1},
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   271
    Algebra {classes = classes2, arities = arities2}) =
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   272
  let
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   273
    val classes' = Graph.merge_trans_acyclic (op =) (classes1, classes2)
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   274
      handle Graph.DUPS cs => err_dup_classes cs
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   275
          | Graph.CYCLES css => err_cyclic_classes pp css;
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   276
    val algebra0 = make_algebra (classes', Symtab.empty);
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   277
    val arities' = Symtab.empty
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   278
      |> add_arities_table pp algebra0 arities1
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   279
      |> add_arities_table pp algebra0 arities2;
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   280
  in make_algebra (classes', arities') end;
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   281
19977
ac1b062c81ac project_algebra: norm_sort;
wenzelm
parents: 19952
diff changeset
   282
fun project_algebra pp proj (algebra as Algebra {classes, arities}) =
19952
eaf2c25654d3 added class projection
haftmann
parents: 19645
diff changeset
   283
  let
19977
ac1b062c81ac project_algebra: norm_sort;
wenzelm
parents: 19952
diff changeset
   284
    val proj_sort = norm_sort algebra o filter proj o Graph.all_succs classes;
ac1b062c81ac project_algebra: norm_sort;
wenzelm
parents: 19952
diff changeset
   285
    fun proj_arity (c, (_, Ss)) =
ac1b062c81ac project_algebra: norm_sort;
wenzelm
parents: 19952
diff changeset
   286
      if proj c then SOME (c, (c, map proj_sort Ss)) else NONE;
ac1b062c81ac project_algebra: norm_sort;
wenzelm
parents: 19952
diff changeset
   287
    val classes' = classes |> Graph.project proj;
ac1b062c81ac project_algebra: norm_sort;
wenzelm
parents: 19952
diff changeset
   288
    val arities' = arities |> (Symtab.map o map_filter) proj_arity;
ac1b062c81ac project_algebra: norm_sort;
wenzelm
parents: 19952
diff changeset
   289
  in rebuild_arities pp (make_algebra (classes', arities')) end;
19952
eaf2c25654d3 added class projection
haftmann
parents: 19645
diff changeset
   290
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   291
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   292
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   293
(** sorts of types **)
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   294
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   295
(* errors *)
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   296
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   297
datatype class_error = NoClassrel of class * class | NoArity of string * class;
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   298
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   299
fun class_error pp (NoClassrel (c1, c2)) =
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   300
      error ("No class relation " ^ Pretty.string_of_classrel pp [c1, c2])
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   301
  | class_error pp (NoArity (a, c)) =
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   302
      error ("No type arity " ^ Pretty.string_of_arity pp (a, [], [c]));
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   303
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   304
exception CLASS_ERROR of class_error;
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   305
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   306
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   307
(* mg_domain *)
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   308
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   309
fun mg_domain algebra a S =
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   310
  let
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   311
    val arities = arities_of algebra;
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   312
    fun dom c =
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   313
      (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
   314
        NONE => raise CLASS_ERROR (NoArity (a, c))
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   315
      | SOME (_, Ss) => Ss);
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   316
    fun dom_inter c Ss = ListPair.map (inter_sort algebra) (dom c, Ss);
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   317
  in
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   318
    (case S of
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   319
      [] => raise Fail "Unknown domain of empty intersection"
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   320
    | c :: cs => fold dom_inter cs (dom c))
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   321
  end;
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   322
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   323
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   324
(* of_sort *)
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   325
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   326
fun of_sort algebra =
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   327
  let
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   328
    fun ofS (_, []) = true
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   329
      | ofS (TFree (_, S), S') = sort_le algebra (S, S')
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   330
      | ofS (TVar (_, S), S') = sort_le algebra (S, S')
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   331
      | ofS (Type (a, Ts), S) =
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   332
          let val Ss = mg_domain algebra a S in
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   333
            ListPair.all ofS (Ts, Ss)
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   334
          end handle CLASS_ERROR _ => false;
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   335
  in ofS end;
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   336
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   337
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   338
(* of_sort_derivation *)
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   339
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   340
fun of_sort_derivation pp algebra {classrel, constructor, variable} =
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   341
  let
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   342
    val {classes, arities} = rep_algebra algebra;
19952
eaf2c25654d3 added class projection
haftmann
parents: 19645
diff changeset
   343
    fun weaken_path (x, c1 :: c2 :: cs) =
eaf2c25654d3 added class projection
haftmann
parents: 19645
diff changeset
   344
          weaken_path (classrel (x, c1) c2, c2 :: cs)
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   345
      | weaken_path (x, _) = x;
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   346
    fun weaken (x, c1) c2 =
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   347
      (case Graph.irreducible_paths classes (c1, c2) of
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   348
        [] => raise CLASS_ERROR (NoClassrel (c1, c2))
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   349
      | cs :: _ => weaken_path (x, cs));
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   350
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   351
    fun weakens S1 S2 = S2 |> map (fn c2 =>
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   352
      (case S1 |> find_first (fn (_, c1) => class_le algebra (c1, c2)) of
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   353
        SOME d1 => weaken d1 c2
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   354
      | NONE => error ("Cannot derive subsort relation " ^
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   355
          Pretty.string_of_sort pp (map #2 S1) ^ " < " ^ Pretty.string_of_sort pp S2)));
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   356
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   357
    fun derive _ [] = []
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   358
      | derive (Type (a, Ts)) S =
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   359
          let
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   360
            val Ss = mg_domain algebra a S;
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   361
            val dom = map2 (fn T => fn S => derive T S ~~ S) Ts Ss;
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   362
          in
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   363
            S |> map (fn c =>
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   364
              let
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   365
                val (c0, Ss') = the (AList.lookup (op =) (Symtab.lookup_list arities a) c);
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   366
                val dom' = map2 (fn d => fn S' => weakens d S' ~~ S') dom Ss';
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   367
              in weaken (constructor a dom' c0, c0) c end)
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   368
          end
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   369
      | derive T S = weakens (variable T) S;
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   370
  in uncurry derive end;
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   371
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   372
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   373
(* witness_sorts *)
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   374
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   375
fun witness_sorts algebra types hyps sorts =
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   376
  let
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   377
    fun le S1 S2 = sort_le algebra (S1, S2);
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   378
    fun get_solved S2 (T, S1) = if le S1 S2 then SOME (T, S2) else NONE;
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   379
    fun get_hyp S2 S1 = if le S1 S2 then SOME (TFree ("'hyp", S1), S2) else NONE;
19645
bbda28f2d379 abstract interfaces for type algebra;
wenzelm
parents: 19584
diff changeset
   380
    fun mg_dom t S = SOME (mg_domain algebra t S) handle CLASS_ERROR _ => NONE;
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   381
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   382
    fun witn_sort _ [] solved_failed = (SOME (propT, []), solved_failed)
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   383
      | witn_sort path S (solved, failed) =
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   384
          if exists (le S) failed then (NONE, (solved, failed))
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   385
          else
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   386
            (case get_first (get_solved S) solved of
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   387
              SOME w => (SOME w, (solved, failed))
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   388
            | NONE =>
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   389
                (case get_first (get_hyp S) hyps of
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   390
                  SOME w => (SOME w, (w :: solved, failed))
19584
wenzelm
parents: 19578
diff changeset
   391
                | NONE => witn_types path types S (solved, failed)))
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   392
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   393
    and witn_sorts path x = fold_map (witn_sort path) x
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   394
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   395
    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
   396
      | witn_types path (t :: ts) S solved_failed =
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   397
          (case mg_dom t S of
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   398
            SOME SS =>
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   399
              (*do not descend into stronger args (achieving termination)*)
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   400
              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
   401
                witn_types path ts S solved_failed
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   402
              else
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   403
                let val (ws, (solved', failed')) = witn_sorts (S :: path) SS solved_failed in
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   404
                  if forall is_some ws then
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   405
                    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
   406
                    in (SOME w, (w :: solved', failed')) end
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   407
                  else witn_types path ts S (solved', failed')
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   408
                end
19578
f93b7637a5e6 added class_error and exception CLASS_ERROR (supercedes DOMAIN);
wenzelm
parents: 19531
diff changeset
   409
          | NONE => witn_types path ts S solved_failed);
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   410
19584
wenzelm
parents: 19578
diff changeset
   411
  in map_filter I (#1 (witn_sorts [] sorts ([], []))) end;
19529
690861f93d2b added domain_error;
wenzelm
parents: 19524
diff changeset
   412
19514
1f0218dab849 moved certify_class/sort to type.ML;
wenzelm
parents: 19482
diff changeset
   413
end;