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