aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/io.lux
blob: 9da4eda3c6706d9a9c5963d1ccc43a558db38abf (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
(.require
 [library
  [lux (.except)
   [abstract
    [functor (.only Functor)]
    [apply (.only Apply)]
    [monad (.only Monad do)]]
   [meta
    ["[0]" code
     ["<[1]>" \\parser]]
    [macro (.only with_symbols)
     [syntax (.only syntax)]
     ["[0]" template]]
    [type
     ["[0]" primitive (.except def)]]]]])

(primitive.def .public (IO a)
  (-> Any a)

  (def .public io'
    (All (_ a) (-> (-> Any a) (IO a)))
    (|>> abstraction))

  (def !io
    (template (!io computation)
      [(abstraction
        (template.with_locals [g!func g!arg]
          (function (g!func g!arg)
            computation)))]))

  (def run!'
    (template (run!' io)
      ... creatio ex nihilo
      [((representation io) [])]))

  (def .public io
    (syntax (_ [computation <code>.any])
      (with_symbols [g!func g!arg]
        (in (list (` (..io' (function ((, g!func) (, g!arg))
                              (, computation)))))))))

  (def .public run!
    (All (_ a) (-> (IO a) a))
    (|>> ..run!'))

  (def .public functor
    (Functor IO)
    (implementation
     (def (each f)
       (|>> ..run!' f !io))))

  (def .public apply
    (Apply IO)
    (implementation
     (def functor ..functor)

     (def (on fa ff)
       (!io ((..run!' ff) (..run!' fa))))))

  (def .public monad
    (Monad IO)
    (implementation
     (def functor ..functor)

     (def in
       (|>> !io))
     
     (def conjoint
       (|>> ..run!' ..run!' !io))))
  )