src/Pure/sorts.ML
author wenzelm
Sun Apr 30 22:50:08 2006 +0200 (2006-04-30)
changeset 19514 1f0218dab849
parent 19482 9f11af8f7ef9
child 19524 f795c1164708
permissions -rw-r--r--
moved certify_class/sort to type.ML;
added operations to build sort algebras (from type.ML);
tuned;
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@19514
     5
The order-sorted algebra of type classes.
wenzelm@2956
     6
*)
wenzelm@2956
     7
wenzelm@2956
     8
signature SORTS =
wenzelm@2956
     9
sig
wenzelm@16598
    10
  val eq_set: sort list * sort list -> bool
wenzelm@16598
    11
  val union: sort list -> sort list -> sort list
wenzelm@16598
    12
  val subtract: sort list -> sort list -> sort list
wenzelm@19463
    13
  val remove_sort: sort -> sort list -> sort list
wenzelm@16598
    14
  val insert_sort: sort -> sort list -> sort list
wenzelm@16598
    15
  val insert_typ: typ -> sort list -> sort list
wenzelm@16598
    16
  val insert_typs: typ list -> sort list -> sort list
wenzelm@16598
    17
  val insert_term: term -> sort list -> sort list
wenzelm@16598
    18
  val insert_terms: term 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_le: classes -> sort * sort -> bool
wenzelm@14782
    26
  val sorts_le: classes -> sort list * sort list -> bool
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@19514
    34
  val add_arities: Pretty.pp -> classes -> string * (class * sort list) list -> arities -> arities
wenzelm@19514
    35
  val rebuild_arities: Pretty.pp -> classes -> arities -> arities
wenzelm@19514
    36
  val merge_arities: Pretty.pp -> classes -> arities * arities -> arities
wenzelm@19514
    37
  val add_class: Pretty.pp -> class * class list -> classes -> classes
wenzelm@19514
    38
  val add_classrel: Pretty.pp -> class * class -> classes -> classes
wenzelm@19514
    39
  val merge_classes: Pretty.pp -> classes * classes -> classes
wenzelm@2956
    40
end;
wenzelm@2956
    41
wenzelm@2956
    42
structure Sorts: SORTS =
wenzelm@2956
    43
struct
wenzelm@2956
    44
wenzelm@19514
    45
wenzelm@2956
    46
(** type classes and sorts **)
wenzelm@2956
    47
wenzelm@2956
    48
(*
wenzelm@2956
    49
  Classes denote (possibly empty) collections of types that are
wenzelm@2956
    50
  partially ordered by class inclusion. They are represented
wenzelm@2956
    51
  symbolically by strings.
wenzelm@2956
    52
wenzelm@2956
    53
  Sorts are intersections of finitely many classes. They are
wenzelm@2956
    54
  represented by lists of classes.  Normal forms of sorts are sorted
wenzelm@2956
    55
  lists of minimal classes (wrt. current class inclusion).
wenzelm@2956
    56
*)
wenzelm@2956
    57
wenzelm@2956
    58
wenzelm@16598
    59
(* ordered lists of sorts *)
wenzelm@14782
    60
wenzelm@16598
    61
val eq_set = OrdList.eq_set Term.sort_ord;
wenzelm@16598
    62
val op union = OrdList.union Term.sort_ord;
wenzelm@16598
    63
val subtract = OrdList.subtract Term.sort_ord;
wenzelm@14782
    64
wenzelm@19463
    65
val remove_sort = OrdList.remove Term.sort_ord;
wenzelm@16598
    66
val insert_sort = OrdList.insert Term.sort_ord;
wenzelm@14782
    67
wenzelm@16598
    68
fun insert_typ (TFree (_, S)) Ss = insert_sort S Ss
wenzelm@16598
    69
  | insert_typ (TVar (_, S)) Ss = insert_sort S Ss
wenzelm@16598
    70
  | insert_typ (Type (_, Ts)) Ss = insert_typs Ts Ss
wenzelm@16598
    71
and insert_typs [] Ss = Ss
wenzelm@16598
    72
  | insert_typs (T :: Ts) Ss = insert_typs Ts (insert_typ T Ss);
wenzelm@14782
    73
wenzelm@16598
    74
