Introduction
A .hs file holds one module of source code in
Haskell, a non-strict, purely functional language
with static types and type classes. A .lhs file holds the same language in
literate form, where text is commentary unless it is marked as code.
The language is defined by the
Haskell 2010 Language
Report. GHC, the main compiler, adds many
language extensions to it, which a file turns on with a LANGUAGE pragma.
Structure
A Haskell file is text in the Unicode character set. It holds a module
header, then import declarations, then top-level declarations. If the header
is left out, the file is taken to begin module Main(main) where. The module
Main must export main, which must have type IO τ
for some type τ.
module Main (main) where
import Data.List (sort)
main :: IO ()
main = print (sort [3, 1, 2])
Indentation is significant. After let, where, do
or of, the column of the next token opens a block unless an explicit
{ follows. A line at that column starts a new item, a line indented further
continues the item, and a line indented less closes the block. Tab stops are 8 columns
apart.
GHC finds an imported module by replacing the dots in its name with directory
separators, so A.B.C is looked for as A/B/C.hs or
A/B/C.lhs.
Literate files
The Report defines two styles of literate source. Both use the same file extension, and the Report advises against mixing them in one file.
In Bird style, a line whose first character is > is
program text, with the > replaced by a space. Every other line is a
comment. A program line next to a non-blank comment line is an error, which catches a
forgotten >.
This is the factorial function.
> fact :: Integer -> Integer
> fact 0 = 1
> fact n = n * fact (n-1)
In LaTeX style, program text starts on the line after one beginning
\begin{code} and ends just before the next line beginning
\end{code}. Every other line is a comment.
This is the factorial function.
\begin{code}
fact :: Integer -> Integer
fact 0 = 1
fact n = n * fact (n-1)
\end{code}
What this page does not cover
This page is an overview only. The full reference is not written yet. These parts of the language have no description here:
- Lexical structure: comments, identifiers, reserved words, operators and literals
- The exact layout algorithm
- Expressions and pattern matching
- Types, data declarations, type classes and bindings
- The module system beyond the outline above
- The Prelude, the standard libraries and basic input and output
- The foreign function interface and compiler pragmas
- GHC’s language extensions and language editions
- The history of the language’s versions
The Haskell 2010 Language Report and the GHC User’s Guide describe all of these.
Links
Software
Developers
- ghc-lib-parser – GHC’s own parser as a library (Haskell)
- tree-sitter-haskell – Haskell grammar for the Tree-sitter parser used by editors
Reference
- Haskell 2010 Language Report – the definition of the language and its standard libraries
- GHC User’s Guide – GHC’s options, language editions and extensions