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}
Comments and literate files in the viewer
A line comment is -- followed by no symbol character. The viewer reads a run
of symbol characters (the ASCII ones !#$%&*+./<=>?@\^|-~: and any
Unicode symbol or punctuation mark except the special characters, _,
" and ') as one operator, and it is a comment only when the run
is nothing but two or more dashes. So a --> b, c |-- d,
x --| y and (-->) are operators, while -- note,
---- heading, -- | doc and a--b are comments that
run to the end of the line.
A .lhs file is told by what it contains. A file with a \begin{code}
line, every one closed by an \end{code} line, is LaTeX style: the lines between
them are Haskell, the two marker lines are coloured as directives and every other line as a
comment. A file with lines that start with > is Bird style if no such line touches
a non-blank line that does not start with > (the Report calls that an error): the
> is coloured as a directive, the rest of the line as Haskell, and the other lines as
comments. A block comment {- -} in the code may run over the lines of text between.
A literate file with no code, an unclosed block or a program line next to a comment line is
shown plain, as one family variant named “no code found”; the two styles are the
variants “Bird style” and “LaTeX style”.
Not described
Known, not yet covered
- Reference: this page is an overview only and 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, and the history of the language’s versions. The Haskell 2010 Language Report and the GHC User’s Guide describe all of these.
- Viewer: literate files are read for Bird tracks and LaTeX style only. A
\begin{spec}block (lhs2TeX), Agda’s.lagdaand a Bird-style line that starts with<are not code to the viewer, and a literate file that mixes the two styles is read as LaTeX style. The commentary of a literate file is coloured as a comment as a whole, with no LaTeX or Markdown colouring inside it. - Viewer: a word that starts with a capital is a type, so data constructors (
Just,A) and module names (Data,Map) are coloured as types. OnlyTrueandFalseare literals. Function and variable names are plain, and so are most extension words (pattern,stock,anyclass,via) because they are ordinary names elsewhere. - Viewer: the layout rule is not applied, no module is parsed, imports are not followed and no type, scope or syntax errors are reported. Only the first 1 MiB of a file is read and coloured.
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