Haskell is a powerful and highly expressive functional programming language. One of its key tools is the Glasgow Haskell Compiler (GHC), which comes with an interactive shell called GHCI. GHCI allows you to test and experiment with Haskell code in real time, making it a valuable tool for both beginners and experienced developers. In this article, we’ll walk through some super basics to help you get started with Haskell and GHCI, covering everything from launching GHCI to navigating folders and quitting the session.
What is GHCI?
GHCI (Glasgow Haskell Compiler Interactive) is an interactive environment for evaluating Haskell expressions, testing code snippets, and loading Haskell scripts. It’s especially useful for quickly checking how functions behave or for experimenting with Haskell concepts without needing to write a full program.
Let’s break down the essentials of how to start using GHCI, and perform some basic tasks such as navigating folders and quitting the shell.
1. How to Start GHCI
Before you can start working with Haskell, you’ll need to install the Glasgow Haskell Compiler (GHC) and GHCI. If you haven’t installed it yet, you can download it from the Haskell Platform, which provides a complete environment to work with Haskell.
Once GHC and GHCI are installed, you can launch GHCI from your terminal. Here’s how to do it:
Step-by-Step: Starting GHCI
- Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux).
- Type the command
ghci
and pressEnter
.
$ ghci
GHCi, version 9.2.2: https://www.haskell.org/ghc/ :? for help
Prelude>
After entering the ghci
command, you’ll be greeted with a Prelude>
prompt, indicating that you’ve entered the interactive shell of GHCI. The Prelude
is the default module loaded, which contains basic functions and types for Haskell.
2. How to Quit GHCI
Now that you’re in GHCI, you might wonder how to exit once you’re done experimenting. GHCI has a simple command for this.
Step-by-Step: Quitting GHCI
- While inside GHCI, type the following command:
:quit
Or simply type:
:q
2. Press Enter
. This will immediately exit you from the GHCI environment and return you to your terminal.
Prelude> :quit
Leaving GHCi.
$
3. Navigating and Launching GHCI in Folders
When working with larger Haskell projects, you’ll often need to load specific files or modules from particular directories. GHCI allows you to navigate your filesystem and start the interactive shell in any directory, enabling you to work within a specific project folder.
Step-by-Step: Launching GHCI in a Specific Folder
- Open your terminal and navigate to the folder where your Haskell files are located. You can use the
cd
(change directory) command to do this.For example, if your Haskell project is in a folder namedmyHaskellProject
, you would type:
$ cd path/to/myHaskellProject
2. Once inside the desired folder, launch GHCI by typing:
$ ghci
3. Now, GHCI will be aware of the files and modules in the current directory. You can load a specific Haskell file using the :load
command followed by the file name:
Prelude> :load Main.hs
[1 of 1] Compiling Main ( Main.hs,interpreted )
Ok, one module loaded.
4. If you want to list the files in your current directory while inside GHCI, you can use the command:
:! ls
This command runs shell commands directly from within GHCI (:!
is used to issue system commands).
4. A Few Useful Commands in GHCI
While GHCI is incredibly useful, its functionality extends beyond just loading and quitting. Here are a few basic commands to enhance your workflow:
:load [filename]
– Loads a Haskell file for you to test its functions and expressions.:reload
or:r
– Reloads the last loaded file after making changes to the code.:type [expression]
– Tells you the type of an expression. For example:
Prelude> :type 5
5 :: Num a => a
:info [name]
– Provides information about a specific function or type.:set -X
– Some advanced Haskell features require enabling certain language extensions. You can enable these directly in GHCI using the:set -X
command.
5. Writing and Loading a Simple Haskell Program
Let’s create a simple Haskell file, load it into GHCI, and test it. Suppose you have a file Main.hs
with the following content:
-- Main.hs
square :: Int -> Int
square x = x * x
To test it:
- Save this file in a directory.
- Open your terminal and navigate to the directory where
Main.hs
is saved. - Launch GHCI by typing
ghci
. - Load the file using
:load Main.hs
:
Prelude> :load Main.hs
[1 of 1] Compiling Main ( Main.hs, interpreted )
Ok, one module loaded.
*Main>
Now you can call the square
function in the GHCI prompt:
*Main> square 5
25
6. Commenting in Haskell
In Haskell, comments are used to annotate code, explain logic, or temporarily disable lines of code during development. Haskell supports two types of comments:
1. Single-Line Comments
Single-line comments in Haskell begin with --
. Anything after --
on the same line is ignored by the compiler.
Example:
-- This is a single-line comment in Haskell
square x = x * x -- This function returns the square of a number
2. Multi-Line Comments
Multi-line comments in Haskell are enclosed between {-
and -}
. Everything between these delimiters is treated as a comment, even if it spans multiple lines.
Example:
{-
This is a multi-line comment.
It can span several lines.
-}
cube x = x * x * x {- This function returns the cube of a number -}
Nesting Multi-Line Comments
Haskell supports nested multi-line comments, which means you can have a multi-line comment within another multi-line comment.
Example:
{- This is a multi-line comment.
{- This is a nested comment. -}
The outer comment continues here.
-}
Commenting Summary:
- Use
--
for single-line comments. - Use
{- -}
for multi-line or block comments, and you can nest them if needed.
Conclusion
GHCI is an excellent tool for experimenting with Haskell in real time. Whether you’re writing small snippets or testing entire modules, it gives you quick feedback on how your Haskell code behaves. In this guide, we covered the very basics: how to start GHCI, how to quit, and how to navigate and work within project folders. As you get more comfortable with GHCI, you’ll find it indispensable for learning and developing in Haskell.
If you’ve tested out these super basics of GHCI and are ready to try some more basic functions, check out my next article List of the Basic Functions in Haskell.
Happy coding!
💡 Helpful References
Learn You a Haskell
https://learnyouahaskell.github.io/starting-out.html
Haskell - Getting Started
https://www.haskell.org/get-started/
Leave a Reply