Merge branch 'master' of git.martlubbers.net:msc-thesis1617
[msc-thesis1617.git] / top.combinators.tex
1 \Glspl{Task} in \gls{iTasks} can be combined using so called
2 \gls{Task}-combinators. Combinators describe relations between \glspl{Task}.
3 There are only two basic types of combinators; parallel and sequence. All other
4 combinators are derived from the basic combinators. Type signatures of
5 simplified versions of the basic combinators and their derivations are given in
6 Listing~\ref{lst:combinators}
7
8 \begin{lstlisting}[language=Clean,%
9 caption={\Gls{Task}-combinators},label={lst:combinators}]
10 //Step combinator
11 (>>=) infixl 1 :: (Task a) (a -> Task b) -> Task b | iTask a & iTask b
12 (>>*) infixl 1 :: (Task a) [TaskCont a (Task b)] -> Task b | iTask a & iTask b
13 :: TaskCont a b
14 = OnValue ((TaskValue a) -> Maybe b)
15 | OnAction Action ((TaskValue a) -> Maybe b)
16 | E.e: OnException (e -> b) & iTask e
17 | OnAllExceptions (String -> b)
18 :: Action = Action String
19
20 //Parallel combinators
21 (-||-) infixr 3 :: (Task a) (Task a) -> Task a | iTask a
22 (||-) infixr 3 :: (Task a) (Task b) -> Task b | iTask a & iTask b
23 (-||) infixl 3 :: (Task a) (Task b) -> Task a | iTask a & iTask b
24 (-&&-) infixr 4 :: (Task a) (Task b) -> Task (a,b) | iTask a & iTask b
25 \end{lstlisting}
26
27 \paragraph{Sequence:}
28 The implementation for the sequence combinator is called the \CI{step}
29 (\CI{>>*}). This combinator runs the left-hand \gls{Task} and starts the
30 right-hand side when a certain predicate holds. Predicates can be propositions
31 about the \CI{TaskValue}, user actions from within the web browser or a thrown
32 exception. The familiar bind-combinator is an example of a sequence combinator.
33 This combinator runs the left-hand side and continues to the right-hand
34 \gls{Task} if there is an \CI{UnStable} value and the user presses continue or
35 when the value is \CI{Stable}. The combinator could have been implemented as
36 follows:
37 \begin{lstlisting}[language=Clean]
38 (>>=) infixl 1 :: (Task a) (a -> (Task b)) -> (Task b) | iTask a & iTask b
39 (>>=) ta f = ta >>* [OnAction "Continue" onValue, OnValue onStable]
40 where
41 onValue (Value a _) = Just (f a)
42 onValue _ = Nothing
43
44 onStable (Value a True) = Just (f a)
45 onStable _ = Nothing
46 \end{lstlisting}
47
48 \paragraph{Parallel:}
49 The parallel combinator allows for concurrent \glspl{Task}. The \glspl{Task}
50 combined with these operators will appear at the same time in the web browser
51 of the user and the results are combined as the type dictates. All parallel
52 combinators used are derived from the basic parallel combinator that is very
53 complex and only used internally.