updates
[phd-thesis.git] / dsl / first-class_datatypes.tex
index 2f3464c..e269a22 100644 (file)
@@ -1,14 +1,13 @@
 \documentclass[../thesis.tex]{subfiles}
 
-\begin{document}
-\ifSubfilesClassLoaded{
-       \pagenumbering{arabic}
-}{}
+\include{subfilepreamble}
 
-\chapter[First-class data types in shallow \acrshortpl{EDSL} using metaprogramming]{First-class data types in shallow \acrlongpl{EDSL} using metaprogramming}%
+\begin{document}
+\chapter{First-class data types in shallow \texorpdfstring{embedded domain-specific languages}{\glsxtrlongpl{EDSL}} using metaprogramming}%
+\chaptermark{bork}%
 \label{chp:first-class_datatypes}%
 \begin{chapterabstract}
-       Functional programming languages are excellent candidates for hosting \glspl{EDSL} because of their rich type systems, minimal syntax, and referential transparency.
+       \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.
 
 \end{chapterabstract}
 
 \section{Introduction}
-Functional programming languages are excellent candidates for hosting \glspl{EDSL} because of their rich type systems, minimal syntax, and referential transparency.
+\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 a functional language are deep embedding (also called initial) and shallow embedding (also called final).
+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.
@@ -75,7 +74,7 @@ infixl 6 +.
 
 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{}.
+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
@@ -114,11 +113,11 @@ instance Div Maybe where
 
 \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{}.
+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[Sec.~4.2.3]{peyton_jones_haskell_2003}.
+       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}:
 
@@ -142,25 +141,24 @@ instance Div Printer where
 \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 function by supplying the arguments in a tuple.
-Furthermore, by defining the \haskellinline{Main} type, the \gls{DSL} forces all functions to be defined at the top level and with the \haskellinline{:-} operator the syntax becomes usable.
+Furthermore, with the \haskellinline{:-} operator the syntax becomes usable.
 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) (Main (v u)) ) -> Main (v u)
-newtype Main a = Main { unmain :: a }
+    fun :: ((a -> v s) -> In (a -> v s) (v u)) -> v u
 data In a b = a :- b
 infix 1 :-
 \end{lstHaskell}
 
-Using the \haskellinline{Function} type class can be used to define functions with little syntactic overhead\footnote{The \GHCmod{LambdaCase} extension of GHC is used to reduce the number of brackets that allows lambda's to be an argument to a function without brackets or explicit function application using \haskellinline{\$}}.
+Using the \haskellinline{Function} type class can be used to define functions with little syntactic overhead.\footnote{The \GHCmod{LambdaCase} extension of GHC is used to reduce the number of brackets that allows lambda's to be an argument to a function without brackets or explicit function application using \haskellinline{\$}}
 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    )
-:- Main (increment (divide (lit 38, lit 5)))
+:- 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.
@@ -169,7 +167,7 @@ Because the laziness of \gls{HASKELL}'s lazy let bindings, this results in a fix
 
 \begin{lstHaskell}
 instance Function a Maybe where
-    fun def = Main (let g :- m = def g in unmain m)
+    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.
@@ -186,13 +184,13 @@ To illustrate this, the instance for unary functions is shown, all other arities
 \begin{lstHaskell}
 instance Function () Printer where ...
 instance Function (Printer a) Printer where ...
-    fun def = Main $ freshLabel >>= \f->
+    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 "] >> unmain m
+        >>  tell [" in "] >> m
 instance Function (Printer a, Printer b) Printer where ...
 \end{lstHaskell}
 
@@ -262,7 +260,7 @@ Metaprogramming is a special flavour of programming where programs have the abil
 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[Sec.~6.13.1]{ghc_team_ghc_2021}.
+\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 AST data types, splicing, quasiquotation and reification.
@@ -341,13 +339,13 @@ Reification is the act of querying the compiler for information about a certain
 For example, reifying a type name results in information about the type and the corresponding 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.
+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 \gls{DSL} functions}
+\section{Metaprogramming for generating \texorpdfstring{\glsxtrshort{DSL}}{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{}.
+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.
 }
@@ -496,7 +494,7 @@ The interpreter is a view on the \gls{DSL} that immediately executes all operati
 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{}
+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
@@ -520,7 +518,7 @@ In the case of a deconstructor a function with two arguments is created: the obj
 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{}.
+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.
 }
@@ -649,12 +647,12 @@ class Support v where
 \footnotetext{\usebox{\LstBox}}
 
 \begin{lstHaskell}
-program :: (ListDSL v, Support v, ...) => Main (v Int)
+program :: (ListDSL v, Support v, ...) => v Int
 program
     = fun \sum->(\l->if'(isNil l)
         (lit 0)
         (unCons l (\hd tl->hd +. sum tl)))
-    :- Main (sum (cons (lit 38) (cons (lit 4) nil)))
+    :- 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.
@@ -711,7 +709,7 @@ bin = QuasiQuoter { quoteExp = parseBin }
 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 a multi-way conditional expressions and can thus be converted to \gls{DSL} constructs straightforwardly~\citep[Chp.~4.4]{peyton_jones_implementation_1987}.
+They correspond to a 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}.
@@ -780,12 +778,12 @@ Finally, with this quasiquotation mechanism we can define our list summation usi
 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, ...) => Main (v Int)
+program :: (ListDSL v, DSL v, ...) => v Int
 program
     = fun \sum->(\l->[dsl|case l of
         Cons hd tl -> hd + sum tl
         Nil        -> 0|])
