src/Pure/type_infer.ML
author wenzelm
Tue Apr 19 10:37:38 2011 +0200 (2011-04-19)
changeset 42400 26c8c9cabb24
parent 42386 50ea65e84d98
child 42405 13ecdb3057d8
permissions -rw-r--r--
more precise treatment of existing type inference parameters;
tuned;
wenzelm@2957
     1
(*  Title:      Pure/type_infer.ML
wenzelm@2957
     2
    Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
wenzelm@2957
     3
wenzelm@39296
     4
Representation of type-inference problems.  Simple type inference.
wenzelm@2957
     5
*)
wenzelm@2957
     6
wenzelm@2957
     7
signature TYPE_INFER =
wenzelm@2957
     8
sig
wenzelm@24504
     9
  val is_param: indexname -> bool
wenzelm@39294
    10
  val is_paramT: typ -> bool
wenzelm@42400
    11
  val param_maxidx: term -> int -> int
wenzelm@42400
    12
  val param_maxidx_of: term list -> int
wenzelm@14788
    13
  val param: int -> string * sort -> typ
wenzelm@40286
    14
  val mk_param: int -> sort -> typ
wenzelm@39296
    15
  val anyT: sort -> typ
wenzelm@22771
    16
  val paramify_vars: typ -> typ
wenzelm@18339
    17
  val paramify_dummies: typ -> int -> typ * int
wenzelm@39296
    18
  val deref: typ Vartab.table -> typ -> typ
wenzelm@39296
    19
  val finish: Proof.context -> typ Vartab.table -> typ list * term list -> typ list * term list
wenzelm@39296
    20
  val fixate: Proof.context -> term list -> term list
wenzelm@39296
    21
  val prepare: Proof.context -> (string -> typ option) -> (string * int -> typ option) ->
wenzelm@39296
    22
    term list -> int * term list
wenzelm@39290
    23
  val infer_types: Proof.context -> (string -> typ option) -> (indexname -> typ option) ->
wenzelm@27263
    24
    term list -> term list
wenzelm@2957
    25
end;
wenzelm@2957
    26
wenzelm@37145
    27
structure Type_Infer: TYPE_INFER =
wenzelm@2957
    28
struct
wenzelm@2957
    29
wenzelm@22698
    30
(** type parameters and constraints **)
wenzelm@2957
    31
wenzelm@39286
    32
(* type inference parameters -- may get instantiated *)
wenzelm@2957
    33
wenzelm@24504
    34
fun is_param (x, _: int) = String.isPrefix "?" x;
wenzelm@39294
    35
wenzelm@39294
    36
fun is_paramT (TVar (xi, _)) = is_param xi
wenzelm@39294
    37
  | is_paramT _ = false;
wenzelm@39294
    38
wenzelm@42400
    39
val param_maxidx =
wenzelm@42400
    40
  (Term.fold_types o Term.fold_atyps)
wenzelm@42400
    41
    (fn (TVar (xi as (_, i), _)) => if is_param xi then Integer.max i else I | _ => I);
wenzelm@42400
    42
wenzelm@42400
    43
fun param_maxidx_of ts = fold param_maxidx ts ~1;
wenzelm@42400
    44
wenzelm@22698
    45
fun param i (x, S) = TVar (("?" ^ x, i), S);
wenzelm@22698
    46
wenzelm@39294
    47
fun mk_param i S = TVar (("?'a", i), S);
wenzelm@39294
    48
wenzelm@39296
    49
wenzelm@39296
    50
(* pre-stage parameters *)
wenzelm@39296
    51
wenzelm@39296
    52
fun anyT S = TFree ("'_dummy_", S);
wenzelm@39296
    53
wenzelm@32002
    54
val paramify_vars =
wenzelm@32146
    55
  Same.commit
wenzelm@32146
    56
    (Term_Subst.map_atypsT_same
wenzelm@32146
    57
      (fn TVar ((x, i), S) => (param i (x, S)) | _ => raise Same.SAME));
wenzelm@22771
    58
wenzelm@22698
    59
val paramify_dummies =
wenzelm@22698
    60
  let
wenzelm@22698
    61
    fun dummy S maxidx = (param (maxidx + 1) ("'dummy", S), maxidx + 1);
wenzelm@2957
    62
wenzelm@22698
    63
    fun paramify (TFree ("'_dummy_", S)) maxidx = dummy S maxidx
