[Haskell-cafe] existential quantification
Andras Slemmer
0slemi0 at gmail.com
Mon Dec 2 19:50:36 UTC 2013
Just expanding on Brandon's answer: DeMorgan's law he's referring to goes
like this:
∀a.P(a) === ¬∃a.¬P(a) where 'a' is a sentence, so P is second order
A special case of this is this:
∀a.(R(a) -> Q) === ¬∃a.¬(R(a) -> Q) === ¬∃a.(R(a)∧¬Q) === ¬((∃a.R(a))∧¬Q)
=== (∃a.R(a)) -> Q (i added extra parantheses for emphasis)
So what does this mean in terms of haskell? R(a) is your data definition's
"body", and Q is the type you are defining. On the lhs the universally
quantified version gives you the type of the constuctor you're defining,
and on the rhs the existential tells you what you're constructing the type
with.
Or in other words the universal version says: For any 'a' give me an R(a)
and i'll give you back a Q.
The existential version says: If you have some 'a' for which R(a) i'll give
you back a Q. (It's hard to phrase the difference without sounding stupid,
they are equivalent after all).
There are of course other considerations, for example introducing 'exists'
would mean another keyword in the syntax.
Having said that I think that the choice of 'forall' for
-XExistentialQuantification is wrong, as the data body defines the type
you're constructing with, not the type of the whole constructor. HOWEVER
for -XGADTs forall makes perfect sense. Compare the following:
data AnyType = forall a. AnyType a
data AnyType where
AnyType :: forall a. a -> AnyType
These two definitions are operationally identical, but I think the GADT way
is the one that actually corresponds to the DeMorgan law.
On 2 December 2013 22:07, TP <paratribulations at free.fr> wrote:
> Hi everybody,
>
>
> I try to understand existential quantification. I have two questions.
>
> 1/ I have just read the answer of yairchu at
>
>
> http://stackoverflow.com/questions/3071136/what-does-the-forall-keyword-in-haskell-ghc-do
>
> He writes:
>
> """
> So with Existential-Quantification, foralls in data definitions mean that,
> the value contained *can* be of *any* suitable type, not that it *must* be
> of *all* suitable types.
> """
>
> This made me think to an error I obtained with the code:
> ---------------
> test :: Show s => s
> test = "foobar"
> ---------------
>
> The error is:
>
> Could not deduce (s ~ [Char])
> from the context (Show s)
> bound by the type signature for test :: Show s => s
> [...]
> `s' is a rigid type variable bound by
> the type signature for test :: Show s => s
>
> Indeed, `test :: Show s => s` means "for any type s which is an instance of
> Show, test is a value of that type s". But for example "foobar" can't be an
> Int that is an instance of Show, so it yields an error.
> By comparison,
>
> ---------------
> test :: Num a => a
> test = 42
> ---------------
>
> works because 42 can be a value of type Int or Integer or Float or anything
> else that is an instance of Num.
> So I thought that by using existential quantification, the first example
> could work:
>
> ---------------
> {-# LANGUAGE ExistentialQuantification #-}
>
> test :: forall s . Show s => s
> test = "asd"
> ---------------
>
> But I obtain the same error, why?
>
> 2/
> Is the notion of existential type related in some way to the classical
> mathematical quantifier "∃" (Unicode symbol U+2203: "There exists")?
> If yes, then why using "forall" for an "existential type"?
>
> At the following address
>
>
> http://www.haskell.org/ghc/docs/6.12.1/html/users_guide/data-type-extensions.html#existential
>
> we read
>
> """
> 7.4.4.1. Why existential?
>
> What has this to do with existential quantification? Simply that MkFoo has
> the (nearly) isomorphic type
>
> MkFoo :: (exists a . (a, a -> Bool)) -> Foo
>
> But Haskell programmers can safely think of the ordinary universally
> quantified type given above, thereby avoiding adding a new existential
> quantification construct.
> """
>
> But I don't understand the explanation.
>
> Thanks in advance,
>
> TP
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20131202/8f02b54d/attachment-0001.html>
More information about the Haskell-Cafe
mailing list