src/Tools/subtyping.ML
author wenzelm
Fri Oct 29 22:19:27 2010 +0200 (2010-10-29)
changeset 40284 c9acf88447e6
parent 40283 805ce1d64af0
child 40285 80136c4240cc
permissions -rw-r--r--
export declarations by default, to allow other ML packages by-pass concrete syntax;
proper Args parsing for attribute syntax (required for proper treatment of morphisms when declarations are moved between contexts);
tuned;
wenzelm@40281
     1
(*  Title:      Tools/subtyping.ML
wenzelm@40281
     2
    Author:     Dmitriy Traytel, TU Muenchen
wenzelm@40281
     3
wenzelm@40281
     4
Coercive subtyping via subtype constraints.
wenzelm@40281
     5
*)
wenzelm@40281
     6
wenzelm@40281
     7
signature SUBTYPING =
wenzelm@40281
     8
sig
wenzelm@40281
     9
  datatype variance = COVARIANT | CONTRAVARIANT | INVARIANT
wenzelm@40281
    10
  val infer_types: Proof.context -> (string -> typ option) -> (indexname -> typ option) ->
wenzelm@40281
    11
    term list -> term list
wenzelm@40284
    12
  val add_type_map: term -> Context.generic -> Context.generic
wenzelm@40284
    13
  val add_coercion: term -> Context.generic -> Context.generic
wenzelm@40283
    14
  val setup: theory -> theory
wenzelm@40281
    15
end;
wenzelm@40281
    16
wenzelm@40283
    17
structure Subtyping: SUBTYPING =
wenzelm@40281
    18
struct
wenzelm@40281
    19
wenzelm@40281
    20
(** coercions data **)
wenzelm@40281
    21
wenzelm@40281
    22
datatype variance = COVARIANT | CONTRAVARIANT | INVARIANT
wenzelm@40281
    23
wenzelm@40281
    24
datatype data = Data of
wenzelm@40282
    25
  {coes: term Symreltab.table,  (*coercions table*)
wenzelm@40282
    26
   coes_graph: unit Graph.T,  (*coercions graph*)
wenzelm@40282
    27
   tmaps: (term * variance list) Symtab.table};  (*map functions*)
wenzelm@40281
    28
wenzelm@40281
    29
fun make_data (coes, coes_graph, tmaps) =
wenzelm@40281
    30
  Data {coes = coes, coes_graph = coes_graph, tmaps = tmaps};
wenzelm@40281
    31
wenzelm@40281
    32
structure Data = Generic_Data
wenzelm@40281
    33
(
wenzelm@40281
    34
  type T = data;
wenzelm@40281
    35
  val empty = make_data (Symreltab.empty, Graph.empty, Symtab.empty);
wenzelm@40281
    36
  val extend = I;
wenzelm@40281
    37
  fun merge
wenzelm@40281
    38
    (Data {coes = coes1, coes_graph = coes_graph1, tmaps = tmaps1},
wenzelm@40281
    39
      Data {coes = coes2, coes_graph = coes_graph2, tmaps = tmaps2}) =
wenzelm@40281
    40
    make_data (Symreltab.merge (op aconv) (coes1, coes2),
wenzelm@40281
    41
      Graph.merge (op =) (coes_graph1, coes_graph2),
wenzelm@40281
    42
      Symtab.merge (eq_pair (op aconv) (op =)) (tmaps1, tmaps2));
wenzelm@40281
    43
);
wenzelm@40281
    44
wenzelm@40281
    45
fun map_data f =
wenzelm@40281
    46
  Data.map (fn Data {coes, coes_graph, tmaps} =>
wenzelm@40281
    47
    make_data (f (coes, coes_graph, tmaps)));
wenzelm@40281
    48
wenzelm@40281
    49
fun map_coes f =
wenzelm@40281
    50
  map_data (fn (coes, coes_graph, tmaps) =>
wenzelm@40281
    51
    (f coes, coes_graph, tmaps));
wenzelm@40281
    52
wenzelm@40281
    53
fun map_coes_graph f =
wenzelm@40281
    54
  map_data (fn (coes, coes_graph, tmaps) =>
wenzelm@40281
    55
    (coes, f coes_graph, tmaps));
wenzelm@40281
    56
wenzelm@40281
    57
fun map_coes_and_graph f =
wenzelm@40281
    58
  map_data (fn (coes, coes_graph, tmaps) =>
wenzelm@40281
    59
    let val (coes', coes_graph') = f (coes, coes_graph);
wenzelm@40281
    60
    in (coes', coes_graph', tmaps) end);
wenzelm@40281
    61
wenzelm@40281
    62
fun map_tmaps f =
wenzelm@40281
    63
  map_data (fn (coes, coes_graph, tmaps) =>
wenzelm@40281
    64
    (coes, coes_graph, f tmaps));
wenzelm@40281
    65
wenzelm@40281
    66
fun rep_data context = Data.get context |> (fn Data args => args);
wenzelm@40281
    67
wenzelm@40281
    68
val coes_of = #coes o rep_data;
wenzelm@40281
    69
val coes_graph_of = #coes_graph o rep_data;
wenzelm@40281
    70
val tmaps_of = #tmaps o rep_data;
wenzelm@40281
    71
wenzelm@40281
    72
wenzelm@40281
    73
wenzelm@40281
    74
(** utils **)
wenzelm@40281
    75
wenzelm@40281
    76
val is_param = Type_Infer.is_param
wenzelm@40281
    77
val is_paramT = Type_Infer.is_paramT
wenzelm@40281
    78
val deref = Type_Infer.deref
wenzelm@40281
    79
fun mk_param i S = TVar (("?'a", i), S); (* TODO dup? see src/Pure/type_infer.ML *)
wenzelm@40281
    80
wenzelm@40281
    81
fun nameT (Type (s, [])) = s;
wenzelm@40281
    82
fun t_of s = Type (s, []);
wenzelm@40281
    83
fun sort_of (TFree (_, S)) = SOME S
wenzelm@40281
    84
  | sort_of (TVar (_, S)) = SOME S
wenzelm@40281
    85
  | sort_of _ = NONE;
wenzelm@40281
    86
wenzelm@40281
    87
val is_typeT = fn (Type _) => true | _ => false;
wenzelm@40282
    88
val is_compT = fn (Type (_, _ :: _)) => true | _ => false;
wenzelm@40281
    89
val is_freeT = fn (TFree _) => true | _ => false;
wenzelm@40281
    90
val is_fixedvarT = fn (TVar (xi, _)) => not (is_param xi) | _ => false;
wenzelm@40281
    91
wenzelm@40281
    92
wenzelm@40282
    93
(* unification *)  (* TODO dup? needed for weak unification *)
wenzelm@40281
    94
wenzelm@40281
    95
exception NO_UNIFIER of string * typ Vartab.table;
wenzelm@40281
    96
wenzelm@40281
    97
fun unify weak ctxt =
wenzelm@40281
    98
  let
