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