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