src/Pure/sorts.ML
author wenzelm
Tue May 02 00:33:40 2006 +0200 (2006-05-02)
changeset 19531 89970e06351f
parent 19529 690861f93d2b
child 19578 f93b7637a5e6
permissions -rw-r--r--
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@19529
     6
wenzelm@19529
     7
Classes denote (possibly empty) collections of types that are
wenzelm@19529
     8
partially ordered by class inclusion. They are represented
wenzelm@19529
     9
symbolically by strings.
wenzelm@19529
    10
wenzelm@19529
    11
Sorts are intersections of finitely many classes. They are represented
wenzelm@19529
    12
by lists of classes.  Normal forms of sorts are sorted lists of
wenzelm@19529
    13
minimal classes (wrt. current class inclusion).
wenzelm@2956
    14
*)
wenzelm@2956
    15
wenzelm@2956
    16
signature SORTS =
wenzelm@2956
    17
sig
wenzelm@16598
    18
  val eq_set: sort list * sort list -> bool
wenzelm@16598
    19
  val union: sort list -> sort list -> sort list
wenzelm@16598
    20
  val subtract: sort list -> sort list -> sort list
wenzelm@19463
    21
  val remove_sort: sort -> sort list -> sort list
wenzelm@16598
    22
  val insert_sort: sort -> sort list -> sort list
wenzelm@16598
    23
  val insert_typ: typ -> sort list -> sort list
wenzelm@16598
    24
  val insert_typs: typ list -> sort list -> sort list
wenzelm@16598
    25
  val insert_term: term -> sort list -> sort list
wenzelm@16598
    26
  val insert_terms: term list -> sort list -> sort list
wenzelm@14782
    27
  type classes
wenzelm@14782
    28
  type arities
wenzelm@14782
    29
  val class_eq: classes -> class * class -> bool
wenzelm@14782
    30
  val class_less: classes -> class * class -> bool
wenzelm@14782
    31
  val class_le: classes -> class * class -> bool
wenzelm@14782
    32
  val sort_eq: classes -> sort * sort -> bool
wenzelm@14782
    33
  val sort_le: classes -> sort * sort -> bool
wenzelm@14782
    34
  val sorts_le: classes -> sort list * sort list -> bool
wenzelm@14782
    35
  val inter_sort: classes -> sort * sort -> sort
wenzelm@14782
    36
  val norm_sort: classes -> sort -> sort
wenzelm@19514
    37
  val add_arities: Pretty.pp -> classes -> string * (class * sort list) list -> arities -> arities
wenzelm@19514
    38
  val rebuild_arities: Pretty.pp -> classes -> arities -> arities
wenzelm@19514
    39
  val merge_arities: Pretty.pp -> classes -> arities * arities -> arities
wenzelm@19514
    40
  val add_class: Pretty.pp -> class * class list -> classes -> classes
wenzelm@19514
    41
  val add_classrel: Pretty.pp -> class * class -> classes -> classes
wenzelm@19514
    42
  val merge_classes: Pretty.pp -> classes * classes -> classes
wenzelm@19529
    43
  exception DOMAIN of string * class
wenzelm@19529
    44
  val domain_error: Pretty.pp -> string * class -> 'a
wenzelm@19529
    45
  val mg_domain: classes * arities -> string -> sort -> sort list  (*exception DOMAIN*)
wenzelm@19529
    46
  val of_sort: classes * arities -> typ * sort -> bool
wenzelm@19529
    47
  val of_sort_derivation: Pretty.pp -> classes * arities ->
