src/Pure/sorts.ML
author wenzelm
Tue Apr 11 16:00:05 2006 +0200 (2006-04-11)
changeset 19408 9a52d5b7fc27
parent 19393 78d6b7a01b12
child 19463 6cb10eea48c3
permissions -rw-r--r--
removed superclasses (see sign.ML);
wenzelm@2956
     1
(*  Title:      Pure/sorts.ML
wenzelm@2956
     2
    ID:         $Id$
wenzelm@2956
     3
    Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
wenzelm@2956
     4
wenzelm@2956
     5
Type classes and sorts.
wenzelm@2956
     6
*)
wenzelm@2956
     7
wenzelm@2956
     8
signature SORTS =
wenzelm@2956
     9
sig
wenzelm@16598
    10
  (*operations on ordered lists*)
wenzelm@16598
    11
  val eq_set: sort list * sort list -> bool
wenzelm@16598
    12
  val union: sort list -> sort list -> sort list
wenzelm@16598
    13
  val subtract: sort list -> sort list -> sort list
wenzelm@16598
    14
  val insert_sort: sort -> sort list -> sort list
wenzelm@16598
    15
  val insert_typ: typ -> sort list -> sort list
wenzelm@16598
    16
  val insert_typs: typ list -> sort list -> sort list
wenzelm@16598
    17
  val insert_term: term -> sort list -> sort list
wenzelm@16598
    18
  val insert_terms: term list -> sort list -> sort list
wenzelm@16598
    19
  (*signature information*)
wenzelm@14782
    20
  type classes
wenzelm@14782
    21
  type arities
wenzelm@14782
    22
  val class_eq: classes -> class * class -> bool
wenzelm@14782
    23
  val class_less: classes -> class * class -> bool
wenzelm@14782
    24
  val class_le: classes -> class * class -> bool
wenzelm@14782
    25
  val sort_eq: classes -> sort * sort -> bool
wenzelm@14782
    26
  val sort_le: classes -> sort * sort -> bool
wenzelm@14782
    27
  val sorts_le: classes -> sort list * sort list -> bool
wenzelm@14782
    28
  val inter_sort: classes -> sort * sort -> sort
wenzelm@14782
    29
  val norm_sort: classes -> sort -> sort
wenzelm@14986
    30
  val certify_class: classes -> class -> class
wenzelm@14986
    31
  val certify_sort: classes -> sort -> sort
wenzelm@14782
    32
  val of_sort: classes * arities -> typ * sort -> bool
wenzelm@7643
    33
  exception DOMAIN of string * class
wenzelm@14782
    34
  val mg_domain: classes * arities -> string -> sort -> sort list  (*exception DOMAIN*)
wenzelm@14828
    35
  val witness_sorts: classes * arities -> string list ->
wenzelm@14828
    36
    sort list -> sort list -> (typ * sort) list
wenzelm@2956
    37
end;
wenzelm@2956
    38
wenzelm@2956
    39
structure Sorts: SORTS =
wenzelm@2956
    40
struct
wenzelm@2956
    41
wenzelm@2956
    42
(** type classes and sorts **)
wenzelm@2956
    43
wenzelm@2956
    44
(*
wenzelm@2956
    45
  Classes denote (possibly empty) collections of types that are
wenzelm@2956
    46
  partially ordered by class inclusion. They are represented
wenzelm@2956
    47
  symbolically by strings.
wenzelm@2956
    48
wenzelm@2956
    49
  Sorts are intersections of finitely many classes. They are
wenzelm@2956
    50
  represented by lists of classes.  Normal forms of sorts are sorted
wenzelm@2956
    51
  lists of minimal classes (wrt. current class inclusion).
wenzelm@2956
    52
wenzelm@14782
    53
  (types already defined in Pure/term.ML)
wenzelm@2956
    54
*)
wenzelm@2956
    55
wenzelm@2956
    56
wenzelm@16598
    57
(* ordered lists of sorts *)
wenzelm@14782
    58
wenzelm@16598
    59
val eq_set = OrdList.eq_set Term.sort_ord;
wenzelm@16598
    60
val op union = OrdList.union Term.sort_ord;
wenzelm@16598
    61
val subtract = OrdList.subtract Term.sort_ord;
wenzelm@14782
    62
