[Haskell-beginners] Overriding >>= for a Monad?

Alexander Dunlap alexander.dunlap at gmail.com
Thu Feb 26 21:24:15 EST 2009


On Thu, Feb 26, 2009 at 5:46 PM, Patrick LeBoutillier
<patrick.leboutillier at gmail.com> 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


More information about the Beginners mailing list