aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/state.lux
blob: d1d58786107cbab4966dae091511a998425c47aa (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
(.using
 [library
  [lux {"-" local}
   [abstract
    [functor {"+" Functor}]
    [apply {"+" Apply}]
    [monad {"+" Monad do}]]]])

(type: .public (State s a)
  (-> s [s a]))

(def: .public get
  (All (_ s) (State s s))
  (function (_ state)
    [state state]))

(def: .public (put new_state)
  (All (_ s) (-> s (State s Any)))
  (function (_ state)
    [new_state []]))

(def: .public (update change)
  (All (_ s) (-> (-> s s) (State s Any)))
  (function (_ state)
    [(change state) []]))

(def: .public (use user)
  (All (_ s a) (-> (-> s a) (State s a)))
  (function (_ state)
    [state (user state)]))

(def: .public (local change action)
  (All (_ s a) (-> (-> s s) (State s a) (State s a)))
  (function (_ state)
    (let [[state' output] (action (change state))]
      [state output])))

(def: .public (result state action)
  (All (_ s a) (-> s (State s a) [s a]))
  (action state))

(implementation: .public functor
  (All (_ s) (Functor (State s)))
  
  (def: (each f ma)
    (function (_ state)
      (let [[state' a] (ma state)]
        [state' (f a)]))))

(implementation: .public apply
  (All (_ s) (Apply (State s)))
  
  (def: functor ..functor)

  (def: (on fa ff)
    (function (_ state)
      (let [[state' f] (ff state)
            [state'' a] (fa state')]
        [state'' (f a)]))))

(implementation: .public monad
  (All (_ s) (Monad (State s)))
  
  (def: functor ..functor)

  (def: (in a)
    (function (_ state)
      [state a]))

  (def: (conjoint mma)
    (function (_ state)
      (let [[state' ma] (mma state)]
        (ma state')))))

(def: .public (while condition body)
  (All (_ s) (-> (State s Bit) (State s Any) (State s Any)))
  (do [! ..monad]
    [execute? condition]
    (if execute?
      (do !
        [_ body]
        (while condition body))
      (in []))))

(def: .public (do_while condition body)
  (All (_ s) (-> (State s Bit) (State s Any) (State s Any)))
  (do ..monad
    [_ body]
    (while condition body)))

(implementation: (with//functor functor)
  (All (_ M s) (-> (Functor M) (Functor (All (_ a) (-> s (M [s a]))))))
  
  (def: (each f sfa)
    (function (_ state)
      (# functor each (function (_ [s a]) [s (f a)])
         (sfa state)))))

(implementation: (with//apply monad)
  (All (_ M s) (-> (Monad M) (Apply (All (_ a) (-> s (M [s a]))))))
  
  (def: functor (with//functor (# monad functor)))

  (def: (on sFa sFf)
    (function (_ state)
      (do monad
        [[state f] (sFf state)
         [state a] (sFa state)]
        (in [state (f a)])))))

(type: .public (+State M s a)
  (-> s (M [s a])))

(def: .public (result' state action)
  (All (_ M s a) (-> s (+State M s a) (M [s a])))
  (action state))

(implementation: .public (with monad)
  (All (_ M s) (-> (Monad M) (Monad (+State M s))))

  (def: functor (with//functor (# monad functor)))

  (def: (in a)
    (function (_ state)
      (# monad in [state a])))
  
  (def: (conjoint sMsMa)
    (function (_ state)
      (do monad
        [[state' sMa] (sMsMa state)]
        (sMa state')))))

(def: .public (lifted monad ma)
  (All (_ M s a) (-> (Monad M) (M a) (+State M s a)))
  (function (_ state)
    (do monad
      [a ma]
      (in [state a]))))