module StaticLS.Maybe where

import Control.Applicative
import Control.Error.Util (maybeT)
import Control.Monad
import Control.Monad.Trans.Except (ExceptT (..), throwE)
import Control.Monad.Trans.Maybe

flatMaybeT :: (Monad m) => MaybeT m (Maybe a) -> MaybeT m a
flatMaybeT :: forall (m :: * -> *) a. Monad m => MaybeT m (Maybe a) -> MaybeT m a
flatMaybeT = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe a) -> MaybeT m a)
-> (MaybeT m (Maybe a) -> m (Maybe a))
-> MaybeT m (Maybe a)
-> MaybeT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe (Maybe a) -> Maybe a) -> m (Maybe (Maybe a)) -> m (Maybe a)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe (Maybe a) -> Maybe a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (m (Maybe (Maybe a)) -> m (Maybe a))
-> (MaybeT m (Maybe a) -> m (Maybe (Maybe a)))
-> MaybeT m (Maybe a)
-> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MaybeT m (Maybe a) -> m (Maybe (Maybe a))
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT

toAlt :: (Functor f, Foldable f, Alternative g) => f a -> g a
toAlt :: forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Foldable f, Alternative g) =>
f a -> g a
toAlt f a
as = f (g a) -> g a
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum ((a -> g a) -> f a -> f (g a)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> g a
forall a. a -> g a
forall (f :: * -> *) a. Applicative f => a -> f a
pure f a
as)

orDie :: (Monad m) => Maybe a -> e -> ExceptT e m a
Maybe a
x orDie :: forall (m :: * -> *) a e. Monad m => Maybe a -> e -> ExceptT e m a
`orDie` e
e = ExceptT e m a -> (a -> ExceptT e m a) -> Maybe a -> ExceptT e m a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (e -> ExceptT e m a
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE e
e) a -> ExceptT e m a
forall a. a -> ExceptT e m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
x

orDieT :: (Monad m) => MaybeT m a -> e -> ExceptT e m a
MaybeT m a
x orDieT :: forall (m :: * -> *) a e.
Monad m =>
MaybeT m a -> e -> ExceptT e m a
`orDieT` e
e = m (Either e a) -> ExceptT e m a
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either e a) -> ExceptT e m a)
-> m (Either e a) -> ExceptT e m a
forall a b. (a -> b) -> a -> b
$ m (Either e a)
-> (a -> m (Either e a)) -> MaybeT m a -> m (Either e a)
forall (m :: * -> *) b a.
Monad m =>
m b -> (a -> m b) -> MaybeT m a -> m b
maybeT (Either e a -> m (Either e a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e a -> m (Either e a))
-> (e -> Either e a) -> e -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> Either e a
forall a b. a -> Either a b
Left (e -> m (Either e a)) -> e -> m (Either e a)
forall a b. (a -> b) -> a -> b
$ e
e) (Either e a -> m (Either e a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e a -> m (Either e a))
-> (a -> Either e a) -> a -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either e a
forall a b. b -> Either a b
Right) MaybeT m a
x