Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:14:49+00:00 2026-06-15T07:14:49+00:00

We’re used to having universally quantified types for polymorphic functions. Existentially quantified types are

  • 0

We’re used to having universally quantified types for polymorphic functions. Existentially quantified types are used much less often. How can we express existentially quantified types using universal type quantifiers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:14:50+00:00Added an answer on June 15, 2026 at 7:14 am

    It turns out that existential types are just a special case of Σ-types (sigma types). What are they?

    Sigma types

    Just as Π-types (pi types) generalise our ordinary function types, allowing the resulting type to depend on the value of its argument, Σ-types generalise pairs, allowing the type of second component to depend on the value of the first one.

    In a made-up Haskell-like syntax, Σ-type would look like this:

    data Sigma (a :: *) (b :: a -> *)
        = SigmaIntro
            { fst :: a
            , snd :: b fst
            }
    
    -- special case is a non-dependent pair
    type Pair a b = Sigma a (\_ -> b)
    

    Assuming * :: * (i.e. the inconsistent Set : Set), we can define exists a. a as:

    Sigma * (\a -> a)
    

    The first component is a type and the second one is a value of that type. Some examples:

    foo, bar :: Sigma * (\a -> a)
    foo = SigmaIntro Int  4
    bar = SigmaIntro Char 'a'
    

    exists a. a is fairly useless – we have no idea what type is inside, so the only operations that can work with it are type-agnostic functions such as id or const. Let’s extend it to exists a. F a or even exists a. Show a => F a. Given F :: * -> *, the first case is:

    Sigma * F   -- or Sigma * (\a -> F a)
    

    The second one is a bit trickier. We cannot just take a Show a type class instance and put it somewhere inside. However, if we are given a Show a dictionary (of type ShowDictionary a), we can pack it with the actual value:

    Sigma * (\a -> (ShowDictionary a, F a))
    -- inside is a pair of "F a" and "Show a" dictionary
    

    This is a bit inconvenient to work with and assumes that we have a Show dictionary around, but it works. Packing the dictionary along is actually what GHC does when compiling existential types, so we could define a shortcut to have it more convenient, but that’s another story. As we will learn soon enough, the encoding doesn’t actually suffer from this problem.


    Digression: thanks to constraint kinds, it’s possible to reify the type class into concrete data type. First, we need some language pragmas and one import:

    {-# LANGUAGE ConstraintKinds, GADTs, KindSignatures  #-}
    import GHC.Exts -- for Constraint
    

    GADTs already give us the option to pack a type class along with the constructor, for example:

    data BST a where
        Nil  :: BST a
        Node :: Ord a => a -> BST a -> BST a -> BST a
    

    However, we can go one step further:

    data Dict :: Constraint -> * where
        D :: ctx => Dict ctx
    

    It works much like the BST example above: pattern matching on D :: Dict ctx gives us access to the whole context ctx:

    show' :: Dict (Show a) -> a -> String
    show' D = show
    
    (.+) :: Dict (Num a) -> a -> a -> a
    (.+) D = (+)
    

    We also get quite natural generalisation for existential types that quantify over more type variables, such as exists a b. F a b.

    Sigma * (\a -> Sigma * (\b -> F a b))
    -- or we could use Sigma just once
    Sigma (*, *) (\(a, b) -> F a b)
    -- though this looks a bit strange
    

    The encoding

    Now, the question is: can we encode Σ-types with just Π-types? If yes, then the existential type encoding is just a special case. In all glory, I present you the actual encoding:

    newtype SigmaEncoded (a :: *) (b :: a -> *)
        = SigmaEncoded (forall r. ((x :: a) -> b x -> r) -> r)
    

    There are some interesting parallels. Since dependent pairs represent existential quantification and from classical logic we know that:

    (∃x)R(x) ⇔ ¬(∀x)¬R(x) ⇔ (∀x)(R(x) → ⊥) → ⊥
    

    forall r. r is almost ⊥, so with a bit of rewriting we get:

    (∀x)(R(x) → r) → r
    

    And finally, representing universal quantification as a dependent function:

    forall r. ((x :: a) -> R x -> r) -> r
    

    Also, let’s take a look at the type of Church-encoded pairs. We get a very similar looking type:

    Pair a b  ~  forall r. (a -> b -> r) -> r
    

    We just have to express the fact that b may depend on the value of a, which we can do by using dependent function. And again, we get the same type.

    The corresponding encoding/decoding functions are:

    encode :: Sigma a b -> SigmaEncoded a b
    encode (SigmaIntro a b) = SigmaEncoded (\f -> f a b)
    
    decode :: SigmaEncoded a b -> Sigma a b
    decode (SigmaEncoded f) = f SigmaIntro
    -- recall that SigmaIntro is a constructor
    

    The special case actually simplifies things enough that it becomes expressible in Haskell, let’s take a look:

    newtype ExistsEncoded (F :: * -> *)
        = ExistsEncoded (forall r. ((x :: *) -> (ShowDictionary x, F x) -> r) -> r)
        -- simplify a bit
        = ExistsEncoded (forall r. (forall x. (ShowDictionary x, F x) -> r) -> r)
        -- curry (ShowDictionary x, F x) -> r
        = ExistsEncoded (forall r. (forall x. ShowDictionary x -> F x -> r) -> r)
        -- and use the actual type class
        = ExistsEncoded (forall r. (forall x. Show x => F x -> r) -> r)
    

    Note that we can view f :: (x :: *) -> x -> x as f :: forall x. x -> x. That is, a function with extra * argument behaves as a polymorphic function.

    And some examples:

    showEx :: ExistsEncoded [] -> String
    showEx (ExistsEncoded f) = f show
    
    someList :: ExistsEncoded []
    someList = ExistsEncoded $ \f -> f [1]
    
    showEx someList == "[1]"
    

    Notice that someList is actually constructed via encode, but we dropped the a argument. That’s because Haskell will infer what x in the forall x. part you actually mean.

    From Π to Σ?

    Strangely enough (although out of the scope of this question), you can encode Π-types via Σ-types and regular function types:

    newtype PiEncoded (a :: *) (b :: a -> *)
        = PiEncoded (forall r. Sigma a (\x -> b x -> r) -> r)
    -- \x -> is lambda introduction, b x -> r is a function type
    -- a bit confusing, I know
    
    encode :: ((x :: a) -> b x) -> PiEncoded a b
    encode f = PiEncoded $ \sigma -> case sigma of
        SigmaIntro a bToR -> bToR (f a)
    
    decode :: PiEncoded a b -> (x :: a) -> b x
    decode (PiEncoded f) x = f (SigmaIntro x (\b -> b))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
We're building an app, our first using Rails 3, and we're having to build
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.