forked from avsm/EpiVM
/
example.tex
230 lines (167 loc) · 7.06 KB
/
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
\section{Example High Level Languages}
In this section we present compilers for three different high level
languages to demonstrate aspects of the Epic API. Firstly, we present
a compiler for the untyped $\lambda$-calculus using Higher Order
Abstract Syntax, which shows the fundamental features of Epic required
to implement a complete compiler. Secondly, we present a compiler for
\LamPi{}~\cite{simply-easy}, a dependently typed language, which shows
how Epic can handle languages with more expressive type
systems. Finally, we present a compiler for a dynamically typed
graphics language, which shows how Epic can be used for languages with
run-time type checking and which require foreign function calls.
\subsection{Untyped $\lambda$-calculus}
\subsubsection{Representation}
Our first example is an implementation of the untyped
$\lambda$-calculus, plus primitive integers and strings, and
arithmetic and string operators. The language is represented in
Haskell using higher order abstract syntax (HOAS). That is, we
represent $\lambda$-bindings (\texttt{Lam}) as Haskell functions,
using a Haskell variable name to refer to the locally bound
variable. We also include global reference (\texttt{Ref}) which refer
to top level functions, function application (\texttt{App}), constants
(\texttt{Const}) and binary operators (\texttt{Op}):
\begin{SaveVerbatim}{llang}
data Lang = Lam (Lang -> Lang)
| Ref Name
| App Lang Lang
| Const Const
| Op Infix Lang Lang
\end{SaveVerbatim}
\useverb{llang}
\noindent
Constants can be either integers or strings:
\begin{SaveVerbatim}{lconsts}
data Const = CInt Int
| CStr String
\end{SaveVerbatim}
\useverb{lconsts}
\noindent
There are infix operators for arithmetic (\texttt{Plus},
\texttt{Minus}, \texttt{Times} and \texttt{Divide}), string
manipulation (\texttt{Append}) and comparison (\texttt{Eq},
\texttt{Lt} and \texttt{Gt}). The comparison operators return an
integer --- zero if the comparison is true, non-zero otherwise:
\begin{SaveVerbatim}{lops}
data Infix = Plus | Minus | Times | Divide | Append
| Eq | Lt | Gt
\end{SaveVerbatim}
\useverb{lops}
\noindent
A complete program consists of a collection of named \texttt{Lang}
definitions:
\begin{SaveVerbatim}{lprogs}
type Defs = [(Name, Lang)]
\end{SaveVerbatim}
\useverb{lprogs}
\subsubsection{Compilation}
Our aim is to convert a collection of \texttt{Defs} into an
executable, using one of the following functions from the Epic API:
\useverb{compepic}
\noindent
Given an Epic \texttt{Program}, \texttt{compile} will generate an
executable, and \texttt{run} will generate an executable then run it.
Recall that a program is a collection of named Epic declarations:
\begin{SaveVerbatim}{eprogs}
data EpicDecl = forall e. EpicExpr e => EpicFn Name e
| ...
type Program = [EpicDecl]
\end{SaveVerbatim}
\useverb{eprogs}
Our goal, then, is to convert a \texttt{Lang} definition into
something which is an instance of \texttt{EpicExpr}. We use
\texttt{Term}, which is an Epic expression which carries a name
supply. Most of the term construction functions in the Epic API return
a \texttt{Term}.
\begin{SaveVerbatim}{buildtype}
build :: Lang -> Term
\end{SaveVerbatim}
\useverb{buildtype}
\noindent
The full implementation of \texttt{build} is given in Figure \ref{lcompile}.
In general, this is a straightforward traversal of the \texttt{Lang}
program, converting \texttt{Lang} constants to Epic constants,
\texttt{Lang} application to Epic application, and \texttt{Lang}
operators to the appropriate built-in Epic operators.
\begin{SaveVerbatim}{lcompile}
build :: Lang -> Term
build (Lam f) = term (\x -> build (f (EpicRef x)))
build (EpicRef x) = term x
build (Ref n) = ref n
build (App f a) = build f @@ build a
build (Const (CInt x)) = int x
build (Const (CStr x)) = str x
build (Op Append l r) = fn "append" @@ build l @@ build r
build (Op op l r) = op_ (eOp op) (build l) (build r)
where eOp Plus = plus_
eOp Minus = minus_
eOp Times = times_
eOp Divide = divide_
eOp Eq = eq_
eOp Lt = lt_
eOp Gt = gt_
\end{SaveVerbatim}
\codefig{lcompile}{Compiling Untyped $\lambda$-calculus}
The cases worth noting are the compilation of $\lambda$-bindings and
string concatenation. Using HOAS has the advantage that Haskell can
manage scoping, but the disadvantage that it is not straightforward to
convert the abstract syntax into another form. The Epic API also
allows scope management using HOAS, so we need to convert a function
where the bound name refers to a \texttt{Lang} value into a function
where the bound name refers to an Epic value. The easiest solution is
to extend the \texttt{Lang} datatype with an Epic reference:
\begin{SaveVerbatim}{lextend}
data Lang = ...
| EpicRef Expr
build (Lam f) = term (\x -> build (f (EpicRef x)))
\end{SaveVerbatim}
\useverb{lextend}
\noindent
To convert a \texttt{Lang} function to an Epic function, we build an
Epic function in which we apply the \texttt{Lang} function to the Epic
reference for its argument. Every reference to a name in \texttt{Lang}
is converted to the equivalent reference to the name in Epic. While
there may be neater solutions involving an environment, or avoiding
HOAS, this solution is very simple to implement, and preserves the
desirable feature that Haskell manages scope.
Compiling string append uses a built in function provided by the Epic
interface in \texttt{basic\_defs}:
\begin{SaveVerbatim}{lappend}
build (Op Append l r)
= fn "append" @@ build l @@ build r
\end{SaveVerbatim}
\useverb{lappend}
\noindent
Given \texttt{build}, we can translate a collection of HOAS
definitions into an Epic program, add the built-in Epic definitions
and execute it directly. Recall that there must be a function called
\texttt{"main"} or Epic will report an error.
\begin{SaveVerbatim}{lmain}
mkProgram :: Defs -> Program
mkProgram ds = basic_defs ++
map (\ (n, d) -> EpicFn n (build d)) ds
execute :: Defs -> IO ()
execute p = run (mkProgram p)
\end{SaveVerbatim}
\useverb{lmain}
\noindent
Alternatively, we can generate an executable. Again, the entry point
is the Epic function called \texttt{"main"}:
\begin{SaveVerbatim}{lcomp}
comp :: Defs -> IO ()
comp p = compile "a.out" (mkProgram p)
\end{SaveVerbatim}
\useverb{lcomp}
\noindent
This is a compiler for a very simple language, but a compiler for any
more complex language using the Epic API follows the same pattern:
convert the abstract syntax for each named definition into a named Epic
\texttt{Term}, add any required primitives (we have just used
\texttt{basic\_defs} here), and pass the collection of definitions to
\texttt{run} or \texttt{compile}.
\subsection{Dependently Typed $\lambda$-calculus}
\LamPi{}~\cite{simply-easy}. Complications: elimination
operators. Representation uses de Bruijn indices. Need a way to dump
output. Non-complication: odd type system.
\subsubsection{Representation}
\subsubsection{Compilation}
\subsection{A Dynamically Typed Turtle Graphics Language}