src/Pure/type_infer.ML
author wenzelm
Wed Jun 18 22:32:02 2008 +0200 (2008-06-18)
changeset 27263 a6b7f934fbc4
parent 24848 5dbbd33c3236
child 29606 fedb8be05f24
permissions -rw-r--r--
improved error output -- variant/mark bounds;
simplified infer_types -- always freeze, no result substitution;
wenzelm@2957
     1
(*  Title:      Pure/type_infer.ML
wenzelm@2957
     2
    ID:         $Id$
wenzelm@2957
     3
    Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
wenzelm@2957
     4
wenzelm@22698
     5
Simple type inference.
wenzelm@2957
     6
*)
wenzelm@2957
     7
wenzelm@2957
     8
signature TYPE_INFER =
wenzelm@2957
     9
sig
wenzelm@8087
    10
  val anyT: sort -> typ
wenzelm@8611
    11
  val polymorphicT: typ -> typ
wenzelm@24682
    12
  val constrain: typ -> term -> term
wenzelm@24504
    13
  val is_param: indexname -> bool
wenzelm@14788
    14
  val param: int -> string * sort -> typ
wenzelm@22771
    15
  val paramify_vars: typ -> typ
wenzelm@18339
    16
  val paramify_dummies: typ -> int -> typ * int
wenzelm@24764
    17
  val fixate_params: Name.context -> term list -> term list
wenzelm@22698
    18
  val appl_error: Pretty.pp -> string -> term -> typ -> term -> typ -> string list
wenzelm@24485
    19
  val infer_types: Pretty.pp -> Type.tsig -> (typ list -> typ list) ->
wenzelm@27263
    20
    (string -> typ option) -> (indexname -> typ option) -> Name.context -> int ->
wenzelm@27263
    21
    term list -> term list
wenzelm@2957
    22
end;
wenzelm@2957
    23
wenzelm@2957
    24
structure TypeInfer: TYPE_INFER =
wenzelm@2957
    25
struct
wenzelm@2957
    26
wenzelm@2957
    27
wenzelm@22698
    28
(** type parameters and constraints **)
wenzelm@2957
    29
wenzelm@22698
    30
fun anyT S = TFree ("'_dummy_", S);
wenzelm@2957
    31
wenzelm@22698
    32
(*indicate polymorphic Vars*)
wenzelm@22698
    33
fun polymorphicT T = Type ("_polymorphic_", [T]);
wenzelm@2957
    34
wenzelm@27263
    35
val constrain = Syntax.type_constraint;
wenzelm@22698
    36
wenzelm@22698
    37
wenzelm@22698
    38
(* user parameters *)
wenzelm@2957
    39
wenzelm@24504
    40
fun is_param (x, _: int) = String.isPrefix "?" x;
wenzelm@22698
    41
fun param i (x, S) = TVar (("?" ^ x, i), S);
wenzelm@22698
    42
wenzelm@22771
    43
val paramify_vars = Term.map_atyps (fn TVar ((x, i), S) => param i (x, S) | T => T);
wenzelm@22771
    44
wenzelm@22698
    45
val paramify_dummies =
wenzelm@22698
    46
  let
wenzelm@22698
    47
    fun dummy S maxidx = (param (maxidx + 1) ("'dummy", S), maxidx + 1);
wenzelm@2957
    48
wenzelm@22698
    49
    fun paramify (TFree ("'_dummy_", S)) maxidx = dummy S maxidx
wenzelm@22698
    50
      | paramify (Type ("dummy", _)) maxidx = dummy [] maxidx
wenzelm@22698
    51
      | paramify (Type (a, Ts)) maxidx =
wenzelm@22698
    52
          let val (Ts', maxidx') = fold_map paramify Ts maxidx
