[Haskell-cafe] Applicative functors with branch/choice ?
Twan van Laarhoven
twanvl at gmail.com
Thu Jul 26 13:14:22 CEST 2012
On 26/07/12 12:40, Евгений Пермяков wrote:
> class Applicative f => Actuative f where
> -- | select computation conditionally . Side effects of only one two
> alternative take place
> select :: f (Either a b) -- ^ selector
> -> f (a -> c) -- ^ first alternative
> -> f (b -> c) -- ^ second alternative
> -> f c
Can't you already define this function in terms of Applicative itself? I.e.
select xs fs gs = sel <$> xs <*> fs <*> gs
where
sel (Left a) f _ = f a
sel (Right b) _ g = g b
I assume that your intent is that `select` behaves differently from the one I
defined here. But you need to specify in what way.
Suppose it should work like if-then-else. Then you would perhaps have these laws:
select (Left <$> x) f g = f <$> x
select (fmap swapEither x) f g = select x g f
I think this is a useful class to have, and I would support adding something
like it to the standard library. Perhaps the arguments should be swapped to the
same order as either, to give
class Functor f => Selective f where
eitherF :: f (a -> c) -> f (b -> c) -> f (Either a b) -> f c
The laws would then be:
eitherF f g . fmap swapEither = eitherF g f
eitherF f g . fmap Left = f
eitherF f g . fmap Right = g -- follows from the other two laws
every Monad is an instance via
defaultEitherF ls rs xs = either ls rs =<< xs
Twan
More information about the Haskell-Cafe
mailing list