src/Pure/sorts.ML
author haftmann
Fri Nov 10 07:44:47 2006 +0100 (2006-11-10)
changeset 21286 b5e7b80caa6a
parent 20573 c945a208e7f8
child 21926 1091904ddb19
permissions -rw-r--r--
introduces canonical AList functions for loop_tacs
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@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@19645
    31
  val 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@19645
    39
  val certify_class: algebra -> class -> class    (*exception TYPE*)
wenzelm@19645
    40
  val certify_sort: algebra -> sort -> sort       (*exception TYPE*)
wenzelm@19645
    41
  val add_class: Pretty.pp -> class * class list -> algebra -> algebra
wenzelm@19645
    42
  val add_classrel: Pretty.pp -> class * class -> algebra -> algebra
wenzelm@19645
    43
  val add_arities: Pretty.pp -> string * (class * sort list) list -> algebra -> algebra
wenzelm@19645
    44
  val empty_algebra: algebra
wenzelm@19645
    45
  val merge_algebra: Pretty.pp -> algebra * algebra -> algebra
haftmann@20465
    46
  val project_algebra: Pretty.pp -> (class -> bool) -> algebra -> (sort -> sort) * algebra
wenzelm@19578
    47
  type class_error
wenzelm@19578
    48
  val class_error: Pretty.pp -> class_error -> 'a
wenzelm@19578
    49
  exception CLASS_ERROR of class_error
wenzelm@19645
    50
  val mg_domain: algebra -> string -> sort -> sort list   (*exception CLASS_ERROR*)
wenzelm@19645
    51
  val of_sort: algebra -> typ * sort -> bool
wenzelm@19645
    52
  val of_sort_derivation: Pretty.pp -> algebra ->
