process many comments
[phd-thesis.git] / top / lang.tex
index 1b5735d..a248aa5 100644 (file)
@@ -2,11 +2,11 @@
 
 \input{subfilepreamble}
 
-\setcounter{chapter}{3}
+\setcounter{chapter}{4}
 
 \begin{document}
 \input{subfileprefix}
-\chapter{The \texorpdfstring{\gls{MTASK}}{mTask} language}%\texorpdfstring{\glsxtrshort{DSL}}{DSL}}%
+\chapter{The mTask language}%
 \label{chp:mtask_dsl}
 \begin{chapterabstract}
        This chapter introduces the \gls{TOP} language \gls{MTASK} by:
@@ -25,8 +25,18 @@ It is implemented as an \gls{EDSL} in \gls{CLEAN} using class-based---or tagless
 This means that the language interface, i.e.\ the language constructs, are a collection of type classes.
 Interpretations of this interface are data types implementing these classes.
 Due to the nature of this embedding technique, it is possible to have multiple interpretations for programs written in the \gls{MTASK} language.
-Furthermore, this particular type of embedding has the property that it is extensible both in language constructs and in intepretations.
-Adding a language construct is as simple as adding a type class and adding an interpretation is done by creating a new data type and providing implementations for the various type classes.
+Furthermore, this particular type of embedding has the property that it is extensible both in language constructs and in interpretations.
+Adding a language construct is as simple as adding a type class.
+Adding an interpretation is done by creating a new data type and providing implementations for the various type classes.
+
+\todo[inline]{Is dit niet de plek om uit te leggen welke restricties we aan mTask opleggen om het op een edge device te kunnen draaien?
+       1 onderscheid tussen mTask en de rest van het programma
+       2 mTask uit kunnen voeren zonder heap gebruik anders dan de task expressie. Dus: 
+       a geen recursieve data types
+       b geen hogere orde functies
+       c strict evaluation
+       d functies en objecten alleen op topniveau
+Nu lijkt het af en toe dat mTask onnodig primitief is, terwijl we het ook algemener hadden kunnen doen.}
 
 \section{Class-based shallow embedding}
 Let us illustrate this technique by taking the very simple language of literal values.
@@ -90,7 +100,7 @@ As it is shallowly embedded, the types of the terms in the language can be const
 Types in the \gls{MTASK} language are expressed as types in the host language, to make the language type safe.
 However, not all types in the host language are suitable for microcontrollers that may only have \qty{2}{\kibi\byte} of \gls{RAM}, so class constraints are added to the \gls{DSL} functions.
 \Cref{tbl:mtask-c-datatypes} shows the mapping from \gls{CLEAN} types to \ccpp{} types.
-The most used class constraint is the \cleaninline{type} class collection containing functions for serialization, printing, \gls{ITASK} constraints, \etc\ \citep[\citesection{6.9}]{plasmeijer_clean_2021}.
+The most used class constraint is the \cleaninline{type} class collection containing functions for serialisation, printing, \gls{ITASK} constraints, \etc\ \citep[\citesection{6.9}]{plasmeijer_clean_2021}.
 Most of these functions are automatically derivable using generic programming but can be specialised when needed.
 An even stronger restriction is defined for types that have a stack representation.
 This \cleaninline{basicType} class has instances for many \gls{CLEAN} basic types such as \cleaninline{Int}, \cleaninline{Real} and \cleaninline{Bool}.
@@ -114,7 +124,7 @@ These class constraints for values in \gls{MTASK} are omnipresent in all functio
        \end{tabular}
 \end{table}
 
-\footnote{In \gls{ARDUINO} \ccpp{} this usually equals a \cinline{long}.}
+\footnotetext{In \gls{ARDUINO} \ccpp{} this usually equals a \cinline{long}.}
 \Cref{lst:constraints} contains the definitions for the auxiliary types and type constraints (such as \cleaninline{type} and \cleaninline{basicType}) that are used to construct \gls{MTASK} expressions.
 
 \begin{lstClean}[caption={Classes and class collections for the \gls{MTASK} language.},label={lst:constraints}]
