src/Pure/sorts.ML
author berghofe
Wed Oct 31 19:37:04 2001 +0100 (2001-10-31)
changeset 11998 b14e7686ce84
parent 9281 a48818595670
child 14782 d6ce35a1c386
permissions -rw-r--r--
- enter_thmx -> enter_thms
- improved naming of theorems: enter_thms now takes functions pre_name and post_name
as arguments
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@2956
    10
  type classrel
wenzelm@2956
    11
  type arities
wenzelm@3633
    12
  val str_of_classrel: class * class -> string
wenzelm@2956
    13
  val str_of_sort: sort -> string
wenzelm@2956
    14
  val str_of_arity: string * sort list * sort -> string
wenzelm@2956
    15
  val class_eq: classrel -> class * class -> bool
wenzelm@2956
    16
  val class_less: classrel -> class * class -> bool
wenzelm@2956
    17
  val class_le: classrel -> class * class -> bool
wenzelm@2956
    18
  val sort_eq: classrel -> sort * sort -> bool
wenzelm@2956
    19
  val sort_less: classrel -> sort * sort -> bool
wenzelm@2956
    20
  val sort_le: classrel -> sort * sort -> bool
wenzelm@2956
    21
  val sorts_le: classrel -> sort list * sort list -> bool
wenzelm@2956
    22
  val inter_class: classrel -> class * sort -> sort
wenzelm@2956
    23
  val inter_sort: classrel -> sort * sort -> sort
wenzelm@2956
    24
  val norm_sort: classrel -> sort -> sort
wenzelm@7643
    25
  val of_sort: classrel * arities -> typ * sort -> bool
wenzelm@7643
    26
  exception DOMAIN of string * class
wenzelm@7643
    27
  val mg_domain: classrel * arities -> string -> sort -> sort list
wenzelm@7643
    28
  val witness_sorts: classrel * arities * string list
wenzelm@7643
    29
    -> sort list -> sort list -> (typ * sort) list
wenzelm@2956
    30
end;
wenzelm@2956
    31
wenzelm@2956
    32
structure Sorts: SORTS =
wenzelm@2956
    33
struct
wenzelm@2956
    34
wenzelm@2956
    35
wenzelm@2956
    36
(** type classes and sorts **)
wenzelm@2956
    37
wenzelm@2956
    38
(*
wenzelm@2956
    39
  Classes denote (possibly empty) collections of types that are
wenzelm@2956
    40
  partially ordered by class inclusion. They are represented
wenzelm@2956
    41
  symbolically by strings.
wenzelm@2956
    42
wenzelm@2956
    43
  Sorts are intersections of finitely many classes. They are
wenzelm@2956
    44
  represented by lists of classes.  Normal forms of sorts are sorted
wenzelm@2956
    45
  lists of minimal classes (wrt. current class inclusion).
wenzelm@2956
    46
wenzelm@2956
    47
  (already defined in Pure/term.ML)
wenzelm@2956
    48
*)
wenzelm@2956
    49
wenzelm@2956
    50
wenzelm@2956
    51
(* sort signature information *)
wenzelm@2956
    52
wenzelm@2956
    53
(*
wenzelm@2956
    54
  classrel:
wenzelm@7067
    55
    table representing the proper subclass relation; entries (c, cs)
wenzelm@7067
    56
    represent the superclasses cs of c;
wenzelm@2956
    57
wenzelm@2956
    58
  arities:
wenzelm@7067
    59
    table of association lists of all type arities; (t, ars) means
wenzelm@2956
    60
    that type constructor t has the arities ars; an element (c, Ss) of
wenzelm@7067
    61
    ars represents the arity t::(Ss)c;
wenzelm@2956
    62
*)
wenzelm@2956
    63
wenzelm@7067
    64
type classrel = (class list) Symtab.table;
wenzelm@7067
    65
type arities = ((class * sort list) list) Symtab.table;
wenzelm@2956
    66
wenzelm@2956
    67
wenzelm@2956
    68
(* print sorts and arities *)
wenzelm@2956
    69
wenzelm@3783
    70
val str_of_sort = Syntax.simple_str_of_sort;
wenzelm@3783
    71
fun str_of_classrel (c1, c2) = str_of_sort [c1] ^ " < " ^ str_of_sort [c2];
wenzelm@2956
    72
fun str_of_dom Ss = enclose "(" ")" (commas (map str_of_sort Ss));
wenzelm@2956
    73
wenzelm@2956
    74
fun str_of_arity (t, [], S) = t ^ " :: " ^ str_of_sort S
wenzelm@2956
    75
  | str_of_arity (t, Ss, S) =
wenzelm@2956
    76
      t ^ " :: " ^ str_of_dom Ss ^ " " ^ str_of_sort S;
