add factorial example'
[msc-thesis1617.git] / arch.example.tex
1 \subsection{Framework}
2 Systems built with support for \gls{mTask} are often following the same design
3 pattern. First the devices are created --- with or without the interaction of
4 the user --- and they are then connected. When all devices are registered, the
5 \gls{mTask}-\glspl{Task} can be sent and \gls{iTasks}-\glspl{Task} can be
6 started to monitor the output. When everything is finished, the devices are
7 removed and the system is powered off.
8
9 \begin{lstlisting}[language=Clean,label={lst:framework},
10 caption={\gls{mTask} framework for building applications}]
11 w :: Task ()
12 w = makeDevice "dev1" (...) >>= connectDevice
13 >>= \dev1->makeDevice "dev2" (...) >>= connectDevice
14 >>= \dev2->...
15 ...
16 >>* [OnAction (Action "Shutdown") $ always
17 $ deleteDevice dev1
18 >>| deleteDevice dev2
19 >>| ...
20 >>| shutDown 0
21 ]
22 \end{lstlisting}
23
24 \subsection{Thermostat}
25 The thermostat is a classic example program for showing interactions between
26 peripherals. The following program shows a system containing two devices. One
27 device is connected via \gls{TCP} and contains a temperature sensor. The second
28
29 \subsection[Lifting mTasks to iTasks-Tasks]%
30 {Lifting \gls{mTask}-\glspl{Task} to \gls{iTasks}-\glspl{Task}}
31 If the user does not want to know where and when a \gls{mTask} is actually
32 executed and is just interested in the results it can lift the \gls{mTask} to
33 an \gls{iTasks}-\gls{Task}. The function is called with a name, \gls{mTask},
34 device and interval specification and it will return a \gls{Task} that finishes
35 if and only if the \gls{mTask} has returned.
36
37 \begin{lstlisting}[caption={Starting up the devices}]
38 liftmTask :: String (Main (ByteCode () Stmt)) (MTaskDevice, MTaskInterval) -> Task [MTaskShare]
39 liftmTask wta mTask c=:(dev, _)= sendTaskToDevice wta mTask c
40 >>= \(t, shs)->wait "Waiting for mTask to return" (taskRemoved t) (deviceShare dev)
41 >>| viewInformation "Done!" [] ()
42 >>| treturn shs
43 where
44 taskRemoved t d = isNothing $ find (\t1->t1.ident==t.ident) d.deviceTasks
45 \end{lstlisting}
46
47 The factorial example can then be lifted to a real \gls{iTasks}-\gls{mTask}
48 with the following code:
49 \begin{lstlisting}[caption={Lifting the factorial \gls{Task} to \gls{iTasks}}]
50 factorial :: MTaskDevice -> Task BCValue
51 factorial dev = enterInformation "Factorial of ?" []
52 >>= \fac->liftmTask "fact" (fact fac) (dev, OnInterval 100)
53 @ fromJust o find (\x->x.humanName == "result")
54 @ \s->s.MTaskShare.value
55 where
56 fact i = sds \y=i
57 In namedsds \x=(1 Named "result")
58 In {main = IF (y <=. lit 1)
59 ( pub x :. retrn )
60 ( x =. x *. y :. y =. y -. lit 1 )}
61 \end{lstlisting}