src/Pure/sorts.ML
author wenzelm
Sat, 21 Jan 2006 23:02:14 +0100
changeset 18728 6790126ab5f6
parent 18428 4059413acbc1
child 18931 427df66052a1
permissions -rw-r--r--
simplified type attribute;
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
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
     5
Type classes and sorts.
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
     6
*)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
     7
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
     8
signature SORTS =
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
     9
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
    10
  (*operations on ordered lists*)
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    11
  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
    12
  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
    13
  val subtract: 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
    14
  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
    15
  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
    16
  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
    17
  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
    18
  val insert_terms: term 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
    19
  (*signature information*)
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    20
  type classes
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    21
  type arities
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    22
  val class_eq: classes -> class * class -> bool
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    23
  val class_less: classes -> class * class -> bool
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    24
  val class_le: classes -> class * class -> bool
17155
e904580c3ee0 added superclasses, class_le_path
haftmann
parents: 16881
diff changeset
    25
  val class_le_path: classes -> class * class -> class list option
e904580c3ee0 added superclasses, class_le_path
haftmann
parents: 16881
diff changeset
    26
  val superclasses: classes -> class -> class list
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    27
  val sort_eq: classes -> sort * sort -> bool
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    28
  val sort_less: classes -> sort * sort -> bool
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    29
  val sort_le: classes -> sort * sort -> bool
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    30
  val sorts_le: classes -> sort list * sort list -> bool
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    31
  val inter_sort: classes -> sort * sort -> sort
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    32
  val norm_sort: classes -> sort -> sort
14986
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
    33
  val certify_class: classes -> class -> class
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
    34
  val certify_sort: classes -> sort -> sort
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    35
  val of_sort: classes * arities -> typ * sort -> bool
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
    36
  exception DOMAIN of string * class
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    37
  val mg_domain: classes * arities -> string -> sort -> sort list  (*exception DOMAIN*)
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14782
diff changeset
    38
  val witness_sorts: classes * arities -> string list ->
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14782
diff changeset
    39
    sort list -> sort list -> (typ * sort) list
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    40
end;
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    41
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    42
structure Sorts: SORTS =
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    43
struct
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    44
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    45
(** type classes and sorts **)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    46
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    47
(*
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    48
  Classes denote (possibly empty) collections of types that are
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    49
  partially ordered by class inclusion. They are represented
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    50
  symbolically by strings.
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    51
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    52
  Sorts are intersections of finitely many classes. They are
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    53
  represented by lists of classes.  Normal forms of sorts are sorted
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    54
  lists of minimal classes (wrt. current class inclusion).
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    55
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    56
  (types already defined in Pure/term.ML)
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    57
*)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    58
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    59
16598
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    60
(* ordered lists of sorts *)
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    61
16598
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    62
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
    63
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
    64
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
    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 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
    67
16598
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    68
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
    69
  | 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
    70
  | 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
    71
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
    72
  | 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
    73
16598
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    74
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
    75
  | 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
    76
  | 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
    77
  | 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
    78
  | 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
    79
  | 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
    80
16598
59381032be14 removed obsolete eq_sort, mem_sort, subset_sort, eq_set_sort, ins_sort, union_sort, rems_sort;
wenzelm
parents: 16400
diff changeset
    81
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
    82
  | 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
    83
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    84
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    85
(* sort signature information *)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    86
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    87
(*
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    88
  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
    89
    subclass relation, which needs to be transitive and acyclic.
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    90
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    91
  arities: table of association lists of all type arities; (t, ars)
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    92
    means that type constructor t has the arities ars; an element (c,
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    93
    Ss) of ars represents the arity t::(Ss)c.  "Coregularity" of the
14870
c5cf7c001313 tuned comments;
wenzelm
parents: 14828
diff changeset
    94
    arities structure requires that for any two declarations
c5cf7c001313 tuned comments;
wenzelm
parents: 14828
diff changeset
    95
    t::(Ss1)c1 and t::(Ss2)c2 such that c1 <= c2 holds Ss1 <= Ss2.
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    96
*)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
    97
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    98
type classes = stamp Graph.T;
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
    99
type arities = (class * sort list) list Symtab.table;
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   100
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   101
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   102
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   103
(** equality and inclusion **)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   104
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   105
(* classes *)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   106
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   107
fun class_eq (_: classes) (c1, c2:class) = c1 = c2;
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   108
val class_less: classes -> class * class -> bool = Graph.is_edge;
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   109
fun class_le classes (c1, c2) = c1 = c2 orelse class_less classes (c1, c2);
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   110
17155
e904580c3ee0 added superclasses, class_le_path
haftmann
parents: 16881
diff changeset
   111