@@ -124,7 +134,7 @@ class basicType t | type t where ...
 
 The \gls{MTASK} language interface consists of a core collection of type classes bundled in the type class \cleaninline{class mtask} (see \cref{lst:collection}).
 Every interpretation of \gls{MTASK} terms implements the type classes in the \cleaninline{mtask} class collection.
-Optional \gls{MTASK} constructs such as perpipherals or lowered \gls{ITASK} \glspl{SDS} are not included in this class collection because not all devices or interpretations support this.
+%Optional \gls{MTASK} constructs such as peripherals or lowered \gls{ITASK} \glspl{SDS} are not included in this class collection because not all devices or interpretations support this.
 
 \begin{lstClean}[caption={Class collection for the \gls{MTASK} language.},label={lst:collection}]
 class mtask v | expr, ..., int, real, long v
@@ -142,14 +152,14 @@ To illustrate the structure of a \gls{MTASK} programs, \cref{lst:mtask_types} sh
 :: Main a = { main :: a }
 :: In a b = (In) infix 0 a b
 
-someTask :: MTask v Int | mtask v & liftsds v & sensor1 v & ...
+someTask :: MTask v Int | mtask v & lowerSds v & sensor1 v & ...
 someTask =
        sensor1 config1 \sns1->
        sensor2 config2 \sns2->
-          sds     \s1  = initialValue
-       In liftsds \s2  = someiTaskSDS
-       In fun     \fun1= ( \(a0, a1)->... )
-       In fun     \fun2= ( \a->... )
+          sds      \s1  = initialValue
+       In lowerSds \s2  = someiTaskSDS
+       In fun      \fun1= ( \(a0, a1)->... )
+       In fun      \fun2= ( \a->... )
        In { main = mainexpr }
 \end{lstClean}
 % VimTeX: SynIgnore off
@@ -190,7 +200,7 @@ class expr v where
        If :: (v Bool) (v t) (v t) -> v t | type t
 \end{lstClean}
 
-Conversion to-and-fro data types is available through the overloaded functions \cleaninline{int}, \cleaninline{long} and \cleaninline{real}.
+Conversion to and fro data types is available through the overloaded functions \cleaninline{int}, \cleaninline{long} and \cleaninline{real}.
 These functions convert the argument to the respective type similar to casting in \gls{C}.
 For most interpretations, there are instances of these classes for all numeric types.
 
@@ -223,16 +233,14 @@ e2 = If (e0 ==. int e1)
 The \gls{MTASK} language is shallowly embedded in \gls{CLEAN} and the terms are constructed and hence compiled at run time.
 This means that \gls{MTASK} programs can also be tailor-made at run time using \gls{CLEAN} functions, maximising the linguistic reuse \citep{krishnamurthi_linguistic_2001}.
 The \cleaninline{approxEqual} function in \cref{lst:example_macro} is an example of this.
-It performs a simple approximate equality---admittedly not taking into account all floating point pecularities.
+It performs a simple approximate equality---admittedly not taking into account all floating point peculiarities.
 When calling \cleaninline{approxEqual} in an \gls{MTASK} expression, the resulting code is inlined.
 
 \begin{lstClean}[label={lst:example_macro},caption={Approximate equality as an example of linguistic reuse in \gls{MTASK}.}]
-approxEqual :: (v Real) (v Real) (v Real) -> v Real | expr v
-approxEqual x y eps = If (x ==. y) true
-       ( If (x >. y)
-               (x -. y <. eps)
-               (y -. x <. eps)
-       )
+approxEqual :: (v Real) (v Real) (v Real) -> v Bool | expr v
+approxEqual x y eps = x ==. y |.  If (x >. y)
+       (x -. y <. eps)
+       (y -. x <. eps)
 \end{lstClean}
 
 \subsection{Data types}
