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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:41:06+00:00 2026-06-03T05:41:06+00:00

I was playing around with the FunctionalDependencies-Extension of Haskell, along with MultiParamTypeClasses. I defined

  • 0

I was playing around with the FunctionalDependencies-Extension of Haskell, along with MultiParamTypeClasses. I defined the following:

class Add a b c | a b -> c where
    (~+) :: a -> b -> c
    (~-) :: a -> b -> c
    neg :: a -> a
    zero :: a

which works fine (I’ve tried with instances for Int and Double with the ultimate goal of being able to add Int and Doubles without explicit conversion).

When I try to define default implementations for neg or (~-) like so:

class Add ...
    ...
    neg n = zero ~- n

GHCi (7.0.4) tells me the following:

Ambiguous type variables `a0', `b0', `c0' in the constraint:
  (Add a0 b0 c0) arising from a use of `zero'
Probable fix: add a type signature that fixes these type variable(s)
In the first argument of `(~-)', namely `zero'
In the expression: zero ~- n
In an equation for `neg': neg n = zero ~- n

Ambiguous type variable `a0' in the constraint:
  (Add a0 a a) arising from a use of `~-'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: zero ~- n
In an equation for `neg': neg n = zero ~- n

I think I do understand the problem here. GHC does not know which zero to use, since it could be any zero yielding anything which in turn is fed into a ~- which we only know of, that it has an a in it’s right argument and yields an a.

So how can I specify that it should be the zero from the very same instance, i.e. how can I express something like:

neg n = (zero :: Add a b c) ~- n

I think the a, b and c here are not the a b c form the surrounding class, but any a b and c, so how can I express a type which is a reference to the local type variables?

  • 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-03T05:41:07+00:00Added an answer on June 3, 2026 at 5:41 am

    Pull neg and zero out into a superclass that only uses the one type:

    class Zero a where
        neg :: a -> a
        zero :: a
    
    class Zero a => Add a b c | a b -> c where
        (~+) :: a -> b -> c
        (~-) :: a -> b -> c
    

    The point is that your way, zero :: Int could be the zero from Add Int Int Int, or the zero from Add Int Double Double, and there is no way to disambiguate between the two, regardless of whether you’re referring to it from inside a default implementation or an instance declaration or normal code.

    (You may object that the zero from Add Int Int Int and the zero from Add Int Double Double will have the same value, but the compiler can’t know that someone isn’t going to define Add Int Char Bool in a different module and give zero a different value there.)

    By splitting the typeclass into two, we remove the ambiguity.

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

Sidebar

Related Questions

Playing around with MSVC++ 2005, I noticed that if the same class is defined
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
Was playing around a little bit with the TimeSpan class and I started wondering
iam playing around with OPEN CV from Intel with the associated PHP Extensions. Works
While playing around with one-to-one associations in castle activerecord I stumbled upon the following
Playing around with type-classes I came up with the seemingly innocent class Pair p
Playing around in order to learn XSLT, I have the following XML file and
Been playing around some with iCanHaz.js templates which i define in the head of
Just playing around with interfaces and I have a question about something which I
When playing around with multiprocessing I noticed that in the following script, __del__ is

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.