wenzelm@16598
    63
val insert_sort = OrdList.insert Term.sort_ord;
wenzelm@14782
    64
wenzelm@16598
    65
fun insert_typ (TFree (_, S)) Ss = insert_sort S Ss
wenzelm@16598
    66
  | insert_typ (TVar (_, S)) Ss = insert_sort S Ss
wenzelm@16598
    67
  | insert_typ (Type (_, Ts)) Ss = insert_typs Ts Ss
wenzelm@16598
    68
and insert_typs [] Ss = Ss
wenzelm@16598
    69
  | insert_typs (T :: Ts) Ss = insert_typs Ts (insert_typ T Ss);
wenzelm@14782
    70
wenzelm@16598
    71
fun insert_term (Const (_, T)) Ss = insert_typ T Ss
wenzelm@16598
    72
  | insert_term (Free (_, T)) Ss = insert_typ T Ss
wenzelm@16598
    73
  | insert_term (Var (_, T)) Ss = insert_typ T Ss
wenzelm@16598
    74
  | insert_term (Bound _) Ss = Ss
wenzelm@16598
    75
  | insert_term (Abs (_, T, t)) Ss = insert_term t (insert_typ T Ss)
wenzelm@16598
    76
  | insert_term (t $ u) Ss = insert_term t (insert_term u Ss);
wenzelm@14782
    77
wenzelm@16598
    78
fun insert_terms [] Ss = Ss
wenzelm@16598
    79
  | insert_terms (t :: ts) Ss = insert_terms ts (insert_term t Ss);
wenzelm@14782
    80
wenzelm@14782
    81
wenzelm@2956
    82
(* sort signature information *)
wenzelm@2956
    83
wenzelm@2956
    84
(*
wenzelm@14782
    85
  classes: graph representing class declarations together with proper
wenzelm@14782
    86
    subclass relation, which needs to be transitive and acyclic.
wenzelm@2956
    87
wenzelm@14782
    88
  arities: table of association lists of all type arities; (t, ars)
wenzelm@14782
    89
    means that type constructor t has the arities ars; an element (c,
wenzelm@14782
    90
    Ss) of ars represents the arity t::(Ss)c.  "Coregularity" of the
wenzelm@14870
    91
    arities structure requires that for any two declarations
wenzelm@14870
    92
    t::(Ss1)c1 and t::(Ss2)c2 such that c1 <= c2 holds Ss1 <= Ss2.
wenzelm@2956
    93
*)
wenzelm@2956
    94
wenzelm@14782
    95
type classes = stamp Graph.T;
wenzelm@14782
    96
type arities = (class * sort list) list Symtab.table;
wenzelm@2956
    97
wenzelm@2956
    98
wenzelm@2956
    99
wenzelm@2956
   100
(** equality and inclusion **)
wenzelm@2956
   101
wenzelm@2956
   102
(* classes *)
wenzelm@2956
   103
wenzelm@14782
   104
fun class_eq (_: classes) (c1, c2:class) = c1 = c2;
wenzelm@14782
   105
val class_less: classes -> class * class -> bool = Graph.is_edge;
wenzelm@14782
   106
fun class_le classes (c1, c2) = c1 = c2 orelse class_less classes (c1, c2);
wenzelm@2956
   107
wenzelm@2956
   108
wenzelm@2956
   109
(* sorts *)
wenzelm@2956
   110
wenzelm@14782
   111
fun sort_le classes (S1, S2) =
haftmann@17155
   112
  forall (fn c2 => exists (fn c1 => class_le classes (c1, c2)) S1) S2;
wenzelm@2956
   113
wenzelm@14782
   114
fun sorts_le classes (Ss1, Ss2) =
wenzelm@14782
   115
  ListPair.all (sort_le classes) (Ss1, Ss2);
wenzelm@2956
   116
wenzelm@14782
   117
fun sort_eq classes (S1, S2) =
wenzelm@14782
   118
  sort_le classes (S1, S2) andalso sort_le classes (S2, S1);
wenzelm@2956
   119
wenzelm@2956
   120
wenzelm@2956
   121
(* normal forms of sorts *)
wenzelm@2956
   122
wenzelm@14782
   123
