aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/io.lux
blob: d538fbfef96f3cfca83c1472b4a8967ef6fc4439 (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
(;module: {#;doc "A method for abstracting I/O and effectful computations to make it safe while writing pure functional code."}
  lux
  (lux (control functor
                applicative
                monad)
       (data (coll list))))

## [Types]
(type: #export (IO a)
  {#;doc "A type that represents synchronous, effectful computations that may interact with the outside world."}
  (-> Void a))

## [Syntax]
(macro: #export (io tokens state)
  {#;doc (doc "Delays the evaluation of an expression, by wrapping it in an IO 'thunk'."
              "Great for wrapping effectful computations (which will not be performed until the IO is \"run\")."
              (io (exec
                    (log! msg)
                    "Some value...")))}
  (case tokens
    (^ (list value))
    (let [blank (: AST [["" +0 +0] (#;SymbolS ["" ""])])]
      (#;Right [state (list (` (;_lux_function (~ blank) (~ blank) (~ value))))]))

    _
    (#;Left "Wrong syntax for io")))

## [Structures]
(struct: #export _ (Functor IO)
  (def: (map f ma)
    (io (f (ma (:! Void []))))))

(struct: #export _ (Applicative IO)
  (def: functor Functor<IO>)

  (def: (wrap x)
    (io x))

  (def: (apply ff fa)
    (io ((ff (:! Void [])) (fa (:! Void []))))))

(struct: #export _ (Monad IO)
  (def: applicative Applicative<IO>)
  
  (def: (join mma)
    (io ((mma (:! Void [])) (:! Void [])))))

## [Functions]
(def: #export (run action)
  {#;doc "A way to execute IO computations and perform their side-effects."}
  (All [a] (-> (IO a) a))
  (action (:! Void [])))