fun insert_term (Const (_, T)) Ss = insert_typ T Ss
wenzelm@16598
    75
  | insert_term (Free (_, T)) Ss = insert_typ T Ss
wenzelm@16598
    76
  | insert_term (Var (_, T)) Ss = insert_typ T Ss
wenzelm@16598
    77
  | insert_term (Bound _) Ss = Ss
wenzelm@16598
    78
  | insert_term (Abs (_, T, t)) Ss = insert_term t (insert_typ T Ss)
wenzelm@16598
    79
  | insert_term (t $ u) Ss = insert_term t (insert_term u Ss);
wenzelm@14782
    80
wenzelm@16598
    81
fun insert_terms [] Ss = Ss
wenzelm@16598
    82
  | insert_terms (t :: ts) Ss = insert_terms ts (insert_term t Ss);
wenzelm@14782
    83
wenzelm@14782
    84
wenzelm@2956
    85
(* sort signature information *)
wenzelm@2956
    86
wenzelm@2956
    87
(*
wenzelm@14782
    88
  classes: graph representing class declarations together with proper
wenzelm@14782
    89
    subclass relation, which needs to be transitive and acyclic.
wenzelm@2956
    90
wenzelm@14782
    91
  arities: table of association lists of all type arities; (t, ars)
wenzelm@14782
    92
    means that type constructor t has the arities ars; an element (c,
wenzelm@14782
    93
    Ss) of ars represents the arity t::(Ss)c.  "Coregularity" of the
wenzelm@14870
    94
    arities structure requires that for any two declarations
wenzelm@14870
    95
    t::(Ss1)c1 and t::(Ss2)c2 such that c1 <= c2 holds Ss1 <= Ss2.
wenzelm@2956
    96
*)
wenzelm@2956
    97
wenzelm@14782
    98
type classes = stamp Graph.T;
wenzelm@14782
    99
type arities = (class * sort list) list Symtab.table;
wenzelm@2956
   100
wenzelm@2956
   101
wenzelm@2956
   102
wenzelm@2956
   103
(** equality and inclusion **)
wenzelm@2956
   104
wenzelm@2956
   105
(* classes *)
wenzelm@2956
   106
wenzelm@14782
   107
fun class_eq (_: classes) (c1, c2:class) = c1 = c2;
wenzelm@14782
   108
val class_less: classes -> class * class -> bool = Graph.is_edge;
wenzelm@14782
   109
fun class_le classes (c1, c2) = c1 = c2 orelse class_less classes (c1, c2);
wenzelm@2956
   110
wenzelm@2956
   111
wenzelm@2956
   112
(* sorts *)
wenzelm@2956
   113
wenzelm@14782
   114
fun sort_le classes (S1, S2) =
haftmann@17155
   115
  forall (fn c2 => exists (fn c1 => class_le classes (c1, c2)) S1) S2;
wenzelm@2956
   116
wenzelm@14782
   117
fun sorts_le classes (Ss1, Ss2) =
wenzelm@14782
   118
  ListPair.all (sort_le classes) (Ss1, Ss2);
wenzelm@2956
   119
wenzelm@14782
   120
fun sort_eq classes (S1, S2) =
wenzelm@14782
   121
  sort_le classes (S1, S2) andalso sort_le classes (S2, S1);
wenzelm@2956
   122
wenzelm@2956
   123
wenzelm@2956
   124
(* normal forms of sorts *)
wenzelm@2956
   125
wenzelm@14782
   126
fun minimal_class classes S c =
wenzelm@14782
   127
  not (exists (fn c' => class_less classes (c', c)) S);
wenzelm@2956
   128
wenzelm@14986
   129
fun norm_sort _ [] = []
wenzelm@14986
   130
  | norm_sort _ (S as [_]) = S
wenzelm@18428
   131
  | norm_sort classes S = sort_distinct string_ord (filter (minimal_class classes S) S);
wenzelm@14986
   132
wenzelm@14986
   133
wenzelm@14986
   134
wenzelm@19514
   135
(** intersection -- preserving minimality **)
wenzelm@2956
   136
wenzelm@16881
   137
fun inter_class classes c S =
wenzelm@2956
   138
  let
wenzelm@2956
   139
    fun intr [] = [c]
