Haskell assignment type

Viewed 157

From the haskell.org website: every expression has a type. So what is the type of

main = putStrLn "Hello, World!"

I would like to know not what the type of main is, but what the type of the whole line is. Also, is it possible to get this type somehow in ghci? I tried

:t (main = putStrLn "Hello, World!")

but this does not work.

2 Answers

main = putStrLn "Hello, World!" is not an expression, and thus does not have a type. It's a definition, which assigns a value of type IO () (resulting from the evaluation of putStrLn :: String -> IO ()) to the name main.

Following the definition, you can find the type of main:

> :t main
main :: IO ()

every expression has a type.

That is correct, but a declaration is not an expression. 1+1 is an expression, a = 1+1 is not an expression, but a declaration.

I would like to know not what the type of main is, but what the type of the whole line is.

This has no type, you simply define a variable with the given expression. The expression putStrLn "Hello, World!" has type IO (), since putStrLn has type putStrLn :: String -> IO ().

Related