For example, take the code written by Don Stewart in reply to some Stack Overflow question:
import Control.Monad
import qualified Data.HashTable as H
import System.Environment
main = do
[size] <- fmap (fmap read) getArgs
m <- H.new (==) H.hashInt
forM_ [1..size] $ \n -> H.insert m n n
v <- H.lookup m 100
print v
Load it in GHCi.
:t getArgs ---> getArgs :: IO [String]
:t main ---> main :: IO ()
Why doesn’t the type signature of main reflect the fact that getArgs :: IO [String] is being called?
When you run the binary you can give an argument.
<prog> 145 returns Just 100
But in GHCi, you cannot: main 145 gives error. How do you run this program in GHCi and give an argument.
The type of
mainis that of its final expression;printproducesIO (), so that’s the type ofmain. Intermediate types are not relevant, as(>>=)doesn’t propagate anything other than the monad.adoesn’t appear in the result type (m b).As for running your program in GHCi, take a look at the
:maincommand.