-- Copyright 2023 Lennart Augustsson-- See LICENSE file for full license.module Data.Maybe(module Data.Maybe, module Data.Maybe_Type) whereimport qualified Prelude() -- do not import Preludeimport Primitivesimport Control.Applicativeimport Control.Errorimport Control.Monadimport Control.Monad.Failimport Data.Boolimport Data.Charimport Data.Eqimport Data.Functionimport Data.Functorimport Data.Int.Intimport Data.Listimport Data.Maybe_Typeimport Data.Monoid.Internalimport Data.Ordimport Text.Showinstance Eq a => Eq (Maybe a) where Nothing == Nothing = True Just x == Just x' = x == x' _ == _ = Falseinstance Ord a => Ord (Maybe a) where Nothing `compare` Nothing = EQ Nothing `compare` Just _ = LT Just _ `compare` Nothing = GT Just x `compare` Just y = x `compare` yinstance (Show a) => Show (Maybe a) where showsPrec _ Nothing = showString "Nothing" showsPrec p (Just a) = showParen (p >= 11) (showString "Just " . showsPrec 11 a)instance Functor Maybe where fmap _ Nothing = Nothing fmap f (Just a) = Just (f a)instance Applicative Maybe where pure = Just Just f <*> Just a = Just (f a) _ <*> _ = Nothinginstance Monad Maybe where Nothing >>= _ = Nothing Just a >>= f = f ainstance MonadFail Maybe where fail _ = Nothinginstance MonadPlus Maybe where mzero = Nothing Nothing `mplus` y = y x `mplus` _ = xinstance Alternative Maybe where empty = Nothing Nothing <|> y = y x <|> _ = xinstance Semigroup a => Semigroup (Maybe a) where Nothing <> b = b a <> Nothing = a Just a <> Just b = Just (a <> b)instance Semigroup a => Monoid (Maybe a) where mempty = Nothingmaybe :: forall a r . r -> (a -> r) -> Maybe a -> rmaybe r _ Nothing = rmaybe _ f (Just a) = f afromMaybe :: forall a . a -> Maybe a -> afromMaybe a Nothing = afromMaybe _ (Just a) = afromJust :: forall a . Maybe a -> afromJust Nothing = error "fromJust: Nothing"fromJust (Just a) = acatMaybes :: forall a . [Maybe a] -> [a]catMaybes mxs = [ x | Just x <- mxs ]isJust :: forall a . Maybe a -> BoolisJust Nothing = FalseisJust (Just _) = TrueisNothing :: forall a . Maybe a -> BoolisNothing Nothing = TrueisNothing (Just _) = FalsemapMaybe :: forall a b . (a -> Maybe b) -> [a] -> [b]mapMaybe _ [] = []mapMaybe f (a:as) = case f a of Nothing -> mapMaybe f as Just b -> b : mapMaybe f asmaybeToList :: forall a . Maybe a -> [a]maybeToList Nothing = []maybeToList (Just a) = [a]listToMaybe :: forall a . [a] -> Maybe alistToMaybe [] = NothinglistToMaybe (a:_) = Just a