Merge branch 'master' of git.martlubbers.net:msc-thesis1617
[msc-thesis1617.git] / edsl.shallow.tex
1 In a shallow \gls{EDSL} all language constructs are expressed as functions in
2 the host language. An evaluator view for the example language then can be
3 implemented as the code shown in Listing~\ref{lst:exshallow}. Note that much of
4 the internals of the language can be hidden using monads.
5
6 \begin{lstlisting}[language=Clean,label={lst:exshallow},%
7 caption={A minimal shallow \gls{EDSL}}]
8 :: Env = ... // Some environment
9 :: DSL a = DSL (Env -> a)
10
11 Lit :: a -> DSL a
12 Lit x = \e -> x
13
14 Var :: String -> DSL Int
15 Var i = \e -> retrEnv e i
16
17 Plus :: (DSL Int) (DSL Int) -> DSL Int
18 Plus x y = \e -> x e + y e
19
20 ...
21
22 Eq :: (DSL a) (DSL a) -> DSL Bool | == a
23 Eq x y = \e -> x e + y e
24 \end{lstlisting}
25
26 The advantage of shallowly embedding a language in a host language is its
27 extendability. It is very easy to add functionality and compile time checks
28 of the host language guarantee whether or not the functionality is available
29 when used. Moreover, the language is type safe as it is directly typed in the
30 host language.
31
32 The downside of this method is extending the language with views. It is nearly
33 impossible to add views to a shallowly embedded language. The only way of
34 achieving this is by decorating the datatype for the \gls{EDSL} with all the
35 information for all the views. This will mean that every component will have to
36 implement all views rendering it slow for multiple views and complex to
37 implement.