-- | Demonstration of an existentially-qualified type class module Main (main) where import Data.Constraint (Dict (Dict)) -- constraints import Data.Kind (Type, Constraint) -- base -- A variant that contains a constraint and a term of unknown type. data Z (c :: Type -> Constraint) = forall t. ZY (Dict (c t)) t | forall t. ZN t -- Simpler variant that only carries a constraint data W (c :: Constraint) = WY (Dict c) | WN -- | Show if possible, determined dynamically. sz :: Z Show -> String sz (ZY Dict x) = show x sz (ZN _) = "<>" -- | -- @ -- >>> main -- ["2","()","<>"] -- @ main :: IO () main = do let zs :: [Z Show] = [ ZY Dict (2 :: Int) , ZY Dict () , ZN (const :: Int -> Int -> Int) ] print . map sz $ zs