wenzelm@2956
    77
wenzelm@2956
    78
wenzelm@2956
    79
wenzelm@2956
    80
(** equality and inclusion **)
wenzelm@2956
    81
wenzelm@2956
    82
(* classes *)
wenzelm@2956
    83
wenzelm@2956
    84
fun class_eq _ (c1, c2:class) = c1 = c2;
wenzelm@2956
    85
wenzelm@2956
    86
fun class_less classrel (c1, c2) =
wenzelm@7067
    87
  (case Symtab.lookup (classrel, c1) of
wenzelm@2956
    88
     Some cs => c2 mem_string cs
wenzelm@2956
    89
   | None => false);
wenzelm@2956
    90
wenzelm@2956
    91
fun class_le classrel (c1, c2) =
wenzelm@2956
    92
   c1 = c2 orelse class_less classrel (c1, c2);
wenzelm@2956
    93
wenzelm@2956
    94
wenzelm@2956
    95
(* sorts *)
wenzelm@2956
    96
wenzelm@2956
    97
fun sort_le classrel (S1, S2) =
wenzelm@2956
    98
  forall (fn c2 => exists  (fn c1 => class_le classrel (c1, c2)) S1) S2;
wenzelm@2956
    99
wenzelm@2956
   100
fun sorts_le classrel (Ss1, Ss2) =
wenzelm@2956
   101
  ListPair.all (sort_le classrel) (Ss1, Ss2);
wenzelm@2956
   102
wenzelm@2956
   103
fun sort_eq classrel (S1, S2) =
wenzelm@2956
   104
  sort_le classrel (S1, S2) andalso sort_le classrel (S2, S1);
wenzelm@2956
   105
wenzelm@2956
   106
fun sort_less classrel (S1, S2) =
wenzelm@2956
   107
  sort_le classrel (S1, S2) andalso not (sort_le classrel (S2, S1));
wenzelm@2956
   108
wenzelm@2956
   109
wenzelm@2956
   110
(* normal forms of sorts *)
wenzelm@2956
   111
wenzelm@2956
   112
fun minimal_class classrel S c =
wenzelm@2956
   113
  not (exists (fn c' => class_less classrel (c', c)) S);
wenzelm@2956
   114
wenzelm@2956
   115
fun norm_sort classrel S =
wenzelm@2956
   116
  sort_strings (distinct (filter (minimal_class classrel S) S));
wenzelm@2956
   117
wenzelm@2956
   118
wenzelm@2956
   119
wenzelm@2956
   120
(** intersection **)
wenzelm@2956
   121
wenzelm@7643
   122
(*intersect class with sort (preserves minimality)*)
wenzelm@2956
   123
fun inter_class classrel (c, S) =
wenzelm@2956
   124
  let
wenzelm@2956
   125
    fun intr [] = [c]
