- tv <- case (isMonomorphic `fmap` mb_ty) of
- Just True -> return (fromJust mb_ty)
- _ -> do
- tv_ <- liftM mkTyVarTy (newVar argTypeKind)
- when (isJust mb_ty) $
- instScheme (sigmaType$ fromJust mb_ty) >>= addConstraint tv_
- return tv_
- go tv (fromMaybe tv mb_ty) hval
+ tv <- liftM mkTyVarTy (newVar argTypeKind)
+ case mb_ty of
+ Nothing -> go tv tv hval
+ Just ty | isMonomorphic ty -> go ty ty hval
+ Just ty -> do
+ (ty',rev_subst) <- instScheme (sigmaType ty)
+ addConstraint tv ty'
+ term <- go tv tv hval
+ --restore original Tyvars
+ return$ flip foldTerm term idTermFold {
+ fTerm = \ty dc hval tt -> Term (substTy rev_subst ty) dc hval tt,
+ fSuspension = \ct mb_ty hval n ->
+ Suspension ct (substTy rev_subst `fmap` mb_ty) hval n}