aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/maybe.lux
blob: 386548905c7faf11a0002630ca5e022d510782f4 (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
(.using
 [library
  [lux {"-" list}
   [abstract
    [monoid {"+" Monoid}]
    [equivalence {"+" Equivalence}]
    [hash {"+" Hash}]
    [apply {"+" Apply}]
    ["[0]" functor {"+" Functor}]
    ["[0]" monad {"+" Monad do}]]
   [meta
    ["[0]" location]]]])

... (type: (Maybe a)
...   {.#None}
...   {.#Some a})

(implementation: .public monoid
  (All (_ a) (Monoid (Maybe a)))
  
  (def: identity
    {.#None})
  
  (def: (composite mx my)
    (case mx
      {.#None}
      my
      
      _
      mx)))

(implementation: .public functor
  (Functor Maybe)
  
  (def: (each f ma)
    (case ma
      {.#Some a}
      {.#Some (f a)}
      
      ... {.#None}
      it
      (:expected it))))

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

  (def: (on fa ff)
    (case [ff fa]
      [{.#Some f} {.#Some a}]
      {.#Some (f a)}

      _
      {.#None})))

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

  (def: (in x)
    {.#Some x})

  (def: (conjoint mma)
    (case mma
      {.#Some mx}
      mx
      
      ... {.#None}
      it
      (:expected it))))

(implementation: .public (equivalence super)
  (All (_ a) (-> (Equivalence a) (Equivalence (Maybe a))))
  
  (def: (= mx my)
    (case [mx my]
      [{.#None} {.#None}]
      #1

      [{.#Some x} {.#Some y}]
      (# super = x y)
      
      _
      #0)))

(implementation: .public (hash super)
  (All (_ a) (-> (Hash a) (Hash (Maybe a))))

  (def: &equivalence
    (..equivalence (# super &equivalence)))
  
  (def: (hash value)
    (case value
      {.#None}
      1

      {.#Some value}
      (# super hash value))))

(implementation: .public (with monad)
  (All (_ M) (-> (Monad M) (Monad (All (_ a) (M (Maybe a))))))

  (def: &functor
    (functor.composite (the monad.&functor monad)
                       ..functor))

  (def: in
    (|>> (# ..monad in)
         (# monad in)))
  
  (def: (conjoint MmMma)
    (do monad
      [mMma MmMma]
      (case mMma
        {.#Some Mma}
        Mma

        ... {.#None}
        it
        (in (:expected it))))))

(def: .public (lifted monad)
  (All (_ M a) (-> (Monad M) (-> (M a) (M (Maybe a)))))
  (# monad each (# ..monad in)))

(macro: .public (else tokens state)
  (case tokens
    (^ (.list else maybe))
    (let [g!temp (: Code [location.dummy {.#Symbol ["" ""]}])]
      {.#Right [state (.list (` (.case (~ maybe)
                                  {.#Some (~ g!temp)}
                                  (~ g!temp)

                                  ... {.#None}
                                  (~ g!temp)
                                  (~ else))))]})

    _
    {.#Left "Wrong syntax for else"}))

(def: .public trusted
  (All (_ a) (-> (Maybe a) a))
  (|>> (..else (undefined))))

(def: .public (list value)
  (All (_ a) (-> (Maybe a) (List a)))
  (case value
    {.#Some value}
    {.#Item value {.#End}}

    ... {.#None}
    _
    {.#End}))

(macro: .public (when tokens state)
  (case tokens
    (^ (.list test then))
    {.#Right [state (.list (` (.if (~ test)
                                (~ then)
                                {.#None})))]}

    _
    {.#Left "Wrong syntax for when"}))