src/Pure/sorts.ML
author wenzelm
Fri May 21 21:21:51 2004 +0200 (2004-05-21)
changeset 14782 d6ce35a1c386
parent 9281 a48818595670
child 14828 15d12761ba54
permissions -rw-r--r--
incorporate sort ops from term.ML; use Graph.T; misc cleanup;
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
  val str_of_sort: sort -> string
wenzelm@2956
    11
  val str_of_arity: string * sort list * sort -> string
wenzelm@14782
    12
  val eq_sort: sort * sort -> bool
wenzelm@14782
    13
  val mem_sort: sort * sort list -> bool
wenzelm@14782
    14
  val subset_sort: sort list * sort list -> bool
wenzelm@14782
    15
  val eq_set_sort: sort list * sort list -> bool
wenzelm@14782
    16
  val ins_sort: sort * sort list -> sort list
wenzelm@14782
    17
  val union_sort: sort list * sort list -> sort list
wenzelm@14782
    18
  val rems_sort: sort list * sort list -> sort list
wenzelm@14782
    19
  type classes
wenzelm@14782
    20
  type arities
wenzelm@14782
    21
  val class_eq: classes -> class * class -> bool
wenzelm@14782
    22
  val class_less: classes -> class * class -> bool
wenzelm@14782
    23
  val class_le: classes -> class * class -> bool
wenzelm@14782
    24
  val sort_eq: classes -> sort * sort -> bool
wenzelm@14782
    25
  val sort_less: classes -> sort * sort -> bool
wenzelm@14782
    26
  val sort_le: classes -> sort * sort -> bool
wenzelm@14782
    27
  val sorts_le: classes -> sort list * sort list -> bool
wenzelm@14782
    28
  val inter_class: classes -> class * sort -> sort
wenzelm@14782
    29
  val inter_sort: classes -> sort * sort -> sort
wenzelm@14782
    30
  val norm_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@14782
    34
  val witness_sorts: classes * arities -> string list
wenzelm@7643
    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
(* simple printing -- lacks pretty printing, translations etc. *)
wenzelm@14782
    57
wenzelm@14782
    58
fun str_of_sort [c] = c
wenzelm@14782
    59
  | str_of_sort cs = Library.enclose "{" "}" (Library.commas cs);
wenzelm@14782
    60
wenzelm@14782
    61
fun str_of_arity (t, [], S) = t ^ " :: " ^ str_of_sort S
wenzelm@14782
    62
  | str_of_arity (t, Ss, S) = t ^ " :: " ^
wenzelm@14782
    63
      Library.enclose "(" ")" (Library.commas (map str_of_sort Ss)) ^ " " ^ str_of_sort S;
wenzelm@14782
    64
wenzelm@14782
    65
wenzelm@14782
    66
(* equality, membership and insertion of sorts *)
wenzelm@14782
    67
wenzelm@14782
    68
fun eq_sort ([]: sort, []) = true
wenzelm@14782
    69
  | eq_sort ((S1 :: Ss1), (S2 :: Ss2)) = S1 = S2 andalso eq_sort (Ss1, Ss2)
wenzelm@14782
    70
  | eq_sort (_, _) = false;
wenzelm@14782
    71
wenzelm@14782
    72
fun mem_sort (_: sort, []) = false
wenzelm@14782
    73
  | mem_sort (S, S' :: Ss) = eq_sort (S, S') orelse mem_sort (S, Ss);
wenzelm@14782
    74
wenzelm@14782
    75
fun ins_sort (S, Ss) = if mem_sort (S, Ss) then Ss else S :: Ss;
wenzelm@14782
    76
wenzelm@14782
    77
fun union_sort (Ss, []) = Ss
wenzelm@14782
    78
  | union_sort ([], Ss) = Ss
wenzelm@14782
    79
  | union_sort ((S :: Ss), Ss') = union_sort (Ss, ins_sort (S, Ss'));
wenzelm@14782
    80
wenzelm@14782
    81
