aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/maybe.lux
blob: a019a4c79b97acf4fb4e5ece04bafb417a9dcbe9 (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
(.module:
  [library
   [lux (#- list)
    [abstract
     [monoid (#+ Monoid)]
     [equivalence (#+ Equivalence)]
     [hash (#+ Hash)]
     [apply (#+ Apply)]
     ["." functor (#+ Functor)]
     ["." monad (#+ Monad do)]]
    [meta
     ["." location]]]])

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

(implementation: .public monoid
  (All [a] (Monoid (Maybe a)))
  
  (def: identity #.None)
  
  (def: (compose mx my)
    (case mx
      #.None
      my
      
      (#.Some x)
      (#.Some x))))

(implementation: .public functor
  (Functor Maybe)
  
  (def: (map f ma)
    (case ma
      #.None     #.None
      (#.Some a) (#.Some (f a)))))

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

  (def: (apply ff fa)
    (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: (join mma)
    (case mma
      #.None
      #.None
      
      (#.Some mx)
      mx)))

(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
      0

      (#.Some value)
      (\ super hash value))))

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

  (def: &functor (functor.compose (get@ #monad.&functor monad) ..functor))

  (def: in (|>> (\ ..monad in) (\ monad in)))
  
  (def: (join MmMma)
    (do monad
      [mMma MmMma]
      (case mMma
        #.None
        (in #.None)

        (#.Some Mma)
        Mma))))

(def: .public (lift monad)
  {#.doc (doc "Wraps a monadic value with Maybe machinery.")}
  (All [M a] (-> (Monad M) (-> (M a) (M (Maybe a)))))
  (\ monad map (\ ..monad in)))

(macro: .public (else tokens state)
  {#.doc (doc "Allows you to provide a default value that will be used"
              "if a (Maybe x) value turns out to be #.None."
              "Note: the expression for the default value will not be computed if the base computation succeeds."
              (else +20 (#.Some +10))
              "=>"
              +10
              --------------------------
              (else +20 #.None)
              "=>"
              +20)}
  (case tokens
    (^ (.list else maybe))
    (let [g!temp (: Code [location.dummy (#.Identifier ["" ""])])]
      (#.Right [state (.list (` (case (~ maybe)
                                  (#.Some (~ g!temp))
                                  (~ g!temp)

                                  #.None
                                  (~ else))))]))

    _
    (#.Left "Wrong syntax for else")))

(def: .public assume
  {#.doc (doc "Assumes that a Maybe value is a #.Some and yields its value."
              "Raises/throws a runtime error otherwise."
              "WARNING: Use with caution.")}
  (All [a] (-> (Maybe a) a))
  (|>> (..else (undefined))))

(def: .public (list value)
  (All [a] (-> (Maybe a) (List a)))
  (case value
    #.None
    #.End

    (#.Some value)
    (#.Item value #.End)))

(macro: .public (when tokens state)
  {#.doc (doc "Can be used as a guard in (co)monadic be/do expressions."
              (do monad
                [value (do_something 1 2 3)
                 ..when (passes_test? value)]
                (do_something_else 4 5 6)))}
  (case tokens
    (^ (.list test then))
    (#.Right [state (.list (` (.if (~ test)
                                (~ then)
                                #.None)))])

    _
    (#.Left "Wrong syntax for when")))