From ca6e01e3ea526969a9cf4dfb48ee862f0c5e7544 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Tue, 2 May 2017 19:57:22 +0200 Subject: [PATCH] Write bit about shallowly embedding --- methods.dsl.tex | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/methods.dsl.tex b/methods.dsl.tex index 639eec4..c28ed62 100644 --- a/methods.dsl.tex +++ b/methods.dsl.tex @@ -12,8 +12,8 @@ Listing~\ref{lst:exdeep}. Deep embedding has the advantage that it is very simple to build and the views are easy to make and add. However, there are also a few downsides. -\begin{lstlisting}[language=Clean,label={lst:exdeep},caption={A minimal deep -\gls{EDSL}}] +\begin{lstlisting}[language=Clean,label={lst:exdeep},% + caption={A minimal deep \gls{EDSL}}] :: DSL = LitI Int | LitB Bool @@ -34,9 +34,10 @@ The first downside of the type of \gls{EDSL} can be overcome by using \glspl{GADT}. Listing~\ref{lst:exdeepgadt} shows the same language, but type-safe with a \gls{GADT}\footnote{Note that \glspl{GADT} are not supported in \gls{Clean}. They can be simulated using bimaps}\todo{cite}. Unfortunately -the lack of extendability stays a problem. +the lack of extendability stays a problem. If a language construct is added no +compile time guarantee is given that all views support it. -\begin{lstlisting}[language=Clean,label={lst:exdeep},% +\begin{lstlisting}[language=Clean,label={lst:exdeepgadt},% caption={A minimal deep \gls{EDSL}}] :: DSL a = LitI Int -> DSL Int @@ -45,10 +46,46 @@ the lack of extendability stays a problem. | Plus (DSL Int) (DSL Int) -> DSL Int | Minus (DSL Int) (DSL Int) -> DSL Int | And (DSL Bool) (DSL Bool) -> DSL Bool - | E.e: Eq (DSL e) (DSL e) -> DSL Bool & == e + | E.e: Eq (DSL e) (DSL e) -> DSL Bool & == e \end{lstlisting} \subsection{Shallow embedding} +In a shallowly \gls{EDSL} all language constructs are expressed as functions in +the host language. Our example language then looks something like the code +shown in Listing~\ref{lst:exshallow} if implementing the evaluator. Note that +much of the internals of the language can be hidden away using monads. + +\begin{lstlisting}[language=Clean,label={lst:exshallow},% + caption={A minimal shallow \gls{EDSL}}] +:: Env = ... // Some environment +:: DSL a = DSL (Env -> a) + +Lit :: a -> DSL a +Lit x = \e->x + +Var :: String -> DSL Int +Var i = \e->retrEnv e i + +Plus :: (DSL Int) (DSL Int) -> DSL Int +Plus x y = \e->x e + y e + +... + +Eq :: (DSL a) (DSL a) -> DSL Bool | == a +Eq x y = \e->x e + y e +\end{lstlisting} + +The advantage of shallowly embedding a language in a host language is the +extendability. It is very easy to add functionality and compile time checks +guarantee that the functionality is available. Moreover, the language is type +safe as it is directly typed in the host language. + +The downside of this method is extending the language with views. It is near +impossible to add views to a shallowly embedded language. The only way of +achieving this is by decorating the datatype for the \gls{EDSL} with all the +information for all the views. This will mean that every component will have to +implement all views. This makes it slow for multiple views and complex to +implement. \subsection{Class based shallow embedding} \todo{while iTasks is also a DSL\ldots} -- 2.20.1