aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/abstract/monad.lux
blob: ecd4e1ac4937d5a4aba41789335f4b9b0bb35a19 (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
(.using
 [library
  [lux (.except all only)
   [meta
    ["[0]" location]]]]
 [//
  [functor (.only Functor)]])

(def (list#mix f init xs)
  (All (_ a b)
    (-> (-> b a a) a (List b) a)) 
  (case xs
    {.#End}
    init

    {.#Item x xs'}
    (list#mix f (f x init) xs')))

(def (list#size xs)
  (All (_ a) (-> (List a) Nat))
  (loop (again [counter 0
                xs xs])
    (case xs
      {.#End}
      counter

      {.#Item _ xs'}
      (again (++ counter) xs'))))

(def (reversed xs)
  (All (_ a)
    (-> (List a) (List a)))
  (list#mix (function (_ head tail) {.#Item head tail})
            {.#End}
            xs))

(def (pairs xs)
  (All (_ a) (-> (List a) (List [a a])))
  (case xs
    {.#Item x1 {.#Item x2 xs'}}
    {.#Item [x1 x2] (pairs xs')}

    _
    {.#End}))

(type: .public (Monad m)
  (Interface
   (is (Functor m)
       functor)
   (is (All (_ a)
         (-> a (m a)))
       in)
   (is (All (_ a)
         (-> (m (m a)) (m a)))
       conjoint)))

(def .public do
  (macro (_ tokens state)
    (case (is (Maybe [(Maybe Text) Code (List Code) Code])
              (case tokens
                (pattern (list [_ {.#Tuple (list [_ {.#Symbol ["" name]}] monad)}] [_ {.#Tuple bindings}] body))
                {.#Some [{.#Some name} monad bindings body]}
                
                (pattern (list monad [_ {.#Tuple bindings}] body))
                {.#Some [{.#None} monad bindings body]}

                _
                {.#None}))
      {.#Some [?name monad bindings body]}
      (if (|> bindings list#size .int ("lux i64 %" +2) ("lux i64 =" +0))
        (let [[module short] (symbol ..do)
              symbol (is (-> Text Code)
                         (|>> (.all "lux text concat" module " " short " ") [""] {.#Symbol} [location.dummy]))
              g!_ (symbol "_")
              g!each (symbol "each")
              g!conjoint (symbol "conjoint")
              body' (list#mix (is (-> [Code Code] Code Code)
                                  (function (_ binding body')
                                    (with_expansions [<default> (` (|> (~ value) ((~ g!each) (function ((~ g!_) (~ var)) (~ body'))) (~ g!conjoint)))]
                                      (let [[var value] binding]
                                        (case var
                                          [_ {.#Symbol ["" _]}]
                                          <default>

                                          [_ {.#Symbol _}]
                                          (` ((~ var) (~ value) (~ body')))

                                          _
                                          <default>)))))
                              body
                              (reversed (pairs bindings)))]
          {.#Right [state (list (case ?name
                                  {.#Some name}
                                  (let [name [location.dummy {.#Symbol ["" name]}]]
                                    (` (.case (~ monad)
                                         (~ name)
                                         (.case (~ name)
                                           [(~ g!each) (~' in) (~ g!conjoint)]
                                           (~ body')))))
                                  
                                  {.#None}
                                  (` (.case (~ monad)
                                       [(~ g!each) (~' in) (~ g!conjoint)]
                                       (~ body')))))]})
        {.#Left "'do' bindings must have an even number of parts."})

      {.#None}
      {.#Left "Wrong syntax for 'do'"})))

(def .public (then monad f)
  (All (_ ! a b)
    (-> (Monad !) (-> a (! b))
        (-> (! a) (! b))))
  (|>> (at monad each f)
       (at monad conjoint)))

(def .public (all monad)
  (All (_ ! a)
    (-> (Monad !) (List (! a))
        (! (List a))))
  (let [(open "!#[0]") monad]
    (function (again xs)
      (case xs
        {.#End}
        (!#in {.#End})
        
        {.#Item x xs'}
        (|> x
            (!#each (function (_ _x)
                      (!#each (|>> {.#Item _x}) (again xs'))))
            !#conjoint)))))

(def .public (each monad f)
  (All (_ M a b)
    (-> (Monad M) (-> a (M b)) (List a)
        (M (List b))))
  (let [(open "!#[0]") monad]
    (function (again xs)
      (case xs
        {.#End}
        (!#in {.#End})
        
        {.#Item x xs'}
        (|> (f x)
            (!#each (function (_ _x)
                      (!#each (|>> {.#Item _x}) (again xs'))))
            !#conjoint)))))

(def .public (only monad f)
  (All (_ ! a b)
    (-> (Monad !) (-> a (! Bit)) (List a)
        (! (List a))))
  (let [(open "!#[0]") monad]
    (function (again xs)
      (case xs
        {.#End}
        (!#in {.#End})
        
        {.#Item head xs'}
        (|> (f head)
            (!#each (function (_ verdict)
                      (!#each (function (_ tail)
                                (if verdict
                                  {.#Item head tail}
                                  tail))
                              (again xs'))))
            !#conjoint)))))

(def .public (mix monad f init xs)
  (All (_ M a b)
    (-> (Monad M) (-> b a (M a)) a (List b)
        (M a)))
  (case xs
    {.#End}
    (at monad in init)

    {.#Item x xs'}
    (do monad
      [init' (f x init)]
      (mix monad f init' xs'))))