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