fun minimal_class classes S c =
wenzelm@14782
   124
  not (exists (fn c' => class_less classes (c', c)) S);
wenzelm@2956
   125
wenzelm@14986
   126
fun norm_sort _ [] = []
wenzelm@14986
   127
  | norm_sort _ (S as [_]) = S
wenzelm@18428
   128
  | norm_sort classes S = sort_distinct string_ord (filter (minimal_class classes S) S);
wenzelm@14986
   129
wenzelm@14986
   130
wenzelm@14986
   131
(* certify *)
wenzelm@14986
   132
wenzelm@14986
   133
fun certify_class classes c =
wenzelm@14986
   134
  if can (Graph.get_node classes) c then c
wenzelm@14986
   135
  else raise TYPE ("Undeclared class: " ^ quote c, [], []);
wenzelm@14986
   136
wenzelm@14986
   137
fun certify_sort classes = norm_sort classes o map (certify_class classes);
wenzelm@2956
   138
wenzelm@2956
   139
wenzelm@2956
   140
wenzelm@2956
   141
(** intersection **)
wenzelm@2956
   142
wenzelm@7643
   143
(*intersect class with sort (preserves minimality)*)
wenzelm@16881
   144
fun inter_class classes c S =
wenzelm@2956
   145
  let
wenzelm@2956
   146
    fun intr [] = [c]
