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

(def: #export parallelism
  Nat
  ("lux process parallelism"))

(abstract: #export (Promise a)
  {#.doc "Represents values produced by asynchronous computations (unlike IO, which is synchronous)."}
  (Atom [(Maybe a) (List (-> a (IO Top)))])

  (def: #export (promise ?value)
    (All [a] (-> (Maybe a) (Promise a)))
    (@abstraction (atom [?value (list)])))

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

  (def: #export (resolve value (^@representation promise))
    {#.doc "Sets an promise's value if it has not been done yet."}
    (All [a] (-> a (Promise a) (IO Bool)))
    (do io.Monad<IO>
      [(^@ old [_value _observers]) (atom.read promise)]
      (case _value
        (#.Some _)
        (wrap false)

        #.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 true))
            (resolve value (@abstraction promise)))))))

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

        #.None
        (let [new [_value (#.Cons f _observers)]]
          (if (io.run (atom.compare-and-swap old new promise))
            []
            (await f (@abstraction promise)))))))
  )

(def: #export (resolved? promise)
  {#.doc "Checks whether a promise's value has already been resolved."}
  (All [a] (-> (Promise a) Bool))
  (case (poll promise)
    #.None
    false

    (#.Some _)
    true))

(struct: #export _ (Functor Promise)
  (def: (map f fa)
    (let [fb (promise #.None)]
      (exec (await (function (_ a) (resolve (f a) fb))
                   fa)
        fb))))

(struct: #export _ (Apply Promise)
  (def: functor Functor<Promise>)

  (def: (apply ff fa)
    (let [fb (promise #.None)]
      (exec (await (function (_ f)
                     (io (await (function (_ a) (resolve (f a) fb))
                                fa)))
                   ff)
        fb))))

(struct: #export _ (Monad Promise)
  (def: functor Functor<Promise>)

  (def: (wrap a)
    (promise (#.Some a)))

  (def: (join mma)
    (let [ma (promise #.None)]
      (exec (await (function (_ ma')
                     (io (await (function (_ a') (resolve a' ma))
                                ma')))
                   mma)
        ma))))

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

(def: #export (alt left right)
  {#.doc "Heterogeneous alternative combinator."}
  (All [a b] (-> (Promise a) (Promise b) (Promise (| a b))))
  (let [a|b (promise #.None)]
    (with-expansions
      [<sides> (do-template [<promise> <tag>]
                 [(await (function (_ value) (resolve (<tag> value) a|b))
                         <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 (promise #.None)]
    (`` (exec (~~ (do-template [<promise>]
                    [(await (function (_ value) (resolve value left||right))
                            <promise>)]

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

(def: #export (schedule millis-delay computation)
  {#.doc "Runs an I/O computation on its own process (after a specified delay) and returns a Promise that will eventually host its result."}
  (All [a] (-> Nat (IO a) (Promise a)))
  (let [!out (promise #.None)]
    (exec ("lux process schedule" millis-delay
           (io (io.run (resolve (io.run computation)
                                !out))))
      !out)))

(def: #export future
  {#.doc "Runs an I/O computation on its own process and 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 Top))
  (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))))
  (alt (wait time-millis) promise))