Existential types
- Existential quantification
- Existential types
- Module abstraction
- Dependent pairs
- Encoding in universal types
ExistentialQuantification
in Haskell
Haskell
{-# LANGUAGE RankNTypes #-}
{-
data ToString = forall a. ToString (a -> String, a)
-}
{-
data ToString where
ToString :: (a -> String) -> a -> ToString
-}
newtype ToString = ToString (forall z . (forall a. (a -> String) -> a -> z) -> z)
toString :: (a -> String) -> a -> ToString
= ToString $ \f -> f s a
toString s a
foo :: [ToString]
= [toString id "he", toString show 11, toString show ()]
foo
main :: IO ()
= putStrLn $ foldr (\(ToString t) acc -> t (\s a -> s a) ++ acc) "!" foo main