wenzelm@40281
    99
    val thy = ProofContext.theory_of ctxt;
wenzelm@40281
   100
    val pp = Syntax.pp ctxt;
wenzelm@40281
   101
    val arity_sorts = Type.arity_sorts pp (Sign.tsig_of thy);
wenzelm@40281
   102
wenzelm@40282
   103
wenzelm@40281
   104
    (* adjust sorts of parameters *)
wenzelm@40281
   105
wenzelm@40281
   106
    fun not_of_sort x S' S =
wenzelm@40281
   107
      "Variable " ^ x ^ "::" ^ Syntax.string_of_sort ctxt S' ^ " not of sort " ^
wenzelm@40281
   108
        Syntax.string_of_sort ctxt S;
wenzelm@40281
   109
wenzelm@40281
   110
    fun meet (_, []) tye_idx = tye_idx
wenzelm@40281
   111
      | meet (Type (a, Ts), S) (tye_idx as (tye, _)) =
wenzelm@40281
   112
          meets (Ts, arity_sorts a S handle ERROR msg => raise NO_UNIFIER (msg, tye)) tye_idx
wenzelm@40281
   113
      | meet (TFree (x, S'), S) (tye_idx as (tye, _)) =
wenzelm@40281
   114
          if Sign.subsort thy (S', S) then tye_idx
wenzelm@40281
   115
          else raise NO_UNIFIER (not_of_sort x S' S, tye)
wenzelm@40281
   116
      | meet (TVar (xi, S'), S) (tye_idx as (tye, idx)) =
wenzelm@40281
   117
          if Sign.subsort thy (S', S) then tye_idx
wenzelm@40281
   118
          else if Type_Infer.is_param xi then
wenzelm@40281
   119
            (Vartab.update_new (xi, mk_param idx (Sign.inter_sort thy (S', S))) tye, idx + 1)
wenzelm@40281
   120
          else raise NO_UNIFIER (not_of_sort (Term.string_of_vname xi) S' S, tye)
wenzelm@40281
   121
    and meets (T :: Ts, S :: Ss) (tye_idx as (tye, _)) =
wenzelm@40281
   122
          meets (Ts, Ss) (meet (deref tye T, S) tye_idx)
wenzelm@40281
   123
      | meets _ tye_idx = tye_idx;
wenzelm@40281
   124
wenzelm@40281
   125
    val weak_meet = if weak then fn _ => I else meet
wenzelm@40281
   126
wenzelm@40281
   127
wenzelm@40281
   128
    (* occurs check and assignment *)
wenzelm@40281
   129
wenzelm@40281
   130
    fun occurs_check tye xi (TVar (xi', _)) =
wenzelm@40281
   131
          if xi = xi' then raise NO_UNIFIER ("Occurs check!", tye)
wenzelm@40281
   132
          else
wenzelm@40281
   133
            (case Vartab.lookup tye xi' of
wenzelm@40281
   134
              NONE => ()
wenzelm@40281
   135
            | SOME T => occurs_check tye xi T)
wenzelm@40281
   136
      | occurs_check tye xi (Type (_, Ts)) = List.app (occurs_check tye xi) Ts
wenzelm@40281
   137
      | occurs_check _ _ _ = ();
wenzelm@40281
   138
wenzelm@40281
   139
    fun assign xi (T as TVar (xi', _)) S env =
wenzelm@40281
   140
          if xi = xi' then env
wenzelm@40281
   141
          else env |> weak_meet (T, S) |>> Vartab.update_new (xi, T)
wenzelm@40281
   142
      | assign xi T S (env as (tye, _)) =
wenzelm@40281
   143
          (occurs_check tye xi T; env |> weak_meet (T, S) |>> Vartab.update_new (xi, T));
wenzelm@40281
   144
wenzelm@40281
   145
wenzelm@40281
   146
    (* unification *)
wenzelm@40281
   147
wenzelm@40281
   148
    fun show_tycon (a, Ts) =
wenzelm@40281
   149
      quote (Syntax.string_of_typ ctxt (Type (a, replicate (length Ts) dummyT)));
wenzelm@40281
   150
wenzelm@40281
   151
    fun unif (T1, T2) (env as (tye, _)) =
wenzelm@40281
   152
      (case pairself (`is_paramT o deref tye) (T1, T2) of
wenzelm@40281
   153
        ((true, TVar (xi, S)), (_, T)) => assign xi T S env
wenzelm@40281
   154
      | ((_, T), (true, TVar (xi, S))) => assign xi T S env
wenzelm@40281
   155
      | ((_, Type (a, Ts)), (_, Type (b, Us))) =>
wenzelm@40281
   156
          if weak andalso null Ts andalso null Us then env
wenzelm@40281
   157
          else if a <> b then
wenzelm@40281
   158
            raise NO_UNIFIER
wenzelm@40281
   159
              ("Clash of types " ^ show_tycon (a, Ts) ^ " and " ^ show_tycon (b, Us), tye)
wenzelm@40281
   160
          else fold unif (Ts ~~ Us) env
wenzelm@40281
   161
      | ((_, T), (_, U)) => if T = U then env else raise NO_UNIFIER ("", tye));
wenzelm@40281
   162
wenzelm@40281
   163
  in unif end;
wenzelm@40281
   164
wenzelm@40281
   165
val weak_unify = unify true;
wenzelm@40281
   166
val strong_unify = unify false;
wenzelm@40281
   167
wenzelm@40281
   168
wenzelm@40281
   169
(* Typ_Graph shortcuts *)
wenzelm@40281
   170
wenzelm@40281
   171
val add_edge = Typ_Graph.add_edge_acyclic;
wenzelm@40281
   172
fun get_preds G T = Typ_Graph.all_preds G [T];
wenzelm@40281
   173
fun get_succs G T = Typ_Graph.all_succs G [T];
wenzelm@40281
   174
fun maybe_new_typnode T G = perhaps (try (Typ_Graph.new_node (T, ()))) G;
wenzelm@40281
   175
fun maybe_new_typnodes Ts G = fold maybe_new_typnode Ts G;
wenzelm@40282
   176
fun new_imm_preds G Ts =
wenzelm@40281
   177
  subtract (op =) Ts (distinct (op =) (maps (Typ_Graph.imm_preds G) Ts));
wenzelm@40282
   178
fun new_imm_succs G Ts =
wenzelm@40281
   179
  subtract op= Ts (distinct (op =) (maps (Typ_Graph.imm_succs G) Ts));
wenzelm@40281
   180
wenzelm@40281
   181
wenzelm@40281
   182
(* Graph shortcuts *)
wenzelm@40281
   183
wenzelm@40281
   184
fun maybe_new_node s G = perhaps (try (Graph.new_node (s, ()))) G
wenzelm@40281
   185
fun maybe_new_nodes ss G = fold maybe_new_node ss G
wenzelm@40281
   186
wenzelm@40281
   187
wenzelm@40281
   188
wenzelm@40281
   189
(** error messages **)
wenzelm@40281
   190
wenzelm@40281
   191
fun prep_output ctxt tye bs ts Ts =
wenzelm@40281
   192
  let
wenzelm@40281
   193
    val (Ts_bTs', ts') = Type_Infer.finish ctxt tye (Ts @ map snd bs, ts);
wenzelm@40281
   194
    val (Ts', Ts'') = chop (length Ts) Ts_bTs';
wenzelm@40281
   195
    fun prep t =
wenzelm@40281
   196
      let val xs = rev (Term.variant_frees t (rev (map fst bs ~~ Ts'')))
wenzelm@40281
   197
      in Term.subst_bounds (map Syntax.mark_boundT xs, t) end;
wenzelm@40281
   198
  in (map prep ts', Ts') end;
wenzelm@40281
   199
wenzelm@40281
   200
fun err_loose i = error ("Loose bound variable: B." ^ string_of_int i);
wenzelm@40281
   201
wenzelm@40281
   202
fun inf_failed msg =
wenzelm@40281
   203
  "Subtype inference failed" ^ (if msg = "" then "" else ": " ^ msg) ^ "\n\n";
wenzelm@40281
   204
wenzelm@40281
   205
fun err_appl ctxt msg tye bs t T u U =
wenzelm@40281
   206
  let val ([t', u'], [T', U']) = prep_output ctxt tye bs [t, u] [T, U]
wenzelm@40281
   207
  in error (inf_failed msg ^ Type.appl_error (Syntax.pp ctxt) t' T' u' U' ^ "\n") end;
wenzelm@40281
   208
wenzelm@40281
   209
fun err_subtype ctxt msg tye (bs, t $ u, U, V, U') =
wenzelm@40281
   210
  err_appl ctxt msg tye bs t (U --> V) u U';
wenzelm@40281
   211
wenzelm@40281
   212
fun err_list ctxt msg tye Ts =
wenzelm@40281
   213
  let
wenzelm@40281
   214
    val (_, Ts') = prep_output ctxt tye [] [] Ts;
wenzelm@40281
   215
    val text = cat_lines ([inf_failed msg,
wenzelm@40281
   216
      "Cannot unify a list of types that should be the same,",
wenzelm@40281
   217
      "according to suptype dependencies:",
wenzelm@40281
   218
      (Pretty.string_of (Pretty.list "[" "]" (map (Pretty.typ (Syntax.pp ctxt)) Ts')))]);
wenzelm@40281
   219
  in
wenzelm@40281
   220
    error text
wenzelm@40281
   221
  end;
wenzelm@40281
   222
wenzelm@40281
   223
fun err_bound ctxt msg tye packs =
wenzelm@40281
   224
  let
wenzelm@40281
   225
    val pp = Syntax.pp ctxt;
wenzelm@40281
   226
    val (ts, Ts) = fold
wenzelm@40281
   227
      (fn (bs, t $ u, U, _, U') => fn (ts, Ts) =>
wenzelm@40281
   228
        let val (t', T') = prep_output ctxt tye bs [t, u] [U, U']
wenzelm@40282
   229
        in (t' :: ts, T' :: Ts) end)
wenzelm@40281
   230
      packs ([], []);
wenzelm@40281
   231
    val text = cat_lines ([inf_failed msg, "Cannot fullfill subtype constraints:"] @
wenzelm@40281
   232
        (map2 (fn [t, u] => fn [T, U] => Pretty.string_of (
wenzelm@40281
   233
          Pretty.block [
wenzelm@40281
   234
            Pretty.typ pp T, Pretty.brk 2, Pretty.str "<:", Pretty.brk 2, Pretty.typ pp U,
wenzelm@40281
   235
            Pretty.brk 3, Pretty.str "from function application", Pretty.brk 2,
wenzelm@40281
   236
            Pretty.block [Pretty.term pp t, Pretty.brk 1, Pretty.term pp u]]))
wenzelm@40281
   237
        ts Ts))
wenzelm@40281
   238
  in
wenzelm@40281
   239
    error text
wenzelm@40281
   240
  end;
wenzelm@40281
   241
wenzelm@40281
   242
wenzelm@40281
   243
wenzelm@40281
   244
(** constraint generation **)
wenzelm@40281
   245
wenzelm@40281
   246
fun generate_constraints ctxt =
wenzelm@40281
   247
  let
wenzelm@40281
   248
    fun gen cs _ (Const (_, T)) tye_idx = (T, tye_idx, cs)
wenzelm@40281
   249
      | gen cs _ (Free (_, T)) tye_idx = (T, tye_idx, cs)
wenzelm@40281
   250
      | gen cs _ (Var (_, T)) tye_idx = (T, tye_idx, cs)
wenzelm@40281
   251
      | gen cs bs (Bound i) tye_idx =
wenzelm@40281
   252
          (snd (nth bs i handle Subscript => err_loose i), tye_idx, cs)
wenzelm@40281
   253
      | gen cs bs (Abs (x, T, t)) tye_idx =
wenzelm@40281
   254
          let val (U, tye_idx', cs') = gen cs ((x, T) :: bs) t tye_idx
wenzelm@40281
   255
          in (T --> U, tye_idx', cs') end
wenzelm@40281
   256
      | gen cs bs (t $ u) tye_idx =
wenzelm@40281
   257
          let
wenzelm@40281
   258
            val (T, tye_idx', cs') = gen cs bs t tye_idx;
wenzelm@40281
   259
            val (U', (tye, idx), cs'') = gen cs' bs u tye_idx';
wenzelm@40281
   260
            val U = mk_param idx [];
wenzelm@40281
   261
            val V = mk_param (idx + 1) [];
wenzelm@40281
   262
            val tye_idx''= strong_unify ctxt (U --> V, T) (tye, idx + 2)
wenzelm@40281
   263
              handle NO_UNIFIER (msg, tye') => err_appl ctxt msg tye' bs t T u U;
wenzelm@40281
   264
            val error_pack = (bs, t $ u, U, V, U');
wenzelm@40281
   265
          in (V, tye_idx'', ((U', U), error_pack) :: cs'') end;
wenzelm@40281
   266
  in
wenzelm@40281
   267
    gen [] []
wenzelm@40281
   268
  end;
wenzelm@40281
   269
wenzelm@40281
   270
wenzelm@40281
   271
wenzelm@40281
   272
(** constraint resolution **)
wenzelm@40281
   273
wenzelm@40281
   274
exception BOUND_ERROR of string;
wenzelm@40281
   275
wenzelm@40281
   276
fun process_constraints ctxt cs tye_idx =
wenzelm@40281
   277
  let
wenzelm@40281
   278
    val coes_graph = coes_graph_of (Context.Proof ctxt);
wenzelm@40281
   279
    val tmaps = tmaps_of (Context.Proof ctxt);
wenzelm@40281
   280
    val tsig = Sign.tsig_of (ProofContext.theory_of ctxt);
wenzelm@40281
   281
    val pp = Syntax.pp ctxt;
wenzelm@40281
   282
    val arity_sorts = Type.arity_sorts pp tsig;
wenzelm@40281
   283
    val subsort = Type.subsort tsig;
wenzelm@40281
   284
wenzelm@40281
   285
    fun split_cs _ [] = ([], [])
wenzelm@40282
   286
      | split_cs f (c :: cs) =
wenzelm@40281
   287
          (case pairself f (fst c) of
wenzelm@40281
   288
            (false, false) => apsnd (cons c) (split_cs f cs)
wenzelm@40281
   289
          | _ => apfst (cons c) (split_cs f cs));
wenzelm@40281
   290
wenzelm@40282
   291
wenzelm@40281
   292
    (* check whether constraint simplification will terminate using weak unification *)
wenzelm@40282
   293
wenzelm@40281
   294
    val _ = fold (fn (TU, error_pack) => fn tye_idx =>
wenzelm@40281
   295
      (weak_unify ctxt TU tye_idx handle NO_UNIFIER (msg, tye) =>
wenzelm@40281
   296
        err_subtype ctxt ("Weak unification of subtype constraints fails:\n" ^ msg)
wenzelm@40281
   297
          tye error_pack)) cs tye_idx;
wenzelm@40281
   298
wenzelm@40281
   299
wenzelm@40281
   300
    (* simplify constraints *)
wenzelm@40282
   301
wenzelm@40281
   302
    fun simplify_constraints cs tye_idx =
wenzelm@40281
   303
      let
wenzelm@40281
   304
        fun contract a Ts Us error_pack done todo tye idx =
wenzelm@40281
   305
          let
wenzelm@40281
   306
            val arg_var =
wenzelm@40281
   307
              (case Symtab.lookup tmaps a of
wenzelm@40281
   308
                (*everything is invariant for unknown constructors*)
wenzelm@40281
   309
                NONE => replicate (length Ts) INVARIANT
wenzelm@40281
   310
              | SOME av => snd av);
wenzelm@40281
   311
            fun new_constraints (variance, constraint) (cs, tye_idx) =
wenzelm@40281
   312
              (case variance of
wenzelm@40281
   313
                COVARIANT => (constraint :: cs, tye_idx)
wenzelm@40281
   314
              | CONTRAVARIANT => (swap constraint :: cs, tye_idx)
wenzelm@40281
   315
              | INVARIANT => (cs, strong_unify ctxt constraint tye_idx
wenzelm@40281
   316
                  handle NO_UNIFIER (msg, tye) => err_subtype ctxt msg tye error_pack));
wenzelm@40281
   317
            val (new, (tye', idx')) = apfst (fn cs => (cs ~~ replicate (length cs) error_pack))
wenzelm@40281
   318
              (fold new_constraints (arg_var ~~ (Ts ~~ Us)) ([], (tye, idx)));
wenzelm@40281
   319
            val test_update = is_compT orf is_freeT orf is_fixedvarT;
wenzelm@40281
   320
            val (ch, done') =
wenzelm@40281
   321
              if not (null new) then ([],   done)
wenzelm@40281
   322
              else split_cs (test_update o deref tye') done;
wenzelm@40281
   323
            val todo' = ch @ todo;
wenzelm@40281
   324
          in
wenzelm@40281
   325
            simplify done' (new @ todo') (tye', idx')
wenzelm@40281
   326
          end
wenzelm@40281
   327
        (*xi is definitely a parameter*)
wenzelm@40281
   328
        and expand varleq xi S a Ts error_pack done todo tye idx =
wenzelm@40281
   329
          let
wenzelm@40281
   330
            val n = length Ts;
wenzelm@40281
   331
            val args = map2 mk_param (idx upto idx + n - 1) (arity_sorts a S);
wenzelm@40281
   332
            val tye' = Vartab.update_new (xi, Type(a, args)) tye;
wenzelm@40281
   333
            val (ch, done') = split_cs (is_compT o deref tye') done;
wenzelm@40281
   334
            val todo' = ch @ todo;
wenzelm@40281
   335
            val new =
wenzelm@40281
   336
              if varleq then (Type(a, args), Type (a, Ts))
wenzelm@40281
   337
              else (Type (a, Ts), Type(a, args));
wenzelm@40281
   338
          in
wenzelm@40281
   339
            simplify done' ((new, error_pack) :: todo') (tye', idx + n)
wenzelm@40281
   340
          end
wenzelm@40281
   341
        (*TU is a pair of a parameter and a free/fixed variable*)
wenzelm@40281
   342
        and eliminate TU error_pack done todo tye idx =
wenzelm@40281
   343
          let
wenzelm@40281
   344
            val [TVar (xi, S)] = filter is_paramT TU;
wenzelm@40281
   345
            val [T] = filter_out is_paramT TU;
wenzelm@40281
   346
            val SOME S' = sort_of T;
wenzelm@40281
   347
            val test_update = if is_freeT T then is_freeT else is_fixedvarT;
wenzelm@40281
   348
            val tye' = Vartab.update_new (xi, T) tye;
wenzelm@40281
   349
            val (ch, done') = split_cs (test_update o deref tye') done;
wenzelm@40281
   350
            val todo' = ch @ todo;
wenzelm@40281
   351
          in
wenzelm@40281
   352
            if subsort (S', S) (*TODO check this*)
wenzelm@40281
   353
            then simplify done' todo' (tye', idx)
wenzelm@40281
   354
            else err_subtype ctxt "Sort mismatch" tye error_pack
wenzelm@40281
   355
          end
wenzelm@40281
   356
        and simplify done [] tye_idx = (done, tye_idx)
wenzelm@40281
   357
          | simplify done (((T, U), error_pack) :: todo) (tye_idx as (tye, idx)) =
wenzelm@40281
   358
              (case (deref tye T, deref tye U) of
wenzelm@40281
   359
                (Type (a, []), Type (b, [])) =>
wenzelm@40281
   360
                  if a = b then simplify done todo tye_idx
wenzelm@40281
   361
                  else if Graph.is_edge coes_graph (a, b) then simplify done todo tye_idx
wenzelm@40281
   362
                  else err_subtype ctxt (a ^" is not a subtype of " ^ b) (fst tye_idx) error_pack
wenzelm@40281
   363
              | (Type (a, Ts), Type (b, Us)) =>
wenzelm@40281
   364
                  if a<>b then err_subtype ctxt "Different constructors" (fst tye_idx) error_pack
wenzelm@40281
   365
                  else contract a Ts Us error_pack done todo tye idx
wenzelm@40282
   366
              | (TVar (xi, S), Type (a, Ts as (_ :: _))) =>
wenzelm@40281
   367
                  expand true xi S a Ts error_pack done todo tye idx
wenzelm@40282
   368
              | (Type (a, Ts as (_ :: _)), TVar (xi, S)) =>
wenzelm@40281
   369
                  expand false xi S a Ts error_pack done todo tye idx
wenzelm@40281
   370
              | (T, U) =>
wenzelm@40281
   371
                  if T = U then simplify done todo tye_idx
wenzelm@40282
   372
                  else if exists (is_freeT orf is_fixedvarT) [T, U] andalso
wenzelm@40281
   373
                    exists is_paramT [T, U]
wenzelm@40281
   374
                  then eliminate [T, U] error_pack done todo tye idx
wenzelm@40281
   375
                  else if exists (is_freeT orf is_fixedvarT) [T, U]
wenzelm@40281
   376
                  then err_subtype ctxt "Not eliminated free/fixed variables"
wenzelm@40281
   377
                        (fst tye_idx) error_pack
wenzelm@40282
   378
                  else simplify (((T, U), error_pack) :: done) todo tye_idx);
wenzelm@40281
   379
      in
wenzelm@40281
   380
        simplify [] cs tye_idx
wenzelm@40281
   381
      end;
wenzelm@40281
   382
wenzelm@40281
   383
wenzelm@40281
   384
    (* do simplification *)
wenzelm@40282
   385
wenzelm@40281
   386
    val (cs', tye_idx') = simplify_constraints cs tye_idx;
wenzelm@40281
   387
wenzelm@40281
   388
    fun find_error_pack lower T' =
wenzelm@40281
   389
      map snd (filter (fn ((T, U), _) => if lower then T' = U else T' = T) cs');
wenzelm@40281
   390
wenzelm@40282
   391
    fun unify_list (T :: Ts) tye_idx =
wenzelm@40281
   392
      fold (fn U => fn tye_idx => strong_unify ctxt (T, U) tye_idx
wenzelm@40282
   393
        handle NO_UNIFIER (msg, tye) => err_list ctxt msg tye (T :: Ts))
wenzelm@40281
   394
      Ts tye_idx;
wenzelm@40281
   395
wenzelm@40281
   396
    (*styps stands either for supertypes or for subtypes of a type T
wenzelm@40281
   397
      in terms of the subtype-relation (excluding T itself)*)
wenzelm@40282
   398
    fun styps super T =
wenzelm@40281
   399
      (if super then Graph.imm_succs else Graph.imm_preds) coes_graph T
wenzelm@40281
   400
        handle Graph.UNDEF _ => [];
wenzelm@40281
   401
wenzelm@40282
   402
    fun minmax sup (T :: Ts) =
wenzelm@40281
   403
      let
wenzelm@40281
   404
        fun adjust T U = if sup then (T, U) else (U, T);
wenzelm@40281
   405
        fun extract T [] = T
wenzelm@40282
   406
          | extract T (U :: Us) =
wenzelm@40281
   407
              if Graph.is_edge coes_graph (adjust T U) then extract T Us
wenzelm@40281
   408
              else if Graph.is_edge coes_graph (adjust U T) then extract U Us
wenzelm@40281
   409
              else raise BOUND_ERROR "Uncomparable types in type list";
wenzelm@40281
   410
      in
wenzelm@40281
   411
        t_of (extract T Ts)
wenzelm@40281
   412
      end;
wenzelm@40281
   413
wenzelm@40282
   414
    fun ex_styp_of_sort super T styps_and_sorts =
wenzelm@40281
   415
      let
wenzelm@40281
   416
        fun adjust T U = if super then (T, U) else (U, T);
wenzelm@40282
   417
        fun styp_test U Ts = forall
wenzelm@40281
   418
          (fn T => T = U orelse Graph.is_edge coes_graph (adjust U T)) Ts;
wenzelm@40281
   419
        fun fitting Ts S U = Type.of_sort tsig (t_of U, S) andalso styp_test U Ts
wenzelm@40281
   420
      in
wenzelm@40281
   421
        forall (fn (Ts, S) => exists (fitting Ts S) (T :: styps super T)) styps_and_sorts
wenzelm@40281
   422
      end;
wenzelm@40281
   423
wenzelm@40281
   424
    (* computes the tightest possible, correct assignment for 'a::S
wenzelm@40281
   425
       e.g. in the supremum case (sup = true):
wenzelm@40281
   426
               ------- 'a::S---
wenzelm@40281
   427
              /        /    \  \
wenzelm@40281
   428
             /        /      \  \
wenzelm@40281
   429
        'b::C1   'c::C2 ...  T1 T2 ...
wenzelm@40281
   430
wenzelm@40281
   431
       sorts - list of sorts [C1, C2, ...]
wenzelm@40281
   432
       T::Ts - non-empty list of base types [T1, T2, ...]
wenzelm@40281
   433
    *)
wenzelm@40282
   434
    fun tightest sup S styps_and_sorts (T :: Ts) =
wenzelm@40281
   435
      let
wenzelm@40281
   436
        fun restriction T = Type.of_sort tsig (t_of T, S)
wenzelm@40281
   437
          andalso ex_styp_of_sort (not sup) T styps_and_sorts;
wenzelm@40281
   438
        fun candidates T = inter (op =) (filter restriction (T :: styps sup T));
wenzelm@40281
   439
      in
wenzelm@40281
   440
        (case fold candidates Ts (filter restriction (T :: styps sup T)) of
wenzelm@40281
   441
          [] => raise BOUND_ERROR ("No " ^ (if sup then "supremum" else "infimum"))
wenzelm@40281
   442
        | [T] => t_of T
wenzelm@40281
   443
        | Ts => minmax sup Ts)
wenzelm@40281
   444
      end;
wenzelm@40281
   445
wenzelm@40281
   446
    fun build_graph G [] tye_idx = (G, tye_idx)
wenzelm@40282
   447
      | build_graph G ((T, U) :: cs) tye_idx =
wenzelm@40281
   448
        if T = U then build_graph G cs tye_idx
wenzelm@40281
   449
        else
wenzelm@40281
   450
          let
wenzelm@40281
   451
            val G' = maybe_new_typnodes [T, U] G;
wenzelm@40281
   452
            val (G'', tye_idx') = (add_edge (T, U) G', tye_idx)
wenzelm@40281
   453
              handle Typ_Graph.CYCLES cycles =>
wenzelm@40281
   454
                let
wenzelm@40281
   455
                  val (tye, idx) = fold unify_list cycles tye_idx
wenzelm@40281
   456
                in
wenzelm@40281
   457
                  (*all cycles collapse to one node,
wenzelm@40281
   458
                    because all of them share at least the nodes x and y*)
wenzelm@40281
   459
                  collapse (tye, idx) (distinct (op =) (flat cycles)) G
wenzelm@40281
   460
                end;
wenzelm@40281
   461
          in
wenzelm@40281
   462
            build_graph G'' cs tye_idx'
wenzelm@40281
   463
          end
wenzelm@40281
   464
    and collapse (tye, idx) nodes G = (*nodes non-empty list*)
wenzelm@40281
   465
      let
wenzelm@40281
   466
        val T = hd nodes;
wenzelm@40281
   467
        val P = new_imm_preds G nodes;
wenzelm@40281
   468
        val S = new_imm_succs G nodes;
wenzelm@40281
   469
        val G' = Typ_Graph.del_nodes (tl nodes) G;
wenzelm@40281
   470
      in
wenzelm@40281
   471
        build_graph G' (map (fn x => (x, T)) P @ map (fn x => (T, x)) S) (tye, idx)
wenzelm@40281
   472
      end;
wenzelm@40281
   473
wenzelm@40281
   474
    fun assign_bound lower G key (tye_idx as (tye, _)) =
wenzelm@40281
   475
      if is_paramT (deref tye key) then
wenzelm@40281
   476
        let
wenzelm@40281
   477
          val TVar (xi, S) = deref tye key;
wenzelm@40281
   478
          val get_bound = if lower then get_preds else get_succs;
wenzelm@40281
   479
          val raw_bound = get_bound G key;
wenzelm@40281
   480
          val bound = map (deref tye) raw_bound;
wenzelm@40281
   481
          val not_params = filter_out is_paramT bound;
wenzelm@40282
   482
          fun to_fulfil T =
wenzelm@40281
   483
            (case sort_of T of
wenzelm@40281
   484
              NONE => NONE
wenzelm@40282
   485
            | SOME S =>
wenzelm@40281
   486
                SOME (map nameT (filter_out is_paramT (map (deref tye) (get_bound G T))), S));
wenzelm@40281
   487
          val styps_and_sorts = distinct (op =) (map_filter to_fulfil raw_bound);
wenzelm@40281
   488
          val assignment =
wenzelm@40281
   489
            if null bound orelse null not_params then NONE
wenzelm@40281
   490
            else SOME (tightest lower S styps_and_sorts (map nameT not_params)
wenzelm@40281
   491
                handle BOUND_ERROR msg => err_bound ctxt msg tye (find_error_pack lower key))
wenzelm@40281
   492
        in
wenzelm@40281
   493
          (case assignment of
wenzelm@40281
   494
            NONE => tye_idx
wenzelm@40281
   495
          | SOME T =>
wenzelm@40281
   496
              if is_paramT T then tye_idx
wenzelm@40281
   497
              else if lower then (*upper bound check*)
wenzelm@40281
   498
                let
wenzelm@40281
   499
                  val other_bound = map (deref tye) (get_succs G key);
wenzelm@40281
   500
                  val s = nameT T;
wenzelm@40281
   501
                in
wenzelm@40281
   502
                  if subset (op = o apfst nameT) (filter is_typeT other_bound, s :: styps true s)
wenzelm@40281
   503
                  then apfst (Vartab.update (xi, T)) tye_idx
wenzelm@40281
   504
                  else err_bound ctxt ("Assigned simple type " ^ s ^
wenzelm@40281
   505
                    " clashes with the upper bound of variable " ^
wenzelm@40281
   506
                    Syntax.string_of_typ ctxt (TVar(xi, S))) tye (find_error_pack (not lower) key)
wenzelm@40281
   507
                end
wenzelm@40281
   508
              else apfst (Vartab.update (xi, T)) tye_idx)
wenzelm@40281
   509
        end
wenzelm@40281
   510
      else tye_idx;
wenzelm@40281
   511
wenzelm@40281
   512
    val assign_lb = assign_bound true;
wenzelm@40281
   513
    val assign_ub = assign_bound false;
wenzelm@40281
   514
wenzelm@40281
   515
    fun assign_alternating ts' ts G tye_idx =
wenzelm@40281
   516
      if ts' = ts then tye_idx
wenzelm@40281
   517
      else
wenzelm@40281
   518
        let
wenzelm@40281
   519
          val (tye_idx' as (tye, _)) = fold (assign_lb G) ts tye_idx
wenzelm@40281
   520
            |> fold (assign_ub G) ts;
wenzelm@40281
   521
        in
wenzelm@40281
   522
          assign_alternating ts (filter (is_paramT o deref tye) ts) G tye_idx'
wenzelm@40281
   523
        end;
wenzelm@40281
   524
wenzelm@40281
   525
    (*Unify all weakly connected components of the constraint forest,
wenzelm@40282
   526
      that contain only params. These are the only WCCs that contain
wenzelm@40281
   527
      params anyway.*)
wenzelm@40281
   528
    fun unify_params G (tye_idx as (tye, _)) =
wenzelm@40281
   529
      let
wenzelm@40281
   530
        val max_params = filter (is_paramT o deref tye) (Typ_Graph.maximals G);
wenzelm@40281
   531
        val to_unify = map (fn T => T :: get_preds G T) max_params;
wenzelm@40281
   532
      in
wenzelm@40281
   533
        fold unify_list to_unify tye_idx
wenzelm@40281
   534
      end;
wenzelm@40281
   535
wenzelm@40281
   536
    fun solve_constraints G tye_idx = tye_idx
wenzelm@40281
   537
      |> assign_alternating [] (Typ_Graph.keys G) G
wenzelm@40281
   538
      |> unify_params G;
wenzelm@40281
   539
  in
wenzelm@40281
   540
    build_graph Typ_Graph.empty (map fst cs') tye_idx'
wenzelm@40281
   541
      |-> solve_constraints
wenzelm@40281
   542
  end;
wenzelm@40281
   543
wenzelm@40281
   544
wenzelm@40281
   545
wenzelm@40281
   546
(** coercion insertion **)
wenzelm@40281
   547
wenzelm@40281
   548
fun insert_coercions ctxt tye ts =
wenzelm@40281
   549
  let
wenzelm@40281
   550
    fun deep_deref T =
wenzelm@40281
   551
      (case deref tye T of
wenzelm@40281
   552
        Type (a, Ts) => Type (a, map deep_deref Ts)
wenzelm@40281
   553
      | U => U);
wenzelm@40281
   554
wenzelm@40281
   555
    fun gen_coercion ((Type (a, [])), (Type (b, []))) =
wenzelm@40281
   556
          if a = b
wenzelm@40281
   557
          then Abs (Name.uu, Type (a, []), Bound 0)
wenzelm@40281
   558
          else
wenzelm@40281
   559
            (case Symreltab.lookup (coes_of (Context.Proof ctxt)) (a, b) of
wenzelm@40281
   560
              NONE => raise Fail (a ^ " is not a subtype of " ^ b)
wenzelm@40281
   561
            | SOME co => co)
wenzelm@40281
   562
      | gen_coercion ((Type (a, Ts)), (Type (b, Us))) =
wenzelm@40281
   563
          if a <> b
wenzelm@40281
   564
          then raise raise Fail ("Different constructors: " ^ a ^ " and " ^ b)
wenzelm@40281
   565
          else
wenzelm@40281
   566
            let
wenzelm@40282
   567
              fun inst t Ts =
wenzelm@40282
   568
                Term.subst_vars
wenzelm@40281
   569
                  (((Term.add_tvar_namesT (fastype_of t) []) ~~ rev Ts), []) t;
wenzelm@40281
   570
              fun sub_co (COVARIANT, TU) = gen_coercion TU
wenzelm@40281
   571
                | sub_co (CONTRAVARIANT, TU) = gen_coercion (swap TU);
wenzelm@40281
   572
              fun ts_of [] = []
wenzelm@40282
   573
                | ts_of (Type ("fun", [x1, x2]) :: xs) = x1 :: x2 :: (ts_of xs);
wenzelm@40281
   574
            in
wenzelm@40281
   575
              (case Symtab.lookup (tmaps_of (Context.Proof ctxt)) a of
wenzelm@40281
   576
                NONE => raise Fail ("No map function for " ^ a ^ " known")
wenzelm@40281
   577
              | SOME tmap =>
wenzelm@40281
   578
                  let
wenzelm@40281
   579
                    val used_coes = map sub_co ((snd tmap) ~~ (Ts ~~ Us));
wenzelm@40281
   580
                  in
wenzelm@40281
   581
                    Term.list_comb
wenzelm@40281
   582
                      (inst (fst tmap) (ts_of (map fastype_of used_coes)), used_coes)
wenzelm@40281
   583
                  end)
wenzelm@40281
   584
            end
wenzelm@40281
   585
      | gen_coercion (T, U) =
wenzelm@40281
   586
          if Type.could_unify (T, U)
wenzelm@40281
   587
          then Abs (Name.uu, T, Bound 0)
wenzelm@40281
   588
          else raise Fail ("Cannot generate coercion from "
wenzelm@40281
   589
            ^ Syntax.string_of_typ ctxt T ^ " to " ^ Syntax.string_of_typ ctxt U);
wenzelm@40281
   590
wenzelm@40281
   591
    fun insert _ (Const (c, T)) =
wenzelm@40281
   592
          let val T' = deep_deref T;
wenzelm@40281
   593
          in (Const (c, T'), T') end
wenzelm@40281
   594
      | insert _ (Free (x, T)) =
wenzelm@40281
   595
          let val T' = deep_deref T;
wenzelm@40281
   596
          in (Free (x, T'), T') end
wenzelm@40281
   597
      | insert _ (Var (xi, T)) =
wenzelm@40281
   598
          let val T' = deep_deref T;
wenzelm@40281
   599
          in (Var (xi, T'), T') end
wenzelm@40281
   600
      | insert bs (Bound i) =
wenzelm@40281
   601
          let val T = nth bs i handle Subscript =>
wenzelm@40281
   602
            raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], []);
wenzelm@40281
   603
          in (Bound i, T) end
wenzelm@40281
   604
      | insert bs (Abs (x, T, t)) =
wenzelm@40281
   605
          let
wenzelm@40281
   606
            val T' = deep_deref T;
wenzelm@40282
   607
            val (t', T'') = insert (T' :: bs) t;
wenzelm@40281
   608
          in
wenzelm@40281
   609
            (Abs (x, T', t'), T' --> T'')
wenzelm@40281
   610
          end
wenzelm@40281
   611
      | insert bs (t $ u) =
wenzelm@40281
   612
          let
wenzelm@40281
   613
            val (t', Type ("fun", [U, T])) = insert bs t;
wenzelm@40281
   614
            val (u', U') = insert bs u;
wenzelm@40281
   615
          in
wenzelm@40281
   616
            if U <> U'
wenzelm@40281
   617
            then (t' $ (gen_coercion (U', U) $ u'), T)
wenzelm@40281
   618
            else (t' $ u', T)
wenzelm@40281
   619
          end
wenzelm@40281
   620
  in
wenzelm@40281
   621
    map (fst o insert []) ts
wenzelm@40281
   622
  end;
wenzelm@40281
   623
wenzelm@40281
   624
wenzelm@40281
   625
wenzelm@40281
   626
(** assembling the pipeline **)
wenzelm@40281
   627
wenzelm@40281
   628
fun infer_types ctxt const_type var_type raw_ts =
wenzelm@40281
   629
  let
wenzelm@40281
   630
    val (idx, ts) = Type_Infer.prepare ctxt const_type var_type raw_ts;
wenzelm@40281
   631
wenzelm@40281
   632
    fun gen_all t (tye_idx, constraints) =
wenzelm@40281
   633
      let
wenzelm@40281
   634
        val (_, tye_idx', constraints') = generate_constraints ctxt t tye_idx
wenzelm@40281
   635
      in (tye_idx', constraints' @ constraints) end;
wenzelm@40281
   636
wenzelm@40281
   637
    val (tye_idx, constraints) = fold gen_all ts ((Vartab.empty, idx), []);
wenzelm@40281
   638
    val (tye, _) = process_constraints ctxt constraints tye_idx;
wenzelm@40281
   639
    val ts' = insert_coercions ctxt tye ts;
wenzelm@40281
   640
wenzelm@40281
   641
    val (_, ts'') = Type_Infer.finish ctxt tye ([], ts');
wenzelm@40281
   642
  in ts'' end;
wenzelm@40281
   643
wenzelm@40281
   644
wenzelm@40281
   645
wenzelm@40281
   646
(** installation **)
wenzelm@40281
   647
wenzelm@40283
   648
(* term check *)
wenzelm@40283
   649
wenzelm@40281
   650
fun coercion_infer_types ctxt =
wenzelm@40281
   651
  infer_types ctxt
wenzelm@40281
   652
    (try (Consts.the_constraint (ProofContext.consts_of ctxt)))
wenzelm@40281
   653
    (ProofContext.def_type ctxt);
wenzelm@40281
   654
wenzelm@40283
   655
val add_term_check =
wenzelm@40283
   656
  Syntax.add_term_check ~100 "coercions"
wenzelm@40283
   657
    (fn xs => fn ctxt =>
wenzelm@40283
   658
      let val xs' = coercion_infer_types ctxt xs
wenzelm@40283
   659
      in if eq_list (op aconv) (xs, xs') then NONE else SOME (xs', ctxt) end);
wenzelm@40281
   660
wenzelm@40281
   661
wenzelm@40283
   662
(* declarations *)
wenzelm@40281
   663
wenzelm@40284
   664
fun add_type_map raw_t context =
wenzelm@40281
   665
  let
wenzelm@40281
   666
    val ctxt = Context.proof_of context;
wenzelm@40284
   667
    val t = singleton (Variable.polymorphic ctxt) raw_t;
wenzelm@40281
   668
wenzelm@40281
   669
    fun err_str () = "\n\nthe general type signature for a map function is" ^
wenzelm@40281
   670
      "\nf1 => f2 => ... => fn => C [x1, ..., xn] => C [x1, ..., xn]" ^
wenzelm@40281
   671
      "\nwhere C is a constructor and fi is of type (xi => yi) or (yi => xi)";
wenzelm@40281
   672
wenzelm@40281
   673
    fun gen_arg_var ([], []) = []
wenzelm@40282
   674
      | gen_arg_var ((T, T') :: Ts, (U, U') :: Us) =
wenzelm@40281
   675
          if T = U andalso T' = U' then COVARIANT :: gen_arg_var (Ts, Us)
wenzelm@40281
   676
          else if T = U' andalso T' = U then CONTRAVARIANT :: gen_arg_var (Ts, Us)
wenzelm@40281
   677
          else error ("Functions do not apply to arguments correctly:" ^ err_str ())
wenzelm@40281
   678
      | gen_arg_var (_, _) =
wenzelm@40281
   679
          error ("Different numbers of functions and arguments\n" ^ err_str ());
wenzelm@40281
   680
wenzelm@40281
   681
    (* TODO: This function is only needed to introde the fun type map
wenzelm@40281
   682
      function: "% f g h . g o h o f". There must be a better solution. *)
wenzelm@40281
   683
    fun balanced (Type (_, [])) (Type (_, [])) = true
wenzelm@40281
   684
      | balanced (Type (a, Ts)) (Type (b, Us)) =
wenzelm@40281
   685
          a = b andalso forall I (map2 balanced Ts Us)
wenzelm@40281
   686
      | balanced (TFree _) (TFree _) = true
wenzelm@40281
   687
      | balanced (TVar _) (TVar _) = true
wenzelm@40281
   688
      | balanced _ _ = false;
wenzelm@40281
   689
wenzelm@40281
   690
    fun check_map_fun (pairs, []) (Type ("fun", [T as Type (C, Ts), U as Type (_, Us)])) =
wenzelm@40281
   691
          if balanced T U
wenzelm@40282
   692
          then ((pairs, Ts ~~ Us), C)
wenzelm@40281
   693
          else if C = "fun"
wenzelm@40281
   694
            then check_map_fun (pairs @ [(hd Ts, hd (tl Ts))], []) U
wenzelm@40281
   695
            else error ("Not a proper map function:" ^ err_str ())
wenzelm@40281
   696
      | check_map_fun _ _ = error ("Not a proper map function:" ^ err_str ());
wenzelm@40281
   697
wenzelm@40281
   698
    val res = check_map_fun ([], []) (fastype_of t);
wenzelm@40281
   699
    val res_av = gen_arg_var (fst res);
wenzelm@40281
   700
  in
wenzelm@40281
   701
    map_tmaps (Symtab.update (snd res, (t, res_av))) context
wenzelm@40281
   702
  end;
wenzelm@40281
   703
wenzelm@40284
   704
fun add_coercion raw_t context =
wenzelm@40281
   705
  let
wenzelm@40281
   706
    val ctxt = Context.proof_of context;
wenzelm@40284
   707
    val t = singleton (Variable.polymorphic ctxt) raw_t;
wenzelm@40281
   708
wenzelm@40281
   709
    fun err_coercion () = error ("Bad type for coercion " ^
wenzelm@40281
   710
        Syntax.string_of_term ctxt t ^ ":\n" ^
wenzelm@40281
   711
        Syntax.string_of_typ ctxt (fastype_of t));
wenzelm@40281
   712
wenzelm@40281
   713
    val (Type ("fun", [T1, T2])) = fastype_of t
wenzelm@40281
   714
      handle Bind => err_coercion ();
wenzelm@40281
   715
wenzelm@40281
   716
    val a =
wenzelm@40281
   717
      (case T1 of
wenzelm@40281
   718
        Type (x, []) => x
wenzelm@40281
   719
      | _ => err_coercion ());
wenzelm@40281
   720
wenzelm@40281
   721
    val b =
wenzelm@40281
   722
      (case T2 of
wenzelm@40281
   723
        Type (x, []) => x
wenzelm@40281
   724
      | _ => err_coercion ());
wenzelm@40281
   725
wenzelm@40281
   726
    fun coercion_data_update (tab, G) =
wenzelm@40281
   727
      let
wenzelm@40281
   728
        val G' = maybe_new_nodes [a, b] G
wenzelm@40281
   729
        val G'' = Graph.add_edge_trans_acyclic (a, b) G'
wenzelm@40281
   730
          handle Graph.CYCLES _ => error (a ^ " is already a subtype of " ^ b ^
wenzelm@40281
   731
            "!\n\nCannot add coercion of type: " ^ a ^ " => " ^ b);
wenzelm@40281
   732
        val new_edges =
wenzelm@40281
   733
          flat (Graph.dest G'' |> map (fn (x, ys) => ys |> map_filter (fn y =>
wenzelm@40281
   734
            if Graph.is_edge G' (x, y) then NONE else SOME (x, y))));
wenzelm@40281
   735
        val G_and_new = Graph.add_edge (a, b) G';
wenzelm@40281
   736
wenzelm@40281
   737
        fun complex_coercion tab G (a, b) =
wenzelm@40281
   738
          let
wenzelm@40281
   739
            val path = hd (Graph.irreducible_paths G (a, b))
wenzelm@40281
   740
            val path' = (fst (split_last path)) ~~ tl path
wenzelm@40281
   741
          in Abs (Name.uu, Type (a, []),
wenzelm@40281
   742
              fold (fn t => fn u => t $ u) (map (the o Symreltab.lookup tab) path') (Bound 0))
wenzelm@40281
   743
          end;
wenzelm@40281
   744
wenzelm@40281
   745
        val tab' = fold
wenzelm@40281
   746
          (fn pair => fn tab => Symreltab.update (pair, complex_coercion tab G_and_new pair) tab)
wenzelm@40281
   747
          (filter (fn pair => pair <> (a, b)) new_edges)
wenzelm@40281
   748
          (Symreltab.update ((a, b), t) tab);
wenzelm@40281
   749
      in
wenzelm@40281
   750
        (tab', G'')
wenzelm@40281
   751
      end;
wenzelm@40281
   752
  in
wenzelm@40281
   753
    map_coes_and_graph coercion_data_update context
wenzelm@40281
   754
  end;
wenzelm@40281
   755
wenzelm@40283
   756
wenzelm@40283
   757
(* theory setup *)
wenzelm@40283
   758
wenzelm@40283
   759
val setup =
wenzelm@40283
   760
  Context.theory_map add_term_check #>
wenzelm@40284
   761
  Attrib.setup @{binding coercion}
wenzelm@40284
   762
    (Args.term >> (fn t => Thm.declaration_attribute (K (add_coercion t))))
wenzelm@40281
   763
    "declaration of new coercions" #>
wenzelm@40284
   764
  Attrib.setup @{binding map_function}
wenzelm@40284
   765
    (Args.term >> (fn t => Thm.declaration_attribute (K (add_type_map t))))
wenzelm@40283
   766
    "declaration of new map functions";
wenzelm@40281
   767
wenzelm@40281
   768
end;