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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:55:07+00:00 2026-05-22T17:55:07+00:00

I didn’t expect the following code to work: foo :: (Num a) => a

  • 0

I didn’t expect the following code to work:

foo :: (Num a) => a -> a
foo x = x + x

main = do
    print (foo (read "7"))

because it is not possible to fully infer the type of (read “7”) based on the code. But GHC (6.12.3) thinks otherwise and prints 14.

If “7” is changed to “7.2”, the code fails with “no parse”. What’s going on here? how is Haskell deciding which instance of Read to use?

  • 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-22T17:55:07+00:00Added an answer on May 22, 2026 at 5:55 pm

    This is caused by Haskell’s defaulting rules for the Num class. If you added

    default (Double, Integer)
    

    to the top of your file, then you’d get the following results:

    main = do
      print (foo (read "7")) -- prints "14.0"
      print (foo (read "7.2")) -- prints "14.2"
    

    In a nutshell, defaulting rules are an attempt to “try to do the right thing” and save you from a compile error when you have an ambiguous type in your program. Unfortunately in this case it trades a compile-time error for a runtime error.

    You can disable defaulting like so:

    default ()
    

    which will force you to explicitly disambiguate the types of such terms via type annotations:

    print (foo (read "7" :: Int))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I didn't write the following code and I am not a Javascript expert. So
I didn't think it was possible for client side applications to access the harddrive?
didn't find an answer to this. I like KeyedCollection, because it keeps the insertion
I didn't believe it when I read the user's complaint... typing .123 in a
This didn't work: Setting the umask of the Apache user Arch doesn't have a
I didn't see any license information on top of a web page's source code.
I didn't expect this is that complex. But I can't figure out how I
Didn't find any from their website. I mostly just trying to see which one
I didn't get the answer to this anywhere. What is the runtime complexity of
I didn't see any similar questions asked on this topic, and I had to

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.