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