Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variables & the reader monad

Tags:

haskell

I have a module where a global environment (defining certain constraints such as neighbor IP addresses, etc.) is created and initialized by calling an initializing function. A number of subsequent functions should use these constraints when they are called.

Whilst in principle I understand what the reader monad does I am not quite sure how I can apply this to my problem, esp.

  • How it can be used to initialize an environment that is defined by the user and passed as data/arguments to the initializing function. I mean, the reader monad has to get the actual values that make up the global immutable environment from somewhere. I would like that the values are read from an initializing function call like myinitial :: arg1 -> arg1 -> IOString where subsequently arg1 and arg2 become global immutable data accessible to subsequent functions via the reader monad(?)

  • How I can use these environment values as function arguments e.g. recvFrom s arg1 where arg1 is global immutable data from my environment. Or if arg2 > arg1 then ... else ...

I could make a configuration file of course, but I feel that a configuration file will take away to much flexibility.

[Edit] I understand about ask, but shouldn't there be additional "pointfree-like" ways so that the global/environment immutable can be omitted if the function signature has been defined right? How would I i.e. need to refactor my if-then-else to apply this.

like image 760
J Fritsch Avatar asked Jun 12 '12 23:06

J Fritsch


2 Answers

Most of your questions can be answered by inspecting the types and documentation for the ask and runReader functions.

First, the ask:

ask :: Reader m r => m r

This returns the underlying read-only data wrapped in the monad. Cool, so that's how you will get to the state when you want to use it with other functions, in your example above:

do x <- ask
   recvFrom s x

(depending on the type of recvFrom, of course)

Next is the runReader, this is how you give it that initial data you were talking about. It basically just runs the Reader computation using the data it's given:

runReader :: Reader r a -> r -> a

which means: run the computation (the first argument) with the read-only data of type r (the second argument). It finally will return the result type of the 1st argument, a. In your case, this may look like:

result = runReader computationUsingArg1Arg2 (arg1, arg2)

Then inside computationUsingArg1Arg2 you can read arg1 and arg2 via ask.

like image 167
ScottWest Avatar answered Oct 04 '22 00:10

ScottWest


Here's an example that may clear things up. First you need to import the Reader module:

import Control.Monad.Reader

Now let's define some data structure (that we're going to use to hold a name and an age)

data Config = Config { name :: String, age :: Int }

Now define a function that works in the Reader monad (it's type is Reader Config (String, Int) but we don't need to specify that - it can be inferred). All this function does is ask for the environment (of type Config) and then extracts the fields and does something with them.

example = do
    c <- ask
    return ("Hello " ++ name c, 2 * age c)

Now we put it all together into a program. The first four lines after the do block allow the user to enter their name and age. Then we build a Config structure using the user's inputs (we have to use read to convert the variable _age, which is a String, into an Int so that we can feed it to the Config constructor) and execute example with this environment, using the runReader function. Finally, we use the result of this computation to generate some output.

main = do
    putStrLn "Enter your name:"
    _name <- getLine
    putStrLn "Enter your age:"
    _age <- getLine
    let config = Config _name (read _age)
    let result = runReader example config
    putStrLn $ fst result
    putStrLn $ "Twice your age is: " ++ show (snd result)
like image 35
Chris Taylor Avatar answered Oct 04 '22 00:10

Chris Taylor