How to get ReaderT to work with another monad transformer?

Sean Clark Hess

I would like to embed ReaderT into another monad transformer. How do I do this? The example below uses Scotty but I think it would be the same with any other monad.

{-# LANGUAGE OverloadedStrings #-}

import qualified Web.Scotty
import Web.Scotty.Trans

import Data.Text.Lazy
import Control.Monad.IO.Class (liftIO)

import Control.Monad.Trans.Reader
import Control.Monad.Trans

data Config = Config Text

main :: IO ()
main = do
    let config = Config "Hello World"
    -- how to I make this line work?
    scottyT 3000 id id routes

routes :: ScottyT Text (ReaderT Config IO) ()
routes = do
    get "/" info

info :: ActionT Text (ReaderT Config IO) ()
info = do
    -- this part seems like it works!
    Config message <- lift ask
    text $ "Info: " `append` message

This errors on the line scottyT 3000 id id routes, because scottyT expects a ScottyT Text IO (). How do I make this work? Here are the current errors:

Server.hs: line 24, column 24:
  Couldn't match type `ReaderT Config IO' with `IO'
    Expected type: ScottyT Text IO ()
      Actual type: ScottyT Text (ReaderT Config IO) ()
bheklilr

You have to change the arguments you've supplied as id to be ones that have the type forall a. m a -> n a and m Response -> IO Response respectively. Why? I don't know, but the example I found here shows someone running it similar to

main = do
    let config = Config "Hello, world"
        runner = flip runReaderT config
    scottyT 3000 runner runner routes

I've tested it, and it at least works. Whether or not this is best practices is unknown to me. If someone has a better method, feel free to post it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get ReaderT to work with another monad transformer?

From Dev

How to get Reader and ReaderT to work together

From Java

How is Kleisli a monad Transformer?

From Dev

How do I use list monad inside of ReaderT?

From Dev

How do I use list monad inside of ReaderT?

From Dev

How to implement the `List` monad transformer in Scala?

From Dev

How to refactor program that uses state monad transformer?

From Dev

How to implement the `List` monad transformer in Scala?

From Dev

How to kickstart monad transformer stack from main?

From Dev

How to use maybe monad inside another monad?

From Dev

How to use a Monad Transformer when Disjunction is the outermost container?

From Dev

How to define local method on top of a monad transformer stack in Scala cats

From Dev

How do I add MonadLogger to my Free monad transformer stack?

From Java

Is there a valid array monad transformer?

From Dev

Stacking ResourceT monad transformer

From Dev

WriterT monad transformer

From Dev

Monad Transformer stacks in Scala

From Dev

Can't get setParameter to work in XSLT transformer

From Dev

Unwrapping the STT monad in a transformer stack?

From Dev

Monad Transformer stacks with MaybeT and RandT

From Dev

Working with monad transformer RWST in Haskell

From Dev

How to work with mutable structures in the IO monad

From Dev

How does this weird monad bind work?

From Dev

Deriving a base monad using a monad transformer and the identity monad

From Dev

Servant always give me a initial value in ReaderT Monad

From Dev

How do I write a function which behaves differently depending on which monad is at the base of the transformer stack

From Dev

Cannot get (->) r monad to work with SDL2 rendering

From Dev

Use list monad inside monad transformer type classes?

From Dev

Tidying up Monads - turning application of a monad transformer into newtype monad

Related Related

  1. 1

    How to get ReaderT to work with another monad transformer?

  2. 2

    How to get Reader and ReaderT to work together

  3. 3

    How is Kleisli a monad Transformer?

  4. 4

    How do I use list monad inside of ReaderT?

  5. 5

    How do I use list monad inside of ReaderT?

  6. 6

    How to implement the `List` monad transformer in Scala?

  7. 7

    How to refactor program that uses state monad transformer?

  8. 8

    How to implement the `List` monad transformer in Scala?

  9. 9

    How to kickstart monad transformer stack from main?

  10. 10

    How to use maybe monad inside another monad?

  11. 11

    How to use a Monad Transformer when Disjunction is the outermost container?

  12. 12

    How to define local method on top of a monad transformer stack in Scala cats

  13. 13

    How do I add MonadLogger to my Free monad transformer stack?

  14. 14

    Is there a valid array monad transformer?

  15. 15

    Stacking ResourceT monad transformer

  16. 16

    WriterT monad transformer

  17. 17

    Monad Transformer stacks in Scala

  18. 18

    Can't get setParameter to work in XSLT transformer

  19. 19

    Unwrapping the STT monad in a transformer stack?

  20. 20

    Monad Transformer stacks with MaybeT and RandT

  21. 21

    Working with monad transformer RWST in Haskell

  22. 22

    How to work with mutable structures in the IO monad

  23. 23

    How does this weird monad bind work?

  24. 24

    Deriving a base monad using a monad transformer and the identity monad

  25. 25

    Servant always give me a initial value in ReaderT Monad

  26. 26

    How do I write a function which behaves differently depending on which monad is at the base of the transformer stack

  27. 27

    Cannot get (->) r monad to work with SDL2 rendering

  28. 28

    Use list monad inside monad transformer type classes?

  29. 29

    Tidying up Monads - turning application of a monad transformer into newtype monad

HotTag

Archive