blob: 066384e11c354536bb355ca61419b417cb02179b (
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
(;module:
lux
(lux (control functor
applicative
monad)
[io #- run]
(data (coll [list #* "List/" Functor<List> Fold<List>]
[dict #+ Dict]
["Q" queue])
[product]
[text]
maybe
[number "Nat/" Codec<Text,Nat>]
text/format)
[macro]
(macro [code]
["s" syntax #+ syntax: Syntax])
(concurrency [atom #+ Atom atom]
["P" promise #+ Promise "Promise/" Monad<Promise>]
[frp])
))
(type: (Var-State a)
{#value a
#observers (Dict Text (-> a (IO Unit)))})
(type: #export (Var a)
{#;doc "A mutable cell containing a value, and observers that will be alerted of any change to it."}
(Atom (Var-State a)))
(type: (Tx-Frame a)
{#var (Var a)
#original a
#current a})
(type: Tx
(List (Ex [a] (Tx-Frame a))))
(type: #export (STM a)
{#;doc "A computation which updates a transaction and produces a value."}
(-> Tx [Tx a]))
(def: #export (var value)
{#;doc "Creates a new STM var, with a default value."}
(All [a] (-> a (Var a)))
(atom;atom {#value value
#observers (dict;new text;Hash<Text>)}))
(def: raw-read
(All [a] (-> (Var a) a))
(|>. atom;get io;run (get@ #value)))
(def: (find-var-value var tx)
(All [a] (-> (Var a) Tx (Maybe a)))
(|> tx
(find (function [[_var _original _current]]
(is (:! (Var Unit) var)
(:! (Var Unit) _var))))
(:: Monad<Maybe> map (function [[_var _original _current]]
_current))
(:! (Maybe ($ +0)))))
(def: #export (read var)
(All [a] (-> (Var a) (STM a)))
(function [tx]
(case (find-var-value var tx)
(#;Some value)
[tx value]
#;None
(let [value (raw-read var)]
[(#;Cons [var value value] tx)
value]))))
(def: #export (read! var)
{#;doc "Reads var immediately, without going through a transaction."}
(All [a] (-> (Var a) (IO a)))
(|> var
atom;get
(:: Functor<IO> map (get@ #value))))
(def: (update-tx-value var value tx)
(All [a] (-> (Var a) a Tx Tx))
(case tx
#;Nil
#;Nil
(#;Cons [_var _original _current] tx')
(if (is (:! (Var ($ +0)) var)
(:! (Var ($ +0)) _var))
(#;Cons [(:! (Var ($ +0)) _var)
(:! ($ +0) _original)
(:! ($ +0) value)]
tx')
(#;Cons [_var _original _current]
(update-tx-value var value tx')))
))
(def: #export (write value var)
(All [a] (-> a (Var a) (STM Unit)))
(function [tx]
(case (find-var-value var tx)
(#;Some _)
[(update-tx-value var value tx)
[]]
#;None
[(#;Cons [var (raw-read var) value] tx)
[]])))
(def: #export (write! new-value var)
{#;doc "Writes value to var immediately, without going through a transaction."}
(All [a] (-> a (Var a) (IO Unit)))
(do Monad<IO>
[old (atom;get var)
#let [old-value (get@ #value old)
new (set@ #value new-value old)]
succeeded? (atom;compare-and-swap old new var)]
(if succeeded?
(do @
[_ (|> old
(get@ #observers)
dict;values
(mapM @ (function [f] (f new-value))))]
(wrap []))
(write! new-value var))))
(def: #export (follow target)
{#;doc "Creates a channel that will receive all changes to the value of the given var."}
(All [a] (-> (Var a) (IO (frp;Chan a))))
(let [head (frp;chan ($ +0))
chan-var (var head)
observer (function [label value]
(case (io;run (|> chan-var raw-read (frp;write value)))
#;None
## By closing the output Chan, the
## observer becomes obsolete.
(atom;update (function [[value observers]]
[value (dict;remove label observers)])
target)
(#;Some tail')
(write! tail' chan-var)))]
(do Monad<IO>
[_ (atom;update (function [[value observers]]
(let [label (Nat/encode (List/fold (function [key base]
(case (Nat/decode key)
(#;Left _)
base
(#;Right key-num)
(n.max key-num base)))
+0
(dict;keys observers)))]
[value (dict;put label (observer label) observers)]))
target)]
(wrap head))))
(struct: #export _ (Functor STM)
(def: (map f fa)
(function [tx]
(let [[tx' a] (fa tx)]
[tx' (f a)]))))
(struct: #export _ (Applicative STM)
(def: functor Functor<STM>)
(def: (wrap a)
(function [tx] [tx a]))
(def: (apply ff fa)
(function [tx]
(let [[tx' f] (ff tx)
[tx'' a] (fa tx')]
[tx'' (f a)]))))
(struct: #export _ (Monad STM)
(def: applicative Applicative<STM>)
(def: (join mma)
(function [tx]
(let [[tx' ma] (mma tx)]
(ma tx')))))
(def: #export (update! f var)
{#;doc "Will update a Var's value, and return a tuple with the old and the new values."}
(All [a] (-> (-> a a) (Var a) (IO [a a])))
(io (loop [_ []]
(let [(^@ state [value observers]) (io;run (atom;get var))
value' (f value)]
(if (io;run (atom;compare-and-swap state
[value' observers]
var))
[value value']
(recur []))))))
(def: #export (update f var)
{#;doc "Will update a Var's value, and return a tuple with the old and the new values."}
(All [a] (-> (-> a a) (Var a) (STM [a a])))
(do Monad<STM>
[a (read var)
#let [a' (f a)]
_ (write a' var)]
(wrap [a a'])))
(def: (can-commit? tx)
(-> Tx Bool)
(every? (function [[_var _original _current]]
(is _original (raw-read _var)))
tx))
(def: (commit-var [_var _original _current])
(-> (Ex [a] (Tx-Frame a)) Unit)
(if (is _original _current)
[]
(io;run (write! _current _var))))
(def: fresh-tx Tx (list))
(def: pending-commits
(Var (Ex [a] [(STM a) (Promise a)]))
(var (:!! [])))
(def: commit-processor-flag
(Atom Bool)
(atom false))
(def: (process-commit commits)
(-> (frp;Chan [(STM Unit) (Promise Unit)])
(Promise Unit))
(do P;Monad<Promise>
[?head+tail commits]
(case ?head+tail
(#;Cons [stm-proc output] tail)
(do @
[#let [[finished-tx value] (stm-proc fresh-tx)]]
(exec (if (can-commit? finished-tx)
(exec (List/map commit-var finished-tx)
(io;run (P;resolve value output))
[])
(exec (io;run (write! [stm-proc output] pending-commits))
[]))
(process-commit tail)))
#;Nil
(undefined)
)))
(def: init-processor!
(IO Unit)
(do Monad<IO>
[flag (atom;get commit-processor-flag)]
(if flag
(wrap [])
(do @
[was-first? (atom;compare-and-swap flag true commit-processor-flag)]
(if was-first?
(do Monad<IO>
[inputs (follow pending-commits)]
(exec (process-commit (:! (frp;Chan [(STM Unit) (Promise Unit)])
inputs))
(wrap [])))
(wrap [])))
)))
(def: #export (commit stm-proc)
{#;doc "Commits a transaction and returns its result (asynchronously).
Note that a transaction may be re-run an indeterminate number of times if other transactions involving the same variables successfully commit first.
For this reason, it's important to note that transactions must be free from side-effects, such as I/O."}
(All [a] (-> (STM a) (Promise a)))
(let [output (P;promise ($ +0))]
(exec (io;run init-processor!)
(io;run (write! [stm-proc output] pending-commits))
output)))
|