some-1.0.6: Existential type: Some

Safe Haskell | Safe
Language | Haskell2010

Data.Some.GADT

Synopsis

- data Some (tag :: k -> Type) where
  - Some :: forall {k} (tag :: k -> Type) (a :: k). tag a -> Some tag
- mkSome :: forall {k} tag (a :: k). tag a -> Some tag
- withSome :: Some tag -> (forall (a :: k). tag a -> b) -> b
- withSomeM :: forall {k} m tag r. Monad m => m (Some tag) -> (forall (a :: k). tag a -> m r) -> m r
- mapSome :: (forall (x :: k). f x -> g x) -> Some f -> Some g
- foldSome :: (forall (a :: k). tag a -> b) -> Some tag -> b
- traverseSome :: forall {k} m f g. Functor m => (forall (a :: k). f a -> m (g a)) -> Some f -> m (Some g)

# Documentation

data Some (tag :: k -> Type) where Source #

Existential. This is type is useful to hide GADTs' parameters.

    >>> data Tag :: Type -> Type where TagInt :: Tag Int; TagBool :: Tag Bool
    >>> instance GShow Tag where gshowsPrec _ TagInt = showString "TagInt"; gshowsPrec _ TagBool = showString "TagBool"
    >>> classify s = case s of "TagInt" -> [mkGReadResult TagInt]; "TagBool" -> [mkGReadResult TagBool]; _ -> []
    >>> instance GRead Tag where greadsPrec _ s = [ (r, rest) | (con, rest) <-  lex s, r <- classify con ]

You can either use constructor:

    >>> let x = Some TagInt
    >>> x
    Some TagInt

    >>> case x of { Some TagInt -> "I"; Some TagBool -> "B" } :: String
    "I"

or you can use functions

    >>> let y = mkSome TagBool
    >>> y
    Some TagBool

    >>> withSome y $ \y' -> case y' of { TagInt -> "I"; TagBool -> "B" } :: String
    "B"

The implementation of `mapSome` is safe.

    >>> let f :: Tag a -> Tag a; f TagInt = TagInt; f TagBool = TagBool
    >>> mapSome f y
    Some TagBool

but you can also use:

    >>> withSome y (mkSome . f)
    Some TagBool

    >>> read "Some TagBool" :: Some Tag
    Some TagBool

    >>> read "mkSome TagInt" :: Some Tag
    Some TagInt

Constructors

Some :: forall {k} (tag :: k -> Type) (a :: k). tag a -> Some tag |

#### Instances

Instances details

GNFData tag => NFData (Some tag) Source # |
Instance details Defined in Data.Some.GADT Methods rnf :: Some tag -> () Source #
Applicative m => Monoid (Some m) Source # |
Instance details Defined in Data.Some.GADT Methods mempty :: Some m Source # mappend :: Some m -> Some m -> Some m Source # mconcat :: [Some m] -> Some m Source #
Applicative m => Semigroup (Some m) Source # |
Instance details Defined in Data.Some.GADT Methods (<>) :: Some m -> Some m -> Some m Source # sconcat :: NonEmpty (Some m) -> Some m Source # stimes :: Integral b => b -> Some m -> Some m Source #
GRead f => Read (Some f) Source # |
Instance details Defined in Data.Some.GADT Methods readsPrec :: Int -> ReadS (Some f) Source # readList :: ReadS [Some f] Source # readPrec :: ReadPrec (Some f) Source # readListPrec :: ReadPrec [Some f] Source #
GShow tag => Show (Some tag) Source # |
Instance details Defined in Data.Some.GADT Methods showsPrec :: Int -> Some tag -> ShowS Source # show :: Some tag -> String Source # showList :: [Some tag] -> ShowS Source #
GEq tag => Eq (Some tag) Source # |
Instance details Defined in Data.Some.GADT Methods (==) :: Some tag -> Some tag -> Bool Source # (/=) :: Some tag -> Some tag -> Bool Source #
GCompare tag => Ord (Some tag) Source # |
Instance details Defined in Data.Some.GADT Methods compare :: Some tag -> Some tag -> Ordering Source # (<) :: Some tag -> Some tag -> Bool Source # (<=) :: Some tag -> Some tag -> Bool Source # (>) :: Some tag -> Some tag -> Bool Source # (>=) :: Some tag -> Some tag -> Bool Source # max :: Some tag -> Some tag -> Some tag Source # min :: Some tag -> Some tag -> Some tag Source #

mkSome :: forall {k} tag (a :: k). tag a -> Some tag Source #

Constructor.

withSome :: Some tag -> (forall (a :: k). tag a -> b) -> b Source #

Eliminator.

withSomeM :: forall {k} m tag r. Monad m => m (Some tag) -> (forall (a :: k). tag a -> m r) -> m r Source #

Monadic `withSome`.

Since: 1.0.1

mapSome :: (forall (x :: k). f x -> g x) -> Some f -> Some g Source #

Map over argument.

foldSome :: (forall (a :: k). tag a -> b) -> Some tag -> b Source #

    flip withSome

traverseSome :: forall {k} m f g. Functor m => (forall (a :: k). f a -> m (g a)) -> Some f -> m (Some g) Source #

Traverse over argument.