@@ -260,7 +268,7 @@ class tupl v where
 Adding functions to the language is achieved by one type class to the \gls{DSL}.
 By using \gls{HOAS}, both the function definition and the calls to the function can be controlled by the \gls{DSL} \citep{pfenning_higher-order_1988,chlipala_parametric_2008}.
 The \gls{MTASK} language only allows first-order functions and no partial function application.
-To restrict this, a multi-paramater type class is used instead of a type class with one type variable.
+To restrict this, a multi-parameter type class is used instead of a type class with one type variable.
 The first parameter represents the shape of the arguments, the second parameter the interpretation.
 An instance is provided for each function arity instead of providing an instance for all possible arities to enforce that all functions are first order.
 By using argument tuples to represent the arity of the function, partial function applications are eradicated.
@@ -277,13 +285,14 @@ instance fun (Show a, Show b, Show c) Show | type a, ... where ...
 ...
 \end{lstClean}
 
-Deriving how to define and use functions from the type is quite the challenge even though the resulting syntax is made easier using the infix type \cleaninline{In}.
+Deriving how to define and use functions from the type is quite a challenge even though the resulting syntax is made easier using the infix type \cleaninline{In}.
 \Cref{lst:function_examples} show some examples of functions to illustrate the syntax.
 Splitting out the function definition for each single arity means that for every function arity and combination of arguments, a separate class constraint must be created.
 Many of the often used functions are already bundled in the \cleaninline{mtask} class constraint collection.
 The \cleaninline{factorial} functions shows a recursive version of the factorial function.
 The \cleaninline{factorialtail} function is a tail-call optimised version of the factorial function.
 It contains a manually added class constraint.
+\todo[inline]{Uitleggen waarom je dit doet}
 Zero-arity functions are always called with unit as an argument.
 An illustration of this is seen in the \cleaninline{zeroarity} expression.
 Finally, \cleaninline{swapTuple} shows an example of a tuple being swapped.
@@ -358,6 +367,8 @@ A task is an expression of the type \cleaninline{TaskValue a} in interpretation
 
 \subsection{Basic tasks}
 The \gls{MTASK} language contains interactive and non-interactive basic tasks.
+As \gls{MTASK} is integrated in \gls{ITASK}, the same notion of stability is applied to the observable task value (see \cref{fig:taskvalue}).
+\todo[inline]{Stability beter uitleggen?}
 The most rudimentary non-interactive basic tasks in the task language of \gls{MTASK} are \cleaninline{rtrn} and \cleaninline{unstable}.
 They lift the value from the \gls{MTASK} expression language to the task domain either as a stable or unstable value.
 There is also a special type of basic task for delaying execution.
@@ -375,7 +386,7 @@ class delay v :: (v n) -> MTask v n | long v n
 In order for the edge device to interact with the environment, peripherals such as sensors and actuators are employed.
 Some peripherals are available on the microcontroller package, others are connected with wires using protocols such as \gls{I2C}.
 For every supported sensor or actuator, basic tasks are available that allow interaction with the specific peripheral.
-The type classes for these tasks are not included in the \cleaninline{mtask} class collection as not all devices nor all language interpretations support every peripherals connected.
+The type classes for these tasks are not included in the \cleaninline{mtask} class collection as not all devices nor all language interpretations support every peripheral connected.
 
 An example of a built-in peripheral is the \gls{GPIO} system.
 This array of digital and analogue pins is controlled through software.
@@ -391,7 +402,7 @@ The first argument of the functions in this class is overloaded, i.e.\ it accept
 Analogue \gls{GPIO} tasks are bundled in the \cleaninline{aio} type class.
 \Gls{GPIO} pins usually operate according to a certain pin mode that states whether the pin acts as an input pin, an input with an internal pull-up resistor or an output pin.
 This setting can be set using the \cleaninline{pinMode} class by hand or by using the \cleaninline{declarePin} \gls{GPIO} pin constructor to declare it at the top level.
-Setting the pin mode is a task that immediately finisheds and yields a stable unit value.
+Setting the pin mode is a task that immediately finishes and yields a stable unit value.
 Writing to a pin is also a task that immediately finishes, but yields the written value.
 Reading a pin is a task that yields an unstable value---i.e.\ the value read from the actual pin.
 
