--- Use CoreSubst to subst a type var in a type
-substitute find (Type repl_ty) (Type ty) = do
- let subst = CoreSubst.extendTvSubst CoreSubst.emptySubst find repl_ty
- let ty' = CoreSubst.substTy subst ty
- return (Type ty')
--- Use CoreSubst to subst a type var in the type annotation of a case
-substitute find repl@(Type repl_ty) (Case scrut bndr ty alts) = do
- let subst = CoreSubst.extendTvSubst CoreSubst.emptySubst find repl_ty
- let ty' = CoreSubst.substTy subst ty
- -- And continue with substituting on all subexpressions of the case
- subeverywhere (substitute find repl) (Case scrut bndr ty' alts)
+-- Use CoreSubst to subst a type var in an expression
+substitute find repl expr = do
+ let subst = CoreSubst.extendSubst CoreSubst.emptySubst find repl
+ return $ CoreSubst.substExpr subst expr
+
+-- | Creates a transformation that substitutes the given binder with the given
+-- expression. This does only work for value expressions! All binders in the
+-- expression are cloned before the replacement, to guarantee uniqueness.
+substitute_clone :: CoreBndr -> CoreExpr -> Transform