src/Pure/type_infer.ML
author wenzelm
Sat May 01 22:08:57 2004 +0200 (2004-05-01)
changeset 14695 9c78044b99c3
parent 13667 0009325e9af0
child 14788 9776f0c747c8
permissions -rw-r--r--
improved Term.invent_names;
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@2957
     5
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@2979
    13
  val infer_types: (term -> Pretty.T) -> (typ -> Pretty.T)
wenzelm@2979
    14
    -> (string -> typ option) -> Sorts.classrel -> Sorts.arities
wenzelm@2957
    15
    -> string list -> bool -> (indexname -> bool) -> term list -> typ list
wenzelm@2957
    16
    -> term list * typ list * (indexname * typ) list
nipkow@5635
    17
  val appl_error: (term -> Pretty.T) -> (typ -> Pretty.T)
nipkow@5635
    18
    -> string -> term -> typ -> term -> typ -> string list
wenzelm@2957
    19
end;
wenzelm@2957
    20
wenzelm@2957
    21
structure TypeInfer: TYPE_INFER =
wenzelm@2957
    22
struct
wenzelm@2957
    23
wenzelm@2957
    24
wenzelm@2957
    25
(** term encodings **)
wenzelm@2957
    26
wenzelm@2957
    27
(*
wenzelm@2957
    28
  Flavours of term encodings:
wenzelm@2957
    29
wenzelm@2957
    30
    parse trees (type term):
wenzelm@2957
    31
      A very complicated structure produced by the syntax module's
wenzelm@2957
    32
      read functions.  Encodes types and sorts as terms; may contain
wenzelm@2957
    33
      explicit constraints and partial typing information (where
wenzelm@8087
    34
      dummies serve as wildcards).
wenzelm@2957
    35
wenzelm@2957
    36
      Parse trees are INTERNAL! Users should never encounter them,
wenzelm@2957
    37
      except in parse / print translation functions.
wenzelm@2957
    38
wenzelm@2957
    39
    raw terms (type term):
wenzelm@2957
    40
      Provide the user interface to type inferences.  They may contain
wenzelm@8087
    41
      partial type information (dummies are wildcards) or explicit
wenzelm@8087
    42
      type constraints (introduced via constrain: term -> typ ->
wenzelm@8087
    43
      term).
wenzelm@2957
    44
wenzelm@2957
    45
      The type inference function also lets users specify a certain
wenzelm@2957
    46
      subset of TVars to be treated as non-rigid inference parameters.
wenzelm@2957
    47
wenzelm@2957
    48
    preterms (type preterm):
wenzelm@2957
    49
      The internal representation for type inference.
wenzelm@2957
    50
wenzelm@2957
    51
    well-typed term (type term):
wenzelm@2957
    52
      Fully typed lambda terms to be accepted by appropriate
wenzelm@2957
    53
      certification functions.
wenzelm@2957
    54
*)
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@2957
    86
fun foldl_pretyps f (x, PConst (_, T)) = f (x, T)
wenzelm@2957
    87
  | foldl_pretyps f (x, PFree (_, T)) = f (x, T)
wenzelm@2957
    88
  | foldl_pretyps f (x, PVar (_, T)) = f (x, T)
wenzelm@2957
    89
  | foldl_pretyps _ (x, PBound _) = x
wenzelm@2957
    90
  | foldl_pretyps f (x, PAbs (_, T, t)) = foldl_pretyps f (f (x, T), t)
wenzelm@2957
    91
  | foldl_pretyps f (x, PAppl (t, u)) = foldl_pretyps f (foldl_pretyps f (x, t), u)
wenzelm@2957
    92
  | foldl_pretyps f (x, Constraint (t, T)) = f (foldl_pretyps f (x, t), T);
wenzelm@2957
    93
wenzelm@2957
    94
wenzelm@2957
    95
wenzelm@2957
    96
(** raw typs/terms to pretyps/preterms **)
wenzelm@2957
    97
wenzelm@2957
    98
