sub_t (P_t e1) (P_t e2) = P_t ("(" ++ e1 ++ "-" ++ e2 ++ ")")
\end{lstHaskellLhstex}
-\section{Lifting the backends}%
+\section{Lifting the interpretations}%
Let us rethink the deeply embedded \gls{DSL} design.
Remember that in shallow embedding, the semantics are embedded in the language construct functions.
Obtaining extensibility both in constructs and semantics was accomplished by abstracting the semantics functions to type classes, making the constructs overloaded in the semantics.
This means that when \haskelllhstexinline{Cons} is pattern matched, the overloading of the type class constraint for \haskelllhstexinline{c dt} can be solved by the compiler.
\GHCmod{KindSignatures} is used to force the kinds of the type parameters and the kind of \haskelllhstexinline{dt} is polymorphic (\GHCmod{PolyKinds}) so that the \haskelllhstexinline{Record} data type can be used for \glspl{DSL} using type classes but also type constructor classes (e.g.\ when using \glspl{GADT}).
-\begin{lstHaskellLhstex}[label={lst_cbde:record_type},caption={Data type for a list of constraints}]
+\begin{lstHaskellLhstex}[label={lst_cbde:record_type},caption={Data type for a list of constraints.}]
data Record (dt :: k) (clist :: [k -> Constraint]) where
Nil :: Record dt '[]
Cons :: c dt => Record dt cs -> Record dt (c ': cs)
To incorporate this type in the \haskelllhstexinline{Expr} type, the \haskelllhstexinline{Ext} constructor changes from containing a single witness dictionary to a \haskelllhstexinline{Record} type containing all the required dictionaries.
-\begin{lstHaskellLhstex}[caption={Data type for a list of constraints}]
+\begin{lstHaskellLhstex}[caption={Main data type with extension constructor.}]
data Expr c
= Lit Int
| Add (Expr c) (Expr c)
Furthermore, we define a type class (\haskelllhstexinline{In}) that allows us to extract explicit dictionaries \haskelllhstexinline{Dict} from these records if the constraint can is present in the list.
Since the constraints become available as soon as the \haskelllhstexinline{Cons} constructor is matched, the implementation is a type-level list traversal.
-\begin{lstHaskellLhstex}[caption={Membership functions for constraints}]
+\begin{lstHaskellLhstex}[caption={Membership functions for constraints.}]
class c `In` cs where
project :: Record dt cs -> Dict (c dt)
instance {-# OVERLAPPING #-} c `In` (c ': cs) where
This type class creates a record structure cons by cons if and only if all type class constraints are available in the list of constraints.
It is not required to provide instances for this for specific records or type classes, the two instances describe all the required constraints.
-\begin{lstHaskellLhstex}[caption={Membership functions for constraints}]
+\begin{lstHaskellLhstex}[caption={Functions for creating the witness records.}]
class CreateRecord dt c where
createRecord :: Record dt c
instance CreateRecord d '[] where
EqLoop_g :: Expr_g d a -> Eq_g d a
\end{lstHaskellLhstex}
-\begin{lstHaskellLhstex}[caption={Smart constructions.}]
+\begin{lstHaskellLhstex}[caption={Smart constructors.}]
sub_g :: (Typeable d, GDict (d (Sub_g d)), Eq a, Num a) =>
Expr_g d a -> Expr_g d a -> Expr_g d a
sub_g e1 e2 = Ext_g gdict (Sub_g e1 e2)
getOpt_g (OptDict_g e) = e
\end{lstHaskellLhstex}
-\begin{lstHaskellLhstex}[caption={\texorpdfstring{\haskelllhstexinline{GDict}}{GDict} instances}]
+\begin{lstHaskellLhstex}[caption={\texorpdfstring{\haskelllhstexinline{GDict}}{GDict} instances.}]
instance Print_g v => GDict (PrintDict_g v) where
gdict = PrintDict_g print_g
instance Opt_g v => GDict (OptDict_g v) where
gdict = OptDict_g opt_g
\end{lstHaskellLhstex}
-\begin{lstHaskellLhstex}[caption={Evaluator instances}]
+\begin{lstHaskellLhstex}[caption={Evaluator instances.}]
instance HasEval_g d => Eval_g (Expr_g d) where
eval_g (Lit_g v) = v
eval_g (Add_g e1 e2) = eval_g e1 + eval_g e2
eval_g (NotLoop_g e) = eval_g e
\end{lstHaskellLhstex}
-\begin{lstHaskellLhstex}[caption={Printer instances}]
+\begin{lstHaskellLhstex}[caption={Printer instances.}]
instance HasPrint_g d => Print_g (Expr_g d) where
print_g (Lit_g v) = show v
print_g (Add_g e1 e2) = "(" ++ print_g e1 ++ "+" ++ print_g e2 ++ ")"
print_g (NotLoop_g e) = print_g e
\end{lstHaskellLhstex}
-\begin{lstHaskellLhstex}[caption={Optimisation instances}]
+\begin{lstHaskellLhstex}[caption={Optimisation instances.}]
instance HasOpt_g d => Opt_g (Expr_g d) where
opt_g (Lit_g v) = Lit_g v
opt_g (Add_g e1 e2) = case (opt_g e1, opt_g e2) of