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