Generalize MonadIO to MonadBase

John Lato jwlato at gmail.com
Wed Apr 21 04:58:26 EDT 2010


This technique of newtype'ing stacks to make an abstraction barrier,
in conjuction with liftBase, seems useful to me.  I can think of at
least two (related) projects where I should use it.

John

On Wed, Apr 21, 2010 at 12:21 AM, Iavor Diatchki
<iavor.diatchki at gmail.com> wrote:
> Hi,
> A common way to get different "base" monads is to "newtype" existing
> monads.  Here is an example:
>
>> newtype CustomMonad a = CM (StateT Int IO a) deriving (Monad)
>>
>> instance BaseM CustomMonad CustomMonad where inBase = id
>>
>> ... define some custom operations on CustomMonad...
>
> The "newtype" provides an "abstraction barrier", which hides the
> implementation of the CustomMonad.   If you add more layers on top of
> "CustomMonad" when you use "inBase" it will only lift up to
> CustomMonad.   This is useful if, for example, CustomMonad needs to
> perform some IO actions, but for one reason or another it does not
> allow for the execution of arbitrary IO computations.
>
> Note that when you are implementing the operations of CustomMonad, you
> can use "inBase" again, this time to lift all the way down to IO.
> Basically, by using "newtypes", you can kind of "punctuate" a tall
> tower of monads, an use "inBase" to jump from one stack to the next.
>
> Hope that this helps,
> -Iavor
>
>
>
> On Tue, Apr 20, 2010 at 2:45 PM, Evan Laforge <qdunkan at gmail.com> wrote:
>> I haven't really been following this discussion, but I use
>> Identity-backed monads all the time.  Usually some combination of
>> StateT, WriterT, ErrorT and ReaderT.
>>
>> Are there any "base" monads other than IO and Identity?
>> _______________________________________________
>> Libraries mailing list
>> Libraries at haskell.org
>> http://www.haskell.org/mailman/listinfo/libraries
>>
>


More information about the Libraries mailing list