updates
[phd-thesis.git] / dsl / dsl_techniques.tex
index 7791c82..29e49c8 100644 (file)
@@ -5,40 +5,64 @@
        \pagenumbering{arabic}
 }{}
 
-\chapter{DSL embedding techniques}%
+\chapter{\texorpdfstring{\Acrshort{DSL}}{DSL} embedding techniques}%
 \label{chp:dsl_embedding_techniques}%
 An \gls{EDSL} is a language embedded in a host language created for a specific domain\todo{citation needed?}.
-\glspl{EDSL} can have one or more backends or views.
-Commonly used views are pretty printing, compiling, simulating, verifying and proving the program.
-Embedding techniques can be characterised by at least the following properties:
-\begin{enumerate}
-       \item Extendibility of constructs
-       \item Extendibility of views
-       \item Type safety, the type of a term in the language is typed in the host language
-       \item Support for well typed variables
-       \item Intensional analyses
-       \item Support for partial views, not all views need to support all constructs
-\end{enumerate}
-
-There are two main techniques for creating embedding \glspl{DSL}, deep---also called tagged---embedding and shallow---also called tagless---embedding.
+Properties such as referential transparency, minimal syntax and \glspl{ADT} make \gls{FP} languages excellent candidates for hosting \glspl{EDSL}.
+Terms in an \glspl{EDSL} can have multiple interpretations---also called backends or views---i.e.\ preferably, a term in the \gls{DSL} is just an interface.
+Commonly used intepretations are printing, compiling, simulating, optimising, verifying, proving the program\etc.
+There are two main flavours of embedding \glspl{DSL}, deep---also called tagged---embedding and shallow---also called tagless---embedding.
+
+\Cref{sec:deep_embedding} shows the basics of deep embedding.
+\Cref{sec:shallow_embedding} shows the basics of shallow embedding including tagless embedding.
+\Cref{sec:compare_embedding} compares the embedding technique.
+
+Both flavours have their strengths and weaknesses and both flavours can be improved in order to mitigate (some of the) downsides.
+
+\begin{table}[ht]
+       \begin{threeparttable}[b]
+               \caption{Comparison of embedding techniques, adapted from \citet[Sec.~3.6]{sun_compositional_2022}}%
+               \label{tbl:dsl_comparison}
+               \begin{tabular}{lllllll}
+                       \toprule
+                                                                       & Shallow & Deep  & Hybrid          & Poly           & Comp. & Classy\\
+                       \midrule
+                       Transcoding free        & yes     & yes   & no              & yes            & yes            & yes\\
+                       Linguistic reuse        & yes     & no    & partly\tnote{1} & yes            & yes            & no?\\
+                       Extend constructs       & yes     & no    & partly\tnote{1} & yes            & yes            & yes\\
+                       Extend interpretations  & no      & yes   & yes             & yes            & yes            & yes\\
+                       Transformations         & no      & yes   & yes             & maybe\tnote{2} & maybe\tnote{2} & yes\\
+                       Modular dependencies    & no      & maybe & maybe           & yes            & yes            & yes\\
+                       Nested pattern matching & no      & yes   & yes             & no             & maybe          & maybe\tnote{3}\\
+                       Type safe               & yes     & maybe & no              & yes            & yes            & yes\\
+                       \bottomrule
+               \end{tabular}
+               \begin{tablenotes}
+                       \item [1] Only in the shallowly embedded part.
+                       \item [2] Transformations require some ingenuity and are sometimes awkward to write.
+                       \item [3] It requires some---safe---form of dynamic typing.
+               \end{tablenotes}
+       \end{threeparttable}
+\end{table}
+
 In the following sections both techniques are explained.
 As an example, a simple language with integers, booleans and some arithmetic operators is used as a running example.
 
-\section{Deep embedding}
-A deep \gls{EDSL} is a language represented as data in the host language.
-Views are functions that transform \emph{something} to the datatype or the other way around.
+\section{Deep embedding}\label{sec:deep_embedding}
+In a deeply embedded \gls{DSL}, the language terms are represented as data type{(s)} in the host language.
+Therefore, interpretations of the terms are functions that operate on these data types.
 Definition~\ref{lst:exdeep} shows an implementation for the example \gls{DSL}.
 
 \begin{lstHaskell}[label={lst:exdeep},caption={A deeply embedded expression \gls{DSL}.}]
 data Value = I Int | B Bool
 data Expr
