\documentclass[../thesis.tex]{subfiles}

\input{subfilepreamble}

\begin{document}
\chapter{Prelude}%
\label{chp:introduction}
\begin{chapterabstract}
	This chapter introduces the contents of the thesis and a reading guide.
	Furthermore, it provides background material on \glsxtrlong{IOT}, \glsxtrlongpl{DSL}, and \glsxtrlong{TOP}; and a detailed overview of the contributions.
\end{chapterabstract}

There are at least 13.4 billion computers connected to the internet at the time of writing\footnote{\url{https://transformainsights.com/research/tam/market}, accessed on: \formatdate{13}{10}{2022}}.
These devices sense, act, or otherwise interact with people, other computers, and the world surrounding us.
Notwithstanding the substantial variety among these devices, they have one thing in common: they are all require software to operate.

An increasing amount of these connected devices are so-called \emph{edge devices} that operate in the \gls{IOT}.
Typically these edge devices are small microprocessors containing sensors and actuators to interact with the physical world.
The variety within edge devices, and the fact that they differ substantially from other types of devices, makes it complex to program \gls{IOT} systems.\todo{make sentence logical}
Hence, an \gls{IOT} programmer has to program each device and their interoperation using different programming paradigms, programming languages, and abstraction levels resulting in semantic friction.

This thesis introduces research on the many aspects of orchestrating \gls{IOT} systems using \gls{TOP}.
\Gls{TOP} is a novel programming paradigm for programming multi-tier workflow systems using a single declarative specification of the work that needs to be done.
Using advanced compiler technologies, much of the internals and communication of multi-tier applications are automatically generated and the result of compilation is a ready-for-work application.
Unfortunately, because the abstraction level is so high, the hardware requirements are too excessive to be suitable for the average edge device.

This is where \glspl{DSL} come into play.
\Glspl{DSL} are languages created with a specific domain in mind.
Consequently, domain knowledge does not have to be expressed in the language itself but they can be built-in features, thus drastically reducing the hardware requirements even with high levels of abstraction.

\section{Reading guide}
On Wikipedia, a musical rhapsody is defined as follows \citep{wikipedia_contributors_rhapsody_2022}:
\begin{quote}\emph{%
	A \emph{rhapsody} in music is a one-movement work that is episodic yet integrated, free-flowing in structure, featuring a range of highly contrasted moods, colour, and tonality.}
\end{quote}
This thesis is structured as a pure functional rhapsody containing three episodes barded by the introduction and conclusion (\cref{chp:introduction,chp:conclusion}).
\Cref{prt:dsl} is a paper-based---otherwise known as cumulative---episode providing insight in advanced \gls{DSL} embedding techniques.
The chapters are readable independently.
\Cref{prt:top} is a monograph showing \gls{MTASK}, a \gls{TOP} \gls{DSL} for the \gls{IOT}.
Hence, the chapters are best read in order.
\Cref{prt:tvt} is a journal article in which quantitative and qualitatively compares traditional tiered \gls{IOT} programming to tierless programming.
The chapter is readable independently.

The following sections provide background material on the \gls{IOT}, \glspl{DSL}, and \gls{TOP} after which a detailed overview of the contributions is presented.
Text typeset as \texttt{teletype} represents source code.
Standalone source code listings are used are marked with the programming language used.
For the \gls{FP} language \gls{CLEAN}, a guide tailored to \gls{HASKELL} programmers is available as in \cref{chp:clean_for_haskell_programmers}.

\section{Internet of things}\label{sec:back_iot}
The \gls{IOT} is growing rapidly and it is changing the way people and machines interact with the world.
While the term \gls{IOT} briefly gained interest around 1999 to describe the communication of \gls{RFID} devices \citep{ashton_internet_1999,ashton_that_2009}, it probably already popped up halfway the eighties in a speech by \citet{peter_t_lewis_speech_1985}:

\begin{quote}
	\emph{The \glsxtrlong{IOT}, or \glsxtrshort{IOT}, is the integration of people, processes and technology with connectable devices and sensors to enable remote monitoring, status, manipulation and evaluation of trends of such devices.}
\end{quote}

CISCO states that the \gls{IOT} started when there where as many connected devices as there were people on the globe, i.e.\ around 2008 \citep{evans_internet_2011}.
Today, \gls{IOT} is the term for a system of devices that sense the environment, act upon it and communicate with each other and the world.
These connected devices are already in households all around us in the form of smart electricity meters, fridges, phones, watches, home automation, \etc.

When describing \gls{IOT} systems, a tiered---or layered---architecture is often used to compartmentalize the technology.
The number of tiers heavily depends on the required complexity of the model but for the intents and purposes of this thesis, the four layer architecture as shown in \cref{fig:iot-layers} is used.

\begin{figure}[ht]
	\centering
	\includestandalone{iot-layers}
	\caption{A four-tier \gls{IOT} architecture.}%
	\label{fig:iot-layers}
\end{figure}

To explain the tiers, an example \gls{IOT} application---home automation---is dissected accordingly.
Closest to the end-user is the presentation layer, it provides the interface between the user and the \gls{IOT} system.
In home automation this may be a web interface or an app used on a phone or mounted tablet to interact with the edge devices and view the sensor data.

The application layer provides the \glspl{API}, data interfaces, and data storage of the \gls{IOT} system.
A cloud server or local server provides this layer in a typical home automation application.

All layers are connected using the network layer.
In many applications this is implemented using conventional networking techniques such as WiFi or Ethernet.
However, networks or layers on top of it---tailored to the needs of \gls{IOT} systems---have been increasingly popular such as \gls{BLE}, LoRa, ZigBee, LTE-M, or \gls{MQTT}.

The perception layer---also called edge layer---collects the data and interacts with the environment.
It consists of edge devices such as microprocessors equipped with various sensors and actuators.
In home automation this layer consists of all the devices hosting the sensors and actuators such as a smart lightbulb, an actuator to open a door or a temperature and humidity sensor.

Across the layers, the devices are a large heterogeneous collection of different platforms, protocols, paradigms, and programming languages often resulting in impedance problems or semantic friction between layers when programming \citep{ireland_classification_2009}.
Even more so, perception layer itself often is a heterogeneous collections of microprocessors in itself as well, each having their own peculiarities, language of choice, and hardware interfaces.
As the edge hardware needs to be cheap, small-scale, and energy efficient, the microprocessors used to power these devices do not have a lot of computational power, only a soup\c{c}on of memory, and little communication bandwidth.
Typically the devices do not run a full fledged \gls{OS} but a compiled firmware that written in an imperative language.
While devices are getting a bit faster, smaller, and cheaper, they keep these properties to an extent, greatly reducing the flexibility for dynamic systems where tasks are created on the fly, executed on demand, or require parallel execution.
As program memory is mostly flash based and only lasts a couple of thousand writes before it wears out, it is not suitable for rapid reconfiguring and reprogramming.

These problems can be mitigated by dynamically sending code to be interpreted to the microprocessor.
With interpretation, a specialized interpreter is flashed in the program memory once that receives the program code to execute at runtime.
Interpretation always comes with an overhead, making it challenging to create them for small edge devices.
However, the hardware requirements can be reduced by embedding domain-specific data into the programming language to be interpreted, so called \glspl{DSL}.

\section{\texorpdfstring{\Glsxtrlongpl{DSL}}{Domain-specific languages}}\label{sec:back_dsl}
% General
Programming languages can be divided up into two categories: \glspl{DSL}\footnote{Historically this has been called DSEL as well.} and \glspl{GPL} \citep{fowler_domain_2010}.
Where \glspl{GPL} are not made with a demarcated area in mind, \glspl{DSL} are tailor-made for a specific domain.
Writing idiomatic domain-specific code in an \gls{DSL} is easy but this may come at the cost of the \gls{DSL} being less expressive to an extent that it may not even be Turing complete.
\Glspl{DSL} come in two main flavours: standalone and embedded (\cref{sec:standalone_embedded})\footnote{Also called external and internal respectively.} of which \glspl{EDSL} can again be classified into heterogeneous and homogeneous languages (\cref{sec:hetero_homo}).
This hyponymy is shown in \cref{fig:hyponymy_of_dsls}.

\begin{figure}[ht]
	\centering
	\includestandalone{hyponymy_of_dsls}
	\caption{Hyponymy of \glspl{DSL} (adapted from \citet[\citepage{2}]{mernik_extensible_2013})}%
	\label{fig:hyponymy_of_dsls}
\end{figure}

\subsection{Standalone and embedded}\label{sec:standalone_embedded}
\glspl{DSL} where historically created as standalone languages, meaning all the machinery is developed solely for the language.
The advantage of this approach is that the language designer is free to define the syntax and type system of the language as they wish, not being restricted by any constraint.
Unfortunately it also means that they need to develop a compiler or interpreter for the language to be usable making standalone \glspl{DSL} costly to create.
Examples of standalone \glspl{DSL} are regular expressions, make, yacc, XML, SQL, \etc.

The dichotomous approach is embedding the \gls{DSL} in a host language, i.e.\ \glspl{EDSL} \citep{hudak_modular_1998}.
By defining the language as constructs in the host language, much of the machinery is inherited and the cost of creating embedded languages is very low.
There is more linguistic reuse~\cite{krishnamurthi_linguistic_2001}.
There are however two sides to the this coin.
If the syntax of the host language is not very flexible, the syntax of the \gls{DSL} may become clumsy.
Furthermore, errors shown to the programmer may be larded with host language errors, making it difficult for a non-expert of the host language to work with the \gls{DSL}.
Pure \gls{FP} languages are especially suitable for hosting embedded \glspl{DSL} because they have strong and versatile type systems, minimal but flexible syntax and offer referential transparency.

\subsection{Heterogeneity and homogeneity}\label{sec:hetero_homo}
\Citet{tratt_domain_2008} applied a notion from metaprogramming \citep{sheard_accomplishments_2001} to \glspl{EDSL} to define homogeneity and heterogeneity of \glspl{EDSL} as follows:

\begin{quote}
	\emph{
	A homogeneous system is one where all the components are specifically designed to work with each other, whereas in heterogeneous systems at least one of the components is largely, or completely, ignorant of the existence of the other parts of the system.
}
\end{quote}

Homogeneous \glspl{EDSL} are therefore languages that are solely defined as an extension to their host language.
They often restrict features of the host language to provide a safer interface or capture an idiomatic pattern in the host language for reuse.
The difference between a library and a homogeneous \glspl{EDSL} is not always clear.
Examples of homogeneous \glspl{EDSL} are libraries such as ones for sets, \glspl{GUI} creation, LISP's macro system, \etc.

On the other hand, heterogeneous \glspl{EDSL} are languages that are not executed in the host language.
For example, \citep{elliott_compiling_2003} describe the language Pan, for which the final representation in the host language is a compiler that will, when executed, generate code for a completely different target platform.
In fact, \gls{ITASK} and \gls{MTASK} are both heterogeneous \glspl{EDSL} and \gls{MTASK} specifically is a compiling \gls{DSL}.

\section{\texorpdfstring{\Glsxtrlong{TOP}}{Task-oriented programming}}\label{sec:back_top}
\Gls{TOP} is a declarative programming paradigm designed to model interactive systems \citep{plasmeijer_task-oriented_2012}.
\Citet{steenvoorden_tophat_2022} defines two instruments for \gls{TOP}: \gls{TOP} languages and \gls{TOP} engines.
A \gls{TOP} language is a language to specify workflows.\todo{improve sentence}
A \gls{TOP} engine executes such a specification as a ready-for-work application.
Instead of dividing problems into layers or tiers, as is done in \gls{IOT} architectures, it deals with separation of concerns in a novel way.
From the data types, utilising various \emph{type-parametrised} concepts, all other aspects are handled automatically (see \cref{fig:tosd}).
This approach to software development is called \gls{TOSD} \citep{wang_maintaining_2018}.

\begin{figure}[ht]
	\centering
	\begin{subfigure}[t]{.5\textwidth}
		\centering
		\includestandalone{traditional}
		\caption{Traditional layered approach.}
	\end{subfigure}%
	\begin{subfigure}[t]{.5\textwidth}
		\centering
		\includestandalone{tosd}
		\caption{\Gls{TOSD} approach.}
	\end{subfigure}
	\caption{Separation of concerns in a traditional setting and in \gls{TOSD} (adapted from~\cite[\citepage{20}]{wang_maintaining_2018}).}%
	\label{fig:tosd}
\end{figure}

\todo{describe relation with \gls{IOT} architecture}
\begin{description}
	\item[\Glsxtrshort{UI} (presentation layer):]
		The \gls{UI} of the system is automatically generated from the representation of the type.
		Even though the \gls{UI} is generated from the structure of the datatypes, in practical \gls{TOP} systems it can be tweaked afterwards to suit the specific needs of the application.
	\item[Tasks (business layer):]
		A task is an abstract representation of a piece of work that needs to be done.
		It provides an intuitive abstraction over work in the real world.
		Just as with real-life tasks and workflow, tasks can be combined in various ways such as in parallel or in sequence.
		Furthermore, a task is observable which means it is possible to observe a---partial---result during execution and act upon it by for example starting new tasks.
		Examples of tasks are filling in a form, sending an email, reading a sensor or even doing a physical task.
	\item[\Glsxtrshortpl{SDS} (resource access):]
		Tasks can communicate using task values but this imposes a problem in many collaboration patterns where tasks that are not necessarily related need to share data.
		Tasks can also share data using \glspl{SDS}, an abstraction over any data.
		An \gls{SDS} can represent typed data stored in a file, a chunk of memory, a database \etc.
		\Glspl{SDS} can also represent external impure data such as the time, random numbers or sensory data.
		Similar to tasks, transformation and combination of \glspl{SDS} is possible.
	\item[Programming language (\glsxtrshort{UOD}):]
		The \gls{UOD} from the business layer is explicitly and separately modelled by the relations that exist in the functions of the host language.
\end{description}

\subsection{\texorpdfstring{\Gls{ITASK}}{ITask}}
The concept of \gls{TOP} originated from the \gls{ITASK} framework, a declarative workflow language and \gls{TOP} engine for defining multi-user distributed web applications implemented as an \gls{EDSL} in the lazy pure \gls{FP} language \gls{CLEAN} \citep{plasmeijer_itasks:_2007,plasmeijer_task-oriented_2012}.
From the structural properties of the data types, the entire user interface is automatically generated.

As an example, \cref{lst:enter_person,fig:enter_person} show the \gls{ITASK} code and the corresponding \gls{UI} for a simple task for entering a person.
From the data type definitions (\cref{lst:dt_fro,lst:dt_to}), using generic programming (\cref{lst:dt_derive}), the \glspl{UI} for the data types are automatically generated.
Using task combinators (see \cleaninline{>>!} at \cref{lst:task_comb}), the tasks can be combined in sequence.
Only when the user entered a complete value in the web editor, then the continue button enables and the result can be viewed.
Special combinators (e.g.\ \cleaninline{@>>} at \cref{lst:task_ui}) are available to tweak the \gls{UI} afterwards.

\begin{figure}[ht]
	\includegraphics[width=.32\linewidth]{person0}
	\includegraphics[width=.32\linewidth]{person1}
	\includegraphics[width=.32\linewidth]{person2}
	\caption{The \gls{UI} for entering a person in \gls{ITASK}.}%
	\label{fig:enter_person}
\end{figure}

\begin{lstClean}[numbers=left,caption={The \gls{UI} and code for entering a person in \gls{ITASK}.},label={lst:enter_person}]
:: Person = { name :: String, gender :: Gender, dateOfBirth :: Date }[+\label{lst:dt_fro}+]
:: Gender = Male | Female | Other String[+\label{lst:dt_to}+]

derive class iTask Person, Gender[+\label{lst:dt_derive}+]

enterPerson :: Task Person
enterPerson
	=            Hint "Enter a person:" @>> enterInformation [][+\label{lst:task_ui}+]
	>>! \result->Hint "You Entered:"    @>> viewInformation  [] result[+\label{lst:task_comb}+]
\end{lstClean}

\subsection{\texorpdfstring{\Gls{MTASK}}{MTask}}
This thesis uses a novel \gls{TOP} language designed for defining workflows for \gls{IOT} edge devices \citep{koopman_task-based_2018}.
It is written in \gls{CLEAN} as a multi-view \gls{EDSL} and hence there are multiple interpretations of the language of which the byte code compiler is the most relevant for this thesis.
From the terms in the \gls{TOP} language, a very compact binary representation of the work that needs to be done is compiled.
This specification is then sent to a device that runs the \gls{MTASK} \gls{RTS}, a domain-specific \gls{TOP} engine implemented as a feather-light domain-specific \gls{OS}.
\Gls{MTASK} is seamlessly integrated with \gls{ITASK}, it allows the programmer to define all layers of an \gls{IOT} system from a single declarative specification.

\todo[inline]{Example application here, e.g.\ blink?}


\subsection{Other \texorpdfstring{\glsxtrshort{TOP}}{TOP} languages}
While \gls{ITASK} conceived \gls{TOP}, it is not the only \gls{TOP} language and engine.
Some \gls{TOP} languages and systems arose from Master's and Bachelor's thesis projects (e.g.\ \textmu{}Task \citep{piers_task-oriented_2016} and LTasks \citep{van_gemert_task_2022}) or were created to solve a practical problem (e.g.\ Toppyt \citep{lijnse_toppyt_2022} and hTask \citep{lubbers_htask_2022}).
Furthermore, \gls{TOPHAT} is a fully formally specified \gls{TOP} language designed to capture the essence of \gls{TOP} formally \citep{steenvoorden_tophat_2019}.
It is also possible to translate \gls{TOPHAT} code to \gls{ITASK} to piggyback on the \gls{TOP} engine it offers \citep[\citesection{G.3}]{steenvoorden_tophat_2022}.

\section{Contributions}\label{sec:contributions}
This section provides a thorough overview of the relation to publications and the scientific contributions of the episodes and chapters.

\subsection{\nameref{prt:dsl}}
The \gls{MTASK} system is a heterogeneous \gls{EDSL} and during the development of it, several novel basal techniques for embedding \glspl{DSL} in \gls{FP} languages have been found.
This first episode is a cumulative---otherwise known as paper-based---episode consisting of two papers published on novel embedding techniques.
Both papers are readable independently.

\subsubsection*{\Fullref{chp:classy_deep_embedding}}
This chapter is based on the paper: \citeentry{lubbers_deep_2022}\todo{change in-press when published}.

While supervising \citeauthor{amazonas_cabral_de_andrade_developing_2018}'s \citeyear{amazonas_cabral_de_andrade_developing_2018} Master's thesis, focussing on an early version of \gls{MTASK}, a seed was planted for a novel deep embedding technique for \glspl{DSL} where the resulting language is extendible both in constructs and in interpretation using type classes and existential data types.
Slowly the ideas organically grew to form the technique shown in the paper.
The related work section is updated with the research found only after publication.
\Cref{sec:classy_reprise} was added after publication and contains a (yet) unpublished extension of the embedding technique for reducing the required boilerplate.
The research from this paper and writing the paper was solely performed by me.

\subsubsection*{\Fullref{chp:first-class_datatypes}}
This chapter is based on the paper: \citeentry{lubbers_first-class_2022}\todo{change when accepted}.

It shows how to inherit data types from the host language in \glspl{EDSL} using metaprogramming.
It does so by providing a proof-of-concept implementation using \gls{HASKELL}'s metaprogramming system: \glsxtrlong{TH}.
Besides showing the result, the paper also serves as a gentle introduction to using \glsxtrlong{TH} and contains a thorough literature study on research that uses \glsxtrlong{TH}.
The research in this paper and writing the paper was performed by me, though there were weekly meetings with Pieter Koopman and Rinus Plasmeijer in which we discussed and refined the ideas.

\subsection{\nameref{prt:top}}
This is a monograph compiled from several papers and revised lecture notes on \gls{MTASK}, the \gls{TOP} system used to orchestrate the \gls{IOT}.
It provides a gentle introduction to the \gls{MTASK} system elaborates on \gls{TOP} for the \gls{IOT}.
\todo[inline]{outline the chapters}

\begin{itemize}
	\item \citeentry{koopman_task-based_2018}

		This is the initial \gls{TOP}/\gls{MTASK} paper.
		It provides an overview of the initial \gls{MTASK} language and shows first versions of a pretty printer, an \gls{ITASK} simulation and a \gls{C} code generation view.
		\paragraph{Contribution}
		Pieter Koopman wrote it, I helped with the software and research.
	\item \citeentry{lubbers_task_2018}
		
		This paper was an extension of my Master's thesis \citep{lubbers_task_2017}.
		It shows how a simple imperative variant of \gls{MTASK} was integrated with \gls{ITASK}.
		While the language was a lot different than later versions, the integration mechanism is still used in \gls{MTASK} today.
		\paragraph{Contribution}
		The research in this paper and writing the paper was performed by me, though there were weekly meetings with Pieter Koopman and Rinus Plasmeijer in which we discussed and refined the ideas.
	\item \citeentry{lubbers_multitasking_2019}\footnote{%
		This work acknowledges the support of the ERASMUS+ project ``Focusing Education on Composability, Comprehensibility and Correctness of Working Software'', no. 2017--1--SK01--KA203--035402
		}

		This paper was a short paper on the multitasking capabilities of \gls{MTASK} in contrast to traditional multitasking methods for \gls{ARDUINO}.
		\paragraph{Contribution}
		The research in this paper and writing the paper was performed by me, though there were weekly meetings with Pieter Koopman and Rinus Plasmeijer.
	\item \citeentry{koopman_simulation_2018}\footnotemark[\value{footnote}]\todo{change when published}

		These revised lecture notes are from a course on the \gls{MTASK} simulator was provided at the 2018 \gls{CEFP}/\gls{3COWS} winter school in Ko\v{s}ice, Slovakia.
		\paragraph{Contribution}
		Pieter Koopman wrote and taught it, I helped with the software and research.
	\item \citeentry{lubbers_writing_2019}\footnotemark[\value{footnote}]\todo{change when published}

		These revised lecture notes are from a course on programming in \gls{MTASK} provided at the 2019 \gls{CEFP}/\gls{3COWS} summer school in Budapest, Hungary.
		\paragraph{Contribution}
		Pieter Koopman prepared and taught half of the lecture and supervised the practical session.
		I taught the other half of the lecture, wrote the lecture notes, made the assignments and supervised the practical session.
	\item \citeentry{lubbers_interpreting_2019}

		This paper shows an implementation for \gls{MTASK} for microcontrollers in the form of a compilation scheme and informal semantics description.
		\paragraph{Contribution}
		The research in this paper and writing the paper was performed by me, though there were weekly meetings with Pieter Koopman and Rinus Plasmeijer.
	\item \citeentry{crooijmans_reducing_2022}\todo{change when published}

		This paper shows how to create a scheduler so that devices running \gls{MTASK} tasks can go to sleep more automatically.
		\paragraph{Contribution}
		The research was carried out by \citet{crooijmans_reducing_2021} during his Master's thesis.
		I did the daily supervision and helped with the research, Pieter Koopman was the formal supervisor and wrote most of the paper.
	\item \emph{Green Computing for the Internet of Things}\footnote{
		This work acknowledges the support of the Erasmus+ project ``SusTrainable---Promoting Sustainability as a Fundamental Driver in Software Development Training and Education'', no. 2020--1--PT01--KA203--078646}\todo{change when published}

		\paragraph{Contribution}
		These revised lecture notes are from a course on sustainable programming using \gls{MTASK} provided at the 2022 SusTrainable summer school in Rijeka, Croatia.
		Pieter prepared and taught a quarter of the lecture and supervised the practical session.
		I prepared and taught the other three quarters of the lecture, made the assignments and supervised the practical session\todo{writing contribution}.
\end{itemize}

\subsection{\nameref{prt:tvt}}
\Cref{prt:tvt} is based on a journal paper that quantitatively and qualitatively compares traditional \gls{IOT} architectures with \gls{IOT} systems using \gls{TOP} and contains a single chapter.
This chapter is based on the journal paper: \citeentry{lubbers_could_2022}\todo{change when published}\footnote{This work is an extension of the conference article: \citeentry{lubbers_tiered_2020}\footnotemark{}}.
\footnotetext{This paper was partly funded by the Radboud-Glasgow Collaboration Fund.}

It compares programming traditional tiered architectures to tierless architectures by showing a qualitative and a quantitative four-way comparison of a smart-campus application.

\paragraph{Contribution}
Writing the paper was performed by all authors.
I created the server application, the \gls{CLEAN}/\gls{ITASK}/\gls{MTASK} implementation (\glsxtrshort{CWS}) and the \gls{CLEAN}/\gls{ITASK} implementation (\glsxtrshort{CRS})
Adrian Ramsingh created the \gls{MICROPYTHON} implementation (\glsxtrshort{PWS}), the original \gls{PYTHON} implementation (\glsxtrshort{PRS}) and the server application were created by \citet{hentschel_supersensors:_2016}.

\input{subfilepostamble}
\end{document}
