src/Pure/type_infer.ML
changeset 32141 31cd1ea502aa
parent 32033 f92df23c3305
child 32146 4937d9836824
     1.1 --- a/src/Pure/type_infer.ML	Wed Jul 22 18:08:45 2009 +0200
     1.2 +++ b/src/Pure/type_infer.ML	Thu Jul 23 09:38:22 2009 +0200
     1.3 @@ -71,13 +71,12 @@
     1.4  
     1.5  (** pretyps and preterms **)
     1.6  
     1.7 -(*links to parameters may get instantiated, anything else is rigid*)
     1.8 +(*parameters may get instantiated, anything else is rigid*)
     1.9  datatype pretyp =
    1.10    PType of string * pretyp list |
    1.11    PTFree of string * sort |
    1.12    PTVar of indexname * sort |
    1.13 -  Param of sort |
    1.14 -  Link of pretyp ref;
    1.15 +  Param of int * sort;
    1.16  
    1.17  datatype preterm =
    1.18    PConst of string * pretyp |
    1.19 @@ -91,11 +90,10 @@
    1.20  
    1.21  (* utils *)
    1.22  
    1.23 -val mk_param = Link o ref o Param;
    1.24 -
    1.25 -fun deref (T as Link (ref (Param _))) = T
    1.26 -  | deref (Link (ref T)) = deref T
    1.27 -  | deref T = T;
    1.28 +fun deref tye (T as Param (i, S)) = (case Inttab.lookup tye i of
    1.29 +      NONE => T
    1.30 +    | SOME U => deref tye U)
    1.31 +  | deref tye T = T;
    1.32  
    1.33  fun fold_pretyps f (PConst (_, T)) x = f T x
    1.34    | fold_pretyps f (PFree (_, T)) x = f T x
    1.35 @@ -111,46 +109,50 @@
    1.36  
    1.37  (* pretyp_of *)
    1.38  
    1.39 -fun pretyp_of is_para typ params =
    1.40 +fun pretyp_of is_para typ params_idx =
    1.41    let
    1.42 -    val params' = fold_atyps
    1.43 +    val (params', idx) = fold_atyps
    1.44        (fn TVar (xi as (x, _), S) =>
    1.45 -          (fn ps =>
    1.46 +          (fn ps_idx as (ps, idx) =>
    1.47              if is_para xi andalso not (Vartab.defined ps xi)
    1.48 -            then Vartab.update (xi, mk_param S) ps else ps)
    1.49 -        | _ => I) typ params;
    1.50 +            then (Vartab.update (xi, Param (idx, S)) ps, idx + 1) else ps_idx)
    1.51 +        | _ => I) typ params_idx;
    1.52  
    1.53 -    fun pre_of (TVar (v as (xi, _))) =
    1.54 +    fun pre_of (TVar (v as (xi, _))) idx =
    1.55            (case Vartab.lookup params' xi of
    1.56              NONE => PTVar v
    1.57 -          | SOME p => p)
    1.58 -      | pre_of (TFree ("'_dummy_", S)) = mk_param S
    1.59 -      | pre_of (TFree v) = PTFree v
    1.60 -      | pre_of (T as Type (a, Ts)) =
    1.61 -          if T = dummyT then mk_param []
    1.62 -          else PType (a, map pre_of Ts);
    1.63 -  in (pre_of typ, params') end;
    1.64 +          | SOME p => p, idx)
    1.65 +      | pre_of (TFree ("'_dummy_", S)) idx = (Param (idx, S), idx + 1)
    1.66 +      | pre_of (TFree v) idx = (PTFree v, idx)
    1.67 +      | pre_of (T as Type (a, Ts)) idx =
    1.68 +          if T = dummyT then (Param (idx, []), idx + 1)
    1.69 +          else
    1.70 +            let val (Ts', idx') = fold_map pre_of Ts idx
    1.71 +            in (PType (a, Ts'), idx') end;
    1.72 +
    1.73 +    val (ptyp, idx') = pre_of typ idx;
    1.74 +  in (ptyp, (params', idx')) end;
    1.75  
    1.76  
    1.77  (* preterm_of *)
    1.78  
    1.79 -fun preterm_of const_type is_para tm (vparams, params) =
    1.80 +fun preterm_of const_type is_para tm (vparams, params, idx) =
    1.81    let
    1.82 -    fun add_vparm xi ps =
    1.83 +    fun add_vparm xi (ps_idx as (ps, idx)) =
    1.84        if not (Vartab.defined ps xi) then
    1.85 -        Vartab.update (xi, mk_param []) ps
    1.86 -      else ps;
    1.87 +        (Vartab.update (xi, Param (idx, [])) ps, idx + 1)
    1.88 +      else ps_idx;
    1.89  
    1.90 -    val vparams' = fold_aterms
    1.91 +    val (vparams', idx') = fold_aterms
    1.92        (fn Var (_, Type ("_polymorphic_", _)) => I
    1.93          | Var (xi, _) => add_vparm xi
    1.94          | Free (x, _) => add_vparm (x, ~1)
    1.95          | _ => I)
    1.96 -      tm vparams;
    1.97 +      tm (vparams, idx);
    1.98      fun var_param xi = the (Vartab.lookup vparams' xi);
    1.99  
   1.100      val preT_of = pretyp_of is_para;
   1.101 -    fun polyT_of T = fst (pretyp_of (K true) T Vartab.empty);
   1.102 +    fun polyT_of T idx = apsnd snd (pretyp_of (K true) T (Vartab.empty, idx));
   1.103  
   1.104      fun constraint T t ps =
   1.105        if T = dummyT then (t, ps)
   1.106 @@ -158,29 +160,33 @@
   1.107          let val (T', ps') = preT_of T ps
   1.108          in (Constraint (t, T'), ps') end;
   1.109  
   1.110 -    fun pre_of (Const (c, T)) ps =
   1.111 +    fun pre_of (Const (c, T)) (ps, idx) =
   1.112            (case const_type c of
   1.113 -            SOME U => constraint T (PConst (c, polyT_of U)) ps
   1.114 +            SOME U =>
   1.115 +              let val (pU, idx') = polyT_of U idx
   1.116 +              in constraint T (PConst (c, pU)) (ps, idx') end
   1.117            | NONE => raise TYPE ("No such constant: " ^ quote c, [], []))
   1.118 -      | pre_of (Var (xi, Type ("_polymorphic_", [T]))) ps = (PVar (xi, polyT_of T), ps)
   1.119 -      | pre_of (Var (xi, T)) ps = constraint T (PVar (xi, var_param xi)) ps
   1.120 -      | pre_of (Free (x, T)) ps = constraint T (PFree (x, var_param (x, ~1))) ps
   1.121 -      | pre_of (Const ("_type_constraint_", Type ("fun", [T, _])) $ t) ps =
   1.122 -          pre_of t ps |-> constraint T
   1.123 -      | pre_of (Bound i) ps = (PBound i, ps)
   1.124 -      | pre_of (Abs (x, T, t)) ps =
   1.125 +      | pre_of (Var (xi, Type ("_polymorphic_", [T]))) (ps, idx) =
   1.126 +          let val (pT, idx') = polyT_of T idx
   1.127 +          in (PVar (xi, pT), (ps, idx')) end
   1.128 +      | pre_of (Var (xi, T)) ps_idx = constraint T (PVar (xi, var_param xi)) ps_idx
   1.129 +      | pre_of (Free (x, T)) ps_idx = constraint T (PFree (x, var_param (x, ~1))) ps_idx
   1.130 +      | pre_of (Const ("_type_constraint_", Type ("fun", [T, _])) $ t) ps_idx =
   1.131 +          pre_of t ps_idx |-> constraint T
   1.132 +      | pre_of (Bound i) ps_idx = (PBound i, ps_idx)
   1.133 +      | pre_of (Abs (x, T, t)) ps_idx =
   1.134            let
   1.135 -            val (T', ps') = preT_of T ps;
   1.136 -            val (t', ps'') = pre_of t ps';
   1.137 -          in (PAbs (x, T', t'), ps'') end
   1.138 -      | pre_of (t $ u) ps =
   1.139 +            val (T', ps_idx') = preT_of T ps_idx;
   1.140 +            val (t', ps_idx'') = pre_of t ps_idx';
   1.141 +          in (PAbs (x, T', t'), ps_idx'') end
   1.142 +      | pre_of (t $ u) ps_idx =
   1.143            let
   1.144 -            val (t', ps') = pre_of t ps;
   1.145 -            val (u', ps'') = pre_of u ps';
   1.146 -          in (PAppl (t', u'), ps'') end;
   1.147 +            val (t', ps_idx') = pre_of t ps_idx;
   1.148 +            val (u', ps_idx'') = pre_of u ps_idx';
   1.149 +          in (PAppl (t', u'), ps_idx'') end;
   1.150  
   1.151 -    val (tm', params') = pre_of tm params;
   1.152 -  in (tm', (vparams', params')) end;
   1.153 +    val (tm', (params', idx'')) = pre_of tm (params, idx');
   1.154 +  in (tm', (vparams', params', idx'')) end;
   1.155  
   1.156  
   1.157  
   1.158 @@ -188,62 +194,61 @@
   1.159  
   1.160  (* add_parms *)
   1.161  
   1.162 -fun add_parmsT (PType (_, Ts)) rs = fold add_parmsT Ts rs
   1.163 -  | add_parmsT (Link (r as ref (Param _))) rs = insert (op =) r rs
   1.164 -  | add_parmsT (Link (ref T)) rs = add_parmsT T rs
   1.165 -  | add_parmsT _ rs = rs;
   1.166 +fun add_parmsT tye T = case deref tye T of
   1.167 +    PType (_, Ts) => fold (add_parmsT tye) Ts
   1.168 +  | Param (i, _) => insert (op =) i
   1.169 +  | _ => I;
   1.170  
   1.171 -val add_parms = fold_pretyps add_parmsT;
   1.172 +fun add_parms tye = fold_pretyps (add_parmsT tye);
   1.173  
   1.174  
   1.175  (* add_names *)
   1.176  
   1.177 -fun add_namesT (PType (_, Ts)) = fold add_namesT Ts
   1.178 -  | add_namesT (PTFree (x, _)) = Name.declare x
   1.179 -  | add_namesT (PTVar ((x, _), _)) = Name.declare x
   1.180 -  | add_namesT (Link (ref T)) = add_namesT T
   1.181 -  | add_namesT (Param _) = I;
   1.182 +fun add_namesT tye T = case deref tye T of
   1.183 +    PType (_, Ts) => fold (add_namesT tye) Ts
   1.184 +  | PTFree (x, _) => Name.declare x
   1.185 +  | PTVar ((x, _), _) => Name.declare x
   1.186 +  | Param _ => I;
   1.187  
   1.188 -val add_names = fold_pretyps add_namesT;
   1.189 +fun add_names tye = fold_pretyps (add_namesT tye);
   1.190  
   1.191  
   1.192  (* simple_typ/term_of *)
   1.193  
   1.194 -(*deref links, fail on params*)
   1.195 -fun simple_typ_of (PType (a, Ts)) = Type (a, map simple_typ_of Ts)
   1.196 -  | simple_typ_of (PTFree v) = TFree v
   1.197 -  | simple_typ_of (PTVar v) = TVar v
   1.198 -  | simple_typ_of (Link (ref T)) = simple_typ_of T
   1.199 -  | simple_typ_of (Param _) = sys_error "simple_typ_of: illegal Param";
   1.200 +fun simple_typ_of tye f T = case deref tye T of
   1.201 +    PType (a, Ts) => Type (a, map (simple_typ_of tye f) Ts)
   1.202 +  | PTFree v => TFree v
   1.203 +  | PTVar v => TVar v
   1.204 +  | Param (i, S) => TVar (f i, S);
   1.205  
   1.206  (*convert types, drop constraints*)
   1.207 -fun simple_term_of (PConst (c, T)) = Const (c, simple_typ_of T)
   1.208 -  | simple_term_of (PFree (x, T)) = Free (x, simple_typ_of T)
   1.209 -  | simple_term_of (PVar (xi, T)) = Var (xi, simple_typ_of T)
   1.210 -  | simple_term_of (PBound i) = Bound i
   1.211 -  | simple_term_of (PAbs (x, T, t)) = Abs (x, simple_typ_of T, simple_term_of t)
   1.212 -  | simple_term_of (PAppl (t, u)) = simple_term_of t $ simple_term_of u
   1.213 -  | simple_term_of (Constraint (t, _)) = simple_term_of t;
   1.214 +fun simple_term_of tye f (PConst (c, T)) = Const (c, simple_typ_of tye f T)
   1.215 +  | simple_term_of tye f (PFree (x, T)) = Free (x, simple_typ_of tye f T)
   1.216 +  | simple_term_of tye f (PVar (xi, T)) = Var (xi, simple_typ_of tye f T)
   1.217 +  | simple_term_of tye f (PBound i) = Bound i
   1.218 +  | simple_term_of tye f (PAbs (x, T, t)) =
   1.219 +      Abs (x, simple_typ_of tye f T, simple_term_of tye f t)
   1.220 +  | simple_term_of tye f (PAppl (t, u)) =
   1.221 +      simple_term_of tye f t $ simple_term_of tye f u
   1.222 +  | simple_term_of tye f (Constraint (t, _)) = simple_term_of tye f t;
   1.223  
   1.224  
   1.225 -(* typs_terms_of *)                             (*DESTRUCTIVE*)
   1.226 +(* typs_terms_of *)
   1.227  
   1.228 -fun typs_terms_of used maxidx (Ts, ts) =
   1.229 +fun typs_terms_of tye used maxidx (Ts, ts) =
   1.230    let
   1.231 -    fun elim (r as ref (Param S), x) = r := PTVar ((x, maxidx + 1), S)
   1.232 -      | elim _ = ();
   1.233 -
   1.234 -    val used' = fold add_names ts (fold add_namesT Ts used);
   1.235 -    val parms = rev (fold add_parms ts (fold add_parmsT Ts []));
   1.236 +    val used' = fold (add_names tye) ts (fold (add_namesT tye) Ts used);
   1.237 +    val parms = rev (fold (add_parms tye) ts (fold (add_parmsT tye) Ts []));
   1.238      val names = Name.invents used' ("?" ^ Name.aT) (length parms);
   1.239 -    val _ = ListPair.app elim (parms, names);
   1.240 -  in (map simple_typ_of Ts, map simple_term_of ts) end;
   1.241 +    val tab = Inttab.make (parms ~~ names);
   1.242 +    fun f i = (the (Inttab.lookup tab i), maxidx + 1);
   1.243 +  in (map (simple_typ_of tye f) Ts, map (simple_term_of tye f) ts) end;
   1.244  
   1.245  
   1.246  
   1.247 -(** order-sorted unification of types **)       (*DESTRUCTIVE*)
   1.248 +(** order-sorted unification of types **)
   1.249  
   1.250 -exception NO_UNIFIER of string;
   1.251 +exception NO_UNIFIER of string * pretyp Inttab.table;
   1.252  
   1.253  fun unify pp tsig =
   1.254    let
   1.255 @@ -254,49 +259,52 @@
   1.256        "Variable " ^ x ^ "::" ^ Pretty.string_of_sort pp S' ^ " not of sort " ^
   1.257          Pretty.string_of_sort pp S;
   1.258  
   1.259 -    fun meet (_, []) = ()
   1.260 -      | meet (Link (r as (ref (Param S'))), S) =
   1.261 -          if Type.subsort tsig (S', S) then ()
   1.262 -          else r := mk_param (Type.inter_sort tsig (S', S))
   1.263 -      | meet (Link (ref T), S) = meet (T, S)
   1.264 -      | meet (PType (a, Ts), S) =
   1.265 -          ListPair.app meet (Ts, Type.arity_sorts pp tsig a S
   1.266 -            handle ERROR msg => raise NO_UNIFIER msg)
   1.267 -      | meet (PTFree (x, S'), S) =
   1.268 -          if Type.subsort tsig (S', S) then ()
   1.269 -          else raise NO_UNIFIER (not_of_sort x S' S)
   1.270 -      | meet (PTVar (xi, S'), S) =
   1.271 -          if Type.subsort tsig (S', S) then ()
   1.272 -          else raise NO_UNIFIER (not_of_sort (Term.string_of_vname xi) S' S)
   1.273 -      | meet (Param _, _) = sys_error "meet";
   1.274 +    fun meet (_, []) tye_idx = tye_idx
   1.275 +      | meet (Param (i, S'), S) (tye_idx as (tye, idx)) =
   1.276 +          if Type.subsort tsig (S', S) then tye_idx
   1.277 +          else (Inttab.update_new (i,
   1.278 +            Param (idx, Type.inter_sort tsig (S', S))) tye, idx + 1)
   1.279 +      | meet (PType (a, Ts), S) (tye_idx as (tye, _)) =
   1.280 +          meets (Ts, Type.arity_sorts pp tsig a S
   1.281 +            handle ERROR msg => raise NO_UNIFIER (msg, tye)) tye_idx
   1.282 +      | meet (PTFree (x, S'), S) (tye_idx as (tye, _)) =
   1.283 +          if Type.subsort tsig (S', S) then tye_idx
   1.284 +          else raise NO_UNIFIER (not_of_sort x S' S, tye)
   1.285 +      | meet (PTVar (xi, S'), S) (tye_idx as (tye, _)) =
   1.286 +          if Type.subsort tsig (S', S) then tye_idx
   1.287 +          else raise NO_UNIFIER (not_of_sort (Term.string_of_vname xi) S' S, tye)
   1.288 +    and meets (T :: Ts, S :: Ss) (tye_idx as (tye, _)) =
   1.289 +          meets (Ts, Ss) (meet (deref tye T, S) tye_idx)
   1.290 +      | meets _ tye_idx = tye_idx;
   1.291  
   1.292  
   1.293      (* occurs check and assigment *)
   1.294  
   1.295 -    fun occurs_check r (Link (r' as ref T)) =
   1.296 -          if r = r' then raise NO_UNIFIER "Occurs check!"
   1.297 -          else occurs_check r T
   1.298 -      | occurs_check r (PType (_, Ts)) = List.app (occurs_check r) Ts
   1.299 -      | occurs_check _ _ = ();
   1.300 +    fun occurs_check tye i (Param (i', S)) =
   1.301 +          if i = i' then raise NO_UNIFIER ("Occurs check!", tye)
   1.302 +          else (case Inttab.lookup tye i' of
   1.303 +              NONE => ()
   1.304 +            | SOME T => occurs_check tye i T)
   1.305 +      | occurs_check tye i (PType (_, Ts)) = List.app (occurs_check tye i) Ts
   1.306 +      | occurs_check _ _ _ = ();
   1.307  
   1.308 -    fun assign r T S =
   1.309 -      (case deref T of
   1.310 -        T' as Link (r' as ref (Param _)) =>
   1.311 -          if r = r' then () else (meet (T', S); r := T')
   1.312 -      | T' => (occurs_check r T'; meet (T', S); r := T'));
   1.313 +    fun assign i (T as Param (i', _)) S (tye_idx as (tye, idx)) =
   1.314 +          if i = i' then tye_idx
   1.315 +          else meet (T, S) (Inttab.update_new (i, T) tye, idx)
   1.316 +      | assign i T S (tye, idx) =
   1.317 +          (occurs_check tye i T; meet (T, S) (Inttab.update_new (i, T) tye, idx));
   1.318  
   1.319  
   1.320      (* unification *)
   1.321  
   1.322 -    fun unif (Link (r as ref (Param S)), T) = assign r T S
   1.323 -      | unif (T, Link (r as ref (Param S))) = assign r T S
   1.324 -      | unif (Link (ref T), U) = unif (T, U)
   1.325 -      | unif (T, Link (ref U)) = unif (T, U)
   1.326 -      | unif (PType (a, Ts), PType (b, Us)) =
   1.327 +    fun unif (T1, T2) (tye_idx as (tye, idx)) = case (deref tye T1, deref tye T2) of
   1.328 +        (Param (i, S), T) => assign i T S tye_idx
   1.329 +      | (T, Param (i, S)) => assign i T S tye_idx
   1.330 +      | (PType (a, Ts), PType (b, Us)) =>
   1.331            if a <> b then
   1.332 -            raise NO_UNIFIER ("Clash of types " ^ quote a ^ " and " ^ quote b)
   1.333 -          else ListPair.app unif (Ts, Us)
   1.334 -      | unif (T, U) = if T = U then () else raise NO_UNIFIER "";
   1.335 +            raise NO_UNIFIER ("Clash of types " ^ quote a ^ " and " ^ quote b, tye)
   1.336 +          else fold unif (Ts ~~ Us) tye_idx
   1.337 +      | (T, U) => if T = U then tye_idx else raise NO_UNIFIER ("", tye);
   1.338  
   1.339    in unif end;
   1.340  
   1.341 @@ -318,7 +326,7 @@
   1.342    ""];
   1.343  
   1.344  
   1.345 -(* infer *)                                     (*DESTRUCTIVE*)
   1.346 +(* infer *)
   1.347  
   1.348  fun infer pp tsig =
   1.349    let
   1.350 @@ -327,9 +335,9 @@
   1.351      fun unif_failed msg =
   1.352        "Type unification failed" ^ (if msg = "" then "" else ": " ^ msg) ^ "\n";
   1.353  
   1.354 -    fun prep_output bs ts Ts =
   1.355 +    fun prep_output tye bs ts Ts =
   1.356        let
   1.357 -        val (Ts_bTs', ts') = typs_terms_of Name.context ~1 (Ts @ map snd bs, ts);
   1.358 +        val (Ts_bTs', ts') = typs_terms_of tye Name.context ~1 (Ts @ map snd bs, ts);
   1.359          val (Ts', Ts'') = chop (length Ts) Ts_bTs';
   1.360          fun prep t =
   1.361            let val xs = rev (Term.variant_frees t (rev (map fst bs ~~ Ts'')))
   1.362 @@ -339,9 +347,9 @@
   1.363      fun err_loose i =
   1.364        raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], []);
   1.365  
   1.366 -    fun err_appl msg bs t T u U =
   1.367 +    fun err_appl msg tye bs t T u U =
   1.368        let
   1.369 -        val ([t', u'], [T', U']) = prep_output bs [t, u] [T, U];
   1.370 +        val ([t', u'], [T', U']) = prep_output tye bs [t, u] [T, U];
   1.371          val why =
   1.372            (case T' of
   1.373              Type ("fun", _) => "Incompatible operand type"
   1.374 @@ -349,9 +357,9 @@
   1.375          val text = unif_failed msg ^ cat_lines (appl_error pp why t' T' u' U');
   1.376        in raise TYPE (text, [T', U'], [t', u']) end;
   1.377  
   1.378 -    fun err_constraint msg bs t T U =
   1.379 +    fun err_constraint msg tye bs t T U =
   1.380        let
   1.381 -        val ([t'], [T', U']) = prep_output bs [t] [T, U];
   1.382 +        val ([t'], [T', U']) = prep_output tye bs [t] [T, U];
   1.383          val text = cat_lines
   1.384           [unif_failed msg,
   1.385            "Cannot meet type constraint:", "",
   1.386 @@ -367,23 +375,28 @@
   1.387  
   1.388      val unif = unify pp tsig;
   1.389  
   1.390 -    fun inf _ (PConst (_, T)) = T
   1.391 -      | inf _ (PFree (_, T)) = T
   1.392 -      | inf _ (PVar (_, T)) = T
   1.393 -      | inf bs (PBound i) = snd (nth bs i handle Subscript => err_loose i)
   1.394 -      | inf bs (PAbs (x, T, t)) = PType ("fun", [T, inf ((x, T) :: bs) t])
   1.395 -      | inf bs (PAppl (t, u)) =
   1.396 +    fun inf _ (PConst (_, T)) tye_idx = (T, tye_idx)
   1.397 +      | inf _ (PFree (_, T)) tye_idx = (T, tye_idx)
   1.398 +      | inf _ (PVar (_, T)) tye_idx = (T, tye_idx)
   1.399 +      | inf bs (PBound i) tye_idx =
   1.400 +          (snd (nth bs i handle Subscript => err_loose i), tye_idx)
   1.401 +      | inf bs (PAbs (x, T, t)) tye_idx =
   1.402 +          let val (U, tye_idx') = inf ((x, T) :: bs) t tye_idx
   1.403 +          in (PType ("fun", [T, U]), tye_idx') end
   1.404 +      | inf bs (PAppl (t, u)) tye_idx =
   1.405            let
   1.406 -            val T = inf bs t;
   1.407 -            val U = inf bs u;
   1.408 -            val V = mk_param [];
   1.409 +            val (T, tye_idx') = inf bs t tye_idx;
   1.410 +            val (U, (tye, idx)) = inf bs u tye_idx';
   1.411 +            val V = Param (idx, []);
   1.412              val U_to_V = PType ("fun", [U, V]);
   1.413 -            val _ = unif (U_to_V, T) handle NO_UNIFIER msg => err_appl msg bs t T u U;
   1.414 -          in V end
   1.415 -      | inf bs (Constraint (t, U)) =
   1.416 -          let val T = inf bs t in
   1.417 -            unif (T, U) handle NO_UNIFIER msg => err_constraint msg bs t T U;
   1.418 -            T
   1.419 +            val tye_idx'' = unif (U_to_V, T) (tye, idx + 1)
   1.420 +              handle NO_UNIFIER (msg, tye') => err_appl msg tye' bs t T u U;
   1.421 +          in (V, tye_idx'') end
   1.422 +      | inf bs (Constraint (t, U)) tye_idx =
   1.423 +          let val (T, tye_idx') = inf bs t tye_idx in
   1.424 +            (T,
   1.425 +             unif (T, U) tye_idx'
   1.426 +             handle NO_UNIFIER (msg, tye) => err_constraint msg tye bs t T U)
   1.427            end;
   1.428  
   1.429    in inf [] end;
   1.430 @@ -402,11 +415,12 @@
   1.431  
   1.432      (*convert to preterms*)
   1.433      val ts = burrow_types check_typs raw_ts;
   1.434 -    val (ts', _) =
   1.435 -      fold_map (preterm_of const_type is_param o constrain_vars) ts (Vartab.empty, Vartab.empty);
   1.436 +    val (ts', (_, _, idx)) =
   1.437 +      fold_map (preterm_of const_type is_param o constrain_vars) ts
   1.438 +      (Vartab.empty, Vartab.empty, 0);
   1.439  
   1.440      (*do type inference*)
   1.441 -    val _ = List.app (ignore o infer pp tsig) ts';
   1.442 -  in #2 (typs_terms_of used maxidx ([], ts')) end;
   1.443 +    val (tye, _) = fold (snd oo infer pp tsig) ts' (Inttab.empty, idx);
   1.444 +  in #2 (typs_terms_of tye used maxidx ([], ts')) end;
   1.445  
   1.446  end;