Following the excercises in chapter 5 of Real World Haskell, I end up with Prettify.hs, which exports among other things an abstract type Doc and a rendering function compact, which is a function from Doc to String. Another file, PrettyJSON.hs exports renderJValue, which ends up giving me a Doc value. In my main, I import just renderJValue and compact, and use the output of the one as input to the other. I am confused why this works. I would think that it would also have been necessary to import the abstract Doc type. Can Haskell see that the two functions fit together without the Doc imported?
For illustration, this is my Main.hs:
module Main where
import System.IO
import SimpleJSON (JValue(..))
import PrettyJSON (renderJValue)
import Prettify (compact)
main = do
let val = renderJValue $ JString "foo"
putStrLn $ compact val
getLine
which outputs
"foo"
Can Haskell see that the two functions fit together without the Doc imported?
Yes.
To elaborate a bit, what you import
is only saying what's in your local namespace, so to mention the Doc type in a type signature, you'd have to import it. ghc is doing a lot of matching up types (and hunting down modules) in the background when loading the dependencies of your code, too, but such matters are implementation detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With