[Haskell-cafe] MonadLib usage

Emil Melnikov emilmeln at gmail.com
Sun Jul 18 15:21:28 EDT 2010


On 2010, July 18, 11:10
Iavor Diatchki <iavor.diatchki at gmail.com> wrote:

> Hi,
> When using monadLib, I use newtype deriving to get the Functor,
> Applicative, and Monad instances for my custom newtyped monad.  Those
> work just fine, and there is nothing unsafe about them.
> 
> For a custom monad, I usually don't derive MonadLib's effect classes
> directly.  Instead, I tend to define new operations specific to the
> monad.  This has the benefit of abstracting away internal
> implementation details, which makes it easier to change or extend the
> monad later.  For example, to implement a monad which provides a
> source of unique identifiers, I might use a state transformer:
> 
> newtype MyMonad a = MyMonad (StateT Int ...)
> 
> Now, instead of deriving a "StateM" instance, I would define a custom
> operation for obtaining new names, something like this:
> 
> newName :: MyMonad Name
> newName = MyMonad (do x <- get; set (x + 1); return (mkName x))
> 

Hello, and thanks for such quick answer.

Your rationale showed me that deriving all possible instances is
a wrong way, because it breaks abstraction.  Somehow I didn't saw
it.

Moreover, now I suppose that "don't derive unnecessary instances"
statement should be highlighted in Haskell wiki page about
type classes/monads.

--
Emil.


More information about the Haskell-Cafe mailing list