Talk:Haskell/Existentially quantified types
From Wikibooks, the open-content textbooks collection
A fellow Haskeller, Daniel G. recently explained this stuff to me. These are my notes. I hope to turn this into a tutorial module someday. Please be bold and flesh it out yourself if I'm taking too long! -- Kowey 14:33, 20 June 2006 (UTC)
Contents |
[edit] Things I understand less well
- Existential types
- Witness types?
- Haskell/GADT?
- Rank 2 polymorphism
- Rank N polymorphism
[edit] Existential/Universal
We ought to do more to clarify the difference between a universal type and an existential type. The fact that we use forall to express existential types is super-confusing, and so a few gentle words about that would be quite useful. -- Kowey 09:23, 6 July 2006 (UTC)
- Nice explanation by Brent Yorgey
- -- apfeλmus 10:29, 24 January 2009 (UTC)
I still can't understand the difference between the standard version of existential datatype wrapper:
data Opaque = forall a. O a
and this version with the forall inside:
data Opaque = O (forall a. a)
Is the second version non existential, but isomorphic to a rank-2 universally quantified type instead?
That is my guess. With this in mind, terms of the datatype:
data Test = Test (forall a . [a -> a])
would contain only lists of functions that are fully parametric in a, like the id function
Whereas terms of the datatype:
data Test = forall a . Test [a -> a]
would contain heterogeneous lists of functions that folllow the regular type scheme: forall a . a -> a
Is that by any chance right? --Pepe 17:22, 16 November 2006 (UTC)
[edit] Superfluous Stuff
This code in the middle section...
main = mapM_ print hetroList
print :: Show s => s -> IO () -- print x = putStrLn (show x)
mapM_ :: (a -> m b) -> [a] -> m ()
mapM_ print :: Show s => [s] -> IO ()
...seems overly verbose and distracts from the main point. It should be changed to...
main = print hetroList
[edit] Heterogeneous list example doesn't work
Hi, i've tried to implement an heterogeneous list example: (as in that version of the page)
instance Show ShowH where show (SH s) = show s
The problem is that GHC complains that it doesn't know if the 2nd "show" on the line is the one of Prelude or the one of SH. I've fixed the problem by using a function with another name for SH, but is there a more elegant solution ? Gh 22:49, 24 March 2007 (UTC)
You just need to indent the second line properly.
[edit] Attribute lists (wxhaskell)
I thought I should note down the wxHaskell article as a potentially good example for using existential types (for lists of attributes, section 6.2) -- Kowey (talk) 15:05, 27 February 2008 (UTC)
[edit] Makes no sense / is pointless!
The example shows something, that exists, but in reality, I've yet to see a keyword “forall”, because that what is explained works without any such keyword:
Or in other words: It is not shown to be any difference between…
map :: (a -> b) -> [a] -> [b]
and
map :: forall a b. (a -> b) -> [a] -> [b]
. What’s the point of that keyword? That most important part is completely missing.
People who did never use Haskell might think, that it does something different. But as all the examples show something that works just like that without the forall keyword (first line above), what would that be? Fancy words like it being "universally quantified" don't mean anything, because from what we are shown, and what is explained everywhere in standard Haskell tutorials and books, the version without the forall does exactly that. So it is not showing any point of the forall keyword.
The whole article needs a major overhaul. It missed the only point of its existence completely. My guess is, that the writer did not understand normal polymorphism in the first place.
— 88.77.134.245 (talk) 13:40, 8 October 2009 (UTC)
- The versions with and without forall do indeed mean the same. However, the latter is best thought of as an abbreviation of the former. More details in Haskell/Polymorphism (unfinished).
- I agree that the chapter on existentially quantified types needs an overhaul. In particular, I intend the beginning of Haskell/Polymorphism to serve as prerequisite.
- -- apfeλmus 13:29, 9 October 2009 (UTC)