src/Pure/sorts.ML
author wenzelm
Sun Apr 11 14:06:35 2010 +0200 (2010-04-11)
changeset 36105 42c37cf849cd
parent 36104 fecb587a1d0e
child 36328 4d9deabf6474
permissions -rw-r--r--
modernized datatype constructors;
wenzelm@2956
     1
(*  Title:      Pure/sorts.ML
wenzelm@2956
     2
    Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
wenzelm@2956
     3
wenzelm@19514
     4
The order-sorted algebra of type classes.
wenzelm@19529
     5
wenzelm@19529
     6
Classes denote (possibly empty) collections of types that are
wenzelm@19529
     7
partially ordered by class inclusion. They are represented
wenzelm@19529
     8
symbolically by strings.
wenzelm@19529
     9
wenzelm@19529
    10
Sorts are intersections of finitely many classes. They are represented
wenzelm@19529
    11
by lists of classes.  Normal forms of sorts are sorted lists of
wenzelm@19529
    12
minimal classes (wrt. current class inclusion).
wenzelm@2956
    13
*)
wenzelm@2956
    14
wenzelm@2956
    15
signature SORTS =
wenzelm@2956
    16
sig
wenzelm@28623
    17
  val make: sort list -> sort OrdList.T
wenzelm@28374
    18
  val subset: sort OrdList.T * sort OrdList.T -> bool
wenzelm@28354
    19
  val union: sort OrdList.T -> sort OrdList.T -> sort OrdList.T
wenzelm@28354
    20
  val subtract: sort OrdList.T -> sort OrdList.T -> sort OrdList.T
wenzelm@28354
    21
  val remove_sort: sort -> sort OrdList.T -> sort OrdList.T
wenzelm@28354
    22
  val insert_sort: sort -> sort OrdList.T -> sort OrdList.T
wenzelm@28354
    23
  val insert_typ: typ -> sort OrdList.T -> sort OrdList.T
wenzelm@28354
    24
  val insert_typs: typ list -> sort OrdList.T -> sort OrdList.T
wenzelm@28354
    25
  val insert_term: term -> sort OrdList.T -> sort OrdList.T
wenzelm@28354
    26
  val insert_terms: term list -> sort OrdList.T -> sort OrdList.T
wenzelm@19645
    27
  type algebra
wenzelm@19645
    28
  val rep_algebra: algebra ->
wenzelm@20573
    29
   {classes: serial Graph.T,
wenzelm@19645
    30
    arities: (class * (class * sort list)) list Symtab.table}
wenzelm@21933
    31
  val all_classes: algebra -> class list
wenzelm@19645
    32
  val super_classes: algebra -> class -> class list
wenzelm@19645
    33
  val class_less: algebra -> class * class -> bool
wenzelm@19645
    34
  val class_le: algebra -> class * class -> bool
wenzelm@19645
    35
  val sort_eq: algebra -> sort * sort -> bool
wenzelm@19645
    36
  val sort_le: algebra -> sort * sort -> bool
wenzelm@19645
    37
  val sorts_le: algebra -> sort list * sort list -> bool
wenzelm@19645
    38
  val inter_sort: algebra -> sort * sort -> sort
wenzelm@24732
    39
  val minimize_sort: algebra -> sort -> sort
wenzelm@24732
    40
  val complete_sort: algebra -> sort -> sort
wenzelm@28623
    41
  val minimal_sorts: algebra -> sort list -> sort OrdList.T
wenzelm@19645
    42
  val certify_class: algebra -> class -> class    (*exception TYPE*)
wenzelm@19645
    43
  val certify_sort: algebra -> sort -> sort       (*exception TYPE*)
wenzelm@19645
    44
  val add_class: Pretty.pp -> class * class list -> algebra -> algebra
wenzelm@19645
    45
  val add_classrel: Pretty.pp -> class * class -> algebra -> algebra
wenzelm@19645
    46
  val add_arities: Pretty.pp -> string * (class * sort list) list -> algebra -> algebra
wenzelm@19645
    47
  val empty_algebra: algebra
wenzelm@19645
    48
  val merge_algebra: Pretty.pp -> algebra * algebra -> algebra
haftmann@30060
    49
  val subalgebra: Pretty.pp -> (class -> bool) -> (class * string -> sort list option)
