src/Pure/type_infer.ML
author wenzelm
Sun Sep 12 20:47:47 2010 +0200 (2010-09-12)
changeset 39290 44e4d8dfd6bf
parent 39289 92b50c8bb67b
child 39291 4b632bb847a8
permissions -rw-r--r--
load type_infer.ML later -- proper context for Type_Infer.infer_types;
renamed Type_Infer.polymorphicT to Type.mark_polymorphic;
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@2957
    79
  PAppl of preterm * preterm |
wenzelm@2957
    80
  Constraint of preterm * pretyp;
wenzelm@2957
    81
wenzelm@2957
    82
wenzelm@2957
    83
(* utils *)
wenzelm@2957
    84
wenzelm@32146
    85
fun deref tye (T as Param (i, S)) =
wenzelm@32146
    86
      (case Inttab.lookup tye i of
wenzelm@32146
    87
        NONE => T
wenzelm@32146
    88
      | SOME U => deref tye U)
berghofe@32141
    89
  | deref tye T = T;
wenzelm@2957
    90
wenzelm@16195
    91
fun fold_pretyps f (PConst (_, T)) x = f T x
wenzelm@16195
    92
  | fold_pretyps f (PFree (_, T)) x = f T x
wenzelm@16195
    93
  | fold_pretyps f (PVar (_, T)) x = f T x
wenzelm@16195
    94
  | fold_pretyps _ (PBound _) x = x
wenzelm@16195
    95
  | fold_pretyps f (PAbs (_, T, t)) x = fold_pretyps f t (f T x)
wenzelm@16195
    96
  | fold_pretyps f (PAppl (t, u)) x = fold_pretyps f u (fold_pretyps f t x)
wenzelm@16195
    97
  | fold_pretyps f (Constraint (t, T)) x = f T (fold_pretyps f t x);
wenzelm@2957
    98
wenzelm@2957
    99
wenzelm@2957
   100
wenzelm@2957
   101
(** raw typs/terms to pretyps/preterms **)
wenzelm@2957
   102
wenzelm@20668
   103
(* pretyp_of *)
wenzelm@2957
   104
wenzelm@39287
   105
fun pretyp_of typ params_idx =
wenzelm@2957
   106
  let
