aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/control/monad.lux
blob: fd940ea835b3c7012b77a9eef96c277ddcc0843e (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
(.module:
  lux
  (// (functor #as F)
      (applicative #as A)))

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

    (#.Cons x xs')
    (list/fold f (f x init) xs')))

(def: (list/size xs)
  (All [a] (-> (List a) Nat))
  (loop [counter +0
         xs xs]
    (case xs
      #.Nil
      counter

      (#.Cons _ xs')
      (recur (n/inc counter) xs'))))

(def: (reverse xs)
  (All [a]
    (-> (List a) (List a)))
  (list/fold (function [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]
(def: _cursor Cursor ["" +0 +0])

(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 [_ (#.Tuple bindings)] (#.Cons body #.Nil)))
    (if (|> bindings list/size (n/% +2) (n/= +0))
      (let [g!map (: Code [_cursor (#.Symbol ["" " map "])])
            g!join (: Code [_cursor (#.Symbol ["" " join "])])
            g!apply (: Code [_cursor (#.Symbol ["" " apply "])])
            body' (list/fold (: (-> [Code Code] Code Code)
                                (function [binding body']
                                  (let [[var value] binding]
                                    (case var
                                      [_ (#.Tag ["" "let"])]
                                      (` (let (~ value) (~ body')))

                                      _
                                      (` (|> (~ value) ((~ g!map) (function [(~ var)] (~ body'))) (~ g!join)))
                                      ))))
                             body
                             (reverse (as-pairs bindings)))]
        (#.Right [state (#.Cons (` ("lux case" (~ monad)
                                    {(~' @)
                                     ("lux case" (~' @)
                                      {{#applicative {#A.functor {#F.map (~ g!map)}
                                                      #A.wrap (~' wrap)
                                                      #A.apply (~ g!apply)}
                                        #join (~ g!join)}
                                       (~ body')})}))
                                #.Nil)]))
      (#.Left "'do' bindings must have an even number of parts."))

    _
    (#.Left "Wrong syntax for 'do'")))

## [Functions]
(def: #export (seq 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 (seq monad xs')]
      (wrap (#.Cons _x _xs)))
    ))

(def: #export (map 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 (map monad f xs')]
      (wrap (#.Cons _x _xs)))
    ))

(def: #export (fold 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)]
      (fold monad f init' xs'))))

(def: #export (lift 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))))
  (function [ma]
    (do Monad<M>
      [a ma]
      (wrap (f a)))))

## [Free Monads]
(type: #export (Free F a)
  {#.doc "The Free Monad."}
  (#Pure a)
  (#Effect (F (Free F a))))

(struct: #export (Functor<Free> dsl)
  (All [F] (-> (F.Functor F) (F.Functor (Free F))))
  (def: (map f ea)
    (case ea
      (#Pure a)
      (#Pure (f a))
      
      (#Effect value)
      (#Effect (:: dsl map (map f) value)))))

(struct: #export (Applicative<Free> dsl)
  (All [F] (-> (F.Functor F) (A.Applicative (Free F))))
  (def: functor (Functor<Free> dsl))

  (def: (wrap a)
    (#Pure a))
  
  (def: (apply ef ea)
    (case [ef ea]
      [(#Pure f) (#Pure a)]
      (#Pure (f a))

      [(#Pure f) (#Effect fa)]
      (#Effect (:: dsl map
                   (:: (Functor<Free> dsl) map f)
                   fa))

      [(#Effect ff) _]
      (#Effect (:: dsl map
                   (function [f] (apply f ea))
                   ff))
      )))

(struct: #export (Monad<Free> dsl)
  (All [F] (-> (F.Functor F) (Monad (Free F))))
  (def: applicative (Applicative<Free> dsl))

  (def: (join efefa)
    (case efefa
      (#Pure efa)
      (case efa
        (#Pure a)
        (#Pure a)

        (#Effect fa)
        (#Effect fa))
      
      (#Effect fefa)
      (#Effect (:: dsl map
                   (:: (Monad<Free> dsl) join)
                   fefa))
      )))