add sections about the original mTask
[msc-thesis1617.git] / methods.mtask.tex
1 \section{mTask}
2 The \gls{mTask}-\gls{EDSL} is the basis on which the system is built. The
3 \gls{mTask} was created by Koopman et al.\ to support several views such as an
4 \gls{iTasks} simulation and a \gls{C}-code generator. The \gls{EDSL} was
5 designed to generate a ready to compile \gls{TOP}-like system for
6 microcontrollers such as the Arduino\cite{koopman_type-safe_nodate}%
7 \cite{plasmeijer_shallow_2016}.
8
9 The \gls{mTask}-\gls{EDSL} is a shallowly embedded class based \gls{EDSL} and
10 therefore it is very suitable to have a new backend that partly implements the
11 given classes. The following subsections show the details of the \gls{EDSL}
12 that are used in the extension. The parts of the \gls{EDSL} that are not used
13 will not be discussed and the details of those parts can be found in the cited
14 literature.
15
16 A view for the \gls{mTask}-\gls{EDSL} is a type of kind \CI{*->*->*} that
17 implements some of the classes given. The types do not have to be present as
18 fields in the higher kinded view and can, and will most often, solely be
19 phantom types. A view is of the form \CI{v t r}. The first variable will be the
20 type of the view, the second type variable will be the type of the
21 \gls{EDSL}-expression and the third type variable represents the role of the
22 expression. Currently the role of the expressions form a hierarchy. The three
23 roles and their hierarchy are shown in Listing~\ref{lst:exprhier}. This implies
24 that everything is a statement, only a \CI{Upd} and a \CI{Expr} are
25 expressions. The \CI{Upd} restriction describes updatable expressions such as
26 \gls{GPIO} pins and \gls{SDS}.
27
28 \begin{lstlisting}[%
29 language=Clean,label={lst:exprhier},caption={Expression role hierarchy}]
30 :: Upd = Upd
31 :: Expr = Expr
32 :: Stmt = Stmt
33
34 class isExpr a :: a -> Int
35 instance isExpr Upd
36 instance isExpr Expr
37 \end{lstlisting}
38
39 \subsection{Expressions}
40 Expressions in the \gls{mTask}-\gls{EDSL} are divided into two types, namely
41 boolean expressions and arithmetic expressions. The class of arithmetic
42 language constructs also contains the function \CI{lit} that lifts a
43 host-language value in to the \gls{EDSL} domain. All standard arithmetic
44 functions are included but are omitted for brevity. Moreover the class
45 restrictions are only shown in the first functions and are later omitted. Both
46 the boolean expression and arithmetic expression classes are shown in
47 Listing~\ref{lst:arithbool}.
48
49 \begin{lstlisting}[language=Clean,label={lst:arithbool},
50 caption={Basic classes for expressions}]
51 class arith v where
52 lit :: t -> v t Expr
53 (+.) infixl 6 :: (v t p) (v t q) -> v t Expr | +, zero t & isExpr p & isExpr q
54 (-.) infixl 6 :: (v t p) (v t q) -> v t Expr | -, zero t & ...
55 ...
56 class boolExpr v where
57 Not :: (v Bool p) -> v Bool Expr | ...
58 (&.) infixr 3 :: (v Bool p) (v Bool q) -> v Bool Expr | ...
59 ...
60 (==.) infix 4 :: (v a p) (v a q) -> v Bool Expr | ==, toCode a & ...
61 \end{lstlisting}
62
63 \subsection{Control flow}
64 \todo{Write this}
65
66 \subsection{Input/Output and class extensions}
67 All expressions that have an \CI{Upd} role can be assigned to. Examples of such
68 expressions are \glspl{SDS} and \gls{GPIO}. Moreover, class extensions can be
69 created for specific peripherals such as user LEDs. The classes facilitating
70 this are shown in Listing~\ref{lst:sdsio}. In this way the assignment is the
71 same for every assignable entity.
72
73 \begin{lstlisting}[%
74 language=Clean,label={lst:sdsio},caption={Input/Output classes}]
75 :: DigitalPin = D0 | D1 | D2 | D3 | D4 | D5 |D6 | D7 | D8 | D9 | D10 | D11 | D12 | D13
76 :: AnalogPin = A0 | A1 | A2 | A3 | A4 | A5
77 :: UserLED = LED1 | LED2 | LED3
78
79 class dIO v where dIO :: DigitalPin -> v Bool Upd
80 class aIO v where aIO :: AnalogPin -> v Int Upd
81 class analogRead v where
82 analogRead :: AnalogPin -> v Int Expr
83 analogWrite :: AnalogPin (v Int p) -> v Int Expr
84 class digitalRead v where
85 digitalRead :: DigitalPin -> v Bin Expr
86 digitalWrite :: DigitalPin (v Bool p) -> v Int Expr
87
88 :: UserLED = LED1 | LED2 | LED3
89 class userLed v where
90 ledOn :: (v UserLED q) -> (v () Stmt)
91 ledOff :: (v UserLED q) -> (v () Stmt)
92
93 class assign v where
94 (=.) infixr 2 :: (v t Upd) (v t p) -> v t Expr | ...
95 \end{lstlisting}
96
97 A way of storing data in \glspl{mTask} is using \glspl{SDS}. \glspl{SDS} serve
98 as variables in the \gls{mTask} and will keep their value across executions.
99 The classes associated with \glspl{SDS} are listed in
100 Listing~\ref{lst:sdsclass}. The \CI{Main} class is introduced to box an
101 \gls{mTask} and make it recognizable by the type system.
102
103 \begin{lstlisting}[%
104 language=Clean,label={lst:sdsclass},caption={\glspl{SDS} in \gls{mTask}}]
105 :: In a b = In infix 0 a b
106 :: Main a = {main :: a}
107
108 class sds v where
109 sds :: ((v t Upd)->In t (Main (v c s))) -> (Main (v c s)) | ...
110 \end{lstlisting}
111
112 \subsection{Example \gls{mTask}}
113 \todo{Also explain semantics about running tasks}
114 Some example \glspl{mTask} using almost all of the functionality are show in
115 Listing~\ref{lst:exmtask}. The \glspl{mTask} shown in the example do not belong
116 to a particular view and therefore are of the type \CI{View t r}. The
117 \CI{blink} \gls{mTask} show the classic \emph{Arduino} \emph{Hello World!}
118 application that blinks a certain LED every interval. The \CI{thermostat}
119 \gls{mTask} will enable a digital pin powering a cooling fan when the analog
120 pin representing a temperature sensor is too high. \CI{thermostat`} shows the
121 same program but now using the assignment style \gls{GPIO}.
122
123 \begin{lstlisting}[%
124 language=Clean,label={lst:exmtask},caption={Some example \glspl{mTask}}]
125 blink :: Main (View Int Stmt)
126 blink = sds \x=1 In sds \led=LED1 In {main =
127 IF (x ==. lit 1) (ledOn led) (ledOff led) :.
128 x =. lit 1 -. x
129 }
130
131 thermostat :: Main (View () Stmt)
132 thermostat = {main =
133 IF (analogRead A0 >. 50)
134 ( digitalWrite D0 (lit True) )
135 ( digitalWrite D0 (lit False) )
136 }
137
138 thermostat` :: Main (View () Stmt)
139 thermostat` = let
140 a0 = aIO A0
141 d0 = dIO D0 in {main = IF (a0 >. 50) (d0 =. lit True) (d0 =. lit False) }
142 \end{lstlisting}