[Haskell-cafe] Improving MTL instances (was: Overlapping/Incoherent instances)

Stephen Hicks sdh33 at cornell.edu
Mon Oct 13 18:02:27 EDT 2008

On Mon, Oct 13, 2008 at 3:29 AM, Ryan Ingram <ryani.spam at gmail.com> wrote:
> On Mon, Oct 13, 2008 at 2:04 AM, J. Garrett Morris
> <jgmorris at cecs.pdx.edu> wrote:
>> Indeed - MTL seems to have been rewritten at some point in the past to
>> prefer exhaustive enumeration to overlap.
> Indeed, and I actually think this is a weakness of the current
> implementation.  Anyone who comes up with a new transformer that
> provides different functionality than what is there needs to
> explicitly provide all the relevant instances, instead of letting
> MonadTrans do its thing.

(First of all, sorry for the double reply.)  I'm certainly way out of
my depth here, but would something like associated classes help here?

I'm imagining something like this (I'm sure my syntax is all wrong, though):

> class TypedMonad m where
>   class MonadType m

> instance (MonadTrans m, TypedMonad n) => (MonadType n) (m n)

So then you could write something like

> instance Monad m => TypedMonad (ReaderT i m) where
>   class MonadType (ReaderT i m) = MonadReader

and likewise for Reader, Writer(T), State(T), IO, etc...  Then, for instance

> instance MonadWriter (StateT s (ReaderT r (WriterT w IO)))

is fully-automatic...  Or wouldn't this work, at least once associated
classes is implemented?


More information about the Haskell-Cafe mailing list