[Haskell-beginners] Existential data types

Michael Snoyman michael at snoyman.com
Mon Dec 15 12:49:38 EST 2008

Hi everyone,

I believe I have come to the conclusion that what I would like to do is
impossible, but I would like to have that confirmed. I would basically like
to be able to have a heterogeneous list without boxing everything in a data
type. Below is the sample code, with the code I would like to use commented
out. I'm I missing something, or does Haskell simply not support what I am
trying for?

{-# LANGUAGE ExistentialQuantification #-}

data Foo = Foo String
class IFoo a where
    toFoo :: a -> Foo
instance IFoo Foo where
    toFoo = id

data A = A String
instance IFoo A where
    toFoo (A a) = Foo a

data B = B Int
instance IFoo B where
    toFoo (B b) = Foo $ show b

data FooBox = forall t. IFoo t => FooBox t
instance IFoo FooBox where
    toFoo (FooBox f) = toFoo f

myPrint :: IFoo t => [(String, t)] -> IO ()
myPrint = mapM_ myPrint'

myPrint' :: IFoo t => (String, t) -> IO ()
myPrint' (key, value) =
    let (Foo foo) = toFoo value
     in putStrLn $ key ++ ": " ++ foo

{- What I'd like to do:
main = myPrint
    [ ("one", Foo "1")
    , ("two", A "2")

main = myPrint
    [ ("one", FooBox $ Foo "1")
    , ("two", FooBox $ A "2")


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20081215/05e6c5ee/attachment.htm

More information about the Beginners mailing list