~jojo/Carth

ref: 55fb4f948f1f3797078b584dc60b4f7dd68b37ed Carth/std/lazy.carth -rw-r--r-- 526 bytes
55fb4f94JoJo Check `cast` in Infer instead of Gen 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(import macros)
(import sync)
(import io)

(data (Lazy a)
  (Lazy [Mutex (Box (Either (Fun Unit a) a))]))

(define: (strict (Lazy [mx ptr]))
    (forall (a) (Fun (Lazy a) a))
  (unsafe-perform-io
   (io/thenr
    (mutex/lock mx)
    (match (deref ptr)
      (case (Left f)
            (let ((x (f Unit)))
              (apps io/thenr
                    (io/write-ref (Right x) ptr)
                    (mutex/release mx)
                    (io/pure x))))
      (case (Right x) (io/thenr (mutex/release mx) (io/pure x)))))))