restructure
[phd-thesis.git] / dsl / dsl_techniques.tex
index 51c88a5..22b9977 100644 (file)
@@ -1,17 +1,37 @@
 \documentclass[../thesis.tex]{subfiles}
 
-\begin{document}
-\ifSubfilesClassLoaded{
-       \pagenumbering{arabic}
-}{}
+\include{subfilepreamble}
 
-\chapter{\texorpdfstring{\Acrshort{DSL}}{DSL} embedding techniques}%
+\begin{document}
+\chapter{\texorpdfstring{\Glsxtrshort{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?}.
-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.
+Properties such as referential transparency, minimal syntax, powerful type systems and rich data types make \gls{FP} languages excellent candidates for hosting \glspl{EDSL}.
+
+There are two flavours of \gls{DSL} embedding: deep- and shallow embedding~\citep{boulton_experience_1992}.
+Shallow embedding---also called tagless embedding---models language constructs as functions in the host language.
+As a result, adding new language constructs---extra functions---is easy.
+However, the interpretation of the language is embedded in these functions, making it troublesome to add semantics since it requires updating all existing language constructs.
+
+In contrast to shallow embedding, deep embedding---also called tagged embedding---models terms in the language as data types.
+Interpretations are functions over these data types.
+
+Consequently, adding new semantics, i.e.\ novel functions, is straightforward.
+It can be stated that the language constructs are embedded in the functions that form a semantics.
+If one wants to add a language construct, all semantics functions must be revisited and revised to avoid ending up with partial functions.
+
+This juxtaposition has been known for many years~\citep{reynolds_user-defined_1978} and discussed by many others~\citep{krishnamurthi_synthesizing_1998} but most famously dubbed the \emph{expression problem} by Wadler~\citep{wadler_expression_1998}:
+
+\begin{quote}
+       The \emph{expression problem} is a new name for an old problem.
+       The goal is to define a data type by cases, where one can add new cases to the data type and new functions over the data type, without recompiling existing code, and while retaining static type safety (e.g., no casts).
+\end{quote}
+
+Terms in an \glspl{EDSL} can have multiple interpretations\footnote{Interpretations are also called backends or views}, i.e.\ 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.
+There are two main flavours of embedding \glspl{DSL}.
+Deep embedding---also called shallow--- models terms in the language as data types, interpretations are functions over these terms.
+Shallow embedding---also called tagless---models terms in the language as functions, interpretations are embedded in these functions.
 
 Most importantly, the two flavours differ on two axes: extensibility of language constructs and extensibility of interpretations.
 \todo{elaborate}
@@ -68,7 +88,7 @@ eval (Eq l r) = case (eval l, eval r) of
        (l, r)       -> error ("Can't compare " ++ show l ++ " to " ++ show r)
 \end{lstHaskell}
 
-\subsection{\texorpdfstring{\Acrlongpl{GADT}}{Generalised algebraic data types}}
+\subsection{\texorpdfstring{\Glsxtrlongpl{GADT}}{Generalised algebraic data types}}
 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{LitI 4 `Plus` LitB True} that adds a boolean to an integer.
@@ -77,7 +97,7 @@ Extending the \gls{ADT} is easy and convenient but extending the views according
 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[Sec.~2.2]{cheney_lightweight_2002}.
+However, it has been shown that \glspl{GADT} can be simulated using bimaps or projection pairs~\citep[\citesection{2.2}]{cheney_lightweight_2002}.
 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.
 
@@ -125,11 +145,11 @@ sub x y = \e->x e - y e
 \end{lstHaskell}
 
 Moreover, the language is type safe as it is directly typed in the host language, i.e.\ \haskellinline{lit True `plus` 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}.
+Another advantage is the intimate link with the host language, allowing for a lot more linguistic reuse such as the support of implicit sharing \citep{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.
-The only way of achieving this is by reimplementing all functions so that they run all backends at the same time or to create a single interpretation that produces a fold function~\citep{gibbons_folding_2014}.
+The only way of achieving this is by reimplementing all functions so that they run all backends at the same time or to create a single interpretation that produces a fold function \citep{gibbons_folding_2014}.
 
 \subsection{Tagless-final embedding}\label{ssec:tagless}
 By lifting the functions representing the \gls{DSL} terms to type classes, interpretations can be added.
@@ -146,7 +166,7 @@ class DSL v where
 
 An interpretation of this view is a data type that implements the type class.
 
-\begin{lstHaskell}[label={lst:extagless},caption={A minimal tagless-final \gls{EDSL}.}]
+\begin{lstHaskell}[label={lst:extaglessprint},caption={A pretty printer for the tagless-final \gls{EDSL}.}]
 data Print a = P {runPrint :: String}
 instance DSL Print where
        lit a = P (show a)
@@ -167,7 +187,7 @@ instance Sub Print where
 
 Adding an interpretation means adding a data type and providing instances for the language constructs.
 
-\begin{lstHaskell}[label={lst:extagless},caption={An evaluator interpretation of the minimal tagless-final \gls{EDSL}.}]
+\begin{lstHaskell}[label={lst:extaglesseval},caption={An evaluator interpretation of the minimal tagless-final \gls{EDSL}.}]
 data Eval a = Eval {runEval :: Env -> a}
 
 instance DSL v where
@@ -185,7 +205,7 @@ Both flavours have their strengths and weaknesses and both flavours can be impro
 
 \begin{table}[ht]
        \begin{threeparttable}[b]
-               \caption{Comparison of embedding techniques, adapted from \citet[Sec.~3.6]{sun_compositional_2022}}%
+               \caption{Comparison of embedding techniques, adapted from \citet[\citesection{3.6}]{sun_compositional_2022}}%
                \label{tbl:dsl_comparison}
                \begin{tabular}{lllllll}
                        \toprule