-    = Lit  Value
-    | Plus Expr Expr
-    | Eq   Expr Expr
+       = Lit  Value
+       | Plus Expr Expr
+       | Eq   Expr Expr
   deriving Show
 \end{lstHaskell}
 
-Implementing a printer for the language is straightforward:
+Implementing a printer for the language is straightforward, we just define a function that transforms the term to a string.
 
 \begin{lstHaskell}[caption={A printer for the deeply embedded expression \gls{DSL}.}]
 print :: Expr -> String
@@ -47,15 +71,17 @@ print (Plus l r) = "(" ++ print l ++ "+" ++ print r ++ ")"
 print (Eq l r)   = "(" ++ print l ++ "==" ++ print r ++ ")"
 \end{lstHaskell}
 
-Adding a construct, for example subtraction reveals the Achilles' heel of deep embedding, namely that we need to revisit the original data type \emph{and} all the existing views.
-I.e.\ we need to add \haskellinline{| Sub Expr Expr} to the \haskellinline{Expr} data type and \haskellinline{print (Sub l r) = ...} to the \haskellinline{print} view.
+Adding a construct---for example subtraction---reveals the Achilles' heel of deep embedding, namely that we need to revisit the original data type \emph{and} all the existing views.
+I.e.\ we need to add \haskellinline{| Sub Expr Expr} to the \haskellinline{Expr} data type.
+Furthermore, we need to add \haskellinline{print (Sub l r) = ...} to the \haskellinline{print} view in order to not end up with a partial function.
 This limitation can be overcome by lifting the views to classes (See \cref{chp:classy_deep_embedding}).
 
 Implementing an evaluator for the language is possible without touching any original code, we just add a function operating on the \haskellinline{Expr} data type.
-To store variables, it has an extra environment argument\todo{cite while}.
+To store variables, it has an extra environment argument.
 Here another downside of basic deep embedding arises immediately, the expressions are not typed, and therefore there has to be some type checking in the evaluation code.
+Luckily this problem can be overcome by switching from regular \glspl{ADT} to \glspl{GADT}, resulting in the following data type and evaluator.
 
-\begin{lstHaskell}[]
+\begin{lstHaskell}[caption={An evaluator for the deeply embedded expression \gls{DSL}.}]
 eval :: Expr -> Value
 eval (Lit i)    = i
 eval (Plus l r) = case (eval l, eval r) of
@@ -67,9 +93,20 @@ eval (Eq l r) = case (eval l, eval r) of
        (l, r)       -> error ("Can't compare " ++ show l ++ " to " ++ show r)
 \end{lstHaskell}
 
-Luckily this problem can be overcome by switching from regular \glspl{ADT} to \glspl{GADT}, resulting in the following data type and evaluator.
+\subsection{Deep embedding with \texorpdfstring{\acrshortpl{GADT}}{GADTs}}
+Deep embedding has the advantage that it is easy to build and views are easy to add.
+On the downside, the expressions created with this language are not necessarily type-safe.
+In the given language it is possible to create an expression such as \haskellinline{Plus (LitI 4) (LitB True)} that adds a boolean to an integer.
+Extending the \gls{ADT} is easy and convenient but extending the views accordingly is tedious since it has to be done individually for all views.
 
-\begin{lstHaskell}[]
+The first downside of this type of \gls{EDSL} can be overcome by using \glspl{GADT}~\citep{cheney_first-class_2003}.
+\Cref{lst:exdeepgadt} shows the same language, but type-safe with a \gls{GADT}.
+\glspl{GADT} are not supported in the current version of \gls{CLEAN} and therefore the syntax is hypothetical (See \todo{insert link to appendix}).
+However, it has been shown that \glspl{GADT} can be simulated using bimaps or projection pairs~\citep{cheney_first-class_2003}.
+Unfortunately the lack of extendability remains a problem.
+If a language construct is added, no compile time guarantee can be given that all views support it.
+
+\begin{lstHaskell}[label={lst:exdeepgadt},caption={A deeply embedded expression \gls{DSL} using \glspl{GADT}.}]
 data Expr a where
     Lit  :: Show a => a -> Expr a
     Plus :: Num a  => Expr a -> Expr a -> Expr a
