| 1 |
\documentclass{article} |
| 2 |
\usepackage{alltt} |
| 3 |
\newcommand{\RewriteGen}{{\sf RewriteGen}} |
| 4 |
\newcommand{\KW}[2]{\newcommand{#1}[1]{{\bf #2}\ }} |
| 5 |
\newcommand{\END}{{\bf end}} |
| 6 |
\newcommand{\OF}{{\bf of}} |
| 7 |
\KW{\FUN}{fun} \KW{\VAL}{val} |
| 8 |
\KW{\CASE}{case} \KW{\LET}{let} |
| 9 |
\KW{\INCLUDE}{include} \KW{\SIGNATURE}{signature} \KW{\DATATYPE}{datatype} |
| 10 |
\newcommand{\SIG}{{\bf sig}} |
| 11 |
\KW{\IN}{in} |
| 12 |
\KW{\AND}{and} |
| 13 |
|
| 14 |
\title{\RewriteGen{} 1.0 Manual} |
| 15 |
\author{Allen Leung\thanks{New York University. Email:{\tt [email protected]}}} |
| 16 |
\begin{document} |
| 17 |
\maketitle |
| 18 |
\section{Introduction} |
| 19 |
Often, given a set of recursively |
| 20 |
datatype definitions, {\em natural} generalizations for |
| 21 |
functions such as \verb|map|, |
| 22 |
\verb|fold|, and \verb|app| exist, defined by the inductive structure |
| 23 |
of the datatypes. However, in Standard ML, these functions |
| 24 |
must be painstakingly written for each datatype. Furthermore, whenever |
| 25 |
the definition of the datatype changes, these must be modified to match |
| 26 |
the new definitions. This is often an error prone process. |
| 27 |
|
| 28 |
\RewriteGen\ is a source-to-source translator that automates some of the |
| 29 |
mundane bookkeeping of writing transformations on user defined datatypes. |
| 30 |
It can automatically generate rewriters, app and fold functions. |
| 31 |
|
| 32 |
Originally, the core transformation routines of \RewriteGen\ |
| 33 |
are developed for the MDGen machine description generator. |
| 34 |
But they turn out to be useful for writing MDGen itself. |
| 35 |
The result is this tool. |
| 36 |
|
| 37 |
\section{An Example} |
| 38 |
Suppose that in file \verb|"wff.sig"| |
| 39 |
we are given a datatype definition for well-formed formulas in logic: |
| 40 |
\begin{alltt} |
| 41 |
\SIGNATURE WFF = |
| 42 |
\SIG |
| 43 |
\DATATYPE wff = FALSE | TRUE | VAR \OF string |
| 44 |
| AND \OF wff * wff |
| 45 |
| OR \OF wff * wff |
| 46 |
| NOT \OF wff |
| 47 |
|
| 48 |
\VAL simplify : wff -> wff |
| 49 |
\END |
| 50 |
\end{alltt} |
| 51 |
\noindent and we want to implement the function \verb|simplify|, |
| 52 |
which simplifes complex wffs. |
| 53 |
|
| 54 |
We can do this by writing the template: |
| 55 |
\begin{alltt} |
| 56 |
\FUN simplify e = |
| 57 |
Generic.rewrite( |
| 58 |
\LET \INCLUDE "wff.sig" |
| 59 |
\FUN wff (NOT FALSE) = TRUE |
| 60 |
| wff (NOT TRUE) = FALSE |
| 61 |
| wff (NOT(NOT x)) = x |
| 62 |
| wff (AND(TRUE,x)) = x |
| 63 |
| wff (AND(x,TRUE)) = x |
| 64 |
| wff (AND(FALSE,x)) = FALSE |
| 65 |
| wff (AND(x,FALSE)) = FALSE |
| 66 |
| wff (OR(TRUE,x)) = TRUE |
| 67 |
| wff (OR(x,TRUE)) = TRUE |
| 68 |
| wff (OR(FALSE,x)) = x |
| 69 |
| wff (OR(x,FALSE)) = x |
| 70 |
\IN rewrite'wff e |
| 71 |
\END) |
| 72 |
\end{alltt} |
| 73 |
The \verb|Generic.rewrite| |
| 74 |
template will read the datatype specification from the |
| 75 |
file \verb|"wff.sig"|, |
| 76 |
then create a rewriting function for the type \verb|wff| using the |
| 77 |
normalization rules specified by the user as the function \verb|wff|. |
| 78 |
The generated rewriter |
| 79 |
will try to apply the normalization rules\footnote{Once} for each |
| 80 |
subpart of a wff. The rewriting function |
| 81 |
is called \verb|rewrite'wff|. |
| 82 |
|
| 83 |
In this example, the template is transformed into the following SML code: |
| 84 |
\begin{alltt} |
| 85 |
\FUN simplify e = |
| 86 |
\LET \FUN rewrite'wff redex = |
| 87 |
\LET \VAL redo = rewrite'wff |
| 88 |
\IN \CASE redex \OF |
| 89 |
FALSE => redex |
| 90 |
| TRUE => redex |
| 91 |
| VAR string => redex |
| 92 |
| AND(wff1, wff2) => |
| 93 |
(\CASE (rewrite'wff wff1, rewrite'wff wff2) \OF |
| 94 |
(TRUE, x) => x |
| 95 |
| (x, TRUE) => x |
| 96 |
| (FALSE, x) => FALSE |
| 97 |
| (x, FALSE) => FALSE |
| 98 |
| arg => AND arg |
| 99 |
) |
| 100 |
| OR(wff1, wff2) => |
| 101 |
(\CASE (rewrite'wff wff1, rewrite'wff wff2) \OF |
| 102 |
(TRUE, x) => TRUE |
| 103 |
| (x, TRUE) => TRUE |
| 104 |
| (FALSE, x) => x |
| 105 |
| (x, FALSE) => x |
| 106 |
| arg => OR arg |
| 107 |
) |
| 108 |
| NOT wff => |
| 109 |
(\CASE rewrite'wff wff \OF |
| 110 |
FALSE => TRUE |
| 111 |
| TRUE => FALSE |
| 112 |
| NOT x => x |
| 113 |
| arg => NOT arg |
| 114 |
) |
| 115 |
) |
| 116 |
\END |
| 117 |
\IN rewrite'wff e |
| 118 |
\END |
| 119 |
\end{alltt} |
| 120 |
\subsection{App and fold functions} |
| 121 |
|
| 122 |
App and fold functions can also be easily generated. |
| 123 |
For example, suppose we want to write a function that counts all occurrances |
| 124 |
of \verb|NOT| in a wff. This can be specified with the |
| 125 |
\verb|Generic.app| template: |
| 126 |
\begin{alltt} |
| 127 |
\FUN countNots e = |
| 128 |
\LET \VAL count = ref 0 |
| 129 |
\IN Generic.app( |
| 130 |
\LET \INCLUDE "wff.sig" |
| 131 |
\FUN wff (NOT _) = count := !count + 1 |
| 132 |
| wff _ = () |
| 133 |
\IN app'wff e; |
| 134 |
!count |
| 135 |
\END) |
| 136 |
\END |
| 137 |
\end{alltt} |
| 138 |
|
| 139 |
Alternatively, we can implement the same function |
| 140 |
functionally using the \verb|Generic.fold| template: |
| 141 |
\begin{alltt} |
| 142 |
\FUN countNots2 e = |
| 143 |
Generic.fold( |
| 144 |
\LET include "wff.sig" |
| 145 |
\FUN wff (NOT _, n) = n+1 |
| 146 |
| wff (_, n) = n |
| 147 |
\IN fold'wff(e, 0) |
| 148 |
\END) |
| 149 |
\end{alltt} |
| 150 |
|
| 151 |
Similarly, a function that enumerates all the identifiers in a wff |
| 152 |
can be written as: |
| 153 |
\begin{alltt} |
| 154 |
\FUN allVars e = |
| 155 |
Generic.fold( |
| 156 |
\LET include "wff.sig" |
| 157 |
\FUN wff (VAR v, vs) = v::vs |
| 158 |
| wff (_, vs) = vs |
| 159 |
\IN fold'wff(e, []) |
| 160 |
\END) |
| 161 |
\end{alltt} |
| 162 |
This function has type \verb|addVars : wff -> string list|. |
| 163 |
|
| 164 |
\section{Multiple Sorts} |
| 165 |
|
| 166 |
Real life applications usually make use of multiple |
| 167 |
mutually recursive datatypes. In general, mutually recursive |
| 168 |
transformations functions such as rewrite, fold, and app, can be also defined |
| 169 |
by specifying a set of rules for each of the datatypes that are involved |
| 170 |
in the recursion. |
| 171 |
|
| 172 |
For example, suppose the abstract syntax tree |
| 173 |
for a simple function programming language includes |
| 174 |
the datatypes \verb|expr| (expression), |
| 175 |
\verb|decl| (declaratons). |
| 176 |
|
| 177 |
\begin{alltt} |
| 178 |
\DATATYPE expr = |
| 179 |
LET \OF decl * expr |
| 180 |
| APPLY \OF expr * expr |
| 181 |
| ID \OF string |
| 182 |
| LAMBDA \OF string * expr |
| 183 |
| {\em others} |
| 184 |
\AND decl = |
| 185 |
VAL \OF string * expr |
| 186 |
| VALREC \OF string * expr |
| 187 |
| LOCAL \OF decl * decl |
| 188 |
| {\em others} |
| 189 |
\end{alltt} |
| 190 |
|
| 191 |
We can define a rewriter that renames every identifier |
| 192 |
from lowercase to uppercase as follows: |
| 193 |
\begin{alltt} |
| 194 |
\FUN renameToUpperCase program = |
| 195 |
Generic.rewrite( |
| 196 |
\LET {\em include datatype definitions} |
| 197 |
\FUN expr (ID x) = ID(String.map Char.toUpper x) |
| 198 |
\AND decl d = d |
| 199 |
\IN rewrite'decl program |
| 200 |
\END |
| 201 |
) |
| 202 |
\end{alltt} |
| 203 |
|
| 204 |
Note that: |
| 205 |
\begin{enumerate} |
| 206 |
\item the function \verb|decl| is necessary to tell that |
| 207 |
\RewriteGen\ that the type \verb|decl| involved in the recursion |
| 208 |
in this set of rewriting rules. |
| 209 |
\item the functions \verb|decl| and \verb|expr| must be defined |
| 210 |
mutual recursively with the \AND{} connector. |
| 211 |
\end{enumerate} |
| 212 |
|
| 213 |
\section{Lists and other Type Constructors} |
| 214 |
|
| 215 |
Suppose we have the following definition of datatype |
| 216 |
\verb|exp| (expression), where we make use of lists. |
| 217 |
\begin{alltt} |
| 218 |
\DATATYPE exp = OPER \OF string * exp list |
| 219 |
| VAR \OF string |
| 220 |
| {\em others} |
| 221 |
\end{alltt} |
| 222 |
Let's say this definition is contained in the file \verb|"exp-def.sig"|. |
| 223 |
|
| 224 |
We can define a rewriter that translates all unary expression $+e$ |
| 225 |
into $e$ as follows: |
| 226 |
\begin{alltt} |
| 227 |
\FUN transform e = |
| 228 |
Generic.rewrite |
| 229 |
( \LET \INCLUDE "exp-def.sig" |
| 230 |
\DATATYPE 'a list = nil | :: \OF 'a * 'a list |
| 231 |
\FUN list x = x |
| 232 |
\FUN exp (OPER("+",[x])) = x |
| 233 |
\IN rewrite'exp e |
| 234 |
\END |
| 235 |
) |
| 236 |
\end{alltt} |
| 237 |
Note that we have to make the definition of |
| 238 |
type \verb|list| visible\footnote{We can also import it from some file |
| 239 |
via \INCLUDE.}, and define the function \verb|list| to tell \RewriteGen{} |
| 240 |
that type \verb|list| is involved in the rewriting. |
| 241 |
|
| 242 |
In general, user defined polymorphic datatypes are also supported. |
| 243 |
We generate code by arity raising\footnote{More on this later.}. |
| 244 |
Of course, we cannot define transformations that make use of |
| 245 |
polymorphic recursion, since SML cannot type such programs. |
| 246 |
|
| 247 |
\section{Running \RewriteGen} |
| 248 |
|
| 249 |
\RewriteGen\ is a source-to-source translator. First, you should |
| 250 |
build the system by running the script \verb|"build.sml"| in this |
| 251 |
directory. This will create an SML image called \verb|rwgen.<arch>|. |
| 252 |
To run the program, type: |
| 253 |
\begin{alltt} |
| 254 |
sml @SMLload=rwgen {\em input-filename} > {\em output-filename} |
| 255 |
\end{alltt} |
| 256 |
\noindent on the Unix commmand. |
| 257 |
|
| 258 |
The input file can contain arbitrary SML code. \RewriteGen\ looks |
| 259 |
for templates like \verb|Generic.rewrite|, \verb|Generic.app|, |
| 260 |
and \verb|Generic.fold|, transform those templates, and |
| 261 |
leave the rest of the code unchanged. |
| 262 |
|
| 263 |
\section{Bugs and Missing Features} |
| 264 |
There are too many to list, but some important ones are: |
| 265 |
\begin{enumerate} |
| 266 |
\item |
| 267 |
\RewriteGen\ is understands only the syntax of SML but not its semantics, |
| 268 |
so it can get horribly confused with scoping rules and/or variable captures. |
| 269 |
|
| 270 |
\item \RewriteGen\ can get confused with \verb|infix|, \verb|infixr| |
| 271 |
declarations. Try to avoid them when possible. |
| 272 |
\item |
| 273 |
The generated code for a rewriter is not ideal. Better algorithms |
| 274 |
should be used in the future. |
| 275 |
|
| 276 |
\item |
| 277 |
Currently conditional patterns (i.e. patterns with guards) |
| 278 |
are not supported but this should be available in the next version, |
| 279 |
once I hook up the match compiler into the tool. |
| 280 |
|
| 281 |
\item |
| 282 |
Subterms built with type constructors, such as lists, are currently not |
| 283 |
handled entirely properly. |
| 284 |
|
| 285 |
\item |
| 286 |
Binary polytypic functions like \verb|equal|, or \verb|zip|, are currently |
| 287 |
missing. Also we should also be able to generate, pretty printers, |
| 288 |
parsers, pickers and unpickers. But these are currently missing. |
| 289 |
|
| 290 |
\item |
| 291 |
Ideally, there should be a generic mechanism for defining new |
| 292 |
generic templates. This is probably the best way of extending the |
| 293 |
tool. I'll consider how to add this later. |
| 294 |
\end{enumerate} |
| 295 |
|
| 296 |
\end{document} |