aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/control/concurrency/promise.lux
blob: 6f8a35f9672aca7fa2225dd4e9d6bdfee7801ae3 (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
(.module:
  [lux (#- and or)
   [abstract
    [functor (#+ Functor)]
    [apply (#+ Apply)]
    ["." monad (#+ Monad do)]]
   [control
    [pipe (#+ case>)]
    ["." function]
    ["." io (#+ IO io)]]
   [data
    ["." product]]
   [type
    abstract]]
  [//
   ["." thread]
   ["." atom (#+ Atom atom)]])

(abstract: #export (Promise a)
  (Atom [(Maybe a) (List (-> a (IO Any)))])

  {#.doc "Represents values produced by asynchronous computations (unlike IO, which is synchronous)."}

  (type: #export (Resolver a)
    (-> a (IO Bit)))

  (def: (resolver promise)
    {#.doc "Sets an promise's value if it has not been done yet."}
    (All [a] (-> (Promise a) (Resolver a)))
    (function (resolve value)
      (let [promise (:representation promise)]
        (do {! io.monad}
          [(^@ old [_value _observers]) (atom.read promise)]
          (case _value
            (#.Some _)
            (wrap #0)

            #.None
            (do !
              [#let [new [(#.Some value) #.None]]
               succeeded? (atom.compare_and_swap old new promise)]
              (if succeeded?
                (do !
                  [_ (monad.map ! (function (_ f) (f value))
                                _observers)]
                  (wrap #1))
                (resolve value))))))))

  (def: #export (resolved value)
    (All [a] (-> a (Promise a)))
    (:abstraction (atom [(#.Some value) (list)])))

  (def: #export (promise _)
    (All [a] (-> Any [(Promise a) (Resolver a)]))
    (let [promise (:abstraction (atom [#.None (list)]))]
      [promise (..resolver promise)]))

  (def: #export poll
    {#.doc "Polls a promise's value."}
    (All [a] (-> (Promise a) (IO (Maybe a))))
    (|>> :representation
         atom.read
         (\ io.functor map product.left)))

  (def: #export (await f promise)
    (All [a] (-> (-> a (IO Any)) (Promise a) (IO Any)))
    (let [promise (:representation promise)
          (^@ old [_value _observers]) (io.run (atom.read promise))]
      (case _value
        (#.Some value)
        (f value)

        #.None
        (let [new [_value (#.Cons f _observers)]]
          (if (io.run (atom.compare_and_swap old new promise))
            (io.io [])
            (await f (:abstraction promise)))))))
  )

(def: #export resolved?
  {#.doc "Checks whether a promise's value has already been resolved."}
  (All [a] (-> (Promise a) (IO Bit)))
  (|>> ..poll
       (\ io.functor map
          (|>> (case> #.None
                      #0

                      (#.Some _)
                      #1)))))

(structure: #export functor
  (Functor Promise)
  
  (def: (map f fa)
    (let [[fb resolve] (..promise [])]
      (exec (io.run (..await (|>> f resolve) fa))
        fb))))

(structure: #export apply
  (Apply Promise)
  
  (def: &functor ..functor)

  (def: (apply ff fa)
    (let [[fb resolve] (..promise [])]
      (exec (io.run (..await (function (_ f)
                               (..await (|>> f resolve) fa))
                             ff))
        fb))))

(structure: #export monad
  (Monad Promise)
  
  (def: &functor ..functor)

  (def: wrap ..resolved)

  (def: (join mma)
    (let [[ma resolve] (promise [])]
      (exec (io.run (..await (..await resolve) mma))
        ma))))

(def: #export (and left right)
  {#.doc "Sequencing combinator."}
  (All [a b] (-> (Promise a) (Promise b) (Promise [a b])))
  (do ..monad
    [a left
     b right]
    (wrap [a b])))

(def: #export (or left right)
  {#.doc "Heterogeneous alternative combinator."}
  (All [a b] (-> (Promise a) (Promise b) (Promise (| a b))))
  (let [[a|b resolve] (..promise [])]
    (with_expansions
      [<sides> (template [<promise> <tag>]
                 [(io.run (await (|>> <tag> resolve) <promise>))]

                 [left  #.Left]
                 [right #.Right]
                 )]
      (exec <sides>
        a|b))))

(def: #export (either left right)
  {#.doc "Homogeneous alternative combinator."}
  (All [a] (-> (Promise a) (Promise a) (Promise a)))
  (let [[left||right resolve] (..promise [])]
    (`` (exec (~~ (template [<promise>]
                    [(io.run (await resolve <promise>))]

                    [left]
                    [right]))
          left||right))))

(def: #export (schedule millis_delay computation)
  {#.doc (doc "Runs an I/O computation on its own thread (after a specified delay)."
              "Returns a Promise that will eventually host its result.")}
  (All [a] (-> Nat (IO a) (Promise a)))
  (let [[!out resolve] (..promise [])]
    (exec (|> (do io.monad
                [value computation]
                (resolve value))
              (thread.schedule millis_delay)
              io.run)
      !out)))

(def: #export future
  {#.doc (doc "Runs an I/O computation on its own thread."
              "Returns a Promise that will eventually host its result.")}
  (All [a] (-> (IO a) (Promise a)))
  (..schedule 0))

(def: #export (delay time_millis value)
  {#.doc "Delivers a value after a certain period has passed."}
  (All [a] (-> Nat a (Promise a)))
  (..schedule time_millis (io value)))

(def: #export (wait time_millis)
  {#.doc "Returns a promise that will be resolved after the specified amount of milliseconds."}
  (-> Nat (Promise Any))
  (..delay time_millis []))

(def: #export (time_out time_millis promise)
  {#.doc "Wait for a promise to be resolved within the specified amount of milliseconds."}
  (All [a] (-> Nat (Promise a) (Promise (Maybe a))))
  (..or (wait time_millis) promise))