wenzelm@2956
   147
      | intr (S' as c' :: c's) =
wenzelm@14782
   148
          if class_le classes (c', c) then S'
wenzelm@14782
   149
          else if class_le classes (c, c') then intr c's
wenzelm@2956
   150
          else c' :: intr c's
wenzelm@2956
   151
  in intr S end;
wenzelm@2956
   152
wenzelm@2956
   153
(*instersect sorts (preserves minimality)*)
wenzelm@16881
   154
fun inter_sort classes (S1, S2) =
wenzelm@16881
   155
  sort_strings (fold (inter_class classes) S1 S2);
wenzelm@2956
   156
wenzelm@2956
   157
wenzelm@2956
   158
wenzelm@2956
   159
(** sorts of types **)
wenzelm@2956
   160
wenzelm@7643
   161
(* mg_domain *)
wenzelm@7643
   162
wenzelm@7643
   163
exception DOMAIN of string * class;
wenzelm@2956
   164
wenzelm@16881
   165
fun mg_domain (classes, arities) a S =
wenzelm@16881
   166
  let
wenzelm@16881
   167
    fun dom c =
wenzelm@18931
   168
      (case AList.lookup (op =) (Symtab.lookup_list arities a) c of
wenzelm@16881
   169
        NONE => raise DOMAIN (a, c)
wenzelm@16881
   170
      | SOME Ss => Ss);
wenzelm@16881
   171
    fun dom_inter c Ss = ListPair.map (inter_sort classes) (dom c, Ss);
wenzelm@16881
   172
  in
wenzelm@16881
   173
    (case S of
wenzelm@16881
   174
      [] => sys_error "mg_domain"  (*don't know number of args!*)
wenzelm@16881
   175
    | c :: cs => fold dom_inter cs (dom c))
wenzelm@16881
   176
  end;
wenzelm@2956
   177
wenzelm@2956
   178
wenzelm@2990
   179
(* of_sort *)
wenzelm@2990
   180
wenzelm@14782
   181
fun of_sort (classes, arities) =
wenzelm@2990
   182
  let
wenzelm@2990
   183
    fun ofS (_, []) = true
wenzelm@14782
   184
      | ofS (TFree (_, S), S') = sort_le classes (S, S')
wenzelm@14782
   185
      | ofS (TVar (_, S), S') = sort_le classes (S, S')
wenzelm@2990
   186
      | ofS (Type (a, Ts), S) =
wenzelm@14782
   187
          let val Ss = mg_domain (classes, arities) a S in
wenzelm@2990
   188
            ListPair.all ofS (Ts, Ss)
wenzelm@7643
   189
          end handle DOMAIN _ => false;
wenzelm@2990
   190
  in ofS end;
wenzelm@2990
   191
wenzelm@2990
   192
wenzelm@2956
   193
wenzelm@7643
   194
(** witness_sorts **)
wenzelm@7643
   195
wenzelm@14828
   196
local
wenzelm@14828
   197
wenzelm@14828
   198
fun witness_aux (classes, arities) log_types hyps sorts =
wenzelm@7643
   199
  let
wenzelm@7643
   200
    val top_witn = (propT, []);
wenzelm@14782
   201
    fun le S1 S2 = sort_le classes (S1, S2);
skalberg@15531
   202
    fun get_solved S2 (T, S1) = if le S1 S2 then SOME (T, S2) else NONE;
skalberg@15531
   203
    fun get_hyp S2 S1 = if le S1 S2 then SOME (TFree ("'hyp", S1), S2) else NONE;
skalberg@15531
   204
    fun mg_dom t S = SOME (mg_domain (classes, arities) t S) handle DOMAIN _ => NONE;
wenzelm@7643
   205
skalberg@15531
   206
    fun witn_sort _ (solved_failed, []) = (solved_failed, SOME top_witn)
wenzelm@7643
   207
      | witn_sort path ((solved, failed), S) =
skalberg@15531
   208
          if exists (le S) failed then ((solved, failed), NONE)
wenzelm@7643
   209
          else
wenzelm@7643
   210
            (case get_first (get_solved S) solved of
skalberg@15531
   211
              SOME w => ((solved, failed), SOME w)
skalberg@15531
   212
            | NONE =>
wenzelm@7643
   213
                (case get_first (get_hyp S) hyps of
skalberg@15531
   214
                  SOME w => ((w :: solved, failed), SOME w)
skalberg@15531
   215
                | NONE => witn_types path log_types ((solved, failed), S)))
wenzelm@7643
   216
wenzelm@7643
   217
    and witn_sorts path x = foldl_map (witn_sort path) x
wenzelm@2956
   218
skalberg@15531
   219
    and witn_types _ [] ((solved, failed), S) = ((solved, S :: failed), NONE)
wenzelm@7643
   220
      | witn_types path (t :: ts) (solved_failed, S) =
wenzelm@7643
   221
          (case mg_dom t S of
skalberg@15531
   222
            SOME SS =>
wenzelm@7643
   223
              (*do not descend into stronger args (achieving termination)*)
wenzelm@7643
   224
              if exists (fn D => le D S orelse exists (le D) path) SS then
wenzelm@7643
   225
                witn_types path ts (solved_failed, S)
wenzelm@7643
   226
              else
wenzelm@7643
   227
                let val ((solved', failed'), ws) = witn_sorts (S :: path) (solved_failed, SS) in
wenzelm@17756
   228
                  if forall is_some ws then
wenzelm@18931
   229
                    let val w = (Type (t, map (#1 o the) ws), S)
skalberg@15531
   230
                    in ((w :: solved', failed'), SOME w) end
wenzelm@7643
   231
                  else witn_types path ts ((solved', failed'), S)
wenzelm@7643
   232
                end
skalberg@15531
   233
          | NONE => witn_types path ts (solved_failed, S));
wenzelm@7643
   234
wenzelm@7643
   235
  in witn_sorts [] (([], []), sorts) end;
wenzelm@7643
   236
wenzelm@14828
   237
fun str_of_sort [c] = c
wenzelm@14828
   238
  | str_of_sort cs = enclose "{" "}" (commas cs);
wenzelm@14828
   239
wenzelm@14828
   240
in
wenzelm@7643
   241
wenzelm@14782
   242
fun witness_sorts (classes, arities) log_types hyps sorts =
wenzelm@7643
   243
  let
wenzelm@16881
   244
    (*double check result of witness construction*)
skalberg@15531
   245
    fun check_result NONE = NONE
skalberg@15531
   246
      | check_result (SOME (T, S)) =
skalberg@15531
   247
          if of_sort (classes, arities) (T, S) then SOME (T, S)
wenzelm@14782
   248
          else sys_error ("Sorts.witness_sorts: bad witness for sort " ^ str_of_sort S);
skalberg@15570
   249
  in List.mapPartial check_result (#2 (witness_aux (classes, arities) log_types hyps sorts)) end;
wenzelm@2956
   250
wenzelm@2956
   251
end;
wenzelm@14828
   252
wenzelm@14828
   253
end;