fork(1) download
  1. {-# LANGUAGE ExistentialQuantification #-}
  2. {-# LANGUAGE FlexibleInstances #-}
  3.  
  4. data Circle = Circle Int deriving Show
  5. data Square = Square Int deriving Show
  6.  
  7. class Drawable a where
  8. draw :: a -> IO ()
  9.  
  10. instance Drawable Circle where
  11. draw (Circle x) = print $ ("I'm circle " ++ show x)
  12.  
  13. instance Drawable Square where
  14. draw (Square x) = print $ ("I'm square " ++ show x)
  15.  
  16. data Proxy = forall a. Drawable a => Proxy a
  17. instance Drawable Proxy where
  18. draw (Proxy a) = draw a
  19.  
  20. instance Drawable [Proxy] where
  21. draw = mapM_ draw
  22.  
  23. main = do
  24. draw [Proxy $ Circle 42, Proxy $ Square 100500]
  25.  
Success #stdin #stdout 0s 5704KB
stdin
Standard input is empty
stdout
"I'm circle 42"
"I'm square 100500"