This is an embarrassingly basic question, but until recently I have avoided properly understanding IO in Haskell and now I need to.

I am writing (with someone else) a program that takes as part of its input a library of background data. So as part of main I'll have a couple of lines such as

lib <- library.txt
let library = interpret lib

That is, it reads the library from a text file and then parses it to create a library with the right structure needed for the program. In particular, the type of "interpret" will be

interpret :: String -> Library

My question now is this. Is there some way for the program to treat the library as a constant? The program will contain functions whose behaviour depends on what's in the library, so it seems as though one would have to give such a function a type such as

foo :: Library -> Type1 -> Type2

and always include library as one of the arguments of foo. But that is a bit tedious when foo will only ever be applied to library and not some other library of type Library. I'd like to be able to treat library as I would a constant such as 2 (the analogy being that the function that takes x to x+2, say, has type Int -> Int rather than Int -> Int -> Int). Will it work if instead I give foo the type Type1 -> Type2 and then allow the definition of foo x to mention library? If not, is there some other way to achieve the same thing?

1 Answer 1


If the Library information is needed at runtime you need to represent it in your types. It's not a constant by definition, so you need to express it in your functions.

You can get easier levels of code reuse though by using the Reader monad to thread this dependency across the program.

  • Thanks. I'm now trying to get my head round the Reader monad, but it does indeed look like what I'm after.
    – user15553
    Sep 29, 2016 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.