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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:06:21+00:00 2026-06-14T14:06:21+00:00

I have coefficient :: ??????? coefficient = 1.0 and val :: Int and I

  • 0

I have

coefficient :: ???????
coefficient = 1.0

and

val :: Int

and I would like to do

result :: ???????
result val coefficient = val * coefficient

What type signatures and conversion functions do I need to do to make this work? What must I do on top of that if I want to have ability to generalize val to any kind of Num?

This:

coefficient = 1.0

val :: Int
val = 3

result :: Num a => a
result = coefficient * (fromIntegral val)

gives me this compiler warning:

Could not deduce (a ~ Double)
from the context (Num a)
  bound by the type signature for result :: Num a => a
  at Move.hs:17:1-41
  `a' is a rigid type variable bound by
      the type signature for result :: Num a => a at Move.hs:17:1
In the first argument of `(*)', namely `coefficient'
In the expression: coefficient * (fromIntegral val)
In an equation for `result':
    result = coefficient * (fromIntegral val)

I know thats not what I asked originally, I made some mistakes when sanitizing my code.

Now with a type for coefficient:

coefficient :: Num a => a
coefficient = 1.0

val :: Int
val = 3

result :: Num a => a
result = coefficient * (fromIntegral val)

the resulting error:

Could not deduce (Fractional a) arising from the literal `1.0'
from the context (Num a)
  bound by the type signature for coefficient :: Num a => a
  at Move.hs:12:1-17
Possible fix:
  add (Fractional a) to the context of
    the type signature for coefficient :: Num a => a
In the expression: 1.0
In an equation for `coefficient': coefficient = 1.0
  • 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-14T14:06:22+00:00Added an answer on June 14, 2026 at 2:06 pm

    There is a function, fromIntegral, which will convert an integral number to any other numeric type. So you can do:

    result :: (Integral n, Num m) => n -> m -> m
    result val coefficient = fromIntegral val * coefficient
    

    Or, in point-free style:

    result = (*) . fromIntegral
    

    Update about updated question(@Drew)

    Consider this code:

    coefficient :: (Num a) => a
    coefficient = 1.0
    

    This is invalid on it’s own, as follows. Because 1.0 is a literal for a fractional number (not a whole integer), then GHC can only encode it as any type which is capable of representing fractional numbers (forall a. Fractional a => a). However, you have specified that it must be valid for any numeric type (forall a. Num a => a). Some numeric types (e.g. Integer) cannot represent fractional values, and are not instances of Fractional (rightly so), so this cannot typecheck. You can fix this as follows:

    coefficient :: (Fractional a) => a
    coefficient = 2.0
    

    Here GHC can infer the type, and coefficient works fine. It is important to note, that Fractional is a subclass of Num, so everything that is a Fractional must also be a Num. If we look at the function in the first part of my answer, coefficient is only required to be a Num type (as we only use it with (*)), so we can use this definition of coefficient in place of that parameter. Your problem occurs for exactly the same reason.

    result :: (Num a) => a
    result = coefficient * fromIntegral val
    

    Again, the result of this function must be of the same type as coefficient. As coefficient cannot be any Num type, but only a fractional type, we need to change this to:

    result :: (Fractional a) => a
    result = coefficient * fromIntegral val
    

    And then that should typecheck. @singpolyma is right that your original error was partly to do with the monomorphism restriction, but you just needed to make the type signatures slightly more specific. If you want it to work with (Num a) => a, then coefficient must be a whole number (e.g. 1).

    Update about GHCi (@Marcin)

    For using this in GHCi, I would suggest letting GHCi infer the type. If in this case you type (in GHCi):

    let result val coefficient = fromIntegral val * coefficient
    

    Then GHCi will correctly infer the type of the result. You can ask GHCi what type it thinks something is using the ‘:t’ command:

    Prelude> :t result
    result :: (Integral a1, Num a) => a1 -> a -> a
    

    If you must have an explicit type signature you can do:

    let result = (\val coefficient -> fromIntegral val * coefficient) :: (Integral a, Num b) => a -> b -> b
    

    To try and have an explicit type, but GHCi will make this monomorphic:

    Prelude> :t result
    result :: Integer -> Integer -> Integer
    

    Which we don’t want (this is because the type annotation refers to the value of the lambda expression, not the declaration of result). I don’t know how to get the explicit type to work here either, so maybe someone more knowledgeable than us can answer 😛

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

Sidebar

Related Questions

I have code that looks like this: public class Polynomial { List<Term> term =
I have two sets of arrays like this for example. $Arr1['uid'][]='user 1'; $Arr1['weight'][]=1; $Arr1['uid'][]='user
I would like to be able to have a series of nested loops that
I would like to create a vector of functions using a two agruments function
I have table with two columns: price and constant . I would like to
I have 3 eqns and 2 unknowns Hb and Hbo2, they look like this:
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
I would like to optimize a part of my program where I'm calculating the

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.