runghc, ghc and the exit status

runghc runs a file

Two commands turn a Haskell file into something that runs. runghc reads the file, compiles it in memory and runs it at once. ghc compiles it to a native executable that you can copy to another machine of the same kind and run again and again.

The file used on this page exits with a chosen status, so that the status can be seen and not guessed:

module Main (main) where import System.Exit (ExitCode (ExitFailure), exitWith) main :: IO () main = do putStrLn "before the exit" exitWith (ExitFailure 3)

runghc Exit.hs
echo "status: $?"

before the exit
status: 3

The line is printed, and the shell reports 3. Nothing was written to the folder, which is what makes runghc the right tool for a fifteen-line experiment: there is nothing to delete afterwards.

runghc is a real compiler, not a text interpreter: a type error stops it before the first line of output, and a slow program is slow for the same reason a compiled one would be. The difference is that the work is thrown away when the program ends.

ghc builds a binary

The same file, through the compiler, with the two flags this course uses everywhere:

ghc -Wall -O1 -o exitcode Exit.hs
./exitcode
echo "status: $?"

[1 of 2] Compiling Main ( Exit.hs, Exit.o )
[2 of 2] Linking exitcode
before the exit
status: 3

The two bracketed lines are the build, and they say what happened in order: the module Main was compiled to Exit.o, then the object file was linked into exitcode. -o names the binary; without it, GHC would name it after the module, so Main here, which is rarely what you want.

The run prints the same line and the same status as runghc did. A second build of an unchanged file prints nothing at all and exits with 0: the compiler compares the timestamps of the source and the object file and does no work.

What a build leaves behind

After the build above, the folder holds four entries where there was one:

ls

Exit.hi
Exit.hs
Exit.o
exitcode

All three of the others are build products, and all three are rebuilt when the source changes. When you copy a project, copy the .hs files and drop the rest; when a build behaves strangely after an upgrade, delete them and let the compiler write them again.

The exit status

Every process ends with a number from 0 to 255. Zero means success, and every other value means some kind of failure. The shell keeps the number of the last command in $?, which is why the transcripts on this page print it right after the run.

A Haskell program chooses the number with System.Exit:

A status is how one program reports to another: a shell script stops on cmd1 && cmd2, a test suite fails the build when it returns a non-zero number, and a CI runner decides red or green from it. Printing the message is not enough, because a message can be missed.

An uncaught Haskell exception also ends the process with a non-zero status, so a script notices a crash without being told about exceptions. The lesson on exceptions takes that further.

Type-check without a binary

A third way to use the compiler answers a narrower question: is this file correct? -fno-code reads and type-checks everything and writes no object file and no binary. It is the fastest check there is, and the one to run while editing:

ghc -fno-code -Wall -O1 Exit.hs
echo "status: $?"

[1 of 1] Compiling Main ( Exit.hs, nothing )
status: 0

nothing is where the object file would be, and it is the compiler saying that this run produced no code. A type error would be reported here in exactly the form it takes in a full build, with the file, the line and the GHC error code, and the status would be 1.

So the same file has three entry points: runghc to run it once, ghc to build it, and ghc -fno-code to check it. The next lesson writes a program that does something useful, and uses the first and the second.

Exercises

  1. Change ExitFailure 3 to exitSuccess and run both runghc and the compiled binary. What does $? say in each case?
  2. Build Exit.hs without -o. What is the binary called, and why is that name a bad idea in a folder with several programs?
  3. Delete Exit.hi and build again. Which of the two bracketed lines is printed this time?
  4. Run ./exitcode && echo ok. Nothing is printed, and the exercise is to explain why the status decides it.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does runghc leave in the folder after it runs a program?

    Answer

    Nothing: it compiles in memory and throws the result away

    runghc compiles to memory and runs at once, so the folder holds only what was there before.

  2. Which file of a build does a program need at run time?

    Answer

    The executable

    The executable holds the linked machine code; the .hi and .o files are inputs of the build, not of the run.

  3. What is the .hi file of a module?

    Answer

    The interface: types, instances and exported names

    The interface file holds what another module needs to compile against this one, which is why it is written next to the object file.

  4. Which exit status means that a program succeeded?

    Answer

    0

    Zero means success in the shell; every other value from 1 to 255 reports a kind of failure.

  5. What does ghc -fno-code -Wall -O1 Exit.hs produce?

    Answer

    Nothing on disk: it only type-checks

    -fno-code stops after type checking and writes no object file, no interface file and no binary.

  6. What does this program print, and what status does it end with? module Main (main) where import System.Exit (ExitCode (ExitFailure), exitWith) main :: IO () main = do putStrLn "before the exit" exitWith (ExitFailure 3)

    Answer

    before the exit / status: 3

    The line is printed before the exit, and exitWith (ExitFailure 3) ends the process with 3.

  7. What does this program print? module Main (main) where import System.Exit (exitSuccess) main :: IO () main = do putStrLn "checking" exitSuccess

    Answer

    checking

    exitSuccess ends the program with status 0 and prints nothing itself; the line before it is already out.

  8. What does this program print? module Main (main) where main :: IO () main = do putStr "a" putStr "b" putStrLn "c"

    Answer

    abc

    putStr writes without a newline and putStrLn adds one at the end of its string, so the three letters share one line.

  9. A script runs ./exitcode || echo failed and prints failed. What does that prove?

    Answer

    The program ended with a non-zero status

    The || branch runs only when the left command returns a non-zero status, whatever it printed.

  10. What is the first line of the compiler output for this program? module Main (main) where main :: IO () main = exitWith (ExitFailure 3)

    Answer

    error: [GHC-88464]

    exitWith is not in scope without the System.Exit import, which GHC reports as a variable not in scope, GHC-88464.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev