@@ -155,7 +155,7 @@ data Err' t
155 | WithFnType t
155 | WithFnType t
156 | NoTypeDecl Name
156 | NoTypeDecl Name
157 | NotInjective t t t
157 | NotInjective t t t
158− | CantResolve Bool -- ^ True if postponed, False if fatal
158+ | CantResolve Bool -- True if postponed, False if fatal
160 | CantResolveAlts [Name]
160 | CantResolveAlts [Name]
161 | IncompleteTerm t
161 | IncompleteTerm t
@@ -936,6 +936,7 @@ vinstances i t = 0
936-- | Replace the outermost (index 0) de Bruijn variable with the given term
936-- | Replace the outermost (index 0) de Bruijn variable with the given term
937instantiate :: TT n -> TT n -> TT n
937instantiate :: TT n -> TT n -> TT n
938instantiate e = subst 0 where
938instantiate e = subst 0 where
939+ subst i (P nt x ty) = P nt x (subst i ty)
939 subst i (V x) | i == x = e
940 subst i (V x) | i == x = e
940 subst i (Bind x b sc) = Bind x (fmap (subst i) b) (subst (i+1) sc)
941 subst i (Bind x b sc) = Bind x (fmap (subst i) b) (subst (i+1) sc)
941 subst i (App f a) = App (subst i f) (subst i a)
942 subst i (App f a) = App (subst i f) (subst i a)
@@ -947,6 +948,7 @@ instantiate e = subst 0 where
947-- that has been substituted.
948-- that has been substituted.
948substV :: TT n -> TT n -> TT n
949substV :: TT n -> TT n -> TT n
949substV x tm = dropV 0 (instantiate x tm) where
950substV x tm = dropV 0 (instantiate x tm) where
951+ subst i (P nt x ty) = P nt x (subst i ty)
950 dropV i (V x) | x > i = V (x - 1)
952 dropV i (V x) | x > i = V (x - 1)
951 | otherwise = V x
953 | otherwise = V x
952 dropV i (Bind x b sc) = Bind x (fmap (dropV i) b) (dropV (i+1) sc)
954 dropV i (Bind x b sc) = Bind x (fmap (dropV i) b) (dropV (i+1) sc)
@@ -1040,6 +1042,9 @@ subst n v tm = fst $ subst' 0 tm
1040 -- substitution happens...)
1042 -- substitution happens...)
1041 subst' i (V x) | i == x = (v, True)
1043 subst' i (V x) | i == x = (v, True)
1042 subst' i (P _ x _) | n == x = (v, True)
1044 subst' i (P _ x _) | n == x = (v, True)
1045+ subst' i t@(P nt x ty)
1046+ = let (ty', ut) = subst' i ty in
1047+ if ut then (P nt x ty', True) else (t, False)
1043 subst' i t@(Bind x b sc) | x /= n
1048 subst' i t@(Bind x b sc) | x /= n
1044 = let (b', ub) = substB' i b
1049 = let (b', ub) = substB' i b
1045 (sc', usc) = subst' (i+1) sc in
1050 (sc', usc) = subst' (i+1) sc in