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

  • SEARCH
  • Home
  • 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 8042861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:43:38+00:00 2026-06-05T04:43:38+00:00

Turns out that it is surprisingly difficult to use existential/rank-n types correctly despite the

  • 0

Turns out that it is surprisingly difficult to use existential/rank-n types correctly despite the very simple idea behind them.

Why are wrapping existential types into data types is necessary?

I have the following simple example:

{-# LANGUAGE RankNTypes, ImpredicativeTypes, ExistentialQuantification #-}
module Main where

c :: Double
c = 3

-- Moving `forall` clause from here to the front of the type tuple does not help,
-- error is the same
lists :: [(Int, forall a. Show a => Int -> a)]
lists = [ (1, \x -> x)
        , (2, \x -> show x)
        , (3, \x -> c^x)
        ]

data HRF = forall a. Show a => HRF (Int -> a)

lists' :: [(Int, HRF)]
lists' = [ (1, HRF $ \x -> x)
         , (2, HRF $ \x -> show x)
         , (3, HRF $ \x -> c^x)
         ]

If I comment out the definition of lists, the code compiles successfully. If I do not comment it out, I’m getting the following errors:

test.hs:8:21:
    Could not deduce (a ~ Int)
    from the context (Show a)
      bound by a type expected by the context: Show a => Int -> a
      at test.hs:8:11-22
      `a' is a rigid type variable bound by
          a type expected by the context: Show a => Int -> a at test.hs:8:11
    In the expression: x
    In the expression: \ x -> x
    In the expression: (1, \ x -> x)

test.hs:9:21:
    Could not deduce (a ~ [Char])
    from the context (Show a)
      bound by a type expected by the context: Show a => Int -> a
      at test.hs:9:11-27
      `a' is a rigid type variable bound by
          a type expected by the context: Show a => Int -> a at test.hs:9:11
    In the return type of a call of `show'
    In the expression: show x
    In the expression: \ x -> show x

test.hs:10:21:
    Could not deduce (a ~ Double)
    from the context (Show a)
      bound by a type expected by the context: Show a => Int -> a
      at test.hs:10:11-24
      `a' is a rigid type variable bound by
          a type expected by the context: Show a => Int -> a at test.hs:10:11
    In the first argument of `(^)', namely `c'
    In the expression: c ^ x
    In the expression: \ x -> c ^ x
Failed, modules loaded: none.

Why is this happening? Shouldn’t the second example be equivalent to the first one? What is the difference between these usages of n-rank types? Is it possible at all to leave out extra ADT definition and use only simple types when I want such kind of polymorphism?

  • 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-05T04:43:40+00:00Added an answer on June 5, 2026 at 4:43 am

    There are two issues you have to consider regarding the handling of existential types:

    • If you store “anything that can be shown,” you have to store the thing that can be shown along with how to show it.
    • An actual variable can only ever have one single type.

    The first point is why you have to have existential type wrappers, because when you write this:

    data Showable = Show a => Showable a
    

    What actually happens is that something like this gets declared:

    data Showable a = Showable (instance of Show a) a
    

    I.e. a reference to the class instance of Show a is stored along with the value a. Without this happening, you can’t have a function that unwraps a Showable, because that function wouldn’t know how to show the a.

    The second point is why you get some type quirkiness sometimes, and why you can’t bind existential types in let bindings, for example.


    You also have an issue with your reasoning in your code. If you have a function like forall a . Show a => (Int -> a) you get a function that, given any integer, will be able to produce any kind of showable value that the caller chooses. You probably mean exists a . Show a => (Int -> a), meaning that if the function gets an integer, it will return some type for which there exists a Show instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently asked how to solve a simple SQL query . Turns out that
[EDITED to add: It turns out that the answer is very boring and has
It turns out that in order to do this one has to bind the
I have an application that uses RPC for interprocess communications. Turns out that synchronous
I resolved this issue myself. It turns out that the activation framework requires some
I wrote a small app that turns out to be using a lot of
Solution Edit: Turns out you can't use the PHP SDK to return the correct
Update: Turns out that this problem was because half my mongrel did not restart.
It turns out that the following example works when using mysql 5.x, however it
EDIT: SORRY! Turns out that I'm an idiot. The exception was being thrown from

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.