src/Pure/sorts.ML
author wenzelm
Thu Aug 15 16:02:47 2019 +0200 (9 months ago)
changeset 70533 031620901fcd
parent 68295 781a98696638
child 71454 b2c9f94e025f
permissions -rw-r--r--
support for (fully reconstructed) proof terms in Scala;
proper cache_typs;
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@39687
    17
  val make: sort list -> sort Ord_List.T
wenzelm@39687
    18
  val subset: sort Ord_List.T * sort Ord_List.T -> bool
wenzelm@39687
    19
  val union: sort Ord_List.T -> sort Ord_List.T -> sort Ord_List.T
wenzelm@39687
    20
  val subtract: sort Ord_List.T -> sort Ord_List.T -> sort Ord_List.T
wenzelm@39687
    21
  val remove_sort: sort -> sort Ord_List.T -> sort Ord_List.T
wenzelm@39687
    22
  val insert_sort: sort -> sort Ord_List.T -> sort Ord_List.T
wenzelm@39687
    23
  val insert_typ: typ -> sort Ord_List.T -> sort Ord_List.T
wenzelm@39687
    24
  val insert_typs: typ list -> sort Ord_List.T -> sort Ord_List.T
wenzelm@39687
    25
  val insert_term: term -> sort Ord_List.T -> sort Ord_List.T
wenzelm@39687
    26
  val insert_terms: term list -> sort Ord_List.T -> sort Ord_List.T
wenzelm@19645
    27
  type algebra
wenzelm@36328
    28
  val classes_of: algebra -> serial Graph.T
