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