[Haskell-cafe] Strong version of 'local' for reader monad

Sebastiaan Visser haskell at fvisser.nl
Mon Dec 13 23:14:50 CET 2010

Hey Brent,

Of course, because your type signature has no valid implementation, the expected semantics are hard to guess. Why should strongLocal bother about the MonadReader constraint on m1 anyway? Are you internally combining it with 'ask' or another 'local'?

The monads are potentially different, so m1 has to be run inside m2.

A more specific version of strongLocal could be:

 strongLocalReader :: MonadReader r2 m2 => (r2 -> r1) -> Reader r1 a -> m2 a
 strongLocalReader f m = 
   do r <- asks f
      return (runReader m r)

Or you can generalize it to every 'Runnable' monad:

 class Monad m => Runnable m where
   run :: m a -> a

   :: (MonadReader r2 m2, Runnable m1)
   => (r2 -> r1) -> (r1 -> m1 a) -> m2 a
 strongLocalRunnable f m =
   do r <- asks f
      return (run (m r))

Is this what you want to express?


On Dec 13, 2010, at 9:30 PM, Brent Yorgey wrote:
> Hi all,
> Today I wanted this function
>  strongLocal :: (MonadReader r1 m1, MonadReader r2 m2) => 
>                 (r2 -> r1) -> m1 a -> m2 a
> Of course, after staring at this type for ten seconds I realized that
> it cannot be implemented.  But I wondered whether anyone has any
> pointers to anything related, or if anyone has good ideas for a
> reasonable interface that would allow this.
> -Brent

More information about the Haskell-Cafe mailing list