ekmett/bound

Add predicate to substitute functions

Opened this issue · 2 comments

substitute :: Monad f => Eq a => a -> f a -> f a -> f a
substitute = substituteOf . (==)

substituteBy :: Monad m => (a -> Bool) -> m a -> m a -> m a
substituteBy old new as = do
  a <- as
  if old a
    then new
    else pure a
substituteVar :: Functor f => Eq a => a -> a -> f a -> f a
substituteVar = substituteVarOf . (==)

substituteVarBy :: Functor f => (a -> Bool) -> a -> f a -> f a
substituteVarBy old new as = do
  a <- as
  pure
    if old a
      then new
      else a

Is there a use for this?

phadej commented

Isn't substitute a composition of abstract and instantiate?

--

EDIT: or alternatively is there much gained over just asking users to write as >>= \a -> ... as a general way to substitute stuff. I haven't found substituting a single variable to be a common theme (compared to performing simultaneous subsitutions) when using bound. abstract1 is common, to build up lambda like terms, but these are instantiated much later.