wenzelm@37248
    29
  val arities_of: algebra -> (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@39687
    40
  val minimal_sorts: algebra -> sort list -> sort Ord_List.T
wenzelm@61262
    41
  val add_class: Context.generic -> class * class list -> algebra -> algebra
wenzelm@61262
    42
  val add_classrel: Context.generic -> class * class -> algebra -> algebra
wenzelm@61262
    43
  val add_arities: Context.generic -> string * (class * sort list) list -> algebra -> algebra
wenzelm@19645
    44
  val empty_algebra: algebra
wenzelm@61262
    45
  val merge_algebra: Context.generic -> algebra * algebra -> algebra
wenzelm@68295
    46
  val dest_algebra: algebra list -> algebra ->
wenzelm@68295
    47
    {classrel: (class * class list) list,
wenzelm@68295
    48
     arities: (string * sort list * class) list}
wenzelm@61262
    49
  val subalgebra: Context.generic -> (class -> bool) -> (class * string -> sort list option)
haftmann@22181
    50
    -> algebra -> (sort -> sort) * algebra
wenzelm@19578
    51
  type class_error
wenzelm@61262
    52
  val class_error: Context.generic -> class_error -> string
wenzelm@19578
    53
  exception CLASS_ERROR of class_error
wenzelm@48272
    54
  val has_instance: algebra -> string -> sort -> bool
wenzelm@19645
    55
  val mg_domain: algebra -> string -> sort -> sort list   (*exception CLASS_ERROR*)
haftmann@28665
    56
  val meet_sort: algebra -> typ * sort
haftmann@28665
    57
    -> sort Vartab.table -> sort Vartab.table   (*exception CLASS_ERROR*)
haftmann@28665
    58
  val meet_sort_typ: algebra -> typ * sort -> typ -> typ   (*exception CLASS_ERROR*)
wenzelm@19645
    59
  val of_sort: algebra -> typ * sort -> bool
wenzelm@32791
    60
  val of_sort_derivation: algebra ->
haftmann@62538
    61
    {class_relation: typ -> bool -> 'a * class -> class -> 'a,
wenzelm@36102
    62
     type_constructor: string * typ list -> ('a * class) list list -> class -> 'a,
wenzelm@22570
    63
     type_variable: typ -> ('a * class) list} ->
wenzelm@19584
    64
    typ * sort -> 'a list   (*exception CLASS_ERROR*)
wenzelm@35961
    65
  val classrel_derivation: algebra ->
wenzelm@35961
    66
    ('a * class -> class -> 'a) -> 'a * class -> class -> 'a  (*exception CLASS_ERROR*)
wenzelm@31946
    67
  val witness_sorts: algebra -> string list -> (typ * 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@39687
    76
val make = Ord_List.make Term_Ord.sort_ord;
wenzelm@39687
    77
val subset = Ord_List.subset Term_Ord.sort_ord;
wenzelm@39687
    78
val union = Ord_List.union Term_Ord.sort_ord;
wenzelm@39687
    79
val subtract = Ord_List.subtract Term_Ord.sort_ord;
wenzelm@14782
    80
wenzelm@39687
    81
val remove_sort = Ord_List.remove Term_Ord.sort_ord;
wenzelm@39687
    82
val insert_sort = Ord_List.insert Term_Ord.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@37248
   110
    (c, Ss) of ars represents the arity t::(Ss)c.  "Coregularity" of
wenzelm@37248
   111
    the arities structure requires that for any two declarations
wenzelm@37248
   112
    t::(Ss1)c1 and t::(Ss2)c2 such that c1 <= c2 holds Ss1 <= Ss2.
wenzelm@2956
   113
*)
wenzelm@2956
   114
wenzelm@19645
   115
datatype algebra = Algebra of
wenzelm@20573
   116
 {classes: serial Graph.T,
wenzelm@37248
   117
  arities: (class * sort list) list Symtab.table};
wenzelm@19645
   118
wenzelm@36328
   119
fun classes_of (Algebra {classes, ...}) = classes;
wenzelm@36328
   120
fun arities_of (Algebra {arities, ...}) = arities;
wenzelm@19645
   121
wenzelm@19645
   122
fun make_algebra (classes, arities) =
wenzelm@19645
   123
  Algebra {classes = classes, arities = arities};
wenzelm@19645
   124
wenzelm@19645
   125
fun map_classes f (Algebra {classes, arities}) = make_algebra (f classes, arities);
wenzelm@19645
   126
fun map_arities f (Algebra {classes, arities}) = make_algebra (classes, f arities);
wenzelm@19645
   127
wenzelm@19645
   128
wenzelm@19645
   129
(* classes *)
wenzelm@19645
   130
wenzelm@21933
   131
fun all_classes (Algebra {classes, ...}) = Graph.all_preds classes (Graph.maximals classes);
wenzelm@21933
   132
wenzelm@44338
   133
val super_classes = Graph.immediate_succs o classes_of;
wenzelm@2956
   134
wenzelm@2956
   135
wenzelm@19529
   136
(* class relations *)
wenzelm@2956
   137
wenzelm@19645
   138
val class_less = Graph.is_edge o classes_of;
wenzelm@19645
   139
fun class_le algebra (c1, c2) = c1 = c2 orelse class_less algebra (c1, c2);
wenzelm@2956
   140
wenzelm@2956
   141
wenzelm@19529
   142
(* sort relations *)
wenzelm@2956
   143
wenzelm@19645
   144
fun sort_le algebra (S1, S2) =
wenzelm@23585
   145
  S1 = S2 orelse forall (fn c2 => exists (fn c1 => class_le algebra (c1, c2)) S1) S2;
wenzelm@2956
   146
wenzelm@19645
   147
fun sorts_le algebra (Ss1, Ss2) =
wenzelm@19645
   148
  ListPair.all (sort_le algebra) (Ss1, Ss2);
wenzelm@2956
   149
wenzelm@19645
   150
fun sort_eq algebra (S1, S2) =
wenzelm@19645
   151
  sort_le algebra (S1, S2) andalso sort_le algebra (S2, S1);
wenzelm@2956
   152
wenzelm@2956
   153
wenzelm@19529
   154
(* intersection *)
wenzelm@2956
   155
wenzelm@19645
   156
fun inter_class algebra c S =
wenzelm@2956
   157
  let
wenzelm@2956
   158
    fun intr [] = [c]
wenzelm@2956
   159
      | intr (S' as c' :: c's) =
wenzelm@19645
   160
          if class_le algebra (c', c) then S'
wenzelm@19645
   161
          else if class_le algebra (c, c') then intr c's
wenzelm@2956
   162
          else c' :: intr c's
wenzelm@2956
   163
  in intr S end;
wenzelm@2956
   164
wenzelm@19645
   165
fun inter_sort algebra (S1, S2) =
wenzelm@19645
   166
  sort_strings (fold (inter_class algebra) S1 S2);
wenzelm@2956
   167
wenzelm@2956
   168
wenzelm@24732
   169
(* normal forms *)
wenzelm@2956
   170
wenzelm@24732
   171
fun minimize_sort _ [] = []
wenzelm@24732
   172
  | minimize_sort _ (S as [_]) = S
wenzelm@24732
   173
  | minimize_sort algebra S =
wenzelm@19645
   174
      filter (fn c => not (exists (fn c' => class_less algebra (c', c)) S)) S
wenzelm@19529
   175
      |> sort_distinct string_ord;
wenzelm@2990
   176
wenzelm@24732
   177
fun complete_sort algebra =
wenzelm@24732
   178
  Graph.all_succs (classes_of algebra) o minimize_sort algebra;
wenzelm@24732
   179
wenzelm@28623
   180
fun minimal_sorts algebra raw_sorts =
wenzelm@28623
   181
  let
wenzelm@28623
   182
    fun le S1 S2 = sort_le algebra (S1, S2);
wenzelm@28623
   183
    val sorts = make (map (minimize_sort algebra) raw_sorts);
wenzelm@28623
   184
  in sorts |> filter_out (fn S => exists (fn S' => le S' S andalso not (le S S')) sorts) end;
wenzelm@28623
   185
wenzelm@2990
   186
wenzelm@2956
   187
wenzelm@19529
   188
(** build algebras **)
wenzelm@19514
   189
wenzelm@19514
   190
(* classes *)
wenzelm@19514
   191
wenzelm@23655
   192
fun err_dup_class c = error ("Duplicate declaration of class: " ^ quote c);
wenzelm@19514
   193
wenzelm@61264
   194
fun err_cyclic_classes context css =
wenzelm@19514
   195
  error (cat_lines (map (fn cs =>
wenzelm@61264
   196
    "Cycle in class relation: " ^ Syntax.string_of_classrel (Syntax.init_pretty context) cs) css));
wenzelm@19514
   197
wenzelm@61264
   198
fun add_class context (c, cs) = map_classes (fn classes =>
wenzelm@19514
   199
  let
wenzelm@20573
   200
    val classes' = classes |> Graph.new_node (c, serial ())
wenzelm@23655
   201
      handle Graph.DUP dup => err_dup_class dup;
wenzelm@19514
   202
    val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs)
wenzelm@61264
   203
      handle Graph.CYCLES css => err_cyclic_classes context css;
wenzelm@19645
   204
  in classes'' end);
wenzelm@19514
   205
wenzelm@19514
   206
wenzelm@19514
   207
(* arities *)
wenzelm@19514
   208
wenzelm@19514
   209
local
wenzelm@19514
   210
wenzelm@19514
   211
fun for_classes _ NONE = ""
wenzelm@42387
   212
  | for_classes ctxt (SOME (c1, c2)) = " for classes " ^ Syntax.string_of_classrel ctxt [c1, c2];
wenzelm@19514
   213
wenzelm@61264
   214
fun err_conflict context t cc (c, Ss) (c', Ss') =
wenzelm@61264
   215
  let val ctxt = Syntax.init_pretty context in
wenzelm@47005
   216
    error ("Conflict of type arities" ^ for_classes ctxt cc ^ ":\n  " ^
wenzelm@47005
   217
      Syntax.string_of_arity ctxt (t, Ss, [c]) ^ " and\n  " ^
wenzelm@47005
   218
      Syntax.string_of_arity ctxt (t, Ss', [c']))
wenzelm@47005
   219
  end;
wenzelm@19514
   220
wenzelm@61264
   221
fun coregular context algebra t (c, Ss) ars =
wenzelm@19514
   222
  let
wenzelm@37248
   223
    fun conflict (c', Ss') =
wenzelm@19645
   224
      if class_le algebra (c, c') andalso not (sorts_le algebra (Ss, Ss')) then
wenzelm@19514
   225
        SOME ((c, c'), (c', Ss'))
wenzelm@19645
   226
      else if class_le algebra (c', c) andalso not (sorts_le algebra (Ss', Ss)) then
wenzelm@19514
   227
        SOME ((c', c), (c', Ss'))
wenzelm@19514
   228
      else NONE;
wenzelm@19514
   229
  in
wenzelm@19514
   230
    (case get_first conflict ars of
wenzelm@61264
   231
      SOME ((c1, c2), (c', Ss')) => err_conflict context t (SOME (c1, c2)) (c, Ss) (c', Ss')
wenzelm@37248
   232
    | NONE => (c, Ss) :: ars)
wenzelm@19514
   233
  end;
wenzelm@19514
   234
wenzelm@37248
   235
fun complete algebra (c, Ss) = map (rpair Ss) (c :: super_classes algebra c);
wenzelm@19645
   236
wenzelm@61264
   237
fun insert context algebra t (c, Ss) ars =
wenzelm@19514
   238
  (case AList.lookup (op =) ars c of
wenzelm@61264
   239
    NONE => coregular context algebra t (c, Ss) ars
wenzelm@37248
   240
  | SOME Ss' =>
wenzelm@19645
   241
      if sorts_le algebra (Ss, Ss') then ars
wenzelm@37248
   242
      else if sorts_le algebra (Ss', Ss)
wenzelm@61264
   243
      then coregular context algebra t (c, Ss) (remove (op =) (c, Ss') ars)
wenzelm@61264
   244
      else err_conflict context t NONE (c, Ss) (c, Ss'));
wenzelm@19514
   245
wenzelm@35975
   246
in
wenzelm@35975
   247
wenzelm@61264
   248
fun insert_ars context algebra t = fold_rev (insert context algebra t);
wenzelm@35975
   249
wenzelm@61264
   250
fun insert_complete_ars context algebra (t, ars) arities =
wenzelm@19645
   251
  let val ars' =
wenzelm@19645
   252
    Symtab.lookup_list arities t
wenzelm@61264
   253
    |> fold_rev (insert_ars context algebra t) (map (complete algebra) ars);
wenzelm@19645
   254
  in Symtab.update (t, ars') arities end;
wenzelm@19514
   255
wenzelm@61264
   256
fun add_arities context arg algebra =
wenzelm@61264
   257
  algebra |> map_arities (insert_complete_ars context algebra arg);
wenzelm@19514
   258
wenzelm@61264
   259
fun add_arities_table context algebra =
wenzelm@61264
   260
  Symtab.fold (fn (t, ars) => insert_complete_ars context algebra (t, ars));
wenzelm@19514
   261
wenzelm@19514
   262
end;
wenzelm@19514
   263
wenzelm@19529
   264
wenzelm@19645
   265
(* classrel *)
wenzelm@19645
   266
wenzelm@61264
   267
fun rebuild_arities context algebra = algebra |> map_arities (fn arities =>
wenzelm@19645
   268
  Symtab.empty
wenzelm@61264
   269
  |> add_arities_table context algebra arities);
wenzelm@19645
   270
wenzelm@61264
   271
fun add_classrel context rel = rebuild_arities context o map_classes (fn classes =>
wenzelm@19645
   272
  classes |> Graph.add_edge_trans_acyclic rel
wenzelm@61264
   273
    handle Graph.CYCLES css => err_cyclic_classes context css);
wenzelm@19645
   274
wenzelm@19645
   275
wenzelm@19645
   276
(* empty and merge *)
wenzelm@19645
   277
wenzelm@19645
   278
val empty_algebra = make_algebra (Graph.empty, Symtab.empty);
wenzelm@19645
   279
wenzelm@61264
   280
fun merge_algebra context
wenzelm@19645
   281
   (Algebra {classes = classes1, arities = arities1},
wenzelm@19645
   282
    Algebra {classes = classes2, arities = arities2}) =
wenzelm@19645
   283
  let
wenzelm@19645
   284
    val classes' = Graph.merge_trans_acyclic (op =) (classes1, classes2)
wenzelm@23655
   285
      handle Graph.DUP c => err_dup_class c
wenzelm@61264
   286
        | Graph.CYCLES css => err_cyclic_classes context css;
wenzelm@19645
   287
    val algebra0 = make_algebra (classes', Symtab.empty);
wenzelm@35975
   288
    val arities' =
wenzelm@35975
   289
      (case (pointer_eq (classes1, classes2), pointer_eq (arities1, arities2)) of
wenzelm@35975
   290
        (true, true) => arities1
wenzelm@35975
   291
      | (true, false) =>  (*no completion*)
wenzelm@35975
   292
          (arities1, arities2) |> Symtab.join (fn t => fn (ars1, ars2) =>
wenzelm@35975
   293
            if pointer_eq (ars1, ars2) then raise Symtab.SAME
wenzelm@61264
   294
            else insert_ars context algebra0 t ars2 ars1)
wenzelm@35975
   295
      | (false, true) =>  (*unary completion*)
wenzelm@35975
   296
          Symtab.empty
wenzelm@61264
   297
          |> add_arities_table context algebra0 arities1
wenzelm@35975
   298
      | (false, false) => (*binary completion*)
wenzelm@35975
   299
          Symtab.empty
wenzelm@61264
   300
          |> add_arities_table context algebra0 arities1
wenzelm@61264
   301
          |> add_arities_table context algebra0 arities2);
wenzelm@19645
   302
  in make_algebra (classes', arities') end;
wenzelm@19645
   303
wenzelm@21933
   304
wenzelm@68295
   305
(* destruct *)
wenzelm@68295
   306
wenzelm@68295
   307
fun dest_algebra parents (Algebra {classes, arities}) =
wenzelm@68295
   308
  let
wenzelm@68295
   309
    fun new_classrel rel = not (exists (fn algebra => class_less algebra rel) parents);
wenzelm@68295
   310
    val classrel =
wenzelm@68295
   311
      (classes, []) |-> Graph.fold (fn (c, (_, (_, ds))) =>
wenzelm@68295
   312
        (case filter (fn d => new_classrel (c, d)) (Graph.Keys.dest ds) of
wenzelm@68295
   313
          [] => I
wenzelm@68295
   314
        | ds' => cons (c, sort_strings ds')))
wenzelm@68295
   315
      |> sort_by #1;
wenzelm@68295
   316
wenzelm@68295
   317
    fun is_arity t ar algebra = member (op =) (Symtab.lookup_list (arities_of algebra) t) ar;
wenzelm@68295
   318
    fun add_arity t (c, Ss) = not (exists (is_arity t (c, Ss)) parents) ? cons (t, Ss, c);
wenzelm@68295
   319
    val arities =
wenzelm@68295
   320
      (arities, []) |-> Symtab.fold (fn (t, ars) => fold_rev (add_arity t) (sort_by #1 ars))
wenzelm@68295
   321
      |> sort_by #1;
wenzelm@68295
   322
  in {classrel = classrel, arities = arities} end;
wenzelm@68295
   323
wenzelm@68295
   324
wenzelm@37248
   325
(* algebra projections *)  (* FIXME potentially violates abstract type integrity *)
haftmann@28922
   326
wenzelm@61264
   327
fun subalgebra context P sargs (algebra as Algebra {classes, arities}) =
haftmann@19952
   328
  let
wenzelm@24732
   329
    val restrict_sort = minimize_sort algebra o filter P o Graph.all_succs classes;
wenzelm@37248
   330
    fun restrict_arity t (c, Ss) =
wenzelm@37248
   331
      if P c then
wenzelm@37248
   332
        (case sargs (c, t) of
wenzelm@37248
   333
          SOME sorts =>
wenzelm@37248
   334
            SOME (c, Ss |> map2 (curry (inter_sort algebra)) sorts |> map restrict_sort)
wenzelm@37248
   335
        | NONE => NONE)
haftmann@22181
   336
      else NONE;
wenzelm@46614
   337
    val classes' = classes |> Graph.restrict P;
haftmann@39020
   338
    val arities' = arities |> Symtab.map (map_filter o restrict_arity);
wenzelm@61264
   339
  in (restrict_sort, rebuild_arities context (make_algebra (classes', arities'))) end;
haftmann@20465
   340
wenzelm@19645
   341
wenzelm@19529
   342
wenzelm@19529
   343
(** sorts of types **)
wenzelm@19529
   344
wenzelm@35961
   345
(* errors -- performance tuning via delayed message composition *)
wenzelm@19578
   346
wenzelm@26639
   347
datatype class_error =
wenzelm@36105
   348
  No_Classrel of class * class |
wenzelm@36105
   349
  No_Arity of string * class |
wenzelm@36105
   350
  No_Subsort of sort * sort;
wenzelm@19529
   351
wenzelm@61264
   352
fun class_error context =
wenzelm@61264
   353
  let val ctxt = Syntax.init_pretty context in
wenzelm@47005
   354
    fn No_Classrel (c1, c2) => "No class relation " ^ Syntax.string_of_classrel ctxt [c1, c2]
wenzelm@47005
   355
     | No_Arity (a, c) => "No type arity " ^ Syntax.string_of_arity ctxt (a, [], [c])
wenzelm@47005
   356
     | No_Subsort (S1, S2) =>
wenzelm@47005
   357
        "Cannot derive subsort relation " ^
wenzelm@47005
   358
          Syntax.string_of_sort ctxt S1 ^ " < " ^ Syntax.string_of_sort ctxt S2
wenzelm@47005
   359
  end;
wenzelm@19529
   360
wenzelm@19578
   361
exception CLASS_ERROR of class_error;
wenzelm@19578
   362
wenzelm@19578
   363
wenzelm@48272
   364
(* instances *)
wenzelm@48272
   365
wenzelm@48272
   366
fun has_instance algebra a =
wenzelm@48272
   367
  forall (AList.defined (op =) (Symtab.lookup_list (arities_of algebra) a));
wenzelm@19529
   368
wenzelm@19645
   369
fun mg_domain algebra a S =
wenzelm@19529
   370
  let
wenzelm@48272
   371
    val ars = Symtab.lookup_list (arities_of algebra) a;
wenzelm@19529
   372
    fun dom c =
wenzelm@48272
   373
      (case AList.lookup (op =) ars c of
wenzelm@36105
   374
        NONE => raise CLASS_ERROR (No_Arity (a, c))
wenzelm@37248
   375
      | SOME Ss => Ss);
wenzelm@19645
   376
    fun dom_inter c Ss = ListPair.map (inter_sort algebra) (dom c, Ss);
wenzelm@19529
   377
  in
wenzelm@19529
   378
    (case S of
wenzelm@19529
   379
      [] => raise Fail "Unknown domain of empty intersection"
wenzelm@19529
   380
    | c :: cs => fold dom_inter cs (dom c))
wenzelm@19529
   381
  end;
wenzelm@19529
   382
wenzelm@19529
   383
wenzelm@26639
   384
(* meet_sort *)
wenzelm@26639
   385
wenzelm@26639
   386
fun meet_sort algebra =
wenzelm@26639
   387
  let
wenzelm@26639
   388
    fun inters S S' = inter_sort algebra (S, S');
wenzelm@26639
   389
    fun meet _ [] = I
wenzelm@26639
   390
      | meet (TFree (_, S)) S' =
wenzelm@26639
   391
          if sort_le algebra (S, S') then I
wenzelm@36105
   392
          else raise CLASS_ERROR (No_Subsort (S, S'))
wenzelm@26639
   393
      | meet (TVar (v, S)) S' =
wenzelm@26639
   394
          if sort_le algebra (S, S') then I
wenzelm@26639
   395
          else Vartab.map_default (v, S) (inters S')
wenzelm@26639
   396
      | meet (Type (a, Ts)) S = fold2 meet Ts (mg_domain algebra a S);
wenzelm@26639
   397
  in uncurry meet end;
wenzelm@26639
   398
haftmann@28665
   399
fun meet_sort_typ algebra (T, S) =
wenzelm@37248
   400
  let val tab = meet_sort algebra (T, S) Vartab.empty;
wenzelm@37248
   401
  in Term.map_type_tvar (fn (v, _) => TVar (v, (the o Vartab.lookup tab) v)) end;
haftmann@28665
   402
wenzelm@26639
   403
wenzelm@19529
   404
(* of_sort *)
wenzelm@19529
   405
wenzelm@19645
   406
fun of_sort algebra =
wenzelm@19529
   407
  let
wenzelm@19529
   408
    fun ofS (_, []) = true
wenzelm@19645
   409
      | ofS (TFree (_, S), S') = sort_le algebra (S, S')
wenzelm@19645
   410
      | ofS (TVar (_, S), S') = sort_le algebra (S, S')
wenzelm@19529
   411
      | ofS (Type (a, Ts), S) =
wenzelm@19645
   412
          let val Ss = mg_domain algebra a S in
wenzelm@19529
   413
            ListPair.all ofS (Ts, Ss)
wenzelm@19578
   414
          end handle CLASS_ERROR _ => false;
wenzelm@19529
   415
  in ofS end;
wenzelm@19529
   416
wenzelm@19529
   417
haftmann@27498
   418
(* animating derivations *)
haftmann@27498
   419
wenzelm@32791
   420
fun of_sort_derivation algebra {class_relation, type_constructor, type_variable} =
wenzelm@19529
   421
  let
wenzelm@27555
   422
    val arities = arities_of algebra;
wenzelm@19578
   423
wenzelm@36104
   424
    fun weaken T D1 S2 =
wenzelm@36104
   425
      let val S1 = map snd D1 in
wenzelm@36104
   426
        if S1 = S2 then map fst D1
wenzelm@36104
   427
        else
wenzelm@36104
   428
          S2 |> map (fn c2 =>
haftmann@62538
   429
            (case D1 |> filter (fn (_, c1) => class_le algebra (c1, c2)) of
haftmann@62538
   430
              [d1] => class_relation T true d1 c2
haftmann@62538
   431
            | (d1 :: _ :: _) => class_relation T false d1 c2
haftmann@62538
   432
            | [] => raise CLASS_ERROR (No_Subsort (S1, S2))))
wenzelm@36104
   433
      end;
wenzelm@19529
   434
wenzelm@36103
   435
    fun derive (_, []) = []
wenzelm@44338
   436
      | derive (Type (a, Us), S) =
wenzelm@19529
   437
          let
wenzelm@19645
   438
            val Ss = mg_domain algebra a S;
wenzelm@36103
   439
            val dom = map2 (fn U => fn S => derive (U, S) ~~ S) Us Ss;
wenzelm@19529
   440
          in
wenzelm@19529
   441
            S |> map (fn c =>
wenzelm@19529
   442
              let
wenzelm@37248
   443
                val Ss' = the (AList.lookup (op =) (Symtab.lookup_list arities a) c);
wenzelm@36102
   444
                val dom' = map (fn ((U, d), S') => weaken U d S' ~~ S') ((Us ~~ dom) ~~ Ss');
wenzelm@37248
   445
              in type_constructor (a, Us) dom' c end)
wenzelm@19529
   446
          end
wenzelm@36103
   447
      | derive (T, S) = weaken T (type_variable T) S;
wenzelm@36103
   448
  in derive end;
wenzelm@19529
   449
wenzelm@35961
   450
fun classrel_derivation algebra class_relation =
wenzelm@35961
   451
  let
wenzelm@35961
   452
    fun path (x, c1 :: c2 :: cs) = path (class_relation (x, c1) c2, c2 :: cs)
wenzelm@35961
   453
      | path (x, _) = x;
wenzelm@35961
   454
  in
wenzelm@35961
   455
    fn (x, c1) => fn c2 =>
wenzelm@35961
   456
      (case Graph.irreducible_paths (classes_of algebra) (c1, c2) of
wenzelm@36105
   457
        [] => raise CLASS_ERROR (No_Classrel (c1, c2))
wenzelm@35961
   458
      | cs :: _ => path (x, cs))
wenzelm@35961
   459
  end;
wenzelm@35961
   460
wenzelm@19529
   461
wenzelm@19529
   462
(* witness_sorts *)
wenzelm@19529
   463
wenzelm@19645
   464
fun witness_sorts algebra types hyps sorts =
wenzelm@19529
   465
  let
wenzelm@19645
   466
    fun le S1 S2 = sort_le algebra (S1, S2);
wenzelm@31946
   467
    fun get S2 (T, S1) = if le S1 S2 then SOME (T, S2) else NONE;
wenzelm@19645
   468
    fun mg_dom t S = SOME (mg_domain algebra t S) handle CLASS_ERROR _ => NONE;
wenzelm@19529
   469
wenzelm@19578
   470
    fun witn_sort _ [] solved_failed = (SOME (propT, []), solved_failed)
wenzelm@19578
   471
      | witn_sort path S (solved, failed) =
wenzelm@19578
   472
          if exists (le S) failed then (NONE, (solved, failed))
wenzelm@19529
   473
          else
wenzelm@31946
   474
            (case get_first (get S) solved of
wenzelm@19578
   475
              SOME w => (SOME w, (solved, failed))
wenzelm@19529
   476
            | NONE =>
wenzelm@31946
   477
                (case get_first (get S) hyps of
wenzelm@19578
   478
                  SOME w => (SOME w, (w :: solved, failed))
wenzelm@19584
   479
                | NONE => witn_types path types S (solved, failed)))
wenzelm@19529
   480
wenzelm@19578
   481
    and witn_sorts path x = fold_map (witn_sort path) x
wenzelm@19529
   482
wenzelm@19578
   483
    and witn_types _ [] S (solved, failed) = (NONE, (solved, S :: failed))
wenzelm@19578
   484
      | witn_types path (t :: ts) S solved_failed =
wenzelm@19529
   485
          (case mg_dom t S of
wenzelm@19529
   486
            SOME SS =>
wenzelm@19529
   487
              (*do not descend into stronger args (achieving termination)*)
wenzelm@19529
   488
              if exists (fn D => le D S orelse exists (le D) path) SS then
wenzelm@19578
   489
                witn_types path ts S solved_failed
wenzelm@19529
   490
              else
wenzelm@19578
   491
                let val (ws, (solved', failed')) = witn_sorts (S :: path) SS solved_failed in
wenzelm@19529
   492
                  if forall is_some ws then
wenzelm@19529
   493
                    let val w = (Type (t, map (#1 o the) ws), S)
wenzelm@19578
   494
                    in (SOME w, (w :: solved', failed')) end
wenzelm@19578
   495
                  else witn_types path ts S (solved', failed')
wenzelm@19529
   496
                end
wenzelm@19578
   497
          | NONE => witn_types path ts S solved_failed);
wenzelm@19529
   498
wenzelm@19584
   499
  in map_filter I (#1 (witn_sorts [] sorts ([], []))) end;
wenzelm@19529
   500
wenzelm@19514
   501
end;