src/Pure/type_infer.ML
author wenzelm
Wed Jul 09 16:54:17 1997 +0200 (1997-07-09)
changeset 3510 24d235feeb2a
parent 2989 8189a4870d19
child 3784 3b15cda31c97
permissions -rw-r--r--
improved type checking errors;
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@2979
    10
  val infer_types: (term -> Pretty.T) -> (typ -> Pretty.T)
wenzelm@2979
    11
    -> (string -> typ option) -> Sorts.classrel -> Sorts.arities
wenzelm@2957
    12
    -> string list -> bool -> (indexname -> bool) -> term list -> typ list
wenzelm@2957
    13
    -> term list * typ list * (indexname * typ) list
wenzelm@2957
    14
end;
wenzelm@2957
    15
wenzelm@2957
    16
structure TypeInfer: TYPE_INFER =
wenzelm@2957
    17
struct
wenzelm@2957
    18
wenzelm@2957
    19
wenzelm@2957
    20
(** generic utils **)
wenzelm@2957
    21
wenzelm@2957
    22
fun seq2 _ [] [] = ()
wenzelm@2957
    23
  | seq2 f (x :: xs) (y :: ys) = (f x y; seq2 f xs ys)
wenzelm@2957
    24
  | seq2 _ _ _ = raise LIST "seq2";
wenzelm@2957
    25
wenzelm@2957
    26
fun scan _ (xs, []) = (xs, [])
wenzelm@2957
    27
  | scan f (xs, y :: ys) =
wenzelm@2957
    28
      let
wenzelm@2957
    29
        val (xs', y') = f (xs, y);
wenzelm@2957
    30
        val (xs'', ys') = scan f (xs', ys);
wenzelm@2957
    31
      in (xs'', y' :: ys') end;
wenzelm@2957
    32
wenzelm@2957
    33
wenzelm@2957
    34
wenzelm@2957
    35
(** term encodings **)
wenzelm@2957
    36
wenzelm@2957
    37
(*
wenzelm@2957
    38
  Flavours of term encodings:
wenzelm@2957
    39
wenzelm@2957
    40
    parse trees (type term):
wenzelm@2957
    41
      A very complicated structure produced by the syntax module's
wenzelm@2957
    42
      read functions.  Encodes types and sorts as terms; may contain
wenzelm@2957
    43
      explicit constraints and partial typing information (where
wenzelm@2957
    44
      dummyT serves as wildcard).
wenzelm@2957
    45
wenzelm@2957
    46
      Parse trees are INTERNAL! Users should never encounter them,
wenzelm@2957
    47
      except in parse / print translation functions.
wenzelm@2957
    48
wenzelm@2957
    49
    raw terms (type term):
wenzelm@2957
    50
      Provide the user interface to type inferences.  They may contain
wenzelm@2957
    51
      partial type information (dummyT is wildcard) or explicit type
wenzelm@2957
    52
      constraints (introduced via constrain: term -> typ -> term).
wenzelm@2957
    53
wenzelm@2957
    54
      The type inference function also lets users specify a certain
wenzelm@2957
    55
      subset of TVars to be treated as non-rigid inference parameters.
wenzelm@2957
    56
wenzelm@2957
    57
    preterms (type preterm):
wenzelm@2957
    58
      The internal representation for type inference.
wenzelm@2957
    59
wenzelm@2957
    60
    well-typed term (type term):
wenzelm@2957
    61
      Fully typed lambda terms to be accepted by appropriate
wenzelm@2957
    62
      certification functions.
wenzelm@2957
    63
*)
wenzelm@2957
    64
wenzelm@2957
    65
wenzelm@2957
    66
wenzelm@2957
    67
(** pretyps and preterms **)
wenzelm@2957
    68
wenzelm@2957
    69
(*links to parameters may get instantiated, anything else is rigid*)
wenzelm@2957
    70
