\documentclass[../thesis.tex]{subfiles}

\input{subfilepreamble}

\setcounter{chapter}{2}

\begin{document}
\input{subfileprefix}
\chapter{First-class data types in shallow embedded domain-specific languages using metaprogramming}%
\label{chp:first-class_datatypes}%
\begin{chapterabstract}
	\Gls{FP} languages are excellent for hosting \glspl{EDSL} because of their rich type systems, minimal syntax, and referential transparency.
	However, data types defined in the host language are not automatically available in the embedded language.
	To do so, all the operations on the data type must be ported to the \gls{EDSL} resulting in a lot of boilerplate.

	This chapter shows that by using metaprogramming, all first-order user-defined data types can be automatically made first class in shallow \glspl{EDSL}.
	We show this by providing an implementation in \gls{TH} for a typical \gls{DSL} with two different semantics.
	Furthermore, we show that by utilising quasiquotation, there is hardly any burden on the syntax.
	Finally, the chapter also serves as a gentle introduction to \gls{TH}.
\end{chapterabstract}

\section{Introduction}
\Gls{FP} languages are excellent candidates for hosting \glspl{EDSL} because of their rich type systems, minimal syntax, and referential transparency.
By expressing the language constructs in the host language, the parser, the type checker, and the run time can be inherited from the host language.
Unfortunately, data types defined in the host language are not automatically available in the \gls{EDSL}.

The two main strategies for embedding \glspl{DSL} in \pgls{FP} language are deep embedding (also called initial) and shallow embedding (also called final).
Deep embedding represents the constructs in the language as data types and the semantics as functions over these data types.
This makes extending the language with new semantics effortless: just add another function.
In contrast, adding language constructs requires changing the data type and updating all existing semantics to support this new construct.
Shallow embedding on the other hand models the language constructs as functions with the semantics embedded.
Consequently, adding a construct is easy, i.e.\ it only entails adding another function.
Contrarily, adding semantics requires adapting all language constructs.
Lifting the functions to type classes, i.e.\ parametrising the constructs over the semantics, allows extension of the language both in constructs and in semantics orthogonally. This advanced style of embedding is called tagless-final or class-based shallow embedding \citep{kiselyov_typed_2012}.

While it is often possible to lift values of a user-defined data type to a value in the \gls{DSL}, it is not possible to interact with it using \gls{DSL} constructs, since they are not first-class citizens.

Concretely, it is not possible to
\begin{enumerate*}
	\item construct values from expressions using a constructor,
	\item deconstruct values into expressions using a deconstructor or pattern matching,
	\item test which constructor the value holds.
\end{enumerate*}
The functions for this are simply not available automatically in the embedded language.
For some semantics---such as an interpreter---it is possible to directly lift the functions from the host language to the \gls{DSL}.
In other cases---e.g.\ \emph{compiling} \glspl{DSL} such as a compiler or a printer---this is not possible \citep{elliott_compiling_2003}. %the torget this is not possible. Cannot just be lifted from the host language to the \gls{DSL} so it requires a lot of boilerplate to define and implement them.
Thus, all the operations on the data type have to be defined by hand requiring a lot of plumbing and resulting in a lot of boilerplate code.

To relieve the burden of adding all these functions, metaprogramming\nobreak---\nobreak\hskip0pt and custom quasiquoters---can be used.
Metaprogramming entails that some parts of the program are generated by a program itself, i.e.\ the program is data.
Quasiquotation is a metaprogramming mechanism that allows entering verbatim code for which a---possibly user defined---translation is used to convert the verbatim code to host language \gls{AST} nodes.
Metaprogramming allows functions to be added to the program at compile time based on the structure of user-defined data types.

\subsection{Contributions}
This chapter shows that with the use of metaprogramming, all first-order user-defined data types can automatically be made first class for shallow \glspl{EDSL}.
It does so by providing an implementation in \gls{TH} for a typical \gls{DSL} with two different semantics: an interpreter and a pretty printer.
Furthermore, we show that by utilising quasiquotation, there is hardly any burden on the syntax.
Finally, the chapter also serves as a gentle introduction to \gls{TH} and reflects on the process of using \gls{TH}.

\section{Tagless-final embedding}
Tagless-final embedding is an upgrade to standard shallow embedding achieved by lifting all language construct functions to type classes.
As a result, views on the \gls{DSL} are data types implementing these classes.

To illustrate the technique, a simple \gls{DSL}, a language consisting of literals and addition, is outlined.
This language, implemented according to the tagless-final style \citep{carette_finally_2009} in \gls{HASKELL} \citep{peyton_jones_haskell_2003} consists initially only of one type class containing two functions.
The \haskellinline{lit} function lifts values from the host language to the \gls{DSL} domain.
The class constraint \haskellinline{Show} is enforced on the type variable \haskellinline{a} to make sure that the value can be printed.
The infix function \haskellinline{+.} represents the addition of two expressions in the \gls{DSL}.

\begin{lstHaskell}
class Expr v where
    lit :: Show a => a -> v a
    (+.) :: Num a => v a -> v a -> v a
infixl 6 +.
\end{lstHaskell}

The implementation of a view on the \gls{DSL} is achieved by implementing the type classes with the data type representing the view.
In the case of our example \gls{DSL}, an interpreter accounting for failure may be implemented as an instance for the \haskellinline{Maybe} type.
The standard infix functor application and infix sequential application are used so that potential failure is abstracted away from\footnotemark.
\begin{lrbox}{\LstBox}
	\begin{lstHaskell}[frame=]
<$> ::   (a -> b) -> f a -> f b
<*> :: f (a -> b) -> f a -> f b
infixl 4 <$>, <*>\end{lstHaskell}
\end{lrbox}
\footnotetext{\usebox{\LstBox}}

\begin{lstHaskell}
instance Expr Maybe where
    lit a = Just a
    (+.) l r = (+) <$> l <*> r
\end{lstHaskell}

\subsection{Adding language constructs}
To add an extra language construct we define a new class housing it.
For example, to add division we define a new class as follows:

\begin{lstHaskell}
class Div v where
    (/.) :: Integral a => v a -> v a -> v a
infixl 7 /.
\end{lstHaskell}

Division is an operation that is undefined if the right operand is equal to zero.
To capture this behaviour, the \haskellinline{Nothing} constructor from \haskellinline{Maybe} is used to represent errors.
Both sides of the division operator are evaluated.
If the right-hand side is zero, the division is not performed and an error is returned instead:

\begin{lstHaskell}
instance Div Maybe where
    (/.) l r = l >>= \x->r >>= \y->
        if y == 0 then Nothing else Just (x `div` y)
\end{lstHaskell}

\subsection{Adding semantics}
To add semantics to the \gls{DSL}, the existing classes are implemented with a novel data type representing the view on the \gls{DSL}.
First a data type representing the semantics is defined. In this case, the printer is kept very simple for brevity and just defined as a \haskellinline{newtype} of a string to store the printed representation\footnotemark.
\footnotetext{%
	In this case a \haskellinline{newtype} is used instead of regular \haskellinline{data} declarations.
	\haskellinline{newtype}s are special data types only consisting a single constructor with one field to which the type is isomorphic.
	During compilation the constructor is completely removed resulting in no overhead \citep[\citesection{4.2.3}]{peyton_jones_haskell_2003}.
}
Since the language is typed, the printer data type has to have a type variable, but it is only used during typing---i.e.\ a phantom type \citep{leijen_domain_2000}:

