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

(def: #export concurrency-level
  Nat
  ("lux process concurrency-level"))

(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 _ (Applicative Promise)
  (def: functor Functor<Promise>)

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

  (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: applicative Applicative<Promise>)

  (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 (future computation)
  {#.doc "Runs an I/O computation on its own process and returns an Promise that will eventually host its result."}
  (All [a] (-> (IO a) (Promise a)))
  (let [!out (promise #.None)]
    (exec ("lux process future" (io (io.run (resolve (io.run computation)
                                                     !out))))
      !out)))

(def: #export (wait time)
  {#.doc "Returns a promise that will be resolved after the specified amount of milliseconds."}
  (-> Nat (Promise Unit))
  (let [!out (: (Promise Unit) (promise #.None))]
    (exec ("lux process schedule" time (resolve [] !out))
      !out)))

(def: #export (time-out time 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) promise))

(def: #export (delay time value)
  {#.doc "Delivers a value after a certain period has passed."}
  (All [a] (-> Nat a (Promise a)))
  (:: Functor<Promise> map (function.const value) (wait time)))