| 1 |
\documentclass{article} |
| 2 |
\usepackage{sml} |
| 3 |
\title{Package {\tt sml}} |
| 4 |
\author{Allen Leung} |
| 5 |
\begin{document} |
| 6 |
\maketitle |
| 7 |
\section{Introduction} |
| 8 |
The \verb|sml| package defines a \verb|verbatim|-like environment |
| 9 |
called \verb|smldisplay| |
| 10 |
for typesetting Standard ML programs. |
| 11 |
Like the \verb|alltt| environment, |
| 12 |
backslashes `\verb|\|' and the braces \verb|{| |
| 13 |
and \verb|}| have their usual meaning in \verb|smldisplay|, |
| 14 |
so it is possible to use other |
| 15 |
macros and commands within the \verb|smldisplay| environment. |
| 16 |
Meta-characters such as \verb|#|, \verb|%|, |
| 17 |
\verb|$|, \verb|_| and \verb|^| are disabled and appears verbatim. |
| 18 |
|
| 19 |
To enter math mode, the user can use \verb|\(| \ldots \verb|)| or |
| 20 |
\verb|\[| \ldots \verb|\]|. But unlike the \verb|alltt| environment, |
| 21 |
the superscripts \verb|^| and subscripts \verb|_| characters |
| 22 |
are available inside math mode. |
| 23 |
|
| 24 |
The character \verb|'| is interpreted as the beginning of a ML |
| 25 |
type variable. Type variables are typeset |
| 26 |
in italics within the \verb|smldisplay| environment. For example, |
| 27 |
\begin{verbatim} |
| 28 |
\begin{smldisplay} |
| 29 |
datatype 'a tree = EMPTY |
| 30 |
| NODE of 'a * 'a tree list |
| 31 |
\end{smldisplay} |
| 32 |
\end{verbatim} |
| 33 |
is typeset as follows: |
| 34 |
\begin{smldisplay} |
| 35 |
datatype 'a tree = EMPTY |
| 36 |
| NODE of 'a * 'a tree list |
| 37 |
\end{smldisplay} |
| 38 |
|
| 39 |
The environment \verb|smlboxeddisplay| is similar to \verb|smldisplay| |
| 40 |
except that a box is also drawn around the displayed program. |
| 41 |
For example, if we write: |
| 42 |
\begin{verbatim} |
| 43 |
\begin{smlboxeddisplay} |
| 44 |
datatype 'a tree = EMPTY |
| 45 |
| NODE of 'a * 'a tree list |
| 46 |
\end{smlboxeddisplay} |
| 47 |
\end{verbatim} |
| 48 |
we get: |
| 49 |
\begin{smlboxeddisplay} |
| 50 |
datatype 'a tree = EMPTY |
| 51 |
| NODE of 'a * 'a tree list |
| 52 |
\end{smlboxeddisplay} |
| 53 |
|
| 54 |
\subsection{Highlighting keywords} |
| 55 |
A similar environment, called \verb|smldisp|, can be used to highlight |
| 56 |
all SML keywords. However, math mode and other macros are {\em unavailable} |
| 57 |
in this environment. For example, in \verb|smldisp| we can write: |
| 58 |
\begin{verbatim} |
| 59 |
\begin{smldisp} |
| 60 |
(* A n-ary tree *) |
| 61 |
datatype 'a tree = EMPTY |
| 62 |
| NODE of 'a * 'a tree list |
| 63 |
(* Flatten a tree as a list in preorder *) |
| 64 |
fun flatten(EMPTY) = [] |
| 65 |
| flatten(NODE(x,children)) = [x] @ List.concat(map flatten children) |
| 66 |
\end{smldisp} |
| 67 |
\end{verbatim} |
| 68 |
and get the following result: |
| 69 |
\begin{smldisp} |
| 70 |
(* A n-ary tree *) |
| 71 |
datatype 'a tree = EMPTY |
| 72 |
| NODE of 'a * 'a tree list |
| 73 |
(* Flatten a tree as a list in preorder *) |
| 74 |
fun flatten(EMPTY) = [] |
| 75 |
| flatten(NODE(x,children)) = [x] @ List.concat(map flatten children) |
| 76 |
\end{smldisp} |
| 77 |
|
| 78 |
Note that the keywords ``datatype'' and ``of'' have been typeset as |
| 79 |
\Sml{datatype} and \Sml{of}. Furthermore, comments are typeset |
| 80 |
in small italics font. |
| 81 |
|
| 82 |
The following macros control how keywords and comments are typeset |
| 83 |
in this environment: |
| 84 |
\begin{verbatim} |
| 85 |
\newcommand{\makeSmlKeyword}[1]{{\bf #1}} |
| 86 |
\newcommand{\smlCommentSize}{\small} |
| 87 |
\newcommand{\smlCommentFont}{\it} |
| 88 |
\newcommand{\BeginSmlComment}{\begingroup\smlCommentSize\smlCommentFont} |
| 89 |
\newcommand{\EndSmlComment}{\endgroup} |
| 90 |
\end{verbatim} |
| 91 |
These can be redefined by the user if necessary. |
| 92 |
|
| 93 |
\subsection{Type Variable Translations} |
| 94 |
It is possible to define type variable translations for |
| 95 |
\verb|smldisplay| and \verb|smldisp| environments. For example, |
| 96 |
if we write: |
| 97 |
\begin{verbatim} |
| 98 |
\smlTypeVar{a}{\(\alpha\)} |
| 99 |
\smlTypeVar{foo}{\(\underline\beta\)} |
| 100 |
\begin{smldisplay} |
| 101 |
datatype 'a tree = EMPTY | NODE of 'a * 'a tree list |
| 102 |
type 'foo foo = ('foo * 'foo) tree |
| 103 |
type 'c seq = 'c list |
| 104 |
\end{smldisplay} |
| 105 |
\end{verbatim} |
| 106 |
we get: |
| 107 |
\smlTypeVar{a}{\(\alpha\)} |
| 108 |
\smlTypeVar{foo}{\(\underline\beta\)} |
| 109 |
\begin{smldisplay} |
| 110 |
datatype 'a tree = EMPTY | NODE of 'a * 'a tree list |
| 111 |
type 'foo foo = ('foo * 'foo) tree |
| 112 |
type 'c seq = 'c list |
| 113 |
\end{smldisplay} |
| 114 |
|
| 115 |
Note that all occurrances of \verb|'a| has been translated into |
| 116 |
$\alpha$, while all occurrances of \verb|'foo| has been translated |
| 117 |
into $\underline\beta$. |
| 118 |
|
| 119 |
A type variable translation declared by \verb|smlTypeVar| is active |
| 120 |
in its scope until it is removed by |
| 121 |
the macro \verb|\smlRemoveTypeVar|. |
| 122 |
For example, we can write: |
| 123 |
\begin{verbatim} |
| 124 |
\smlRemoveTypeVar{foo} |
| 125 |
\end{verbatim} |
| 126 |
to remove the translation on type variable \verb|'foo|. |
| 127 |
|
| 128 |
|
| 129 |
\subsection{{\tt $\backslash${verb}}-like macros} |
| 130 |
A \verb|\verb|-like macro called \verb|\sml| is available for typesetting |
| 131 |
short SML program fragments within running text. |
| 132 |
For example, we can write the following: |
| 133 |
\begin{verbatim} |
| 134 |
\begin{quotation} |
| 135 |
The datatype \sml{'a tree} implements a polymorphic n-ary tree. |
| 136 |
The function \sml{val rev : 'a tree -> 'a list} flattens a tree into a list. |
| 137 |
\end{quotation} |
| 138 |
\end{verbatim} |
| 139 |
and obtain: |
| 140 |
\begin{quotation} |
| 141 |
The datatype \sml{'a tree} implements a polymorphic n-ary tree. |
| 142 |
The function \sml{val rev : 'a tree -> 'a list} flattens a tree into a list. |
| 143 |
\end{quotation} |
| 144 |
The macro \verb|\sml| behaves very much like the \verb|smldisplay| |
| 145 |
environment, except that newlines are not interpreted verbatim. |
| 146 |
|
| 147 |
Similarly, there is a \verb|\verb|-like macro called \verb|\Sml| that |
| 148 |
behaves like the \verb|smldisp| environment. For example, writing |
| 149 |
\begin{verbatim} |
| 150 |
\begin{quotation} |
| 151 |
The datatype \Sml{'a tree} implements a polymorphic n-ary tree. |
| 152 |
The function \Sml{val rev : 'a tree -> 'a list} flattens a tree into a list. |
| 153 |
\end{quotation} |
| 154 |
\end{verbatim} |
| 155 |
we obtain: |
| 156 |
\begin{quotation} |
| 157 |
The datatype \Sml{'a tree} implements a polymorphic n-ary tree. |
| 158 |
The function \Sml{val rev : 'a tree -> 'a list} flattens a tree into a list. |
| 159 |
\end{quotation} |
| 160 |
|
| 161 |
\subsection{Changing the Fonts} |
| 162 |
The macros \verb|\smlFont| and \verb|\smlTypeVarFont| |
| 163 |
define the fonts used for typesetting ML text and type variables. |
| 164 |
They are predefined as follows: |
| 165 |
\begin{verbatim} |
| 166 |
\newcommand{\smlFont}{\verbatim@font} |
| 167 |
\newcommand{\smlTypeVarFont}{\it} |
| 168 |
\end{verbatim} |
| 169 |
Furthermore, the default method of typesetting a type variable |
| 170 |
is defined as: |
| 171 |
\begin{verbatim} |
| 172 |
\newcommand{\makeSmlTypeVar}[1]{'{\smlTypeVarFont #1}} |
| 173 |
\end{verbatim} |
| 174 |
These can be overridden by the user if desired. |
| 175 |
|
| 176 |
\subsection{Enabling {\tt \$}} |
| 177 |
By default, the math shift character \verb|$| |
| 178 |
is disabled within the environment \verb|smldisplay| |
| 179 |
and the macro \verb|sml|. It is possible to |
| 180 |
enable this character by declaring: |
| 181 |
\begin{verbatim} |
| 182 |
\smlDollarOn |
| 183 |
\end{verbatim} |
| 184 |
\noindent in the prologue of a document. |
| 185 |
For example, we can write: |
| 186 |
\begin{verbatim} |
| 187 |
\smlDollarOn |
| 188 |
\begin{smldisplay} |
| 189 |
datatype 'a tree = EMPTY | NODE of 'a * 'a tree list |
| 190 |
\textrm{A balanced tree with $n$ nodes has height $O(\log n)$} |
| 191 |
\end{smldisplay} |
| 192 |
\end{verbatim} |
| 193 |
and obtain: |
| 194 |
\smlDollarOn |
| 195 |
\begin{smldisplay} |
| 196 |
datatype 'a tree = EMPTY | NODE of 'a * 'a tree list |
| 197 |
\textrm{A balanced tree with $n$ nodes has height $O(\log n)$} |
| 198 |
\end{smldisplay} |
| 199 |
|
| 200 |
To turn off the math shift character \verb|$|, we can |
| 201 |
write |
| 202 |
\begin{verbatim} |
| 203 |
\smlDollarOff |
| 204 |
\end{verbatim} |
| 205 |
|
| 206 |
\subsection{Numbered Program Listings} |
| 207 |
|
| 208 |
Numbered program listings can be displayed using the |
| 209 |
\verb|smllisting| environment, which behaves exactly like |
| 210 |
\verb|smldisplay| except that every line is prefixed by a |
| 211 |
line number. For example, when we write: |
| 212 |
\begin{verbatim} |
| 213 |
\smlTypeVar{n}{\(\alpha\)} |
| 214 |
\smlTypeVar{e}{\(\beta\)} |
| 215 |
\smlTypeVar{g}{\(\gamma\)} |
| 216 |
\begin{smllisting}{1}{1} |
| 217 |
signature SINGLE_SOURCE_SHORTEST_PATHS = |
| 218 |
sig |
| 219 |
|
| 220 |
val single_source_shortest_paths : |
| 221 |
\{ weight : 'e Graph.edge -> 'w, |
| 222 |
< : 'w * 'w -> bool, |
| 223 |
+ : 'w * 'w -> 'w, |
| 224 |
zero : 'w, |
| 225 |
inf : 'w |
| 226 |
\} -> |
| 227 |
('n,'e,'g) Graph.graph -> |
| 228 |
Graph.node_id -> |
| 229 |
\{ dist : 'w Array.array, |
| 230 |
pred : Graph.node_id Array.array |
| 231 |
\} |
| 232 |
end |
| 233 |
\end{smllisting} |
| 234 |
\end{verbatim} |
| 235 |
\noindent we get: |
| 236 |
\smlTypeVar{n}{\(\alpha\)} |
| 237 |
\smlTypeVar{e}{\(\beta\)} |
| 238 |
\smlTypeVar{g}{\(\gamma\)} |
| 239 |
\begin{smllisting}{1}{1} |
| 240 |
signature SINGLE_SOURCE_SHORTEST_PATHS = |
| 241 |
sig |
| 242 |
|
| 243 |
val single_source_shortest_paths : |
| 244 |
\{ weight : 'e Graph.edge -> 'w, |
| 245 |
< : 'w * 'w -> bool, |
| 246 |
+ : 'w * 'w -> 'w, |
| 247 |
zero : 'w, |
| 248 |
inf : 'w |
| 249 |
\} -> |
| 250 |
('n,'e,'g) Graph.graph -> |
| 251 |
Graph.node_id -> |
| 252 |
\{ dist : 'w Array.array, |
| 253 |
pred : Graph.node_id Array.array |
| 254 |
\} |
| 255 |
end |
| 256 |
\end{smllisting} |
| 257 |
|
| 258 |
The environment \verb|smllisting| requires two numeric parameters. |
| 259 |
The first parameter determines the initial line number of the |
| 260 |
listing, while the second parameter determines how often the line |
| 261 |
number should be printed. For example, if the second parameter |
| 262 |
is 2, then the line number appears every two lines. |
| 263 |
|
| 264 |
The environment \verb|smlboxedlisting| is similar to |
| 265 |
\verb|smllisting| except that a box is also drawn around the program |
| 266 |
listing. |
| 267 |
|
| 268 |
The following macros control how the numbers are displayed |
| 269 |
\begin{verbatim} |
| 270 |
\newcommand{\smlNumberFont}{\smlFont} |
| 271 |
\newcommand{\smlNumberStyle}[1]{\arabic{#1}} |
| 272 |
\end{verbatim} |
| 273 |
The first macro \verb|\smlNumberFont| controls the font used |
| 274 |
for line numbering, which by default is \verb|\tt|. |
| 275 |
The second macro \verb|\smlNumberStyle| displays the line count as |
| 276 |
arabic numerals. |
| 277 |
\end{document} |