Wednesday, January 2, 2013

HASKELL GHC "COMPILER MODE"

HASKELL GHCI "COMPILER MODE"



REVISED: Friday, February 9, 2024




The Glasgow Haskell Compiler (GHC).

I. WRITING YOUR FIRST HASKELL COMPILED PROGRAM

GHC is written in Haskell and is a single program which is run with different options to provide two modes of operation: GHCi (the interactive Haskell interpreter mode) and GHC (the batch compiler mode).

A. WRITE A HASKELL PROGRAM

"Copy Paste" the following Haskell program script into your text editor:

main = putStrLn "Hello, World!"

From your text editor, do a "File Save As" and save the above Haskell program, using the file name hello.hs to your working directory.  Using my text editor I saved my hello.hs file using the following working directory path:

C:\Users\Tinnel\Haskell2024\newProjects\

B. LOAD HASKELL PROGRAM INTO GHCI INTERPRETER

"Double left mouse click" the Haskell icon on your desktop to start Haskell GHCi.  A Haskell GHCi window similar to the following will open:

GHCi, version 9.8.1: http://www.haskell.org/ghc/  :? for help
Prelude> 

Haskell code can be compiled to an executable, or run interactively. The GHCi runtime system interprets the byte-code compiled from the source code

After the above Haskell GHCi window Prelude> prompt, type :load hello, and then press Enter. You should now see the following response:

Prelude> :load hello
[1 of 1] Compiling Main             ( hello.hs, interpreted )
Ok, 1 module loaded.


You can now call the function main as follows:

*Main>  main
Hello, World!
*Main>  

You are now in the Haskell GHCi "interactive mode". You have an interactive feedback loop. Each time you change the Haskell program in the hello.hs file, you must reload it in Haskell using :load hello or :reload before you call the function main. You can make as many changes as you want to your program and interactively interpret the Haskell program until you are satisfied with your work and you are ready to compile your finished Haskell program.

C. COMPILE HASKELL PROGRAM USING GHC COMPILER

After the above Haskell GHCi window *Main> prompt, type :! ghc --make "hello", and then press Enter. You should now see the following response:

Prelude> :! ghc --make "hello"
[1 of 1] Compiling Main             ( hello.hs, hello.o )
Linking hello.exe ...
Prelude>

Congratulations, you just compiled your first Haskell program!

Examine your working directory and you will see it now contains the following files:

hello.hs      Haskell source file.
hello.o        Haskell object file.
hello.hi       Haskell interface file.
hello.exe     Haskell executable file.

GHC compiled the source file hello.hs, producing an object file hello.o and an interface file hello.hi, and then it linked the object file to the libraries that come with GHC to produce an executable called hello on Unix/Linux/Mac, or hello.exe on Windows.

II. EXECUTING YOUR FIRST HASKELL COMPILED PROGRAM

Open your "Windows Command Prompt." In Windows this will be a window with the title "Command Prompt." This window has a black background and displays white letters and when you open it, it will display something similar to the following:

C:\Users\Tinnel>

After the > prompt finish typing in your working directory path and then the hello.exe file name as shown below, and press Enter:

C:\Users\Tinnel>Haskell2024\newProjects\hello.exe
Hello, World!

C:\Users\Tinnel>

As shown above, your compiled Haskell program was executed and Hello, World! was displayed on your "Windows Command Prompt" window.

Congratulations, you have just executed your first compiled Haskell program!

Haskell programmers believe in the motto “compile early, compile often.” Live by this motto and your Haskell life will be fun and exciting!

III. REVISING A HASKELL COMPILED PROGRAM

You can revise the source file of your program as often as you like.

GHCi automatically detects if a script source file has been changed and will only reload, and or, recompile a source file if GHCi determines it is necessary to do so.

When you are done with your revisions use the following commands to ask GHCi if it is necessary to reload or recompile your source code:

A. RELOAD

Prelude> :load hello
[1 of 1] Compiling Main             ( hello.hs, interpreted )
Ok, 1 module loaded.

B. RECOMPILE

Prelude> :! ghc --make "hello"
Prelude>

C.  VIEW REVISION

Open your "Windows Command Prompt" and follow the above directions in section "II. EXECUTING YOUR FIRST HASKELL COMPILED PROGRAM."

IV. CONCLUSION

In this tutorial, you have been introduced to the Glasgow Haskell Compiler (GHC) "compiler mode."

V. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.