@@ -81,34 +118,14 @@ eval (Plus l r) = eval l + eval r
 eval (Eq l r)   = eval l == eval r
 \end{lstHaskell}
 
-Deep embedding has the advantage that it is easy to build and views are easy to add.
-On the downside, the expressions created with this language are not necessarily type-safe.
-In the given language it is possible to create an expression such as \cleaninline{Plus (LitI 4) (LitB True)} that adds a boolean to an integer.
-Extending the \gls{ADT} is easy and convenient but extending the views accordingly is tedious since it has to be done individually for all views.
-
-The first downside of this type of \gls{EDSL} can be overcome by using \glspl{GADT}~\citep{cheney_first-class_2003}.
-Example~\ref{lst:exdeepgadt} shows the same language, but type-safe with a \gls{GADT}.
-\glspl{GADT} are not supported in the current version of \gls{CLEAN} and therefore the syntax is hypothetical (See \todo{insert link to appendix}).
-However, it has been shown that \glspl{GADT} can be simulated using bimaps or projection pairs~\citep{cheney_first-class_2003}.
-Unfortunately the lack of extendability remains a problem.
-If a language construct is added, no compile time guarantee can be given that all views support it.
-
-\begin{lstClean}[label={lst:exdeepgadt},caption={A deeply embedded expression \gls{DSL} using \glspl{GADT}.}]
-:: Expr a
-       =      Lit  a                        -> Expr a
-       | E.e: Var   String                  -> Expr e
-       |      Plus  (Expr Int)  (Expr Int)  -> Expr Int
-       | E.e: Eq    (Expr e)    (Expr e)    -> Expr Bool & == e
-\end{lstClean}
-
 \section{Shallow embedding}
 In a shallowly \gls{EDSL} all language constructs are expressed as functions in the host language.
-An evaluator view for the example language then can be implemented as the code shown in Definition~\ref{lst:exshallow}.
+An evaluator view for the example language then can be implemented as the code shown in \cref{lst:exshallow}.
 Note that much of the internals of the language can be hidden using monads.
 
-\begin{lstClean}[label={lst:exshallow}, caption={A minimal shallow \gls{EDSL}.}]
-:: Env   :== (String -> Int)
-:: DSL a :== (Env -> a)
+\begin{lstHaskell}[label={lst:exshallow}, caption={A minimal shallow \gls{EDSL}.}]
+type Env   = String -> Int
+type DSL a = Env -> a
 
 Lit :: a -> DSL a
 Lit x = \e->x
@@ -116,16 +133,17 @@ Lit x = \e->x
 Var :: String -> DSL Int
 Var i = \e->retrEnv e i
 
-Plus :: (DSL Int) (DSL Int) -> DSL Int
+Plus :: DSL Int -> DSL Int -> DSL Int
 Plus x y = \e->x e + y e
 
-Eq :: (DSL a) (DSL a) -> DSL Bool | == a
+Eq :: Eq a => DSL a -> DSL a -> DSL Bool
 Eq x y = \e->x e == y e
-\end{lstClean}
+\end{lstHaskell}
 
-The advantage of shallowly embedding a language in a host language is its extendability.
+One of the advantages of shallowly embedding a language in a host language is its extendability.
 It is very easy to add functionality because the compile time checks of the host language guarantee whether or not the functionality is available when used.
 Moreover, the language is type safe as it is directly typed in the host language, i.e.\ \cleaninline{Lit True +. Lit 4} is rejected.
+Another advantage is the intimate link with the host language, allowing for a lot more linguistic reuse such as the support of implicit sharing~\cite{krishnamurthi_linguistic_2001}.
 
 The downside of this method is extending the language with views.
 It is nearly impossible to add views to a shallowly embedded language.
@@ -134,6 +152,7 @@ This will mean that every component will have to implement all views rendering i
 
 \subsection{Tagless-final embedding}\label{ssec:tagless}
 
+
 \section{Comparison}
 \todo{cite compositional DSLs}