How do I use list monad inside of ReaderT?

Daniel K

How do I use Reader/ReaderT to ask for a list type, e.g. [(Int, Int)] and then perform calculations inside the list monad (of the type that was asked for)?

My broken code follows, shortened for clarity:

attempt :: Int -> Int -> ReaderT [(Int,Int)] [] [[Int]]
attempt start end =
  do  (s0, e0) <- ask
      return [0]

To give you an idea of what I'm trying to do, here is the same function, using the list monad but not Reader:

paths :: [(Int, Int)] -> Int -> Int -> [[Int]]
paths edges start end =
  if start == end
    then return [end]
    else do   (s0, e0) <- edges
              guard $ s0 == start
              subpath <- paths edges e0 end
              return $ s0 : subpath

I'm using ReaderT because I'm learning monad transformers. It's part of a larger problem using both Reader and Writer and list monad to implement paths.

David Miani

The trick here is to use lift to convert a list monad (ie an [a]) into a ReaderT env [] by using lift:

lift :: (Monad m, MonadTrans t) => m a -> t m a

or specialized to your monad stack:

lift :: [a] -> ReaderT [(Int,Int)] [] a

ask returns the state (ie [(Int, Int)]) wrapped in the ReaderT monad eg:

ask :: ReaderT [(Int, Int)] [] [(Int, Int)]

We want to convert that into another value in the same monad but with the type:

??? :: ReaderT [(Int, Int)] [] (Int, Int)

So the alternatives are tracked by the monad instead of in the output. Consider the basic function >>=:

(>>=) :: Monad m => m a -> (a -> m b) -> m b

You should be able to see we have all the pieces required. Using ask >>= lift:

  1. The first argument is ReaderT [(Int, Int)] [] [(Int, Int)], meaning a is [(Int, Int)], and m is ReaderT [(Int, Int)] []
  2. We want the result m b to be ReaderT [(Int, Int)] [] (Int, Int), so b is (Int, Int)
  3. So the function needs the type [(Int, Int)] -> ReaderT [(Int, Int)] [] (Int, Int). If you replace the a in the lift function with (Int, Int), it is a perfect match, meaning the expression ask >>= lift does what you want.

The other mistake you had was the output type of the ReaderT monad - as it contained a list monad you didn't need to wrap the result in another pair of brackets. A ReaderT state [] already contains the concept of multiple results, and a single result in this case is a [Int] showing the graph path.

Here is the working code:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Main where
import Control.Monad.Reader
import Control.Applicative


paths :: Int -> Int -> ReaderT [(Int,Int)] [] [Int]
paths start end = do
  if start == end
     then return [end]
     else do
      (s0, e0) <- ask >>= lift
      guard $ s0 == start
      (s0 :) <$> paths e0 end


input :: [(Int, Int)]
input = [(1,2), (2,7), (3,4), (7, 3), (7, 5), (5, 3)]

test :: [[Int]]
test = runReaderT (paths 2 4) input


> test
[[2,7,3,4],[2,7,5,3,4]]

I hope that explains it clearly. In this situation, I would probably just stick with the original solution (using Reader by itself is normally not very useful), but it is good to know how to understand and manipulate the types of monads and monad transformers.

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 do I use list monad inside of ReaderT?

From Dev

How to use maybe monad inside another monad?

From Dev

How do I perform IO inside of Parsec's monad?

From Dev

How do I perform IO inside of Parsec's monad?

From Dev

How do I use Bootstrap columns inside a list item?

From Dev

How do I use a CheckBoxFor inside a foreach when working with a list?

From Dev

Use list monad inside monad transformer type classes?

From Dev

How to get ReaderT to work with another monad transformer?

From Dev

How to get ReaderT to work with another monad transformer?

From Dev

Scala: How do I compose a list of operations into a free monad?

From Dev

How do I use a persistent State monad with Spock?

From Dev

How to build a Haskell list inside a monad lazily?

From Dev

How do I modify a list inside a thread?

From Dev

How do I use await inside a generator?

From Dev

How do I use colorama inside a tuple?

From Dev

How do I add the Reader monad to Scotty's monad?

From Dev

How do I "continue" in a `Monad` loop?

From Dev

How do I use the Supply monad to create a function that generates globally unique names?

From Dev

how do i create a two list from a list inside of an array?

From Dev

Use List as monad in Scala

From Dev

How do I use this Linked List implementation?

From Dev

How do I use a Maybe (List a) in a map?

From Dev

How can i use dynamically generated string inside `list() = $variable`?

From Dev

How do I list the files inside a python wheel?

From Java

How do I know a particular button was clicked inside a List

From Dev

How do I access an array list inside of a class constructor in java?

From Dev

How do I get a list of all functions inside a controller in cakephp

From Dev

How do I compare items inside a List with a substring?

From Dev

How do I retrieve a Value from a KeyValuePair inside a List?

Related Related

  1. 1

    How do I use list monad inside of ReaderT?

  2. 2

    How to use maybe monad inside another monad?

  3. 3

    How do I perform IO inside of Parsec's monad?

  4. 4

    How do I perform IO inside of Parsec's monad?

  5. 5

    How do I use Bootstrap columns inside a list item?

  6. 6

    How do I use a CheckBoxFor inside a foreach when working with a list?

  7. 7

    Use list monad inside monad transformer type classes?

  8. 8

    How to get ReaderT to work with another monad transformer?

  9. 9

    How to get ReaderT to work with another monad transformer?

  10. 10

    Scala: How do I compose a list of operations into a free monad?

  11. 11

    How do I use a persistent State monad with Spock?

  12. 12

    How to build a Haskell list inside a monad lazily?

  13. 13

    How do I modify a list inside a thread?

  14. 14

    How do I use await inside a generator?

  15. 15

    How do I use colorama inside a tuple?

  16. 16

    How do I add the Reader monad to Scotty's monad?

  17. 17

    How do I "continue" in a `Monad` loop?

  18. 18

    How do I use the Supply monad to create a function that generates globally unique names?

  19. 19

    how do i create a two list from a list inside of an array?

  20. 20

    Use List as monad in Scala

  21. 21

    How do I use this Linked List implementation?

  22. 22

    How do I use a Maybe (List a) in a map?

  23. 23

    How can i use dynamically generated string inside `list() = $variable`?

  24. 24

    How do I list the files inside a python wheel?

  25. 25

    How do I know a particular button was clicked inside a List

  26. 26

    How do I access an array list inside of a class constructor in java?

  27. 27

    How do I get a list of all functions inside a controller in cakephp

  28. 28

    How do I compare items inside a List with a substring?

  29. 29

    How do I retrieve a Value from a KeyValuePair inside a List?

HotTag

Archive