src/Pure/sorts.ML
author paulson
Thu Sep 25 12:09:41 1997 +0200 (1997-09-25)
changeset 3706 e57b5902822f
parent 3633 1884b433c6a5
child 3783 37fb4f64eb9d
permissions -rw-r--r--
Generalized and exported biresolution_from_nets_tac to allow the declaration
of Clarify_tac
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@2956
    53
    association list representing the proper subclass relation;
wenzelm@2956
    54
    pairs (c, cs) represent the superclasses cs of c;
wenzelm@2956
    55
wenzelm@2956
    56
  arities:
wenzelm@2956
    57
    two-fold association list of all type arities; (t, ars) means
wenzelm@2956
    58
    that type constructor t has the arities ars; an element (c, Ss) of
wenzelm@2956
    59
    ars represents the arity t::(Ss)c.
wenzelm@2956
    60
*)
wenzelm@2956
    61
wenzelm@2956
    62
type classrel = (class * class list) list;
wenzelm@2956
    63
type arities = (string * (class * sort list) list) list;
wenzelm@2956
    64
wenzelm@2956
    65
wenzelm@2956
    66
(* print sorts and arities *)
wenzelm@2956
    67
wenzelm@3633
    68
fun str_of_classrel (c1, c2) = c1 ^ " < " ^ c2;
wenzelm@3633
    69
wenzelm@2956
    70
fun str_of_sort [c] = c
wenzelm@2956
    71
  | str_of_sort cs = enclose "{" "}" (commas cs);
wenzelm@2956
    72
wenzelm@2956
    73
fun str_of_dom Ss = enclose "(" ")" (commas (map str_of_sort Ss));
wenzelm@2956
    74
wenzelm@2956
    75
fun str_of_arity (t, [], S) = t ^ " :: " ^ str_of_sort S
wenzelm@2956
    76
  | str_of_arity (t, Ss, S) =
wenzelm@2956
    77
      t ^ " :: " ^ str_of_dom Ss ^ " " ^ str_of_sort S;
wenzelm@2956
    78
wenzelm@2956
    79
wenzelm@2956
    80
wenzelm@2956
    81
(** equality and inclusion **)
wenzelm@2956
    82
wenzelm@2956
    83
(* classes *)
wenzelm@2956
    84
wenzelm@2956
    85
fun class_eq _ (c1, c2:class) = c1 = c2;
wenzelm@2956
    86
wenzelm@2956
    87
fun class_less classrel (c1, c2) =
wenzelm@2956
    88
  (case assoc (classrel, c1) of
wenzelm@2956
    89
     Some cs => c2 mem_string cs
wenzelm@2956
    90
   | None => false);
wenzelm@2956
    91
wenzelm@2956
    92
fun class_le classrel (c1, c2) =
wenzelm@2956
    93
   c1 = c2 orelse class_less classrel (c1, c2);
wenzelm@2956
    94
wenzelm@2956
    95
wenzelm@2956
    96
(* sorts *)
wenzelm@2956
    97
wenzelm@2956
    98
fun sort_le classrel (S1, S2) =
wenzelm@2956
    99
  forall (fn c2 => exists  (fn c1 => class_le classrel (c1, c2)) S1) S2;
wenzelm@2956
   100
wenzelm@2956
   101
fun sorts_le classrel (Ss1, Ss2) =
wenzelm@2956
   102
  ListPair.all (sort_le classrel) (Ss1, Ss2);
wenzelm@2956
   103
wenzelm@2956
   104
fun sort_eq classrel (S1, S2) =
wenzelm@2956
   105
  sort_le classrel (S1, S2) andalso sort_le classrel (S2, S1);
wenzelm@2956
   106
wenzelm@2956
   107
fun sort_less classrel (S1, S2) =
wenzelm@2956
   108
  sort_le classrel (S1, S2) andalso not (sort_le classrel (S2, S1));
wenzelm@2956
   109
wenzelm@2956
   110
wenzelm@2956
   111
(* normal forms of sorts *)
wenzelm@2956
   112
wenzelm@2956
   113