wenzelm@19529
    48
    {classrel: 'a * class -> class -> 'a,
wenzelm@19529
    49
     constructor: string -> ('a * class) list list -> class -> 'a,
wenzelm@19529
    50
     variable: typ -> ('a * class) list} -> typ * sort -> 'a list
wenzelm@19529
    51
  val witness_sorts: classes * arities -> string list ->
wenzelm@19529
    52
    sort list -> sort list -> (typ * sort) list
wenzelm@2956
    53
end;
wenzelm@2956
    54
wenzelm@2956
    55
structure Sorts: SORTS =
wenzelm@2956
    56
struct
wenzelm@2956
    57
wenzelm@19514
    58
wenzelm@19529
    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@19529
    85
wenzelm@19529
    86
(** order-sorted algebra **)
wenzelm@2956
    87
wenzelm@2956
    88
(*
wenzelm@14782
    89
  classes: graph representing class declarations together with proper
wenzelm@14782
    90
    subclass relation, which needs to be transitive and acyclic.
wenzelm@2956
    91
wenzelm@14782
    92
  arities: table of association lists of all type arities; (t, ars)
wenzelm@19531
    93
    means that type constructor t has the arities ars; an element
wenzelm@19531
    94
    (c, (c0, Ss)) of ars represents the arity t::(Ss)c being derived
wenzelm@19531
    95
    via c0 <= c.  "Coregularity" of the arities structure requires
wenzelm@19531
    96
    that for any two declarations t::(Ss1)c1 and t::(Ss2)c2 such that
wenzelm@19531
    97
    c1 <= c2 holds Ss1 <= Ss2.
wenzelm@2956
    98
*)
wenzelm@2956
    99
wenzelm@14782
   100
type classes = stamp Graph.T;
wenzelm@19524
   101
type arities = (class * (class * sort list)) list Symtab.table;
wenzelm@2956
   102
wenzelm@2956
   103
wenzelm@19529
   104
(* class relations *)
wenzelm@2956
   105
wenzelm@14782
   106
fun class_eq (_: classes) (c1, c2:class) = c1 = c2;
wenzelm@14782
   107
val class_less: classes -> class * class -> bool = Graph.is_edge;
wenzelm@14782
   108
fun class_le classes (c1, c2) = c1 = c2 orelse class_less classes (c1, c2);
wenzelm@2956
   109
wenzelm@2956
   110
wenzelm@19529
   111
(* sort relations *)
wenzelm@2956
   112
wenzelm@14782
   113
fun sort_le classes (S1, S2) =
haftmann@17155
   114
  forall (fn c2 => exists (fn c1 => class_le classes (c1, c2)) S1) S2;
wenzelm@2956
   115
wenzelm@14782
   116
fun sorts_le classes (Ss1, Ss2) =
wenzelm@14782
   117
  ListPair.all (sort_le classes) (Ss1, Ss2);
wenzelm@2956
   118
wenzelm@14782
   119
fun sort_eq classes (S1, S2) =
wenzelm@14782
   120
  sort_le classes (S1, S2) andalso sort_le classes (S2, S1);
wenzelm@2956
   121
wenzelm@2956
   122
wenzelm@19529
   123
(* intersection *)
wenzelm@2956
   124
wenzelm@16881
   125
fun inter_class classes c S =
wenzelm@2956
   126
  let
wenzelm@2956
   127
    fun intr [] = [c]
wenzelm@2956
   128
      | intr (S' as c' :: c's) =
wenzelm@14782
   129
          if class_le classes (c', c) then S'
wenzelm@14782
   130
          else if class_le classes (c, c') then intr c's
wenzelm@2956
   131
          else c' :: intr c's
wenzelm@2956
   132
  in intr S end;
wenzelm@2956
   133
wenzelm@16881
   134
fun inter_sort classes (S1, S2) =
wenzelm@16881
   135
  sort_strings (fold (inter_class classes) S1 S2);
wenzelm@2956
   136
wenzelm@2956
   137
wenzelm@19529
   138
(* normal forms *)
wenzelm@2956
   139
wenzelm@19529
   140
fun norm_sort _ [] = []
wenzelm@19529
   141
  | norm_sort _ (S as [_]) = S
wenzelm@19529
   142
  | norm_sort classes S =
wenzelm@19529
   143
      filter (fn c => not (exists (fn c' => class_less classes (c', c)) S)) S
wenzelm@19529
   144
      |> sort_distinct string_ord;
wenzelm@2990
   145
wenzelm@2990
   146
wenzelm@2956
   147
wenzelm@19529
   148
(** build algebras **)
wenzelm@19514
   149
wenzelm@19514
   150
(* classes *)
wenzelm@19514
   151
wenzelm@19514
   152
local
wenzelm@19514
   153
wenzelm@19514
   154
fun err_dup_classes cs =
wenzelm@19514
   155
  error ("Duplicate declaration of class(es): " ^ commas_quote cs);
wenzelm@19514
   156
wenzelm@19514
   157
fun err_cyclic_classes pp css =
wenzelm@19514
   158
  error (cat_lines (map (fn cs =>
wenzelm@19514
   159
    "Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css));
wenzelm@19514
   160
wenzelm@19514
   161
in
wenzelm@19514
   162
wenzelm@19514
   163
fun add_class pp (c, cs) classes =
wenzelm@19514
   164
  let
wenzelm@19514
   165
    val classes' = classes |> Graph.new_node (c, stamp ())
wenzelm@19514
   166
      handle Graph.DUP dup => err_dup_classes [dup];
wenzelm@19514
   167
    val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs)
wenzelm@19514
   168
      handle Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@19514
   169
  in classes'' end;
wenzelm@19514
   170
wenzelm@19514
   171
fun add_classrel pp rel classes =
wenzelm@19514
   172
  classes |> Graph.add_edge_trans_acyclic rel
wenzelm@19514
   173
    handle Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@19514
   174
wenzelm@19514
   175
fun merge_classes pp args : classes =
wenzelm@19514
   176
  Graph.merge_trans_acyclic (op =) args
wenzelm@19514
   177
    handle Graph.DUPS cs => err_dup_classes cs
wenzelm@19514
   178
        | Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@19514
   179
wenzelm@14828
   180
end;
wenzelm@19514
   181
wenzelm@19514
   182
wenzelm@19514
   183
(* arities *)
wenzelm@19514
   184
wenzelm@19514
   185
local
wenzelm@19514
   186
wenzelm@19514
   187
fun for_classes _ NONE = ""
wenzelm@19514
   188
  | for_classes pp (SOME (c1, c2)) =
wenzelm@19514
   189
      " for classes " ^ Pretty.string_of_classrel pp [c1, c2];
wenzelm@19514
   190
wenzelm@19514
   191
fun err_conflict pp t cc (c, Ss) (c', Ss') =
wenzelm@19514
   192
  error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n  " ^
wenzelm@19514
   193
    Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n  " ^
wenzelm@19514
   194
    Pretty.string_of_arity pp (t, Ss', [c']));
wenzelm@19514
   195
wenzelm@19524
   196
fun coregular pp C t (c, (c0, Ss)) ars =
wenzelm@19514
   197
  let
wenzelm@19524
   198
    fun conflict (c', (_, Ss')) =
wenzelm@19514
   199
      if class_le C (c, c') andalso not (sorts_le C (Ss, Ss')) then
wenzelm@19514
   200
        SOME ((c, c'), (c', Ss'))
wenzelm@19514
   201
      else if class_le C (c', c) andalso not (sorts_le C (Ss', Ss)) then
wenzelm@19514
   202
        SOME ((c', c), (c', Ss'))
wenzelm@19514
   203
      else NONE;
wenzelm@19514
   204
  in
wenzelm@19514
   205
    (case get_first conflict ars of
wenzelm@19514
   206
      SOME ((c1, c2), (c', Ss')) => err_conflict pp t (SOME (c1, c2)) (c, Ss) (c', Ss')
wenzelm@19524
   207
    | NONE => (c, (c0, Ss)) :: ars)
wenzelm@19514
   208
  end;
wenzelm@19514
   209
wenzelm@19524
   210
fun insert pp C t (c, (c0, Ss)) ars =
wenzelm@19514
   211
  (case AList.lookup (op =) ars c of
wenzelm@19524
   212
    NONE => coregular pp C t (c, (c0, Ss)) ars
wenzelm@19524
   213
  | SOME (_, Ss') =>
wenzelm@19514
   214
      if sorts_le C (Ss, Ss') then ars
wenzelm@19524
   215
      else if sorts_le C (Ss', Ss) then
wenzelm@19524
   216
        coregular pp C t (c, (c0, Ss))
wenzelm@19524
   217
          (filter_out (fn (c'', (_, Ss'')) => c = c'' andalso Ss'' = Ss') ars)
wenzelm@19514
   218
      else err_conflict pp t NONE (c, Ss) (c, Ss'));
wenzelm@19514
   219
wenzelm@19524
   220
fun complete C (c0, Ss) = map (rpair (c0, Ss)) (Graph.all_succs C [c0]);
wenzelm@19514
   221
wenzelm@19514
   222
in
wenzelm@19514
   223
wenzelm@19514
   224
fun add_arities pp classes (t, ars) arities =
wenzelm@19514
   225
  let val ars' =
wenzelm@19514
   226
    Symtab.lookup_list arities t
wenzelm@19514
   227
    |> fold_rev (fold_rev (insert pp classes t)) (map (complete classes) ars)
wenzelm@19514
   228
  in Symtab.update (t, ars') arities end;
wenzelm@19514
   229
wenzelm@19529
   230
fun add_arities_table pp classes =
wenzelm@19529
   231
  Symtab.fold (fn (t, ars) => add_arities pp classes (t, map snd ars));
wenzelm@19514
   232
wenzelm@19514
   233
fun rebuild_arities pp classes arities =
wenzelm@19514
   234
  Symtab.empty
wenzelm@19514
   235
  |> add_arities_table pp classes arities;
wenzelm@19514
   236
wenzelm@19514
   237
fun merge_arities pp classes (arities1, arities2) =
wenzelm@19514
   238
  Symtab.empty
wenzelm@19514
   239
  |> add_arities_table pp classes arities1
wenzelm@19514
   240
  |> add_arities_table pp classes arities2;
wenzelm@19514
   241
wenzelm@19514
   242
end;
wenzelm@19514
   243
wenzelm@19529
   244
wenzelm@19529
   245
wenzelm@19529
   246
(** sorts of types **)
wenzelm@19529
   247
wenzelm@19529
   248
(* mg_domain *)
wenzelm@19529
   249
wenzelm@19529
   250
exception DOMAIN of string * class;
wenzelm@19529
   251
wenzelm@19529
   252
fun domain_error pp (a, c) =
wenzelm@19529
   253
  error ("No way to get " ^ Pretty.string_of_arity pp (a, [], [c]));
wenzelm@19529
   254
wenzelm@19529
   255
fun mg_domain (classes, arities) a S =
wenzelm@19529
   256
  let
wenzelm@19529
   257
    fun dom c =
wenzelm@19529
   258
      (case AList.lookup (op =) (Symtab.lookup_list arities a) c of
wenzelm@19529
   259
        NONE => raise DOMAIN (a, c)
wenzelm@19529
   260
      | SOME (_, Ss) => Ss);
wenzelm@19529
   261
    fun dom_inter c Ss = ListPair.map (inter_sort classes) (dom c, Ss);
wenzelm@19529
   262
  in
wenzelm@19529
   263
    (case S of
wenzelm@19529
   264
      [] => raise Fail "Unknown domain of empty intersection"
wenzelm@19529
   265
    | c :: cs => fold dom_inter cs (dom c))
wenzelm@19529
   266
  end;
wenzelm@19529
   267
wenzelm@19529
   268
wenzelm@19529
   269
(* of_sort *)
wenzelm@19529
   270
wenzelm@19529
   271
fun of_sort (classes, arities) =
wenzelm@19529
   272
  let
wenzelm@19529
   273
    fun ofS (_, []) = true
wenzelm@19529
   274
      | ofS (TFree (_, S), S') = sort_le classes (S, S')
wenzelm@19529
   275
      | ofS (TVar (_, S), S') = sort_le classes (S, S')
wenzelm@19529
   276
      | ofS (Type (a, Ts), S) =
wenzelm@19529
   277
          let val Ss = mg_domain (classes, arities) a S in
wenzelm@19529
   278
            ListPair.all ofS (Ts, Ss)
wenzelm@19529
   279
          end handle DOMAIN _ => false;
wenzelm@19529
   280
  in ofS end;
wenzelm@19529
   281
wenzelm@19529
   282
wenzelm@19529
   283
(* of_sort_derivation *)
wenzelm@19529
   284
wenzelm@19529
   285
fun of_sort_derivation pp (classes, arities) {classrel, constructor, variable} =
wenzelm@19529
   286
  let
wenzelm@19529
   287
    fun weaken (x, c1) c2 = if c1 = c2 then x else classrel (x, c1) c2;
wenzelm@19529
   288
    fun weakens S1 S2 = S2 |> map (fn c2 =>
wenzelm@19529
   289
      (case S1 |> find_first (fn (_, c1) => class_le classes (c1, c2)) of
wenzelm@19529
   290
        SOME d1 => weaken d1 c2
wenzelm@19529
   291
      | NONE => error ("Cannot derive subsort relation " ^
wenzelm@19529
   292
          Pretty.string_of_sort pp (map #2 S1) ^ " < " ^ Pretty.string_of_sort pp S2)));
wenzelm@19529
   293
wenzelm@19529
   294
    fun derive _ [] = []
wenzelm@19529
   295
      | derive (Type (a, Ts)) S =
wenzelm@19529
   296
          let
wenzelm@19529
   297
            val Ss = mg_domain (classes, arities) a S
wenzelm@19529
   298
              handle DOMAIN d => domain_error pp d;
wenzelm@19529
   299
            val dom = map2 (fn T => fn S => derive T S ~~ S) Ts Ss;
wenzelm@19529
   300
          in
wenzelm@19529
   301
            S |> map (fn c =>
wenzelm@19529
   302
              let
wenzelm@19529
   303
                val (c0, Ss') = the (AList.lookup (op =) (Symtab.lookup_list arities a) c);
wenzelm@19529
   304
                val dom' = map2 (fn d => fn S' => weakens d S' ~~ S') dom Ss';
wenzelm@19529
   305
              in weaken (constructor a dom' c0, c0) c end)
wenzelm@19529
   306
          end
wenzelm@19529
   307
      | derive T S = weakens (variable T) S;
wenzelm@19529
   308
  in uncurry derive end;
wenzelm@19529
   309
wenzelm@19529
   310
wenzelm@19529
   311
(* witness_sorts *)
wenzelm@19529
   312
wenzelm@19529
   313
local
wenzelm@19529
   314
wenzelm@19529
   315
fun witness_aux (classes, arities) log_types hyps sorts =
wenzelm@19529
   316
  let
wenzelm@19529
   317
    val top_witn = (propT, []);
wenzelm@19529
   318
    fun le S1 S2 = sort_le classes (S1, S2);
wenzelm@19529
   319
    fun get_solved S2 (T, S1) = if le S1 S2 then SOME (T, S2) else NONE;
wenzelm@19529
   320
    fun get_hyp S2 S1 = if le S1 S2 then SOME (TFree ("'hyp", S1), S2) else NONE;
wenzelm@19529
   321
    fun mg_dom t S = SOME (mg_domain (classes, arities) t S) handle DOMAIN _ => NONE;
wenzelm@19529
   322
wenzelm@19529
   323
    fun witn_sort _ (solved_failed, []) = (solved_failed, SOME top_witn)
wenzelm@19529
   324
      | witn_sort path ((solved, failed), S) =
wenzelm@19529
   325
          if exists (le S) failed then ((solved, failed), NONE)
wenzelm@19529
   326
          else
wenzelm@19529
   327
            (case get_first (get_solved S) solved of
wenzelm@19529
   328
              SOME w => ((solved, failed), SOME w)
wenzelm@19529
   329
            | NONE =>
wenzelm@19529
   330
                (case get_first (get_hyp S) hyps of
wenzelm@19529
   331
                  SOME w => ((w :: solved, failed), SOME w)
wenzelm@19529
   332
                | NONE => witn_types path log_types ((solved, failed), S)))
wenzelm@19529
   333
wenzelm@19529
   334
    and witn_sorts path x = foldl_map (witn_sort path) x
wenzelm@19529
   335
wenzelm@19529
   336
    and witn_types _ [] ((solved, failed), S) = ((solved, S :: failed), NONE)
wenzelm@19529
   337
      | witn_types path (t :: ts) (solved_failed, S) =
wenzelm@19529
   338
          (case mg_dom t S of
wenzelm@19529
   339
            SOME SS =>
wenzelm@19529
   340
              (*do not descend into stronger args (achieving termination)*)
wenzelm@19529
   341
              if exists (fn D => le D S orelse exists (le D) path) SS then
wenzelm@19529
   342
                witn_types path ts (solved_failed, S)
wenzelm@19529
   343
              else
wenzelm@19529
   344
                let val ((solved', failed'), ws) = witn_sorts (S :: path) (solved_failed, SS) in
wenzelm@19529
   345
                  if forall is_some ws then
wenzelm@19529
   346
                    let val w = (Type (t, map (#1 o the) ws), S)
wenzelm@19529
   347
                    in ((w :: solved', failed'), SOME w) end
wenzelm@19529
   348
                  else witn_types path ts ((solved', failed'), S)
wenzelm@19529
   349
                end
wenzelm@19529
   350
          | NONE => witn_types path ts (solved_failed, S));
wenzelm@19529
   351
wenzelm@19529
   352
  in witn_sorts [] (([], []), sorts) end;
wenzelm@19529
   353
wenzelm@19529
   354
fun str_of_sort [c] = c
wenzelm@19529
   355
  | str_of_sort cs = enclose "{" "}" (commas cs);
wenzelm@19529
   356
wenzelm@19529
   357
in
wenzelm@19529
   358
wenzelm@19529
   359
fun witness_sorts (classes, arities) log_types hyps sorts =
wenzelm@19529
   360
  let
wenzelm@19529
   361
    fun double_check_result NONE = NONE
wenzelm@19529
   362
      | double_check_result (SOME (T, S)) =
wenzelm@19529
   363
          if of_sort (classes, arities) (T, S) then SOME (T, S)
wenzelm@19529
   364
          else sys_error ("Sorts.witness_sorts: bad witness for sort " ^ str_of_sort S);
wenzelm@19529
   365
  in map_filter double_check_result (#2 (witness_aux (classes, arities) log_types hyps sorts)) end;
wenzelm@19529
   366
wenzelm@19514
   367
end;
wenzelm@19529
   368
wenzelm@19529
   369
end;