datatype pretyp =
wenzelm@2957
    71
  PType of string * pretyp list |
wenzelm@2957
    72
  PTFree of string * sort |
wenzelm@2957
    73
  PTVar of indexname * sort |
wenzelm@2957
    74
  Param of sort |
wenzelm@2957
    75
  Link of pretyp ref;
wenzelm@2957
    76
wenzelm@2957
    77
datatype preterm =
wenzelm@2957
    78
  PConst of string * pretyp |
wenzelm@2957
    79
  PFree of string * pretyp |
wenzelm@2957
    80
  PVar of indexname * pretyp |
wenzelm@2957
    81
  PBound of int |
wenzelm@2957
    82
  PAbs of string * pretyp * preterm |
wenzelm@2957
    83
  PAppl of preterm * preterm |
wenzelm@2957
    84
  Constraint of preterm * pretyp;
wenzelm@2957
    85
wenzelm@2957
    86
wenzelm@2957
    87
(* utils *)
wenzelm@2957
    88
wenzelm@2957
    89
val mk_param = Link o ref o Param;
wenzelm@2957
    90
wenzelm@2957
    91
fun deref (T as Link (ref (Param _))) = T
wenzelm@2957
    92
  | deref (Link (ref T)) = deref T
wenzelm@2957
    93
  | deref T = T;
wenzelm@2957
    94
wenzelm@2957
    95
fun foldl_pretyps f (x, PConst (_, T)) = f (x, T)
wenzelm@2957
    96
  | foldl_pretyps f (x, PFree (_, T)) = f (x, T)
wenzelm@2957
    97
  | foldl_pretyps f (x, PVar (_, T)) = f (x, T)
wenzelm@2957
    98
  | foldl_pretyps _ (x, PBound _) = x
wenzelm@2957
    99
  | foldl_pretyps f (x, PAbs (_, T, t)) = foldl_pretyps f (f (x, T), t)
wenzelm@2957
   100
  | foldl_pretyps f (x, PAppl (t, u)) = foldl_pretyps f (foldl_pretyps f (x, t), u)
wenzelm@2957
   101
  | foldl_pretyps f (x, Constraint (t, T)) = f (foldl_pretyps f (x, t), T);
wenzelm@2957
   102
wenzelm@2957
   103
wenzelm@2957
   104
wenzelm@2957
   105
(** raw typs/terms to pretyps/preterms **)
wenzelm@2957
   106
wenzelm@2957
   107
(* pretyp(s)_of *)
wenzelm@2957
   108
wenzelm@2957
   109
fun pretyp_of is_param (params, typ) =
wenzelm@2957
   110
  let
wenzelm@2957
   111
    fun add_parms (ps, TVar (xi as (x, _), S)) =
wenzelm@2957
   112
          if is_param xi andalso is_none (assoc (ps, xi))
wenzelm@2957
   113
          then (xi, mk_param S) :: ps else ps
wenzelm@2957
   114
      | add_parms (ps, TFree _) = ps
wenzelm@2957
   115
      | add_parms (ps, Type (_, Ts)) = foldl add_parms (ps, Ts);
wenzelm@2957
   116
wenzelm@2957
   117
    val params' = add_parms (params, typ);
wenzelm@2957
   118
wenzelm@2957
   119
    fun pre_of (TVar (v as (xi, _))) =
