
On 26 August 2010 08:08, Stephen Tetley
Hi Drew
Bear in mind though that existentials are not equivalent to subtyping in OO.
For instance, with example 2.1 from [1] all you can do with an Obj is show it, so for the list xs all you can do is show the elements:
data Obj = forall a. (Show a) => Obj a
xs :: [Obj] xs = [Obj 1, Obj "foo", Obj 'c']
Because Obj is an existential you can't do an case analysis on it - so you can't write a function like this:
add_one_if_int (Obj (n::Int)) = Obj (n+1) add_one_if_int (Obj other) = Obj other
There really is nothing you can do with Obj other than show it.
But that's because you use Show while defining the Obj data type. You can implement other functionalities, by introducing a custom type class, and implementing functionalities in instance declarations. class Show a => CustomTC a where add_one_if_int :: a -> a instance CustomTC Int where add_one_if_int x = x + 1 instance CustomTC Char where add_one_if_int = id instance CustomTC String where add_one_if_int = id xs :: [Obj] xs = [Obj (1 :: Int), Obj "foo", Obj 'c'] xs' :: [Obj] xs' = map (\ (Obj i) -> Obj (add_one_if_int i) ) xs -- xs' = [Obj 2,Obj "foo",Obj 'c'] If you are trying to transliterate OO designs, you might quickly find
existentials are too inert to be useful.
Best wishes
Stephen
[1] http://www.haskell.org/haskellwiki/Existential_type
On 26 August 2010 07:45, Drew Haven
wrote: I think I found the answers to all my questions at http://www.haskell.org/haskellwiki/Existential_type
Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Ozgur Akgun