aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/control/concurrency/frp.lux
blob: 5e5178e4b99ed338e2390ff7961f180b7dba5206 (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
277
278
(.module:
  [lux (#- Source)
   [control
    [functor (#+ Functor)]
    [apply (#+ Apply)]
    ["." monad (#+ do Monad)]
    [predicate (#+ Predicate)]
    [equivalence (#+ Equivalence)]]
   ["." io (#+ IO)]
   [data
    [maybe ("maybe/." Functor<Maybe>)]
    [collection
     [list ("list/." Monoid<List>)]]]
   [type (#+ :share)
    abstract]]
  [//
   ["." atom (#+ Atom)]
   ["." promise (#+ Promise) ("promise/." Functor<Promise>)]])

(type: #export (Channel a)
  {#.doc "An asynchronous channel to distribute values."}
  (Promise (Maybe [a (Channel a)])))

(signature: (Resolver a)
  (: (IO Bit)
     close)
  (: (-> a (IO (Maybe (Resolver a))))
     feed))

(def: (resolver resolve)
  (All [a]
    (-> (promise.Resolver (Maybe [a (Channel a)]))
        (Resolver a)))
  (structure
   (def: close
     (resolve #.None))
   
   (def: (feed value)
     (do io.Monad<IO>
       [#let [[next resolve-next] (:share [a]
                                          {(promise.Resolver (Maybe [a (Channel a)]))
                                           resolve}
                                          {[(Promise (Maybe [a (Channel a)]))
                                            (promise.Resolver (Maybe [a (Channel a)]))]
                                           (promise.promise [])})]
        resolved? (resolve (#.Some [value next]))]
       (if resolved?
         (wrap (#.Some (resolver resolve-next)))
         (wrap #.None))))))

(abstract: #export (Source a)
  {}

  (Atom (Resolver a))

  (def: #export (close source)
    (All [a] (-> (Source a) (IO Bit)))
    (do io.Monad<IO>
      [resolver (atom.read (:representation source))
       closed? (:: resolver close)]
      (if closed?
        ## I closed the source.
        (wrap true)
        ## Someone else interacted with the source.
        (do @
          [resolver' (atom.read (:representation source))]
          (if (is? resolver resolver')
            ## Someone else closed the source.
            (wrap true)
            ## Someone else fed the source while I was closing it.
            (close source))))))

  (def: #export (feed source value)
    (All [a] (-> (Source a) a (IO Bit)))
    (loop [source (:representation source)]
      (do io.Monad<IO>
        [current (atom.read source)
         ?next (:: current feed value)
         fed? (case ?next
                (#.Some next)
                (atom.compare-and-swap current next source)

                #.None
                (wrap false))]
        (if fed?
          ## I fed the source.
          (wrap true)
          ## Someone else interacted with the source.
          (do @
            [latter (atom.read source)]
            (if (is? current latter)
              ## Someone else closed the source while I was feeding it.
              (wrap false)
              ## Someone else fed the source.
              (recur source)))))))

  (def: source
    (All [a] (-> (Resolver a) (Source a)))
    (|>> atom.atom :abstraction))
  )

(def: #export (channel _)
  (All [a] (-> Any [(Channel a) (Source a)]))
  (let [[promise resolve] (promise.promise [])]
    [promise (..source (..resolver resolve))]))

(def: #export (listen listener channel)
  (All [a] (-> (-> a (IO Any)) (Channel a) (IO Any)))
  (io.io (exec (: (Promise Any)
                  (loop [channel channel]
                    (do promise.Monad<Promise>
                      [cons channel]
                      (case cons
                        (#.Some [head tail])
                        (exec (io.run (listener head))
                          (recur tail))
                        
                        #.None
                        (wrap [])))))
           [])))

(structure: #export _ (Functor Channel)
  (def: (map f)
    (promise/map
     (maybe/map
      (function (_ [head tail])
        [(f head) (map f tail)])))))

(structure: #export _ (Apply Channel)
  (def: functor Functor<Channel>)

  (def: (apply ff fa)
    (do promise.Monad<Promise>
      [cons-f ff
       cons-a fa]
      (case [cons-f cons-a]
        [(#.Some [head-f tail-f]) (#.Some [head-a tail-a])]
        (wrap (#.Some [(head-f head-a) (apply tail-f tail-a)]))

        _
        (wrap #.None)))))

(structure: #export _ (Monad Channel)
  (def: functor Functor<Channel>)

  (def: (wrap a)
    (promise.resolved (#.Some [a (promise.resolved #.None)])))

  (def: (join mma)
    (let [[output source] (channel [])]
      (exec (io.run (..listen (..listen (..feed source))
                              mma))
        output))))

(def: #export (filter pass? channel)
  (All [a] (-> (Predicate a) (Channel a) (Channel a)))
  (do promise.Monad<Promise>
    [cons channel]
    (case cons
      (#.Some [head tail])
      (let [tail' (filter pass? tail)]
        (if (pass? head)
          (wrap (#.Some [head tail']))
          tail'))
      
      #.None
      (wrap #.None))))

(def: #export (from-promise promise)
  (All [a] (-> (Promise a) (Channel a)))
  (promise/map (function (_ value)
                 (#.Some [value (promise.resolved #.None)]))
               promise))

(def: #export (fold f init channel)
  {#.doc "Asynchronous fold over channels."}
  (All [a b]
    (-> (-> b a (Promise a)) a (Channel b)
        (Promise a)))
  (do promise.Monad<Promise>
    [cons channel]
    (case cons
      #.None
      (wrap init)
      
      (#.Some [head tail])
      (do @
        [init' (f head init)]
        (fold f init' tail)))))

(def: #export (folds f init channel)
  {#.doc "A channel of folds."}
  (All [a b]
    (-> (-> b a (Promise a)) a (Channel b)
        (Channel a)))
  (do promise.Monad<Promise>
    [cons channel]
    (case cons
      #.None
      (wrap (#.Some [init (wrap #.None)]))
      
      (#.Some [head tail])
      (do @
        [init' (f head init)]
        (folds f init' tail)))))

(def: #export (poll milli-seconds action)
  (All [a] (-> Nat (IO a) (Channel a)))
  (let [[output source] (channel [])]
    (exec (io.run (loop [_ []]
                    (do io.Monad<IO>
                      [value action
                       _ (..feed source value)]
                      (promise.await recur (promise.wait milli-seconds)))))
      output)))

(def: #export (periodic milli-seconds)
  (-> Nat (Channel Any))
  (poll milli-seconds (io.io [])))

(def: #export (iterate f init)
  (All [a] (-> (-> a (Promise (Maybe a))) a (Channel a)))
  (do promise.Monad<Promise>
    [?next (f init)]
    (case ?next
      (#.Some next)
      (wrap (#.Some [init (iterate f next)]))
      
      #.None
      (wrap (#.Some [init (wrap #.None)])))))

(def: (distinct' equivalence previous channel)
  (All [a] (-> (Equivalence a) a (Channel a) (Channel a)))
  (do promise.Monad<Promise>
    [cons channel]
    (case cons
      (#.Some [head tail])
      (if (:: equivalence = previous head)
        (distinct' equivalence previous tail)
        (wrap (#.Some [head (distinct' equivalence head tail)])))
      
      #.None
      (wrap #.None))))

(def: #export (distinct equivalence channel)
  (All [a] (-> (Equivalence a) (Channel a) (Channel a)))
  (do promise.Monad<Promise>
    [cons channel]
    (case cons
      (#.Some [head tail])
      (wrap (#.Some [head (distinct' equivalence head tail)]))
      
      #.None
      (wrap #.None))))

(def: #export (consume channel)
  {#.doc "Reads the entirety of a channel's content and returns it as a list."}
  (All [a] (-> (Channel a) (Promise (List a))))
  (do promise.Monad<Promise>
    [cons channel]
    (case cons
      (#.Some [head tail])
      (:: @ map (|>> (#.Cons head))
          (consume tail))

      #.None
      (wrap #.Nil))))

(def: #export (sequential milli-seconds values)
  (All [a] (-> Nat (List a) (Channel a)))
  (case values
    #.Nil
    (promise.resolved #.None)

    (#.Cons head tail)
    (promise.resolved (#.Some [head (do promise.Monad<Promise>
                                      [_ (promise.wait milli-seconds)]
                                      (sequential milli-seconds tail))]))))