add relevant research
[msc-thesis1617.git] / methods.dsl.tex
1 There are several techniques available for creating \glspl{EDSL}. Each of
2 them have their own advantages and disadvantages such as extendability,
3 typedness and view support. In the following subsections each of the main
4 techniques are briefly explained.
5
6 \section{Deep embedding}
7 A deep \gls{EDSL} means that the language is represented as an \gls{ADT}. Views
8 are functions that transform something to the datatype or the other way around.
9 As an example we have the simple arithmetic \gls{EDSL} shown in
10 Listing~\ref{lst:exdeep}.
11
12 \begin{lstlisting}[language=Clean,label={lst:exdeep},%
13 caption={A minimal deep \gls{EDSL}}]
14 :: DSL
15 = LitI Int
16 | LitB Bool
17 | Var String
18 | Plus DSL DSL
19 | Minus DSL DSL
20 | And DSL DSL
21 | Eq DSL
22 \end{lstlisting}
23 Deep embedding has the advantage that it is very simple to build and the views
24 are easy to make and add. However, there are also a few downsides. The
25 expressions created with this language are not type-safe. In the given language
26 it is possible an expression such as \CI{Plus (LitI 4) (LitB True)} which to
27 add a boolean to an integer. Evermore so, extending the \gls{ADT} is easy and
28 convenient but extending the views accordingly is tedious and has to be done
29 individually for all views.
30
31 The first downside of the type of \gls{EDSL} can be overcome by using
32 \glspl{GADT}\cite{cheney_first-class_2003}. Listing~\ref{lst:exdeepgadt} shows
33 the same language, but type-safe with a \gls{GADT}. \glspl{GADT} are not
34 supported in the current version of \gls{Clean} and therefore the syntax is
35 artificial. However, it has been shown that \glspl{GADT} can be simulated using
36 bimaps or projection pairs\cite{cheney_lightweight_2002}. Unfortunately the
37 lack of extendability stays a problem. If a language construct is added no
38 compile time guarantee is given that all views support it.
39
40 \begin{lstlisting}[language=Clean,label={lst:exdeepgadt},%
41 caption={A minimal deep \gls{EDSL} using \glspl{GADT}}]
42 :: DSL a
43 = LitI Int -> DSL Int
44 | LitB Bool -> DSL Bool
45 | E.e: Var String -> DSL e
46 | Plus (DSL Int) (DSL Int) -> DSL Int
47 | Minus (DSL Int) (DSL Int) -> DSL Int
48 | And (DSL Bool) (DSL Bool) -> DSL Bool
49 | E.e: Eq (DSL e) (DSL e) -> DSL Bool & == e
50 \end{lstlisting}
51
52 \section{Shallow embedding}
53 In a shallowly \gls{EDSL} all language constructs are expressed as functions in
54 the host language. An evaluator view for our example language then looks
55 something like the code shown in Listing~\ref{lst:exshallow}. Note that much of
56 the internals of the language can be hidden using monads.
57
58 \begin{lstlisting}[language=Clean,label={lst:exshallow},%
59 caption={A minimal shallow \gls{EDSL}}]
60 :: Env = ... // Some environment
61 :: DSL a = DSL (Env -> a)
62
63 Lit :: a -> DSL a
64 Lit x = \e->x
65
66 Var :: String -> DSL Int
67 Var i = \e->retrEnv e i
68
69 Plus :: (DSL Int) (DSL Int) -> DSL Int
70 Plus x y = \e->x e + y e
71
72 ...
73
74 Eq :: (DSL a) (DSL a) -> DSL Bool | == a
75 Eq x y = \e->x e + y e
76 \end{lstlisting}
77
78 The advantage of shallowly embedding a language in a host language is the
79 extendability. It is very easy to add functionality and compile time checks
80 guarantee that the functionality is available. Moreover, the language is type
81 safe as it is directly typed in the host language.
82
83 The downside of this method is extending the language with views. It is near
84 impossible to add views to a shallowly embedded language. The only way of
85 achieving this is by decorating the datatype for the \gls{EDSL} with all the
86 information for all the views. This will mean that every component will have to
87 implement all views. This makes it slow for multiple views and complex to
88 implement.
89
90 \section{Class based shallow embedding}
91 The third type of embedding is called class based shallow embedding and has the
92 best of both shallow and deep embedding. In class based shallow embedding the
93 language constructs are defined as type classes. The same language is shown
94 with the new method in Listing~\ref{lst:exclassshallow}.
95
96 This type of embedding inherits the easiness of adding views from shallow
97 embedding. A view is just a different data type implementing one or more of the
98 type classes as shown in the aforementioned Listing where an evaluator and a
99 pretty printer are implemented.
100
101 Just as with \glspl{GADT} in deep embedding type safety is guaranteed. Type
102 constraints are enforced through phantom types. One can add as many phantom
103 type as is necessary. Lastly, extensions can be added easily, just as in
104 shallow embedding. When an extension is made in an existing class, all views
105 must be updated accordingly to prevent possible runtime errors. When an
106 extension is added in a new class this problem does not arise and views can
107 choose to implement only parts of the collection of classes.
108
109 \begin{lstlisting}[language=Clean,label={lst:exclassshallow},%
110 caption={A minimal class based shallow \gls{EDSL}}]
111 :: Env = ... // Some environment
112 :: Evaluator a = Evaluator (Env -> a)
113 :: PrettyPrinter a = PP String
114
115 class intArith where
116 lit :: t -> v t | toString t
117 add :: (v t) (v t) -> (v t) | + t
118 minus :: (v t) (v t) -> (v t) | - t
119
120 class boolArith where
121 and :: (v Bool) (v Bool) -> (v Bool)
122 eq :: (v t) (v t) -> (v Bool) | == t
123
124 instance intArith Evaluator where
125 lit x = \e->x
126 add x y = ...
127
128 instance intArith PrettyPrinter where
129 lit x = toString x
130 add x y = x +++ "+" +++ y
131 ...
132 \end{lstlisting}