wenzelm@2956
   140
      | intr (S' as c' :: c's) =
wenzelm@14782
   141
          if class_le classes (c', c) then S'
wenzelm@14782
   142
          else if class_le classes (c, c') then intr c's
wenzelm@2956
   143
          else c' :: intr c's
wenzelm@2956
   144
  in intr S end;
wenzelm@2956
   145
wenzelm@16881
   146
fun inter_sort classes (S1, S2) =
wenzelm@16881
   147
  sort_strings (fold (inter_class classes) S1 S2);
wenzelm@2956
   148
wenzelm@2956
   149
wenzelm@2956
   150
wenzelm@2956
   151
(** sorts of types **)
wenzelm@2956
   152
wenzelm@7643
   153
(* mg_domain *)
wenzelm@7643
   154
wenzelm@7643
   155
exception DOMAIN of string * class;
wenzelm@2956
   156
wenzelm@16881
   157
fun mg_domain (classes, arities) a S =
wenzelm@16881
   158
  let
wenzelm@16881
   159
    fun dom c =
wenzelm@18931
   160
      (case AList.lookup (op =) (Symtab.lookup_list arities a) c of
wenzelm@16881
   161
        NONE => raise DOMAIN (a, c)
wenzelm@16881
   162
      | SOME Ss => Ss);
wenzelm@16881
   163
    fun dom_inter c Ss = ListPair.map (inter_sort classes) (dom c, Ss);
wenzelm@16881
   164
  in
wenzelm@16881
   165
    (case S of
wenzelm@16881
   166
      [] => sys_error "mg_domain"  (*don't know number of args!*)
wenzelm@16881
   167
    | c :: cs => fold dom_inter cs (dom c))
wenzelm@16881
   168
  end;
wenzelm@2956
   169
wenzelm@2956
   170
wenzelm@2990
   171
(* of_sort *)
wenzelm@2990
   172
wenzelm@14782
   173
fun of_sort (classes, arities) =
wenzelm@2990
   174
  let
wenzelm@2990
   175
    fun ofS (_, []) = true
wenzelm@14782
   176
      | ofS (TFree (_, S), S') = sort_le classes (S, S')
wenzelm@14782
   177
      | ofS (TVar (_, S), S') = sort_le classes (S, S')
wenzelm@2990
   178
      | ofS (Type (a, Ts), S) =
wenzelm@14782
   179
          let val Ss = mg_domain (classes, arities) a S in
wenzelm@2990
   180
            ListPair.all ofS (Ts, Ss)
wenzelm@7643
   181
          end handle DOMAIN _ => false;
wenzelm@2990
   182
  in ofS end;
wenzelm@2990
   183
wenzelm@2990
   184
wenzelm@2956
   185
wenzelm@7643
   186
(** witness_sorts **)
wenzelm@7643
   187
wenzelm@14828
   188
local
wenzelm@14828
   189
wenzelm@14828
   190
fun witness_aux (classes, arities) log_types hyps sorts =
wenzelm@7643
   191
  let
wenzelm@7643
   192
    val top_witn = (propT, []);
wenzelm@14782
   193
    fun le S1 S2 = sort_le classes (S1, S2);
skalberg@15531
   194
    fun get_solved S2 (T, S1) = if le S1 S2 then SOME (T, S2) else NONE;
skalberg@15531
   195
    fun get_hyp S2 S1 = if le S1 S2 then SOME (TFree ("'hyp", S1), S2) else NONE;
skalberg@15531
   196
    fun mg_dom t S = SOME (mg_domain (classes, arities) t S) handle DOMAIN _ => NONE;
wenzelm@7643
   197
skalberg@15531
   198
    fun witn_sort _ (solved_failed, []) = (solved_failed, SOME top_witn)
wenzelm@7643
   199
      | witn_sort path ((solved, failed), S) =
skalberg@15531
   200
          if exists (le S) failed then ((solved, failed), NONE)
wenzelm@7643
   201
          else
wenzelm@7643
   202
            (case get_first (get_solved S) solved of
skalberg@15531
   203
              SOME w => ((solved, failed), SOME w)
skalberg@15531
   204
            | NONE =>
wenzelm@7643
   205
                (case get_first (get_hyp S) hyps of
skalberg@15531
   206
                  SOME w => ((w :: solved, failed), SOME w)
skalberg@15531
   207
                | NONE => witn_types path log_types ((solved, failed), S)))
wenzelm@7643
   208
wenzelm@7643
   209
    and witn_sorts path x = foldl_map (witn_sort path) x
wenzelm@2956
   210
skalberg@15531
   211
    and witn_types _ [] ((solved, failed), S) = ((solved, S :: failed), NONE)
wenzelm@7643
   212
      | witn_types path (t :: ts) (solved_failed, S) =
wenzelm@7643
   213
          (case mg_dom t S of
skalberg@15531
   214
            SOME SS =>
wenzelm@7643
   215
              (*do not descend into stronger args (achieving termination)*)
wenzelm@7643
   216
              if exists (fn D => le D S orelse exists (le D) path) SS then
wenzelm@7643
   217
                witn_types path ts (solved_failed, S)
wenzelm@7643
   218
              else
wenzelm@7643
   219
                let val ((solved', failed'), ws) = witn_sorts (S :: path) (solved_failed, SS) in
wenzelm@17756
   220
                  if forall is_some ws then
wenzelm@18931
   221
                    let val w = (Type (t, map (#1 o the) ws), S)
skalberg@15531
   222
                    in ((w :: solved', failed'), SOME w) end
wenzelm@7643
   223
                  else witn_types path ts ((solved', failed'), S)
wenzelm@7643
   224
                end
skalberg@15531
   225
          | NONE => witn_types path ts (solved_failed, S));
wenzelm@7643
   226
wenzelm@7643
   227
  in witn_sorts [] (([], []), sorts) end;
wenzelm@7643
   228
wenzelm@14828
   229
fun str_of_sort [c] = c
wenzelm@14828
   230
  | str_of_sort cs = enclose "{" "}" (commas cs);
wenzelm@14828
   231
wenzelm@14828
   232
in
wenzelm@7643
   233
wenzelm@14782
   234
fun witness_sorts (classes, arities) log_types hyps sorts =
wenzelm@7643
   235
  let
wenzelm@16881
   236
    (*double check result of witness construction*)
skalberg@15531
   237
    fun check_result NONE = NONE
skalberg@15531
   238
      | check_result (SOME (T, S)) =
skalberg@15531
   239
          if of_sort (classes, arities) (T, S) then SOME (T, S)
wenzelm@14782
   240
          else sys_error ("Sorts.witness_sorts: bad witness for sort " ^ str_of_sort S);
wenzelm@19482
   241
  in map_filter check_result (#2 (witness_aux (classes, arities) log_types hyps sorts)) end;
wenzelm@2956
   242
wenzelm@2956
   243
end;
wenzelm@14828
   244
wenzelm@19514
   245
wenzelm@19514
   246
wenzelm@19514
   247
(** build sort algebras **)
wenzelm@19514
   248
wenzelm@19514
   249
(* classes *)
wenzelm@19514
   250
wenzelm@19514
   251
local
wenzelm@19514
   252
wenzelm@19514
   253
fun err_dup_classes cs =
wenzelm@19514
   254
  error ("Duplicate declaration of class(es): " ^ commas_quote cs);
wenzelm@19514
   255
wenzelm@19514
   256
fun err_cyclic_classes pp css =
wenzelm@19514
   257
  error (cat_lines (map (fn cs =>
wenzelm@19514
   258
    "Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css));
wenzelm@19514
   259
wenzelm@19514
   260
in
wenzelm@19514
   261
wenzelm@19514
   262
fun add_class pp (c, cs) classes =
wenzelm@19514
   263
  let
wenzelm@19514
   264
    val classes' = classes |> Graph.new_node (c, stamp ())
wenzelm@19514
   265
      handle Graph.DUP dup => err_dup_classes [dup];
wenzelm@19514
   266
    val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs)
wenzelm@19514
   267
      handle Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@19514
   268
  in classes'' end;
wenzelm@19514
   269
wenzelm@19514
   270
fun add_classrel pp rel classes =
wenzelm@19514
   271
  classes |> Graph.add_edge_trans_acyclic rel
wenzelm@19514
   272
    handle Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@19514
   273
wenzelm@19514
   274
fun merge_classes pp args : classes =
wenzelm@19514
   275
  Graph.merge_trans_acyclic (op =) args
wenzelm@19514
   276
    handle Graph.DUPS cs => err_dup_classes cs
wenzelm@19514
   277
        | Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@19514
   278
wenzelm@14828
   279
end;
wenzelm@19514
   280
wenzelm@19514
   281
wenzelm@19514
   282
(* arities *)
wenzelm@19514
   283
wenzelm@19514
   284
local
wenzelm@19514
   285
wenzelm@19514
   286
fun for_classes _ NONE = ""
wenzelm@19514
   287
  | for_classes pp (SOME (c1, c2)) =
wenzelm@19514
   288
      " for classes " ^ Pretty.string_of_classrel pp [c1, c2];
wenzelm@19514
   289
wenzelm@19514
   290
fun err_conflict pp t cc (c, Ss) (c', Ss') =
wenzelm@19514
   291
  error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n  " ^
wenzelm@19514
   292
    Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n  " ^
wenzelm@19514
   293
    Pretty.string_of_arity pp (t, Ss', [c']));
wenzelm@19514
   294
wenzelm@19514
   295
fun coregular pp C t (c, Ss) ars =
wenzelm@19514
   296
  let
wenzelm@19514
   297
    fun conflict (c', Ss') =
wenzelm@19514
   298
      if class_le C (c, c') andalso not (sorts_le C (Ss, Ss')) then
wenzelm@19514
   299
        SOME ((c, c'), (c', Ss'))
wenzelm@19514
   300
      else if class_le C (c', c) andalso not (sorts_le C (Ss', Ss)) then
wenzelm@19514
   301
        SOME ((c', c), (c', Ss'))
wenzelm@19514
   302
      else NONE;
wenzelm@19514
   303
  in
wenzelm@19514
   304
    (case get_first conflict ars of
wenzelm@19514
   305
      SOME ((c1, c2), (c', Ss')) => err_conflict pp t (SOME (c1, c2)) (c, Ss) (c', Ss')
wenzelm@19514
   306
    | NONE => (c, Ss) :: ars)
wenzelm@19514
   307
  end;
wenzelm@19514
   308
wenzelm@19514
   309
fun insert pp C t (c, Ss) ars =
wenzelm@19514
   310
  (case AList.lookup (op =) ars c of
wenzelm@19514
   311
    NONE => coregular pp C t (c, Ss) ars
wenzelm@19514
   312
  | SOME Ss' =>
wenzelm@19514
   313
      if sorts_le C (Ss, Ss') then ars
wenzelm@19514
   314
      else if sorts_le C (Ss', Ss)
wenzelm@19514
   315
      then coregular pp C t (c, Ss) (remove (op =) (c, Ss') ars)
wenzelm@19514
   316
      else err_conflict pp t NONE (c, Ss) (c, Ss'));
wenzelm@19514
   317
wenzelm@19514
   318
fun complete C (c, Ss) = map (rpair Ss) (Graph.all_succs C [c]);
wenzelm@19514
   319
wenzelm@19514
   320
in
wenzelm@19514
   321
wenzelm@19514
   322
fun add_arities pp classes (t, ars) arities =
wenzelm@19514
   323
  let val ars' =
wenzelm@19514
   324
    Symtab.lookup_list arities t
wenzelm@19514
   325
    |> fold_rev (fold_rev (insert pp classes t)) (map (complete classes) ars)
wenzelm@19514
   326
  in Symtab.update (t, ars') arities end;
wenzelm@19514
   327
wenzelm@19514
   328
fun add_arities_table pp classes = Symtab.fold (fn (t, ars) =>
wenzelm@19514
   329
  add_arities pp classes (t, map (apsnd (map (norm_sort classes))) ars));
wenzelm@19514
   330
wenzelm@19514
   331
fun rebuild_arities pp classes arities =
wenzelm@19514
   332
  Symtab.empty
wenzelm@19514
   333
  |> add_arities_table pp classes arities;
wenzelm@19514
   334
wenzelm@19514
   335
fun merge_arities pp classes (arities1, arities2) =
wenzelm@19514
   336
  Symtab.empty
wenzelm@19514
   337
  |> add_arities_table pp classes arities1
wenzelm@19514
   338
  |> add_arities_table pp classes arities2;
wenzelm@19514
   339
wenzelm@19514
   340
end;
wenzelm@19514
   341
wenzelm@19514
   342
end;