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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:19:03+00:00 2026-05-15T20:19:03+00:00

If I write foo :: (Num a) => a foo = 42 GHC happily

  • 0

If I write

foo :: (Num a) => a
foo = 42

GHC happily accepts it, but if I write

bar :: (Num a) => a
bar = (42 :: Int)

it tells me that the expected type a doesn’t match the inferred type Int. I don’t quite understand why, since Int is an instance of the class Num that a stands for.

I’m running into this same situation while trying to write a function that, boiled down to the core of the problem, looks roughly like this:

-- Note, Frob is an instance of class Frobbable
getFrobbable :: (Frobbable a) => Frob -> a
getFrobbable x = x

Is it possible to write a function like this? How can I make the result compatible with the type signature?

  • 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-15T20:19:04+00:00Added an answer on May 15, 2026 at 8:19 pm

    You are treating typeclass constraints as if they were subtype constraints. This is a common thing to do, but in fact they only coincide in the contravariant case, that is, when concerning function arguments rather than results. The signature:

    bar :: (Num a) => a
    

    Means that the caller gets to choose the type a, provided that it is an instance of Num. So here, the caller may choose to call bar :: Int or bar :: Double, they should all work. So bar :: (Num a) => a must be able to construct any kind of number, bar does not know what specific type was chosen.

    The contravariant case is exactly the same, it just corresponds with OO programmers’ intuition in this case. Eg.

    baz :: (Num a) => a -> Bool
    

    Means that the caller gets to choose the type a, again, and again baz does not know what specific type was chosen.

    If you need to have the callee choose the result type, just change the signature of the function to reflect this knowledge. Eg.:

    bar :: Int
    

    Or in your getFrobbable case, getFrobbable :: Frob -> Frob (which makes the function trivial). Anywhere that a (Frobbable a) constraint occurs, a Frob will satisfy it, so just say Frob instead.

    This may seem awkward, but it’s really just that information hiding happens at different boundaries in functional programming. There is a way to hide the specific choice, but it is uncommon, and I consider it a mistake in most cases where it is used.

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

Sidebar

Related Questions

I know that I can write: foo = 'bar' def update_foo(): global foo foo
If I write a C# class called Foo and that is compiled into an
Is there a nicer way to write in jUnit String x = foo bar;
In perl I can write ($var1, $var2, $var3) = split(/:/, foo:bar:blarg) to split a
I write Foo<T>(IComparer<T> comp) I want to call it with Foo<DateTime> (DateTime.compare) but i
Is this possible? I inserted a simple test snippet like this <script type=text/javascript>//<![CDATA[ document.write('foo');
Why is that this code: var foo = {one: 1, two: 2}; var bar
In Python, it is tedious to write: print foo is + bar + '.'
In gcc, I can write foo ? : bar which is a shorthand form
I want to use variable a from foo.c in main.c, and I write: foo.c

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.