wenzelm@22698
    53
          in (Type (a, Ts'), maxidx') end
wenzelm@22698
    54
      | paramify T maxidx = (T, maxidx);
wenzelm@22698
    55
  in paramify end;
wenzelm@2957
    56
wenzelm@24764
    57
fun fixate_params name_context ts =
wenzelm@24764
    58
  let
wenzelm@24764
    59
    fun subst_param (xi, S) (inst, used) =
wenzelm@24764
    60
      if is_param xi then
wenzelm@24764
    61
        let
wenzelm@24848
    62
          val [a] = Name.invents used Name.aT 1;
wenzelm@24764
    63
          val used' = Name.declare a used;
wenzelm@24764
    64
        in (((xi, S), TFree (a, S)) :: inst, used') end
wenzelm@24764
    65
      else (inst, used);
wenzelm@24764
    66
    val name_context' = (fold o fold_types) Term.declare_typ_names ts name_context;
wenzelm@24764
    67
    val (inst, _) = fold_rev subst_param (fold Term.add_tvars ts []) ([], name_context');
wenzelm@24764
    68
  in (map o map_types) (TermSubst.instantiateT inst) ts end;
wenzelm@24764
    69
wenzelm@2957
    70
wenzelm@2957
    71
wenzelm@2957
    72
(** pretyps and preterms **)
wenzelm@2957
    73
wenzelm@2957
    74
(*links to parameters may get instantiated, anything else is rigid*)
wenzelm@2957
    75
datatype pretyp =
wenzelm@2957
    76
  PType of string * pretyp list |
wenzelm@2957
    77
  PTFree of string * sort |
wenzelm@2957
    78
  PTVar of indexname * sort |
wenzelm@2957
    79
  Param of sort |
wenzelm@2957
    80
  Link of pretyp ref;
wenzelm@2957
    81
wenzelm@2957
    82
datatype preterm =
wenzelm@2957
    83
  PConst of string * pretyp |
wenzelm@2957
    84
  PFree of string * pretyp |
wenzelm@2957
    85
  PVar of indexname * pretyp |
wenzelm@2957
    86
  PBound of int |
wenzelm@2957
    87
  PAbs of string * pretyp * preterm |
wenzelm@2957
    88
  PAppl of preterm * preterm |
wenzelm@2957
    89
  Constraint of preterm * pretyp;
wenzelm@2957
    90
wenzelm@2957
    91
wenzelm@2957
    92
(* utils *)
wenzelm@2957
    93
wenzelm@2957
    94
val mk_param = Link o ref o Param;
wenzelm@2957
    95
wenzelm@2957
    96
fun deref (T as Link (ref (Param _))) = T
wenzelm@2957
    97
  | deref (Link (ref T)) = deref T
wenzelm@2957
    98
  | deref T = T;
wenzelm@2957
    99
wenzelm@16195
   100
fun fold_pretyps f (PConst (_, T)) x = f T x
wenzelm@16195
   101
  | fold_pretyps f (PFree (_, T)) x = f T x
wenzelm@16195
   102
  | fold_pretyps f (PVar (_, T)) x = f T x
wenzelm@16195
   103
  | fold_pretyps _ (PBound _) x = x
wenzelm@16195
   104
  | fold_pretyps f (PAbs (_, T, t)) x = fold_pretyps f t (f T x)
wenzelm@16195
   105
  | fold_pretyps f (PAppl (t, u)) x = fold_pretyps f u (fold_pretyps f t x)
wenzelm@16195
   106
  | fold_pretyps f (Constraint (t, T)) x = f T (fold_pretyps f t x);
wenzelm@2957
   107
wenzelm@2957
   108
wenzelm@2957
   109
wenzelm@2957
   110
(** raw typs/terms to pretyps/preterms **)
wenzelm@2957
   111
wenzelm@20668
   112
(* pretyp_of *)
wenzelm@2957
   113
wenzelm@24504
   114
fun pretyp_of is_para typ params =
wenzelm@2957
   115
  let
wenzelm@20668
   116
    val params' = fold_atyps
wenzelm@20668
   117
      (fn TVar (xi as (x, _), S) =>
wenzelm@20668
   118
          (fn ps =>
wenzelm@24504
   119
            if is_para xi andalso not (Vartab.defined ps xi)
wenzelm@20735
   120
            then Vartab.update (xi, mk_param S) ps else ps)
wenzelm@20668
   121
        | _ => I) typ params;
wenzelm@2957
   122
wenzelm@2957
   123
    fun pre_of (TVar (v as (xi, _))) =
wenzelm@20735
   124
          (case Vartab.lookup params' xi of
skalberg@15531
   125
            NONE => PTVar v
skalberg@15531
   126
          | SOME p => p)
wenzelm@8087
   127
      | pre_of (TFree ("'_dummy_", S)) = mk_param S
wenzelm@2957
   128
      | pre_of (TFree v) = PTFree v
wenzelm@2957
   129
      | pre_of (T as Type (a, Ts)) =
wenzelm@2957
   130
          if T = dummyT then mk_param []
wenzelm@2957
   131
          else PType (a, map pre_of Ts);
wenzelm@20668
   132
  in (pre_of typ, params') end;
wenzelm@2957
   133
wenzelm@2957
   134
wenzelm@20668
   135
(* preterm_of *)
wenzelm@2957
   136
wenzelm@24504
   137
fun preterm_of const_type is_para tm (vparams, params) =
wenzelm@2957
   138
  let
wenzelm@20668
   139
    fun add_vparm xi ps =
wenzelm@20735
   140
      if not (Vartab.defined ps xi) then
wenzelm@20735
   141
        Vartab.update (xi, mk_param []) ps
wenzelm@2957
   142
      else ps;
wenzelm@2957
   143
wenzelm@20668
   144
    val vparams' = fold_aterms
wenzelm@20668
   145
      (fn Var (_, Type ("_polymorphic_", _)) => I
wenzelm@20668
   146
        | Var (xi, _) => add_vparm xi
wenzelm@20668
   147
        | Free (x, _) => add_vparm (x, ~1)
wenzelm@20668
   148
        | _ => I)
wenzelm@20668
   149
      tm vparams;
wenzelm@20735
   150
    fun var_param xi = the (Vartab.lookup vparams' xi);
wenzelm@2957
   151
wenzelm@24504
   152
    val preT_of = pretyp_of is_para;
wenzelm@20735
   153
    fun polyT_of T = fst (pretyp_of (K true) T Vartab.empty);
wenzelm@2957
   154
wenzelm@22698
   155
    fun constraint T t ps =
wenzelm@20668
   156
      if T = dummyT then (t, ps)
wenzelm@2957
   157
      else
wenzelm@20668
   158
        let val (T', ps') = preT_of T ps
wenzelm@20668
   159
        in (Constraint (t, T'), ps') end;
wenzelm@2957
   160
wenzelm@20668
   161
    fun pre_of (Const (c, T)) ps =
wenzelm@2957
   162
          (case const_type c of
wenzelm@22698
   163
            SOME U => constraint T (PConst (c, polyT_of U)) ps
skalberg@15531
   164
          | NONE => raise TYPE ("No such constant: " ^ quote c, [], []))
wenzelm@20735
   165
      | pre_of (Var (xi, Type ("_polymorphic_", [T]))) ps = (PVar (xi, polyT_of T), ps)
wenzelm@22698
   166
      | pre_of (Var (xi, T)) ps = constraint T (PVar (xi, var_param xi)) ps
wenzelm@22698
   167
      | pre_of (Free (x, T)) ps = constraint T (PFree (x, var_param (x, ~1))) ps
wenzelm@20668
   168
      | pre_of (Const ("_type_constraint_", Type ("fun", [T, _])) $ t) ps =
wenzelm@22698
   169
          pre_of t ps |-> constraint T
wenzelm@20668
   170
      | pre_of (Bound i) ps = (PBound i, ps)
wenzelm@20668
   171
      | pre_of (Abs (x, T, t)) ps =
wenzelm@2957
   172
          let
wenzelm@20668
   173
            val (T', ps') = preT_of T ps;
wenzelm@20668
   174
            val (t', ps'') = pre_of t ps';
wenzelm@20668
   175
          in (PAbs (x, T', t'), ps'') end
wenzelm@20668
   176
      | pre_of (t $ u) ps =
wenzelm@2957
   177
          let
wenzelm@20668
   178
            val (t', ps') = pre_of t ps;
wenzelm@20668
   179
            val (u', ps'') = pre_of u ps';
wenzelm@20668
   180
          in (PAppl (t', u'), ps'') end;
wenzelm@2957
   181
wenzelm@20668
   182
    val (tm', params') = pre_of tm params;
wenzelm@20668
   183
  in (tm', (vparams', params')) end;
wenzelm@2957
   184
wenzelm@2957
   185
wenzelm@2957
   186
wenzelm@2957
   187
(** pretyps/terms to typs/terms **)
wenzelm@2957
   188
wenzelm@2957
   189
(* add_parms *)
wenzelm@2957
   190
wenzelm@16195
   191
fun add_parmsT (PType (_, Ts)) rs = fold add_parmsT Ts rs
haftmann@20854
   192
  | add_parmsT (Link (r as ref (Param _))) rs = insert (op =) r rs
wenzelm@16195
   193
  | add_parmsT (Link (ref T)) rs = add_parmsT T rs
wenzelm@16195
   194
  | add_parmsT _ rs = rs;
wenzelm@2957
   195
wenzelm@16195
   196
val add_parms = fold_pretyps add_parmsT;
wenzelm@2957
   197
wenzelm@2957
   198
wenzelm@2957
   199
(* add_names *)
wenzelm@2957
   200
wenzelm@20161
   201
fun add_namesT (PType (_, Ts)) = fold add_namesT Ts
wenzelm@20161
   202
  | add_namesT (PTFree (x, _)) = Name.declare x
wenzelm@20161
   203
  | add_namesT (PTVar ((x, _), _)) = Name.declare x
wenzelm@20161
   204
  | add_namesT (Link (ref T)) = add_namesT T
wenzelm@20161
   205
  | add_namesT (Param _) = I;
wenzelm@2957
   206
wenzelm@16195
   207
val add_names = fold_pretyps add_namesT;
wenzelm@2957
   208
wenzelm@2957
   209
wenzelm@2957
   210
(* simple_typ/term_of *)
wenzelm@2957
   211
wenzelm@2957
   212
(*deref links, fail on params*)
wenzelm@2957
   213
fun simple_typ_of (PType (a, Ts)) = Type (a, map simple_typ_of Ts)
wenzelm@2957
   214
  | simple_typ_of (PTFree v) = TFree v
wenzelm@2957
   215
  | simple_typ_of (PTVar v) = TVar v
wenzelm@2957
   216
  | simple_typ_of (Link (ref T)) = simple_typ_of T
wenzelm@2957
   217
  | simple_typ_of (Param _) = sys_error "simple_typ_of: illegal Param";
wenzelm@2957
   218
wenzelm@2957
   219
(*convert types, drop constraints*)
wenzelm@2957
   220
fun simple_term_of (PConst (c, T)) = Const (c, simple_typ_of T)
wenzelm@2957
   221
  | simple_term_of (PFree (x, T)) = Free (x, simple_typ_of T)
wenzelm@2957
   222
  | simple_term_of (PVar (xi, T)) = Var (xi, simple_typ_of T)
wenzelm@2957
   223
  | simple_term_of (PBound i) = Bound i
wenzelm@2957
   224
  | simple_term_of (PAbs (x, T, t)) = Abs (x, simple_typ_of T, simple_term_of t)
wenzelm@2957
   225
  | simple_term_of (PAppl (t, u)) = simple_term_of t $ simple_term_of u
wenzelm@2957
   226
  | simple_term_of (Constraint (t, _)) = simple_term_of t;
wenzelm@2957
   227
wenzelm@2957
   228
wenzelm@2957
   229
(* typs_terms_of *)                             (*DESTRUCTIVE*)
wenzelm@2957
   230
wenzelm@27263
   231
fun typs_terms_of used maxidx (Ts, ts) =
wenzelm@2957
   232
  let
wenzelm@27263
   233
    fun elim (r as ref (Param S), x) = r := PTVar ((x, maxidx + 1), S)
wenzelm@4957
   234
      | elim _ = ();
wenzelm@2957
   235
wenzelm@16195
   236
    val used' = fold add_names ts (fold add_namesT Ts used);
wenzelm@16195
   237
    val parms = rev (fold add_parms ts (fold add_parmsT Ts []));
wenzelm@27263
   238
    val names = Name.invents used' ("?" ^ Name.aT) (length parms);
wenzelm@27263
   239
    val _ = ListPair.app elim (parms, names);
wenzelm@27263
   240
  in (map simple_typ_of Ts, map simple_term_of ts) end;
wenzelm@2957
   241
wenzelm@2957
   242
wenzelm@2957
   243
wenzelm@2957
   244
(** order-sorted unification of types **)       (*DESTRUCTIVE*)
wenzelm@2957
   245
wenzelm@2957
   246
exception NO_UNIFIER of string;
wenzelm@2957
   247
wenzelm@19465
   248
fun unify pp tsig =
wenzelm@2957
   249
  let
wenzelm@2957
   250
wenzelm@2957
   251
    (* adjust sorts of parameters *)
wenzelm@2957
   252
wenzelm@19465
   253
    fun not_of_sort x S' S =
wenzelm@14828
   254
      "Variable " ^ x ^ "::" ^ Pretty.string_of_sort pp S' ^ " not of sort " ^
wenzelm@14828
   255
        Pretty.string_of_sort pp S;
wenzelm@2957
   256
wenzelm@4957
   257
    fun meet (_, []) = ()
wenzelm@4957
   258
      | meet (Link (r as (ref (Param S'))), S) =
wenzelm@19465
   259
          if Type.subsort tsig (S', S) then ()
wenzelm@19465
   260
          else r := mk_param (Type.inter_sort tsig (S', S))
wenzelm@4957
   261
      | meet (Link (ref T), S) = meet (T, S)
wenzelm@4957
   262
      | meet (PType (a, Ts), S) =
wenzelm@19465
   263
          ListPair.app meet (Ts, Type.arity_sorts pp tsig a S
wenzelm@19465
   264
            handle ERROR msg => raise NO_UNIFIER msg)
wenzelm@4957
   265
      | meet (PTFree (x, S'), S) =
wenzelm@19465
   266
          if Type.subsort tsig (S', S) then ()
wenzelm@19465
   267
          else raise NO_UNIFIER (not_of_sort x S' S)
wenzelm@4957
   268
      | meet (PTVar (xi, S'), S) =
wenzelm@19465
   269
          if Type.subsort tsig (S', S) then ()
wenzelm@22678
   270
          else raise NO_UNIFIER (not_of_sort (Term.string_of_vname xi) S' S)
wenzelm@4957
   271
      | meet (Param _, _) = sys_error "meet";
wenzelm@2957
   272
wenzelm@2957
   273
wenzelm@2957
   274
    (* occurs check and assigment *)
wenzelm@2957
   275
wenzelm@2957
   276
    fun occurs_check r (Link (r' as ref T)) =
wenzelm@2957
   277
          if r = r' then raise NO_UNIFIER "Occurs check!"
wenzelm@2957
   278
          else occurs_check r T
skalberg@15570
   279
      | occurs_check r (PType (_, Ts)) = List.app (occurs_check r) Ts
wenzelm@2957
   280
      | occurs_check _ _ = ();
wenzelm@2957
   281
wenzelm@2957
   282
    fun assign r T S =
wenzelm@2957
   283
      (case deref T of
wenzelm@2957
   284
        T' as Link (r' as ref (Param _)) =>
wenzelm@8087
   285
          if r = r' then () else (meet (T', S); r := T')
wenzelm@8087
   286
      | T' => (occurs_check r T'; meet (T', S); r := T'));
wenzelm@2957
   287
wenzelm@2957
   288
wenzelm@2957
   289
    (* unification *)
wenzelm@2957
   290
wenzelm@4957
   291
    fun unif (Link (r as ref (Param S)), T) = assign r T S
wenzelm@4957
   292
      | unif (T, Link (r as ref (Param S))) = assign r T S
wenzelm@4957
   293
      | unif (Link (ref T), U) = unif (T, U)
wenzelm@4957
   294
      | unif (T, Link (ref U)) = unif (T, U)
wenzelm@4957
   295
      | unif (PType (a, Ts), PType (b, Us)) =
wenzelm@2979
   296
          if a <> b then
wenzelm@14828
   297
            raise NO_UNIFIER ("Clash of types " ^ quote a ^ " and " ^ quote b)
wenzelm@16195
   298
          else ListPair.app unif (Ts, Us)
wenzelm@4957
   299
      | unif (T, U) = if T = U then () else raise NO_UNIFIER "";
wenzelm@2957
   300
wenzelm@2957
   301
  in unif end;
wenzelm@2957
   302
wenzelm@2957
   303
wenzelm@2957
   304
wenzelm@2957
   305
(** type inference **)
wenzelm@2957
   306
wenzelm@22698
   307
(* appl_error *)
wenzelm@22698
   308
wenzelm@14828
   309
fun appl_error pp why t T u U =
wenzelm@8087
   310
 ["Type error in application: " ^ why,
wenzelm@8087
   311
  "",
wenzelm@8087
   312
  Pretty.string_of (Pretty.block
wenzelm@14828
   313
    [Pretty.str "Operator:", Pretty.brk 2, Pretty.term pp t,
wenzelm@14828
   314
      Pretty.str " ::", Pretty.brk 1, Pretty.typ pp T]),
wenzelm@8087
   315
  Pretty.string_of (Pretty.block
wenzelm@14828
   316
    [Pretty.str "Operand:", Pretty.brk 3, Pretty.term pp u,
wenzelm@14828
   317
      Pretty.str " ::", Pretty.brk 1, Pretty.typ pp U]),
wenzelm@8087
   318
  ""];
wenzelm@8087
   319
nipkow@5635
   320
wenzelm@2957
   321
(* infer *)                                     (*DESTRUCTIVE*)
wenzelm@2957
   322
wenzelm@19465
   323
fun infer pp tsig =
wenzelm@2957
   324
  let
wenzelm@2979
   325
    (* errors *)
wenzelm@2957
   326
wenzelm@2979
   327
    fun unif_failed msg =
wenzelm@14828
   328
      "Type unification failed" ^ (if msg = "" then "" else ": " ^ msg) ^ "\n";
wenzelm@2979
   329
wenzelm@2979
   330
    fun prep_output bs ts Ts =
wenzelm@2957
   331
      let
wenzelm@27263
   332
        val (Ts_bTs', ts') = typs_terms_of Name.context ~1 (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@3510
   342
    fun err_appl msg bs t T u U =
wenzelm@2979
   343
      let
wenzelm@3510
   344
        val ([t', u'], [T', U']) = prep_output bs [t, u] [T, U];
wenzelm@3510
   345
        val why =
wenzelm@3510
   346
          (case T' of
wenzelm@14828
   347
            Type ("fun", _) => "Incompatible operand type"
wenzelm@14828
   348
          | _ => "Operator not of function type");
wenzelm@14828
   349
        val text = unif_failed msg ^ cat_lines (appl_error pp why t' T' u' U');
wenzelm@3784
   350
      in raise TYPE (text, [T', U'], [t', u']) end;
wenzelm@2979
   351
wenzelm@2979
   352
    fun err_constraint msg bs t T U =
wenzelm@2979
   353
      let
wenzelm@2979
   354
        val ([t'], [T', U']) = prep_output bs [t] [T, U];
wenzelm@2979
   355
        val text = cat_lines
wenzelm@2979
   356
         [unif_failed msg,
nipkow@5635
   357
          "Cannot meet type constraint:", "",
wenzelm@14828
   358
          Pretty.string_of (Pretty.block
wenzelm@14828
   359
           [Pretty.str "Term:", Pretty.brk 2, Pretty.term pp t',
wenzelm@14828
   360
            Pretty.str " ::", Pretty.brk 1, Pretty.typ pp T']),
wenzelm@14828
   361
          Pretty.string_of (Pretty.block
wenzelm@14828
   362
           [Pretty.str "Type:", Pretty.brk 2, Pretty.typ pp U']), ""];
wenzelm@3784
   363
      in raise TYPE (text, [T', U'], [t']) end;
wenzelm@2979
   364
wenzelm@2979
   365
wenzelm@2979
   366
    (* main *)
wenzelm@2979
   367
wenzelm@19465
   368
    val unif = unify pp tsig;
wenzelm@2957
   369
wenzelm@2957
   370
    fun inf _ (PConst (_, T)) = T
wenzelm@2957
   371
      | inf _ (PFree (_, T)) = T
wenzelm@2957
   372
      | inf _ (PVar (_, T)) = T
skalberg@15570
   373
      | inf bs (PBound i) = snd (List.nth (bs, i) handle Subscript => err_loose i)
wenzelm@2957
   374
      | inf bs (PAbs (x, T, t)) = PType ("fun", [T, inf ((x, T) :: bs) t])
wenzelm@2957
   375
      | inf bs (PAppl (t, u)) =
wenzelm@2957
   376
          let
wenzelm@2957
   377
            val T = inf bs t;
wenzelm@2957
   378
            val U = inf bs u;
wenzelm@2957
   379
            val V = mk_param [];
wenzelm@2957
   380
            val U_to_V = PType ("fun", [U, V]);
wenzelm@4957
   381
            val _ = unif (U_to_V, T) handle NO_UNIFIER msg => err_appl msg bs t T u U;
wenzelm@2957
   382
          in V end
wenzelm@2957
   383
      | inf bs (Constraint (t, U)) =
wenzelm@2957
   384
          let val T = inf bs t in
wenzelm@4957
   385
            unif (T, U) handle NO_UNIFIER msg => err_constraint msg bs t T U;
wenzelm@2957
   386
            T
wenzelm@2957
   387
          end;
wenzelm@2957
   388
wenzelm@2957
   389
  in inf [] end;
wenzelm@2957
   390
wenzelm@2957
   391
wenzelm@22698
   392
(* infer_types *)
wenzelm@2957
   393
wenzelm@27263
   394
fun infer_types pp tsig check_typs const_type var_type used maxidx raw_ts =
wenzelm@2957
   395
  let
wenzelm@22698
   396
    (*constrain vars*)
wenzelm@22698
   397
    val get_type = the_default dummyT o var_type;
wenzelm@22698
   398
    val constrain_vars = Term.map_aterms
wenzelm@24682
   399
      (fn Free (x, T) => constrain T (Free (x, get_type (x, ~1)))
wenzelm@24682
   400
        | Var (xi, T) => constrain T (Var (xi, get_type xi))
wenzelm@22698
   401
        | t => t);
wenzelm@22698
   402
wenzelm@27263
   403
    (*convert to preterms*)
wenzelm@27263
   404
    val ts = burrow_types check_typs raw_ts;
wenzelm@22698
   405
    val (ts', (vps, ps)) =
wenzelm@27263
   406
      fold_map (preterm_of const_type is_param o constrain_vars) ts (Vartab.empty, Vartab.empty);
wenzelm@2957
   407
wenzelm@27263
   408
    (*do type inference*)
wenzelm@27263
   409
    val _ = List.app (ignore o infer pp tsig) ts';
wenzelm@27263
   410
  in #2 (typs_terms_of used maxidx ([], ts')) end;
wenzelm@14788
   411
wenzelm@2957
   412
end;