fun subset_sort ([], Ss) = true
wenzelm@14782
    82
  | subset_sort (S :: Ss, Ss') = mem_sort (S, Ss') andalso subset_sort (Ss, Ss');
wenzelm@14782
    83
wenzelm@14782
    84
fun eq_set_sort (Ss, Ss') =
wenzelm@14782
    85
  Ss = Ss' orelse (subset_sort (Ss, Ss') andalso subset_sort (Ss', Ss));
wenzelm@14782
    86
wenzelm@14782
    87
val rems_sort = gen_rems eq_sort;
wenzelm@14782
    88
wenzelm@14782
    89
wenzelm@2956
    90
(* sort signature information *)
wenzelm@2956
    91
wenzelm@2956
    92
(*
wenzelm@14782
    93
  classes: graph representing class declarations together with proper
wenzelm@14782
    94
    subclass relation, which needs to be transitive and acyclic.
wenzelm@2956
    95
wenzelm@14782
    96
  arities: table of association lists of all type arities; (t, ars)
wenzelm@14782
    97
    means that type constructor t has the arities ars; an element (c,
wenzelm@14782
    98
    Ss) of ars represents the arity t::(Ss)c.  "Coregularity" of the
wenzelm@14782
    99
    arities structure requires that for any two declarations t:(Ss1)c1
wenzelm@14782
   100
    and t:(Ss2)c2 such that c1 <= c2 holds Ss1 <= Ss2.
wenzelm@2956
   101
*)
wenzelm@2956
   102
wenzelm@14782
   103
type classes = stamp Graph.T;
wenzelm@14782
   104
type arities = (class * sort list) list Symtab.table;
wenzelm@2956
   105
wenzelm@2956
   106
wenzelm@2956
   107
wenzelm@2956
   108
(** equality and inclusion **)
wenzelm@2956
   109
wenzelm@2956
   110
(* classes *)
wenzelm@2956
   111
wenzelm@14782
   112
fun class_eq (_: classes) (c1, c2:class) = c1 = c2;
wenzelm@14782
   113
val class_less: classes -> class * class -> bool = Graph.is_edge;
wenzelm@14782
   114
fun class_le classes (c1, c2) = c1 = c2 orelse class_less classes (c1, c2);
wenzelm@2956
   115
wenzelm@2956
   116
wenzelm@2956
   117
(* sorts *)
wenzelm@2956
   118
wenzelm@14782
   119
fun sort_le classes (S1, S2) =
wenzelm@14782
   120
  forall (fn c2 => exists  (fn c1 => class_le classes (c1, c2)) S1) S2;
wenzelm@2956
   121
wenzelm@14782
   122
fun sorts_le classes (Ss1, Ss2) =
wenzelm@14782
   123
  ListPair.all (sort_le classes) (Ss1, Ss2);
wenzelm@2956
   124
wenzelm@14782
   125
fun sort_eq classes (S1, S2) =
wenzelm@14782
   126
  sort_le classes (S1, S2) andalso sort_le classes (S2, S1);
wenzelm@2956
   127
wenzelm@14782
   128
fun sort_less classes (S1, S2) =
wenzelm@14782
   129
  sort_le classes (S1, S2) andalso not (sort_le classes (S2, S1));
wenzelm@2956
   130
wenzelm@2956
   131
wenzelm@2956
   132
(* normal forms of sorts *)
wenzelm@2956
   133
wenzelm@14782
   134
fun minimal_class classes S c =
wenzelm@14782
   135
  not (exists (fn c' => class_less classes (c', c)) S);
wenzelm@2956
   136
wenzelm@14782
   137
fun norm_sort classes S =
wenzelm@14782
   138
  sort_strings (distinct (filter (minimal_class classes S) S));
wenzelm@2956
   139
wenzelm@2956
   140
wenzelm@2956
   141
wenzelm@2956
   142
(** intersection **)
wenzelm@2956
   143
wenzelm@7643
   144
(*intersect class with sort (preserves minimality)*)
wenzelm@14782
   145
fun inter_class classes (c, S) =
wenzelm@2956
   146
  let
wenzelm@2956
   147
    fun intr [] = [c]
wenzelm@2956
   148
      | intr (S' as c' :: c's) =
wenzelm@14782
   149
          if class_le classes (c', c) then S'
wenzelm@14782
   150
          else if class_le classes (c, c') then intr c's
wenzelm@2956
   151
          else c' :: intr c's
wenzelm@2956
   152
  in intr S end;
wenzelm@2956
   153
wenzelm@2956
   154
(*instersect sorts (preserves minimality)*)
wenzelm@14782
   155
fun inter_sort classes = sort_strings o foldr (inter_class classes);
wenzelm@2956
   156
wenzelm@2956
   157
wenzelm@2956
   158
wenzelm@2956
   159
(** sorts of types **)
wenzelm@2956
   160
wenzelm@7643
   161
(* mg_domain *)
wenzelm@7643
   162
wenzelm@7643
   163
exception DOMAIN of string * class;
wenzelm@2956
   164
wenzelm@7643
   165
fun mg_domain _ _ [] = sys_error "mg_domain"  (*don't know number of args!*)
wenzelm@14782
   166
  | mg_domain (classes, arities) a S =
wenzelm@14782
   167
      let
wenzelm@14782
   168
        fun mg_dom c =
wenzelm@14782
   169
          (case Library.assoc_string (Symtab.lookup_multi (arities, a), c) of
wenzelm@14782
   170
            None => raise DOMAIN (a, c)
wenzelm@14782
   171
          | Some Ss => Ss);
wenzelm@14782
   172
        val doms = map mg_dom S;
wenzelm@14782
   173
      in foldl (ListPair.map (inter_sort classes)) (hd doms, tl doms) end;
wenzelm@2956
   174
wenzelm@2956
   175
wenzelm@2990
   176
(* of_sort *)
wenzelm@2990
   177
wenzelm@14782
   178
fun of_sort (classes, arities) =
wenzelm@2990
   179
  let
wenzelm@2990
   180
    fun ofS (_, []) = true
wenzelm@14782
   181
      | ofS (TFree (_, S), S') = sort_le classes (S, S')
wenzelm@14782
   182
      | ofS (TVar (_, S), S') = sort_le classes (S, S')
wenzelm@2990
   183
      | ofS (Type (a, Ts), S) =
wenzelm@14782
   184
          let val Ss = mg_domain (classes, arities) a S in
wenzelm@2990
   185
            ListPair.all ofS (Ts, Ss)
wenzelm@7643
   186
          end handle DOMAIN _ => false;
wenzelm@2990
   187
  in ofS end;
wenzelm@2990
   188
wenzelm@2990
   189
wenzelm@2956
   190
wenzelm@7643
   191
(** witness_sorts **)
wenzelm@7643
   192
wenzelm@14782
   193
fun witness_sorts_aux (classes, arities) log_types hyps sorts =
wenzelm@7643
   194
  let
wenzelm@7643
   195
    val top_witn = (propT, []);
wenzelm@14782
   196
    fun le S1 S2 = sort_le classes (S1, S2);
wenzelm@7643
   197
    fun get_solved S2 (T, S1) = if le S1 S2 then Some (T, S2) else None;
wenzelm@7643
   198
    fun get_hyp S2 S1 = if le S1 S2 then Some (TFree ("'hyp", S1), S2) else None;
wenzelm@14782
   199
    fun mg_dom t S = Some (mg_domain (classes, arities) t S) handle DOMAIN _ => None;
wenzelm@7643
   200
wenzelm@7643
   201
    fun witn_sort _ (solved_failed, []) = (solved_failed, Some top_witn)
wenzelm@7643
   202
      | witn_sort path ((solved, failed), S) =
wenzelm@7643
   203
          if exists (le S) failed then ((solved, failed), None)
wenzelm@7643
   204
          else
wenzelm@7643
   205
            (case get_first (get_solved S) solved of
wenzelm@7643
   206
              Some w => ((solved, failed), Some w)
wenzelm@7643
   207
            | None =>
wenzelm@7643
   208
                (case get_first (get_hyp S) hyps of
wenzelm@7643
   209
                  Some w => ((w :: solved, failed), Some w)
wenzelm@7643
   210
                | None => witn_types path log_types ((solved, failed), S)))
wenzelm@7643
   211
wenzelm@7643
   212
    and witn_sorts path x = foldl_map (witn_sort path) x
wenzelm@2956
   213
wenzelm@7643
   214
    and witn_types _ [] ((solved, failed), S) = ((solved, S :: failed), None)
wenzelm@7643
   215
      | witn_types path (t :: ts) (solved_failed, S) =
wenzelm@7643
   216
          (case mg_dom t S of
wenzelm@7643
   217
            Some SS =>
wenzelm@7643
   218
              (*do not descend into stronger args (achieving termination)*)
wenzelm@7643
   219
              if exists (fn D => le D S orelse exists (le D) path) SS then
wenzelm@7643
   220
                witn_types path ts (solved_failed, S)
wenzelm@7643
   221
              else
wenzelm@7643
   222
                let val ((solved', failed'), ws) = witn_sorts (S :: path) (solved_failed, SS) in
wenzelm@7643
   223
                  if forall is_some ws then
wenzelm@7643
   224
                    let val w = (Type (t, map (#1 o the) ws), S)
wenzelm@7643
   225
                    in ((w :: solved', failed'), Some w) end
wenzelm@7643
   226
                  else witn_types path ts ((solved', failed'), S)
wenzelm@7643
   227
                end
wenzelm@7643
   228
          | None => witn_types path ts (solved_failed, S));
wenzelm@7643
   229
wenzelm@7643
   230
  in witn_sorts [] (([], []), sorts) end;
wenzelm@7643
   231
wenzelm@7643
   232
wenzelm@14782
   233
fun witness_sorts (classes, arities) log_types hyps sorts =
wenzelm@7643
   234
  let
wenzelm@14782
   235
    (*double check result of witness search*)
wenzelm@7643
   236
    fun check_result None = None
wenzelm@7643
   237
      | check_result (Some (T, S)) =
wenzelm@14782
   238
          if of_sort (classes, arities) (T, S) then Some (T, S)
wenzelm@14782
   239
          else sys_error ("Sorts.witness_sorts: bad witness for sort " ^ str_of_sort S);
wenzelm@14782
   240
  in mapfilter check_result (#2 (witness_sorts_aux (classes, arities) log_types hyps sorts)) end;
wenzelm@7643
   241
wenzelm@2956
   242
wenzelm@2956
   243
end;