(* pretyp(s)_of *)
wenzelm@2957
    99
wenzelm@8087
   100
fun anyT S = TFree ("'_dummy_", S);
wenzelm@8087
   101
val logicT = anyT logicS;
wenzelm@8087
   102
wenzelm@8611
   103
(*indicate polymorphic Vars*)
wenzelm@8611
   104
fun polymorphicT T = Type ("_polymorphic_", [T]);
wenzelm@8611
   105
wenzelm@2957
   106
fun pretyp_of is_param (params, typ) =
wenzelm@2957
   107
  let
wenzelm@2957
   108
    fun add_parms (ps, TVar (xi as (x, _), S)) =
wenzelm@2957
   109
          if is_param xi andalso is_none (assoc (ps, xi))
wenzelm@2957
   110
          then (xi, mk_param S) :: ps else ps
wenzelm@2957
   111
      | add_parms (ps, TFree _) = ps
wenzelm@2957
   112
      | add_parms (ps, Type (_, Ts)) = foldl add_parms (ps, Ts);
wenzelm@2957
   113
wenzelm@2957
   114
    val params' = add_parms (params, typ);
wenzelm@2957
   115
wenzelm@2957
   116
    fun pre_of (TVar (v as (xi, _))) =
wenzelm@2957
   117
          (case assoc (params', xi) of
wenzelm@2957
   118
            None => PTVar v
wenzelm@2957
   119
          | Some p => p)
wenzelm@8087
   120
      | pre_of (TFree ("'_dummy_", S)) = mk_param S
wenzelm@2957
   121
      | pre_of (TFree v) = PTFree v
wenzelm@2957
   122
      | pre_of (T as Type (a, Ts)) =
wenzelm@2957
   123
          if T = dummyT then mk_param []
wenzelm@2957
   124
          else PType (a, map pre_of Ts);
wenzelm@2957
   125
  in (params', pre_of typ) end;
wenzelm@2957
   126
wenzelm@4957
   127
fun pretyps_of is_param = foldl_map (pretyp_of is_param);
wenzelm@2957
   128
wenzelm@2957
   129
wenzelm@2957
   130
(* preterm(s)_of *)
wenzelm@2957
   131
wenzelm@2957
   132
fun preterm_of const_type is_param ((vparams, params), tm) =
wenzelm@2957
   133
  let
wenzelm@2957
   134
    fun add_vparm (ps, xi) =
wenzelm@2957
   135
      if is_none (assoc (ps, xi)) then
wenzelm@2957
   136
        (xi, mk_param []) :: ps
wenzelm@2957
   137
      else ps;
wenzelm@2957
   138
wenzelm@8611
   139
    fun add_vparms (ps, Var (xi, Type ("_polymorphic_", _))) = ps
wenzelm@8611
   140
      | add_vparms (ps, Var (xi, _)) = add_vparm (ps, xi)
wenzelm@2957
   141
      | add_vparms (ps, Free (x, _)) = add_vparm (ps, (x, ~1))
wenzelm@2957
   142
      | add_vparms (ps, Abs (_, _, t)) = add_vparms (ps, t)
wenzelm@2957
   143
      | add_vparms (ps, t $ u) = add_vparms (add_vparms (ps, t), u)
wenzelm@2957
   144
      | add_vparms (ps, _) = ps;
wenzelm@2957
   145
wenzelm@2957
   146
    val vparams' = add_vparms (vparams, tm);
wenzelm@2957
   147
    fun var_param xi = the (assoc (vparams', xi));
wenzelm@2957
   148
wenzelm@2957
   149
wenzelm@2957
   150
    val preT_of = pretyp_of is_param;
wenzelm@2957
   151
wenzelm@2957
   152
    fun constrain (ps, t) T =
wenzelm@2957
   153
      if T = dummyT then (ps, t)
wenzelm@2957
   154
      else
wenzelm@8087
   155
        let val (ps', T') = preT_of (ps, T)
wenzelm@8087
   156
        in (ps', Constraint (t, T')) end;
wenzelm@2957
   157
wenzelm@2957
   158
    fun pre_of (ps, Const (c, T)) =
wenzelm@2957
   159
          (case const_type c of
wenzelm@2957
   160
            Some U => constrain (ps, PConst (c, snd (pretyp_of (K true) ([], U)))) T
wenzelm@3784
   161
          | None => raise TYPE ("No such constant: " ^ quote c, [], []))
wenzelm@8611
   162
      | pre_of (ps, Var (xi, Type ("_polymorphic_", [T]))) =
wenzelm@8611
   163
          (ps, PVar (xi, snd (pretyp_of (K true) ([], T))))
wenzelm@8611
   164
      | pre_of (ps, Var (xi, T)) = constrain (ps, PVar (xi, var_param xi)) T
wenzelm@2957
   165
      | pre_of (ps, Free (x, T)) = constrain (ps, PFree (x, var_param (x, ~1))) T
wenzelm@2957
   166
      | pre_of (ps, Const ("_type_constraint_", T) $ t) = constrain (pre_of (ps, t)) T
wenzelm@2957
   167
      | pre_of (ps, Bound i) = (ps, PBound i)
wenzelm@2957
   168
      | pre_of (ps, Abs (x, T, t)) =
wenzelm@2957
   169
          let
wenzelm@2957
   170
            val (ps', T') = preT_of (ps, T);
wenzelm@2957
   171
            val (ps'', t') = pre_of (ps', t);
wenzelm@2957
   172
          in (ps'', PAbs (x, T', t')) end
wenzelm@2957
   173
      | pre_of (ps, t $ u) =
wenzelm@2957
   174
          let
wenzelm@2957
   175
            val (ps', t') = pre_of (ps, t);
wenzelm@2957
   176
            val (ps'', u') = pre_of (ps', u);
wenzelm@2957
   177
          in (ps'', PAppl (t', u')) end;
wenzelm@2957
   178
wenzelm@2957
   179
    val (params', tm') = pre_of (params, tm);
wenzelm@8611
   180
  in ((vparams', params'), tm') end;
wenzelm@2957
   181
wenzelm@4957
   182
fun preterms_of const_type is_param = foldl_map (preterm_of const_type is_param);
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@2957
   190
fun add_parmsT (rs, PType (_, Ts)) = foldl add_parmsT (rs, Ts)
wenzelm@2957
   191
  | add_parmsT (rs, Link (r as ref (Param _))) = r ins rs
wenzelm@2957
   192
  | add_parmsT (rs, Link (ref T)) = add_parmsT (rs, T)
wenzelm@2957
   193
  | add_parmsT (rs, _) = rs;
wenzelm@2957
   194
wenzelm@2957
   195
val add_parms = foldl_pretyps add_parmsT;
wenzelm@2957
   196
wenzelm@2957
   197
wenzelm@2957
   198
(* add_names *)
wenzelm@2957
   199
wenzelm@2957
   200
fun add_namesT (xs, PType (_, Ts)) = foldl add_namesT (xs, Ts)
wenzelm@2957
   201
  | add_namesT (xs, PTFree (x, _)) = x ins xs
wenzelm@2957
   202
  | add_namesT (xs, PTVar ((x, _), _)) = x ins xs
wenzelm@2957
   203
  | add_namesT (xs, Link (ref T)) = add_namesT (xs, T)
wenzelm@2957
   204
  | add_namesT (xs, Param _) = xs;
wenzelm@2957
   205
wenzelm@2957
   206
val add_names = foldl_pretyps add_namesT;
wenzelm@2957
   207
wenzelm@2957
   208
wenzelm@2957
   209
(* simple_typ/term_of *)
wenzelm@2957
   210
wenzelm@2957
   211
(*deref links, fail on params*)
wenzelm@2957
   212
fun simple_typ_of (PType (a, Ts)) = Type (a, map simple_typ_of Ts)
wenzelm@2957
   213
  | simple_typ_of (PTFree v) = TFree v
wenzelm@2957
   214
  | simple_typ_of (PTVar v) = TVar v
wenzelm@2957
   215
  | simple_typ_of (Link (ref T)) = simple_typ_of T
wenzelm@2957
   216
  | simple_typ_of (Param _) = sys_error "simple_typ_of: illegal Param";
wenzelm@2957
   217
wenzelm@2957
   218
(*convert types, drop constraints*)
wenzelm@2957
   219
fun simple_term_of (PConst (c, T)) = Const (c, simple_typ_of T)
wenzelm@2957
   220
  | simple_term_of (PFree (x, T)) = Free (x, simple_typ_of T)
wenzelm@2957
   221
  | simple_term_of (PVar (xi, T)) = Var (xi, simple_typ_of T)
wenzelm@2957
   222
  | simple_term_of (PBound i) = Bound i
wenzelm@2957
   223
  | simple_term_of (PAbs (x, T, t)) = Abs (x, simple_typ_of T, simple_term_of t)
wenzelm@2957
   224
  | simple_term_of (PAppl (t, u)) = simple_term_of t $ simple_term_of u
wenzelm@2957
   225
  | simple_term_of (Constraint (t, _)) = simple_term_of t;
wenzelm@2957
   226
wenzelm@2957
   227
wenzelm@2957
   228
(* typs_terms_of *)                             (*DESTRUCTIVE*)
wenzelm@2957
   229
wenzelm@2957
   230
fun typs_terms_of used mk_var prfx (Ts, ts) =
wenzelm@2957
   231
  let
wenzelm@4957
   232
    fun elim (r as ref (Param S), x) = r := mk_var (x, S)
wenzelm@4957
   233
      | elim _ = ();
wenzelm@2957
   234
wenzelm@2957
   235
    val used' = foldl add_names (foldl add_namesT (used, Ts), ts);
wenzelm@2957
   236
    val parms = rev (foldl add_parms (foldl add_parmsT ([], Ts), ts));
wenzelm@14695
   237
    val names = Term.invent_names used' (prfx ^ "'a") (length parms);
wenzelm@2957
   238
  in
wenzelm@4957
   239
    seq2 elim (parms, names);
wenzelm@2957
   240
    (map simple_typ_of Ts, map simple_term_of ts)
wenzelm@2957
   241
  end;
wenzelm@2957
   242
wenzelm@2957
   243
wenzelm@2957
   244
wenzelm@2957
   245
(** order-sorted unification of types **)       (*DESTRUCTIVE*)
wenzelm@2957
   246
wenzelm@2957
   247
exception NO_UNIFIER of string;
wenzelm@2957
   248
wenzelm@2957
   249
wenzelm@2957
   250
fun unify classrel arities =
wenzelm@2957
   251
  let
wenzelm@2957
   252
wenzelm@2957
   253
    (* adjust sorts of parameters *)
wenzelm@2957
   254
wenzelm@2957
   255
    fun not_in_sort x S' S =
wenzelm@2989
   256
      "Variable " ^ x ^ "::" ^ Sorts.str_of_sort S' ^ " not of sort " ^
wenzelm@2979
   257
        Sorts.str_of_sort S ^ ".";
wenzelm@2957
   258
wenzelm@7639
   259
    fun no_domain (a, c) = "No way to get " ^ a ^ "::" ^ c ^ ".";
wenzelm@7639
   260
wenzelm@4957
   261
    fun meet (_, []) = ()
wenzelm@4957
   262
      | meet (Link (r as (ref (Param S'))), S) =
wenzelm@2957
   263
          if Sorts.sort_le classrel (S', S) then ()
wenzelm@2957
   264
          else r := mk_param (Sorts.inter_sort classrel (S', S))
wenzelm@4957
   265
      | meet (Link (ref T), S) = meet (T, S)
wenzelm@4957
   266
      | meet (PType (a, Ts), S) =
wenzelm@7639
   267
          seq2 meet (Ts, Sorts.mg_domain (classrel, arities) a S
wenzelm@7639
   268
            handle Sorts.DOMAIN ac => raise NO_UNIFIER (no_domain ac))
wenzelm@4957
   269
      | meet (PTFree (x, S'), S) =
wenzelm@2957
   270
          if Sorts.sort_le classrel (S', S) then ()
wenzelm@2957
   271
          else raise NO_UNIFIER (not_in_sort x S' S)
wenzelm@4957
   272
      | meet (PTVar (xi, S'), S) =
wenzelm@2957
   273
          if Sorts.sort_le classrel (S', S) then ()
wenzelm@2957
   274
          else raise NO_UNIFIER (not_in_sort (Syntax.string_of_vname xi) S' S)
wenzelm@4957
   275
      | meet (Param _, _) = sys_error "meet";
wenzelm@2957
   276
wenzelm@2957
   277
wenzelm@2957
   278
    (* occurs check and assigment *)
wenzelm@2957
   279
wenzelm@2957
   280
    fun occurs_check r (Link (r' as ref T)) =
wenzelm@2957
   281
          if r = r' then raise NO_UNIFIER "Occurs check!"
wenzelm@2957
   282
          else occurs_check r T
wenzelm@2957
   283
      | occurs_check r (PType (_, Ts)) = seq (occurs_check r) Ts
wenzelm@2957
   284
      | occurs_check _ _ = ();
wenzelm@2957
   285
wenzelm@2957
   286
    fun assign r T S =
wenzelm@2957
   287
      (case deref T of
wenzelm@2957
   288
        T' as Link (r' as ref (Param _)) =>
wenzelm@8087
   289
          if r = r' then () else (meet (T', S); r := T')
wenzelm@8087
   290
      | T' => (occurs_check r T'; meet (T', S); r := T'));
wenzelm@2957
   291
wenzelm@2957
   292
wenzelm@2957
   293
    (* unification *)
wenzelm@2957
   294
wenzelm@4957
   295
    fun unif (Link (r as ref (Param S)), T) = assign r T S
wenzelm@4957
   296
      | unif (T, Link (r as ref (Param S))) = assign r T S
wenzelm@4957
   297
      | unif (Link (ref T), U) = unif (T, U)
wenzelm@4957
   298
      | unif (T, Link (ref U)) = unif (T, U)
wenzelm@4957
   299
      | unif (PType (a, Ts), PType (b, Us)) =
wenzelm@2979
   300
          if a <> b then
wenzelm@2979
   301
            raise NO_UNIFIER ("Clash of types " ^ quote a ^ " and " ^ quote b ^ ".")
wenzelm@4957
   302
          else seq2 unif (Ts, Us)
wenzelm@4957
   303
      | unif (T, U) = if T = U then () else raise NO_UNIFIER "";
wenzelm@2957
   304
wenzelm@2957
   305
  in unif end;
wenzelm@2957
   306
wenzelm@2957
   307
wenzelm@2957
   308
wenzelm@2957
   309
(** type inference **)
wenzelm@2957
   310
nipkow@5635
   311
fun appl_error prt prT why t T u U =
wenzelm@8087
   312
 ["Type error in application: " ^ why,
wenzelm@8087
   313
  "",
wenzelm@8087
   314
  Pretty.string_of (Pretty.block
wenzelm@8087
   315
    [Pretty.str "Operator:", Pretty.brk 2, prt t, Pretty.str " ::", Pretty.brk 1, prT T]),
wenzelm@8087
   316
  Pretty.string_of (Pretty.block
wenzelm@8087
   317
    [Pretty.str "Operand:", Pretty.brk 3, prt u, Pretty.str " ::", Pretty.brk 1, prT U]),
wenzelm@8087
   318
  ""];
wenzelm@8087
   319
nipkow@5635
   320
wenzelm@2957
   321
(* infer *)                                     (*DESTRUCTIVE*)
wenzelm@2957
   322
wenzelm@2979
   323
fun infer prt prT classrel arities =
wenzelm@2957
   324
  let
wenzelm@2979
   325
    (* errors *)
wenzelm@2957
   326
wenzelm@2979
   327
    fun unif_failed msg =
wenzelm@2979
   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@2957
   332
        val (Ts_bTs', ts') = typs_terms_of [] PTFree "??" (Ts @ map snd bs, ts);
nipkow@13629
   333
        val (Ts',Ts'') = Library.splitAt(length Ts, Ts_bTs');
nipkow@13629
   334
        val xs = map Free (map fst bs ~~ Ts'');
wenzelm@2957
   335
        val ts'' = map (fn t => subst_bounds (xs, t)) ts';
wenzelm@2979
   336
      in (ts'', Ts') end;
wenzelm@2979
   337
wenzelm@2979
   338
    fun err_loose i =
wenzelm@3784
   339
      raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], []);
wenzelm@2979
   340
wenzelm@3510
   341
    fun err_appl msg bs t T u U =
wenzelm@2979
   342
      let
wenzelm@3510
   343
        val ([t', u'], [T', U']) = prep_output bs [t, u] [T, U];
wenzelm@3510
   344
        val why =
wenzelm@3510
   345
          (case T' of
wenzelm@3510
   346
            Type ("fun", _) => "Incompatible operand type."
wenzelm@3510
   347
          | _ => "Operator not of function type.");
nipkow@5635
   348
        val text = unif_failed msg ^
nipkow@5635
   349
                   cat_lines (appl_error prt prT 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:", "",
nipkow@5635
   358
          Pretty.string_of
nipkow@5635
   359
           (Pretty.block [Pretty.str "Term:", Pretty.brk 2, prt t',
nipkow@5635
   360
                          Pretty.str " ::", Pretty.brk 1, prT T']),
nipkow@5635
   361
          Pretty.string_of
nipkow@5635
   362
           (Pretty.block [Pretty.str "Type:", Pretty.brk 2, prT 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@2979
   368
    val unif = unify classrel arities;
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
wenzelm@2979
   373
      | inf bs (PBound i) = snd (nth_elem (i, bs) handle LIST _ => 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@2957
   392
(* infer_types *)
wenzelm@2957
   393
wenzelm@2979
   394
fun infer_types prt prT const_type classrel arities used freeze is_param ts Ts =
wenzelm@2957
   395
  let
wenzelm@2957
   396
    (*convert to preterms/typs*)
wenzelm@2957
   397
    val (Tps, Ts') = pretyps_of (K true) ([], Ts);
wenzelm@2957
   398
    val ((vps, ps), ts') = preterms_of const_type is_param (([], Tps), ts);
wenzelm@2957
   399
wenzelm@2957
   400
    (*run type inference*)
wenzelm@2957
   401
    val tTs' = ListPair.map Constraint (ts', Ts');
wenzelm@2979
   402
    val _ = seq (fn t => (infer prt prT classrel arities t; ())) tTs';
wenzelm@2957
   403
wenzelm@2957
   404
    (*collect result unifier*)
wenzelm@2957
   405
    fun ch_var (xi, Link (r as ref (Param S))) = (r := PTVar (xi, S); None)
wenzelm@2957
   406
      | ch_var xi_T = Some xi_T;
wenzelm@2957
   407
    val env = mapfilter ch_var Tps;
wenzelm@2957
   408
wenzelm@2957
   409
    (*convert back to terms/typs*)
wenzelm@2957
   410
    val mk_var =
wenzelm@2957
   411
      if freeze then PTFree
wenzelm@2957
   412
      else (fn (x, S) => PTVar ((x, 0), S));
wenzelm@2957
   413
    val (final_Ts, final_ts) = typs_terms_of used mk_var "" (Ts', ts');
wenzelm@2957
   414
    val final_env = map (apsnd simple_typ_of) env;
wenzelm@2957
   415
  in
wenzelm@2957
   416
    (final_ts, final_Ts, final_env)
wenzelm@2957
   417
  end;
wenzelm@2957
   418
wenzelm@4957
   419
wenzelm@2957
   420
end;