berghofe@32141
   107
    val (params', idx) = fold_atyps
wenzelm@20668
   108
      (fn TVar (xi as (x, _), S) =>
berghofe@32141
   109
          (fn ps_idx as (ps, idx) =>
wenzelm@39287
   110
            if is_param xi andalso not (Vartab.defined ps xi)
berghofe@32141
   111
            then (Vartab.update (xi, Param (idx, S)) ps, idx + 1) else ps_idx)
berghofe@32141
   112
        | _ => I) typ params_idx;
wenzelm@2957
   113
berghofe@32141
   114
    fun pre_of (TVar (v as (xi, _))) idx =
wenzelm@20735
   115
          (case Vartab.lookup params' xi of
skalberg@15531
   116
            NONE => PTVar v
berghofe@32141
   117
          | SOME p => p, idx)
berghofe@32141
   118
      | pre_of (TFree ("'_dummy_", S)) idx = (Param (idx, S), idx + 1)
berghofe@32141
   119
      | pre_of (TFree v) idx = (PTFree v, idx)
berghofe@32141
   120
      | pre_of (T as Type (a, Ts)) idx =
berghofe@32141
   121
          if T = dummyT then (Param (idx, []), idx + 1)
berghofe@32141
   122
          else
berghofe@32141
   123
            let val (Ts', idx') = fold_map pre_of Ts idx
berghofe@32141
   124
            in (PType (a, Ts'), idx') end;
berghofe@32141
   125
berghofe@32141
   126
    val (ptyp, idx') = pre_of typ idx;
berghofe@32141
   127
  in (ptyp, (params', idx')) end;
wenzelm@2957
   128
wenzelm@2957
   129
wenzelm@20668
   130
(* preterm_of *)
wenzelm@2957
   131
wenzelm@39287
   132
fun preterm_of const_type tm (vparams, params, idx) =
wenzelm@2957
   133
  let
berghofe@32141
   134
    fun add_vparm xi (ps_idx as (ps, idx)) =
wenzelm@20735
   135
      if not (Vartab.defined ps xi) then
berghofe@32141
   136
        (Vartab.update (xi, Param (idx, [])) ps, idx + 1)
berghofe@32141
   137
      else ps_idx;
wenzelm@2957
   138
berghofe@32141
   139
    val (vparams', idx') = fold_aterms
wenzelm@20668
   140
      (fn Var (_, Type ("_polymorphic_", _)) => I
wenzelm@20668
   141
        | Var (xi, _) => add_vparm xi
wenzelm@20668
   142
        | Free (x, _) => add_vparm (x, ~1)
wenzelm@20668
   143
        | _ => I)
berghofe@32141
   144
      tm (vparams, idx);
wenzelm@20735
   145
    fun var_param xi = the (Vartab.lookup vparams' xi);
wenzelm@2957
   146
wenzelm@39287
   147
    fun polyT_of T idx = apsnd snd (pretyp_of (paramify_vars T) (Vartab.empty, idx));
wenzelm@2957
   148
wenzelm@22698
   149
    fun constraint T t ps =
wenzelm@20668
   150
      if T = dummyT then (t, ps)
wenzelm@2957
   151
      else
wenzelm@39287
   152
        let val (T', ps') = pretyp_of T ps
wenzelm@20668
   153
        in (Constraint (t, T'), ps') end;
wenzelm@2957
   154
berghofe@32141
   155
    fun pre_of (Const (c, T)) (ps, idx) =
wenzelm@2957
   156
          (case const_type c of
berghofe@32141
   157
            SOME U =>
berghofe@32141
   158
              let val (pU, idx') = polyT_of U idx
berghofe@32141
   159
              in constraint T (PConst (c, pU)) (ps, idx') end
wenzelm@39286
   160
          | NONE => raise TYPE ("Undeclared constant: " ^ quote c, [], []))
berghofe@32141
   161
      | pre_of (Var (xi, Type ("_polymorphic_", [T]))) (ps, idx) =
berghofe@32141
   162
          let val (pT, idx') = polyT_of T idx
berghofe@32141
   163
          in (PVar (xi, pT), (ps, idx')) end
berghofe@32141
   164
      | pre_of (Var (xi, T)) ps_idx = constraint T (PVar (xi, var_param xi)) ps_idx
berghofe@32141
   165
      | pre_of (Free (x, T)) ps_idx = constraint T (PFree (x, var_param (x, ~1))) ps_idx
berghofe@32141
   166
      | pre_of (Const ("_type_constraint_", Type ("fun", [T, _])) $ t) ps_idx =
berghofe@32141
   167
          pre_of t ps_idx |-> constraint T
berghofe@32141
   168
      | pre_of (Bound i) ps_idx = (PBound i, ps_idx)
berghofe@32141
   169
      | pre_of (Abs (x, T, t)) ps_idx =
wenzelm@2957
   170
          let
wenzelm@39287
   171
            val (T', ps_idx') = pretyp_of T ps_idx;
berghofe@32141
   172
            val (t', ps_idx'') = pre_of t ps_idx';
berghofe@32141
   173
          in (PAbs (x, T', t'), ps_idx'') end
berghofe@32141
   174
      | pre_of (t $ u) ps_idx =
wenzelm@2957
   175
          let
berghofe@32141
   176
            val (t', ps_idx') = pre_of t ps_idx;
berghofe@32141
   177
            val (u', ps_idx'') = pre_of u ps_idx';
berghofe@32141
   178
          in (PAppl (t', u'), ps_idx'') end;
wenzelm@2957
   179
berghofe@32141
   180
    val (tm', (params', idx'')) = pre_of tm (params, idx');
berghofe@32141
   181
  in (tm', (vparams', params', idx'')) end;
wenzelm@2957
   182
wenzelm@2957
   183
wenzelm@2957
   184
wenzelm@2957
   185
(** pretyps/terms to typs/terms **)
wenzelm@2957
   186
wenzelm@2957
   187
(* add_parms *)
wenzelm@2957
   188
wenzelm@32146
   189
fun add_parmsT tye T =
wenzelm@32146
   190
  (case deref tye T of
berghofe@32141
   191
    PType (_, Ts) => fold (add_parmsT tye) Ts
berghofe@32141
   192
  | Param (i, _) => insert (op =) i
wenzelm@32146
   193
  | _ => I);
wenzelm@2957
   194
berghofe@32141
   195
fun add_parms tye = fold_pretyps (add_parmsT tye);
wenzelm@2957
   196
wenzelm@2957
   197
wenzelm@2957
   198
(* add_names *)
wenzelm@2957
   199
wenzelm@32146
   200
fun add_namesT tye T =
wenzelm@32146
   201
  (case deref tye T of
berghofe@32141
   202
    PType (_, Ts) => fold (add_namesT tye) Ts
berghofe@32141
   203
  | PTFree (x, _) => Name.declare x
berghofe@32141
   204
  | PTVar ((x, _), _) => Name.declare x
wenzelm@32146
   205
  | Param _ => I);
wenzelm@2957
   206
berghofe@32141
   207
fun add_names tye = fold_pretyps (add_namesT tye);
wenzelm@2957
   208
wenzelm@2957
   209
wenzelm@2957
   210
(* simple_typ/term_of *)
wenzelm@2957
   211
wenzelm@32146
   212
fun simple_typ_of tye f T =
wenzelm@32146
   213
  (case deref tye T of
berghofe@32141
   214
    PType (a, Ts) => Type (a, map (simple_typ_of tye f) Ts)
berghofe@32141
   215
  | PTFree v => TFree v
berghofe@32141
   216
  | PTVar v => TVar v
wenzelm@32146
   217
  | Param (i, S) => TVar (f i, S));
wenzelm@2957
   218
wenzelm@2957
   219
(*convert types, drop constraints*)
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)) =
berghofe@32141
   227
      simple_term_of tye f t $ simple_term_of tye f u
berghofe@32141
   228
  | simple_term_of tye f (Constraint (t, _)) = simple_term_of tye f t;
wenzelm@2957
   229
wenzelm@2957
   230
berghofe@32141
   231
(* typs_terms_of *)
wenzelm@2957
   232
wenzelm@39290
   233
fun typs_terms_of ctxt tye (Ts, ts) =
wenzelm@2957
   234
  let
wenzelm@39290
   235
    val used = fold (add_names tye) ts (fold (add_namesT tye) Ts (Variable.names_of ctxt));
berghofe@32141
   236
    val parms = rev (fold (add_parms tye) ts (fold (add_parmsT tye) Ts []));
wenzelm@39290
   237
    val names = Name.invents used ("?" ^ Name.aT) (length parms);
berghofe@32141
   238
    val tab = Inttab.make (parms ~~ names);
wenzelm@39290
   239
wenzelm@39290
   240
    val maxidx = Variable.maxidx_of ctxt;
berghofe@32141
   241
    fun f i = (the (Inttab.lookup tab i), maxidx + 1);
berghofe@32141
   242
  in (map (simple_typ_of tye f) Ts, map (simple_term_of tye f) ts) end;
wenzelm@2957
   243
wenzelm@2957
   244
wenzelm@2957
   245
berghofe@32141
   246
(** order-sorted unification of types **)
wenzelm@2957
   247
berghofe@32141
   248
exception NO_UNIFIER of string * pretyp Inttab.table;
wenzelm@2957
   249
wenzelm@39290
   250
fun unify ctxt pp =
wenzelm@2957
   251
  let
wenzelm@39290
   252
    val thy = ProofContext.theory_of ctxt;
wenzelm@39290
   253
    val arity_sorts = Type.arity_sorts pp (Sign.tsig_of thy);
wenzelm@39290
   254
wenzelm@39290
   255
wenzelm@2957
   256
    (* adjust sorts of parameters *)
wenzelm@2957
   257
wenzelm@19465
   258
    fun not_of_sort x S' S =
wenzelm@39290
   259
      "Variable " ^ x ^ "::" ^ Syntax.string_of_sort ctxt S' ^ " not of sort " ^
wenzelm@39290
   260
        Syntax.string_of_sort ctxt S;
wenzelm@2957
   261
berghofe@32141
   262
    fun meet (_, []) tye_idx = tye_idx
berghofe@32141
   263
      | meet (Param (i, S'), S) (tye_idx as (tye, idx)) =
wenzelm@39290
   264
          if Sign.subsort thy (S', S) then tye_idx
berghofe@32141
   265
          else (Inttab.update_new (i,
wenzelm@39290
   266
            Param (idx, Sign.inter_sort thy (S', S))) tye, idx + 1)
berghofe@32141
   267
      | meet (PType (a, Ts), S) (tye_idx as (tye, _)) =
wenzelm@39290
   268
          meets (Ts, arity_sorts a S
berghofe@32141
   269
            handle ERROR msg => raise NO_UNIFIER (msg, tye)) tye_idx
berghofe@32141
   270
      | meet (PTFree (x, S'), S) (tye_idx as (tye, _)) =
wenzelm@39290
   271
          if Sign.subsort thy (S', S) then tye_idx
berghofe@32141
   272
          else raise NO_UNIFIER (not_of_sort x S' S, tye)
berghofe@32141
   273
      | meet (PTVar (xi, S'), S) (tye_idx as (tye, _)) =
wenzelm@39290
   274
          if Sign.subsort thy (S', S) then tye_idx
berghofe@32141
   275
          else raise NO_UNIFIER (not_of_sort (Term.string_of_vname xi) S' S, tye)
berghofe@32141
   276
    and meets (T :: Ts, S :: Ss) (tye_idx as (tye, _)) =
berghofe@32141
   277
          meets (Ts, Ss) (meet (deref tye T, S) tye_idx)
berghofe@32141
   278
      | meets _ tye_idx = tye_idx;
wenzelm@2957
   279
wenzelm@2957
   280
wenzelm@35013
   281
    (* occurs check and assignment *)
wenzelm@2957
   282
berghofe@32141
   283
    fun occurs_check tye i (Param (i', S)) =
berghofe@32141
   284
          if i = i' then raise NO_UNIFIER ("Occurs check!", tye)
wenzelm@32146
   285
          else
wenzelm@32146
   286
            (case Inttab.lookup tye i' of
berghofe@32141
   287
              NONE => ()
berghofe@32141
   288
            | SOME T => occurs_check tye i T)
berghofe@32141
   289
      | occurs_check tye i (PType (_, Ts)) = List.app (occurs_check tye i) Ts
berghofe@32141
   290
      | occurs_check _ _ _ = ();
wenzelm@2957
   291
berghofe@37235
   292
    fun assign i (T as Param (i', _)) S tye_idx =
berghofe@32141
   293
          if i = i' then tye_idx
berghofe@37235
   294
          else tye_idx |> meet (T, S) |>> Inttab.update_new (i, T)
berghofe@37235
   295
      | assign i T S (tye_idx as (tye, _)) =
berghofe@37235
   296
          (occurs_check tye i T; tye_idx |> meet (T, S) |>> Inttab.update_new (i, T));
wenzelm@2957
   297
wenzelm@2957
   298
wenzelm@2957
   299
    (* unification *)
wenzelm@2957
   300
wenzelm@39286
   301
    fun show_tycon (a, Ts) =
wenzelm@39290
   302
      quote (Syntax.string_of_typ ctxt (Type (a, replicate (length Ts) dummyT)));
wenzelm@39286
   303
wenzelm@32146
   304
    fun unif (T1, T2) (tye_idx as (tye, idx)) =
wenzelm@32146
   305
      (case (deref tye T1, deref tye T2) of
berghofe@32141
   306
        (Param (i, S), T) => assign i T S tye_idx
berghofe@32141
   307
      | (T, Param (i, S)) => assign i T S tye_idx
berghofe@32141
   308
      | (PType (a, Ts), PType (b, Us)) =>
wenzelm@2979
   309
          if a <> b then
wenzelm@39286
   310
            raise NO_UNIFIER
wenzelm@39286
   311
              ("Clash of types " ^ show_tycon (a, Ts) ^ " and " ^ show_tycon (b, Us), tye)
berghofe@32141
   312
          else fold unif (Ts ~~ Us) tye_idx
wenzelm@32146
   313
      | (T, U) => if T = U then tye_idx else raise NO_UNIFIER ("", tye));
wenzelm@2957
   314
wenzelm@2957
   315
  in unif end;
wenzelm@2957
   316
wenzelm@2957
   317
wenzelm@2957
   318
wenzelm@2957
   319
(** type inference **)
wenzelm@2957
   320
berghofe@32141
   321
(* infer *)
wenzelm@2957
   322
wenzelm@39290
   323
fun infer ctxt =
wenzelm@2957
   324
  let
wenzelm@39290
   325
    val pp = Syntax.pp ctxt;
wenzelm@39290
   326
wenzelm@39290
   327
wenzelm@2979
   328
    (* errors *)
wenzelm@2957
   329
berghofe@32141
   330
    fun prep_output tye bs ts Ts =
wenzelm@2957
   331
      let
wenzelm@39290
   332
        val (Ts_bTs', ts') = typs_terms_of ctxt tye (Ts @ map snd bs, ts);
wenzelm@19012
   333
        val (Ts', Ts'') = chop (length Ts) Ts_bTs';
wenzelm@27263
   334
        fun prep t =
wenzelm@27263
   335
          let val xs = rev (Term.variant_frees t (rev (map fst bs ~~ Ts'')))
wenzelm@27263
   336
          in Term.subst_bounds (map Syntax.mark_boundT xs, t) end;
wenzelm@27263
   337
      in (map prep ts', Ts') end;
wenzelm@2979
   338
wenzelm@2979
   339
    fun err_loose i =
wenzelm@3784
   340
      raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], []);
wenzelm@2979
   341
wenzelm@39289
   342
    fun unif_failed msg =
wenzelm@39289
   343
      "Type unification failed" ^ (if msg = "" then "" else ": " ^ msg) ^ "\n\n";
wenzelm@39289
   344
berghofe@32141
   345
    fun err_appl msg tye bs t T u U =
wenzelm@2979
   346
      let
berghofe@32141
   347
        val ([t', u'], [T', U']) = prep_output tye bs [t, u] [T, U];
wenzelm@39289
   348
        val text = unif_failed msg ^ Type.appl_error pp t' T' u' U' ^ "\n";
wenzelm@3784
   349
      in raise TYPE (text, [T', U'], [t', u']) end;
wenzelm@2979
   350
berghofe@32141
   351
    fun err_constraint msg tye bs t T U =
wenzelm@2979
   352
      let
berghofe@32141
   353
        val ([t'], [T', U']) = prep_output tye bs [t] [T, U];
wenzelm@39289
   354
        val text =
wenzelm@39289
   355
          unif_failed msg ^
wenzelm@39289
   356
            Type.appl_error pp (Const ("_type_constraint_", U' --> U')) (U' --> U') t' T' ^ "\n";
wenzelm@3784
   357
      in raise TYPE (text, [T', U'], [t']) end;
wenzelm@2979
   358
wenzelm@2979
   359
wenzelm@2979
   360
    (* main *)
wenzelm@2979
   361
berghofe@32141
   362
    fun inf _ (PConst (_, T)) tye_idx = (T, tye_idx)
berghofe@32141
   363
      | inf _ (PFree (_, T)) tye_idx = (T, tye_idx)
berghofe@32141
   364
      | inf _ (PVar (_, T)) tye_idx = (T, tye_idx)
berghofe@32141
   365
      | inf bs (PBound i) tye_idx =
berghofe@32141
   366
          (snd (nth bs i handle Subscript => err_loose i), tye_idx)
berghofe@32141
   367
      | inf bs (PAbs (x, T, t)) tye_idx =
berghofe@32141
   368
          let val (U, tye_idx') = inf ((x, T) :: bs) t tye_idx
berghofe@32141
   369
          in (PType ("fun", [T, U]), tye_idx') end
berghofe@32141
   370
      | inf bs (PAppl (t, u)) tye_idx =
wenzelm@2957
   371
          let
berghofe@32141
   372
            val (T, tye_idx') = inf bs t tye_idx;
berghofe@32141
   373
            val (U, (tye, idx)) = inf bs u tye_idx';
berghofe@32141
   374
            val V = Param (idx, []);
wenzelm@2957
   375
            val U_to_V = PType ("fun", [U, V]);
wenzelm@39290
   376
            val tye_idx'' = unify ctxt pp (U_to_V, T) (tye, idx + 1)
berghofe@32141
   377
              handle NO_UNIFIER (msg, tye') => err_appl msg tye' bs t T u U;
berghofe@32141
   378
          in (V, tye_idx'') end
berghofe@32141
   379
      | inf bs (Constraint (t, U)) tye_idx =
berghofe@32141
   380
          let val (T, tye_idx') = inf bs t tye_idx in
berghofe@32141
   381
            (T,
wenzelm@39290
   382
             unify ctxt pp (T, U) tye_idx'
wenzelm@32146
   383
               handle NO_UNIFIER (msg, tye) => err_constraint msg tye bs t T U)
wenzelm@2957
   384
          end;
wenzelm@2957
   385
wenzelm@2957
   386
  in inf [] end;
wenzelm@2957
   387
wenzelm@2957
   388
wenzelm@22698
   389
(* infer_types *)
wenzelm@2957
   390
wenzelm@39290
   391
fun infer_types ctxt const_type var_type raw_ts =
wenzelm@2957
   392
  let
wenzelm@22698
   393
    (*constrain vars*)
wenzelm@22698
   394
    val get_type = the_default dummyT o var_type;
wenzelm@22698
   395
    val constrain_vars = Term.map_aterms
wenzelm@39288
   396
      (fn Free (x, T) => Type.constraint T (Free (x, get_type (x, ~1)))
wenzelm@39288
   397
        | Var (xi, T) => Type.constraint T (Var (xi, get_type xi))
wenzelm@22698
   398
        | t => t);
wenzelm@22698
   399
wenzelm@27263
   400
    (*convert to preterms*)
wenzelm@39290
   401
    val ts = burrow_types (Syntax.check_typs ctxt) raw_ts;
berghofe@32141
   402
    val (ts', (_, _, idx)) =
wenzelm@39287
   403
      fold_map (preterm_of const_type o constrain_vars) ts
berghofe@32141
   404
      (Vartab.empty, Vartab.empty, 0);
wenzelm@2957
   405
wenzelm@27263
   406
    (*do type inference*)
wenzelm@39290
   407
    val (tye, _) = fold (snd oo infer ctxt) ts' (Inttab.empty, idx);
wenzelm@39290
   408
  in #2 (typs_terms_of ctxt tye ([], ts')) end;
wenzelm@14788
   409
wenzelm@2957
   410
end;