-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhaskell-example.tex
308 lines (263 loc) · 9.55 KB
/
haskell-example.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
%%% This is a LaTeX2e document.
%%%
%%% It documents the use of `haskell.sty'.
%%%
%%% Manuel M. T. Chakravarty <[email protected]> [1998..2013]
%%%
%%% This file is available under a BSD3 licence - see the LICENSE file.
\documentclass[a4paper]{article}
\usepackage{a4}
\usepackage{haskell}
% backslash hack (uses the answer to Exercise 7.5 from Knuth's TeXbook)
%
{
\catcode`/=0
\catcode`\\=13
/gdef/backslashchar{/string\}
}
\newcommand{\cmd}[1]{\texttt{\backslashchar#1}}
\begin{document}
\title{Haskell for \LaTeX2e}
\author{%
Manuel M. T. Chakravarty\\
School of Computer Science and Engineering\\
University of New South Wales, Australia\\[1ex]
\texttt{[email protected]}\\
\texttt{www.cse.unsw.edu.au/\string~chak}
}
\date{Version 1.0e (for Version 1.0e of the style file)}
\maketitle
\section{What's it good for?}
Setting large pieces of code in \texttt{verbatim} is ugly and complicates
the use of subscripts or to set comments in a proportional type face. On
the other hand, the use of \TeX's math mode requires
additional macros to achieve proper kerning in multi-letter identifiers and to
typeset application by juxtaposition. The \texttt{haskell} style provides
environments and macros that simplify setting Haskell~\cite{haskell,haskell14}
programs in \LaTeX~\cite{lamport:latex}. While the style is specifically
geared towards Haskell, it should also be useful for other functional
languages like ML or Nesl.
The famous \<map\> function can be set as follows:
%
\begin{haskell*}
map &::& (\alpha\to\beta)\to[\alpha]\to[\beta]
&\hscom{type assertion}\\
map f [] &=& []
&\hscom{\<[]\> is the empty list}\\
map f (x:xs) &=& f x : map f xs
&\hscom{\<:\> is the infix list constr.}
\end{haskell*}
%
Under this definition, \<map (+1) [1, 2, 3]\> evaluates to \<[2, 3, 4]\>.
The previous example was set using the following input:
%
\begin{quote}
\begin{verbatim}
The famous \<map\> function can be set as follows:
%
\begin{haskell*}
map &::& (\alpha\to\beta)\to[\alpha]\to[\beta]
&\hscom{type assertion}\\
map f [] &=& []
&\hscom{\<[]\> is the empty list}\\
map f (x:xs) &=& f x : map f xs
&\hscom{\<:\> is the infix list constr.}
\end{haskell*}
%
Under this definition, \<map (+1) [1, 2, 3]\>
evaluates to \<[2, 3, 4]\>.
\end{verbatim}
\end{quote}
%
There are a number of points worth noting. In a Haskell phrase, math mode
commands, such as \cmd{alpha} are available, but nevertheless, space
characters can be used to denote juxtaposition of expressions (i.e.,
application in Haskell) and multi-letter identifiers are set properly.
Compare $ffoo$ and \<ffoo\>, set by \verb|$ffoo$| and \verb|\<ffoo\>|,
respectively.
\textit{The examples in the following sections do not provide the \LaTeX\
sources used to typeset the examples in the formatted version. Please look
at the source file to see the code.}
\section{Basic Usage}
\subsection{Haskell Mode}
The environment \texttt{haskell} as well as the commands \cmd< and \cmd> enter
Haskell mode. Keywords can be set in \textbf{bold face} using \cmd{hskwd}.
Within a \texttt{haskell} environment, columns are defined using the alignment
character \texttt{\&}. Usually, it is not necessary to wrap relations into
alignment characters, but instead a single alignment to the right of the
relational symbol suffices (just like AMS\LaTeX's \texttt{align}
environment). However, sometimes we want to align relational symbols of
different size (i.e., the \<::\> of a type assertion and the \<=\> of the
corresponding equations). In this case, the star form \texttt{haskell*}
should be used, which centers the material enclosed between the first and
second alignment character.
\subsection{Comments}
The unary command \cmd{hscom} is used to set single line comments.
%
\begin{haskell*}
\hskwd{data} Maybe \alpha
& = & Nothing &\hscom{No result}\\
& | & Just \alpha &\hscom{Just a value of type \<\alpha\>}
\end{haskell*}
%
The second line shows that in a comment, you can again enter Haskell mode.
This is particularly important when identifiers appear in comments or when you
want to cite some program fragement, as the font choice in these cases should
be identical to the fonts in the displayed program.
Sometimes, we like to omit some details of an algorithm and instead give an
informal description. This is achieved with \cmd{hsinf}.
%
\begin{haskell}
main &= \hsdo{
config \hsfrom \hsinf{read the configuration file}\\
result \hsfrom \hsinf{process the input according to \<config\>}\\
putStr result
}
\end{haskell}
\subsection{Features from Math Mode}
You can use all sorts of symbols, subscripts, and superscripts from \LaTeX's
math mode in Haskell mode. This includes \TeX's relational symbols, for
example, $\geq$ or $\not=$, and greek characters for type variables.
\subsection{Aligned Material}
Larger function bodies usually require the use of aligned subphrases to
visually structure the definition. The command \cmd{hsalign} generates an
alignment with an arbitrary number of columns. Consider the function
\<transpose\> from Haskell's standard library:
%
\begin{haskell*}
transpose &::& [[a]]\to [[a]]\\
transpose &=&
\hsalign{
foldr\\
\quad(\lambda xs xss\to zipWith (:) xs (xss\hsapp repeat []))\\
\quad[]
}
\end{haskell*}
For several constructs that require alignments, there are predefined macros
internally using \cmd{hsalign}. For example for \<\hskwd{let}\> expressions,
we have \cmd{hslet}:
%
\begin{haskell}
foo a = \hslet{
x &= 1\\
y &= 2
}{%
x+y
}
\end{haskell}
%
Another macros is \cmd{hsif} for if-then-else expression. In the following,
example, we see that aligned subphrases can be nested:
%
\begin{haskell}
foo a = \hsif{a == 0}{%
\hslet{%
x = a + 1
}{%
x}
}{%
a}
\end{haskell}
Aligned material frequently gets big enough to require to break the
surrounding alignment. This achieved with \cmd{hsbody}:
%
\begin{haskell*}
calculateNextPos &::& Pos\to Mover\to State\to [Pos]\\
calculateNextPos oldPos move state &=&\relax
\hsbody{%
map &(\lambda (lowerHalf, upperHalf)\to (upperHalf, lowerHalf)\\
&(compare oldPos (move state))
}
\end{haskell*}
A canned use of \cmd{hsbody} is provided by \cmd{hswhere} for
\<\hskwd{where}\> clauses:
%
\begin{haskell*}
partition &::& (a\to Bool)\to [a]\to ([a],[a])\\
partition p xs &=& foldr select ([],[]) xs
\hswhere{%
select x (ts,fs) &\mid p x &= (x:ts,fs)\\
&\mid otherwise &= (ts, x:fs)
}
\end{haskell*}
\section{Advanced Features}
If you need to set complex documents, you may want to know some of the more
subtle points of the Haskell mode. Generally, whenever you attempt to use
Haskell mode in sophistcated ways or try to define your own commands, keep in
mind that spaces matter in Haskell mode, i.e., Haskell mode uses \TeX's
\cmd{obeyspaces}. This changes \TeX's behaviour in subtle ways.
\subsection{Defining Commands}
Commands that are used in Haskell mode should be defined with \cmd{hscommand}
instead of \cmd{newcommand}. The arguments expected by \cmd{hscommand} are
the same as those for \cmd{newcommand} (however, there is no \texttt*-version
of \cmd{hscommand}). \cmd{hscommand} works slightly different, for example,
with respect to processing spaces in the body of the command definition, to
allow application by juxtaposition.
\subsection{Entering ``Real'' Math Mode}
Haskell mode is rather similar to math mode, to allow using special
characters, subscripts, and so forth. However, sometimes, it is desirable to
enter plain math mode. Within a Haskell phrase, the commands \cmd( and \cmd)
may be used to enter \LaTeX's standard math mode. This is often helpful, when
defining macros\footnote{Sometimes it may be necessary to prefix such a use
of \cmd( by \cmd{relax}, when the macro is used behind a \texttt{\string&}.}
and, for example, the macro \cmd{hscom} is defined this way.
\subsection{Use in Transformation Rules}
For example in transformation rules, we want complex expressions, such as
let-in constructs to be vertically centered. This is achieved by specifying
an optional argument giving the alignment, for example,
\cmd{hslet[c]\{$\cdots$\}}.
%
\begin{haskell}
\hslet[c]{%
x &= e
}{C[x]}
\Longrightarrow
C[e]
\end{haskell}
%
By default boxes are vertically aligned by the baseline of the first line
(corresponding to the option \texttt{t}). Using \texttt{c} the alignment is
in the center and with \texttt{b} it is at the baseline of the last line.
\subsection{\TeX's Formula Parser}
\TeX\ is capable of understanding some of the structure of formulae in math
mode. The parsed structure influences the space inserted between the elements
of a formula. Compare
%
\begin{quote}
\begin{tabular}{l@{~$\Longrightarrow$~}l}
\texttt{3+4} & \(3+4\)\quad and\\
\texttt{3\{+\}4} & \(3{+}4\)
\end{tabular}
\end{quote}
%
In the first case, \TeX\ groks that \texttt{+} is a binary operator applied to
$3$ and $4$, but not in the second. Details are provided in \emph{The
\TeX{}book}~\cite{knuth:tex}.
%\section{Tests}
%\begin{haskell}
%foo x_1 &= x_1 + 10 &\hscom{\<x_1\> is increased by \<10\>}
%\end{haskell}
%\begin{haskell}
% foo x &=& \hslet{
% y &=\hsif{x == 0}{bar 1}{%
% \hslet{%
% z & = foobar x 5 + x
% }{
% foo' z}}
% }{
% bar' y}
% \\
% foo x &=& \hslet{
% y &=\hsif{x == 0}{bar 1}{%
% \hslet{%
% z & = foobar x 5 + x
% }{
% foo' z}}
% }{
% bar' y}
%\end{haskell}
\section{BSD3}
The Haskell style is available under a BSD3 license.
\bibliographystyle{alpha}
\bibliography{haskell-example}
\end{document}