-
Notifications
You must be signed in to change notification settings - Fork 1
/
system.idr
67 lines (49 loc) · 1.77 KB
/
system.idr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module system
import Control.Monad.Identity
import Prelude.Stream
import lens
import monadalgebra
%access public export
System : (m : Type -> Type)
-> (s : Type)
-> (i : Interface)
-> Type
System m s i = Lens m (SimpleInterface s s) i
MkSystem : (readout : state -> output i)
-> (update : (s : state) -> input i (readout s) -> m state)
-> System m state i
MkSystem = MkLens
interfaceOf : System m s i -> Interface
interfaceOf _ = i
readout : (sys : System m s i)
-> (s -> output i)
readout = forward
update : (sys : System m s i)
-> ((state : s) -> input i (readout sys state) -> m s)
update = back
SimpleSystem : (m : Type -> Type)
-> (s : Type)
-> (o : Type)
-> (i : Type)
-> Type
SimpleSystem m s o i = System m s (SimpleInterface o i)
PureSystem : (s : Type) -> (i : Interface) -> Type
PureSystem = System Identity
PureSimpleSystem : (s : Type) -> (o : Type) -> (i : Type) -> Type
PureSimpleSystem = SimpleSystem Identity
runPureSystem : {i : Interface}
-> (sys : PureSystem s i)
-> (start : s)
-> Trajectory i
runPureSystem {i} sys start = current :: rest
where
current : output i
current = readout sys start
rest : input i (readout sys start) -> Trajectory i
rest nextinput = runPureSystem sys (eval $ update sys start nextinput)
boxUp : (Monad m) => (f : b -> m a) -> System m a (SimpleInterface a b)
boxUp f = MkSystem id (\_ => f)
OneStepDelay : (Monad m) => (s : Type) -> System m s (SimpleInterface s s)
OneStepDelay s = MkSystem id (\_ => pure)
(<+>) : (Monad m) => System m s i -> System m t j -> System m (s, t) (pair i j)
(<+>) = (<+>)