[Haskell-cafe] MonadLib usage

Iavor Diatchki iavor.diatchki at gmail.com
Sun Jul 18 14:10:12 EDT 2010


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))

This is why GHC's limitation of deriving only the last argument of a
class has not been too much of a problem for me.   On a more general
note, I don't think that there is anything particularly difficult
about lifting the "deriving only the last argument" restriction,
except for picking a reasonable notation and finding a willing
contributor to hack it up.  (Generalized generalized deriving anyone
:-) ?)  If you are interested in some of the issues with generalized
newtype deriving in general, this thread has some examples:
http://osdir.com/ml/[email protected]/2010-03/msg00388.html

Hope that this helps,
-Iavor

On Sun, Jul 18, 2010 at 9:59 AM, Emil Melnikov <emilmeln at gmail.com> wrote:
> On 2010, July 18, 23:27
> Ivan Lazar Miljenovic <ivan.miljenovic at gmail.com> wrote:
>
>> When discussing a similar issue with Manuel Chakravarty, he convinced me
>> that cunning newtype deriving is actually rather bad in practice and
>> shouldn't be used as there's a lack of proofs or some such (I can't
>> remember the arguments, but I remember being convinced by them :p).
>
> Hmm...  I can't imagine how it is possible, since new and
> original types are isomorphic.
>
> Can you give me some pointers to this discussion (links or
> keywords)?
>
> --
> Emil.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list