Haskell Code Blocks in Babel
Babel support for Haskell
Introduction
Haskell describes itself as an advanced, purely functional programming language with declarative, statically typed code. Its design makes possible formal mathematical proof of correctness for a function.
Babel provides a facility to develop literate Haskell programs.
Requirements and Setup
Babel support for Haskell requires a working Haskell installation. The default value of the variable org-babel-haskell-compiler
is "ghc", which assumes a command in the path. This value can be customized to another command in the path or to an absolute path name. The value can also include parameters, e.g., "ghc -v".
Babel support for Haskell also requires Haskell-mode for Emacs version 25.1 or later. Haskell-mode provides an excellent editing, debugging, and development experience.
Optionally, lhs2tex
, a preprocessor for typesetting Haskell sources with LaTeX can be installed. It intends to be a tool that works on literate Haskell documents where the non-Haskell parts form a valid LaTeX document.
Babel can be configured to use Haskell by creating an entry for it in org-babel-load-languages
. Typically, org-babel-load-languages
will contain many entries. The example below omits other languages.
(org-babel-do-load-languages 'org-babel-load-languages '((haskell . t)))
Babel Features for Haskell Code Blocks
Header Arguments
Babel support for Haskell defines one Haskell-specific header argument, :compile
. When set to yes
, the source code block is sent to the Haskell compiler, otherwise the source code block is sent to the Haskell interpreter. Note: Haskell code that runs in the interpreter might not compile without changes (see the examples).
By default, the header argument :padline
is set to no
, which inhibits writing blank lines between tangled source code blocks.
Sessions
Babel supports Haskell sessions by default in a buffer named *ob-haskell*
. If the header argument :session
is set to none
, then the session uses a temporary buffer named *ob-haskell-tmp*
. Otherwise, the session buffer is named according to the value associated with :session
.
Export to lhs2tex
Babel can export Haskell code blocks to a file suitable for input to the lhs2tex
preprocessor. The interactive function org-babel-haskell-export-to-lhs
will export to a file named for the buffer, <buffer-file-name>.lhs
, ignoring all Babel literate programming constructs. When the function is called with a prefix argument, then it will call lhs2tex
to create the file <buffer-file-name>.tex
.
Examples of Use
Here is the obligatory "Hello world!" example in compiled Haskell.
#+name: haskell-compile-hello-world #+begin_src haskell :compile yes :results output main :: IO () main = putStrLn "Hello world!" #+end_src #+RESULTS: haskell-compile-hello-world : Hello world!
And, here it is in interpreted Haskell.
#+name: haskell-interpret-hello-world #+begin_src haskell putStrLn "Hello world!" #+end_src #+RESULTS: haskell-interpret-hello-world : Hello world!