wenzelm@22698
    64
      | paramify (Type ("dummy", _)) maxidx = dummy [] maxidx
wenzelm@22698
    65
      | paramify (Type (a, Ts)) maxidx =
wenzelm@22698
    66
          let val (Ts', maxidx') = fold_map paramify Ts maxidx
wenzelm@22698
    67
          in (Type (a, Ts'), maxidx') end
wenzelm@22698
    68
      | paramify T maxidx = (T, maxidx);
wenzelm@22698
    69
  in paramify end;
wenzelm@2957
    70
wenzelm@2957
    71
wenzelm@2957
    72
wenzelm@39294
    73
(** prepare types/terms: create inference parameters **)
wenzelm@2957
    74
wenzelm@39294
    75
(* prepare_typ *)
wenzelm@2957
    76
wenzelm@39294
    77
fun prepare_typ typ params_idx =
wenzelm@2957
    78
  let
berghofe@32141
    79
    val (params', idx) = fold_atyps
wenzelm@42400
    80
      (fn TVar (xi, S) =>
berghofe@32141
    81
          (fn ps_idx as (ps, idx) =>
wenzelm@39287
    82
            if is_param xi andalso not (Vartab.defined ps xi)
wenzelm@39294
    83
            then (Vartab.update (xi, mk_param idx S) ps, idx + 1) else ps_idx)
berghofe@32141
    84
        | _ => I) typ params_idx;
wenzelm@2957
    85
wenzelm@39294
    86
    fun prepare (T as Type (a, Ts)) idx =
wenzelm@39294
    87
          if T = dummyT then (mk_param idx [], idx + 1)
wenzelm@39294
    88
          else
wenzelm@39294
    89
            let val (Ts', idx') = fold_map prepare Ts idx
wenzelm@39294
    90
            in (Type (a, Ts'), idx') end
wenzelm@39294
    91
      | prepare (T as TVar (xi, _)) idx =
wenzelm@20735
    92
          (case Vartab.lookup params' xi of
wenzelm@39294
    93
            NONE => T
berghofe@32141
    94
          | SOME p => p, idx)
wenzelm@39294
    95
      | prepare (TFree ("'_dummy_", S)) idx = (mk_param idx S, idx + 1)
wenzelm@39294
    96
      | prepare (T as TFree _) idx = (T, idx);
berghofe@32141
    97
wenzelm@39294
    98
    val (typ', idx') = prepare typ idx;
wenzelm@39294
    99
  in (typ', (params', idx')) end;
wenzelm@2957
   100
wenzelm@2957
   101
wenzelm@39294
   102
(* prepare_term *)
wenzelm@2957
   103
wenzelm@42143
   104
fun prepare_term ctxt const_type tm (vparams, params, idx) =
wenzelm@2957
   105
  let
berghofe@32141
   106
    fun add_vparm xi (ps_idx as (ps, idx)) =
wenzelm@20735
   107
      if not (Vartab.defined ps xi) then
wenzelm@39294
   108
        (Vartab.update (xi, mk_param idx []) ps, idx + 1)
berghofe@32141
   109
      else ps_idx;
wenzelm@2957
   110
berghofe@32141
   111
    val (vparams', idx') = fold_aterms
wenzelm@20668
   112
      (fn Var (_, Type ("_polymorphic_", _)) => I
wenzelm@20668
   113
        | Var (xi, _) => add_vparm xi
wenzelm@20668
   114
        | Free (x, _) => add_vparm (x, ~1)
wenzelm@20668
   115
        | _ => I)
berghofe@32141
   116
      tm (vparams, idx);
wenzelm@20735
   117
    fun var_param xi = the (Vartab.lookup vparams' xi);
wenzelm@2957
   118
wenzelm@39294
   119
    fun polyT_of T idx = apsnd snd (prepare_typ (paramify_vars T) (Vartab.empty, idx));
wenzelm@2957
   120
wenzelm@22698
   121
    fun constraint T t ps =
wenzelm@20668
   122
      if T = dummyT then (t, ps)
wenzelm@2957
   123
      else
wenzelm@39294
   124
        let val (T', ps') = prepare_typ T ps
wenzelm@39294
   125
        in (Type.constraint T' t, ps') end;
wenzelm@2957
   126
wenzelm@39294
   127
    fun prepare (Const ("_type_constraint_", T) $ t) ps_idx =
wenzelm@39294
   128
          let
wenzelm@42143
   129
            fun err () =
wenzelm@42143
   130
              error ("Malformed internal type constraint: " ^ Syntax.string_of_typ ctxt T);
wenzelm@42143
   131
            val A = (case T of Type ("fun", [A, B]) => if A = B then A else err () | _ => err ());
wenzelm@42143
   132
            val (A', ps_idx') = prepare_typ A ps_idx;
wenzelm@39294
   133
            val (t', ps_idx'') = prepare t ps_idx';
wenzelm@42143
   134
          in (Const ("_type_constraint_", A' --> A') $ t', ps_idx'') end
wenzelm@39294
   135
      | prepare (Const (c, T)) (ps, idx) =
wenzelm@2957
   136
          (case const_type c of
berghofe@32141
   137
            SOME U =>
wenzelm@39294
   138
              let val (U', idx') = polyT_of U idx
wenzelm@39294
   139
              in constraint T (Const (c, U')) (ps, idx') end
wenzelm@39291
   140
          | NONE => error ("Undeclared constant: " ^ quote c))
wenzelm@39294
   141
      | prepare (Var (xi, Type ("_polymorphic_", [T]))) (ps, idx) =
wenzelm@39294
   142
          let val (T', idx') = polyT_of T idx
wenzelm@39294
   143
          in (Var (xi, T'), (ps, idx')) end
wenzelm@39294
   144
      | prepare (Var (xi, T)) ps_idx = constraint T (Var (xi, var_param xi)) ps_idx
wenzelm@39294
   145
      | prepare (Free (x, T)) ps_idx = constraint T (Free (x, var_param (x, ~1))) ps_idx
wenzelm@39294
   146
      | prepare (Bound i) ps_idx = (Bound i, ps_idx)
wenzelm@39294
   147
      | prepare (Abs (x, T, t)) ps_idx =
wenzelm@39292
   148
          let
wenzelm@39294
   149
            val (T', ps_idx') = prepare_typ T ps_idx;
wenzelm@39294
   150
            val (t', ps_idx'') = prepare t ps_idx';
wenzelm@39294
   151
          in (Abs (x, T', t'), ps_idx'') end
wenzelm@39294
   152
      | prepare (t $ u) ps_idx =
wenzelm@2957
   153
          let
wenzelm@39294
   154
            val (t', ps_idx') = prepare t ps_idx;
wenzelm@39294
   155
            val (u', ps_idx'') = prepare u ps_idx';
wenzelm@39294
   156
          in (t' $ u', ps_idx'') end;
wenzelm@2957
   157
wenzelm@39294
   158
    val (tm', (params', idx'')) = prepare tm (params, idx');
berghofe@32141
   159
  in (tm', (vparams', params', idx'')) end;
wenzelm@2957
   160
wenzelm@2957
   161
wenzelm@2957
   162
wenzelm@39296
   163
(** results **)
wenzelm@2957
   164
wenzelm@39294
   165
(* dereferenced views *)
wenzelm@2957
   166
wenzelm@39294
   167
fun deref tye (T as TVar (xi, _)) =
wenzelm@39294
   168
      (case Vartab.lookup tye xi of
wenzelm@39294
   169
        NONE => T
wenzelm@39294
   170
      | SOME U => deref tye U)
wenzelm@42400
   171
  | deref _ T = T;
wenzelm@39294
   172
wenzelm@39294
   173
fun add_parms tye T =
wenzelm@32146
   174
  (case deref tye T of
wenzelm@39294
   175
    Type (_, Ts) => fold (add_parms tye) Ts
wenzelm@39294
   176
  | TVar (xi, _) => if is_param xi then insert (op =) xi else I
wenzelm@32146
   177
  | _ => I);
wenzelm@2957
   178
wenzelm@39294
   179
fun add_names tye T =
wenzelm@32146
   180
  (case deref tye T of
wenzelm@39294
   181
    Type (_, Ts) => fold (add_names tye) Ts
wenzelm@39294
   182
  | TFree (x, _) => Name.declare x
wenzelm@39294
   183
  | TVar ((x, i), _) => if is_param (x, i) then I else Name.declare x);
wenzelm@2957
   184
wenzelm@2957
   185
wenzelm@39296
   186
(* finish -- standardize remaining parameters *)
wenzelm@2957
   187
wenzelm@39294
   188
fun finish ctxt tye (Ts, ts) =
wenzelm@39294
   189
  let
wenzelm@39294
   190
    val used =
wenzelm@39294
   191
      (fold o fold_types) (add_names tye) ts (fold (add_names tye) Ts (Variable.names_of ctxt));
wenzelm@39294
   192
    val parms = rev ((fold o fold_types) (add_parms tye) ts (fold (add_parms tye) Ts []));
wenzelm@39294
   193
    val names = Name.invents used ("?" ^ Name.aT) (length parms);
wenzelm@39294
   194
    val tab = Vartab.make (parms ~~ names);
wenzelm@2957
   195
wenzelm@39294
   196
    fun finish_typ T =
wenzelm@39294
   197
      (case deref tye T of
wenzelm@39294
   198
        Type (a, Ts) => Type (a, map finish_typ Ts)
wenzelm@39294
   199
      | U as TFree _ => U
wenzelm@39294
   200
      | U as TVar (xi, S) =>
wenzelm@39294
   201
          (case Vartab.lookup tab xi of
wenzelm@39294
   202
            NONE => U
wenzelm@39295
   203
          | SOME a => TVar ((a, 0), S)));
wenzelm@39294
   204
  in (map finish_typ Ts, map (Type.strip_constraints o Term.map_types finish_typ) ts) end;
wenzelm@2957
   205
wenzelm@2957
   206
wenzelm@39296
   207
(* fixate -- introduce fresh type variables *)
wenzelm@39296
   208
wenzelm@39296
   209
fun fixate ctxt ts =
wenzelm@39296
   210
  let
wenzelm@39296
   211
    fun subst_param (xi, S) (inst, used) =
wenzelm@39296
   212
      if is_param xi then
wenzelm@39296
   213
        let
wenzelm@39296
   214
          val [a] = Name.invents used Name.aT 1;
wenzelm@39296
   215
          val used' = Name.declare a used;
wenzelm@39296
   216
        in (((xi, S), TFree (a, S)) :: inst, used') end
wenzelm@39296
   217
      else (inst, used);
wenzelm@39296
   218
    val used = (fold o fold_types) Term.declare_typ_names ts (Variable.names_of ctxt);
wenzelm@39296
   219
    val (inst, _) = fold_rev subst_param (fold Term.add_tvars ts []) ([], used);
wenzelm@39296
   220
  in (map o map_types) (Term_Subst.instantiateT inst) ts end;
wenzelm@39296
   221
wenzelm@39296
   222
wenzelm@2957
   223
berghofe@32141
   224
(** order-sorted unification of types **)
wenzelm@2957
   225
wenzelm@39294
   226
exception NO_UNIFIER of string * typ Vartab.table;
wenzelm@2957
   227
wenzelm@42386
   228
fun unify ctxt =
wenzelm@2957
   229
  let
wenzelm@42360
   230
    val thy = Proof_Context.theory_of ctxt;
wenzelm@42386
   231
    val arity_sorts = Type.arity_sorts (Context.pretty ctxt) (Sign.tsig_of thy);
wenzelm@39290
   232
wenzelm@39290
   233
wenzelm@2957
   234
    (* adjust sorts of parameters *)
wenzelm@2957
   235
wenzelm@19465
   236
    fun not_of_sort x S' S =
wenzelm@39290
   237
      "Variable " ^ x ^ "::" ^ Syntax.string_of_sort ctxt S' ^ " not of sort " ^
wenzelm@39290
   238
        Syntax.string_of_sort ctxt S;
wenzelm@2957
   239
berghofe@32141
   240
    fun meet (_, []) tye_idx = tye_idx
wenzelm@39294
   241
      | meet (Type (a, Ts), S) (tye_idx as (tye, _)) =
wenzelm@39291
   242
          meets (Ts, arity_sorts a S handle ERROR msg => raise NO_UNIFIER (msg, tye)) tye_idx
wenzelm@39294
   243
      | meet (TFree (x, S'), S) (tye_idx as (tye, _)) =
wenzelm@39290
   244
          if Sign.subsort thy (S', S) then tye_idx
berghofe@32141
   245
          else raise NO_UNIFIER (not_of_sort x S' S, tye)
wenzelm@39294
   246
      | meet (TVar (xi, S'), S) (tye_idx as (tye, idx)) =
wenzelm@39290
   247
          if Sign.subsort thy (S', S) then tye_idx
wenzelm@39294
   248
          else if is_param xi then
wenzelm@39294
   249
            (Vartab.update_new (xi, mk_param idx (Sign.inter_sort thy (S', S))) tye, idx + 1)
berghofe@32141
   250
          else raise NO_UNIFIER (not_of_sort (Term.string_of_vname xi) S' S, tye)
berghofe@32141
   251
    and meets (T :: Ts, S :: Ss) (tye_idx as (tye, _)) =
berghofe@32141
   252
          meets (Ts, Ss) (meet (deref tye T, S) tye_idx)
berghofe@32141
   253
      | meets _ tye_idx = tye_idx;
wenzelm@2957
   254
wenzelm@2957
   255
wenzelm@35013
   256
    (* occurs check and assignment *)
wenzelm@2957
   257
wenzelm@42400
   258
    fun occurs_check tye xi (TVar (xi', _)) =
wenzelm@39294
   259
          if xi = xi' then raise NO_UNIFIER ("Occurs check!", tye)
wenzelm@32146
   260
          else
wenzelm@39294
   261
            (case Vartab.lookup tye xi' of
berghofe@32141
   262
              NONE => ()
wenzelm@39294
   263
            | SOME T => occurs_check tye xi T)
wenzelm@39294
   264
      | occurs_check tye xi (Type (_, Ts)) = List.app (occurs_check tye xi) Ts
berghofe@32141
   265
      | occurs_check _ _ _ = ();
wenzelm@2957
   266
wenzelm@39294
   267
    fun assign xi (T as TVar (xi', _)) S env =
wenzelm@39294
   268
          if xi = xi' then env
wenzelm@39294
   269
          else env |> meet (T, S) |>> Vartab.update_new (xi, T)
wenzelm@39294
   270
      | assign xi T S (env as (tye, _)) =
wenzelm@39294
   271
          (occurs_check tye xi T; env |> meet (T, S) |>> Vartab.update_new (xi, T));
wenzelm@2957
   272
wenzelm@2957
   273
wenzelm@2957
   274
    (* unification *)
wenzelm@2957
   275
wenzelm@39286
   276
    fun show_tycon (a, Ts) =
wenzelm@39290
   277
      quote (Syntax.string_of_typ ctxt (Type (a, replicate (length Ts) dummyT)));
wenzelm@39286
   278
wenzelm@39294
   279
    fun unif (T1, T2) (env as (tye, _)) =
wenzelm@39294
   280
      (case pairself (`is_paramT o deref tye) (T1, T2) of
wenzelm@39294
   281
        ((true, TVar (xi, S)), (_, T)) => assign xi T S env
wenzelm@39294
   282
      | ((_, T), (true, TVar (xi, S))) => assign xi T S env
wenzelm@39294
   283
      | ((_, Type (a, Ts)), (_, Type (b, Us))) =>
wenzelm@2979
   284
          if a <> b then
wenzelm@39286
   285
            raise NO_UNIFIER
wenzelm@39286
   286
              ("Clash of types " ^ show_tycon (a, Ts) ^ " and " ^ show_tycon (b, Us), tye)
wenzelm@39294
   287
          else fold unif (Ts ~~ Us) env
wenzelm@39294
   288
      | ((_, T), (_, U)) => if T = U then env else raise NO_UNIFIER ("", tye));
wenzelm@2957
   289
wenzelm@2957
   290
  in unif end;
wenzelm@2957
   291
wenzelm@2957
   292
wenzelm@2957
   293
wenzelm@39296
   294
(** simple type inference **)
wenzelm@2957
   295
berghofe@32141
   296
(* infer *)
wenzelm@2957
   297
wenzelm@39290
   298
fun infer ctxt =
wenzelm@2957
   299
  let
wenzelm@2979
   300
    (* errors *)
wenzelm@2957
   301
berghofe@32141
   302
    fun prep_output tye bs ts Ts =
wenzelm@2957
   303
      let
wenzelm@39294
   304
        val (Ts_bTs', ts') = finish ctxt tye (Ts @ map snd bs, ts);
wenzelm@19012
   305
        val (Ts', Ts'') = chop (length Ts) Ts_bTs';
wenzelm@27263
   306
        fun prep t =
wenzelm@27263
   307
          let val xs = rev (Term.variant_frees t (rev (map fst bs ~~ Ts'')))
wenzelm@42284
   308
          in Term.subst_bounds (map Syntax_Trans.mark_boundT xs, t) end;
wenzelm@27263
   309
      in (map prep ts', Ts') end;
wenzelm@2979
   310
wenzelm@39291
   311
    fun err_loose i = error ("Loose bound variable: B." ^ string_of_int i);
wenzelm@2979
   312
wenzelm@39289
   313
    fun unif_failed msg =
wenzelm@39289
   314
      "Type unification failed" ^ (if msg = "" then "" else ": " ^ msg) ^ "\n\n";
wenzelm@39289
   315
berghofe@32141
   316
    fun err_appl msg tye bs t T u U =
wenzelm@39291
   317
      let val ([t', u'], [T', U']) = prep_output tye bs [t, u] [T, U]
wenzelm@42383
   318
      in error (unif_failed msg ^ Type.appl_error ctxt t' T' u' U' ^ "\n") end;
wenzelm@2979
   319
wenzelm@2979
   320
wenzelm@2979
   321
    (* main *)
wenzelm@2979
   322
wenzelm@39294
   323
    fun inf _ (Const (_, T)) tye_idx = (T, tye_idx)
wenzelm@39294
   324
      | inf _ (Free (_, T)) tye_idx = (T, tye_idx)
wenzelm@39294
   325
      | inf _ (Var (_, T)) tye_idx = (T, tye_idx)
wenzelm@39294
   326
      | inf bs (Bound i) tye_idx =
berghofe@32141
   327
          (snd (nth bs i handle Subscript => err_loose i), tye_idx)
wenzelm@39294
   328
      | inf bs (Abs (x, T, t)) tye_idx =
berghofe@32141
   329
          let val (U, tye_idx') = inf ((x, T) :: bs) t tye_idx
wenzelm@39294
   330
          in (T --> U, tye_idx') end
wenzelm@39294
   331
      | inf bs (t $ u) tye_idx =
wenzelm@2957
   332
          let
berghofe@32141
   333
            val (T, tye_idx') = inf bs t tye_idx;
berghofe@32141
   334
            val (U, (tye, idx)) = inf bs u tye_idx';
wenzelm@39294
   335
            val V = mk_param idx [];
wenzelm@42386
   336
            val tye_idx'' = unify ctxt (U --> V, T) (tye, idx + 1)
berghofe@32141
   337
              handle NO_UNIFIER (msg, tye') => err_appl msg tye' bs t T u U;
wenzelm@39292
   338
          in (V, tye_idx'') end;
wenzelm@2957
   339
wenzelm@2957
   340
  in inf [] end;
wenzelm@2957
   341
wenzelm@2957
   342
wenzelm@39296
   343
(* main interfaces *)
wenzelm@2957
   344
wenzelm@39296
   345
fun prepare ctxt const_type var_type raw_ts =
wenzelm@2957
   346
  let
wenzelm@22698
   347
    val get_type = the_default dummyT o var_type;
wenzelm@22698
   348
    val constrain_vars = Term.map_aterms
wenzelm@39288
   349
      (fn Free (x, T) => Type.constraint T (Free (x, get_type (x, ~1)))
wenzelm@39288
   350
        | Var (xi, T) => Type.constraint T (Var (xi, get_type xi))
wenzelm@22698
   351
        | t => t);
wenzelm@22698
   352
wenzelm@39290
   353
    val ts = burrow_types (Syntax.check_typs ctxt) raw_ts;
wenzelm@42400
   354
    val idx = param_maxidx_of ts + 1;
wenzelm@42400
   355
    val (ts', (_, _, idx')) =
wenzelm@42143
   356
      fold_map (prepare_term ctxt const_type o constrain_vars) ts
wenzelm@42400
   357
        (Vartab.empty, Vartab.empty, idx);
wenzelm@42400
   358
  in (idx', ts') end;
wenzelm@2957
   359
wenzelm@39296
   360
fun infer_types ctxt const_type var_type raw_ts =
wenzelm@39296
   361
  let
wenzelm@39296
   362
    val (idx, ts) = prepare ctxt const_type var_type raw_ts;
wenzelm@39296
   363
    val (tye, _) = fold (snd oo infer ctxt) ts (Vartab.empty, idx);
wenzelm@39296
   364
    val (_, ts') = finish ctxt tye ([], ts);
wenzelm@39296
   365
  in ts' end;
wenzelm@14788
   366
wenzelm@2957
   367
end;