fun minimal_class classrel S c =
wenzelm@2956
   114
  not (exists (fn c' => class_less classrel (c', c)) S);
wenzelm@2956
   115
wenzelm@2956
   116
fun norm_sort classrel S =
wenzelm@2956
   117
  sort_strings (distinct (filter (minimal_class classrel S) S));
wenzelm@2956
   118
wenzelm@2956
   119
wenzelm@2956
   120
wenzelm@2956
   121
(** intersection **)
wenzelm@2956
   122
wenzelm@2956
   123
(*intersect class with sort (preserves minimality)*)    (* FIXME tune? *)
wenzelm@2956
   124
fun inter_class classrel (c, S) =
wenzelm@2956
   125
  let
wenzelm@2956
   126
    fun intr [] = [c]
wenzelm@2956
   127
      | intr (S' as c' :: c's) =
wenzelm@2956
   128
          if class_le classrel (c', c) then S'
wenzelm@2956
   129
          else if class_le classrel (c, c') then intr c's
wenzelm@2956
   130
          else c' :: intr c's
wenzelm@2956
   131
  in intr S end;
wenzelm@2956
   132
wenzelm@2956
   133
(*instersect sorts (preserves minimality)*)
wenzelm@2956
   134
fun inter_sort classrel = foldr (inter_class classrel);
wenzelm@2956
   135
wenzelm@2956
   136
wenzelm@2956
   137
wenzelm@2956
   138
(** sorts of types **)
wenzelm@2956
   139
wenzelm@2956
   140
(* mg_domain *)                 (*exception TYPE*)
wenzelm@2956
   141
wenzelm@2956
   142
fun mg_dom arities a c =
wenzelm@2956
   143
  (case assoc2 (arities, (a, c)) of
wenzelm@2956
   144
    None => raise_type ("No way to get " ^ a ^ "::" ^ c ^ ".") [] []
wenzelm@2956
   145
  | Some Ss => Ss);
wenzelm@2956
   146
wenzelm@2956
   147
fun mg_domain _ _ _ [] = sys_error "mg_domain"  (*don't know number of args!*)
wenzelm@2956
   148
  | mg_domain classrel arities a S =
wenzelm@2956
   149
      let val doms = map (mg_dom arities a) S in
wenzelm@2956
   150
        foldl (ListPair.map (inter_sort classrel)) (hd doms, tl doms)
wenzelm@2956
   151
      end;
wenzelm@2956
   152
wenzelm@2956
   153
wenzelm@2990
   154
(* of_sort *)
wenzelm@2990
   155
wenzelm@2990
   156
fun of_sort classrel arities =
wenzelm@2990
   157
  let
wenzelm@2990
   158
    fun ofS (_, []) = true
wenzelm@2990
   159
      | ofS (TFree (_, S), S') = sort_le classrel (S, S')
wenzelm@2990
   160
      | ofS (TVar (_, S), S') = sort_le classrel (S, S')
wenzelm@2990
   161
      | ofS (Type (a, Ts), S) =
wenzelm@2990
   162
          let val Ss = mg_domain classrel arities a S in
wenzelm@2990
   163
            ListPair.all ofS (Ts, Ss)
wenzelm@2990
   164
          end handle TYPE _ => false;
wenzelm@2990
   165
  in ofS end;
wenzelm@2990
   166
wenzelm@2990
   167
wenzelm@2956
   168
wenzelm@2956
   169
(** nonempty_sort **)
wenzelm@2956
   170
wenzelm@2956
   171
(* FIXME improve: proper sorts; non-base, non-ground types (vars from hyps) *)
wenzelm@2956
   172
fun nonempty_sort classrel _ _ [] = true
wenzelm@2956
   173
  | nonempty_sort classrel arities hyps S =
wenzelm@2956
   174
      exists (exists (fn (c, Ss) => [c] = S andalso null Ss) o snd) arities
wenzelm@2956
   175
        orelse exists (fn S' => sort_le classrel (S', S)) hyps;
wenzelm@2956
   176
wenzelm@2956
   177
end;