wenzelm@2957
   120
          (case assoc (params', xi) of
wenzelm@2957
   121
            None => PTVar v
wenzelm@2957
   122
          | Some p => p)
wenzelm@2957
   123
      | pre_of (TFree v) = PTFree v
wenzelm@2957
   124
      | pre_of (T as Type (a, Ts)) =
wenzelm@2957
   125
          if T = dummyT then mk_param []
wenzelm@2957
   126
          else PType (a, map pre_of Ts);
wenzelm@2957
   127
  in (params', pre_of typ) end;
wenzelm@2957
   128
wenzelm@2957
   129
fun pretyps_of is_param = scan (pretyp_of is_param);
wenzelm@2957
   130
wenzelm@2957
   131
wenzelm@2957
   132
(* preterm(s)_of *)
wenzelm@2957
   133
wenzelm@2957
   134
fun preterm_of const_type is_param ((vparams, params), tm) =
wenzelm@2957
   135
  let
wenzelm@2957
   136
    fun add_vparm (ps, xi) =
wenzelm@2957
   137
      if is_none (assoc (ps, xi)) then
wenzelm@2957
   138
        (xi, mk_param []) :: ps
wenzelm@2957
   139
      else ps;
wenzelm@2957
   140
wenzelm@2957
   141
    fun add_vparms (ps, Var (xi, _)) = add_vparm (ps, xi)
wenzelm@2957
   142
      | add_vparms (ps, Free (x, _)) = add_vparm (ps, (x, ~1))
wenzelm@2957
   143
      | add_vparms (ps, Abs (_, _, t)) = add_vparms (ps, t)
wenzelm@2957
   144
      | add_vparms (ps, t $ u) = add_vparms (add_vparms (ps, t), u)
wenzelm@2957
   145
      | add_vparms (ps, _) = ps;
wenzelm@2957
   146
wenzelm@2957
   147
    val vparams' = add_vparms (vparams, tm);
wenzelm@2957
   148
    fun var_param xi = the (assoc (vparams', xi));
wenzelm@2957
   149
wenzelm@2957
   150
wenzelm@2957
   151
    val preT_of = pretyp_of is_param;
wenzelm@2957
   152
wenzelm@2957
   153
    fun constrain (ps, t) T =
wenzelm@2957
   154
      if T = dummyT then (ps, t)
wenzelm@2957
   155
      else
wenzelm@2957
   156
        let val (ps', T') = preT_of (ps, T) in
wenzelm@2957
   157
          (ps', Constraint (t, T'))
wenzelm@2957
   158
        end;
wenzelm@2957
   159
wenzelm@2957
   160
    fun pre_of (ps, Const (c, T)) =
wenzelm@2957
   161
          (case const_type c of
wenzelm@2957
   162
            Some U => constrain (ps, PConst (c, snd (pretyp_of (K true) ([], U)))) T
wenzelm@2957
   163
          | None => raise_type ("No such constant: " ^ quote c) [] [])
wenzelm@2957
   164
      | pre_of (ps, Free (x, T)) = constrain (ps, PFree (x, var_param (x, ~1))) T
wenzelm@2957
   165
      | pre_of (ps, Var (xi, T)) = constrain (ps, PVar (xi, var_param xi)) 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
wenzelm@2957
   180
    val (params', tm') = pre_of (params, tm);
wenzelm@2957
   181
  in
wenzelm@2957
   182
    ((vparams', params'), tm')
wenzelm@2957
   183
  end;
wenzelm@2957
   184
wenzelm@2957
   185
fun preterms_of const_type is_param = scan (preterm_of const_type is_param);
wenzelm@2957
   186
wenzelm@2957
   187
wenzelm@2957
   188
wenzelm@2957
   189
(** pretyps/terms to typs/terms **)
wenzelm@2957
   190
wenzelm@2957
   191
(* add_parms *)
wenzelm@2957
   192
wenzelm@2957
   193
fun add_parmsT (rs, PType (_, Ts)) = foldl add_parmsT (rs, Ts)
wenzelm@2957
   194
  | add_parmsT (rs, Link (r as ref (Param _))) = r ins rs
wenzelm@2957
   195
  | add_parmsT (rs, Link (ref T)) = add_parmsT (rs, T)
wenzelm@2957
   196
  | add_parmsT (rs, _) = rs;
wenzelm@2957
   197
wenzelm@2957
   198
val add_parms = foldl_pretyps add_parmsT;
wenzelm@2957
   199
wenzelm@2957
   200
wenzelm@2957
   201
(* add_names *)
wenzelm@2957
   202
wenzelm@2957
   203
fun add_namesT (xs, PType (_, Ts)) = foldl add_namesT (xs, Ts)
wenzelm@2957
   204
  | add_namesT (xs, PTFree (x, _)) = x ins xs
wenzelm@2957
   205
  | add_namesT (xs, PTVar ((x, _), _)) = x ins xs
wenzelm@2957
   206
  | add_namesT (xs, Link (ref T)) = add_namesT (xs, T)
wenzelm@2957
   207
  | add_namesT (xs, Param _) = xs;
wenzelm@2957
   208
wenzelm@2957
   209
val add_names = foldl_pretyps add_namesT;
wenzelm@2957
   210
wenzelm@2957
   211
wenzelm@2957
   212
(* simple_typ/term_of *)
wenzelm@2957
   213
wenzelm@2957
   214
(*deref links, fail on params*)
wenzelm@2957
   215
fun simple_typ_of (PType (a, Ts)) = Type (a, map simple_typ_of Ts)
wenzelm@2957
   216
  | simple_typ_of (PTFree v) = TFree v
wenzelm@2957
   217
  | simple_typ_of (PTVar v) = TVar v
wenzelm@2957
   218
  | simple_typ_of (Link (ref T)) = simple_typ_of T
wenzelm@2957
   219
  | simple_typ_of (Param _) = sys_error "simple_typ_of: illegal Param";
wenzelm@2957
   220
wenzelm@2957
   221
(*convert types, drop constraints*)
wenzelm@2957
   222
fun simple_term_of (PConst (c, T)) = Const (c, simple_typ_of T)
wenzelm@2957
   223
  | simple_term_of (PFree (x, T)) = Free (x, simple_typ_of T)
wenzelm@2957
   224
  | simple_term_of (PVar (xi, T)) = Var (xi, simple_typ_of T)
wenzelm@2957
   225
  | simple_term_of (PBound i) = Bound i
wenzelm@2957
   226
  | simple_term_of (PAbs (x, T, t)) = Abs (x, simple_typ_of T, simple_term_of t)
wenzelm@2957
   227
  | simple_term_of (PAppl (t, u)) = simple_term_of t $ simple_term_of u
wenzelm@2957
   228
  | simple_term_of (Constraint (t, _)) = simple_term_of t;
wenzelm@2957
   229
wenzelm@2957
   230
wenzelm@2957
   231
(* typs_terms_of *)                             (*DESTRUCTIVE*)
wenzelm@2957
   232
wenzelm@2957
   233
fun typs_terms_of used mk_var prfx (Ts, ts) =
wenzelm@2957
   234
  let
wenzelm@2957
   235
    fun elim (r as ref (Param S)) x = r := mk_var (x, S)
wenzelm@2957
   236
      | elim _ _ = ();
wenzelm@2957
   237
wenzelm@2957
   238
    val used' = foldl add_names (foldl add_namesT (used, Ts), ts);
wenzelm@2957
   239
    val parms = rev (foldl add_parms (foldl add_parmsT ([], Ts), ts));
wenzelm@2957
   240
    val pre_names = replicate (length parms) (prfx ^ "'");
wenzelm@2957
   241
    val names = variantlist (pre_names, prfx ^ "'" :: used');
wenzelm@2957
   242
  in
wenzelm@2957
   243
    seq2 elim parms names;
wenzelm@2957
   244
    (map simple_typ_of Ts, map simple_term_of ts)
wenzelm@2957
   245
  end;
wenzelm@2957
   246
wenzelm@2957
   247
wenzelm@2957
   248
wenzelm@2957
   249
(** order-sorted unification of types **)       (*DESTRUCTIVE*)
wenzelm@2957
   250
wenzelm@2957
   251
exception NO_UNIFIER of string;
wenzelm@2957
   252
wenzelm@2957
   253
wenzelm@2957
   254
fun unify classrel arities =
wenzelm@2957
   255
  let
wenzelm@2957
   256
wenzelm@2957
   257
    (* adjust sorts of parameters *)
wenzelm@2957
   258
wenzelm@2957
   259
    fun not_in_sort x S' S =
wenzelm@2989
   260
      "Variable " ^ x ^ "::" ^ Sorts.str_of_sort S' ^ " not of sort " ^
wenzelm@2979
   261
        Sorts.str_of_sort S ^ ".";
wenzelm@2957
   262
wenzelm@2957
   263
    fun meet _ [] = ()
wenzelm@2957
   264
      | meet (Link (r as (ref (Param S')))) S =
wenzelm@2957
   265
          if Sorts.sort_le classrel (S', S) then ()
wenzelm@2957
   266
          else r := mk_param (Sorts.inter_sort classrel (S', S))
wenzelm@2957
   267
      | meet (Link (ref T)) S = meet T S
wenzelm@2957
   268
      | meet (PType (a, Ts)) S =
wenzelm@2957
   269
          seq2 meet Ts (Sorts.mg_domain classrel arities a S
wenzelm@2957
   270
            handle TYPE (msg, _, _) => raise NO_UNIFIER msg)
wenzelm@2957
   271
      | meet (PTFree (x, S')) S =
wenzelm@2957
   272
          if Sorts.sort_le classrel (S', S) then ()
wenzelm@2957
   273
          else raise NO_UNIFIER (not_in_sort x S' S)
wenzelm@2957
   274
      | meet (PTVar (xi, S')) S =
wenzelm@2957
   275
          if Sorts.sort_le classrel (S', S) then ()
wenzelm@2957
   276
          else raise NO_UNIFIER (not_in_sort (Syntax.string_of_vname xi) S' S)
wenzelm@2957
   277
      | meet (Param _) _ = sys_error "meet";
wenzelm@2957
   278
wenzelm@2957
   279
wenzelm@2957
   280
    (* occurs check and assigment *)
wenzelm@2957
   281
wenzelm@2957
   282
    fun occurs_check r (Link (r' as ref T)) =
wenzelm@2957
   283
          if r = r' then raise NO_UNIFIER "Occurs check!"
wenzelm@2957
   284
          else occurs_check r T
wenzelm@2957
   285
      | occurs_check r (PType (_, Ts)) = seq (occurs_check r) Ts
wenzelm@2957
   286
      | occurs_check _ _ = ();
wenzelm@2957
   287
wenzelm@2957
   288
    fun assign r T S =
wenzelm@2957
   289
      (case deref T of
wenzelm@2957
   290
        T' as Link (r' as ref (Param _)) =>
wenzelm@2957
   291
          if r = r' then () else (r := T'; meet T' S)
wenzelm@2957
   292
      | T' => (occurs_check r T'; r := T'; meet T' S));
wenzelm@2957
   293
wenzelm@2957
   294
wenzelm@2957
   295
    (* unification *)
wenzelm@2957
   296
wenzelm@2957
   297
    fun unif (Link (r as ref (Param S))) T = assign r T S
wenzelm@2957
   298
      | unif T (Link (r as ref (Param S))) = assign r T S
wenzelm@2957
   299
      | unif (Link (ref T)) U = unif T U
wenzelm@2957
   300
      | unif T (Link (ref U)) = unif T U
wenzelm@2957
   301
      | unif (PType (a, Ts)) (PType (b, Us)) =
wenzelm@2979
   302
          if a <> b then
wenzelm@2979
   303
            raise NO_UNIFIER ("Clash of types " ^ quote a ^ " and " ^ quote b ^ ".")
wenzelm@2957
   304
          else seq2 unif Ts Us
wenzelm@2979
   305
      | unif T U = if T = U then () else raise NO_UNIFIER "";
wenzelm@2957
   306
wenzelm@2957
   307
  in unif end;
wenzelm@2957
   308
wenzelm@2957
   309
wenzelm@2957
   310
wenzelm@2957
   311
(** type inference **)
wenzelm@2957
   312
wenzelm@2957
   313
(* infer *)                                     (*DESTRUCTIVE*)
wenzelm@2957
   314
wenzelm@2979
   315
fun infer prt prT classrel arities =
wenzelm@2957
   316
  let
wenzelm@2979
   317
    (* errors *)
wenzelm@2957
   318
wenzelm@2979
   319
    fun unif_failed msg =
wenzelm@2979
   320
      "Type unification failed" ^ (if msg = "" then "." else ": " ^ msg) ^ "\n";
wenzelm@2979
   321
wenzelm@2979
   322
    val str_of = Pretty.string_of;
wenzelm@2979
   323
wenzelm@2979
   324
    fun prep_output bs ts Ts =
wenzelm@2957
   325
      let
wenzelm@2957
   326
        val (Ts_bTs', ts') = typs_terms_of [] PTFree "??" (Ts @ map snd bs, ts);
wenzelm@2957
   327
        val len = length Ts;
wenzelm@2957
   328
        val Ts' = take (len, Ts_bTs');
wenzelm@2957
   329
        val xs = map Free (map fst bs ~~ drop (len, Ts_bTs'));
wenzelm@2957
   330
        val ts'' = map (fn t => subst_bounds (xs, t)) ts';
wenzelm@2979
   331
      in (ts'', Ts') end;
wenzelm@2979
   332
wenzelm@2979
   333
    fun err_loose i =
wenzelm@2979
   334
      raise_type ("Loose bound variable: B." ^ string_of_int i) [] [];
wenzelm@2979
   335
wenzelm@3510
   336
    fun err_appl msg bs t T u U =
wenzelm@2979
   337
      let
wenzelm@3510
   338
        val ([t', u'], [T', U']) = prep_output bs [t, u] [T, U];
wenzelm@3510
   339
        val why =
wenzelm@3510
   340
          (case T' of
wenzelm@3510
   341
            Type ("fun", _) => "Incompatible operand type."
wenzelm@3510
   342
          | _ => "Operator not of function type.");
wenzelm@2979
   343
        val text = cat_lines
wenzelm@2979
   344
         [unif_failed msg,
wenzelm@3510
   345
          "Type error in application: " ^ why,
wenzelm@2979
   346
          "",
wenzelm@3510
   347
          str_of (Pretty.block [Pretty.str "Operator:", Pretty.brk 2, prt t',
wenzelm@2979
   348
            Pretty.str " :: ", prT T']),
wenzelm@3510
   349
          str_of (Pretty.block [Pretty.str "Operand:", Pretty.brk 3, prt u',
wenzelm@2979
   350
            Pretty.str " :: ", prT U']), ""];
wenzelm@3510
   351
      in raise_type text [T', U'] [t', u'] end;
wenzelm@2979
   352
wenzelm@2979
   353
    fun err_constraint msg bs t T U =
wenzelm@2979
   354
      let
wenzelm@2979
   355
        val ([t'], [T', U']) = prep_output bs [t] [T, U];
wenzelm@2979
   356
        val text = cat_lines
wenzelm@2979
   357
         [unif_failed msg,
wenzelm@2979
   358
          "Cannot meet type constraint:",
wenzelm@2979
   359
          "",
wenzelm@3510
   360
          str_of (Pretty.block [Pretty.str "Term:", Pretty.brk 2, prt t',
wenzelm@2979
   361
            Pretty.str " :: ", prT T']),
wenzelm@3510
   362
          str_of (Pretty.block [Pretty.str "Type:", Pretty.brk 2, prT U']), ""];
wenzelm@2979
   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@3510
   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@2979
   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@2957
   419
end;