update presentation more
[msc-thesis1617.git] / pres.mtask.tex
1 \subsection{EDSLs}
2 \begin{frame}
3 \frametitle{Embedded Domain Specific Language}
4 \framesubtitle{What is are EDSL}
5 \begin{itemize}
6 \item DSL:\ language for a specific domain
7 \item E.g., PostScript, VimScript, HTML, \ldots
8 \pause{}
9 \item EDSL:\ Embedded DSL
10 \item Language in a language
11 \item Use the properties of the host
12 \item E.g., Ivory, \ldots\pause{} and iTasks
13 \pause{}
14 \item Different embedding techniques
15 \end{itemize}
16 \end{frame}
17
18 \begin{frame}[fragile]
19 \frametitle{Deep embedding}
20 \begin{columns}[t]
21 \column{.49\textwidth}
22 \begin{block}{What is deep embedding}
23 \begin{itemize}
24 \item The EDSL as an ADT
25 \item A view is a function transforming the ADT
26 \end{itemize}
27 \end{block}
28 \column{.49\textwidth}
29 \pause{}
30 \begin{lstlisting}
31 :: DSL = LitI Int | LitB Bool
32 | Var String | Plus DSL DSL
33 | Minus DSL DSL | And DSL DSL
34 | Eq DSL
35
36 eval :: DSL Env -> Env
37 pprint :: DSL -> String
38 \end{lstlisting}
39 \end{columns}
40 \pause{}
41 \begin{block}{Properties}
42 \begin{itemize}
43 \item Easy to add views
44 \item Hard to extend
45 \item Not type safe
46 \pause\item GADT
47 \end{itemize}
48 \end{block}
49 \end{frame}
50
51 \begin{frame}[fragile]
52 \frametitle{Shallow embedding}
53 \begin{columns}[t]
54 \column{.49\textwidth}
55 \begin{block}{What is shallow embedding}
56 \begin{itemize}
57 \item The EDSL as a function
58 \item The view is embedded in the function
59 \end{itemize}
60 \end{block}
61 \pause{}
62 \column{.49\textwidth}
63 \begin{lstlisting}
64 :: Env = ...
65 :: DSL a = DSL (Env -> a)
66
67 Lit :: a -> DSL a
68 Lit x = \e -> x
69
70 Var :: String -> DSL Int
71 Var i = \e -> retrEnv e i
72
73 Plus :: (DSL Int) (DSL Int) -> DSL Int
74 Plus x y = \e -> x e + y e
75 \end{lstlisting}
76 \end{columns}
77 \pause{}
78 \begin{block}{Properties}
79 \begin{itemize}
80 \item Difficult to add views
81 \item Easy to extend
82 \item Type safe
83 \end{itemize}
84 \end{block}
85 \end{frame}
86
87 \begin{frame}[fragile]
88 \frametitle{Class based shallow embedding}
89 \begin{columns}[t]
90 \column{.49\textwidth}
91 \onslide<1->{
92 \begin{block}{The best of both worlds}
93 \begin{itemize}
94 \item The EDSL is a collection of classes
95 \item Views are types implementing a subset
96 \end{itemize}
97 \end{block}
98 }
99 \onslide<3->{
100 \begin{block}{Properties}
101 \begin{itemize}
102 \item Extendable
103 \item Type safe
104 \item Easy to add views
105 \end{itemize}
106 \end{block}
107 }
108 \column{.49\textwidth}
109 \begin{onlyenv}<2->
110 \begin{lstlisting}
111 :: Env = ...
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
122 instance intArith Evaluator where
123 lit x = Evaluator \e->x
124 add x y = Evaluator ...
125 ...
126
127 instance intArith PrettyPrinter where
128 lit x = PP $ toString x
129 add x y = PP $ x +++ "+" +++ y
130 ...
131 \end{lstlisting}
132 \end{onlyenv}
133 \end{columns}
134 \end{frame}
135
136 \subsection{mTask}
137 \begin{frame}
138 \frametitle{mTask}
139 \pause{}
140 \begin{block}{What is mTask}
141 \begin{itemize}
142 \item Created by Pieter Koopman and Rinus Plasmeijer
143 \item EDSL
144 \item Arduino C++ generation, iTasks simulation
145 \end{itemize}
146 \end{block}
147
148 \begin{block}{Considerations}
149 \begin{itemize}
150 \item Type safe
151 \item Embedded in Clean
152 \item Extendable
153 \end{itemize}
154 \end{block}
155 \end{frame}
156
157 \begin{frame}[fragile]
158 \frametitle{Expressions}
159 \begin{block}{mTask}
160 \begin{itemize}[<+->]
161 \item Of the form \CI{v t p}
162 \item \CI{p} is hierarchical
163 \end{itemize}
164 \end{block}
165 \pause{}
166 \begin{lstlisting}
167 class arith v where
168 lit :: t -> v t Expr
169 (+.) infixl 6 :: (v t p) (v t q) -> v t Expr | +, zero t & isExpr p & isExpr q
170 (-.) infixl 6 :: (v t p) (v t q) -> v t Expr | -, zero t & ...
171 ...
172 class boolExpr v where
173 Not :: (v Bool p) -> v Bool Expr | ...
174 (&.) infixr 3 :: (v Bool p) (v Bool q) -> v Bool Expr | ...
175 ...
176 (==.) infix 4 :: (v a p) (v a q) -> v Bool Expr | ...
177 \end{lstlisting}
178 \end{frame}
179
180 \begin{frame}[fragile]
181 \frametitle{Control flow}
182 \begin{lstlisting}
183 class IF v where
184 IF :: (v Bool p) (v t q) (v s r) -> v () Stmt | ...
185 (?) infix 1 :: (v Bool p) (v t q) -> v () Stmt | ...
186
187 class seq v where
188 (:.) infixr 0 :: (v t p) (v u q) -> v u Stmt | ...
189 \end{lstlisting}
190 \end{frame}
191
192 \begin{frame}[fragile]
193 \frametitle{Assignment and Input/Output}
194 \begin{lstlisting}
195 :: DigitalPin = D0 | D1 | D2 ...
196 :: AnalogPin = A0 | A1 | A2 ...
197
198 class dIO v where dIO :: DigitalPin -> v Bool Upd
199 class aIO v where aIO :: AnalogPin -> v Int Upd
200
201 class analogRead v where
202 analogRead :: AnalogPin -> v Int Expr
203 analogWrite :: AnalogPin (v Int p) -> v Int Expr
204
205 class digitalRead v where
206 digitalRead :: DigitalPin -> v Bin Expr
207 digitalWrite :: DigitalPin (v Bool p) -> v Int Expr
208 \end{lstlisting}
209 \end{frame}
210
211 \begin{frame}[fragile]
212 \frametitle{Shared Data Sources and Assignment}
213 \begin{lstlisting}
214 :: In a b = In infix 0 a b
215 :: Main a = {main :: a}
216
217 class sds v where
218 sds :: ((v t Upd)->In t (Main (v c s))) -> (Main (v c s)) | ...
219
220 class assign v where
221 (=.) infixr 2 :: (v t Upd) (v t p) -> v t Expr | ...
222 \end{lstlisting}
223 \end{frame}
224
225 \begin{frame}[fragile]
226 \frametitle{Examples}
227 \begin{lstlisting}
228 blink = task \blink=(\x.
229 IF (x ==. lit True)
230 (ledOn LED1)
231 (ledOff LED2) :.
232 blink (lit 1000) (Not x))
233 In {main=blink (lit 1000) True}
234
235 thermostat :: Main (View () Stmt)
236 thermostat = {main = IF (analogRead A0 >. lit 50)
237 ( digitalWrite D0 (lit True) )
238 ( digitalWrite D0 (lit False) )
239 }
240
241 thermostat2 :: Main (View () Stmt)
242 thermostat2 = let a0 = aIO A0
243 d0 = dIO D0
244 in {main = IF (a0 >. lit 50)
245 (d0 =. lit True)
246 (d0 =. lit False)
247 }
248 \end{lstlisting}
249 \end{frame}