transformers

David Menendez dave at zednenem.com
Thu Jan 27 00:10:00 CET 2011


On Wed, Jan 26, 2011 at 5:14 PM, Edward Kmett <ekmett at gmail.com> wrote:
> On Wed, Jan 26, 2011 at 5:00 PM, David Menendez <dave at zednenem.com> wrote:
>>
>> On Wed, Jan 26, 2011 at 3:56 PM, Edward Kmett <ekmett at gmail.com> wrote:
>> > Fair enough.
>> > These are the observations I have about 'transformers'.
>>
>> > 2.) The instance for Applicative for MaybeT doesn't really follow the
>> > other
>> > instances for Applicative in transformers.
>> > instance Applicative m => Applicative (MaybeT m) where
>> >    pure = MaybeT . pure . Just
>> >    f <*> a = MaybeT $ (<*>) <$> runMaybeT f <*> runMaybeT a
>> > would instead follow the convention that Applicatives depend only on
>> > Applicatives where possible, as opposed to the current default instance.
>> > This convention is followed everywhere in transformers except this
>> > module.
>>
>> Then you no longer have (<*>) = ap.
>>
>> This convention is also not followed for the StateT and ErrorT
>> transformers for the same reason.
>>
>> If you want something like MaybeT f whose Applicative instance depends
>> only on the Applicative instance of f, try Compose f Maybe.
>
>
> By that reasoning the existing Applicative instances for WriterT and ListT
> would be wrong in mtl as well.
> StateT actually requires a monad, because the s -> m (a, s) monad is given
> rise to by sandwiching a monad in an adjunction, which isn't sufficient to
>  yield an Applicative instance, but the ErrorT and MaybeT can both perfectly
> well get by on the weaker Applicative requirement, because they simply
> compose.
> The consistency of ap and (<*>) is assumed universally. Applicative merely
> allows you to optimize this case.

In the case of MaybeT, it isn't an optimization. It's a different
function. Using your proposed definition, we get:

*Main> flip runState 0 . runMaybeT $ mzero <*> lift (put 1)
(Nothing,1)
*Main> flip runState 0 . runMaybeT $ mzero `ap` lift (put 1)
(Nothing,0)


-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>



More information about the Libraries mailing list