[Haskell-cafe] Bool is not...safe?!
Olaf Klinke
olf at aatal-apotheke.de
Sat Jul 7 19:58:55 UTC 2018
>
>> true = return ()
>> (&&) = liftM2 const
>> (||) = mplus
>> false = mzero
>
> I don't think it's by definition yet, but surely by convention, that
> this is a restricted form of
>
> true = pure ()
> (&&) = (<*)
> (||) = (<|>)
> false = empty
>
> All of these functions require only an Applicative or an Alternative,
> and except for "true", they're all in the library.
True, and after sending I realized I should have used exaclty the definitions you gave. It's just that I don't know whether there exists the concept of "commutative applicative". It is equired for (&&) to be commutative. How could one even state it without the monad bind? The following definitions are from a category theory paper [1] by Anders Kock.
import Control.Monad
-- tensorial strengths
t'' :: (Functor m) => a -> m b -> m (a,b)
t'' a mb = fmap (\b -> (a,b)) mb
t' :: (Functor m) => m a -> b -> m (a,b)
t' ma b = fmap (\a -> (a,b)) ma
-- so-called linear extensions in the first and second argument
linearext1 :: Functor m => (m c -> c) -> (a -> b -> c) -> m a -> b -> c
linearext1 struct f ma b = struct $ (fmap $ uncurry f) $ t' ma b
linearext2 :: Functor m => (m c -> c) -> (a -> b -> c) -> a -> m b -> c
linearext2 struct f a mb = struct $ (fmap $ uncurry f) $ t'' a mb
The first argument to linearext1 and linearext2 is called a "structure map" in the context of [1]. Such structure maps exist when m is a monad. Indeed, we have
linearext1 join t'' :: (Monad m) => m a -> m b -> m (a, b)
linearext2 join t' :: (Monad m) => m a -> m b -> m (a, b)
Now m is called commutative if the above two maps are the same [*]. But join does not exist for applicative functors, it is interdefinable with (>>=).
Olaf
[1] Anders Kock: "Commutative Monads as a Theory of Distributions".
Theory and Applications of Categories, Vol. 26 No. 4 (2012).
[*] They are called Fubini maps because a special case is Fubini's theorem about integration by parts.
More information about the Haskell-Cafe
mailing list