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