aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/thread.lux
blob: b2945a7a0deaabe816351e1b0f17d7b5686cf910 (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
(.module:
  [library
   [lux "*"
    ["@" target]
    [abstract
     [functor {"+" [Functor]}]
     [apply {"+" [Apply]}]
     [monad {"+" [Monad do]}]]
    [control
     ["[0]" io {"+" [IO]}]]
    [data
     [collection
      ["[0]" array {"+" [Array]}]]]
    [type
     abstract]]])

(type: .public (Thread ! a)
  (-> ! a))

(abstract: .public (Box t v)
  (Array v)

  [(def: .public (box init)
     (All (_ a) (-> a (All (_ !) (Thread ! (Box ! a)))))
     (function (_ !)
       (|> (array.empty 1)
           (array.write! 0 init)
           :abstraction)))

   (def: .public (read! box)
     (All (_ ! a) (-> (Box ! a) (Thread ! a)))
     (function (_ !)
       (for {@.old
             ("jvm aaload" (:representation box) 0)

             @.jvm
             ("jvm array read object"
              (|> 0
                  (:as (primitive "java.lang.Long"))
                  "jvm object cast"
                  "jvm conversion long-to-int")
              (:representation box))

             @.js ("js array read" 0 (:representation box))
             @.python ("python array read" 0 (:representation box))
             @.lua ("lua array read" 0 (:representation box))
             @.ruby ("ruby array read" 0 (:representation box))
             @.php ("php array read" 0 (:representation box))
             @.scheme ("scheme array read" 0 (:representation box))})))

   (def: .public (write! value box)
     (All (_ a) (-> a (All (_ !) (-> (Box ! a) (Thread ! Any)))))
     (function (_ !)
       (|> box :representation (array.write! 0 value) :abstraction)))]
  )

(def: .public (result thread)
  (All (_ a)
    (-> (All (_ !) (Thread ! a))
        a))
  (thread []))

(def: .public io
  (All (_ a)
    (-> (All (_ !) (Thread ! a))
        (IO a)))
  (|>> ..result io.io))

(implementation: .public functor
  (All (_ !) (Functor (Thread !)))

  (def: (each f)
    (function (_ fa)
      (function (_ !)
        (f (fa !))))))

(implementation: .public apply
  (All (_ !) (Apply (Thread !)))

  (def: &functor ..functor)

  (def: (on fa ff)
    (function (_ !)
      ((ff !) (fa !)))))

(implementation: .public monad
  (All (_ !) (Monad (Thread !)))

  (def: &functor ..functor)

  (def: (in value)
    (function (_ !)
      value))

  (def: (conjoint ffa)
    (function (_ !)
      ((ffa !) !))))

(def: .public (update! f box)
  (All (_ a !) (-> (-> a a) (Box ! a) (Thread ! a)))
  (do ..monad
    [old (read! box)
     _ (write! (f old) box)]
    (in old)))