fun class_le_path classes (subclass, superclass) =
e904580c3ee0 added superclasses, class_le_path
haftmann
parents: 16881
diff changeset
   112
  if class_eq classes (subclass, superclass)
e904580c3ee0 added superclasses, class_le_path
haftmann
parents: 16881
diff changeset
   113
  then SOME [subclass]
e904580c3ee0 added superclasses, class_le_path
haftmann
parents: 16881
diff changeset
   114
  else case Graph.find_paths classes (subclass, superclass)
e904580c3ee0 added superclasses, class_le_path
haftmann
parents: 16881
diff changeset
   115
    of [] => NONE
e904580c3ee0 added superclasses, class_le_path
haftmann
parents: 16881
diff changeset
   116
     | (p::_) => SOME p
e904580c3ee0 added superclasses, class_le_path
haftmann
parents: 16881
diff changeset
   117
e904580c3ee0 added superclasses, class_le_path
haftmann
parents: 16881
diff changeset
   118
val superclasses = Graph.imm_succs
e904580c3ee0 added superclasses, class_le_path
haftmann
parents: 16881
diff changeset
   119
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   120
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   121
(* sorts *)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   122
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   123
fun sort_le classes (S1, S2) =
17155
e904580c3ee0 added superclasses, class_le_path
haftmann
parents: 16881
diff changeset
   124
  forall (fn c2 => exists (fn c1 => class_le classes (c1, c2)) S1) S2;
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   125
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   126
fun sorts_le classes (Ss1, Ss2) =
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   127
  ListPair.all (sort_le classes) (Ss1, Ss2);
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   128
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   129
fun sort_eq classes (S1, S2) =
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   130
  sort_le classes (S1, S2) andalso sort_le classes (S2, S1);
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   131
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   132
fun sort_less classes (S1, S2) =
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   133
  sort_le classes (S1, S2) andalso not (sort_le classes (S2, S1));
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   134
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   135
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   136
(* normal forms of sorts *)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   137
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   138
fun minimal_class classes S c =
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   139
  not (exists (fn c' => class_less classes (c', c)) S);
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   140
14986
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
   141
fun norm_sort _ [] = []
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
   142
  | norm_sort _ (S as [_]) = S
18428
4059413acbc1 sort_distinct;
wenzelm
parents: 17756
diff changeset
   143
  | norm_sort classes S = sort_distinct string_ord (filter (minimal_class classes S) S);
14986
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
   144
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
   145
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
   146
(* certify *)
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
   147
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
   148
fun certify_class classes c =
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
   149
  if can (Graph.get_node classes) c then c
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
   150
  else raise TYPE ("Undeclared class: " ^ quote c, [], []);
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
   151
c97190ae13bd added certify_class/sort;
wenzelm
parents: 14870
diff changeset
   152
