I'm struggling to understand the exists
keyword in relation to Haskell type system. As far as I know, there is no such keyword in Haskell by default, but:
- There are extensions which add them, in declarations like these
data Accum a = exists s. MkAccum s (a -> s -> s) (s -> a)
- I've seen a paper about them, and (if I recall correctly) it stated that
exists
keyword is unnecessary for type system since it can be generalized byforall
But I can't even understand what exists
means.
When I say, forall a . a -> Int
, it means (in my understanding, the incorrect one, I guess) "for every (type) a
, there is a function of a type a -> Int
":
myF1 :: forall a . a -> Int
myF1 _ = 123
-- okay, that function (`a -> Int`) does exist for any `a`
-- because we have just defined it
When I say exists a . a -> Int
, what can it even mean? "There is at least one type a
for which there is a function of a type a -> Int
"? Why one would write a statement like that? What the purpose? Semantics? Compiler behavior?
myF2 :: exists a . a -> Int
myF2 _ = 123
-- okay, there is at least one type `a` for which there is such function
-- because, in fact, we have just defined it for any type
-- and there is at least one type...
-- so these two lines are equivalent to the two lines above
Please note it's not intended to be a real code which can compile, just an example of what I'm imagining then I hear about these quantifiers.
P.S. I'm not exactly a total newbie in Haskell (maybe like a second grader), but my Math foundations of these things are lacking.
Ctrl-F exists
- one occurrence, and not in the main text... But I'm reading it, thank you very much – Angelika