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