In type theory, the existential quantifier, $\exists$ is the dual to the more familiar universal quantifier, $\forall$ (which expresses parametric polymorphism). Believe it or not, these existentially quantified types can be useful in day-to-day programming in languages like Haskell where you wish to perform some kind of “information hiding”. As a simple example, consider this program:

We start off by declaring a datatype, Showable, with a single constructor, MkShowable that has type $\forall \alpha. (\text{S}\text{how}~\alpha) \Rightarrow \alpha \rightarrow \text{S}\text{how}\text{able}$. This is to say, it injects any value with a Show instance into the Showable type.

This is really an existentially quantified datatype. However, many newbies are confused by this pronouncement, as it quite clearly uses the forall keyword, which introduces universal quantification - I’ll come back to that at the end of this article.

This next chunk of code declares that any value of type Showable is itself a member of the Show type class. We implement it by unpacking the Showable value and applying show “recursively” to that value. We are able to do this precisely because of the constraint in the MkShowable constructor that specified that whatever type gets injected into Showable must have a Show instance somewhere.

An aside: this construction has something of the flavour of object orientation, as it implements a uniform interface using dynamic dispatch. I’ll leave it up to the inimitable Oleg to expand on this point

Here we make use of the marvellous information-hiding properties of the existential to create a list holding values of varying types. All we can know about the elements of the resulting list is that they certainly have a Show instance.

This last declaration is just a bit of top-level gunk that outputs all the things in that list to the screen using the Show typeclass.

So, that’s a pretty interesting technique. But in precisely what sense is it existential, given that we got all this going by using the universal quantifier (the forall keyword)? I think the easiest way to look at this problem is using the marvellously useful Curry-Howard correspondence between types and terms in a logic system.

First, remember than the function arrow at the type level ($\tau_1 \rightarrow \tau_2$) just corresponds to implication in proofs, and the quantifiers carry through unchanged. Secondly, remember that in the proof system corresponding to simple ML-style types (second-order intuionistic logic) $\sigma_1 \rightarrow \sigma_2 \equiv \lnot \sigma_1 \lor \sigma_2$. Thirdly, recall that if $\alpha$ is free in $\sigma_2$ then $\forall \alpha. \sigma_1 \lor \sigma_2 \equiv (\forall \alpha. \sigma_1) \lor \sigma_2$. Finally, you need to know that pushing a negation through an existiental or universal quantifier flips the kind of quantifier we have (the statement that an $x$ does not exist satisfying $P$ is equivalent to saying that all $x$s do not satisfy $P$). Now we can gradually manipulate the type of a simple data constructor function into a form that makes it obvious where the existiential comes from. Let’s try this example (which doesn’t have any typeclasses, since they don’t really fit into the classical correspondence):

So MkK has type $\forall \alpha. \alpha \rightarrow K$. Let’s push that into the realm of logic and proof and see what we can do:

And we’re done! We can extract this last proof term back out as a type in our source language without any fuss, and hence from only a few logical rules (which, if you didn’t know them, are fairly easy to convince yourself of) we have derived why these constructs are known as “existential”. In fact, some Haskell compilers once supported the exists keywoard to let you declare them directly, but due to precisely this correspondence between the two quantifiers GHC dropped the extra keyword in favour of just using the forall to do both jobs.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8869162,"math_prob":0.95520574,"size":6007,"snap":"2023-40-2023-50","text_gpt3_token_len":1354,"char_repetition_ratio":0.1182742,"word_repetition_ratio":0.014271152,"special_character_ratio":0.21824539,"punctuation_ratio":0.088607594,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97966087,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-03T17:34:50Z\",\"WARC-Record-ID\":\"<urn:uuid:2fc1c4eb-a9ff-463c-9d08-e428afac1c10>\",\"Content-Length\":\"16329\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b7455c0c-b040-4629-bebd-36a1db169358>\",\"WARC-Concurrent-To\":\"<urn:uuid:525af133-f453-43a4-afd6-9dc0a90a43b2>\",\"WARC-IP-Address\":\"208.94.117.153\",\"WARC-Target-URI\":\"http://blog.omega-prime.co.uk/2008/11/12/bitesize-functional-programming-universals-as-existentials/\",\"WARC-Payload-Digest\":\"sha1:O3YJP237JP6DGYPTM7S5SFJ4JO2FX4KJ\",\"WARC-Block-Digest\":\"sha1:22TJR2CRYUSLBNLVC6HZUQZ3YK7WRTMV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100508.42_warc_CC-MAIN-20231203161435-20231203191435-00857.warc.gz\"}"}