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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:41:54+00:00 2026-05-27T13:41:54+00:00

Playing around with type-classes I came up with the seemingly innocent class Pair p

  • 0

Playing around with type-classes I came up with the seemingly innocent

class Pair p a | p -> a where
  one :: p -> a
  two :: p -> a

This seems to work fine, e.g.

instance Pair [a] a where
  one [x,_] = x
  two [_,y] = y 

However I run in trouble for tuples. Even though the following definition compiles…

instance Pair (a,a) a where
  one p = fst p 
  two p = snd p

… I can’t use it as I expected:

main = print $ two (3, 4)

No instance for (Pair (t, t1) a)
  arising from a use of `two' at src\Main.hs:593:15-23
Possible fix: add an instance declaration for (Pair (t, t1) a)
In the second argument of `($)', namely `two (3, 4)'
In the expression: print $ two (3, 4)
In the definition of `main': main = print $ two (3, 4)

Is there a way to define the instance correctly? Or do I have to resort to a newtype wrapper?

  • 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-27T13:41:54+00:00Added an answer on May 27, 2026 at 1:41 pm

    Your instance works just fine, actually. Observe:

    main = print $ two (3 :: Int, 4 :: Int)
    

    This works as expected. So why doesn’t it work without the type annotation, then? Well, consider the tuple’s type: (3, 4) :: (Num t, Num t1) => (t, t1). Because numeric literals are polymorphic, nothing requires them to be the same type. The instance is defined for (a, a), but the existence of that instance won’t tell GHC to unify the types (for a variety of good reasons). Unless GHC can deduce by other means that the two types are the same, it won’t choose the instance you want, even if the two types could be made equal.

    To solve your problem, you could just add type annotations, as I did above. If the arguments are coming from elsewhere it’s usually unnecessary because they’ll already be known to be the same type, but it gets clumsy quickly if you want to use numeric literals.

    An alternative solution is to note that, because of how instance selection works, having an instance for (a, a) means that you can’t write an instance like (a, b) as well even if you wanted to. So we can cheat a bit, to force the unification using the type class, like this:

    instance (a ~ b) => Pair (a,b) a where
    

    That needs the TypeFamilies extension for the ~ context, I think. What this does is allow the instance to match on any tuple at first, because instance selection ignores the context. After choosing the instance, however, the a ~ b context asserts type equality, which will produce an error if they’re different but–more importantly here–will unify the type variables if possible. Using this, your definition of main works as is, without annotations.

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

Sidebar

Related Questions

While playing around with regexps in Scala I wrote something like this: scala> val
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
I'm currently playing around with HTML_QuickForm for generating forms in PHP. It seems kind
I'm playing around with the C# reflection API. I can easily load Type information
I'm playing around with quotations and I can't see an expression pattern for type
I'm trying to start playing around with the <amp.h> library. But when I type
Whilst playing around with resources in my visual studio 10 project, I came across
I've been playing around with type deduction/printing using templates with code of the form:
I've been playing around with ASP.NET MVC 4 beta and I noticed two controllers:
I'm playing around with some HTML5 elements, and ran into a fun behavior. This

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.