What Haskell is and where it runs
What Haskell is
Haskell is a compiled, statically typed, purely functional
language. Each of those words changes the way a program is written, and the
smallest useful program already shows two of them.
A Haskell file is a list of definitions. One of them is called
main, and the runtime calls it when the program
starts. Everything else is a function or a value the compiler can evaluate
on its own. The compiler is GHC, the Glasgow Haskell Compiler, and it
produces a native executable, exactly as a C compiler does.
The first program below keeps two functions apart on purpose. Neither of them prints anything; only main talks to the terminal.
module Main (main) where
-- A pure function: the result depends on the argument and nothing else.
circleArea :: Double -> Double
circleArea r = pi * r * r
-- A second pure function, built from the first one.
ringArea :: Double -> Double -> Double
ringArea outer inner = circleArea outer - circleArea inner
main :: IO ()
main = do
putStrLn "area of a unit circle:"
print (circleArea 1)
putStrLn "area of a ring between r=2 and r=1:"
print (ringArea 2 1)
Save it as Circle.hs and compile it. The compiler reads the file, checks it against its type, and writes an executable named after the -o argument.
ghc -Wall -O1 -o circle Circle.hs
./circle
echo "status: $?"
[1 of 2] Compiling Main ( Circle.hs, Circle.o )
[2 of 2] Linking circle
area of a unit circle:
3.141592653589793
area of a ring between r=2 and r=1:
9.42477796076938
status: 0
The two numbers are what the machine really printed, and they are the same on any machine, because the definition of the function is the whole story. That is what purely functional means: no hidden input, no hidden output.
Purity, in one program
In a language with mutable state a function can read a variable that somebody
else changed, and calling it twice with the same argument can give two
answers. Here it cannot: circleArea has one argument
and one result, and there is no place in the definition to read anything
else. A call with 2 is always the same value, so the
compiler is free to compute it once, and a reader is free to replace the call
with the number.
Effects are not forbidden, they are typed. putStrLn
and print return IO (), and
only an IO action may run them. The signature of
main says it is one. A function whose signature has
no IO in it cannot print, read a file or look at the
clock, which is why it is so easy to test on its own.
The signature line is not decoration. Double -> Double is a function from one double-precision number to another; Double -> Double -> Double is a function that takes one and returns a function that takes the next. The lesson on types and signatures reads these lines in full.
Laziness, in one program
Haskell evaluates an expression when its value is needed, not when it is written. A list can therefore be infinite, as long as nothing asks for all of it. The program below builds the list of every even number and takes four of them.
module Main (main) where
-- [0 ..] is an infinite list. take asks for four elements and stops.
firstFourDoubles :: [Int]
firstFourDoubles = take 4 (map (* 2) [0 ..])
main :: IO ()
main = print firstFourDoubles
ghc -Wall -O1 -o lazy Lazy.hs
./lazy
[1 of 2] Compiling Main ( Lazy.hs, Lazy.o )
[2 of 2] Linking lazy
[0,2,4,6]
A strict language would have to build the whole list first, and it would never finish. Here take 4 pulls four elements and the rest is never computed. Laziness is a tool and a trap at the same time: the lesson on laziness shows both sides, and the one on space leaks shows what happens when it keeps memory alive by accident.
The toolchain
Two commands are enough to start. runghc interprets a file and runs it at once, which is what a quick experiment wants; ghc compiles it to an executable. The next lessons install both, then open the interactive shell.
ghc --version
runghc --version
The Glorious Glasgow Haskell Compilation System, version 9.10.3
runghc 9.10.3
The build above used -Wall, which turns on the warnings a beginner should see, and -O1, the first level of optimisation. Every program in this course is built with those two flags, so a warning on a page is a warning you would get too. The lesson on warnings reads what they say.
Exercises
- Change circleArea to take the diameter instead of the radius. The signature stays the same, so the compiler will not help: write down the call that proves the change is right.
- Add a function sphereVolume :: Double -> Double and print it for r = 1. What is the exact number the machine prints?
- Replace take 4 with take 0 in Lazy.hs. The program still finishes; explain why the infinite list is never touched.
- Build Circle.hs without -Wall and then add an unused import. Which of the two builds tells you about it?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does it mean that a Haskell function is pure?
Answer
Its result depends only on its arguments, and it has no side effects
A pure function reads nothing but its arguments and changes nothing outside itself, so the same call always gives the same value.
-
Which part of a program may print to the terminal?
Answer
Only an IO action, such as the body of main
Printing returns IO (), and only an IO action may run another IO action; a function without IO in its type cannot print.
-
What does laziness let this definition do?
firstFourDoubles :: [Int] firstFourDoubles = take 4 (map (* 2) [0 ..])Answer
Build an infinite list and take four elements without computing the rest
The list is infinite, but take asks for four elements, so only those four are ever computed.
-
Which command compiles a file to a native executable?
Answer
ghc
ghc is the compiler; runghc interprets a file, ghci is the interactive shell and ghc-pkg manages installed packages.
-
What does this program print?
main :: IO () main = print (2 + 3)Answer
5
print shows the value of the expression, and 2 + 3 is 5.
-
What does this program print?
module Main (main) where main :: IO () main = do putStr "one " putStrLn "two"Answer
one two
putStr writes without a newline and putStrLn adds one at the end, so both words share a line.
-
What does this program print?
module Main (main) where area :: Double -> Double area r = pi * r * r main :: IO () main = print (area 0)Answer
0.0
Zero squared is zero, so the product is 0.0, printed as a Double.
-
What does this program print?
module Main (main) where main :: IO () main = print (take 0 [1 ..])Answer
[]
take 0 asks for no elements at all, so the infinite list is never evaluated.
-
A file has the definition
area r = pi * r * rand no signature. What do you lose?Answer
The compiler still infers it, but -Wall warns and the reader loses the documented contract
The type is inferred, but -Wall reports a missing signature, and the signature is the part a reader reads first.
-
What is the first line of the compiler output for this program?
module Main (main) where main :: IO () main = putStrLn 42Answer
error: [GHC-39999]
putStrLn needs a String and 42 is a number, so GHC reports that no Num String instance exists, with code GHC-39999.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev