fix minor errors, split up preamble
[phd-thesis.git] / dsl / first.tex
index ad70ed3..68a5baf 100644 (file)
@@ -13,7 +13,7 @@
        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 paper shows that by using metaprogramming, all first order user-defined data types can be automatically made first class in shallow \glspl{EDSL}.
+       This paper 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 paper also serves as a gentle introduction to \gls{TH}.
@@ -33,7 +33,7 @@ Consequently, adding a construct is easy, i.e.\ it only entails adding another f
 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, they are not first-class citizens.
+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*}
@@ -48,7 +48,7 @@ Thus, all of the operations on the data type have to be defined by hand requirin
 
 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 AST nodes.
+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 of the paper}
@@ -101,9 +101,9 @@ class Div v where
 infixl 7 /.
 \end{lstHaskell}
 
-Division is an operation that undefined if the right operand is equal to zero.
+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.
-The right-hand side of the division operator is evaluated first.
+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}
@@ -141,7 +141,7 @@ 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.
+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 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:
@@ -153,7 +153,7 @@ 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{BlockArguments} extension of GHC is used to reduce the number of brackets that allows lambda's to be an argument to a function without brackets}
+The \haskellinline{Function} type class is now used to define functions with little syntactic overhead\footnote{The \GHCmod{BlockArguments} extension of 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}
@@ -203,7 +203,7 @@ 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 type of the value has instances for all the class constraints.
+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 the 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.
@@ -261,8 +261,8 @@ Even though it has been around for many years, it is considered complex \citep{s
 \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.
-With this machinery, regular \gls{HASKELL} functions can be defined that are called at compile time, inserting generated code into the {AST}.
+\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.
@@ -271,7 +271,7 @@ For example it can subvert module boundaries, thus accessing constructors that w
 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 AST elements, data types are provided that are mostly isomorphic to the actual data types used in the compiler.
+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:
@@ -288,7 +288,7 @@ data Exp = VarE Name | LitE Lit | AppE Exp Exp | TupE [Maybe Exp]
 data Lit = CharL Char | StringL String | IntegerL Integer | ...
 \end{lstHaskell}
 
-To ease creating AST data types in the \haskellinline{Q} monad, lowercase variants of the constructors are available that lift the constructor to the \haskellinline{Q} monad as.
+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}
@@ -298,7 +298,7 @@ lamE ps es = LamE <$> sequence ps <*> es
 
 \subsection{Splicing}
 Special splicing syntax (\haskellinline{\$(...)}) marks functions for compile-time execution.
-Other than that they always produce a value of an AST data type, they are regular functions.
+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.
@@ -316,7 +316,7 @@ tsel field total = do
 \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 AST nodes easing the creation of language constructs.
+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
@@ -332,7 +332,7 @@ For example, \haskellinline{[\|\\x->x\|]} translates to \haskellinline{newName "
 
 \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 AST nodes of the type's definition.
+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.
@@ -622,7 +622,7 @@ mkPredicate n = fun (predicateName n) []
 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 contstruct expressions in the \gls{DSL}.
+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}]
@@ -670,7 +670,7 @@ data QuasiQuoter = QuasiQuoter
 
 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 AST nodes produced by the quasiquoter are inserted into the location and checked as if they were written by the programmer.
+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.
@@ -703,7 +703,7 @@ First the location of the quasiquoted code is retrieved using the \haskellinline
 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 AST data types in the \haskellinline{Q} monad.
+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.
@@ -814,7 +814,7 @@ From a specification of the grammar, given in verbatim using a custom quasiquote
 \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}.
 \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.
+\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}
@@ -823,7 +823,7 @@ Yet, this is dangerous territory because unwantedly the semantics of the optimis
 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 AST, it detects cycles by labelling nodes automatically so that it can generate \emph{netlists}.
+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.
@@ -855,7 +855,7 @@ Using quasiquotation, they make a complicated embedding of non-linear pattern ma
 
 \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.
+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.