Overriding >>= for a Monad?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Overriding >>= for a Monad?

Patrick LeBoutillier
Hi all,

I'm using Control.Monad.StateT as such:

 data TapState = TapState {
   planSet :: Bool,
   noPlan :: Bool,
   skipAll :: Bool,
   testDied :: Bool,
   expectedTests :: Int,
   executedTests :: Int,
   failedTests :: Int
 } deriving (Show)

 type TAP a = StateT TapState IO a

but I'd like to provide my own >>= function. Is there a way to
"derive" a new type from StateT in order to implement my own >>=?
Is thin done using "instance"?

Thanks,

Patrick

--
=====================
Patrick LeBoutillier
Rosem?re, Qu?bec, Canada
Reply | Threaded
Open this post in threaded view
|

Overriding >>= for a Monad?

Alexander Dunlap
On Thu, Feb 26, 2009 at 5:46 PM, Patrick LeBoutillier
<[hidden email]> wrote:

> Hi all,
>
> I'm using Control.Monad.StateT as such:
>
> ?data TapState = TapState {
> ? planSet :: Bool,
> ? noPlan :: Bool,
> ? skipAll :: Bool,
> ? testDied :: Bool,
> ? expectedTests :: Int,
> ? executedTests :: Int,
> ? failedTests :: Int
> ?} deriving (Show)
>
> ?type TAP a = StateT TapState IO a
>
> but I'd like to provide my own >>= function. Is there a way to
> "derive" a new type from StateT in order to implement my own >>=?
> Is thin done using "instance"?
>
> Thanks,
>
> Patrick
>
> --
> =====================
> Patrick LeBoutillier
> Rosem?re, Qu?bec, Canada

The most direct way would probably to copy StateT's source code and
tinker with the >>= definition (also change the name to MyStateT or
whatever).

You could also define

> newtype MyStateT s a = MyStateT (StateT s IO a)
>
> instance Monad (MyStateT s) where
>   return = MyStateT . return
>   (>>=) = ...

Alex