aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/concurrency/actor.lux
blob: e2842752bd7bcd3d5b8c29b734cad50ba0d3f2a7 (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
279
(;module: {#;doc "The actor model of concurrency."}
  lux
  (lux (control monad)
       [io #- run]
       (codata function)
       (data [error #- fail]
             text/format
             (coll [list "List/" Monoid<List> Monad<List>])
             [product]
             [number "Nat/" Codec<Text,Nat>])
       [compiler #+ with-gensyms]
       (macro [ast]
              ["s" syntax #+ syntax: Syntax]
              (syntax [common]))
       [type])
  (.. ["P" promise #+ Monad<Promise>]
      [stm #+ Monad<STM>]
      [frp]))

## [Types]
(type: #export (Actor s m)
  {#;doc "An actor, defined as all the necessities it requires."}
  {#mailbox (stm;Var m)
   #kill-signal (P;Promise Unit)
   #obituary (P;Promise [(Maybe Text) s (List m)])})

(type: #export (Behavior s m)
  {#;doc "An actor's behavior when messages are received."}
  {#step (-> (Actor s m) (-> m s (P;Promise (Error s))))
   #end (-> (Maybe Text) s (P;Promise Unit))})

## [Values]
(def: #export (spawn init behavior)
  {#;doc "Given a behavior and initial state, spawns an actor and returns it."}
  (All [s m] (-> s (Behavior s m) (IO (Actor s m))))
  (io (let [[step on-death] behavior
            mailbox (stm;var (:! ($ +1) []))
            kill-signal (P;promise Unit)
            obituary (P;promise [(Maybe Text) ($ +0) (List ($ +1))])
            self {#mailbox mailbox
                  #kill-signal kill-signal
                  #obituary obituary}
            mailbox-chan (io;run (stm;follow "\tmailbox\t" mailbox))
            step (step self)
            |mailbox| (stm;var mailbox-chan)
            _ (:: Monad<Promise> map
                  (lambda [_]
                    (io;run (do Monad<IO>
                              [mb (stm;read! |mailbox|)]
                              (frp;close mb))))
                  kill-signal)
            process (loop [state init
                           messages mailbox-chan]
                      (do Monad<Promise>
                        [?messages+ messages]
                        (case ?messages+
                          ## No kill-signal so far, so I may proceed...
                          (#;Some [message messages'])
                          (do Monad<Promise>
                            [#let [_ (io;run (stm;write! messages' |mailbox|))]
                             ?state' (step message state)]
                            (case ?state'
                              (#;Left error)
                              (do @
                                [#let [_ (io;run (P;resolve [] kill-signal))
                                       _ (io;run (frp;close messages'))
                                       death-message (#;Some error)]
                                 _ (on-death death-message state)
                                 remaining-messages (frp;consume messages')]
                                (wrap [death-message state (#;Cons message remaining-messages)]))

                              (#;Right state')
                              (recur state' messages')))

                          ## Otherwise, clean-up and return current state.
                          #;None
                          (do Monad<Promise>
                            [#let [_ (io;run (frp;close messages))
                                   death-message #;None]
                             _ (on-death death-message state)]
                            (wrap [death-message state (list)])))))]
        self)))

(def: #export poison
  {#;doc "Immediately kills the given actor (if it's not already dead)."}
  (All [s m] (-> (Actor s m) (io;IO Bool)))
  (|>. (get@ #kill-signal) (P;resolve [])))

(def: #export (alive? actor)
  (All [s m] (-> (Actor s m) Bool))
  (case [(P;poll (get@ #kill-signal actor))
         (P;poll (get@ #obituary actor))]
    [#;None #;None]
    true

    _
    false))

(def: #export (send message actor)
  {#;doc "Communicate with an actor through message passing."}
  (All [s m] (-> m (Actor s m) (P;Promise Bool)))
  (if (alive? actor)
    (exec (io;run (stm;write! message (get@ #mailbox actor)))
      (:: Monad<Promise> wrap true))
    (:: Monad<Promise> wrap false)))

(def: #export (keep-alive init behavior)
  {#;doc "Given initial-state and a behavior, spawns an actor that will reboot if it dies of errors.

          However, if it is killed, it won't re-spawn."}
  (All [s m] (-> s (Behavior s m) (IO (Actor s m))))
  (io (let [ka-actor (: (Actor (Actor ($ +0) ($ +1)) ($ +1))
                        (io;run (spawn (io;run (spawn init behavior))
                                       {#step (lambda [*self* message server]
                                                (do Monad<Promise>
                                                  [was-sent? (send message server)]
                                                  (if was-sent?
                                                    (wrap (#;Right server))
                                                    (do @
                                                      [[?cause state unprocessed-messages] (get@ #obituary server)]
                                                      (exec (log! (format "ACTOR DIED:" "\n"
                                                                          (default "" ?cause) "\n"
                                                                          "RESTARTING" "\n"))
                                                        (do @
                                                          [#let [new-server (io;run (spawn state behavior))
                                                                 mailbox (get@ #mailbox new-server)]
                                                           _ (P;future (mapM io;Monad<IO> ((flip stm;write!) mailbox) (#;Cons message unprocessed-messages)))]
                                                          (wrap (#;Right new-server))))
                                                      ))))
                                        #end (lambda [_ server] (exec (io;run (poison server))
                                                                  (:: Monad<Promise> wrap [])))})))]
        (update@ #obituary (: (-> (P;Promise [(Maybe Text) (Actor ($ +0) ($ +1)) (List ($ +1))])
                                  (P;Promise [(Maybe Text) ($ +0) (List ($ +1))]))
                              (lambda [process]
                                (do Monad<Promise>
                                  [[_ server unprocessed-messages-0] process
                                   [cause state unprocessed-messages-1] (get@ #obituary server)]
                                  (wrap [cause state (List/append unprocessed-messages-0 unprocessed-messages-1)]))))
                 ka-actor))))

## [Syntax]
(type: Method
  {#name Text
   #vars (List Text)
   #args (List [Text AST])
   #return AST
   #body AST})

(def: method^
  (Syntax Method)
  (s;form (do s;Monad<Syntax>
            [_ (s;this! (' method:))
             vars (s;default (list) (s;tuple (s;some s;local-symbol)))
             [name args] (s;form ($_ s;seq
                                     s;local-symbol
                                     (s;many common;typed-arg)
                                     ))
             return s;any
             body s;any]
            (wrap {#name name
                   #vars vars
                   #args args
                   #return return
                   #body body}))))

(def: stop^
  (Syntax AST)
  (s;form (do s;Monad<Syntax>
            [_ (s;this! (' stop:))]
            s;any)))

(def: actor-decl^
  (Syntax [(List Text) Text (List [Text AST])])
  (s;seq (s;default (list) (s;tuple (s;some s;local-symbol)))
         (s;either (s;form (s;seq s;local-symbol (s;many common;typed-arg)))
                   (s;seq s;local-symbol (:: s;Monad<Syntax> wrap (list))))))

(def: (actor-def-decl [_vars _name _args] return-type)
  (-> [(List Text) Text (List [Text AST])] AST (List AST))
  (let [decl (` ((~ (ast;symbol ["" (format _name "//new")])) (~@ (List/map (|>. product;left [""] ast;symbol) _args))))
        base-type (` (-> (~@ (List/map product;right _args))
                         (~ return-type)))
        type (case _vars
               #;Nil
               base-type

               _
               (` (All [(~@ (List/map (|>. [""] ast;symbol) _vars))]
                    (~ base-type))))]
    (list decl
          type)))

(syntax: #export (actor: [_ex-lev common;export-level]
                   [(^@ decl [_vars _name _args]) actor-decl^]
                   state-type
                   [methods (s;many method^)]
                   [?stop (s;opt stop^)])
  {#;doc (doc "Allows defining an actor, with a pice of state and a set of methods that can be called on it."
              "A method can access the actor's state through the *state* variable."
              "A method can also access the actor itself through the *self* variable."
              "A method may succeed or fail (in case of failure, the actor dies). This is handled through the Either type."
              "A method's output must be a promise containing a 2-tuple with the updated state and a return value."
              "All methods are run implicitly within the Promise monad."

              (actor: #export Adder
                Int
                
                (method: (count! [to-add Int])
                  [Int Int]
                  (if (i.>= 0 to-add)
                    (let [new-state (i.+ to-add *state*)]
                      (wrap (#;Right [new-state [*state* new-state]])))
                    (wrap (#;Left "Can't add negative numbers!"))))
                ))}
  (with-gensyms [g!message g!error g!return g!error g!output]
    (let [g!state-name (ast;symbol ["" (format _name "//STATE")])
          g!protocol-name (ast;symbol ["" (format _name "//PROTOCOL")])
          g!self (ast;symbol ["" "*self*"])
          g!state (ast;symbol ["" "*state*"])
          g!cause (ast;symbol ["" "*cause*"])
          g!stop-body (default (` (:: P;Monad<Promise> (~' wrap) [])) ?stop)
          protocol (List/map (lambda [(^slots [#name #vars #args #return #body])]
                               (` ((~ (ast;tag ["" name])) [(~@ (List/map product;right args))] (P;Promise (~ return)))))
                             methods)
          protocol-pm (List/map (: (-> Method [AST AST])
                                   (lambda [(^slots [#name #vars #args #return #body])]
                                     (let [arg-names (|> (list;size args) (list;n.range +1) (List/map (|>. Nat/encode [""] ast;symbol)))
                                           body-func (` (: (-> (~ g!state-name) (~@ (List/map product;right args)) (P;Promise (Error [(~ g!state-name) (~ return)])))
                                                           (lambda (~ (ast;symbol ["" _name])) [(~ g!state) (~@ (List/map (|>. product;left [""] ast;symbol) args))]
                                                             (do P;Monad<Promise>
                                                               []
                                                               (~ body)))))]
                                       [(` [[(~@ arg-names)] (~ g!return)])
                                        (` (do P;Monad<Promise>
                                             [(~ g!output) ((~ body-func) (~ g!state) (~@ arg-names))]
                                             (case (~ g!output)
                                               (#;Right [(~ g!state) (~ g!output)])
                                               (exec (io;run (P;resolve (~ g!output) (~ g!return)))
                                                 ((~' wrap) (#;Right (~ g!state))))

                                               (#;Left (~ g!error))
                                               ((~' wrap) (#;Left (~ g!error))))
                                             ))])))
                                methods)
          g!behavior (` {#step (lambda [(~ g!self) (~ g!message) (~ g!state)]
                                 (case (~ g!message)
                                   (~@ (if (n.= +1 (list;size protocol-pm))
                                         (List/join (List/map (lambda [[pattern clause]]
                                                                (list pattern clause))
                                                              protocol-pm))
                                         (List/join (List/map (lambda [[method [pattern clause]]]
                                                                (list (` ((~ (ast;tag ["" (get@ #name method)])) (~ pattern)))
                                                                      clause))
                                                              (list;zip2 methods protocol-pm)))))
                                   ))
                         #end (lambda [(~ g!cause) (~ g!state)]
                                (do P;Monad<Promise>
                                  []
                                  (~ g!stop-body)))})
          g!actor-name (ast;symbol ["" _name])
          g!methods (List/map (: (-> Method AST)
                                 (lambda [(^slots [#name #vars #args #return #body])]
                                   (let [arg-names (|> (list;size args) (list;n.range +1) (List/map (|>. Nat/encode [""] ast;symbol)))
                                         type (` (-> (~@ (List/map product;right args))
                                                     (~ g!actor-name)
                                                     (P;Promise (~ return))))]
                                     (` (def: (~@ (common;gen-export-level _ex-lev)) ((~ (ast;symbol ["" name])) (~@ arg-names) (~ g!self))
                                          (~ type)
                                          (let [(~ g!output) (P;promise (~ return))]
                                            (exec (send ((~ (ast;tag ["" name])) [[(~@ arg-names)] (~ g!output)]) (~ g!self))
                                              (~ g!output))))))))
                              methods)]
      (wrap (list& (` (type: (~@ (common;gen-export-level _ex-lev)) (~ g!state-name) (~ state-type)))
                   (` (type: (~@ (common;gen-export-level _ex-lev)) (~ g!protocol-name) (~@ protocol)))
                   (` (type: (~@ (common;gen-export-level _ex-lev)) (~ g!actor-name) (Actor (~ g!state-name) (~ g!protocol-name))))
                   (` (def: (~@ (common;gen-export-level _ex-lev)) (~@ (actor-def-decl decl (` (Behavior (~ g!state-name) (~ g!protocol-name)))))
                        (~ g!behavior)))
                   g!methods))
      )))