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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:17:11+00:00 2026-06-18T04:17:11+00:00

I trying to wrap my head around Haskell type coercion. Meaning, when does can

  • 0

I trying to wrap my head around Haskell type coercion. Meaning, when does can one pass a value into a function without casting and how that works. Here is a specific example, but I am looking for a more general explanation I can use going forward to try and understand what is going on:

Prelude> 3 * 20 / 4
15.0
Prelude> let c = 20
Prelude> :t c
c :: Integer
Prelude> 3 * c / 4

<interactive>:94:7:
    No instance for (Fractional Integer)
      arising from a use of `/'
    Possible fix: add an instance declaration for (Fractional Integer)
    In the expression: 3 * c / 4
    In an equation for `it': it = 3 * c / 4

The type of (/) is Fractional a => a -> a -> a. So, I’m guessing that when I do “3 * 20” using literals, Haskell somehow assumes that the result of that expression is a Fractional. However, when a variable is used, it’s type is predefined to be Integer based on the assignment.

My first question is how to fix this. Do I need to cast the expression or convert it somehow?
My second question is that this seems really weird to me that you can’t do basic math without having to worry so much about int/float types. I mean there’s an obvious way to convert automatically between these, why am I forced to think about this and deal with it? Am I doing something wrong to begin with?

I am basically looking for a way to easily write simple arithmetic expressions without having to worry about the neaty greaty details and keeping the code nice and clean. In most top-level languages the compiler works for me — not the other way around.

  • 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-18T04:17:12+00:00Added an answer on June 18, 2026 at 4:17 am

    If you just want the solution, look at the end.

    You nearly answered your own question already. Literals in Haskell are overloaded:

    Prelude> :t 3
    3 :: Num a => a
    

    Since (*) also has a Num constraint

    Prelude> :t (*)
    (*) :: Num a => a -> a -> a
    

    this extends to the product:

    Prelude> :t 3 * 20
    3 * 20 :: Num a => a
    

    So, depending on context, this can be specialized to be of type Int, Integer, Float, Double, Rational and more, as needed. In particular, as Fractional is a subclass of Num, it can be used without problems in a division, but then the constraint will become
    stronger and be for class Fractional:

    Prelude> :t 3 * 20 / 4
    3 * 20 / 4 :: Fractional a => a
    

    The big difference is the identifier c is an Integer. The reason why a simple let-binding in GHCi prompt isn’t assigned an overloaded type is the dreaded monomorphism restriction. In short: if you define a value that doesn’t have any explicit arguments,
    then it cannot have overloaded type unless you provide an explicit type signature.
    Numeric types are then defaulted to Integer.

    Once c is an Integer, the result of the multiplication is Integer, too:

    Prelude> :t 3 * c
    3 * c :: Integer
    

    And Integer is not in the Fractional class.

    There are two solutions to this problem.

    1. Make sure your identifiers have overloaded type, too. In this case, it would
      be as simple as saying

        Prelude> let c :: Num a => a; c = 20
        Prelude> :t c
        c :: Num a => a
      
    2. Use fromIntegral to cast an integral value to an arbitrary numeric value:

        Prelude> :t fromIntegral
        fromIntegral :: (Integral a, Num b) => a -> b
        Prelude> let c = 20
        Prelude> :t c
        c :: Integer
        Prelude> :t fromIntegral c
        fromIntegral c :: Num b => b
        Prelude> 3 * fromIntegral c / 4
        15.0
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to wrap my head around the jQuery .not() function, and running into a
I am trying to wrap my head around the syntax of Haskell. This problem
Trying to wrap my head around this function any_name($div,$width = false){ if($width){ $add_style ='
I'm trying to wrap my head around Key-Value stores like CouchDB and Cassandra. I
I am trying to wrap my head around LINQ, so I can figure out
I trying to wrap my head around setTimeout , but I can't get it
Still trying to wrap my head around Clojure. I can see how to implement
Trying to wrap my head around JTA and have arbitrarily chosen Bitronix as the
Im trying to wrap my head around MVVM. I understand a lot of it,
I'm trying to wrap my head around Arquillian and am just not fundamentally understanding

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.