{-# LANGUAGE Safe, TypeOperators, QuasiQuotes #-}
module Data.Invertible.Maybe
( isJust
, isNothing
, listToMaybe
, maybeToList
, fromMaybe
, fromJust
) where
import qualified Data.Maybe as M
import Data.Invertible.Bijection
import Data.Invertible.TH
import Data.Invertible.Internal
isJust :: Maybe () <-> Bool
isJust :: Maybe () <-> Bool
isJust =
[biCase|
Just () <-> True
Nothing <-> False
|]
isNothing :: Maybe () <-> Bool
isNothing :: Maybe () <-> Bool
isNothing =
[biCase|
Nothing <-> True
Just () <-> False
|]
listToMaybe :: [a] <-> Maybe a
listToMaybe :: forall a. [a] <-> Maybe a
listToMaybe = [a] -> Maybe a
forall a. [a] -> Maybe a
M.listToMaybe ([a] -> Maybe a)
-> (Maybe a -> [a]) -> Bijection (->) [a] (Maybe a)
forall (a :: * -> * -> *) b c. a b c -> a c b -> Bijection a b c
:<->: Maybe a -> [a]
forall a. Maybe a -> [a]
M.maybeToList
maybeToList :: Maybe a <-> [a]
maybeToList :: forall a. Maybe a <-> [a]
maybeToList = Bijection (->) [a] (Maybe a) -> Bijection (->) (Maybe a) [a]
forall (a :: * -> * -> *) b c. Bijection a b c -> Bijection a c b
invert Bijection (->) [a] (Maybe a)
forall a. [a] <-> Maybe a
listToMaybe
fromMaybe :: Eq a => a -> Maybe a <-> a
fromMaybe :: forall a. Eq a => a -> Maybe a <-> a
fromMaybe a
d = a -> Maybe a -> a
forall a. a -> Maybe a -> a
M.fromMaybe a
d (Maybe a -> a) -> (a -> Maybe a) -> Bijection (->) (Maybe a) a
forall (a :: * -> * -> *) b c. a b c -> a c b -> Bijection a b c
:<->: \a
a -> if a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
d then Maybe a
forall a. Maybe a
Nothing else a -> Maybe a
forall a. a -> Maybe a
Just a
a
fromJust :: Maybe a <-> a
fromJust :: forall a. Maybe a <-> a
fromJust = Maybe a -> a
forall a. HasCallStack => Maybe a -> a
M.fromJust (Maybe a -> a) -> (a -> Maybe a) -> Bijection (->) (Maybe a) a
forall (a :: * -> * -> *) b c. a b c -> a c b -> Bijection a b c
:<->: a -> Maybe a
forall a. a -> Maybe a
Just