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