fun certify_sort classes = norm_sort classes o map (certify_class classes);
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   153
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   154
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   155
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   156
(** intersection **)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   157
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   158
(*intersect class with sort (preserves minimality)*)
16881
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   159
fun inter_class classes c S =
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   160
  let
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   161
    fun intr [] = [c]
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   162
      | intr (S' as c' :: c's) =
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   163
          if class_le classes (c', c) then S'
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   164
          else if class_le classes (c, c') then intr c's
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   165
          else c' :: intr c's
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   166
  in intr S end;
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   167
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   168
(*instersect sorts (preserves minimality)*)
16881
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   169
fun inter_sort classes (S1, S2) =
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   170
  sort_strings (fold (inter_class classes) S1 S2);
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   171
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   172
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   173
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   174
(** sorts of types **)
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   175
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   176
(* mg_domain *)
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   177
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   178
exception DOMAIN of string * class;
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   179
16881
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   180
fun mg_domain (classes, arities) a S =
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   181
  let
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   182
    fun dom c =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   183
      (case AList.lookup (op =) (Symtab.lookup_multi arities a) c of
16881
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   184
        NONE => raise DOMAIN (a, c)
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   185
      | SOME Ss => Ss);
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   186
    fun dom_inter c Ss = ListPair.map (inter_sort classes) (dom c, Ss);
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   187
  in
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   188
    (case S of
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   189
      [] => sys_error "mg_domain"  (*don't know number of args!*)
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   190
    | c :: cs => fold dom_inter cs (dom c))
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   191
  end;
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   192
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   193
2990
271062b8c461 removed least_sort;
wenzelm
parents: 2956
diff changeset
   194
(* of_sort *)
271062b8c461 removed least_sort;
wenzelm
parents: 2956
diff changeset
   195
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   196
fun of_sort (classes, arities) =
2990
271062b8c461 removed least_sort;
wenzelm
parents: 2956
diff changeset
   197
  let
271062b8c461 removed least_sort;
wenzelm
parents: 2956
diff changeset
   198
    fun ofS (_, []) = true
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   199
      | ofS (TFree (_, S), S') = sort_le classes (S, S')
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   200
      | ofS (TVar (_, S), S') = sort_le classes (S, S')
2990
271062b8c461 removed least_sort;
wenzelm
parents: 2956
diff changeset
   201
      | ofS (Type (a, Ts), S) =
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   202
          let val Ss = mg_domain (classes, arities) a S in
2990
271062b8c461 removed least_sort;
wenzelm
parents: 2956
diff changeset
   203
            ListPair.all ofS (Ts, Ss)
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   204
          end handle DOMAIN _ => false;
2990
271062b8c461 removed least_sort;
wenzelm
parents: 2956
diff changeset
   205
  in ofS end;
271062b8c461 removed least_sort;
wenzelm
parents: 2956
diff changeset
   206
271062b8c461 removed least_sort;
wenzelm
parents: 2956
diff changeset
   207
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   208
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   209
(** witness_sorts **)
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   210
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14782
diff changeset
   211
local
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14782
diff changeset
   212
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14782
diff changeset
   213
fun witness_aux (classes, arities) log_types hyps sorts =
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   214
  let
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   215
    val top_witn = (propT, []);
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   216
    fun le S1 S2 = sort_le classes (S1, S2);
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   217
    fun get_solved S2 (T, S1) = if le S1 S2 then SOME (T, S2) else NONE;
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   218
    fun get_hyp S2 S1 = if le S1 S2 then SOME (TFree ("'hyp", S1), S2) else NONE;
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   219
    fun mg_dom t S = SOME (mg_domain (classes, arities) t S) handle DOMAIN _ => NONE;
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   220
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   221
    fun witn_sort _ (solved_failed, []) = (solved_failed, SOME top_witn)
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   222
      | witn_sort path ((solved, failed), S) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   223
          if exists (le S) failed then ((solved, failed), NONE)
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   224
          else
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   225
            (case get_first (get_solved S) solved of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   226
              SOME w => ((solved, failed), SOME w)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   227
            | NONE =>
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   228
                (case get_first (get_hyp S) hyps of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   229
                  SOME w => ((w :: solved, failed), SOME w)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   230
                | NONE => witn_types path log_types ((solved, failed), S)))
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   231
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   232
    and witn_sorts path x = foldl_map (witn_sort path) x
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   233
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   234
    and witn_types _ [] ((solved, failed), S) = ((solved, S :: failed), NONE)
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   235
      | witn_types path (t :: ts) (solved_failed, S) =
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   236
          (case mg_dom t S of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   237
            SOME SS =>
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   238
              (*do not descend into stronger args (achieving termination)*)
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   239
              if exists (fn D => le D S orelse exists (le D) path) SS then
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   240
                witn_types path ts (solved_failed, S)
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   241
              else
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   242
                let val ((solved', failed'), ws) = witn_sorts (S :: path) (solved_failed, SS) in
17756
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17412
diff changeset
   243
                  if forall is_some ws then
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   244
                    let val w = (Type (t, map (#1 o valOf) ws), S)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   245
                    in ((w :: solved', failed'), SOME w) end
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   246
                  else witn_types path ts ((solved', failed'), S)
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   247
                end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   248
          | NONE => witn_types path ts (solved_failed, S));
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   249
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   250
  in witn_sorts [] (([], []), sorts) end;
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   251
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14782
diff changeset
   252
fun str_of_sort [c] = c
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14782
diff changeset
   253
  | str_of_sort cs = enclose "{" "}" (commas cs);
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14782
diff changeset
   254
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14782
diff changeset
   255
in
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   256
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   257
fun witness_sorts (classes, arities) log_types hyps sorts =
7643
59f8feff9766 mg_domain: exception DOMAIN;
wenzelm
parents: 7067
diff changeset
   258
  let
16881
570592642670 tuned norm_sort, mg_domain;
wenzelm
parents: 16598
diff changeset
   259
    (*double check result of witness construction*)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   260
    fun check_result NONE = NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   261
      | check_result (SOME (T, S)) =
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14986
diff changeset
   262
          if of_sort (classes, arities) (T, S) then SOME (T, S)
14782
d6ce35a1c386 incorporate sort ops from term.ML; use Graph.T; misc cleanup;
wenzelm
parents: 9281
diff changeset
   263
          else sys_error ("Sorts.witness_sorts: bad witness for sort " ^ str_of_sort S);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   264
  in List.mapPartial check_result (#2 (witness_aux (classes, arities) log_types hyps sorts)) end;
2956
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   265
d128ae3e7421 Type classes and sorts (isolated from type.ML).
wenzelm
parents:
diff changeset
   266
end;
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14782
diff changeset
   267
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14782
diff changeset
   268
end;