Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Haskell programs compiled and executed internally?

I'm having trouble understanding how Haskell (GHC) compiles programs, and how those programs are run.

  1. GHC is the canonical example of a nontrivial program written in Haskell. However, parts of GHC seem not to be written in Haskell, namely the runtime environment (in C/C--). Why is that? Performance reasons? (I am aware of this site and its friends, but cannot make much sense of them.)
  2. Speaking of the runtime environment: Why does a compiled language need one? Shouldn't the compiled program be machine code and nothing else? From what I understand, a runtime environment is somewhat similar to a virtual machine or a bytecode interpreter, that deals with some form of meta code and does the actual calculations based on that. So: what does the GHC runtime do exactly, and why is it necessary in the first place?
  3. Concerning the FFI: How are C calls handled? Initially, I thought using the FFI generates a single executable where Haskell and C are compiled together. However, I read multiple times that GHC programs kind of do a call out of the program to the C function. This is especially relevant to understand the problem the FFI has with parallel programming. So: how are FFI functions different from normal Haskell functions?
like image 869
David Avatar asked Sep 13 '12 21:09

David


People also ask

How do I compile and run a Haskell program?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)

Is Haskell compiled or interpreted?

Unlike Python, Ruby, JavaScript, Lua, and other interpreted languages, Haskell is compiled ahead-of-time, directly to native machine code. The compiler (GHC) is remarkably good at optimization and generating efficient executables.

Does Haskell have a compiler?

Getting started Like some languages Haskell can be both compiled and interpreted. The most widely used implementation of Haskell currently is GHC, which provides both an optimising native code compiler, and an interactive bytecode interpreter.

Is Haskell compiled to C?

It is all compiled [or rather, linked] together. If you write a C program, one C function can call another C function. When Haskell calls a C function, it's pretty much like any other function calling that C function.


1 Answers

To compile and execute a programming language on stock hardware you need a number of things:

  • a compiler to translate your source language into assembly code executable by the native host
  • a support library (aka runtime) for primitive language services, such as memory management, IO and thread management. Things that must be leveraged from lower-level system services.

C, Java, and GHC Haskell are examples of such systems. In the case of GHC, the entire architecture is described here. The pieces are also described individually, and in detail.

  • The compiler (written in Haskell), translates Haskell to C, assembly, LLVM bitcode and other formats. The strategy it uses is described best here: Implementing lazy functional languages on stock hardware:the Spineless Tagless G-machine.
  • The runtime services (aka "the GHC runtime") are described over several papers:

    1. The multicore garbage collector with thread-local heaps
    2. How the garbage collector services work
    3. How multithreading works in GHC
    4. How laziness is implemented
    5. How the runtime calls code in foreign languages
like image 110
Don Stewart Avatar answered Sep 20 '22 20:09

Don Stewart