3 \frametitle{Embedded Domain Specific Language
}
4 \framesubtitle{What is are EDSL
}
6 \item DSL:\ language for a specific domain
7 \item E.g., PostScript, VimScript, HTML,
\ldots
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
16 \begin{frame
}[fragile
]
17 \frametitle{Deep embedding
}
19 \column{.49\textwidth}
20 \begin{block
}{What is deep embedding
}
22 \item The EDSL as an ADT
23 \item A view is a function transforming the ADT
26 \column{.49\textwidth}
29 :: DSL = LitI Int | LitB Bool
30 | Var String | Plus DSL DSL
31 | Minus DSL DSL | And DSL DSL
34 eval :: DSL Env -> Env
35 pprint :: DSL -> String
39 \begin{block
}{Properties
}
41 \item Easy to add views
49 \begin{frame
}[fragile
]
50 \frametitle{Shallow embedding
}
52 \column{.49\textwidth}
53 \begin{block
}{What is shallow embedding
}
55 \item The EDSL as a function
56 \item The view is embedded in the function
60 \column{.49\textwidth}
63 :: DSL a = DSL (Env -> a)
68 Var :: String -> DSL Int
69 Var i =
\e -> retrEnv e i
71 Plus :: (DSL Int) (DSL Int) -> DSL Int
72 Plus x y =
\e -> x e + y e
76 \begin{block
}{Properties
}
78 \item Difficult to add views
85 \begin{frame
}[fragile
]
86 \frametitle{Class based shallow embedding
}
88 \column{.49\textwidth}
90 \begin{block
}{The best of both worlds
}
92 \item The EDSL is a collection of classes
93 \item Views are types implementing a subset
98 \begin{block
}{Properties
}
102 \item Easy to add views
106 \column{.49\textwidth}
110 :: Evaluator a = Evaluator (Env -> a)
111 :: PrettyPrinter a = PP String
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
118 class boolArith where ...
120 instance intArith Evaluator where
121 lit x = Evaluator
\e->x
122 add x y = Evaluator ...
125 instance intArith PrettyPrinter where
126 lit x = PP $ toString x
127 add x y = PP $ x +++ "+" +++ y
138 \begin{block
}{What is mTask
}
141 \item Arduino C++ generation, iTasks simulation
145 \begin{block
}{Considerations
}
148 \item Embedded in Clean
154 \begin{frame
}[fragile
]
155 \frametitle{Expressions
}
158 \item Two phantom types
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 & ...
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 | ...
173 (==.) infix
4 :: (v a p) (v a q) -> v Bool Expr | ...
177 \begin{frame
}[fragile
]
178 \frametitle{Control flow
}
180 class If v q r ~s where
181 If :: (v Bool p) (v t q) (v t r) -> v t s | ...
184 IF :: (v Bool p) (v t q) (v s r) -> v () Stmt | ...
185 (?) infix
1 :: (v Bool p) (v t q) -> v () Stmt | ...
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
193 (:.) infixr
0 :: (v t p) (v u q) -> v u Stmt | ...
197 \begin{frame
}[fragile
]
198 \frametitle{Assignment and Input/Output
}
200 :: DigitalPin = D0 | D1 | D2 ...
201 :: AnalogPin = A0 | A1 | A2 ...
203 class dIO v where dIO :: DigitalPin -> v Bool Upd
204 class aIO v where aIO :: AnalogPin -> v Int Upd
206 class analogRead v where
207 analogRead :: AnalogPin -> v Int Expr
208 analogWrite :: AnalogPin (v Int p) -> v Int Expr
210 class digitalRead v where
211 digitalRead :: DigitalPin -> v Bin Expr
212 digitalWrite :: DigitalPin (v Bool p) -> v Int Expr
216 \begin{frame
}[fragile
]
217 \frametitle{Shared Data Sources and Assignment
}
219 :: In a b = In infix
0 a b
220 :: Main a =
{main :: a
}
223 sds :: ((v t Upd)->In t (Main (v c s))) -> (Main (v c s)) | ...
226 (=.) infixr
2 :: (v t Upd) (v t p) -> v t Expr | ...
230 \begin{frame
}[fragile
]
231 \frametitle{Examples
}
233 blink = task
\blink=(
\x.
237 blink (lit
1000) (Not x))
238 In
{main=blink (lit
1000) True
}
240 thermostat :: Main (View () Stmt)
241 thermostat =
{main = IF (analogRead A0 >. lit
50)
242 ( digitalWrite D0 (lit True) )
243 ( digitalWrite D0 (lit False) )
246 thermostat2 :: Main (View () Stmt)
247 thermostat2 = let a0 = aIO A0
249 in
{main = IF (a0 >. lit
50)