aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/io.lux
blob: 13eda4b75b89dad61e903c2d8cde4437736c8e15 (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)]]
   [macro (.only with_symbols)
    [syntax (.only syntax)]
    ["[0]" template]
    ["[0]" code
     ["<[1]>" \\parser]]]
   [meta
    [type
     [primitive (.except)]]]]])

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

  (def label
    (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 (` ((~! ..label) (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))))
  )