File: sml.tex

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (277 lines) | stat: -rw-r--r-- 9,357 bytes parent folder | download | duplicates (5)
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
\documentclass{article}
  \usepackage{sml}
  \title{Package {\tt sml}}
  \author{Allen Leung}
\begin{document}
  \maketitle
\section{Introduction}
  The \verb|sml| package defines a \verb|verbatim|-like environment
called \verb|smldisplay|
for typesetting Standard ML programs.   
Like the \verb|alltt| environment, 
backslashes `\verb|\|' and the braces \verb|{|
and \verb|}| have their usual meaning in \verb|smldisplay|, 
so it is possible to use other
macros and commands within the \verb|smldisplay| environment.
Meta-characters such as \verb|#|, \verb|%|, 
\verb|$|, \verb|_| and \verb|^| are disabled and appears verbatim.  

To enter math mode, the user can use \verb|\(| \ldots \verb|)| or
\verb|\[| \ldots \verb|\]|.  But unlike the \verb|alltt| environment,
the superscripts \verb|^| and subscripts \verb|_| characters
are available inside math mode. 

The character \verb|'| is interpreted as the beginning of a ML 
type variable.  Type variables are typeset 
in italics within the \verb|smldisplay| environment.  For example, 
\begin{verbatim}
\begin{smldisplay} 
   datatype 'a tree = EMPTY
                    | NODE of 'a * 'a tree list
\end{smldisplay}
\end{verbatim}
is typeset as follows:
\begin{smldisplay} 
   datatype 'a tree = EMPTY 
                    | NODE of 'a * 'a tree list
\end{smldisplay}

The environment \verb|smlboxeddisplay| is similar to \verb|smldisplay|
except that a box is also drawn around the displayed program.
For example, if we write:
\begin{verbatim}
\begin{smlboxeddisplay} 
   datatype 'a tree = EMPTY 
                    | NODE of 'a * 'a tree list
\end{smlboxeddisplay}
\end{verbatim}
we get:
\begin{smlboxeddisplay} 
   datatype 'a tree = EMPTY 
                    | NODE of 'a * 'a tree list
\end{smlboxeddisplay}

\subsection{Highlighting keywords}
A similar environment, called \verb|smldisp|, can be used to highlight
all SML keywords.   However, math mode and other macros are {\em unavailable}
in this environment.  For example, in \verb|smldisp| we can write:
\begin{verbatim}
\begin{smldisp} 
   (* A n-ary tree *)
   datatype 'a tree = EMPTY 
                    | NODE of 'a * 'a tree list
   (* Flatten a tree as a list in preorder *)
   fun flatten(EMPTY) = []
     | flatten(NODE(x,children)) = [x] @ List.concat(map flatten children)
\end{smldisp}
\end{verbatim}
and get the following result:
\begin{smldisp} 
   (* A n-ary tree *)
   datatype 'a tree = EMPTY 
                    | NODE of 'a * 'a tree list
   (* Flatten a tree as a list in preorder *)
   fun flatten(EMPTY) = []
     | flatten(NODE(x,children)) = [x] @ List.concat(map flatten children)
\end{smldisp}

Note that the keywords ``datatype'' and ``of'' have been typeset as
\Sml{datatype} and \Sml{of}.  Furthermore, comments are typeset
in small italics font.   

The following macros control how keywords and comments are typeset
in this environment:
\begin{verbatim}
 \newcommand{\makeSmlKeyword}[1]{{\bf #1}}
 \newcommand{\smlCommentSize}{\small}
 \newcommand{\smlCommentFont}{\it}
 \newcommand{\BeginSmlComment}{\begingroup\smlCommentSize\smlCommentFont}
 \newcommand{\EndSmlComment}{\endgroup}
\end{verbatim}
These can be redefined by the user if necessary.

\subsection{Type Variable Translations}
It is possible to define type variable translations for
\verb|smldisplay| and \verb|smldisp| environments.  For example, 
if we write:
\begin{verbatim}
   \smlTypeVar{a}{\(\alpha\)}
   \smlTypeVar{foo}{\(\underline\beta\)}
   \begin{smldisplay} 
      datatype 'a tree = EMPTY | NODE of 'a * 'a tree list
      type 'foo foo = ('foo * 'foo) tree
      type 'c seq = 'c list
   \end{smldisplay}
\end{verbatim}
we get:
   \smlTypeVar{a}{\(\alpha\)}
   \smlTypeVar{foo}{\(\underline\beta\)}
   \begin{smldisplay} 
      datatype 'a tree = EMPTY | NODE of 'a * 'a tree list
      type 'foo foo = ('foo * 'foo) tree
      type 'c seq = 'c list
   \end{smldisplay}

Note that all occurrances of \verb|'a| has been translated into
$\alpha$, while all occurrances of \verb|'foo| has been translated
into $\underline\beta$.

A type variable translation declared by \verb|smlTypeVar| is active
in its scope until it is removed by 
the macro \verb|\smlRemoveTypeVar|.
For example, we can write:
\begin{verbatim}
  \smlRemoveTypeVar{foo}
\end{verbatim}
to remove the translation on type variable \verb|'foo|.


\subsection{{\tt $\backslash${verb}}-like macros}
A \verb|\verb|-like macro called \verb|\sml| is available for typesetting
short SML program fragments within running text.  
For example, we can write the following:
\begin{verbatim}
\begin{quotation}
   The datatype \sml{'a tree} implements a polymorphic n-ary tree.
The function \sml{val rev : 'a tree -> 'a list} flattens a tree into a list.
\end{quotation}
\end{verbatim}
and obtain:
\begin{quotation}
   The datatype \sml{'a tree} implements a polymorphic n-ary tree.
The function \sml{val rev : 'a tree -> 'a list} flattens a tree into a list.
\end{quotation}
The macro \verb|\sml| behaves very much like the \verb|smldisplay|
environment, except that newlines are not interpreted verbatim.

Similarly, there is a \verb|\verb|-like macro called \verb|\Sml| that
behaves like the \verb|smldisp| environment.  For example, writing
\begin{verbatim}
\begin{quotation}
   The datatype \Sml{'a tree} implements a polymorphic n-ary tree.
The function \Sml{val rev : 'a tree -> 'a list} flattens a tree into a list.
\end{quotation}
\end{verbatim}
we obtain:
\begin{quotation}
   The datatype \Sml{'a tree} implements a polymorphic n-ary tree.
The function \Sml{val rev : 'a tree -> 'a list} flattens a tree into a list.
\end{quotation}

\subsection{Changing the Fonts}
The macros \verb|\smlFont| and \verb|\smlTypeVarFont| 
define the fonts used for typesetting ML text and type variables.  
They are predefined as follows:
\begin{verbatim}
   \newcommand{\smlFont}{\verbatim@font}
   \newcommand{\smlTypeVarFont}{\it}
\end{verbatim}
Furthermore, the default method of typesetting a type variable
is defined as: 
\begin{verbatim}
   \newcommand{\makeSmlTypeVar}[1]{'{\smlTypeVarFont #1}}
\end{verbatim}
These can be overridden by the user if desired.

\subsection{Enabling {\tt \$}}
By default, the math shift character \verb|$| 
is disabled within the environment \verb|smldisplay|
and the macro \verb|sml|.  It is possible to
enable this character by declaring:
\begin{verbatim}
   \smlDollarOn
\end{verbatim}
\noindent in the prologue of a document.
For example, we can write:
\begin{verbatim}
\smlDollarOn
\begin{smldisplay}
   datatype 'a tree = EMPTY | NODE of 'a * 'a tree list
   \textrm{A balanced tree with $n$ nodes has height $O(\log n)$}
\end{smldisplay}
\end{verbatim}
and obtain:
\smlDollarOn
\begin{smldisplay}
   datatype 'a tree = EMPTY | NODE of 'a * 'a tree list
   \textrm{A balanced tree with $n$ nodes has height $O(\log n)$}
\end{smldisplay}

To turn off the math shift character \verb|$|, we can
write 
\begin{verbatim}
   \smlDollarOff
\end{verbatim}

\subsection{Numbered Program Listings}

Numbered program listings can be displayed using the
\verb|smllisting| environment, which behaves exactly like
\verb|smldisplay| except that every line is prefixed by a
line number.  For example, when we write:
\begin{verbatim}
\smlTypeVar{n}{\(\alpha\)}
\smlTypeVar{e}{\(\beta\)}
\smlTypeVar{g}{\(\gamma\)}
\begin{smllisting}{1}{1}
signature SINGLE_SOURCE_SHORTEST_PATHS =
sig

   val single_source_shortest_paths :
                 \{ weight : 'e Graph.edge -> 'w,
                   <      : 'w * 'w -> bool,
                   +      : 'w * 'w -> 'w,
                   zero   : 'w,
                   inf    : 'w
                 \} ->
                 ('n,'e,'g) Graph.graph ->
                 Graph.node_id ->
                 \{ dist : 'w Array.array,
                   pred :  Graph.node_id Array.array
                 \}
end
\end{smllisting}
\end{verbatim}
\noindent we get:
\smlTypeVar{n}{\(\alpha\)}
\smlTypeVar{e}{\(\beta\)}
\smlTypeVar{g}{\(\gamma\)}
\begin{smllisting}{1}{1}
signature SINGLE_SOURCE_SHORTEST_PATHS =
sig

   val single_source_shortest_paths :
                 \{ weight : 'e Graph.edge -> 'w,
                   <      : 'w * 'w -> bool,
                   +      : 'w * 'w -> 'w,
                   zero   : 'w,
                   inf    : 'w
                 \} ->
                 ('n,'e,'g) Graph.graph ->
                 Graph.node_id ->
                 \{ dist : 'w Array.array,
                   pred :  Graph.node_id Array.array
                 \}
end
\end{smllisting}

The environment \verb|smllisting| requires two numeric parameters.
The first parameter determines the initial line number of the
listing, while the second parameter determines how often the line
number should be printed.  For example, if the second parameter
is 2, then the line number appears every two lines. 

The environment \verb|smlboxedlisting| is similar to 
\verb|smllisting| except that a box is also drawn around the program
listing.

The following macros control how the numbers are displayed
\begin{verbatim}
\newcommand{\smlNumberFont}{\smlFont}
\newcommand{\smlNumberStyle}[1]{\arabic{#1}}
\end{verbatim}
The first macro \verb|\smlNumberFont| controls the font used
for line numbering, which by default is \verb|\tt|.  
The second macro \verb|\smlNumberStyle| displays the line count as
arabic numerals.  
\end{document}