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?