<<

Installing Haskell

0.1 Learning programming

Throughout this course you’ll be learning to program in Haskell. One way of learning a is to start with formal syntax and semantics (although very few programming languages have formal semantics). This is very different from how most people learn languages, be it natural or computer languages. A child learns the meaning of words and sentences not by analyzing their structure and looking up the definitions in Wikipedia. In fact, a child’s understanding of language is very similar to how we work in category theory. We derive the meaning of objects from the network of interactions with other objects. This is easy in mathematics, where we can say that an object is defined by the totality of its interactions with possibly infinite of other objects (we’ll see that when we talk about the Yoneda lemma). In real life we don’t have the time to explore the infinity, so we can only approximate this process. We do this by living on credit. This is how we are going to learn Haskell. We’ll be using things, from day one, without fully understanding their meaning, thus constantly incurring a conceptual debt. Some of this debt will be paid later. Some of it will stay with us forever. The alternative would be to first learn about cartesian closed categories and domain theory, formally define simply typed , build its categorical model, and only then wave our hands a bit and claim that at its core Haskell approximates this model.

0.1.1 Installing Haskell

Download and install on your computer. (On the Mac or , you’ll have to close and reopen the terminal after installation). To start an interactive session from the command line, type ghci (GHC stands for the Glasgow Haskell or, more affectionately, among Haskell aficionados, the Glorious Haskell Compiler; and ’i’ stands for interactive). You can start by evaluating simple expressions at the prompt, e.g.:

$ ghci

1 2

GHCi, version 8.6.5: http://www.haskell.org/ghc/ :? for help Prelude> 2 * (3 + 4) 14 Prelude> 1/2 0.5 Prelude> 2^64 18446744073709551616 Prelude> cos pi -1.0 Prelude> mod 8 3 2 Prelude> :q Leaving GHCi.

Prelude is the name of the standard Haskell library, which is automatically included in every Haskell program and in every ghci session. As you can see, besides basic arithmetic operations, you have access to some stan- dard functions. Notice that a function call does not require the the arguments to be parenthesized, even for two-argument functions like mod (division modulo). This is something you will have to get used to, but it will make a lot of sense later, when we talk about partial application and currying (that’s the of conceptual debt we were talking about). To exit the command loop, type :q The interactive environment is great for trying things out, but real programs have to be compiled from files. We’ll be mostly working with single-file programs. Use your favorite editor to create and edit Haskell source files, which are files with the extension .hs. To begin with, create a file main.hs and put this line of code in it:

main = putStrLn "Hello Haskell!"

Compile this file using the command:

ghc main and then run the resulting executable. The details depend on the . For instance, you might have to invoke the program by typing ./main at the command prompt:

$ ./main Hello Haskell!

If you name your file something else, it will be treated as a separate module, and will have to include a module declaration at the top. For instance, a file named hello.hs will have to start with: 0.1. LEARNING PROGRAMMING 3

module Hello where main = putStrLn "Hello Haskell!"

(Module names must start with upper case.)

0.1.2 Haskell points

Throughout the book, elements of Haskell will be interspersed with concepts from category theory, so it makes sense to periodically summarize what we have learned in a more organized way. For instance, we have just learned that you can do arithmetic in Haskell. You have at your disposal the usual operators +, -, *, and /, as well as the power operator ^. You can apply them to integers and floating-point numbers (except for the power, which only allows integer exponents). There is a little gotcha with the unary minus: when you write a negative literal, like -8, it is treated like the application of the function negate to a literal 8. This is why you sometimes have to parenthesize it, as in:

mod (-8)3 or

2*(-3)

Without parentheses, the Haskell compiler will issue one of its infamously cryptic error messages. Useful functions: abs, floor, ceiling, round; integral division div and mod satisfy- ing the equality:

(div x y)*y+ (mod x y) ==x

Square root , sqrt, trigonometric functions and the constant pi, natural exponential exp and logartithm log. When using the interactive environment, ghci, variables are defined using the let syntax, e.g.:

let x = sin (pi/4) let y = cos (pi/4) x^2+y^2

Expressions are immediately evaluated and the results are printed (if they are print- able). Variable names must start with a lowercase letter. Conveniently, following mathematical usage, variable names may contain apostrophes, as in

let x' = 2*x

When working source files, the module definition has to be included at the top (except if the file is called main.hs, in which case the module statement can be omitted). For example, if the file is called test.hs, it must start with: 4

module Test where

Module names must start with an uppercase letter and may contain periods, as in Data.List. If you want to compile and execute a Haskell file, it must contain the definition of main. In general, input and output are performed in main. Unlike in imperative languages, I/O is special in Haskell and it requires the use of the IO monad. We will eventually talk about monads, but for now, we’ll just follows a few simple rules. To begin with, we can print any string in main using putStrLn. We can also print things that can be displayed, like numbers or lists, using print. For instance:

main = print (2^31-1)

Remember to enclose composite expressions in parentheses when passing them to print. If you want to do multiple I/O operations in main, you have to put them in the do block:

main = do putStrLn "This is a prime number" print (2^31-1)

Statements in a block must be all indented by the same amount. Here are some useful online resources: • Hoogle: https://hoogle.haskell.org. Lets you look up Haskell function defini- tions, either by name or by type signature. • Haskell cheat sheet: https://www.cs.dartmouth.edu/ cbk/classes/8/handouts/CheatSheet.pdf