src/Pure/sorts.ML
author wenzelm
Fri Jul 23 16:50:55 1999 +0200 (1999-07-23)
changeset 7067 601f930d3739
parent 3783 37fb4f64eb9d
child 7643 59f8feff9766
permissions -rw-r--r--
replace assoc lists by Symtab.table;
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@2990
    25
  val of_sort: classrel -> arities -> typ * sort -> bool
wenzelm@2956
    26
  val mg_domain: classrel -> arities -> string -> sort -> sort list
wenzelm@2956
    27
  val nonempty_sort: classrel -> arities -> sort list -> sort -> bool
wenzelm@2956
    28
end;
wenzelm@2956
    29
wenzelm@2956
    30
structure Sorts: SORTS =
wenzelm@2956
    31
struct
wenzelm@2956
    32
wenzelm@2956
    33
wenzelm@2956
    34
(** type classes and sorts **)
wenzelm@2956
    35
wenzelm@2956
    36
(*
wenzelm@2956
    37
  Classes denote (possibly empty) collections of types that are
wenzelm@2956
    38
  partially ordered by class inclusion. They are represented
wenzelm@2956
    39
  symbolically by strings.
wenzelm@2956
    40
wenzelm@2956
    41
  Sorts are intersections of finitely many classes. They are
wenzelm@2956
    42
  represented by lists of classes.  Normal forms of sorts are sorted
wenzelm@2956
    43
  lists of minimal classes (wrt. current class inclusion).
wenzelm@2956
    44
wenzelm@2956
    45
  (already defined in Pure/term.ML)
wenzelm@2956
    46
*)
wenzelm@2956
    47
wenzelm@2956
    48
wenzelm@2956
    49
(* sort signature information *)
wenzelm@2956
    50
wenzelm@2956
    51
(*
wenzelm@2956
    52
  classrel:
wenzelm@7067
    53
    table representing the proper subclass relation; entries (c, cs)
wenzelm@7067
    54
    represent the superclasses cs of c;
wenzelm@2956
    55
wenzelm@2956
    56
  arities:
wenzelm@7067
    57
    table of association lists of all type arities; (t, ars) means
wenzelm@2956
    58
    that type constructor t has the arities ars; an element (c, Ss) of
wenzelm@7067
    59
    ars represents the arity t::(Ss)c;
wenzelm@2956
    60
*)
wenzelm@2956
    61
wenzelm@7067
    62
type classrel = (class list) Symtab.table;
wenzelm@7067
    63
type arities = ((class * sort list) list) Symtab.table;
wenzelm@2956
    64
wenzelm@2956
    65
wenzelm@2956
    66
(* print sorts and arities *)
wenzelm@2956
    67
wenzelm@3783
    68
val str_of_sort = Syntax.simple_str_of_sort;
wenzelm@3783
    69
fun str_of_classrel (c1, c2) = str_of_sort [c1] ^ " < " ^ str_of_sort [c2];
wenzelm@2956
    70
fun str_of_dom Ss = enclose "(" ")" (commas (map str_of_sort Ss));
wenzelm@2956
    71
wenzelm@2956
    72
fun str_of_arity (t, [], S) = t ^ " :: " ^ str_of_sort S
wenzelm@2956
    73
  | str_of_arity (t, Ss, S) =
wenzelm@2956
    74
      t ^ " :: " ^ str_of_dom Ss ^ " " ^ str_of_sort S;
wenzelm@2956
    75
wenzelm@2956
    76
wenzelm@2956
    77
wenzelm@2956
    78
(** equality and inclusion **)
wenzelm@2956
    79
wenzelm@2956
    80
(* classes *)
wenzelm@2956
    81
wenzelm@2956
    82
fun class_eq _ (c1, c2:class) = c1 = c2;
wenzelm@2956
    83
wenzelm@2956
    84
fun class_less classrel (c1, c2) =
wenzelm@7067
    85
  (case Symtab.lookup (classrel, c1) of
wenzelm@2956
    86
     Some cs => c2 mem_string cs
wenzelm@2956
    87
   | None => false);
wenzelm@2956
    88
wenzelm@2956
    89
fun class_le classrel (c1, c2) =
wenzelm@2956
    90
   c1 = c2 orelse class_less classrel (c1, c2);
wenzelm@2956
    91
wenzelm@2956
    92
wenzelm@2956
    93
(* sorts *)
wenzelm@2956
    94
wenzelm@2956
    95
fun sort_le classrel (S1, S2) =
wenzelm@2956
    96
  forall (fn c2 => exists  (fn c1 => class_le classrel (c1, c2)) S1) S2;
wenzelm@2956
    97
wenzelm@2956
    98
fun sorts_le classrel (Ss1, Ss2) =
wenzelm@2956
    99
  ListPair.all (sort_le classrel) (Ss1, Ss2);
wenzelm@2956
   100
wenzelm@2956
   101
fun sort_eq classrel (S1, S2) =
wenzelm@2956
   102
  sort_le classrel (S1, S2) andalso sort_le classrel (S2, S1);
wenzelm@2956
   103
wenzelm@2956
   104
fun sort_less classrel (S1, S2) =
wenzelm@2956
   105
  sort_le classrel (S1, S2) andalso not (sort_le classrel (S2, S1));
wenzelm@2956
   106
wenzelm@2956
   107
wenzelm@2956
   108
(* normal forms of sorts *)
wenzelm@2956
   109
wenzelm@2956
   110
fun minimal_class classrel S c =
wenzelm@2956
   111
  not (exists (fn c' => class_less classrel (c', c)) S);
wenzelm@2956
   112
wenzelm@2956
   113
fun norm_sort classrel S =
wenzelm@2956
   114
  sort_strings (distinct (filter (minimal_class classrel S) S));
wenzelm@2956
   115
wenzelm@2956
   116
wenzelm@2956
   117
wenzelm@2956
   118
(** intersection **)
wenzelm@2956
   119
wenzelm@2956
   120
(*intersect class with sort (preserves minimality)*)    (* FIXME tune? *)
wenzelm@2956
   121
fun inter_class classrel (c, S) =
wenzelm@2956
   122
  let
wenzelm@2956
   123
    fun intr [] = [c]
wenzelm@2956
   124
      | intr (S' as c' :: c's) =
wenzelm@2956
   125
          if class_le classrel (c', c) then S'
wenzelm@2956
   126
          else if class_le classrel (c, c') then intr c's
wenzelm@2956
   127
          else c' :: intr c's
wenzelm@2956
   128
  in intr S end;
wenzelm@2956
   129
wenzelm@2956
   130
(*instersect sorts (preserves minimality)*)
wenzelm@2956
   131
fun inter_sort classrel = foldr (inter_class classrel);
wenzelm@2956
   132
wenzelm@2956
   133
wenzelm@2956
   134
wenzelm@2956
   135
(** sorts of types **)
wenzelm@2956
   136
wenzelm@2956
   137
(* mg_domain *)                 (*exception TYPE*)
wenzelm@2956
   138
wenzelm@2956
   139
fun mg_dom arities a c =
wenzelm@7067
   140
  let fun err () = raise TYPE ("No way to get " ^ a ^ "::" ^ c ^ ".", [], []) in
wenzelm@7067
   141
    (case Symtab.lookup (arities, a) of
wenzelm@7067
   142
      None => err ()
wenzelm@7067
   143
    | Some ars => (case assoc (ars, c) of None => err () | Some Ss => Ss))
wenzelm@7067
   144
  end;
wenzelm@2956
   145
wenzelm@2956
   146
fun mg_domain _ _ _ [] = sys_error "mg_domain"  (*don't know number of args!*)
wenzelm@2956
   147
  | mg_domain classrel arities a S =
wenzelm@2956
   148
      let val doms = map (mg_dom arities a) S in
wenzelm@2956
   149
        foldl (ListPair.map (inter_sort classrel)) (hd doms, tl doms)
wenzelm@2956
   150
      end;
wenzelm@2956
   151
wenzelm@2956
   152
wenzelm@2990
   153
(* of_sort *)
wenzelm@2990
   154
wenzelm@2990
   155
fun of_sort classrel arities =
wenzelm@2990
   156
  let
wenzelm@2990
   157
    fun ofS (_, []) = true
wenzelm@2990
   158
      | ofS (TFree (_, S), S') = sort_le classrel (S, S')
wenzelm@2990
   159
      | ofS (TVar (_, S), S') = sort_le classrel (S, S')
wenzelm@2990
   160
      | ofS (Type (a, Ts), S) =
wenzelm@2990
   161
          let val Ss = mg_domain classrel arities a S in
wenzelm@2990
   162
            ListPair.all ofS (Ts, Ss)
wenzelm@2990
   163
          end handle TYPE _ => false;
wenzelm@2990
   164
  in ofS end;
wenzelm@2990
   165
wenzelm@2990
   166
wenzelm@2956
   167
wenzelm@2956
   168
(** nonempty_sort **)
wenzelm@2956
   169
wenzelm@2956
   170
(* FIXME improve: proper sorts; non-base, non-ground types (vars from hyps) *)
wenzelm@2956
   171
fun nonempty_sort classrel _ _ [] = true
wenzelm@2956
   172
  | nonempty_sort classrel arities hyps S =
wenzelm@7067
   173
      Symtab.exists (exists (fn (c, Ss) => [c] = S andalso null Ss) o snd) arities
wenzelm@2956
   174
        orelse exists (fn S' => sort_le classrel (S', S)) hyps;
wenzelm@2956
   175
wenzelm@2956
   176
end;