many updates
[phd-thesis.git] / mtask / mtask.tex
1 \documentclass[../thesis.tex]{subfiles}
2
3 \begin{document}
4 \ifSubfilesClassLoaded{
5 \pagenumbering{arabic}
6 }{}
7
8 \chapter{\Gls{TOP} for the \gls{IOT}}
9 \begin{chapterabstract}
10 This chapter introduces \gls{MTASK} and puts it into perspective compared to traditional microprocessor programming.
11 \end{chapterabstract}
12 Traditionally, the first program that one writes when trying a new language is the so called \emph{Hello World!} program.
13 This program has the single task of printing the text \emph{Hello World!} to the screen and exiting again, useful to become familiarised with the syntax and verify that the toolchain and runtime environment is working.
14 On microprocessors, there often is no screen for displaying text.
15 Nevertheless, almost always there is a monochrome $1\times1$ pixel screen, namely an---often builtin---\gls{LED}.
16 The \emph{Hello World!} equivalent on microprocessors blinks this \gls{LED}.
17
18 \Cref{lst:arduinoBlink} shows how the logic of a blink program might look when using \gls{ARDUINO}'s \gls{CPP} dialect.
19 Every \gls{ARDUINO} program contains a \arduinoinline{setup} and a \arduinoinline{loop} function.
20 The \arduinoinline{setup} function is executed only once on boot, the \arduinoinline{loop} function is continuously called afterwards and contains the event loop.
21 After setting the \gls{GPIO} pin to the correct mode, blink's \arduinoinline{loop} function alternates the state of the pin representing the \gls{LED} between \arduinoinline{HIGH} and \arduinoinline{LOW}, turning the \gls{LED} off and on respectively.
22 In between it waits for 500 milliseconds so that the blinking is actually visible for the human eye.
23 Compiling this results in a binary firmware that needs to be flashed onto the program memory.
24
25 Translating the traditional blink program to \gls{MTASK} can almost be done by simply substituting some syntax as seen in \cref{lst:blinkImp}.
26 E.g.\ \cleaninline{digitalWrite} becomes \cleaninline{writeD}, literals are prefixed with \cleaninline{lit} and the pin to blink is changed to represent the actual pin for the builtin \gls{LED} of the device used in the exercises.
27 In contrast to the imperative \gls{CPP} dialect, \gls{MTASK} is a \gls{TOP} language and therefore there is no such thing as a loop, only task combinators to combine tasks.
28 To simulate a loop, the \cleaninline{rpeat} task can be used, this task executes the argument task and, when stable, reinstates it.
29 The body of the \cleaninline{rpeat} contains similarly named tasks to write to the pins and to wait in between.
30 The tasks are connected using the sequential \cleaninline{>>|.} combinator that for all current intents and purposes executes the tasks after each other.
31
32 \begin{figure}[!ht]
33 \begin{subfigure}[b]{.5\linewidth}
34 \begin{lstArduino}[caption={Blink program.},label={lst:arduinoBlink}]
35 void setup() {
36 pinMode(D2, OUTPUT);
37 }
38
39 void loop() {
40 digitalWrite(D2, HIGH);
41 delay(500);
42 digitalWrite(D2, LOW);
43 delay(500);
44 }\end{lstArduino}
45 \end{subfigure}%
46 \begin{subfigure}[b]{.5\linewidth}
47 \begin{lstClean}[caption={Blink program.},label={lst:blinkImp}]
48
49 blink :: Main (MTask v ()) | mtask v
50 blink =
51 declarePin D4 PMOutput \d4->
52 {main = rpeat (
53 writeD d4 true
54 >>|. delay (lit 500)
55 >>|. writeD d4 false
56 >>|. delay (lit 500)
57 )}\end{lstClean}
58 \end{subfigure}
59 \end{figure}
60
61 \chapter{The \gls{MTASK} \gls{DSL}}
62 \begin{chapterabstract}
63 This chapter serves as a complete guide to the \gls{MTASK} language, from an \gls{MTASK} programmer's perspective.
64 \end{chapterabstract}
65
66 The \gls{MTASK} system is a \gls{TOP} programming environment for programming microprocessors.
67 It is implemented as an \gls{EDSL} in \gls{CLEAN} using class-based---or tagless-final---embedding (See~\cref{ssec:tagless}).
68 Due to the nature of the embedding technique, it is possible to have multiple interpretations of---or views on---programs written in the \gls{MTASK} language.
69
70 \begin{itemize}
71 \item Pretty printer
72
73 This interpretation converts the expression to a string representation.
74 \item Simulator
75
76 The simulator converts the expression to a ready-for-work \gls{ITASK} simulation in which the user can inspect and control the simulated peripherals and see the internal state of the tasks.
77 \item Compiler
78
79 The compiler compiles the \gls{MTASK} program at runtime to a specialised bytecode.
80 Using a handful of integration functions and tasks, \gls{MTASK} tasks can be executed on microprocessors and integrated in \gls{ITASK} as if they were regular \gls{ITASK} tasks.
81 Furthermore, with special language constructs, \glspl{SDS} can be shared between \gls{MTASK} and \gls{ITASK} programs.
82 \end{itemize}
83
84 \section{Types}
85 To leverage the type checker of the host language, types in the \gls{MTASK} language are expressed as types in the host language, to make the language type safe.
86 However, not all types in the host language are suitable for microprocessors that may only have 2KiB of \gls{RAM} so class constraints are therefore added to the \gls{DSL} functions.
87 The most used class constraint is the \cleaninline{type} class collection containing functions for serialization, printing, \gls{ITASK} constraints etc.
88 Many of these functions can be derived using generic programming.
89 An even stronger restriction on types is defined for types that have a stack representation.
90 This \cleaninline{basicType} class has instances for many \gls{CLEAN} basic types such as \cleaninline{Int}, \cleaninline{Real} and \cleaninline{Bool}.
91 The class constraints for values in \gls{MTASK} are omnipresent in all functions and therefore often omitted throughout throughout the chapters for brevity and clarity.
92
93 \begin{table}[ht]
94 \centering
95 \begin{tabular}{lll}
96 \toprule
97 \gls{CLEAN}/\gls{MTASK} & \gls{CPP} type & \textnumero{}bits\\
98 \midrule
99 \cleaninline{Bool} & \cinline{bool} & 16\\
100 \cleaninline{Char} & \cinline{char} & 16\\
101 \cleaninline{Int} & \cinline{int16_t} & 16\\
102 \cleaninline{:: Long} & \cinline{int32_t} & 32\\
103 \cleaninline{Real} & \cinline{float} & 32\\
104 \cleaninline{:: T = A | B | C} & \cinline{enum} & 16\\
105 \bottomrule
106 \end{tabular}
107 \caption{Mapping from \gls{CLEAN}/\gls{MTASK} data types to \gls{CPP} datatypes.}%
108 \label{tbl:mtask-c-datatypes}
109 \end{table}
110
111 The \gls{MTASK} language consists of a core collection of type classes bundled in the type class \cleaninline{class mtask}.
112 Every interpretation implements the type classes in the \cleaninline{mtask} class
113 There are also \gls{MTASK} extensions that not every interpretation implements such as peripherals and integration with \gls{ITASK}.
114
115 \Cref{lst:constraints} contains the definitions for the type constraints and shows some example type signatures for typical \gls{MTASK} expressions and tasks.
116
117 \begin{lstClean}[caption={Classes and class collections for the \gls{MTASK} language.},label={lst:constraints}]
118 class type t | iTask, ... ,fromByteCode, toByteCode t
119 class basicType t | type t where ...
120
121 class mtask v | expr, ..., int, real, long v
122
123 someExpr :: v Int | mtask v
124 tempTask :: MTask v Bool | mtask, dht v
125 \end{lstClean}
126
127 \section{Expressions}
128 \Cref{lst:expressions} shows the \cleaninline{expr} class containing the functionality to lift values from the host language to the \gls{MTASK} language (\cleaninline{lit}), to do basic arithmetics and conditional execution.
129 For every common arithmetic operator in the host language, an \gls{MTASK} variant is present, suffixed by a period to not clash with \gls{CLEAN}'s builtin operators.
130
131 \begin{lstClean}[caption={The \gls{MTASK} class for expressions},label={lst:expressions}]
132 class expr v where
133 lit :: t -> v t | type t
134 (+.) infixl 6 :: (v t) (v t) -> v t | basicType, +, zero t
135 (-.) infixl 6 :: (v t) (v t) -> v t | basicType, -, zero t
136 (*.) infixl 7 :: (v t) (v t) -> v t | basicType, *, zero, one t
137 (/.) infixl 7 :: (v t) (v t) -> v t | basicType, /, zero t
138 (&.) infixr 3 :: (v Bool) (v Bool) -> v Bool
139 (|.) infixr 2 :: (v Bool) (v Bool) -> v Bool
140 Not :: (v Bool) -> v Bool
141 (==.) infix 4 :: (v a) (v a) -> v Bool | Eq, basicType a
142 (!=.) infix 4 :: (v a) (v a) -> v Bool | Eq, basicType a
143 (<.) infix 4 :: (v a) (v a) -> v Bool | Ord, basicType a
144 (>.) infix 4 :: (v a) (v a) -> v Bool | Ord, basicType a
145 (<=.) infix 4 :: (v a) (v a) -> v Bool | Ord, basicType a
146 (>=.) infix 4 :: (v a) (v a) -> v Bool | Ord, basicType a
147 If :: (v Bool) (v t) (v t) -> v t | type t
148 \end{lstClean}
149
150 Conversion to-and-fro data types is available through the overloaded functions \cleaninline{int}, \cleaninline{long} and \cleaninline{real}.
151
152 \begin{lstClean}
153 class int v a :: (v a) -> v Int
154 class real v a :: (v a) -> v Real
155 class long v a :: (v a) -> v Long
156 \end{lstClean}
157
158 Finally, values from the host language must be explicitly lifted to the \gls{MTASK} language using the \cleaninline{lit} function.
159 For convenience, there are many lower-cased macro definitions for often used constants as can be seen in \cref{lst:convenience_lits}
160
161 \begin{lstClean}[label={lst:convenience_lits}]
162 // Booleans
163 true :== lit True
164 false :== lit False
165 \end{lstClean}
166
167 \subsection{Functions}
168
169 \section{Tasks}
170 \subsection{Basic tasks}
171 \subsubsection{Peripherals}
172 \subsection{Task combinators}
173 \subsubsection{Parallel}
174 \subsubsection{Sequential}
175 \subsubsection{Miscellaneous}
176 \subsection{\glspl{SDS}}
177
178 \chapter{Green computing with \gls{MTASK}}
179
180 \chapter{Integration with \gls{ITASK}}
181 \section{Devices}
182 \section{Lift tasks}
183 \section{Lift \glspl{SDS}}
184
185 \chapter{Implementation}
186 IFL19 paper, bytecode instructieset~\cref{chp:bytecode_instruction_set}
187
188 \section{Integration with \gls{ITASK}}
189 IFL18 paper stukken
190
191 \input{subfilepostamble}
192 \end{document}