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