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