mtl-style-for-free

MTL style for free

Introduction

“MTL style” and “free monad style” are competing ways of implementing classes of effectful operations in Haskell programs. When we use MTL style we specify a type class of monadic operations and give various instances which interpret the operations in different monads. When we use free monad style we specify a Functor which encodes the monadic operations and we implement various interpretations of that functor in different monads.

After reading an excellent article by Matt Parsons (which advocated the MTL style) I realised that you can have the best of both worlds! The base functor that defines the free monad also gives rise to an MTL style type class.

Example

Inspired by Matthew’s article on the three layer cake pattern in Haskell, we might have the interfaces

class MonadTime m where getCurrentTime :: m UTCTime class MonadLock m where acquireLock :: NominalDiffTime -> Key -> m ( Maybe Lock ) m ( renewLock :: NominalDiffTime -> Lock -> m ( Maybe Lock ) m ( releaseLock :: Lock -> m () m ()

and give them interpretations which can be either effectful or pure mocked versions

acquireLockIO :: NominalDiffTime -> Key -> IO ( Maybe Lock ) renewLockIO :: NominalDiffTime -> Lock -> IO ( Maybe Lock ) releaseLockIO :: Lock -> IO () () instance MonadTime (( -> ) UTCTime ) where (( = id getCurrentTime instance MonadTime IO where = Data.Time.getCurrentTime getCurrentTimeData.Time.getCurrentTime instance MonadLock IO where = acquireLockIO acquireLockacquireLockIO = renewLockIO renewLockrenewLockIO = releaseLockIO releaseLockreleaseLockIO

MTL style for free

Instead of defining different classes for different sets of operations let’s define a class once and for all parameterized by a Functor which carries the operations.

class Monad_ f m where f m interpret :: forall r . f r -> m r f rm r

Then instead of MonadTime we have Monad_ Time_ and instead of MonadLock we have Monad_ Lock_ , where

data Time_ a = GetCurrentTime ( UTCTime -> a) a) data Lock_ a = AcquireLock ( Maybe Lock -> a) NominalDiffTime Key a) | RenewLock ( Maybe Lock -> a) NominalDiffTime Lock a) | ReleaseLock (() -> a) Lock (()a)

The way that we write a functor for a class of monadic operations is that we take our collection of operations of the form

class MonadOperations where operation1 :: arg1 -> ... -> argN -> m result arg1argNm result ...

and convert it to a sum type of the form

data Operations_ a = Operation1 (result -> a) arg1 ... argN (resulta) arg1argN | ...

We can go ahead and give instance definitions for our classes

instance Monad_ Time_ (( -> ) UTCTime ) where (( = \ case interpret GetCurrentTime f -> k0 id f k0 instance Monad_ Time_ IO where = \ case interpret GetCurrentTime f -> k0 Data.Time.getCurrentTime f k0 Data.Time.getCurrentTime f instance Monad_ Lock_ IO where = \ case interpret AcquireLock f t k -> k2 acquireLockIO f t k f t kk2 acquireLockIO f t k RenewLock f t l -> k2 renewLockIO f t l f t lk2 renewLockIO f t l ReleaseLock f l -> k1 releaseLockIO f l f lk1 releaseLockIO f l

For each constructor we give its interpretation in the target monad. k0 , k1 and k2 are somewhat uninteresting combinators for making this look cleaner.

= fmap f op k0 op ff op = fmap f (op a) k1 op f af (op a) = fmap f (op a b) k2 op f a bf (op a b)

The downside of this new style is that if we want to use direct function calls for our operations we have to wrap them:

-- All these types are inferrable -- with NoMonomorphismRestriction getCurrentTime :: Monad_ Time_ m => m UTCTime = j0 GetCurrentTime getCurrentTimej0 acquireLock :: Monad_ Lock_ m => NominalDiffTime -> Key -> m ( Maybe Lock ) m ( = j2 AcquireLock acquireLockj2 renewLock :: Monad_ Lock_ m => NominalDiffTime -> Lock -> m ( Maybe Lock ) m ( = j2 RenewLock renewLockj2 releaseLock :: Monad_ Lock_ m => Lock -> m () m () = j1 ReleaseLock releaseLockj1

j0 , j1 , and j2 are combinators for implementing operations conveniently

= interpret (op id ) j0 opinterpret (op = interpret (op id a) j1 op ainterpret (opa) = interpret (op id a b) j2 op a binterpret (opa b)

Or we could use j0 GetCurrentTime , j2 AcquireLock etc. directly, but that looks rather bizarre.

Interoperating with Free for free

The benefit we get is automatic interaction with free monads. The functor of operations that we defined is exactly the base functor of the corresponding free monad. There is a function freely which interprets Free f in the MTL style Monad_ f m

freely :: ( Functor f, Monad m, Monad_ f m) f,m,f m) => Free f a -> m a f am a = foldFree interpret f freely ffoldFree interpret f

The inverse to freely is liftF (which is also the interpret of the Monad_ f (Free f) instance).

instance Functor f => Monad_ f ( Free f) where f (f) = liftF interpretliftF

Performance

I haven’t measured the performance of this but with a suitable combination of inlining plus RULE s it feels like it should perform equally well to the MTL style.

This approach doesn’t provide support for non-algebraic operations such as Reader.local , Writer.listen and Writer.pass , but perhaps that’s a good thing.

Conclusion

Implemention of classes of operations in MTL style contains a common pattern parametrized by a functor. Using the parametrized style gives you automatic interchange between MTL style and free monad style so you can have the best of both worlds for free!

I’m probably not the first person to notice this. If anyone knows any prior references please let me know and I’ll add a reference.

Oliver Charles did some stuff along these lines.