Haskell: Why isn't the type deduced by GHC for the main method complete?

For example, take the code written by don Stewart in response to a 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

Upload it to GHCi.

:t getArgs ---> getArgs :: IO [String]
:t main    ---> main :: IO ()

Why does a signature of type main not reflect the fact that it getArgs :: IO [String]is being called?

When you run the binary, you can give an argument. <prog> 145returns Just 100 But in GHCi you cannot: main 145gives an error. How to run this program in GHCi and give an argument.

+3
source share
2 answers

A type mainis its final expression; printcreates IO (), so type main. Intermediate types are not relevant, since it (>>=)does not apply to anything other than a monad.

(>>=) :: Monad m => m a -> (a -> m b) -> m b

a (m b).

GHCi, :main.

+6

:set args. :

Prelude> import System.Environment
Prelude System.Environment> getArgs
[]
Prelude System.Environment> :set args ["foo","bar"]
Prelude System.Envrionment> getArgs
["foo","bar"]

, main print v. , >>.

+4

All Articles