\begin{lstHaskell}
newtype Printer a = P { runPrinter :: String }
\end{lstHaskell}

The class instances for \haskellinline{Expr} and \haskellinline{Div} for the pretty printer are straightforward and as follows:

\begin{lstHaskell}
instance Expr Printer where
    lit a = P (show a)
    (+.) l r = P ("(" ++ runPrinter l
               ++ "+" ++ runPrinter r ++ ")")

instance Div Printer where
    (/.) l r = P ("(" ++ runPrinter l
               ++ "/" ++ runPrinter r ++ ")")
\end{lstHaskell}

\subsection{Functions}
Adding functions to the language is achieved by adding a multi-parameter class to the \gls{DSL}.
The type of the class function allows for the implementation to only allow first-order functions by supplying the arguments in a tuple.
Furthermore, with the \haskellinline{:-} operator the syntax becomes useable.
Finally, by defining the functions as a \gls{HOAS} type safety is achieved \citep{pfenning_higher-order_1988,chlipala_parametric_2008}.
The complete definition looks as follows:

\begin{lstHaskell}
class Function a v where
    fun :: ((a -> v s) -> In (a -> v s) (v u)) -> v u
data In a b = a :- b
infix 1 :-
\end{lstHaskell}

The \haskellinline{Function} type class is now used to define functions with little syntactic overhead\footnote{The \GHCmod{BlockArguments} extension of \gls{GHC} is used to reduce the number of brackets that allows lambda's to be an argument to a function without brackets}.
The following listing shows an expression in the \gls{DSL} utilising two user-defined functions:

\begin{lstHaskell}
   fun \increment-> (\x     ->x +. lit 1)
:- fun \divide->    (\(x, y)->x /. y    )
:- increment (divide (lit 38, lit 5))
\end{lstHaskell}

The interpreter only requires one instance of the \haskellinline{Function} class that works for any argument type.
In the implementation, the resulting function \haskellinline{g} is simultaneously provided to the definition \haskellinline{def}.
Because the laziness of \gls{HASKELL}'s lazy let bindings, this results in a fixed point calculation:

\begin{lstHaskell}
instance Function a Maybe where
    fun def = let g :- m = def g in m
\end{lstHaskell}

The given \haskellinline{Printer} type is not sufficient to implement the instances for the \haskellinline{Function} class, it must be possible to generate fresh function names.
After extending the \haskellinline{Printer} type to contain some sort of state to generate fresh function names and a \haskellinline{MonadWriter [String]}\footnotemark\ to streamline the output, we define an instance for every arity.
\begin{lrbox}{\LstBox}
	\begin{lstHaskell}[frame=]
freshLabel :: Printer String
tell :: MonadWriter w m => w -> m ()\end{lstHaskell}
\end{lrbox}
\footnotetext{\usebox{\LstBox}}
To illustrate this, the instance for unary functions is shown, all other arities are implemented in similar fashion.

\begin{lstHaskell}
instance Function () Printer where ...
instance Function (Printer a) Printer where ...
    fun def = freshLabel >>= \f->
        let g :- m = def $ \a0->const undefined
                <$> (tell ["f", show f, " ("]
                     >> a0 >> tell [")"])
        in  tell ["let f", f, " a0 = "]
                >> g (const undefined <$> tell ["a0"])
        >>  tell [" in "] >> m
instance Function (Printer a, Printer b) Printer where ...
\end{lstHaskell}

Running the given printer on the example code shown before produces roughly the following output, running the interpreter on this code results in \haskellinline{Just 8}.

\begin{lstHaskell}
   let f0 a1 = a1 + 1
in let f2 a3 a4 = a3 / a4
in f0 (f2 38 5)
\end{lstHaskell}

\subsection{Data types}
Lifting values from the host language to the \gls{DSL} is possible using the \haskellinline{lit} function as long as the type of the value has instances for all the class constraints.
Unfortunately, once lifted, it is not possible to do anything with values of the user-defined data type other than passing them around.
It is not possible to construct new values from expressions in the \gls{DSL}, to deconstruct a value into the fields, nor to test of which constructor the value is.
Furthermore, while in our language the only constraint is the automatically derivable \haskellinline{Show}, in real-world languages the class constraints may be very difficult to satisfy for complex types, for example serialisation to a single stack cell in the case of a compiler.

As a consequence, for user-defined data types---such as a pro\-gram\-mer-defined list type\footnotemark---to become first-class citizens in the \gls{DSL}, language constructs for constructors, deconstructors and constructor predicates must be defined.
Field selectors are also useful functions for working with user-defined data types, they are not considered for the sake of brevity but can be implemented using the deconstructor functions.
\footnotetext{
	For example: \haskellinline{data List a = Nil \| Cons \{hd :: a, tl :: List a\}}
}
The constructs for the list type would result in the following class definition:

\begin{lstHaskell}
class ListDSL v where
    -- constructors
    nil    :: v (List a)
    cons   :: v a -> v (List a) -> v (List a)
    -- deconstructors
    unNil  :: v (List a) -> v b -> v b
    unCons :: v (List a) -> (v a -> v (List a) -> v b) -> v b
    -- constructor predicates
    isNil  :: v (List a) -> v Bool
    isCons :: v (List a) -> v Bool
\end{lstHaskell}

Furthermore, instances for the \gls{DSL}'s views need to be created.
For example, to use the interpreter, the following instance must be available.
Note that at first glance, it would feel natural to have \haskellinline{isNil} and \haskellinline{isCons} return \haskellinline{Nothing} since we are in the \haskellinline{Maybe} monad.
However, this would fail the entire expression and the idea is that the constructor test can be done from within the \gls{DSL}.

\begin{lstHaskell}
instance ListDSL Maybe where
    nil        = Just Nil
    cons hd tl = Cons <$> hd <*> tl
    unNil  d f = d >>= \Nil->f
    unCons d f = d >>= \(Cons hd tl)->f (Just hd) (Just tl)
    isNil  d   = d >>= \case[+\footnotemark+]
        Nil -> Just True
        _   -> Just False
    isCons d   = d >>= \case
        Cons _ _ -> Just True
        Nil      -> Just False
\end{lstHaskell}
\footnotetext{%
	\haskellinline{\\case} is an abbreviation for \haskellinline{\\x->case x of ...} when using GHC's \GHCmod{LambdaCase} extension.
}

Adding these classes and their corresponding instances is tedious and results in boilerplate code.
We therefore resort to metaprogramming, and in particular \gls{TH} \citep{sheard_template_2002} to alleviate this burden.

\section{Template metaprogramming}
Metaprogramming is a special flavour of programming where programs have the ability to treat and manipulate programs or program fragments as data.
There are several techniques to facilitate metaprogramming, moreover it has been around for many years now \citep{lilis_survey_2019}.
Even though it has been around for many years, it is considered complex \citep{sheard_accomplishments_2001}.

\gls{TH} is GHC's de facto metaprogramming system, implemented as a compiler extension together with a library \citep{sheard_template_2002}\citep[\citesection{6.13.1}]{ghc_team_ghc_2021}.
Readers already familiar with \gls{TH} can safely skip this section.

\gls{TH} adds four main concepts to the language, na\-me\-ly \gls{AST} data types, splicing, quasiquotation and reification.
With this machinery, regular \gls{HASKELL} functions can be defined that are called at compile time, inserting generated code into the \gls{AST}.
These functions are monadic functions operating in the \haskellinline{Q} monad.
The \haskellinline{Q} monad facilitates failure, reification and fresh identifier generation for hygienic macros \citep{kohlbecker_hygienic_1986}.
Within the \haskellinline{Q} monad, capturable and non-capturable identifiers can be generated using the \haskellinline{mkName} and \haskellinline{newName} functions respectively.
The \emph{Peter Parker principle}\footnote{With great power comes great responsibility.} holds for the \haskellinline{Q} monad as well because it executes at compile time and is very powerful.
For example, it can subvert module boundaries, thus accessing constructors that were hidden; access the structure of abstract types; and it may cause side effects during compilation because it is possible to call \haskellinline{IO} operations \citep{terei_safe_2012}.
To achieve the goal of embedding data types in a \gls{DSL} we refrain from using these \emph{unsafe} features.

\subsection{Data types}
Firstly, for all of \gls{HASKELL}'s \gls{AST} elements, data types are provided that are mostly isomorphic to the actual data types used in the compiler.
With these data types, the entire syntax of a \gls{HASKELL} program can be specified.
Often, a data type is suffixed with the context, e.g.\ there is a \haskellinline{VarE} and a \haskellinline{VarP} for a variable in an expression or in a pattern respectively.
To give an impression of these data types, a selection of data types available in \gls{TH} is given below:

\begin{lstHaskell}
data Dec = FunD Name [Clause] | DataD Cxt Name ... | SigD Name Type
	| ClassD Cxt Name | ...
data Clause = Clause [Pat] Body [Dec]
data Pat = LitP Lit | VarP Name | TupP [Pat] | WildP | ListP [Pat] | ...
data Body = GuardedB [(Guard, Exp)] | NormalB Exp
data Guard = NormalG Exp | PatG [Stmt]
data Exp = VarE Name | LitE Lit | AppE Exp Exp | TupE [Maybe Exp]
	| LamE [Pat] Exp | ...
data Lit = CharL Char | StringL String | IntegerL Integer | ...
\end{lstHaskell}

To ease creating \gls{AST} data types in the \haskellinline{Q} monad, lowercase variants of the constructors are available that lift the constructor to the \haskellinline{Q} monad.
For example, for the \haskellinline{LamE} constructor, the following \haskellinline{lamE} function is available.

\begin{lstHaskell}
lamE :: [Q Pat] -> Q Exp -> Q Exp
lamE ps es = LamE <$> sequence ps <*> es
\end{lstHaskell}

\subsection{Splicing}
Special splicing syntax (\haskellinline{\$(...)}) marks functions for compile-time execution.
Other than that they always produce a value of an \gls{AST} data type, they are regular functions.
Depending on the context and location of the splice, the result type is either a list of declarations, a type, an expression or a pattern.
The result of this function, when successful, is then spliced into the code and treated as regular code by the compiler.
Consequently, the code that is generated may not be type safe, in which case the compiler provides a type error on the generated code.
The following listing shows an example of a \gls{TH} function generating on-the-fly functions for arbitrary field selection in a tuple.
When called as \haskellinline{\$(tsel 2 4)} it expands at compile time to \haskellinline{\\(_, _, f, _)->f}:

\begin{lstHaskell}
tsel :: Int -> Int -> Q Exp
tsel field total = do
    f <- newName "f"
    lamE [ tupP [if i == field then varP f else wildP
         | i<-[0..total-1]]] (varE f)
\end{lstHaskell}

\subsection{Quasiquotation}
Another key concept of \gls{TH} is Quasiquotation, the dual of splicing \citep{bawden_quasiquotation_1999}.
While it is possible to construct entire programs using the provided data types, it is a little cumbersome.
Using \emph{Oxford brackets} (\verb#[|# \ldots\verb#|]#) or single or double apostrophes, verbatim \gls{HASKELL} code can be entered that is converted automatically to the corresponding \gls{AST} nodes easing the creation of language constructs.
Depending on the context, different quasiquotes are used:
\begin{itemize*}
	\item \haskellinline{[\|...\|]} or \haskellinline{[e\|...\|]} for expressions
	\item \haskellinline{[d\|...\|]} for declarations
	\item \haskellinline{[p\|...\|]} for patterns
	\item \haskellinline{[t\|...\|]} for types
	\item \haskellinline{'...} for function names
	\item \haskellinline{''...} for type names
\end{itemize*}.
It is possible to escape the quasiquotes again by splicing.
Variables defined within quasiquotes are always fresh---as if defined with \haskellinline{newName}---but it is possible to capture identifiers using \haskellinline{mkName}.
For example, \haskellinline{[\|\\x->x\|]} translates to \haskellinline{newName "x" >>= \\x->lamE [varP x] (varE x)} and does not interfere with other \haskellinline{x}s already defined.

\subsection{Reification}
Reification is the act of querying the compiler for information about a certain name.
For example, reifying a type name results in information about the type and the corresponding \gls{AST} nodes of the type's definition.
This information can then be used to generate code according to the structure of data types.
Reification is done using the \haskellinline{reify :: Name -> Q Info} function.
The \haskellinline{Info} type is an \gls{ADT} containing all the---known to the compiler---information about the matching type: constructors, instances, \etc.

\section{Metaprogramming for generating DSL functions}
With the power of metaprogramming, we can generate the boilerplate code for our user-defined data types automatically at compile time.
To generate the code required for the \gls{DSL}, we define the \haskellinline{genDSL} function.
The type belonging to the name passed as an argument to this function is made available for the \gls{DSL} by generating the \haskellinline{typeDSL} class and view instances.
For the \haskellinline{List} type it is called as: \haskellinline{\$(genDSL ''List)}\footnotemark.
\footnotetext{
	\haskellinline{''} is used instead of \haskellinline{'} to instruct the compiler to look up the information for \haskellinline{List} as a type and not as a constructor.
}

The \haskellinline{genDSL} function is a regular function---though \gls{TH} requires that it is defined in a separate module---that has type: \haskellinline{Name -> Q [Dec]}, i.e.\ given a name, it produces a list of declarations in the \haskellinline{Q} monad.
The \haskellinline{genDSL} function first reifies the name to retrieve the structural information.
If the name matches a type constructor containing a data type declaration, the structure of the type---the type variables, the type name and information about the constructors\footnotemark---are passed to the \haskellinline{genDSL'} function.
\footnotetext{
	Defined as \haskellinline{type VarBangType = (Name, Bang, Type)} by \gls{TH}.
}
The \haskellinline{getConsName} function filters out unsupported data types such as \glspl{GADT} and makes sure that every field has a name.
For regular \glspl{ADT}, the \haskellinline{adtFieldName} function is used to generate a name for the constructor based on the indices of the fields\footnotemark.
\footnotetext{
	\haskellinline{adtFieldName :: Name -> Integer -> Name}
}
From this structure of the type, \haskellinline{genDSL'} generates a list of declarations containing a class definition (\cref{sec_fcd:class}), instances for the interpreter (\cref{sec_fcd:interpreter}), and instances of the printer (\cref{sec_fcd:prettyprinter}) respectively.

\begin{lstHaskell}
genDSL :: Name -> Q [Dec]
genDSL name = reify name >>= \case
    TyConI (DataD cxt typeName tvs mkind constructors derives)
        -> mapM getConsName constructors >>= \d->genDSL' tvs typeName d
    t -> fail ("genDSL does not support: " ++ show t)

getConsName :: Con -> Q (Name, [VarBangType])
getConsName (NormalC consName fs) = pure (consName,
    [(adtFieldName consName i, b, t) | (i, (b, t))<-[0..] `zip` fs])
getConsName (RecC consName fs) = pure (consName, fs)
getConsName c = fail ("genDSL does not support: " ++ show c)

genDSL' :: [TyVarBndr] -> Name -> [(Name, [VarBangType])] -> Q [Dec]
genDSL' typeVars typeName constructors = sequence
    [ mkClass, mkInterpreter, mkPrinter, ... ]
  where
    (consNames, fields) = unzip constructors
    ...
\end{lstHaskell}

\subsection{Class generation}\label{sec_fcd:class}
The function for generating the class definition is defined in the \haskellinline{where} clause of the \haskellinline{genDSL'} function.
Using the \haskellinline{classD} constructor, a single type class is created with a single type variable \haskellinline{v}.
The \haskellinline{classD} function takes five arguments:
\begin{enumerate*}
	\item a context, i.e.\ the class constraints, which is empty in this case
	\item a name, generated from the type name using the \haskellinline{className} function that simply appends the text \haskellinline{DSL}
	\item a list of type variables, in this case the only type variable is the view on the \gls{DSL}, i.e.\ \haskellinline{v}
	\item functional dependencies, empty in our case
	\item a list of function declarations, i.e.\ the class members, in this case it is a concatenation of the constructors, deconstructors, and constructor predicates
\end{enumerate*}
Depending on the required information, either \haskellinline{zipWith} or \haskellinline{map} is used to apply the generation function to all constructors.

\begin{lstHaskell}
mkClass :: Q Dec
mkClass = classD (cxt []) (className typeName) [PlainTV (mkName "v")] []
    (  zipWith mkConstructor   consNames fields
    ++ zipWith mkDeconstructor consNames fields
    ++ map     mkPredicate     consNames
    )
\end{lstHaskell}

In all class members, the view \haskellinline{v} plays a crucial role.
Therefore, a definition for \haskellinline{v} is accessible for all generation functions.
Furthermore, the \haskellinline{res} type represents the \emph{result} type, it is defined as the type including all type variables.
This result type is derived from the type name and the list of type variables.
In case of the \haskellinline{List} type, \haskellinline{res} is defined as \haskellinline{v (List a)} and is available for as well:

\begin{lstHaskell}
v = varT (mkName "v")
res = v `appT` foldl appT (conT typeName)
    (map getName typeVars)
  where getName (PlainTV name)    = varT name
        getName (KindedTV name _) = varT name
\end{lstHaskell}

\subsubsection{Constructors}
The constructor definitions are generated from just the constructor names and the field information.
All class members are defined using the \haskellinline{sigD} constructor that represents a function signature.
The first argument is the name of the constructor function, a lowercase variant of the actual constructor name generated using the \haskellinline{constructorName} function.
The second argument is the type of the function.
A constructor $C_k$ of type $T$ where
$T~tv_0~\ldots~tv_n = \ldots |~ C_k~a_0~\ldots~a_m~| \ldots~$
is defined as a \gls{DSL} function
$c_k \dcolon v~a_0 \shortrightarrow \ldots \shortrightarrow v~a_m \shortrightarrow v~(T~v_0~\ldots~v_n) $.
In the implementation, first the view \haskellinline{v} is applied to all the field types.
Then, the constructor type is constructed by folding over the lifted field types with the result type as the initial value using \haskellinline{mkCFun}.

\begin{lstHaskell}
mkConstructor :: Name -> [VarBangType] -> Q Dec
mkConstructor n fs = sigD (constructorName n) (mkCFun fs res)

mkCFun :: [VarBangType] -> Q Type -> Q Type
mkCFun fs res = foldr (\x y->[t|$x -> $y|])
    (map (\(_, _, t)->v `appT` pure t) fs)
\end{lstHaskell}

\subsubsection{Deconstructors}
The deconstructor is generated similarly to the constructor as the function for generating the constructor is the second argument modulo change in the result type.
A deconstructor $C_k$ of type $T$ is defined as a \gls{DSL} function
$\mathit{unC_k} \dcolon v~(T~v_0 \ldots v_n) \shortrightarrow (v~a_0 \shortrightarrow \ldots \shortrightarrow v~a_m \shortrightarrow v~b) \shortrightarrow v~b $.
In the implementation, \haskellinline{mkCFun} is reused to construct the type of the deconstructor as follows:

\begin{lstHaskell}
mkDeconstructor :: Name -> [VarBangType] -> Q Dec
mkDeconstructor n fs = sigD (deconstructorName n)
    [t|$res -> $(mkCFun fs [t|$v $b|]) -> $v $b|]
  where b = varT (mkName "b")
\end{lstHaskell}

\subsubsection{Constructor predicates}
The last part of the class definition are the constructor predicates, a function that checks whether the provided value of type $T$ contains a value with constructor $C_k$.
A constructor predicate for constructor $C_k$ of type $T$ is defined as a \gls{DSL} function $\mathit{isC_k} \dcolon v~(T~v_0~\ldots~v_n) \shortrightarrow v~\mathit{Bool}$.
A constructor predicate---name prefixed by \haskellinline{is}---is generated for all constructors.
They all have the same type:

\begin{lstHaskell}
mkPredicate :: Name -> Q Dec
mkPredicate n = sigD (predicateName n) [t|$res -> $v Bool|]
\end{lstHaskell}

\subsection{Interpreter instance generation}\label{sec_fcd:interpreter}
Generating the interpreter for the \gls{DSL} means generating the class instance for the \haskellinline{Interpreter} data type using the \haskellinline{instanceD} function.
The first argument of the instance is the context, this is left empty.
The second argument of the instance is the type, the \haskellinline{Interpreter} data type applied to the class name.
Finally, the class function instances are generated using the information derived from the structure of the type.
The structure for generating the function instances is very similar to the class definition, only for the function instances of the constructor predicates, the field information is required as well as the constructor names.

\begin{lstHaskell}
mkInterpreter :: Q Dec
mkInterpreter = instanceD (cxt [])
        [t|$(conT (className typeName)) Interpreter|]
    (  zipWith mkConstructor consNames fields
    ++ zipWith mkDeconstructor consNames fields
    ++ zipWith mkPredicate consNames fields)
  where ...
\end{lstHaskell}

\subsubsection{Constructors}
The interpreter is a view on the \gls{DSL} that immediately executes all operations in the \haskellinline{Maybe} monad.
Therefore, the constructor function can be implemented by lifting the actual constructor to the \haskellinline{Maybe} type using sequential application.
I.e.\ for a constructor $C_k$ this results in the following constructor: \haskellinline{ck a0 ... am = pure Ck <*> a0 <*> ... <*> am}.
To avoid accidental shadowing, fresh names for all the arguments are generated.
The \haskellinline{ifx} function is used as a shorthand for defining infix expressions\footnotemark.
\begin{lrbox}{\LstBox}
	\begin{lstHaskell}[frame=]
ifx :: String -> Q Exp -> Q Exp -> Q Exp
ifx op a b = infixE (Just a) (varE (mkName op)) (Just b)\end{lstHaskell}
\end{lrbox}
\footnotetext{\usebox{\LstBox}}

\begin{lstHaskell}
mkConstructor :: Name -> [VarBangType] -> Q Dec
mkConstructor consName fs = do
    fresh <- sequence [newName "a" | _<-fs]
    fun (constructorName consName) (map varP fresh)
        (foldl (ifx "<*>") [|pure $(conE consName)|]
        (map varE fresh))
\end{lstHaskell}


\subsubsection{Deconstructors}
In the case of a deconstructor a function with two arguments is created: the object itself (\haskellinline{f}) and the function doing something with the individual fields (\haskellinline{d}).
To avoid accidental shadowing first fresh names for the arguments and fields are generated.
Then, a function is created with the two arguments.
First \haskellinline{d} is evaluated and bound to a host language function that deconstructs the constructor and passes the fields to \haskellinline{f}.
I.e.\ a deconstructor function $C_k$ is defined as: \haskellinline{unCk d f = d >>= \\(Ck a0 .. am)->f (pure a0) ... (pure am))}\footnotemark.
\footnotetext{
	The \haskellinline{nameBase :: Name -> String} function from the \gls{TH} library is used to convert a name to a string.
}

\begin{lstHaskell}
mkDeconstructor :: Name -> [VarBangType] -> Q Dec
mkDeconstructor consName fs = do
    d <- newName "d"
    f <- newName "f"
    fresh <- mapM (newName . nameBase . fst3) fs
    fun (deconstructorName consName) [varP d, varP f]
        [|$(varE d) >>= \($(match f))->$(fapp f fresh)|]
  where fapp f = foldl appE (varE f) . map (\f->[|pure $(varE f)|])
        match f = pure (ConP consName (map VarP f))
\end{lstHaskell}

\subsubsection{Constructor predicates}
Constructor predicates evaluate the argument and make a case distinction on the result to determine the constructor.
To be able to generate a valid pattern in the case distinction, the total number of fields must be known.
To avoid having to explicitly generate a fresh name for the first argument, a lambda function is used.
In general, the constructor selector for $C_k$ results in the following code \haskellinline{isCk f = f >>= \\case Ck _ ... _ -> pure True; _ -> pure False}.
Generating this code is done with the following function:

\begin{lstHaskell}
mkPredicate :: Name -> [(Var, Bang, Type)] -> Q Dec
mkPredicate n fs = fun (predicateName n) []
    [|\x->x >>= \case
        $(conP n [wildP | _<-fs]) -> pure True
        _                         -> pure False|]
\end{lstHaskell}

\subsection{Pretty printer instance generation}\label{sec_fcd:prettyprinter}
Generating the printer happen analogously to the interpreter, a class instance for the \haskellinline{Printer} data type using the \haskellinline{instanceD} function.

\begin{lstHaskell}
mkPrinter :: Q Dec
mkPrinter = instanceD (cxt []) [t|$(conT (className typeName)) Printer|]
    (  zipWith mkConstructor consNames fields
    ++ zipWith mkDeconstructor consNames fields
    ++ map mkPredicate consNames)
\end{lstHaskell}

To be able to define a printer that is somewhat more powerful, we provide instances for \haskellinline{MonadWriter}; add a state for fresh variables and a context; and define some helper functions the \haskellinline{Printer} datatype.
The \haskellinline{printLit} function is a variant of \haskellinline{MonadWriter}s \haskellinline{tell} that prints a literal string, but it can be of any type (it is a phantom type anyway).
\haskellinline{printCons} prints a constructor name followed by an expression, it inserts parenthesis only when required depending on the state.
\haskellinline{paren} always prints parenthesis around the given printer.
\haskellinline{>->} is a variant of the sequence operator \haskellinline{>>} from the \haskellinline{Monad} class, it prints whitespace in between the arguments.

\begin{lstHaskell}
printLit  :: String -> Printer a
printCons :: String -> Printer a -> Printer a
paren     :: Printer a -> Printer a
(>->)     :: Printer a1 -> Printer a2 -> Printer a3
pl        :: String -> Q Exp
\end{lstHaskell}

\subsubsection{Constructors}
For a constructor $C_k$ the printer is defined as: \haskellinline{ck a0 ... am = printCons "Ck" (printLit "" >-> a0 >-> ... >-> am)}.
To generate the second argument to the \haskellinline{printCons} function, a fold is used with \haskellinline{printLit ""} as the initial element to account for constructors without any fields as well, e.g.\ \haskellinline{Nil} is translated to \haskellinline{nil = printCons "Nil" (printLit "")}.

\begin{lstHaskell}
mkConstructor :: Name -> [VarBangType] -> Q Dec
mkConstructor consName fs = do
    fresh <- sequence [newName "f" | _<- fs]
    fun (constructorName consName) (map varP fresh)
        (pcons `appE` pargs fresh)
  where pcons = [|printCons $(lift (nameBase consName))|]
        pargs fresh = foldl (ifx ">->") (pl "")
            (map varE fresh)
\end{lstHaskell}

\subsubsection{Deconstructors}
Printing the deconstructor for $C_k$ is defined as:
\begin{lstHaskell}
unCk d f
    =  printLit "unCk d"
    >-> paren (
           printLit "\(Ck" >-> printLit "a0 ... am" >> printLit ")->"
        >> f (printLit "a0") ... (printLit "am")
    )
\end{lstHaskell}

The implementation for this is a little elaborate and it heavily uses the \haskellinline{pl} function, a helper function that translates a string literal \haskellinline{s} to \haskellinline{[|printLit \$(lift s)|]}, i.e.\ it lifts the \haskellinline{printLit} function to the \gls{TH} domain.

\begin{lstHaskell}
mkDeconstructor :: Name -> [VarBangType] -> Q Dec
mkDeconstructor consName fs = do
    d <- newName "d"
    f <- newName "f"
    fresh <- sequence [newName "a" | _<-fs]
    fun (deconstructorName consName) (map varP [d, f])
        [| $(pl (nameBase (deconstructorName consName)))
        >-> $(pl (nameBase d))
        >-> paren ($(pl ('\\':'(':nameBase consName))
                   >-> $lam >> printLit ")->"
                   >>  $(hoas f))|]
  where
    lam = pl $ unwords [nameBase f | (f, _, _)<-fs]
    hoas f = foldl appE (varE f)
        [pl (nameBase f) | (f, _, _)<-fs]
\end{lstHaskell}

\subsubsection{Constructor predicates}
For the printer, the constructor selector for $C_k$ results in the following code \haskellinline{isCk f = printLit "isCk" >-> f}.

\begin{lstHaskell}
mkPredicate :: Name -> Q Dec
mkPredicate n = fun (predicateName n) []
    [|\x-> $(pl $ nameBase $ predicateName n) >-> x|]
\end{lstHaskell}

\section{Pattern matching}
It is possible to construct and deconstruct values from other \gls{DSL} expressions, and to perform tests on the constructor but with a clunky and unwieldy syntax.
They have become first-class citizens in a grotesque way.
For example, given that we have some language constructs to denote failure and conditionals\footnotemark, writing a list summation function in our \gls{DSL} would be done as follows.
For the sake of the argument we take a little shortcut here and assume that the interpretation of the \gls{DSL} supports lazy evaluation by using the host language as a metaprogramming language as well, allowing us to use functions in the host language to construct expressions in the \gls{DSL}.

\begin{lrbox}{\LstBox}
	\begin{lstHaskell}[frame=,deletekeywords={if}]
class Support v where
    if'    :: v Bool -> v a -> v a -> v a
    bottom :: String -> v a\end{lstHaskell}
\end{lrbox}
\footnotetext{\usebox{\LstBox}}

\begin{lstHaskell}[deletekeywords={if}]
program :: (ListDSL v, Support v, ...) => v Int
program
    = fun \sum->(\l->if'(isNil l)
        (lit 0)
        (unCons l (\hd tl->hd +. sum tl)))
    :- sum (cons (lit 38) (cons (lit 4) nil))
\end{lstHaskell}

A similar \gls{HASKELL} implementation is much more elegant and less cluttered because of the support for pattern matching.
Pattern matching offers a convenient syntax for doing deconstruction and constructor tests at the same time.

\begin{lstHaskell}
sum :: List Int -> Int
sum Nil = 0
sum (List hd tl) = hd + sum tl

main = sum (Cons 38 (Cons 4 Nil))
\end{lstHaskell}

\subsection{Custom quasiquoters}
The syntax burden of \glspl{EDSL} can be reduced using quasiquotation.
In \gls{TH}, quasiquotation is a convenient way to create \gls{HASKELL} language constructs by entering them verbatim using Oxford brackets.
However, it is also possible to create so-called custom quasiquoters \citep{mainland_why_2007}.
If the programmer writes down a fragment of code between tagged \emph{Oxford brackets}, the compiler executes the associated quasiquoter functions at compile time.
A quasiquoter is a value of the following data type:

\begin{lstHaskell}
data QuasiQuoter = QuasiQuoter
    { quoteExp  :: String -> Q Exp
    , quotePat  :: String -> Q Pat
    , quoteType :: String -> Q Type
    , quoteDec  :: String -> Q Dec
    }
\end{lstHaskell}

The code between \emph{dsl} brackets (\haskellinline{[dsl\|...\|]}) is preprocessed by the \haskellinline{dsl} quasiquoter.
Because the functions are executed at compile time, errors---thrown using the \haskellinline{MonadFail} instance of the \haskellinline{Q} monad---in these functions result in compile time errors.
The \gls{AST} nodes produced by the quasiquoter are inserted into the location and checked as if they were written by the programmer.

To illustrate writing a custom quasiquoter, we show an implementation of a quasiquoter for binary literals.
The \haskellinline{bin} quasiquoter is only defined for expressions and parses subsequent zeros and ones as a binary number and splices it back in the code as a regular integer.
Thus, \haskellinline{[bin\|101010\|]} results in the literal integer expression \haskellinline{42}.
If an invalid character is used, a compile-time error is shown.
The quasiquoter is defined as follows:

\begin{lstHaskell}
bin :: QuasiQuoter
bin = QuasiQuoter { quoteExp = parseBin }
  where
    parseBin :: String -> Q Exp
    parseBin s = LitE . IntegerL <$> foldM bindigit 0 s

    bindigit :: Integer -> Char -> Q Integer
    bindigit acc '0' = pure (2 * acc)
    bindigit acc '1' = pure (2 * acc + 1)
    bindigit acc c = fail ("invalid char: " ++ show c)
\end{lstHaskell}

\subsection{Quasiquotation for pattern matching}
Custom quasiquoters allow the \gls{DSL} user to enter fragments verbatim, bypassing the syntax of the host language.
Pattern matching in general is not suitable for a custom quasiquoter because it does not really fit in one of the four syntactic categories for which custom quasiquoter support is available.
However, a concrete use of pattern matching, interesting enough to be beneficial, but simple enough for a demonstration is the \emph{simple case expression}, a case expression that does not contain nested patterns and is always exhaustive.
They correspond to multi-way conditional expressions and can thus be converted to \gls{DSL} constructs straightforwardly \citep[\citesection{4.4}]{peyton_jones_implementation_1987}.

In contrast to the binary literal quasiquoter example, we do not create the parser by hand.
The parser combinator library \emph{parsec} is used instead to ease the creation of the parser \citep{leijen_parsec_2001}.
First the location of the quasiquoted code is retrieved using the \haskellinline{location} function that operates in the \haskellinline{Q} monad.
This location is inserted in the parsec parser so that errors are localised in the source code.
Then, the \haskellinline{expr} parser is called that returns an \haskellinline{Exp} in the \haskellinline{Q} monad.
The \haskellinline{expr} parser uses parsec's commodity expression parser primitive \haskellinline{buildExpressionParser}.
The resulting parser translates the string directly into \gls{TH}'s \gls{AST} data types in the \haskellinline{Q} monad.
The most interesting parser is the parser for the case expression that is an alternative in the basic expression parser \haskellinline{basic}.
A case expression is parsed when a keyword \haskellinline{case} is followed by an expression that is in turn followed by a non-empty list of matches.
A match is parsed when a pattern (\haskellinline{pat}) is followed by an arrow and an expression.
The results of this parser are fed into the \haskellinline{mkCase} function that transforms the case into an expression using \gls{DSL} primitives such as conditionals, deconstructors and constructor predicates.
The above translates to the following skeleton implementation:

\begin{lstHaskell}
expr :: Parser (Q Exp)
expr = buildExpressionParser [...] basic
  where
    basic :: Parser (Q Exp)
    basic =   ...
        <|> mkCase <$ reserved "case" <*> expr
                   <* reserved "of"   <*> many1 match
        <|> ...

    match :: Parser (Q Pat, Q Exp)
    match = (,) <$> pat <* reserved "->" <*> expr

    pat :: Parser (Q Pat)
    pat = conP <$> con <*> many var
\end{lstHaskell}

Case expressions are transformed into constructors, deconstructors and constructor predicates, e.g.\ \haskellinline{case e1 of Cons hd tl -> e2; Nil -> e3;} is converted to:
\begin{lstHaskell}[deletekeywords={if}]
if' (isList e1)
    (unCons e1 (\hd tl->e2))
    (if' (isNil e1)
        (unNil e1 e3)
        (bottom "Exhausted case"))
\end{lstHaskell}

The \haskellinline{mkCase} (\cref{mkcase_fcd:mkcase}) function transforms a case expression into constructors, deconstructors and constructor predicates.
\Cref{mkcase_fcd:eval} first evaluates the patterns.
Then the patterns and their expressions are folded using the \haskellinline{mkCase`} function (\cref{mkcase_fcd:pairs}).
While a case exhaustion error is used as the initial value, this is never called since all case expressions are exhaustive.
For every case, code is generated that checks whether the constructor used in the pattern matches the constructor of the value using constructor predicates (\cref{mkcase_fcd:conspred}).
If the constructor matches, the deconstructor (\cref{mkcase_fcd:consdec}) is used to bind all names to the correct identifiers and evaluate the expression.
If the constructor does not match, the continuation (\haskellinline{\$rest}) is used (\cref{mkcase_fcd:consstart}).

\begin{lstHaskell}[numbers=left,deletekeywords={if}]
mkCase :: Q Exp -> [(Q Pat, Q Exp)] -> Q Exp [+\label{mkcase_fcd:mkcase} +]
mkCase name cases = do
    pats <- mapM fst cases [+ \label{mkcase_fcd:eval} +]
    foldr (uncurry mkCase') [|bottom "Exhausted case"|][+ \label{mkcase_fcd:fold}\label{mkcase_fcd:foldinit} +]
        (zip pats (map snd cases)) [+\label{mkcase_fcd:pairs}+]
  where
    mkCase' :: Pat -> Q Exp -> Q Exp -> Q Exp
    mkCase' (ConP cons fs) e rest
        = [|if' $pred $then_ $rest|] [+\label{mkcase_fcd:consstart}+]
      where
        pred  = varE (predicateName cons) `appE` name[+\label{mkcase_fcd:conspred}+]
        then_ = [|$(varE (deconstructorName cons))[+\label{mkcase_fcd:consdec}+]
                 $name $(lamE [pure f | f<-fs] e)|][+\label{mkcase_fcd:consend}+]
\end{lstHaskell}

Finally, with this quasiquotation mechanism we can define our list summation using a case expression.
As a byproduct, syntactic cruft such as the special symbols for the operators and calls to \haskellinline{lit} can be removed as well resulting in the following summation implementation:

\begin{lstHaskell}
program :: (ListDSL v, DSL v, ...) => v Int
program
    = fun \sum->(\l->[dsl|case l of
        Cons hd tl -> hd + sum tl
        Nil        -> 0|])
    :- sum (cons (lit 38) (cons (lit 4) nil))
\end{lstHaskell}

\section{Related work}
Generic or polytypic programming is a promising technique at first glance for automating the generation of function implementations \citep{lammel_scrap_2003}.
However, while it is possible to define a function that works on all first-order types, adding a new function with a new name to the language is not possible.
This does not mean that generic programming is not useable for embedding pattern matches.
In generic programming, types are represented as sums of products and using this representation it is possible to define pattern matching functions.

For example, \citet{rhiger_type-safe_2009} showed a method for expressing statically typed pattern matching using typed higher-order functions.
If not the host language but the \gls{DSL} contains higher order functions, the same technique could be applied to port pattern matching to \glspl{DSL} though using an explicit sums of products representation.
\Citeauthor{atkey_unembedding_2009} describe embedding pattern matching in a \gls{DSL} by giving patterns an explicit representation in the \gls{DSL} by using pairs, sums and injections \citep[\citesection{3.3}]{atkey_unembedding_2009}.

\Citet{mcdonell_embedded_2022} extends on this idea, resulting in a very similar but different solution to ours.
They used the technique that \citeauthor{atkey_unembedding_2009} showed and applied it to deep embedding using the concrete syntax of the host language.
The scaffolding---e.g.\ generating the pairs, sums and injections---for embedding is automated using generics but the required pattern synonyms are generated using \gls{TH}.
The key difference to our approach is that we specialise the implementation for each of the interpretations instead of providing a general implementation of data type handling operations.
Furthermore, our implementation does not require a generic function to trace all constructors, resulting in problems with (mutual) recursion.

\Citet{young_adding_2021} added pattern matching to a deeply \gls{EDSL} using a compiler plugin.
This plugin implements an \haskellinline{externalise :: a -> E a} function that allows lifting all machinery required for pattern matching automatically from the host language to the \gls{DSL}.
Under the hood, this function translates the pattern match to constructors, deconstructors, and constructor predicates.
The main difference with this work is that it requires a compiler plugin while our metaprogramming approach works on any compiler supporting a metaprogramming system similar to \gls{TH}.

\subsection{Related work on Template Haskell}
Metaprogramming in general is a very broad research topic and has been around for years already.
We therefore do not claim an exhaustive overview of related work on all aspects of metaprogramming.
However, we have tried to present most research on metaprogramming in \gls{TH}.
\Citet{czarnecki_dsl_2004} provide a more detailed comparison of different metaprogramming techniques.
They compare staged interpreters, metaprogramming and templating by comparing MetaOCaml, \gls{TH} and \gls{CPP} templates.
\gls{TH} has been used to implement related work.
They all differ slightly in functionality from our domain and can be divided into several categories.

\subsubsection{Generating extra code}
Using \gls{TH} or other metaprogramming systems it is possible to add extra code to your program.
The original \gls{TH} paper showed that it is possible to create variadic functions such as \haskellinline{printf} using \gls{TH} that would be almost impossible to define without \citep{sheard_template_2002}.
\Citet{hammond_automatic_2003} used \gls{TH} to generate parallel programming skeletons.
In practise, this means that the programmer selects a skeleton and, at compile time, the code is massaged to suit the pattern and information about the environment is inlined for optimisation.

\Citet{polak_automatic_2006} implemented automatic GUI generation using \gls{TH}.
\Citet{duregard_embedded_2011} wrote a parser generator using \gls{TH} and the custom quasiquoting facilities.
From a specification of the grammar, given in verbatim using a custom quasiquoter, a parser is generated at compile time.
\Citet{shioda_libdsl_2014} used metaprogramming in the D programming language to create a \gls{DSL} toolkit.
They also programmatically generate parsers and an interpretation for either compiling or interpreting the \gls{IR}.
\Citet{blanchette_liquid_2022} use \gls{TH} to simplify the development of Liquid \gls{HASKELL} proofs.
\Citet{folmer_high-level_2022} used \gls{TH} to synthesize C$\lambda$aSH \citep{baaij_digital_2015} \glspl{AST} to be processed.
In similar fashion, \citet{materzok_generating_2022} used \gls{TH} to translate YieldFSM programs to {C$\lambda$aSH}.

\subsubsection{Optimisation}
Besides generating code, it is also possible to analyse existing code and perform optimisations.
Yet, this is dangerous territory because unwantedly, the semantics of the optimised program may be slightly different from the original program.
For example, \citet{lynagh_unrolling_2003} implemented various optimisations in \gls{TH} such as automatic loop unrolling.
The compile-time executed functions analyse the recursive function and unroll the recursion to a fixed depth to trade execution speed for program space.
Also, \citet{odonnell_embedding_2004} embedded Hydra, a hardware description language, in \gls{HASKELL} utilising \gls{TH}.
Using intensional analysis of the \gls{AST}, it detects cycles by labelling nodes automatically so that it can generate \emph{netlists}.
The authors mention that alternatively this could have be done using a monad but this hampers equational reasoning greatly, which is a key property of Hydra.
Finally, \citet{viera_staged_2018} present a way of embedding attribute grammars in \gls{HASKELL} in a staged fashion.
Checking several aspects of the grammar is done at compile time using \gls{TH} while other safety checks are performed at runtime.

\subsubsection{Compiler extension}
Sometimes, expressing certain functionalities in the host languages requires a lot of boilerplate, syntax wrestling, or other pains.
Metaprogramming can relieve some of this stress by performing this translation to core constructs automatically.
For example, implementing generic---or polytypic--- functions in the compiler is a major effort.
\Citet{norell_prototyping_2004} used \gls{TH} to implement the machinery required to implement generic functions at compile time.
\Citet{adams_template_2012} also explores implementing generic programming using \gls{TH} to speed things up considerably compared to regular generic programming.
\Citet{clifton-everest_embedding_2014} use \gls{TH} with a custom quasiquoter to offer skeletons for workflows and embed foreign function interfaces in a \gls{DSL}.
\Citet{eisenberg_promoting_2014} showed that it is possible to programmatically lift some functions from the function domain to the type domain at compile time, i.e.\ type families.
Furthermore, \citet{seefried_optimising_2004} argued that it is difficult to do some optimisations in \glspl{EDSL} and that metaprogramming can be of use there.
They use \gls{TH} to change all types to unboxed types, unroll loops to a certain depth and replace some expressions by equivalent more efficient ones.
\Citet{torrano_strictness_2005} showed that it is possible to use \gls{TH} to perform a strictness analysis and perform let-to-case translation.
Both applications are examples of compiler extensions that can be implemented using \gls{TH}.
Another example of such a compiler extension is shown by \citet{gill_haskell_2009}.
They created a meta level \gls{DSL} to describe rewrite rules on \gls{HASKELL} syntax that are applied on the source code at compile time.

\subsubsection{Quasiquotation}
By means of quasiquotation, the host language syntax that usually seeps through the embedding can be hidden.
The original \gls{TH} quasiquotation paper \citep{mainland_why_2007} shows how this can be done for regular expressions, not only resulting in a nicer syntax but syntax errors are also lifted to compile time instead of run time.
Also, \citet{kariotis_making_2008} used \gls{TH} to automatically construct monad stacks without having to resort to the monad transformers library which requires advanced type system extensions.

\Citet{najd_everything_2016} uses the compile time to be able to do normalisation for a \gls{DSL}, dubbing it \glspl{QDSL}.
They utilise the quasiquation facilities of \gls{TH} to convert \gls{HASKELL} \gls{DSL} code to constructs in the \gls{DSL}, applying optimisations such as eliminating lambda abstractions and function applications along the way.
\Citet{egi_embedding_2022} extended \gls{HASKELL} to support non-free data type pattern matching---i.e.\ data type with no standard form, e.g.\ sets, graphs---using \gls{TH}.
Using quasiquotation, they make a complicated embedding of non-linear pattern matching available through a simple lens.

\subsubsection{Typed Template Haskell}\label{ssec_fcd:typed_template_haskell}
\Gls{TTH} is a very recent extension/alternative to normal \gls{TH} \citep{pickering_multi-stage_2019,xie_staging_2022}.
Where in \gls{TH} you can manipulate arbitrary parts of the syntax tree, add top-level splices of data types, definitions and functions, in \gls{TTH} the programmer can only splice expressions but the \gls{AST} fragments representing the expressions are well-typed by construction instead of untyped.

\Citet{pickering_staged_2020} implemented staged compilation for the \emph{generics-sop} \citep{de_vries_true_2014} generics library to improve the efficiency of the code using \gls{TTH}.
\Citet{willis_staged_2020} used \gls{TTH} to remove the overhead of parsing combinators.

\section{Discussion}
This chapter aims to be twofold, first, it shows how to inherit data types in a \gls{DSL} as first-class citizens by generating the boilerplate at compile time using \gls{TH}.
Secondly, it introduces the reader to \gls{TH} by giving an overview of the literature in which \gls{TH} is used and provides a gentle introduction by explaining the case study.

\Gls{FP} languages are especially suitable for embedding \glspl{DSL} but adding user-defined data types is still an issue.
The tagless-final style of embedding offers great modularity, extensibility and flexibility.
However, user-defined data types are awkward to handle because the built-in operations on them---construction, deconstruction and constructor tests---are not inherited from the host language.
We showed how to create a \gls{TH} function that will splice the required class definitions and view instances.
The code dataset also contains an implementation for defining field selectors and provides an implementation for a compiler (see \cref{chp:research_data_management}).
Furthermore, by writing a custom quasiquoter, pattern matches in natural syntax can be automatically converted to the internal representation of the \gls{DSL}, thus removing the syntax burden of the facilities.
The use of a custom quasiquoter does require the \gls{DSL} programmer to write a parser for their \gls{DSL}, i.e.\ the parser is not inherited from the host language as is often the case in an embedded \gls{DSL}.
However, by making use of modern parser combinator libraries, this overhead is limited and errors are already caught at compilation.

\subsection{Future work}
For future work, it would be interesting to see how generating boilerplate for user-defined data types translates from shallow embedding to deep embedding.
In deep embedding, the language constructs are expressed as data types in the host language.
Adding new constructs, e.g.\ constructors, deconstructors, and constructor tests, for the user-defined data type therefore requires extending the data type.
Techniques such as data types \`a la carte \citep{swierstra_data_2008} and open data types \citep{loh_open_2006} show that it is possible to extend data types orthogonally but whether metaprogramming can still readily be used is something that needs to be researched.
It may also be possible to implemented (parts) of the boilerplate generation using \gls{TTH} (see \cref{ssec_fcd:typed_template_haskell}) to achieve more confidence in the type correctness of the implementation.

Another venue of research is to try to find the limits of this technique regarding richer data type definitions.
It would be interesting to see whether it is possible to apply the technique on data types with existentially quantified type variables or full-fledged generalised \glspl{ADT} \citep{hinze_fun_2003}.
It is not possible to straightforwardly lift the deconstructors to type classes because existentially quantified type variables will escape.
Rank-2 polymorphism offers tools to define the types in such a way that this is not the case anymore.
However, implementing compiling views on the \gls{DSL} is complicated because it would require inventing values of an existentially quantified type variable to satisfy the type system which is difficult.

Finally, having to write a parser for the \gls{DSL} is extra work.
Future research could determine whether it is possible to generate this using \gls{TH} as well.

\input{subfilepostamble}
\end{document}
