[Haskell-cafe] Applicative functors with branch/choice ?

Twan van Laarhoven twanvl at gmail.com
Thu Jul 26 14:12:41 CEST 2012


On 26/07/12 13:58, Евгений Пермяков wrote:
> As you can see, if I use select definition with Control.Applicative.<*>, I'll
> execute both l and r and the only choice will be, what result to drop. Both l
> and r, however, will be executed, and their side effects will take place. With
> select from my code only one action will be executed, depending on result of i,
> and only effects of one of actions (either l or r) will take place.

I realize that, and that is why I insisted on laws to formalize this.

Your instance for IO is a special case of a function that works for any Monad:

     defaultEitherF :: (Functor f, Monad f)
                    => f (a -> c) -> f (b -> c) -> f (Either a b) -> f c
     defaultEitherF ml mr mx = either (ml <$$>) (mr <$$>) =<< mx
       where
         (<$$>) :: Functor f => f (a -> b) -> a -> f b
         f <$$> x = ($ x) <$> f

(the version of this function in my previous post was not correct)

>
> I'm not sure, what categorical concept will correspond to this typeclass.
>

Well, this type class kind of corresponds to the functionality of ArrowChoice. I 
believe that class corresponds to a (symmetric) monoidal structure on the dual 
category. Plus a whole bunch of junk you get from its super classes.



Twan



More information about the Haskell-Cafe mailing list