@@ -469,7 +480,7 @@ Sequential task combination allows the right-hand side expression to \emph{obser
 All sequential task combinators are defined in the \cleaninline{step} class and are by default defined in terms of the Swiss Army knife step combinator (\cleaninline{>>*.}, \cref{lst:mtask_sequential}).
 This combinator has a single task on the left-hand side and a list of \emph{task continuations} on the right-hand side.
 Every rewrite step, the list of task continuations are tested on the task value.
-If one of the predicates matches, the task continues with the result of this continuations.
+If one of the predicates matches, the task continues with the result of these continuations.
 Several shorthand combinators are derived from the step combinator.
 \cleaninline{>>=.} is a shorthand for the bind operation, if the left-hand side is stable, the right-hand side function is called to produce a new task.
 \cleaninline{>>\|.} is a shorthand for the sequence operation, if the left-hand side is stable, it continues with the right-hand side task.
@@ -538,7 +549,7 @@ dis :: (TaskValue a) (TaskValue a)
 dis NoValue r = r
 dis l NoValue = l
 dis (Value l ls) (Value r rs)
-       | rs        = Value r True
+       | rs && not ls = Value r rs
        | otherwise = Value l ls
                \end{lstClean}
        \end{subfigure}
@@ -553,7 +564,7 @@ task :: MTask v Bool
 task =
        declarePin D0 PMInput \d0->
        declarePin D1 PMInput \d1->
-       let monitor pin = readD pin >>*. [IfValue (\x->x) \x->rtrn x]
+       let monitor pin = readD pin >>*. [IfValue id rtrn]
        In {main = monitor d0 .||. monitor d1}
 \end{lstClean}
 
@@ -578,7 +589,7 @@ task = declarePin A1 PMInput  \a1->
        {main = rpeat (readA a1 >>~. writeA a2 >>|. delay (lit 1000))}
 \end{lstClean}
 
-\subsection{\texorpdfstring{\Glsxtrlongpl{SDS}}{Shared data sources}}
+\subsection{Shared data sources}
 For some collaborations it is cumbersome to only communicate data using task values.
 \Glspl{SDS} are a safe abstraction over any data that fill this gap.
 It allows tasks to safely and atomically read, write and update data stores in order to share data with other tasks.
@@ -611,20 +622,22 @@ Using a sequence of \cleaninline{getSds} and \cleaninline{setSds} would be unsaf
 
 \begin{lstClean}[label={lst:mtask_sds_examples},caption={Examples with \glspl{SDS} in \gls{MTASK}.}]
 task :: MTask v Int | mtask v
-task = declarePin D3 PMInput \d3->
+task =
+       declarePin D3 PMInput \d3->
        declarePin D5 PMInput \d5->
           sds \share=0
        In fun \count=(\pin->
                    readD pin
-               >>* [IfValue (\x->x) (\_->updSds (\x->x +. lit 1) share)]
+               >>* [IfValue id (\_->updSds (\x->x +. lit 1) share)]
                >>| delay (lit 100) // debounce
                >>| count pin)
        In {main=count d3 .||. count d5}
 \end{lstClean}
 
 \section{Interpretations}
-This section describes all the interpretations that the \gls{MTASK} language has.
-Not all of these interpretations are necessarily \gls{TOP} engines, i.e.\ not all the interpretations execute the resulting tasks.
+\todo[inline]{Iets meer uitleg over waarom dit zo handig is}
+This section describes all the interpretations that the \gls{MTASK} language has to offer.
+Not all these interpretations are necessarily \gls{TOP} engines, i.e.\ not all the interpretations execute the resulting tasks.
 Some may perform an analysis over the program or typeset the program.
 
 \subsection{Pretty printer}
@@ -635,7 +648,7 @@ It runs the pretty printer and returns a list of strings containing the pretty p
 The pretty printing function does the best it can but obviously cannot reproduce layout, curried functions, and variable names.
 This shortcoming is illustrated by printing a blink task that contains a function and currying in \cref{lst:showexample}.
 
-\begin{lstClean}[caption={The entrypoint for the pretty printing interpretation.},label={lst:showmain}]
+\begin{lstClean}[caption={The entry point for the pretty printing interpretation.},label={lst:showmain}]
 :: Show a // from the mTask pretty printing library
 showMain :: (Main (Show a)) -> [String]
 \end{lstClean}
@@ -654,11 +667,11 @@ blinkTask =
 
 \subsection{Simulator}
 The simulator converts the expression to a ready-for-work \gls{ITASK} simulation.
-There is one entrypoint for this interpretation (see \cref{lst:simulatemain}).
+There is one entry point for this interpretation (see \cref{lst:simulatemain}).
 The task resulting from the \cleaninline{simulate} function presents the user with an interactive simulation environment (see \cref{fig:sim}).
 The simulation allows the user to (partially) execute tasks, control the simulated peripherals, inspect the internal state of the tasks, and interact with \glspl{SDS}.
 
-\begin{lstClean}[caption={The entrypoint for the simulation interpretation.},label={lst:simulatemain}]
+\begin{lstClean}[caption={The entry point for the simulation interpretation.},label={lst:simulatemain}]
 :: TraceTask a // from the mTask simulator library
 simulate :: (Main (TraceTask a)) -> [String]
 \end{lstClean}
@@ -670,23 +683,26 @@ simulate :: (Main (TraceTask a)) -> [String]
 \end{figure}
 
 \subsection{Byte code compiler}
-The main interpretation of the \gls{MTASK} system is the byte code compiler (\cleaninline{:: BCInterpret}).
+The main interpretation of the \gls{MTASK} system is the byte code compiler (\cleaninline{:: BCInterpret a}).
+This interpretation compiles the \gls{MTASK} term at run time to byte code.
 With it, and a handful of integration functions, \gls{MTASK} tasks can be executed on microcontrollers and integrated in \gls{ITASK} as if they were regular \gls{ITASK} tasks.
 Furthermore, with a special language construct, \glspl{SDS} can be shared between \gls{MTASK} and \gls{ITASK} programs as well.
 The integration with \gls{ITASK} is explained thoroughly later in \cref{chp:integration_with_itask}.
 
 The \gls{MTASK} language together with \gls{ITASK} is a heterogeneous \gls{DSL}.
 I.e.\ some components---for example the \gls{RTS} on the microcontroller that executes the tasks---is largely unaware of the other components in the system, and it is executed on a completely different architecture.
-The \gls{MTASK} language is based on a simply-typed $\lambda$-calculus with support for some basic types, arithmetic operations, and function definitions.
-As the language is a \gls{TOP} language, it is also enriched with a task language (see \cref{sec:top}).
+The \gls{MTASK} language is a \gls{TOP} language with basic tasks tailored for \gls{IOT} edge devices (see \cref{sec:top}).
+It uses expressions based a simply-typed $\lambda$-calculus with support for some basic types, arithmetic operations, and function definitions.
 
 \section{Conclusion}
+This chapter gave an overview of the complete \gls{MTASK} \gls{DSL}.
 The \gls{MTASK} language is a rich \gls{TOP} language tailored for \gls{IOT} edge devices.
 The language is implemented as a class-based shallowly \gls{EDSL} in the pure functional host language \gls{CLEAN}.
 The language is an enriched lambda calculus as a host language.
 It provides language constructs for arithmetic expressions, conditionals, functions, but also non-interactive basic tasks, task combinators, peripheral support, and integration with \gls{ITASK}.
 Terms in the language are just interfaces and can be interpreted by one or more interpretations.
-The most important interpretation of the language is the byte code compiler.
+When using the byte code compiler, terms in the \gls{MTASK} language are type checked at compile time but are constructed and compiled at run time.
+This facilitates tailor-making tasks for the current work requirements.
 
 \input{subfilepostamble}
 \end{document}