{-|
Description : Type constructors which can be turned into 'Maybe'.

Type constructors which can be turned into 'Maybe'.
-}
module Data.Functor.MaybeLike where

import Data.Functor.Identity

-- | This class may be defined in two ways:
--
--   * Type constructors which can be turned into 'Maybe' generically.
--   * Type constructors which admit a natural transformation to 'Maybe'.
--
--   We expect the following rules to hold for those
--   instances of 'MaybeLike' which are also 'Control.Applicative.Alternative':
--
--   * @likeMaybe empty = empty = Nothing@
--   * @likeMaybe (x <|> y) = likeMaybe x <|> likeMaybe y@
class MaybeLike f where
  likeMaybe :: f a -> Maybe a

instance MaybeLike Identity where
  likeMaybe :: Identity a -> Maybe a
likeMaybe = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> (Identity a -> a) -> Identity a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity a -> a
forall a. Identity a -> a
runIdentity
instance MaybeLike Maybe where
  likeMaybe :: Maybe a -> Maybe a
likeMaybe = Maybe a -> Maybe a
forall a. a -> a
id
instance MaybeLike (Either a) where
  likeMaybe :: Either a a -> Maybe a
likeMaybe (Left  _) = Maybe a
forall a. Maybe a
Nothing
  likeMaybe (Right y :: a
y) = a -> Maybe a
forall a. a -> Maybe a
Just a
y