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 6605637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:18:27+00:00 2026-05-25T19:18:27+00:00

First attempt It’s difficult to make this question pithy, but to provide a minimal

  • 0

First attempt

It’s difficult to make this question pithy, but to provide a minimal example, suppose I have this type:

{-# LANGUAGE GADTs #-}
data Val where
  Val :: Eq a => a -> Val

This type lets me happily construct the following heterogeneous-looking list:

l = [Val 5, Val True, Val "Hello!"]

But, alas, when I write down an Eq instance, things go wrong:

instance Eq Val where
  (Val x) == (Val y) = x == y -- type error

Ah, so we Could not deduce (a1 ~ a). Quite right; there’s nothing in the definition that says x and y must be the same type. In fact, the whole point was to allow the possibility that they differ.

Second attempt

Let’s bring Data.Typeable into the mix, and only try comparing the two if they happen to be the same type:

data Val2 where
  Val2 :: (Eq a, Typeable a) => a -> Val2

instance Eq Val2 where
  (Val2 x) == (Val2 y) = fromMaybe False $ (==) x <$> cast y

This is pretty nice. If x and y are the same type, it uses the underlying Eq instance. If they differ, it just returns False. However, this check is delayed until runtime, allowing nonsense = Val2 True == Val2 "Hello" to typecheck without complaint.

Question

I realize I’m flirting with dependent types here, but is it possible for the Haskell type system to statically reject something like the above nonsense, while allowing something like sensible = Val2 True == Val2 False to hand back False at runtime?

The more I work with this problem, the more it seems I need to adopt some of the techniques of HList to implement the operations I need as type-level functions. However, I am relatively new to using existentials and GADTs, and I am curious to know whether there’s a solution to be found just with these. So, if the answer is no, I’d very much appreciate a discussion of exactly where this problem hits the limit of those features, as well as a nudge toward appropriate techniques, HList or otherwise.

  • 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-05-25T19:18:28+00:00Added an answer on May 25, 2026 at 7:18 pm

    So you want a constructor that allows you to use heterogeneous types, but you want comparisons between heterogeneous types that are knowable at compile time to be rejected. As in:

    Val True == Val "bar"  --> type error
    
    allSame [] = True
    allSame (x:xs) = all (== x) xs
    
    allSame [Val True, Val "bar"]  --> False
    

    But surely:

    (x == y) = allSame [x,y]
    

    So I’m pretty sure a function with satisfies these constraints would violate some desirable property of a type system. Doesn’t it look like that to you? I am strongly guessing “no, you can’t do that”.

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

Sidebar

Related Questions

This is my first attempt to use boost::threads and I have a silly question.
This is my first attempt at a JOIN MySQL statement... I have 2 tables..
This is my first attempt at a plugin but I think I'm missing the
This is my first attempt at using Backbone.js, so I decided to make a
This is my very first attempt at including ads in my app. I have
This is my first attempt at java problem I have been given as part
In my first attempt at an MVC web application I have a fundamental question:
This is my first attempt at CGI (I know some Perl) but i'm falling
Question arising from my first attempt at using an Async object. I have a
First attempt to use this cool site - after searching for 2 hours: So

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.