Programming · Concepts · Compiler
What is a compiler?
You write a program in a language you can read: words, indentation, names that mean something. The processor in your machine understands none of that — it runs raw binary instructions. The tool that bridges that gap is the compiler. This guide explains what a compiler is, the stages it works through, how it differs from an interpreter, and where a compiler like GHC fits when you write Haskell.
The short definition
A compiler is a program that translates source code written in one language into another form — usually the machine code a computer can run directly. You hand it the text files you wrote, and it produces an executable (or an intermediate format) that the machine, or a runtime, can carry out. The translation happens once, ahead of time, and the result runs on its own without the compiler being present.
How a compiler works, stage by stage
A compiler does not translate in a single leap. It works through a pipeline of stages, each handing its result to the next:
- Lexing (tokenizing): the source text is broken into small units — keywords, names, numbers, symbols — called tokens.
- Parsing: the tokens are arranged into a tree that reflects the structure of the program, following the language's grammar.
- Semantic analysis: the compiler checks that the program makes sense — that names are defined, that types line up, that the rules of the language hold. This is where many of your errors are caught.
- Optimization: the compiler rewrites the program into a faster or smaller equivalent without changing what it does.
- Code generation: finally it emits the target output — machine code, or an intermediate representation that another tool will finish.
If any stage finds a problem, you get a compile error and no program is produced. That is the trade the compiler makes: it refuses to build broken code, so a class of mistakes is caught before the program ever runs.

Compiler vs interpreter
The other common approach is an interpreter, which reads your source code and carries it out directly, line by line, each time the program runs — no separate executable is built ahead of time. A compiler translates the whole program once, up front, into a form the machine runs on its own.
The practical difference: compiled programs usually start and run faster, because the translation and optimization already happened, and errors surface at compile time. Interpreted programs are quicker to try out and more flexible, but do the translation work every run. Many real toolchains mix both — they compile to an intermediate form and then run that on a virtual machine.
Where GHC and Haskell fit
When you write Haskell, the compiler you reach for is GHC (the Glasgow Haskell Compiler). It reads your .hs source files, type-checks them thoroughly, optimizes the result, and produces a native executable you can run on its own. Haskell's strong type system means GHC's semantic-analysis stage is doing a lot of work for you: many bugs are reported as compile errors instead of crashing at run time. If you want the full walkthrough of using it, see our GHC compiler guide and how to install Haskell with GHCup.
The commands you actually meet
- Compile a single file: a tool like
ghc Main.hsreads your source and produces an executable. - Build a project: build tools such as Cabal or Stack drive the compiler across many files and dependencies for you.
- Type-check without a full build: many compilers offer a faster mode that checks for errors without emitting a binary.
Most of the time you don't call the compiler by hand — your build tool or editor does it for you and shows you the errors. But knowing what it is doing underneath makes those errors far easier to read.
The honest trade-offs
Compilers add a step: you change code, you wait for it to build, then you run it. On a big project that wait can be noticeable, which is why fast compilers and incremental builds matter. The payoff is real: errors caught early, optimized output, and a single executable you can ship without shipping your toolchain. For a strongly typed language like Haskell, that up-front checking is much of the appeal — the compiler is a safety net, not just a translator.
Frequently asked questions
What is a compiler in simple terms?
A compiler is a program that translates the source code you write into a form the computer can run — usually machine code. You give it your text files and it produces an executable that runs on its own, without the compiler being present. The translation happens once, ahead of time.
What is the difference between a compiler and an interpreter?
A compiler translates your whole program once, ahead of time, into a runnable form, so the result starts and runs quickly. An interpreter reads and carries out your source code directly each time the program runs, with no separate build step. Compiled programs are usually faster; interpreted ones are quicker to try and more flexible.
What are the stages of a compiler?
A compiler typically works through lexing (splitting the text into tokens), parsing (building a structure tree), semantic analysis (checking names and types), optimization (making the program faster or smaller), and code generation (emitting the final machine code or intermediate form). An error in any stage stops the build.
Is GHC a compiler?
Yes. GHC, the Glasgow Haskell Compiler, is the standard compiler for Haskell. It reads your .hs source files, type-checks and optimizes them, and produces a native executable you can run on its own. Haskell's strong type system means GHC catches many bugs as compile errors before the program ever runs.
A Linux box to build and run your code
Compiling a real project — building GHC, type-checking a large Haskell codebase, then running the executable — is much easier on a proper Linux machine than a laptop. A cloud server gives you root access to install a compiler toolchain and build your app in a clean environment. Infomaniak — a Swiss, privacy-respecting provider — offers cloud servers you can set up and reach over SSH.
See Infomaniak Cloud →Affiliate link — it supports these free guides.
Browse more clear explainers in our guides index.