haftmann@22181
    50
    -> algebra -> (sort -> sort) * algebra
wenzelm@19578
    51
  type class_error
wenzelm@26639
    52
  val class_error: Pretty.pp -> class_error -> string
wenzelm@19578
    53
  exception CLASS_ERROR of class_error
wenzelm@19645
    54
  val mg_domain: algebra -> string -> sort -> sort list   (*exception CLASS_ERROR*)
haftmann@28665
    55
  val meet_sort: algebra -> typ * sort
haftmann@28665
    56
    -> sort Vartab.table -> sort Vartab.table   (*exception CLASS_ERROR*)
haftmann@28665
    57
  val meet_sort_typ: algebra -> typ * sort -> typ -> typ   (*exception CLASS_ERROR*)
wenzelm@19645
    58
  val of_sort: algebra -> typ * sort -> bool
wenzelm@32791
    59
  val of_sort_derivation: algebra ->
wenzelm@36102
    60
    {class_relation: typ -> 'a * class -> class -> 'a,
wenzelm@36102
    61
     type_constructor: string * typ list -> ('a * class) list list -> class -> 'a,
wenzelm@22570
    62
     type_variable: typ -> ('a * class) list} ->
wenzelm@19584
    63
    typ * sort -> 'a list   (*exception CLASS_ERROR*)
wenzelm@35961
    64
  val classrel_derivation: algebra ->
wenzelm@35961
    65
    ('a * class -> class -> 'a) -> 'a * class -> class -> 'a  (*exception CLASS_ERROR*)
wenzelm@31946
    66
  val witness_sorts: algebra -> string list -> (typ * sort) list -> sort list -> (typ * sort) list
wenzelm@2956
    67
end;
wenzelm@2956
    68
wenzelm@20573
    69
structure Sorts: SORTS =
wenzelm@2956
    70
struct
wenzelm@2956
    71
wenzelm@19514
    72
wenzelm@19529
    73
(** ordered lists of sorts **)
wenzelm@14782
    74
wenzelm@35408
    75
val make = OrdList.make Term_Ord.sort_ord;
wenzelm@35408
    76
val subset = OrdList.subset Term_Ord.sort_ord;
wenzelm@35408
    77
val union = OrdList.union Term_Ord.sort_ord;
wenzelm@35408
    78
val subtract = OrdList.subtract Term_Ord.sort_ord;
wenzelm@14782
    79
wenzelm@35408
    80
val remove_sort = OrdList.remove Term_Ord.sort_ord;
wenzelm@35408
    81
val insert_sort = OrdList.insert Term_Ord.sort_ord;
wenzelm@14782
    82
wenzelm@16598
    83
fun insert_typ (TFree (_, S)) Ss = insert_sort S Ss
wenzelm@16598
    84
  | insert_typ (TVar (_, S)) Ss = insert_sort S Ss
wenzelm@16598
    85
  | insert_typ (Type (_, Ts)) Ss = insert_typs Ts Ss
wenzelm@16598
    86
and insert_typs [] Ss = Ss
wenzelm@16598
    87
  | insert_typs (T :: Ts) Ss = insert_typs Ts (insert_typ T Ss);
wenzelm@14782
    88
wenzelm@16598
    89
fun insert_term (Const (_, T)) Ss = insert_typ T Ss
wenzelm@16598
    90
  | insert_term (Free (_, T)) Ss = insert_typ T Ss
wenzelm@16598
    91
  | insert_term (Var (_, T)) Ss = insert_typ T Ss
wenzelm@16598
    92
  | insert_term (Bound _) Ss = Ss
wenzelm@16598
    93
  | insert_term (Abs (_, T, t)) Ss = insert_term t (insert_typ T Ss)
wenzelm@16598
    94
  | insert_term (t $ u) Ss = insert_term t (insert_term u Ss);
wenzelm@14782
    95
wenzelm@16598
    96
fun insert_terms [] Ss = Ss
wenzelm@16598
    97
  | insert_terms (t :: ts) Ss = insert_terms ts (insert_term t Ss);
wenzelm@14782
    98
wenzelm@14782
    99
wenzelm@19529
   100
wenzelm@19529
   101
(** order-sorted algebra **)
wenzelm@2956
   102
wenzelm@2956
   103
(*
wenzelm@14782
   104
  classes: graph representing class declarations together with proper
wenzelm@14782
   105
    subclass relation, which needs to be transitive and acyclic.
wenzelm@2956
   106
wenzelm@14782
   107
  arities: table of association lists of all type arities; (t, ars)
wenzelm@19531
   108
    means that type constructor t has the arities ars; an element
wenzelm@19531
   109
    (c, (c0, Ss)) of ars represents the arity t::(Ss)c being derived
wenzelm@19531
   110
    via c0 <= c.  "Coregularity" of the arities structure requires
wenzelm@19531
   111
    that for any two declarations t::(Ss1)c1 and t::(Ss2)c2 such that
wenzelm@19531
   112
    c1 <= c2 holds Ss1 <= Ss2.
wenzelm@2956
   113
*)
wenzelm@2956
   114
wenzelm@19645
   115
datatype algebra = Algebra of
wenzelm@20573
   116
 {classes: serial Graph.T,
wenzelm@19645
   117
  arities: (class * (class * sort list)) list Symtab.table};
wenzelm@19645
   118
wenzelm@19645
   119
fun rep_algebra (Algebra args) = args;
wenzelm@19645
   120
wenzelm@19645
   121
val classes_of = #classes o rep_algebra;
wenzelm@19645
   122
val arities_of = #arities o rep_algebra;
wenzelm@19645
   123
wenzelm@19645
   124
fun make_algebra (classes, arities) =
wenzelm@19645
   125
  Algebra {classes = classes, arities = arities};
wenzelm@19645
   126
wenzelm@19645
   127
fun map_classes f (Algebra {classes, arities}) = make_algebra (f classes, arities);
wenzelm@19645
   128
fun map_arities f (Algebra {classes, arities}) = make_algebra (classes, f arities);
wenzelm@19645
   129
wenzelm@19645
   130
wenzelm@19645
   131
(* classes *)
wenzelm@19645
   132
wenzelm@21933
   133
fun all_classes (Algebra {classes, ...}) = Graph.all_preds classes (Graph.maximals classes);
wenzelm@21933
   134
wenzelm@19645
   135
val super_classes = Graph.imm_succs o classes_of;
wenzelm@2956
   136
wenzelm@2956
   137
wenzelm@19529
   138
(* class relations *)
wenzelm@2956
   139
wenzelm@19645
   140
val class_less = Graph.is_edge o classes_of;
wenzelm@19645
   141
fun class_le algebra (c1, c2) = c1 = c2 orelse class_less algebra (c1, c2);
wenzelm@2956
   142
wenzelm@2956
   143
wenzelm@19529
   144
(* sort relations *)
wenzelm@2956
   145
wenzelm@19645
   146
fun sort_le algebra (S1, S2) =
wenzelm@23585
   147
  S1 = S2 orelse forall (fn c2 => exists (fn c1 => class_le algebra (c1, c2)) S1) S2;
wenzelm@2956
   148
wenzelm@19645
   149
fun sorts_le algebra (Ss1, Ss2) =
wenzelm@19645
   150
  ListPair.all (sort_le algebra) (Ss1, Ss2);
wenzelm@2956
   151
wenzelm@19645
   152
fun sort_eq algebra (S1, S2) =
wenzelm@19645
   153
  sort_le algebra (S1, S2) andalso sort_le algebra (S2, S1);
wenzelm@2956
   154
wenzelm@2956
   155
wenzelm@19529
   156
(* intersection *)
wenzelm@2956
   157
wenzelm@19645
   158
fun inter_class algebra c S =
wenzelm@2956
   159
  let
wenzelm@2956
   160
    fun intr [] = [c]
wenzelm@2956
   161
      | intr (S' as c' :: c's) =
wenzelm@19645
   162
          if class_le algebra (c', c) then S'
wenzelm@19645
   163
          else if class_le algebra (c, c') then intr c's
wenzelm@2956
   164
          else c' :: intr c's
wenzelm@2956
   165
  in intr S end;
wenzelm@2956
   166
wenzelm@19645
   167
fun inter_sort algebra (S1, S2) =
wenzelm@19645
   168
  sort_strings (fold (inter_class algebra) S1 S2);
wenzelm@2956
   169
wenzelm@2956
   170
wenzelm@24732
   171
(* normal forms *)
wenzelm@2956
   172
wenzelm@24732
   173
fun minimize_sort _ [] = []
wenzelm@24732
   174
  | minimize_sort _ (S as [_]) = S
wenzelm@24732
   175
  | minimize_sort algebra S =
wenzelm@19645
   176
      filter (fn c => not (exists (fn c' => class_less algebra (c', c)) S)) S
wenzelm@19529
   177
      |> sort_distinct string_ord;
wenzelm@2990
   178
wenzelm@24732
   179
fun complete_sort algebra =
wenzelm@24732
   180
  Graph.all_succs (classes_of algebra) o minimize_sort algebra;
wenzelm@24732
   181
wenzelm@28623
   182
fun minimal_sorts algebra raw_sorts =
wenzelm@28623
   183
  let
wenzelm@28623
   184
    fun le S1 S2 = sort_le algebra (S1, S2);
wenzelm@28623
   185
    val sorts = make (map (minimize_sort algebra) raw_sorts);
wenzelm@28623
   186
  in sorts |> filter_out (fn S => exists (fn S' => le S' S andalso not (le S S')) sorts) end;
wenzelm@28623
   187
wenzelm@2990
   188
wenzelm@19645
   189
(* certify *)
wenzelm@19645
   190
wenzelm@19645
   191
fun certify_class algebra c =
wenzelm@19645
   192
  if can (Graph.get_node (classes_of algebra)) c then c
wenzelm@19645
   193
  else raise TYPE ("Undeclared class: " ^ quote c, [], []);
wenzelm@19645
   194
wenzelm@24732
   195
fun certify_sort classes = minimize_sort classes o map (certify_class classes);
wenzelm@19645
   196
wenzelm@19645
   197
wenzelm@2956
   198
wenzelm@19529
   199
(** build algebras **)
wenzelm@19514
   200
wenzelm@19514
   201
(* classes *)
wenzelm@19514
   202
wenzelm@23655
   203
fun err_dup_class c = error ("Duplicate declaration of class: " ^ quote c);
wenzelm@19514
   204
wenzelm@19514
   205
fun err_cyclic_classes pp css =
wenzelm@19514
   206
  error (cat_lines (map (fn cs =>
wenzelm@19514
   207
    "Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css));
wenzelm@19514
   208
wenzelm@19645
   209
fun add_class pp (c, cs) = map_classes (fn classes =>
wenzelm@19514
   210
  let
wenzelm@20573
   211
    val classes' = classes |> Graph.new_node (c, serial ())
wenzelm@23655
   212
      handle Graph.DUP dup => err_dup_class dup;
wenzelm@19514
   213
    val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs)
wenzelm@19514
   214
      handle Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@19645
   215
  in classes'' end);
wenzelm@19514
   216
wenzelm@19514
   217
wenzelm@19514
   218
(* arities *)
wenzelm@19514
   219
wenzelm@19514
   220
local
wenzelm@19514
   221
wenzelm@19514
   222
fun for_classes _ NONE = ""
wenzelm@19514
   223
  | for_classes pp (SOME (c1, c2)) =
wenzelm@19514
   224
      " for classes " ^ Pretty.string_of_classrel pp [c1, c2];
wenzelm@19514
   225
wenzelm@19514
   226
fun err_conflict pp t cc (c, Ss) (c', Ss') =
wenzelm@19514
   227
  error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n  " ^
wenzelm@19514
   228
    Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n  " ^
wenzelm@19514
   229
    Pretty.string_of_arity pp (t, Ss', [c']));
wenzelm@19514
   230
wenzelm@19645
   231
fun coregular pp algebra t (c, (c0, Ss)) ars =
wenzelm@19514
   232
  let
wenzelm@19524
   233
    fun conflict (c', (_, Ss')) =
wenzelm@19645
   234
      if class_le algebra (c, c') andalso not (sorts_le algebra (Ss, Ss')) then
wenzelm@19514
   235
        SOME ((c, c'), (c', Ss'))
wenzelm@19645
   236
      else if class_le algebra (c', c) andalso not (sorts_le algebra (Ss', Ss)) then
wenzelm@19514
   237
        SOME ((c', c), (c', Ss'))
wenzelm@19514
   238
      else NONE;
wenzelm@19514
   239
  in
wenzelm@19514
   240
    (case get_first conflict ars of
wenzelm@19514
   241
      SOME ((c1, c2), (c', Ss')) => err_conflict pp t (SOME (c1, c2)) (c, Ss) (c', Ss')
wenzelm@19524
   242
    | NONE => (c, (c0, Ss)) :: ars)
wenzelm@19514
   243
  end;
wenzelm@19514
   244
wenzelm@19645
   245
fun complete algebra (c0, Ss) = map (rpair (c0, Ss)) (c0 :: super_classes algebra c0);
wenzelm@19645
   246
wenzelm@19645
   247
fun insert pp algebra t (c, (c0, Ss)) ars =
wenzelm@19514
   248
  (case AList.lookup (op =) ars c of
wenzelm@19645
   249
    NONE => coregular pp algebra t (c, (c0, Ss)) ars
wenzelm@19524
   250
  | SOME (_, Ss') =>
wenzelm@19645
   251
      if sorts_le algebra (Ss, Ss') then ars
wenzelm@19645
   252
      else if sorts_le algebra (Ss', Ss) then
wenzelm@19645
   253
        coregular pp algebra t (c, (c0, Ss))
wenzelm@19524
   254
          (filter_out (fn (c'', (_, Ss'')) => c = c'' andalso Ss'' = Ss') ars)
wenzelm@19514
   255
      else err_conflict pp t NONE (c, Ss) (c, Ss'));
wenzelm@19514
   256
wenzelm@35975
   257
in
wenzelm@35975
   258
wenzelm@35975
   259
fun insert_ars pp algebra t = fold_rev (insert pp algebra t);
wenzelm@35975
   260
wenzelm@35975
   261
fun insert_complete_ars pp algebra (t, ars) arities =
wenzelm@19645
   262
  let val ars' =
wenzelm@19645
   263
    Symtab.lookup_list arities t
wenzelm@35975
   264
    |> fold_rev (insert_ars pp algebra t) (map (complete algebra) ars);
wenzelm@19645
   265
  in Symtab.update (t, ars') arities end;
wenzelm@19514
   266
wenzelm@35975
   267
fun add_arities pp arg algebra =
wenzelm@35975
   268
  algebra |> map_arities (insert_complete_ars pp algebra arg);
wenzelm@19514
   269
wenzelm@19645
   270
fun add_arities_table pp algebra =
wenzelm@35975
   271
  Symtab.fold (fn (t, ars) => insert_complete_ars pp algebra (t, map snd ars));
wenzelm@19514
   272
wenzelm@19514
   273
end;
wenzelm@19514
   274
wenzelm@19529
   275
wenzelm@19645
   276
(* classrel *)
wenzelm@19645
   277
wenzelm@19645
   278
fun rebuild_arities pp algebra = algebra |> map_arities (fn arities =>
wenzelm@19645
   279
  Symtab.empty
wenzelm@19645
   280
  |> add_arities_table pp algebra arities);
wenzelm@19645
   281
wenzelm@19645
   282
fun add_classrel pp rel = rebuild_arities pp o map_classes (fn classes =>
wenzelm@19645
   283
  classes |> Graph.add_edge_trans_acyclic rel
wenzelm@19645
   284
    handle Graph.CYCLES css => err_cyclic_classes pp css);
wenzelm@19645
   285
wenzelm@19645
   286
wenzelm@19645
   287
(* empty and merge *)
wenzelm@19645
   288
wenzelm@19645
   289
val empty_algebra = make_algebra (Graph.empty, Symtab.empty);
wenzelm@19645
   290
wenzelm@19645
   291
fun merge_algebra pp
wenzelm@19645
   292
   (Algebra {classes = classes1, arities = arities1},
wenzelm@19645
   293
    Algebra {classes = classes2, arities = arities2}) =
wenzelm@19645
   294
  let
wenzelm@19645
   295
    val classes' = Graph.merge_trans_acyclic (op =) (classes1, classes2)
wenzelm@23655
   296
      handle Graph.DUP c => err_dup_class c
wenzelm@35975
   297
        | Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@19645
   298
    val algebra0 = make_algebra (classes', Symtab.empty);
wenzelm@35975
   299
    val arities' =
wenzelm@35975
   300
      (case (pointer_eq (classes1, classes2), pointer_eq (arities1, arities2)) of
wenzelm@35975
   301
        (true, true) => arities1
wenzelm@35975
   302
      | (true, false) =>  (*no completion*)
wenzelm@35975
   303
          (arities1, arities2) |> Symtab.join (fn t => fn (ars1, ars2) =>
wenzelm@35975
   304
            if pointer_eq (ars1, ars2) then raise Symtab.SAME
wenzelm@35975
   305
            else insert_ars pp algebra0 t ars2 ars1)
wenzelm@35975
   306
      | (false, true) =>  (*unary completion*)
wenzelm@35975
   307
          Symtab.empty
wenzelm@35975
   308
          |> add_arities_table pp algebra0 arities1
wenzelm@35975
   309
      | (false, false) => (*binary completion*)
wenzelm@35975
   310
          Symtab.empty
wenzelm@35975
   311
          |> add_arities_table pp algebra0 arities1
wenzelm@35975
   312
          |> add_arities_table pp algebra0 arities2);
wenzelm@19645
   313
  in make_algebra (classes', arities') end;
wenzelm@19645
   314
wenzelm@21933
   315
haftmann@28922
   316
(* algebra projections *)
haftmann@28922
   317
haftmann@22181
   318
fun subalgebra pp P sargs (algebra as Algebra {classes, arities}) =
haftmann@19952
   319
  let
wenzelm@24732
   320
    val restrict_sort = minimize_sort algebra o filter P o Graph.all_succs classes;
haftmann@22181
   321
    fun restrict_arity tyco (c, (_, Ss)) =
haftmann@30060
   322
      if P c then case sargs (c, tyco)
haftmann@30060
   323
       of SOME sorts => SOME (c, (c, Ss |> map2 (curry (inter_sort algebra)) sorts
haftmann@22181
   324
          |> map restrict_sort))
haftmann@30062
   325
        | NONE => NONE
haftmann@22181
   326
      else NONE;
wenzelm@21933
   327
    val classes' = classes |> Graph.subgraph P;
haftmann@22181
   328
    val arities' = arities |> Symtab.map' (map_filter o restrict_arity);
wenzelm@21933
   329
  in (restrict_sort, rebuild_arities pp (make_algebra (classes', arities'))) end;
haftmann@20465
   330
wenzelm@19645
   331
wenzelm@19529
   332
wenzelm@19529
   333
(** sorts of types **)
wenzelm@19529
   334
wenzelm@35961
   335
(* errors -- performance tuning via delayed message composition *)
wenzelm@19578
   336
wenzelm@26639
   337
datatype class_error =
wenzelm@36105
   338
  No_Classrel of class * class |
wenzelm@36105
   339
  No_Arity of string * class |
wenzelm@36105
   340
  No_Subsort of sort * sort;
wenzelm@19529
   341
wenzelm@36105
   342
fun class_error pp (No_Classrel (c1, c2)) =
haftmann@22196
   343
      "No class relation " ^ Pretty.string_of_classrel pp [c1, c2]
wenzelm@36105
   344
  | class_error pp (No_Arity (a, c)) =
haftmann@26326
   345
      "No type arity " ^ Pretty.string_of_arity pp (a, [], [c])
wenzelm@36105
   346
  | class_error pp (No_Subsort (S1, S2)) =
haftmann@26994
   347
     "Cannot derive subsort relation " ^ Pretty.string_of_sort pp S1
haftmann@26994
   348
       ^ " < " ^ Pretty.string_of_sort pp S2;
wenzelm@19529
   349
wenzelm@19578
   350
exception CLASS_ERROR of class_error;
wenzelm@19578
   351
wenzelm@19578
   352
wenzelm@19578
   353
(* mg_domain *)
wenzelm@19529
   354
wenzelm@19645
   355
fun mg_domain algebra a S =
wenzelm@19529
   356
  let
wenzelm@19645
   357
    val arities = arities_of algebra;
wenzelm@19529
   358
    fun dom c =
wenzelm@19529
   359
      (case AList.lookup (op =) (Symtab.lookup_list arities a) c of
wenzelm@36105
   360
        NONE => raise CLASS_ERROR (No_Arity (a, c))
wenzelm@19529
   361
      | SOME (_, Ss) => Ss);
wenzelm@19645
   362
    fun dom_inter c Ss = ListPair.map (inter_sort algebra) (dom c, Ss);
wenzelm@19529
   363
  in
wenzelm@19529
   364
    (case S of
wenzelm@19529
   365
      [] => raise Fail "Unknown domain of empty intersection"
wenzelm@19529
   366
    | c :: cs => fold dom_inter cs (dom c))
wenzelm@19529
   367
  end;
wenzelm@19529
   368
wenzelm@19529
   369
wenzelm@26639
   370
(* meet_sort *)
wenzelm@26639
   371
wenzelm@26639
   372
fun meet_sort algebra =
wenzelm@26639
   373
  let
wenzelm@26639
   374
    fun inters S S' = inter_sort algebra (S, S');
wenzelm@26639
   375
    fun meet _ [] = I
wenzelm@26639
   376
      | meet (TFree (_, S)) S' =
wenzelm@26639
   377
          if sort_le algebra (S, S') then I
wenzelm@36105
   378
          else raise CLASS_ERROR (No_Subsort (S, S'))
wenzelm@26639
   379
      | meet (TVar (v, S)) S' =
wenzelm@26639
   380
          if sort_le algebra (S, S') then I
wenzelm@26639
   381
          else Vartab.map_default (v, S) (inters S')
wenzelm@26639
   382
      | meet (Type (a, Ts)) S = fold2 meet Ts (mg_domain algebra a S);
wenzelm@26639
   383
  in uncurry meet end;
wenzelm@26639
   384
haftmann@28665
   385
fun meet_sort_typ algebra (T, S) =
haftmann@28665
   386
  let
haftmann@28665
   387
    val tab = meet_sort algebra (T, S) Vartab.empty;
haftmann@28665
   388
  in Term.map_type_tvar (fn (v, _) =>
haftmann@28665
   389
    TVar (v, (the o Vartab.lookup tab) v))
haftmann@28665
   390
  end;
haftmann@28665
   391
wenzelm@26639
   392
wenzelm@19529
   393
(* of_sort *)
wenzelm@19529
   394
wenzelm@19645
   395
fun of_sort algebra =
wenzelm@19529
   396
  let
wenzelm@19529
   397
    fun ofS (_, []) = true
wenzelm@19645
   398
      | ofS (TFree (_, S), S') = sort_le algebra (S, S')
wenzelm@19645
   399
      | ofS (TVar (_, S), S') = sort_le algebra (S, S')
wenzelm@19529
   400
      | ofS (Type (a, Ts), S) =
wenzelm@19645
   401
          let val Ss = mg_domain algebra a S in
wenzelm@19529
   402
            ListPair.all ofS (Ts, Ss)
wenzelm@19578
   403
          end handle CLASS_ERROR _ => false;
wenzelm@19529
   404
  in ofS end;
wenzelm@19529
   405
wenzelm@19529
   406
haftmann@27498
   407
(* animating derivations *)
haftmann@27498
   408
wenzelm@32791
   409
fun of_sort_derivation algebra {class_relation, type_constructor, type_variable} =
wenzelm@19529
   410
  let
wenzelm@27555
   411
    val arities = arities_of algebra;
wenzelm@19578
   412
wenzelm@36104
   413
    fun weaken T D1 S2 =
wenzelm@36104
   414
      let val S1 = map snd D1 in
wenzelm@36104
   415
        if S1 = S2 then map fst D1
wenzelm@36104
   416
        else
wenzelm@36104
   417
          S2 |> map (fn c2 =>
wenzelm@36104
   418
            (case D1 |> find_first (fn (_, c1) => class_le algebra (c1, c2)) of
wenzelm@36104
   419
              SOME d1 => class_relation T d1 c2
wenzelm@36105
   420
            | NONE => raise CLASS_ERROR (No_Subsort (S1, S2))))
wenzelm@36104
   421
      end;
wenzelm@19529
   422
wenzelm@36103
   423
    fun derive (_, []) = []
wenzelm@36103
   424
      | derive (T as Type (a, Us), S) =
wenzelm@19529
   425
          let
wenzelm@19645
   426
            val Ss = mg_domain algebra a S;
wenzelm@36103
   427
            val dom = map2 (fn U => fn S => derive (U, S) ~~ S) Us Ss;
wenzelm@19529
   428
          in
wenzelm@19529
   429
            S |> map (fn c =>
wenzelm@19529
   430
              let
wenzelm@19529
   431
                val (c0, Ss') = the (AList.lookup (op =) (Symtab.lookup_list arities a) c);
wenzelm@36102
   432
                val dom' = map (fn ((U, d), S') => weaken U d S' ~~ S') ((Us ~~ dom) ~~ Ss');
wenzelm@36102
   433
              in class_relation T (type_constructor (a, Us) dom' c0, c0) c end)
wenzelm@19529
   434
          end
wenzelm@36103
   435
      | derive (T, S) = weaken T (type_variable T) S;
wenzelm@36103
   436
  in derive end;
wenzelm@19529
   437
wenzelm@35961
   438
fun classrel_derivation algebra class_relation =
wenzelm@35961
   439
  let
wenzelm@35961
   440
    fun path (x, c1 :: c2 :: cs) = path (class_relation (x, c1) c2, c2 :: cs)
wenzelm@35961
   441
      | path (x, _) = x;
wenzelm@35961
   442
  in
wenzelm@35961
   443
    fn (x, c1) => fn c2 =>
wenzelm@35961
   444
      (case Graph.irreducible_paths (classes_of algebra) (c1, c2) of
wenzelm@36105
   445
        [] => raise CLASS_ERROR (No_Classrel (c1, c2))
wenzelm@35961
   446
      | cs :: _ => path (x, cs))
wenzelm@35961
   447
  end;
wenzelm@35961
   448
wenzelm@19529
   449
wenzelm@19529
   450
(* witness_sorts *)
wenzelm@19529
   451
wenzelm@19645
   452
fun witness_sorts algebra types hyps sorts =
wenzelm@19529
   453
  let
wenzelm@19645
   454
    fun le S1 S2 = sort_le algebra (S1, S2);
wenzelm@31946
   455
    fun get S2 (T, S1) = if le S1 S2 then SOME (T, S2) else NONE;
wenzelm@19645
   456
    fun mg_dom t S = SOME (mg_domain algebra t S) handle CLASS_ERROR _ => NONE;
wenzelm@19529
   457
wenzelm@19578
   458
    fun witn_sort _ [] solved_failed = (SOME (propT, []), solved_failed)
wenzelm@19578
   459
      | witn_sort path S (solved, failed) =
wenzelm@19578
   460
          if exists (le S) failed then (NONE, (solved, failed))
wenzelm@19529
   461
          else
wenzelm@31946
   462
            (case get_first (get S) solved of
wenzelm@19578
   463
              SOME w => (SOME w, (solved, failed))
wenzelm@19529
   464
            | NONE =>
wenzelm@31946
   465
                (case get_first (get S) hyps of
wenzelm@19578
   466
                  SOME w => (SOME w, (w :: solved, failed))
wenzelm@19584
   467
                | NONE => witn_types path types S (solved, failed)))
wenzelm@19529
   468
wenzelm@19578
   469
    and witn_sorts path x = fold_map (witn_sort path) x
wenzelm@19529
   470
wenzelm@19578
   471
    and witn_types _ [] S (solved, failed) = (NONE, (solved, S :: failed))
wenzelm@19578
   472
      | witn_types path (t :: ts) S solved_failed =
wenzelm@19529
   473
          (case mg_dom t S of
wenzelm@19529
   474
            SOME SS =>
wenzelm@19529
   475
              (*do not descend into stronger args (achieving termination)*)
wenzelm@19529
   476
              if exists (fn D => le D S orelse exists (le D) path) SS then
wenzelm@19578
   477
                witn_types path ts S solved_failed
wenzelm@19529
   478
              else
wenzelm@19578
   479
                let val (ws, (solved', failed')) = witn_sorts (S :: path) SS solved_failed in
wenzelm@19529
   480
                  if forall is_some ws then
wenzelm@19529
   481
                    let val w = (Type (t, map (#1 o the) ws), S)
wenzelm@19578
   482
                    in (SOME w, (w :: solved', failed')) end
wenzelm@19578
   483
                  else witn_types path ts S (solved', failed')
wenzelm@19529
   484
                end
wenzelm@19578
   485
          | NONE => witn_types path ts S solved_failed);
wenzelm@19529
   486
wenzelm@19584
   487
  in map_filter I (#1 (witn_sorts [] sorts ([], []))) end;
wenzelm@19529
   488
wenzelm@19514
   489
end;