Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Synopsis
- newtype Some tag = S {
- withSome :: forall r. (forall a. tag a -> r) -> r
- mkSome :: tag a -> Some tag
- mapSome :: (forall x. f x -> g x) -> Some f -> Some g
- withSomeM :: Monad m => m (Some tag) -> (forall a. tag a -> m r) -> m r
- foldSome :: (forall a. tag a -> b) -> Some tag -> b
- traverseSome :: Functor m => (forall a. f a -> m (g a)) -> Some f -> m (Some g)
Documentation
Existential. This is type is useful to hide GADTs' parameters.
>>>
data Tag :: * -> * 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 ]
With Church-encoding youcan only use a functions:
>>>
let y = mkSome TagBool
>>>
y
mkSome TagBool
>>>
withSome y $ \y' -> case y' of { TagInt -> "I"; TagBool -> "B" } :: String
"B"
or explicitly work with S
>>>
let x = S $ \f -> f TagInt
>>>
x
mkSome TagInt
>>>
case x of S f -> f $ \x' -> case x' of { TagInt -> "I"; TagBool -> "B" } :: String
"I"
The implementation of mapSome
is safe.
>>>
let f :: Tag a -> Tag a; f TagInt = TagInt; f TagBool = TagBool
>>>
mapSome f y
mkSome TagBool
but you can also use:
>>>
withSome y (mkSome . f)
mkSome TagBool
>>>
read "Some TagBool" :: Some Tag
mkSome TagBool
>>>
read "mkSome TagInt" :: Some Tag
mkSome TagInt