aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/maybe.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/lux/data/maybe.lux')
-rw-r--r--stdlib/source/lux/data/maybe.lux82
1 files changed, 82 insertions, 0 deletions
diff --git a/stdlib/source/lux/data/maybe.lux b/stdlib/source/lux/data/maybe.lux
new file mode 100644
index 000000000..16aa9e30a
--- /dev/null
+++ b/stdlib/source/lux/data/maybe.lux
@@ -0,0 +1,82 @@
+## Copyright (c) Eduardo Julian. All rights reserved.
+## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+## If a copy of the MPL was not distributed with this file,
+## You can obtain one at http://mozilla.org/MPL/2.0/.
+
+(;module:
+ lux
+ (lux (control (monoid #as m #refer #all)
+ (functor #as F #refer #all)
+ (applicative #as A #refer #all)
+ (monad #as M #refer #all)
+ eq)))
+
+## [Types]
+## (type: (Maybe a)
+## #;None
+## (#;Some a))
+
+## [Structures]
+(struct: #export Monoid<Maybe> (All [a] (Monoid (Maybe a)))
+ (def: unit #;None)
+ (def: (append xs ys)
+ (case xs
+ #;None ys
+ (#;Some x) (#;Some x))))
+
+(struct: #export _ (Functor Maybe)
+ (def: (map f ma)
+ (case ma
+ #;None #;None
+ (#;Some a) (#;Some (f a)))))
+
+(struct: #export _ (Applicative Maybe)
+ (def: functor Functor<Maybe>)
+
+ (def: (wrap x)
+ (#;Some x))
+
+ (def: (apply ff fa)
+ (case [ff fa]
+ [(#;Some f) (#;Some a)]
+ (#;Some (f a))
+
+ _
+ #;None)))
+
+(struct: #export _ (Monad Maybe)
+ (def: applicative Applicative<Maybe>)
+
+ (def: (join mma)
+ (case mma
+ #;None #;None
+ (#;Some xs) xs)))
+
+(struct: #export (Eq<Maybe> Eq<a>) (All [a] (-> (Eq a) (Eq (Maybe a))))
+ (def: (= mx my)
+ (case [mx my]
+ [#;None #;None]
+ true
+
+ [(#;Some x) (#;Some y)]
+ (:: Eq<a> = x y)
+
+ _
+ false)))
+
+(struct: #export (MaybeT Monad<M>)
+ (All [M] (-> (Monad M) (Monad (All [a] (M (Maybe a))))))
+ (def: applicative (A;compA (get@ #M;applicative Monad<M>) Applicative<Maybe>))
+ (def: (join MmMma)
+ (do Monad<M>
+ [mMma MmMma]
+ (case mMma
+ #;None
+ (wrap #;None)
+
+ (#;Some Mma)
+ (join Mma)))))
+
+(def: #export (lift-maybe Monad<M>)
+ (All [M a] (-> (Monad M) (-> (M a) (M (Maybe a)))))
+ (liftM Monad<M> (:: Monad<Maybe> wrap)))