wenzelm@19529
    53
    {classrel: 'a * class -> class -> 'a,
wenzelm@19529
    54
     constructor: string -> ('a * class) list list -> class -> 'a,
wenzelm@19584
    55
     variable: typ -> ('a * class) list} ->
wenzelm@19584
    56
    typ * sort -> 'a list   (*exception CLASS_ERROR*)
wenzelm@19645
    57
  val witness_sorts: algebra -> string list -> sort list -> sort list -> (typ * sort) list
wenzelm@2956
    58
end;
wenzelm@2956
    59
wenzelm@20573
    60
structure Sorts: SORTS =
wenzelm@2956
    61
struct
wenzelm@2956
    62
wenzelm@19514
    63
wenzelm@19529
    64
(** ordered lists of sorts **)
wenzelm@14782
    65
wenzelm@16598
    66
val eq_set = OrdList.eq_set Term.sort_ord;
wenzelm@16598
    67
val op union = OrdList.union Term.sort_ord;
wenzelm@16598
    68
val subtract = OrdList.subtract Term.sort_ord;
wenzelm@14782
    69
wenzelm@19463
    70
val remove_sort = OrdList.remove Term.sort_ord;
wenzelm@16598
    71
val insert_sort = OrdList.insert Term.sort_ord;
wenzelm@14782
    72
wenzelm@16598
    73
fun insert_typ (TFree (_, S)) Ss = insert_sort S Ss
wenzelm@16598
    74
  | insert_typ (TVar (_, S)) Ss = insert_sort S Ss
wenzelm@16598
    75
  | insert_typ (Type (_, Ts)) Ss = insert_typs Ts Ss
wenzelm@16598
    76
and insert_typs [] Ss = Ss
wenzelm@16598
    77
  | insert_typs (T :: Ts) Ss = insert_typs Ts (insert_typ T Ss);
wenzelm@14782
    78
wenzelm@16598
    79
fun insert_term (Const (_, T)) Ss = insert_typ T Ss
wenzelm@16598
    80
  | insert_term (Free (_, T)) Ss = insert_typ T Ss
wenzelm@16598
    81
  | insert_term (Var (_, T)) Ss = insert_typ T Ss
wenzelm@16598
    82
  | insert_term (Bound _) Ss = Ss
wenzelm@16598
    83
  | insert_term (Abs (_, T, t)) Ss = insert_term t (insert_typ T Ss)
wenzelm@16598
    84
  | insert_term (t $ u) Ss = insert_term t (insert_term u Ss);
wenzelm@14782
    85
wenzelm@16598
    86
fun insert_terms [] Ss = Ss
wenzelm@16598
    87
  | insert_terms (t :: ts) Ss = insert_terms ts (insert_term t Ss);
wenzelm@14782
    88
wenzelm@14782
    89
wenzelm@19529
    90
wenzelm@19529
    91
(** order-sorted algebra **)
wenzelm@2956
    92
wenzelm@2956
    93
(*
wenzelm@14782
    94
  classes: graph representing class declarations together with proper
wenzelm@14782
    95
    subclass relation, which needs to be transitive and acyclic.
wenzelm@2956
    96
wenzelm@14782
    97
  arities: table of association lists of all type arities; (t, ars)
wenzelm@19531
    98
    means that type constructor t has the arities ars; an element
wenzelm@19531
    99
    (c, (c0, Ss)) of ars represents the arity t::(Ss)c being derived
wenzelm@19531
   100
    via c0 <= c.  "Coregularity" of the arities structure requires
wenzelm@19531
   101
    that for any two declarations t::(Ss1)c1 and t::(Ss2)c2 such that
wenzelm@19531
   102
    c1 <= c2 holds Ss1 <= Ss2.
wenzelm@2956
   103
*)
wenzelm@2956
   104
wenzelm@19645
   105
datatype algebra = Algebra of
wenzelm@20573
   106
 {classes: serial Graph.T,
wenzelm@19645
   107
  arities: (class * (class * sort list)) list Symtab.table};
wenzelm@19645
   108
wenzelm@19645
   109
fun rep_algebra (Algebra args) = args;
wenzelm@19645
   110
wenzelm@19645
   111
val classes_of = #classes o rep_algebra;
wenzelm@19645
   112
val arities_of = #arities o rep_algebra;
wenzelm@19645
   113
wenzelm@19645
   114
fun make_algebra (classes, arities) =
wenzelm@19645
   115
  Algebra {classes = classes, arities = arities};
wenzelm@19645
   116
wenzelm@19645
   117
fun map_classes f (Algebra {classes, arities}) = make_algebra (f classes, arities);
wenzelm@19645
   118
fun map_arities f (Algebra {classes, arities}) = make_algebra (classes, f arities);
wenzelm@19645
   119
wenzelm@19645
   120
wenzelm@19645
   121
(* classes *)
wenzelm@19645
   122
wenzelm@19645
   123
val classes = Graph.keys o classes_of;
wenzelm@19645
   124
val super_classes = Graph.imm_succs o classes_of;
wenzelm@2956
   125
wenzelm@2956
   126
wenzelm@19529
   127
(* class relations *)
wenzelm@2956
   128
wenzelm@19645
   129
val class_less = Graph.is_edge o classes_of;
wenzelm@19645
   130
fun class_le algebra (c1, c2) = c1 = c2 orelse class_less algebra (c1, c2);
wenzelm@2956
   131
wenzelm@2956
   132
wenzelm@19529
   133
(* sort relations *)
wenzelm@2956
   134
wenzelm@19645
   135
fun sort_le algebra (S1, S2) =
wenzelm@19645
   136
  forall (fn c2 => exists (fn c1 => class_le algebra (c1, c2)) S1) S2;
wenzelm@2956
   137
wenzelm@19645
   138
fun sorts_le algebra (Ss1, Ss2) =
wenzelm@19645
   139
  ListPair.all (sort_le algebra) (Ss1, Ss2);
wenzelm@2956
   140
wenzelm@19645
   141
fun sort_eq algebra (S1, S2) =
wenzelm@19645
   142
  sort_le algebra (S1, S2) andalso sort_le algebra (S2, S1);
wenzelm@2956
   143
wenzelm@2956
   144
wenzelm@19529
   145
(* intersection *)
wenzelm@2956
   146
wenzelm@19645
   147
fun inter_class algebra c S =
wenzelm@2956
   148
  let
wenzelm@2956
   149
    fun intr [] = [c]
wenzelm@2956
   150
      | intr (S' as c' :: c's) =
wenzelm@19645
   151
          if class_le algebra (c', c) then S'
wenzelm@19645
   152
          else if class_le algebra (c, c') then intr c's
wenzelm@2956
   153
          else c' :: intr c's
wenzelm@2956
   154
  in intr S end;
wenzelm@2956
   155
wenzelm@19645
   156
fun inter_sort algebra (S1, S2) =
wenzelm@19645
   157
  sort_strings (fold (inter_class algebra) S1 S2);
wenzelm@2956
   158
wenzelm@2956
   159
wenzelm@19645
   160
(* normal form *)
wenzelm@2956
   161
wenzelm@19529
   162
fun norm_sort _ [] = []
wenzelm@19529
   163
  | norm_sort _ (S as [_]) = S
wenzelm@19645
   164
  | norm_sort algebra S =
wenzelm@19645
   165
      filter (fn c => not (exists (fn c' => class_less algebra (c', c)) S)) S
wenzelm@19529
   166
      |> sort_distinct string_ord;
wenzelm@2990
   167
wenzelm@2990
   168
wenzelm@19645
   169
(* certify *)
wenzelm@19645
   170
wenzelm@19645
   171
fun certify_class algebra c =
wenzelm@19645
   172
  if can (Graph.get_node (classes_of algebra)) c then c
wenzelm@19645
   173
  else raise TYPE ("Undeclared class: " ^ quote c, [], []);
wenzelm@19645
   174
wenzelm@19645
   175
fun certify_sort classes = norm_sort classes o map (certify_class classes);
wenzelm@19645
   176
wenzelm@19645
   177
wenzelm@2956
   178
wenzelm@19529
   179
(** build algebras **)
wenzelm@19514
   180
wenzelm@19514
   181
(* classes *)
wenzelm@19514
   182
wenzelm@19514
   183
fun err_dup_classes cs =
wenzelm@19514
   184
  error ("Duplicate declaration of class(es): " ^ commas_quote cs);
wenzelm@19514
   185
wenzelm@19514
   186
fun err_cyclic_classes pp css =
wenzelm@19514
   187
  error (cat_lines (map (fn cs =>
wenzelm@19514
   188
    "Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css));
wenzelm@19514
   189
wenzelm@19645
   190
fun add_class pp (c, cs) = map_classes (fn classes =>
wenzelm@19514
   191
  let
wenzelm@20573
   192
    val classes' = classes |> Graph.new_node (c, serial ())
wenzelm@19514
   193
      handle Graph.DUP dup => err_dup_classes [dup];
wenzelm@19514
   194
    val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs)
wenzelm@19514
   195
      handle Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@19645
   196
  in classes'' end);
wenzelm@19514
   197
wenzelm@19514
   198
wenzelm@19514
   199
(* arities *)
wenzelm@19514
   200
wenzelm@19514
   201
local
wenzelm@19514
   202
wenzelm@19514
   203
fun for_classes _ NONE = ""
wenzelm@19514
   204
  | for_classes pp (SOME (c1, c2)) =
wenzelm@19514
   205
      " for classes " ^ Pretty.string_of_classrel pp [c1, c2];
wenzelm@19514
   206
wenzelm@19514
   207
fun err_conflict pp t cc (c, Ss) (c', Ss') =
wenzelm@19514
   208
  error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n  " ^
wenzelm@19514
   209
    Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n  " ^
wenzelm@19514
   210
    Pretty.string_of_arity pp (t, Ss', [c']));
wenzelm@19514
   211
wenzelm@19645
   212
fun coregular pp algebra t (c, (c0, Ss)) ars =
wenzelm@19514
   213
  let
wenzelm@19524
   214
    fun conflict (c', (_, Ss')) =
wenzelm@19645
   215
      if class_le algebra (c, c') andalso not (sorts_le algebra (Ss, Ss')) then
wenzelm@19514
   216
        SOME ((c, c'), (c', Ss'))
wenzelm@19645
   217
      else if class_le algebra (c', c) andalso not (sorts_le algebra (Ss', Ss)) then
wenzelm@19514
   218
        SOME ((c', c), (c', Ss'))
wenzelm@19514
   219
      else NONE;
wenzelm@19514
   220
  in
wenzelm@19514
   221
    (case get_first conflict ars of
wenzelm@19514
   222
      SOME ((c1, c2), (c', Ss')) => err_conflict pp t (SOME (c1, c2)) (c, Ss) (c', Ss')
wenzelm@19524
   223
    | NONE => (c, (c0, Ss)) :: ars)
wenzelm@19514
   224
  end;
wenzelm@19514
   225
wenzelm@19645
   226
fun complete algebra (c0, Ss) = map (rpair (c0, Ss)) (c0 :: super_classes algebra c0);
wenzelm@19645
   227
wenzelm@19645
   228
fun insert pp algebra t (c, (c0, Ss)) ars =
wenzelm@19514
   229
  (case AList.lookup (op =) ars c of
wenzelm@19645
   230
    NONE => coregular pp algebra t (c, (c0, Ss)) ars
wenzelm@19524
   231
  | SOME (_, Ss') =>
wenzelm@19645
   232
      if sorts_le algebra (Ss, Ss') then ars
wenzelm@19645
   233
      else if sorts_le algebra (Ss', Ss) then
wenzelm@19645
   234
        coregular pp algebra t (c, (c0, Ss))
wenzelm@19524
   235
          (filter_out (fn (c'', (_, Ss'')) => c = c'' andalso Ss'' = Ss') ars)
wenzelm@19514
   236
      else err_conflict pp t NONE (c, Ss) (c, Ss'));
wenzelm@19514
   237
wenzelm@19645
   238
fun insert_ars pp algebra (t, ars) arities =
wenzelm@19645
   239
  let val ars' =
wenzelm@19645
   240
    Symtab.lookup_list arities t
wenzelm@19645
   241
    |> fold_rev (fold_rev (insert pp algebra t)) (map (complete algebra) ars)
wenzelm@19645
   242
  in Symtab.update (t, ars') arities end;
wenzelm@19514
   243
wenzelm@19514
   244
in
wenzelm@19514
   245
wenzelm@19645
   246
fun add_arities pp arg algebra = algebra |> map_arities (insert_ars pp algebra arg);
wenzelm@19514
   247
wenzelm@19645
   248
fun add_arities_table pp algebra =
wenzelm@19645
   249
  Symtab.fold (fn (t, ars) => insert_ars pp algebra (t, map snd ars));
wenzelm@19514
   250
wenzelm@19514
   251
end;
wenzelm@19514
   252
wenzelm@19529
   253
wenzelm@19645
   254
(* classrel *)
wenzelm@19645
   255
wenzelm@19645
   256
fun rebuild_arities pp algebra = algebra |> map_arities (fn arities =>
wenzelm@19645
   257
  Symtab.empty
wenzelm@19645
   258
  |> add_arities_table pp algebra arities);
wenzelm@19645
   259
wenzelm@19645
   260
fun add_classrel pp rel = rebuild_arities pp o map_classes (fn classes =>
wenzelm@19645
   261
  classes |> Graph.add_edge_trans_acyclic rel
wenzelm@19645
   262
    handle Graph.CYCLES css => err_cyclic_classes pp css);
wenzelm@19645
   263
wenzelm@19645
   264
wenzelm@19645
   265
(* empty and merge *)
wenzelm@19645
   266
wenzelm@19645
   267
val empty_algebra = make_algebra (Graph.empty, Symtab.empty);
wenzelm@19645
   268
wenzelm@19645
   269
fun merge_algebra pp
wenzelm@19645
   270
   (Algebra {classes = classes1, arities = arities1},
wenzelm@19645
   271
    Algebra {classes = classes2, arities = arities2}) =
wenzelm@19645
   272
  let
wenzelm@19645
   273
    val classes' = Graph.merge_trans_acyclic (op =) (classes1, classes2)
wenzelm@19645
   274
      handle Graph.DUPS cs => err_dup_classes cs
wenzelm@19645
   275
          | Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@19645
   276
    val algebra0 = make_algebra (classes', Symtab.empty);
wenzelm@19645
   277
    val arities' = Symtab.empty
wenzelm@19645
   278
      |> add_arities_table pp algebra0 arities1
wenzelm@19645
   279
      |> add_arities_table pp algebra0 arities2;
wenzelm@19645
   280
  in make_algebra (classes', arities') end;
wenzelm@19645
   281
wenzelm@19977
   282
fun project_algebra pp proj (algebra as Algebra {classes, arities}) =
haftmann@19952
   283
  let
haftmann@20465
   284
    val proj_sort = norm_sort algebra o filter proj o Graph.all_succs classes;
haftmann@20465
   285
    fun proj_arity (c, (_, Ss)) =
haftmann@20465
   286
      if proj c then SOME (c, (c, map proj_sort Ss)) else NONE;
haftmann@20454
   287
    val classes' = classes |> Graph.project proj;
wenzelm@19977
   288
    val arities' = arities |> (Symtab.map o map_filter) proj_arity;
haftmann@20465
   289
  in (proj_sort, rebuild_arities pp (make_algebra (classes', arities'))) end;
haftmann@20465
   290
wenzelm@19645
   291
wenzelm@19529
   292
wenzelm@19529
   293
(** sorts of types **)
wenzelm@19529
   294
wenzelm@19578
   295
(* errors *)
wenzelm@19578
   296
wenzelm@19578
   297
datatype class_error = NoClassrel of class * class | NoArity of string * class;
wenzelm@19529
   298
wenzelm@19578
   299
fun class_error pp (NoClassrel (c1, c2)) =
wenzelm@19578
   300
      error ("No class relation " ^ Pretty.string_of_classrel pp [c1, c2])
wenzelm@19578
   301
  | class_error pp (NoArity (a, c)) =
wenzelm@19578
   302
      error ("No type arity " ^ Pretty.string_of_arity pp (a, [], [c]));
wenzelm@19529
   303
wenzelm@19578
   304
exception CLASS_ERROR of class_error;
wenzelm@19578
   305
wenzelm@19578
   306
wenzelm@19578
   307
(* mg_domain *)
wenzelm@19529
   308
wenzelm@19645
   309
fun mg_domain algebra a S =
wenzelm@19529
   310
  let
wenzelm@19645
   311
    val arities = arities_of algebra;
wenzelm@19529
   312
    fun dom c =
wenzelm@19529
   313
      (case AList.lookup (op =) (Symtab.lookup_list arities a) c of
wenzelm@19578
   314
        NONE => raise CLASS_ERROR (NoArity (a, c))
wenzelm@19529
   315
      | SOME (_, Ss) => Ss);
wenzelm@19645
   316
    fun dom_inter c Ss = ListPair.map (inter_sort algebra) (dom c, Ss);
wenzelm@19529
   317
  in
wenzelm@19529
   318
    (case S of
wenzelm@19529
   319
      [] => raise Fail "Unknown domain of empty intersection"
wenzelm@19529
   320
    | c :: cs => fold dom_inter cs (dom c))
wenzelm@19529
   321
  end;
wenzelm@19529
   322
wenzelm@19529
   323
wenzelm@19529
   324
(* of_sort *)
wenzelm@19529
   325
wenzelm@19645
   326
fun of_sort algebra =
wenzelm@19529
   327
  let
wenzelm@19529
   328
    fun ofS (_, []) = true
wenzelm@19645
   329
      | ofS (TFree (_, S), S') = sort_le algebra (S, S')
wenzelm@19645
   330
      | ofS (TVar (_, S), S') = sort_le algebra (S, S')
wenzelm@19529
   331
      | ofS (Type (a, Ts), S) =
wenzelm@19645
   332
          let val Ss = mg_domain algebra a S in
wenzelm@19529
   333
            ListPair.all ofS (Ts, Ss)
wenzelm@19578
   334
          end handle CLASS_ERROR _ => false;
wenzelm@19529
   335
  in ofS end;
wenzelm@19529
   336
wenzelm@19529
   337
wenzelm@19529
   338
(* of_sort_derivation *)
wenzelm@19529
   339
wenzelm@19645
   340
fun of_sort_derivation pp algebra {classrel, constructor, variable} =
wenzelm@19529
   341
  let
wenzelm@19645
   342
    val {classes, arities} = rep_algebra algebra;
haftmann@19952
   343
    fun weaken_path (x, c1 :: c2 :: cs) =
haftmann@19952
   344
          weaken_path (classrel (x, c1) c2, c2 :: cs)
wenzelm@19578
   345
      | weaken_path (x, _) = x;
wenzelm@19578
   346
    fun weaken (x, c1) c2 =
wenzelm@19578
   347
      (case Graph.irreducible_paths classes (c1, c2) of
wenzelm@19578
   348
        [] => raise CLASS_ERROR (NoClassrel (c1, c2))
wenzelm@19578
   349
      | cs :: _ => weaken_path (x, cs));
wenzelm@19578
   350
wenzelm@19529
   351
    fun weakens S1 S2 = S2 |> map (fn c2 =>
wenzelm@19645
   352
      (case S1 |> find_first (fn (_, c1) => class_le algebra (c1, c2)) of
wenzelm@19529
   353
        SOME d1 => weaken d1 c2
wenzelm@19529
   354
      | NONE => error ("Cannot derive subsort relation " ^
wenzelm@19529
   355
          Pretty.string_of_sort pp (map #2 S1) ^ " < " ^ Pretty.string_of_sort pp S2)));
wenzelm@19529
   356
wenzelm@19529
   357
    fun derive _ [] = []
wenzelm@19529
   358
      | derive (Type (a, Ts)) S =
wenzelm@19529
   359
          let
wenzelm@19645
   360
            val Ss = mg_domain algebra a S;
wenzelm@19529
   361
            val dom = map2 (fn T => fn S => derive T S ~~ S) Ts Ss;
wenzelm@19529
   362
          in
wenzelm@19529
   363
            S |> map (fn c =>
wenzelm@19529
   364
              let
wenzelm@19529
   365
                val (c0, Ss') = the (AList.lookup (op =) (Symtab.lookup_list arities a) c);
wenzelm@19529
   366
                val dom' = map2 (fn d => fn S' => weakens d S' ~~ S') dom Ss';
wenzelm@19529
   367
              in weaken (constructor a dom' c0, c0) c end)
wenzelm@19529
   368
          end
wenzelm@19529
   369
      | derive T S = weakens (variable T) S;
wenzelm@19529
   370
  in uncurry derive end;
wenzelm@19529
   371
wenzelm@19529
   372
wenzelm@19529
   373
(* witness_sorts *)
wenzelm@19529
   374
wenzelm@19645
   375
fun witness_sorts algebra types hyps sorts =
wenzelm@19529
   376
  let
wenzelm@19645
   377
    fun le S1 S2 = sort_le algebra (S1, S2);
wenzelm@19529
   378
    fun get_solved S2 (T, S1) = if le S1 S2 then SOME (T, S2) else NONE;
wenzelm@19529
   379
    fun get_hyp S2 S1 = if le S1 S2 then SOME (TFree ("'hyp", S1), S2) else NONE;
wenzelm@19645
   380
    fun mg_dom t S = SOME (mg_domain algebra t S) handle CLASS_ERROR _ => NONE;
wenzelm@19529
   381
wenzelm@19578
   382
    fun witn_sort _ [] solved_failed = (SOME (propT, []), solved_failed)
wenzelm@19578
   383
      | witn_sort path S (solved, failed) =
wenzelm@19578
   384
          if exists (le S) failed then (NONE, (solved, failed))
wenzelm@19529
   385
          else
wenzelm@19529
   386
            (case get_first (get_solved S) solved of
wenzelm@19578
   387
              SOME w => (SOME w, (solved, failed))
wenzelm@19529
   388
            | NONE =>
wenzelm@19529
   389
                (case get_first (get_hyp S) hyps of
wenzelm@19578
   390
                  SOME w => (SOME w, (w :: solved, failed))
wenzelm@19584
   391
                | NONE => witn_types path types S (solved, failed)))
wenzelm@19529
   392
wenzelm@19578
   393
    and witn_sorts path x = fold_map (witn_sort path) x
wenzelm@19529
   394
wenzelm@19578
   395
    and witn_types _ [] S (solved, failed) = (NONE, (solved, S :: failed))
wenzelm@19578
   396
      | witn_types path (t :: ts) S solved_failed =
wenzelm@19529
   397
          (case mg_dom t S of
wenzelm@19529
   398
            SOME SS =>
wenzelm@19529
   399
              (*do not descend into stronger args (achieving termination)*)
wenzelm@19529
   400
              if exists (fn D => le D S orelse exists (le D) path) SS then
wenzelm@19578
   401
                witn_types path ts S solved_failed
wenzelm@19529
   402
              else
wenzelm@19578
   403
                let val (ws, (solved', failed')) = witn_sorts (S :: path) SS solved_failed in
wenzelm@19529
   404
                  if forall is_some ws then
wenzelm@19529
   405
                    let val w = (Type (t, map (#1 o the) ws), S)
wenzelm@19578
   406
                    in (SOME w, (w :: solved', failed')) end
wenzelm@19578
   407
                  else witn_types path ts S (solved', failed')
wenzelm@19529
   408
                end
wenzelm@19578
   409
          | NONE => witn_types path ts S solved_failed);
wenzelm@19529
   410
wenzelm@19584
   411
  in map_filter I (#1 (witn_sorts [] sorts ([], []))) end;
wenzelm@19529
   412
wenzelm@19514
   413
end;