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

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

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

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

(implementation: #export apply
  (Apply Maybe)
  
  (def: &functor ..functor)

  (def: (apply ff fa)
    (case [ff fa]
      [(#.Some f) (#.Some a)]
      (#.Some (f a))

      _
      #.None)))

(implementation: #export monad
  (Monad Maybe)
  
  (def: &functor ..functor)

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

  (def: (join mma)
    (case mma
      #.None
      #.None
      
      (#.Some mx)
      mx)))

(implementation: #export (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: #export (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: #export (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: #export (lift monad)
  (All [M a] (-> (Monad M) (-> (M a) (M (Maybe a)))))
  (\ monad map (\ ..monad in)))

(macro: #export (default 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."
              (default +20 (#.Some +10))
              "=>"
              +10
              (default +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 default")))

(def: #export assume
  (All [a] (-> (Maybe a) a))
  (|>> (..default (undefined))))

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

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