add class based dsl part
authorMart Lubbers <mart@martlubbers.net>
Thu, 4 May 2017 13:50:58 +0000 (15:50 +0200)
committerMart Lubbers <mart@martlubbers.net>
Thu, 4 May 2017 13:50:58 +0000 (15:50 +0200)
methods.dsl.tex

index 262f228..bd41dbb 100644 (file)
@@ -88,10 +88,45 @@ implement all views. This makes it slow for multiple views and complex to
 implement.
 
 \subsection{Class based shallow embedding}
-
-\todo{while iTasks is also a DSL\ldots}
-\glspl{mTask} are expressed in a class based shallowly embedded \gls{EDSL}.
-There are two main types of \glspl{EDSL}.
-\todo{Small shallow embedded dsl intro}
-\todo{Small deep embedded dsl}
-\todo{Show that class based has the best of both worlds}
+The third type of embedding is called class based shallow embedding and is
+supposed to have the best of both shallow and deep embedding. In class based
+shallow embedding the language constructs are defined as type classes. The same
+language is shown with the new method in Listing~\ref{lst:exclassshallow}.
+
+This type of embedding inherits the easyness of adding views from shallow
+embedding. A view is just a different type implementing one or more type
+classes as shown in the aforementioned Listing where an evaluator and a pretty
+printer are implemented.
+
+Just as with \glspl{GADT} in deep embedding type safety is guaranteed. Type
+constraints are enforced through phantom types. One can add as many phantom
+type as is necessary. Lastly, extensions can be added easily, just as in
+shallow embedding. When an extension is made in an existing class, all views
+must be updated accordingly to prevent possible runtime errors. When an
+extension is added in a new class this problem does not arise and views can
+choose to implement only parts of the collection of classes.
+
+\begin{lstlisting}[language=Clean,label={lst:exclassshallow},%
+       caption={A minimal class based shallow \gls{EDSL}}]
+:: Env   = ...            // Some environment
+:: Evaluator a = Evaluator (Env -> a)
+:: PrettyPrinter a = PP String
+
+class intArith where
+       lit :: t -> v t | toString t
+       add :: (v t) (v t) -> (v t) | + t
+       minus :: (v t) (v t) -> (v t) | - t
+
+class boolArith where
+       and :: (v Bool) (v Bool) -> (v Bool)
+       eq :: (v t) (v t) -> (v Bool) | == t
+
+instance intArith Evaluator where
+       lit x = \e->x
+       add x y = ...
+
+instance intArith PrettyPrinter where
+       lit x = toString x
+       add x y = x +++ "+" +++ y
+       ...
+\end{lstlisting}