aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/concurrency/actor.lux
blob: 7184a105e9074e05ef50efd570f9edd8dd1d60dc (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" pipe]
    ["[0]" try (.only Try)]
    ["[0]" exception (.only exception:)]
    ["[0]" io (.only IO io)]]
   [data
    ["[0]" bit]
    ["[0]" product]]
   [macro
    ["[0]" local]]
   [type (.only sharing)
    [primitive (.only primitive representation abstraction)]]]]
 [//
  ["[0]" atom (.only Atom atom)]
  ["[0]" async (.only Async Resolver)]
  ["[0]" frp (.only Channel Channel')]])

(exception: .public poisoned)
(exception: .public dead)

(local.let [<Mail> (template (_ Actor s)
                     [(-> s (Actor s) (Async (Try s)))])
            <Obituary> (template (_ Actor s)
                         [[Text s (List (<Mail> Actor s))]])
            <Mailbox> (template (_ Actor s)
                        [(Rec Mailbox
                           [(Async [(<Mail> Actor s) Mailbox])
                            (Resolver [(<Mail> Actor s) Mailbox])])])]
  (these (def (pending [read write])
           (All (_ a)
             (-> (Rec Mailbox
                   [(Async [a Mailbox])
                    (Resolver [a Mailbox])])
                 (IO (List a))))
           (do [! io.monad]
             [current (async.value read)]
             (case current
               {.#Some [head tail]}
               (at ! each (|>> {.#Item head})
                   (pending tail))
               
               {.#None}
               (in {.#End}))))
         
         (primitive .public (Actor s)
           (Record
            [#obituary [(Async (<Obituary> Actor s))
                        (Resolver (<Obituary> Actor s))]
             #mailbox (Atom (<Mailbox> Actor s))])

           (type: .public (Mail s)
             (<Mail> Actor s))

           (type: .public (Obituary s)
             (<Obituary> Actor s))

           (type: .public (Behavior s)
             (-> (Mail s) s (Actor s) (Async (Try s))))

           (def .public (spawn! behavior init)
             (All (_ s) (-> (Behavior s) s (IO (Actor s))))
             (io (let [self (sharing [s]
                              (is (Behavior s)
                                  behavior)
                              (is (Actor s)
                                  (abstraction [#obituary (async.async [])
                                                #mailbox (atom (async.async []))])))
                       process (loop (again [state init
                                             [|mailbox| _] (io.run! (atom.read! (the #mailbox (representation self))))])
                                 (do [! async.monad]
                                   [[head tail] |mailbox|
                                    ?state' (behavior head state self)]
                                   (case ?state'
                                     {try.#Failure error}
                                     (let [[_ resolve] (the #obituary (representation self))]
                                       (exec (io.run!
                                              (do io.monad
                                                [pending (..pending tail)]
                                                (resolve [error state {.#Item head pending}])))
                                         (in [])))

                                     {try.#Success state'}
                                     (again state' tail))))]
                   self)))

           (def .public (alive? actor)
             (All (_ s) (-> (Actor s) (IO Bit)))
             (let [[obituary _] (the #obituary (representation actor))]
               (|> obituary
                   async.value
                   (at io.functor each
                       (|>> (pipe.case
                              {.#None}
                              bit.yes

                              _
                              bit.no))))))

           (def .public (obituary' actor)
             (All (_ s) (-> (Actor s) (IO (Maybe (Obituary s)))))
             (let [[obituary _] (the #obituary (representation actor))]
               (async.value obituary)))

           (def .public obituary
             (All (_ s) (-> (Actor s) (Async (Obituary s))))
             (|>> representation
                  (the #obituary)
                  product.left))

           (def .public (mail! mail actor)
             (All (_ s) (-> (Mail s) (Actor s) (IO (Try Any))))
             (do [! io.monad]
               [alive? (..alive? actor)]
               (if alive?
                 (let [entry [mail (async.async [])]]
                   (do !
                     [|mailbox|&resolve (atom.read! (the #mailbox (representation actor)))]
                     (loop (again [[|mailbox| resolve] |mailbox|&resolve])
                       (do !
                         [|mailbox| (async.value |mailbox|)]
                         (case |mailbox|
                           {.#None}
                           (do !
                             [resolved? (resolve entry)]
                             (if resolved?
                               (do !
                                 [_ (atom.write! (product.right entry) (the #mailbox (representation actor)))]
                                 (in {try.#Success []}))
                               (again |mailbox|&resolve)))
                           
                           {.#Some [_ |mailbox|']}
                           (again |mailbox|'))))))
                 (in (exception.except ..dead [])))))

           (type: .public (Message s o)
             (-> s (Actor s) (Async (Try [s o]))))

           (def (mail message)
             (All (_ s o) (-> (Message s o) [(Async (Try o)) (Mail s)]))
             (let [[async resolve] (sharing [s o]
                                     (is (Message s o)
                                         message)
                                     (is [(Async (Try o))
                                          (Resolver (Try o))]
                                         (async.async [])))]
               [async
                (function (_ state self)
                  (do [! async.monad]
                    [outcome (message state self)]
                    (case outcome
                      {try.#Success [state' return]}
                      (exec
                        (io.run! (resolve {try.#Success return}))
                        (async.resolved {try.#Success state'}))
                      
                      {try.#Failure error}
                      (exec
                        (io.run! (resolve {try.#Failure error}))
                        (async.resolved {try.#Failure error})))))]))

           (def .public (tell! message actor)
             (All (_ s o) (-> (Message s o) (Actor s) (Async (Try o))))
             (let [[async mail] (..mail message)]
               (do async.monad
                 [outcome (async.future (..mail! mail actor))]
                 (case outcome
                   {try.#Success}
                   async
                   
                   {try.#Failure error}
                   (in {try.#Failure error})))))
           )))

(def .public default
  Behavior
  (function (_ mail state self)
    (mail state self)))

(def .public (poison! actor)
  (All (_ s) (-> (Actor s) (IO (Try Any))))
  (..mail! (function (_ state self)
             (async.resolved (exception.except ..poisoned [])))
           actor))

(type: .public Stop
  (IO Any))

(def continue! true)
(def stop! false)

(def .public (observe! action channel actor)
  (All (_ r w s) (-> (-> r Stop (Mail s)) (Channel' r w) (Actor s) (IO Any)))
  (let [signal (is (Atom Bit)
                   (atom.atom ..continue!))
        stop (is Stop
                 (atom.write! ..stop! signal))]
    (frp.subscribe! (function (_ event)
                      (do [! io.monad]
                        [continue? (atom.read! signal)]
                        (if continue?
                          (|> actor
                              (..mail! (action event stop))
                              (at ! each try.maybe))
                          (in {.#None}))))
                    channel)))