aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/io.lux
blob: 3edf5e3ee35cc1c442ba0127cc5a44796f0b30db (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
(.using
 [library
  [lux "*"
   [abstract
    [functor {"+" Functor}]
    [apply {"+" Apply}]
    [monad {"+" Monad do}]]
   [control
    [parser
     ["<[0]>" code]]]
   [type
    [abstract {"-" pattern}]]
   [macro {"+" with_symbols}
    [syntax {"+" syntax:}]
    ["[0]" template]]]])

(abstract: .public (IO a)
  (-> Any a)

  (def: label
    (All (_ a) (-> (-> Any a) (IO a)))
    (|>> abstraction))

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

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

  (syntax: .public (io [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!'))

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

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

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

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

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