aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/control/monad.lux
blob: 5c540791ada162dd6d9fcdc6ff5c94640391e857 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(;module:
  lux
  (.. (functor #as F)
      (applicative #as A)))

## [Utils]
(def: (fold f init xs)
  (All [a b]
    (-> (-> b a a) a (List b) a)) 
  (case xs
    #;Nil
    init

    (#;Cons x xs')
    (fold f (f x init) xs')))

(def: (map f xs)
  (All [a b]
    (-> (-> a b) (List a) (List b))) 
  (case xs
    #;Nil
    #;Nil

    (#;Cons x xs')
    (#;Cons (f x) (map f xs'))))

(def: (reverse xs)
  (All [a]
    (-> (List a) (List a)))
  (fold (lambda [head tail] (#;Cons head tail))
        #;Nil
        xs))

(def: (as-pairs xs)
  (All [a] (-> (List a) (List [a a])))
  (case xs
    (#;Cons x1 (#;Cons x2 xs'))
    (#;Cons [x1 x2] (as-pairs xs'))

    _
    #;Nil))

## [Signatures]
(sig: #export (Monad m)
  (: (A;Applicative m)
     applicative)
  (: (All [a]
       (-> (m (m a)) (m a)))
     join))

## [Syntax]
(macro: #export (do tokens state)
  {#;doc (doc "Macro for easy concatenation of monadic operations."
              (do Monad<Maybe>
                [y (f1 x)
                 z (f2 z)]
                (wrap (f3 z))))}
  (case tokens
    (#;Cons monad (#;Cons [_ (#;TupleS bindings)] (#;Cons body #;Nil)))
    (let [g!@ (: AST [["" -1 -1] (#;SymbolS ["" "@"])])
          g!map (: AST [["" -1 -1] (#;SymbolS ["" " map "])])
          g!join (: AST [["" -1 -1] (#;SymbolS ["" " join "])])
          g!apply (: AST [["" -1 -1] (#;SymbolS ["" " apply "])])
          body' (fold (: (-> [AST AST] AST AST)
                         (lambda [binding body']
                           (let [[var value] binding]
                             (case var
                               [_ (#;TagS ["" "let"])]
                               (` (let (~ value) (~ body')))

                               _
                               (` (|> (~ value) ((~ g!map) (lambda [(~ var)] (~ body'))) (~ g!join)))
                               ))))
                      body
                      (reverse (as-pairs bindings)))]
      (#;Right [state (#;Cons (` (;_lux_case (~ monad)
                                   (~ g!@)
                                   (;_lux_case (~ g!@)
                                     {#applicative {#A;functor {#F;map (~ g!map)}
                                                    #A;wrap (~' wrap)
                                                    #A;apply (~ g!apply)}
                                      #join (~ g!join)}
                                     (~ body'))))
                              #;Nil)]))

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

## [Functions]
(def: #export (seqM monad xs)
  {#;doc "Run all the monadic values in the list and produce a list of the base values."}
  (All [M a]
    (-> (Monad M) (List (M a)) (M (List a))))
  (case xs
    #;Nil
    (:: monad wrap #;Nil)
    
    (#;Cons x xs')
    (do monad
      [_x x
       _xs (seqM monad xs')]
      (wrap (#;Cons _x _xs)))
    ))

(def: #export (mapM monad f xs)
  {#;doc "Apply a monad-producing function to all values in a list."}
  (All [M a b]
    (-> (Monad M) (-> a (M b)) (List a) (M (List b))))
  (case xs
    #;Nil
    (:: monad wrap #;Nil)
    
    (#;Cons x xs')
    (do monad
      [_x (f x)
       _xs (mapM monad f xs')]
      (wrap (#;Cons _x _xs)))
    ))

(def: #export (foldM monad f init xs)
  {#;doc "Fold a list with a monad-producing function."}
  (All [M a b]
    (-> (Monad M) (-> b a (M a)) a (List b)
        (M a)))
  (case xs
    #;Nil
    (:: monad wrap init)

    (#;Cons x xs')
    (do monad
      [init' (f x init)]
      (foldM monad f init' xs'))))

(def: #export (liftM Monad<M> f)
  {#;doc "Lift a normal function into the space of monads."}
  (All [M a b]
    (-> (Monad M) (-> a b) (-> (M a) (M b))))
  (lambda [ma]
    (do Monad<M>
      [a ma]
      (wrap (f a)))))