2020-05-18 15:59:07 +03:00
|
|
|
module Data.Maybe
|
|
|
|
|
2021-11-26 13:55:17 +03:00
|
|
|
import Control.Function
|
|
|
|
|
2021-06-09 01:05:10 +03:00
|
|
|
%default total
|
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
public export
|
|
|
|
isNothing : Maybe a -> Bool
|
|
|
|
isNothing Nothing = True
|
2020-06-30 01:35:54 +03:00
|
|
|
isNothing (Just _) = False
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
public export
|
|
|
|
isJust : Maybe a -> Bool
|
|
|
|
isJust Nothing = False
|
2020-06-30 01:35:54 +03:00
|
|
|
isJust (Just _) = True
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
||| Proof that some `Maybe` is actually `Just`
|
|
|
|
public export
|
|
|
|
data IsJust : Maybe a -> Type where
|
|
|
|
ItIsJust : IsJust (Just x)
|
|
|
|
|
|
|
|
export
|
|
|
|
Uninhabited (IsJust Nothing) where
|
|
|
|
uninhabited ItIsJust impossible
|
|
|
|
|
|
|
|
||| Decide whether a 'Maybe' is 'Just'
|
|
|
|
public export
|
|
|
|
isItJust : (v : Maybe a) -> Dec (IsJust v)
|
2020-06-30 01:35:54 +03:00
|
|
|
isItJust (Just _) = Yes ItIsJust
|
2020-05-18 15:59:07 +03:00
|
|
|
isItJust Nothing = No absurd
|
|
|
|
|
|
|
|
||| Convert a `Maybe a` value to an `a` value by providing a default `a` value
|
|
|
|
||| in the case that the `Maybe` value is `Nothing`.
|
|
|
|
public export
|
|
|
|
fromMaybe : (Lazy a) -> Maybe a -> a
|
|
|
|
fromMaybe def Nothing = def
|
2020-06-30 01:35:54 +03:00
|
|
|
fromMaybe _ (Just j) = j
|
2020-05-18 15:59:07 +03:00
|
|
|
|
2020-05-20 21:11:35 +03:00
|
|
|
||| Returns the `a` value of a `Maybe a` which is proved `Just`.
|
|
|
|
public export
|
2021-02-11 16:18:13 +03:00
|
|
|
fromJust : (v : Maybe a) -> (0 _ : IsJust v) => a
|
2020-05-20 21:11:35 +03:00
|
|
|
fromJust (Just x) = x
|
|
|
|
fromJust Nothing impossible
|
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
||| Returns `Just` the given value if the conditional is `True`
|
|
|
|
||| and `Nothing` if the conditional is `False`.
|
|
|
|
public export
|
|
|
|
toMaybe : Bool -> Lazy a -> Maybe a
|
|
|
|
toMaybe True j = Just j
|
2020-06-30 01:35:54 +03:00
|
|
|
toMaybe False _ = Nothing
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
2021-11-26 13:55:17 +03:00
|
|
|
Injective Just where
|
|
|
|
injective Refl = Refl
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
||| Convert a `Maybe a` value to an `a` value, using `neutral` in the case
|
|
|
|
||| that the `Maybe` value is `Nothing`.
|
|
|
|
public export
|
|
|
|
lowerMaybe : Monoid a => Maybe a -> a
|
|
|
|
lowerMaybe Nothing = neutral
|
|
|
|
lowerMaybe (Just x) = x
|
|
|
|
|
|
|
|
||| Returns `Nothing` when applied to `neutral`, and `Just` the value otherwise.
|
|
|
|
export
|
2021-10-17 01:32:16 +03:00
|
|
|
raiseToMaybe : Monoid a => Eq a => a -> Maybe a
|
2020-05-18 15:59:07 +03:00
|
|
|
raiseToMaybe x = if x == neutral then Nothing else Just x
|
2021-04-25 11:13:55 +03:00
|
|
|
|
|
|
|
public export
|
|
|
|
filter : (a -> Bool) -> Maybe a -> Maybe a
|
|
|
|
filter _ Nothing = Nothing
|
|
|
|
filter f (Just x) = toMaybe (f x) x
|
2022-03-23 16:33:13 +03:00
|
|
|
|
|
|
|
namespace Semigroup
|
|
|
|
|
|
|
|
public export
|
|
|
|
[Deep] Semigroup a => Semigroup (Maybe a) where
|
|
|
|
Nothing <+> Nothing = Nothing
|
|
|
|
Just l <+> Nothing = Just l
|
|
|
|
Nothing <+> Just r = Just r
|
|
|
|
Just l <+> Just r = Just $ l <+> r
|
|
|
|
|
|
|
|
namespace Monoid
|
|
|
|
|
|
|
|
public export
|
|
|
|
[Deep] Semigroup a => Monoid (Maybe a) using Semigroup.Deep where
|
|
|
|
neutral = Nothing
|