© Stefania Loredana Nita and Marius Mihailescu 2019
Stefania Loredana Nita and Marius MihailescuHaskell Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-4507-1_3

3. GHC

Stefania Loredana Nita1  and Marius Mihailescu1
(1)
Bucharest, Romania
 

GHC stands for Glasgow Haskell Compiler , which is the native code compiler for Haskell. It is open source and can be downloaded from https://www.haskell.org/ghc/ . It is also integrated into the Haskell Platform, available at https://www.haskell.org/downloads#platform . (We recommend you install the Haskell Platform because it contains the compiler, Cabal,1 and other tools you’ll use in this book.)

Introducing GHC

The main components of GHC are the interactive interpreter GHCi and the batch compiler. GHC contains many libraries, but you can also use other extensions, such as for concurrency, exceptions, the type system, quantifications, a foreign function interface, and so on. When a program is compiled, the default behavior of GHC is to compile quickly but not to optimize the generated code. Using the batch compiler with GHCi has some advantages: the library code is precompiled, and a user’s program is compiled very quickly.

Let’s look at how to use GHC in a terminal. In these examples, the operating system is Microsoft Windows 10, and we’re using GHCi version 8.4.3. So, open a terminal and type the ghci command (if this command is not recognized in the terminal, then it needs to be added to the environment variables). Now, the terminal should display the version of GHCi followed on the next line by Prelude> (as in Figure 3-1).
../images/475690_1_En_3_Chapter/475690_1_En_3_Fig1_HTML.jpg
Figure 3-1.

Using GHCi in a terminal

The GHCi represents GHC’s interactive environment, so the i in the command ghci stands for interactive. Prelude is a standard module in Haskell (a module is a set of connected functions, types, or type classes), imported by default into all Haskell modules.

Let’s print the string “Hello, World!” to the terminal. To do that, type putStrLn "Hello, World!" after Prelude>. putStrLn is a function in the Prelude standard module that prints the argument on the standard output device and adds a new line after the argument. (We will talk more about functions in the next chapters.) You’ll get Hello, World!, as shown here:
Prelude> putStrLn "Hello, World!"
Hello, World!
Of course, real-world programs will be much more complex. It is more convenient to write them organized into files than to write them line by line at the terminal like this. The extension for Haskell files is .hs. So, in a text editor, write the following line:
main = putStrLn "This is my first Haskell program!"
and save it as first.hs. To compile the program, write the following line at the terminal:
ghc -o first first.hs
If you don’t receive any error message, then the output should look like this:
[1 of 1] Compiling Main             ( first.hs, first.o )
Linking first.exe ...
If you receive the error <no location info>: error: can't find file: first.hs, make sure the path before the compiling command is the one that contains the first.hs file or put the file’s full path in the compiling command. Here’s an example:
ghc -o first D:HaskellFirstProgramfirst.hs

The –o option tells the compiler to optimize compiler flags in order to generate faster code.

If you want to see more options or how a command works, type --help after the desired command.

To run the executable, type first.exe and then press Enter. Here’s an example:
D:HaskellFirstProgram>first.exe
If you use Unix, just type first (without the extension). You will get the following:
This is my first Haskell program!
For Windows users, another way to use GHCi is to launch WinGHCi (Figure 3-2).
../images/475690_1_En_3_Chapter/475690_1_En_3_Fig2_HTML.jpg
Figure 3-2.

A graphical user interface (GUI) for GHCi in Windows

Examples

For the next examples, you will keep the standard prompt of Prelude. Note that Haskell is case sensitive, so a is different from A. You already used strings in the previous section, so let’s look at some simple arithmetic operations.
Prelude> 10 + 5
15
Prelude> 100 - 20
80
Prelude> 14 * 88
1232
Prelude> 78 / 5
15.6
Prelude> (27 - 15) * 42
504
If you try 12 * -7, you will get an error, as shown here:
Prelude> 12 * -7
<interactive>:12:1: error:
    Precedence parsing error
        cannot mix '*' [infixl 7] and prefix '-' [infixl 6] in the same infix expression
But if you try 12 * (-7), you will get the right answer.
Prelude> 12 * (-7)
-84
GHC contains predefined mathematical functions such as succ, min, max, div, and so on. Let’s take a deep look at the div function. Its type is as follows:
Integral a => a -> a -> a
You should call the function like this:
Prelude> div 15 3
5
But Haskell permits you to use it in a more natural way.
Prelude> 15 'div' 3
5
Functions are the most important elements in Haskell; in the next example, you’ll write a simple function that sums two numbers.
Prelude> mySum a b = a + b
In this definition, mySum is the name of the function, and a and b are the parameters. The part to the right of the equal sign is what the function does; in these examples, it adds the parameters. To call the function, you just type the name of the function and pass the arguments.
Prelude> mySum 7 8
15

So, there it is. In the next chapters, you will see that there are more types of functions, and you will write your own complex functions.

Summary

This chapter presented basic concepts about Haskell’s compiler.
  • You learned how to use GHC in two different ways.

  • You compiled and ran some introductory examples.

References

  1. 1.
     
  2. 2.
     
  3. 3.
     
  4. 4.

    P. Hudak, J. Hughes, S. Peyton Jones, and P. Wadler, “A history of Haskell: being lazy with class,” in proceedings of the third ACM SIGPLAN conference on the history of programming languages, pp. 12–1 (ACM, 2007).

     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.49.106