-    :- Main (sum (cons (lit 38) (cons (lit 4) nil)))
+    :- sum (cons (lit 38) (cons (lit 4) nil))
 \end{lstHaskell}
 
 \section{Related work}
@@ -794,93 +792,96 @@ However, while it is possible to define a function that works on all first-order
 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, Rhiger showed a method for expressing statically typed pattern matching using typed higher-order functions~\citep{rhiger_type-safe_2009}.
+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.
-Atkey et al.\ 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[Sec.~3.3]{atkey_unembedding_2009}.
+\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}.
 
-McDonell et al.\ extends on this idea, resulting in a very similar but different solution to ours~\citep{mcdonell_embedded_2022}.
-They used the technique that Atkey et al.\ showed and applied it to deep embedding using the concrete syntax of the host language.
+\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 backends 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.
 
-Young et al.\ added pattern matching to a deeply \gls{EDSL} using a compiler plugin~\citep{young_adding_2021}.
+\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 \gls{TH}}
+\subsection{Related work on \texorpdfstring{\glsxtrlong{TH}}{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 have tried to present most research on metaprogramming in \gls{TH}.
-Czarnecki et al.\ provide a more detailed comparison of different metaprogramming techniques.
-They compare staged interpreters, metaprogramming and templating by comparing MetaOCaml, \gls{TH} and C++ templates~\citep{czarnecki_dsl_2004}.
+\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}.
-Hammond et al.\ used \gls{TH} to generate parallel programming skeletons~\citep{hammond_automatic_2003}.
+\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.
 
-Polak et al.\ implemented automatic GUI generation using \gls{TH}~\citep{polak_automatic_2006}.
-Dureg\aa{}rd et al.\ wrote a parser generator using \gls{TH} and the custom quasiquoting facilities~\citep{duregard_embedded_2011}.
+\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.
-Shioda et al.\ used metaprogramming in the D programming language to create a \gls{DSL} toolkit~\citep{shioda_libdsl_2014}.
+\Citet{shioda_libdsl_2014} used metaprogramming in the D programming language to create a \gls{DSL} toolkit.
 They also programmatically generate parsers and a backend for either compiling or interpreting the \gls{IR}.
-Blanchette et al.\ use \gls{TH} to simplify the development of Liquid \gls{HASKELL} proofs~\citep{blanchette_liquid_2022}.
-Folmer et al.\ used \gls{TH} to synthesize C$\lambda$aSH~\citep{baaij_digital_2015} abstract syntax trees to be processed~\citep{folmer_high-level_2022}.
-In similar fashion, Materzok used \gls{TH} to translate YieldFSM programs to  C$\lambda$aSH~\citep{materzok_generating_2022}.
+\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} abstract syntax trees 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, Lynagh implemented various optimisations in \gls{TH} such as automatic loop unrolling~\citep{lynagh_unrolling_2003}.
+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, O'Donnell embedded Hydra, a hardware description language, in \gls{HASKELL} utilising \gls{TH}~\citep{odonnell_embedding_2004}.
+Also, \citet{odonnell_embedding_2004} embedded Hydra, a hardware description language, in \gls{HASKELL} utilising \gls{TH}.
 Using intensional analysis of the 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, Viera et al.\ present a way of embedding attribute grammars in \gls{HASKELL} in a staged fashion~\citep{viera_staged_2018}.
+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.
-Norell et al.\ used \gls{TH} to implement the machinery required to implement generic functions at compile time~\citep{norell_prototyping_2004}.
-Adams et al.\ also explores implementing generic programming using \gls{TH} to speed things up considerably compared to regular generic programming~\citep{adams_template_2012}.
-Clifton et al.\ use \gls{TH} with a custom quasiquoter to offer skeletons for workflows and embed foreign function interfaces in a \gls{DSL}~\citep{clifton-everest_embedding_2014}.
-Eisenberg et al.\ 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~\citep{eisenberg_promoting_2014}.
-Furthermore, Seefried et al.\ argued that it is difficult to do some optimisations in \glspl{EDSL} and that metaprogramming can be of use there~\citep{seefried_optimising_2004}.
+\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.
-Torrano et al.\ showed that it is possible to use \gls{TH} to perform a strictness analysis and perform let-to-case translation~\citep{torrano_strictness_2005}.
+\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 Gill et al.~\citep{gill_haskell_2009}.
+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, Kariotis et al.\ used \gls{TH} to automatically construct monad stacks without having to resort to the monad transformers library which requires advanced type system extensions~\citep{kariotis_making_2008}.
+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.
 
-Najd uses the compile time to be able to do normalisation for a \gls{DSL}, dubbing it \glspl{QDSL}~\citep{najd_everything_2016}.
+\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.
-Egi et al.\ 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}~\citep{egi_embedding_2022}.
+\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{\gls{TTH}}\label{ssec_fcd:typed_template_haskell}
+\subsubsection{\texorpdfstring{\glsxtrlong{TTH}}{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 abstract syntax tree fragments representing the expressions are well-typed by construction instead of untyped.
 
-Pickering et al.\ 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}~\citep{pickering_staged_2020}.
-Willis et al.\ used \gls{TTH} to remove the overhead of parsing combinators~\citep{willis_staged_2020}.
+\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}
-Functional programming languages are especially suitable for embedding \glspl{DSL} but adding user-defined data types is still an issue.
+This paper 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.
-By giving an implementation, we showed how to create a \gls{TH} function that will splice the required class definitions and view instances.
+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}.