\section{mTask}
The \gls{mTask}-\gls{EDSL} is the basis on which the system is built. The
\gls{mTask}-\gls{EDSL} was created by Koopman et al.\ to support several views
such as an \gls{iTasks} simulation and a \gls{C}-code generator. The \gls{EDSL}
was designed to generate a ready to compile \gls{TOP}-like system for
microcontrollers such as the Arduino\cite{koopman_type-safe_nodate}%
\cite{plasmeijer_shallow_2016}.

The \gls{mTask}-\gls{EDSL} is a shallowly embedded class based \gls{EDSL} and
therefore it is very suitable to have a new backend that partly implements the
given classes. The following subsections show the details of the \gls{EDSL}
that are used in this extension. The parts of the \gls{EDSL} that are not used
will not be discussed and the details of those parts can be found in the cited
literature.

A view for the \gls{mTask}-\gls{EDSL} is a type of kind \CI{*->*->*} that
implements some of the classes given. The types do not have to be present as
fields in the higher kinded view and can, and will most often, solely be
phantom types. A view is of the form \CI{v t r}. The first variable will be the
type of the view, the second type variable will be the type of the
\gls{EDSL}-expression and the third type variable represents the role of the
expression. Currently the role of the expressions form a hierarchy. The three
roles and their hierarchy are shown in Listing~\ref{lst:exprhier}. This implies
that everything is a statement, only a \CI{Upd} and a \CI{Expr} are
expressions. The \CI{Upd} restriction describes updatable expressions such as
\gls{GPIO} pins and \gls{SDS}.

\begin{lstlisting}[%
	language=Clean,label={lst:exprhier},caption={Expression role hierarchy}]
:: Upd   = Upd
:: Expr  = Expr
:: Stmt  = Stmt

class isExpr a :: a -> Int
instance isExpr Upd
instance isExpr Expr
\end{lstlisting}

\subsection{Semantics}
\gls{mTask} do not behave like functions but more like
\gls{iTasks}-\glspl{Task}. When an \gls{mTask} is created it returns immediatly
and the \gls{Task} will be executed sometime in the future. \glspl{Task} can
run at an interval and they can start other tasks.
\todo{Meer uitwijden over de mTask semantiek}

\subsection{Expressions}
Expressions in the \gls{mTask}-\gls{EDSL} are divided into two types, namely
boolean expressions and arithmetic expressions. The class of arithmetic
language constructs also contains the function \CI{lit} that lifts a
host-language value in to the \gls{EDSL} domain. All standard arithmetic
functions are included but are omitted for brevity. Moreover the class
restrictions are only shown in the first functions and are later omitted. Both
the boolean expression and arithmetic expression classes are shown in
Listing~\ref{lst:arithbool}.

\begin{lstlisting}[language=Clean,label={lst:arithbool},
	caption={Basic classes for expressions}]
class arith v where
  lit           :: t -> v t Expr
  (+.) infixl 6 :: (v t p) (v t q) -> v t Expr          | +, zero t    & isExpr p & isExpr q
  (-.) infixl 6 :: (v t p) (v t q) -> v t Expr          | -, zero t    & ...
  ...
class boolExpr v where
  Not           :: (v Bool p) -> v Bool Expr            | ...
  (&.) infixr 3 :: (v Bool p) (v Bool q) -> v Bool Expr | ...
  ...
  (==.) infix 4 :: (v a p) (v a q) -> v Bool Expr       | ==, toCode a & ...
\end{lstlisting}

\subsection{Control flow}
Looping of \glspl{Task} happens because \glspl{Task} are launched at regular
intervals or relaunch themselves. Therefore there is no need for loop control
flow functionality such as \CI{While} or \CI{For} constructions. The main
control flow is the sequence operator and the \CI{If} statement. Both are shown
in Listing~\ref{lst:control}. The first class of \CI{If} statements describe
the regular if statement. The expressions given can have any role. The
functional dependency on \CI{s} determines the return type of the statement.
The sequence operator is very straightforward and just ties the two expressions
together in sequence.

