How to use the command "ghc" (with examples)
The Glasgow Haskell Compiler (ghc) is a tool used to compile and link Haskell source files. It provides various options and functionalities for compiling Haskell programs efficiently. This article will provide examples of different use cases for the “ghc” command.
Use case 1: Find and compile all modules in the current directory
Code:
ghc Main
Motivation:
The use case is useful when we have multiple Haskell modules in the current directory and we want to compile them all together. This eliminates the manual process of compiling each module individually.
Explanation:
The command “ghc Main” instructs the compiler to compile all the Haskell modules (.hs files) present in the current directory. The name “Main” is usually used as the entry point module, where the program execution starts.
Example output:
If the current directory contains three files A.hs
, B.hs
, and C.hs
, executing ghc Main
would compile all these modules and generate an executable file named “Main”.
Use case 2: Compile a single file
Code:
ghc file.hs
Motivation:
This use case is handy when we have a single Haskell source file and want to compile it into an executable or an object file.
Explanation:
The command “ghc file.hs” compiles the Haskell source code from the file “file.hs” and generates an executable file by default. The executable will have the same name as the file, excluding the extension.
Example output:
Executing ghc hello.hs
would compile the “hello.hs” file and generate an executable named “hello”.
Use case 3: Compile using extra optimization
Code:
ghc -O file.hs
Motivation:
Sometimes we want to optimize our Haskell code for better performance. This use case allows us to compile with extra optimization flags applied.
Explanation:
The flag “-O” enables additional optimization during the compilation process. It applies various optimizations, such as inlining functions, performing strictness analysis, and applying loop optimizations, to produce a more optimized executable.
Example output:
By executing ghc -O program.hs
, the Haskell code in “program.hs” will be compiled with extra optimization flags applied, resulting in a more efficient executable.
Use case 4: Stop compilation after generating object files
Code:
ghc -c file.hs
Motivation:
In certain scenarios, we may want to stop the compilation process after generating object files (.o files). This can be useful if we want to distribute pre-compiled object files to other developers.
Explanation:
The “-c” flag in the “ghc” command instructs the compiler to only compile the Haskell source file and generate object files (.o files). It halts the compilation process before linking the object files into an executable.
Example output:
When executing ghc -c module.hs
, the compiler will compile “module.hs” and produce an object file named “module.o” without linking it to create an executable.
Use case 5: Start a REPL (interactive shell)
Code:
ghci
Motivation:
The Read-Eval-Print Loop (REPL), provided by the “ghci” command, allows us to interactively evaluate Haskell expressions and experiment with code snippets. It is a quick and convenient way to test and debug Haskell code.
Explanation:
Executing the “ghci” command starts a REPL session. It launches an interactive shell where we can enter Haskell expressions and see the evaluated results. It provides an environment to experiment, test, and debug Haskell code interactively.
Example output:
After running ghci
, the terminal will switch to a Haskell REPL prompt where we can enter Haskell expressions for evaluation. For example, entering 2 + 3
would produce the output 5
.
Use case 6: Evaluate a single expression
Code:
ghc -e expression
Motivation:
Sometimes, we might want to quickly evaluate a single Haskell expression as a one-off task. This use case allows us to do so without the need to create a separate Haskell file.
Explanation:
The “-e” flag in the “ghc” command allows us to directly evaluate a single Haskell expression without the need for a source file. We can provide the expression as an argument to the “-e” flag.
Example output:
Executing ghc -e "map (*2) [1, 2, 3]"
would immediately evaluate the expression map (*2) [1, 2, 3]
and print the resulting list [2, 4, 6]
.
Conclusion:
In this article, we explored various use cases of the “ghc” command. We learned how to compile all modules in the current directory, compile a single file, enable extra optimization during compilation, stop compilation after generating object files, start a REPL session, and evaluate a single expression. These use cases provide flexibility and efficiency while working with Haskell code, enabling developers to compile, test, and debug their programs effectively. The “ghc” command is an essential tool for Haskell developers, assisting them in turning their Haskell source code into performant executables.