wenzelm@2956
   126
      | intr (S' as c' :: c's) =
wenzelm@2956
   127
          if class_le classrel (c', c) then S'
wenzelm@2956
   128
          else if class_le classrel (c, c') then intr c's
wenzelm@2956
   129
          else c' :: intr c's
wenzelm@2956
   130
  in intr S end;
wenzelm@2956
   131
wenzelm@2956
   132
(*instersect sorts (preserves minimality)*)
wenzelm@9281
   133
fun inter_sort classrel = sort_strings o foldr (inter_class classrel);
wenzelm@2956
   134
wenzelm@2956
   135
wenzelm@2956
   136
wenzelm@2956
   137
(** sorts of types **)
wenzelm@2956
   138
wenzelm@7643
   139
(* mg_domain *)
wenzelm@7643
   140
wenzelm@7643
   141
exception DOMAIN of string * class;
wenzelm@2956
   142
wenzelm@2956
   143
fun mg_dom arities a c =
wenzelm@7643
   144
  (case Symtab.lookup (arities, a) of
wenzelm@7643
   145
    None => raise DOMAIN (a, c)
wenzelm@7643
   146
  | Some ars => (case assoc (ars, c) of None => raise DOMAIN (a, c) | Some Ss => Ss));
wenzelm@2956
   147
wenzelm@7643
   148
fun mg_domain _ _ [] = sys_error "mg_domain"  (*don't know number of args!*)
wenzelm@7643
   149
  | mg_domain (classrel, arities) a S =
wenzelm@2956
   150
      let val doms = map (mg_dom arities a) S in
wenzelm@2956
   151
        foldl (ListPair.map (inter_sort classrel)) (hd doms, tl doms)
wenzelm@2956
   152
      end;
wenzelm@2956
   153
wenzelm@2956
   154
wenzelm@2990
   155
(* of_sort *)
wenzelm@2990
   156
wenzelm@7643
   157
fun of_sort (classrel, arities) =
wenzelm@2990
   158
  let
wenzelm@2990
   159
    fun ofS (_, []) = true
wenzelm@2990
   160
      | ofS (TFree (_, S), S') = sort_le classrel (S, S')
wenzelm@2990
   161
      | ofS (TVar (_, S), S') = sort_le classrel (S, S')
wenzelm@2990
   162
      | ofS (Type (a, Ts), S) =
wenzelm@7643
   163
          let val Ss = mg_domain (classrel, arities) a S in
wenzelm@2990
   164
            ListPair.all ofS (Ts, Ss)
wenzelm@7643
   165
          end handle DOMAIN _ => false;
wenzelm@2990
   166
  in ofS end;
wenzelm@2990
   167
wenzelm@2990
   168
wenzelm@2956
   169
wenzelm@7643
   170
(** witness_sorts **)
wenzelm@7643
   171
wenzelm@7643
   172
fun witness_sorts_aux (classrel, arities, log_types) hyps sorts =
wenzelm@7643
   173
  let
wenzelm@7643
   174
    val top_witn = (propT, []);
wenzelm@7643
   175
    fun le S1 S2 = sort_le classrel (S1, S2);
wenzelm@7643
   176
    fun get_solved S2 (T, S1) = if le S1 S2 then Some (T, S2) else None;
wenzelm@7643
   177
    fun get_hyp S2 S1 = if le S1 S2 then Some (TFree ("'hyp", S1), S2) else None;
wenzelm@7643
   178
    fun mg_dom t S = Some (mg_domain (classrel, arities) t S) handle DOMAIN _ => None;
wenzelm@7643
   179
wenzelm@7643
   180
    fun witn_sort _ (solved_failed, []) = (solved_failed, Some top_witn)
wenzelm@7643
   181
      | witn_sort path ((solved, failed), S) =
wenzelm@7643
   182
          if exists (le S) failed then ((solved, failed), None)
wenzelm@7643
   183
          else
wenzelm@7643
   184
            (case get_first (get_solved S) solved of
wenzelm@7643
   185
              Some w => ((solved, failed), Some w)
wenzelm@7643
   186
            | None =>
wenzelm@7643
   187
                (case get_first (get_hyp S) hyps of
wenzelm@7643
   188
                  Some w => ((w :: solved, failed), Some w)
wenzelm@7643
   189
                | None => witn_types path log_types ((solved, failed), S)))
wenzelm@7643
   190
wenzelm@7643
   191
    and witn_sorts path x = foldl_map (witn_sort path) x
wenzelm@2956
   192
wenzelm@7643
   193
    and witn_types _ [] ((solved, failed), S) = ((solved, S :: failed), None)
wenzelm@7643
   194
      | witn_types path (t :: ts) (solved_failed, S) =
wenzelm@7643
   195
          (case mg_dom t S of
wenzelm@7643
   196
            Some SS =>
wenzelm@7643
   197
              (*do not descend into stronger args (achieving termination)*)
wenzelm@7643
   198
              if exists (fn D => le D S orelse exists (le D) path) SS then
wenzelm@7643
   199
                witn_types path ts (solved_failed, S)
wenzelm@7643
   200
              else
wenzelm@7643
   201
                let val ((solved', failed'), ws) = witn_sorts (S :: path) (solved_failed, SS) in
wenzelm@7643
   202
                  if forall is_some ws then
wenzelm@7643
   203
                    let val w = (Type (t, map (#1 o the) ws), S)
wenzelm@7643
   204
                    in ((w :: solved', failed'), Some w) end
wenzelm@7643
   205
                  else witn_types path ts ((solved', failed'), S)
wenzelm@7643
   206
                end
wenzelm@7643
   207
          | None => witn_types path ts (solved_failed, S));
wenzelm@7643
   208
wenzelm@7643
   209
  in witn_sorts [] (([], []), sorts) end;
wenzelm@7643
   210
wenzelm@7643
   211
wenzelm@7643
   212
fun witness_sorts (classrel, arities, log_types) hyps sorts =
wenzelm@7643
   213
  let
wenzelm@7643
   214
    fun check_result None = None
wenzelm@7643
   215
      | check_result (Some (T, S)) =
wenzelm@7643
   216
          if of_sort (classrel, arities) (T, S) then Some (T, S)
wenzelm@7643
   217
          else (warning ("witness_sorts: rejected bad witness for " ^ str_of_sort S); None);
wenzelm@7643
   218
  in mapfilter check_result (#2 (witness_sorts_aux (classrel, arities, log_types) hyps sorts)) end;
wenzelm@7643
   219
wenzelm@2956
   220
wenzelm@2956
   221
end;