\begin{lstlisting}[%
	language=Clean,label={lst:control},caption={Control flow operators}]
class If v q r ~s where
  If :: (v Bool p) (v t q) (v t r) -> v t s | ...

class seq v where
  (:.) infixr 0 :: (v t p) (v u q) -> v u Stmt | ...
\end{lstlisting}

\subsection{Input/Output and class extensions}
All expressions that have an \CI{Upd} role can be assigned to. Examples of such
expressions are \glspl{SDS} and \gls{GPIO}. Moreover, class extensions can be
created for specific peripherals such as user LEDs. The classes facilitating
this are shown in Listing~\ref{lst:sdsio}. In this way the assignment is the
same for every assignable entity.

\begin{lstlisting}[%
	language=Clean,label={lst:sdsio},caption={Input/Output classes}]
:: DigitalPin = D0 | D1 | D2 | D3 | D4 | D5 |D6 | D7 | D8 | D9 | D10 | D11 | D12 | D13
:: AnalogPin  = A0 | A1 | A2 | A3 | A4 | A5
:: UserLED = LED1 | LED2 | LED3

class dIO v where dIO :: DigitalPin -> v Bool Upd
class aIO v where aIO :: AnalogPin -> v Int Upd
class analogRead v where
  analogRead :: AnalogPin -> v Int Expr
  analogWrite :: AnalogPin (v Int p) -> v Int Expr
class digitalRead v where
  digitalRead :: DigitalPin -> v Bin Expr
  digitalWrite :: DigitalPin (v Bool p) -> v Int Expr

:: UserLED = LED1 | LED2 | LED3
class userLed v where
  ledOn  :: (v UserLED q) -> (v () Stmt)
  ledOff :: (v UserLED q) -> (v () Stmt)

class assign v where
  (=.) infixr 2 :: (v t Upd) (v t p) -> v t Expr | ...
\end{lstlisting}

A way of storing data in \glspl{mTask} is using \glspl{SDS}. \glspl{SDS} serve
as variables in the \gls{mTask} and will keep their value across executions.
The classes associated with \glspl{SDS} are listed in
Listing~\ref{lst:sdsclass}. The \CI{Main} class is introduced to box an
\gls{mTask} and make it recognizable by the type system.

\begin{lstlisting}[%
	language=Clean,label={lst:sdsclass},caption={\glspl{SDS} in \gls{mTask}}]
:: In a b = In infix 0 a b
:: Main a = {main :: a}

class sds v where
  sds :: ((v t Upd)->In t (Main (v c s))) -> (Main (v c s)) | ...
\end{lstlisting}

\subsection{Example \gls{mTask}}
\todo{Also explain semantics about running tasks}
Some example \glspl{mTask} using almost all of the functionality are show in
Listing~\ref{lst:exmtask}. The \glspl{mTask} shown in the example do not belong
to a particular view and therefore are of the type \CI{View t r}. The
\CI{blink} \gls{mTask} show the classic \emph{Arduino} \emph{Hello World!}
application that blinks a certain LED every interval. The \CI{thermostat}
\gls{mTask} will enable a digital pin powering a cooling fan when the analog
pin representing a temperature sensor is too high. \CI{thermostat`} shows the
same program but now using the assignment style \gls{GPIO}.

\begin{lstlisting}[%
	language=Clean,label={lst:exmtask},caption={Some example \glspl{mTask}}]
blink :: Main (View Int Stmt)
blink = sds \x=1 In sds \led=LED1 In {main =
  IF (x ==. lit 1) (ledOn led) (ledOff led) :.
  x =. lit 1 -. x
  }

thermostat :: Main (View () Stmt)
thermostat = {main =
  IF (analogRead A0 >. 50)
	( digitalWrite D0 (lit True)  )
    ( digitalWrite D0 (lit False) )
  }

thermostat` :: Main (View () Stmt)
thermostat` = let 
  a0 = aIO A0
  d0 = dIO D0 in {main = IF (a0 >. 50) (d0 =. lit True) (d0 =. lit False) }
\end{lstlisting}
