C
u/code_monkey_x · 6 hr ago
14 local karma · 8 contributions here · active since May 2026

My Newbie Error "Could not deduce ‘RealFrac a’ arising from a use of ‘floor’ from the context: Num a"

Hi, I'm learning Haskell, and I admit I'm just playing with it right now. While I'm not a terribly experienced programmer, I do have a Bachelor's in Computer Science. My professional experience has been restricted to mostly web development in Typescript sadly. I have not touched functional programming, but this seems unrelated to functional intricacies and more me having a primitive understanding of number types. I'm playing with pattern matching and guards so maybe excuse the silly code, I'm just starting. I try loading (":l haskell_test.hs") my file in GHCi and get the following compile error "Could not deduce ‘RealFrac a’ arising from a use of ‘floor’ from the context: Num a" . I take in type Num, and floor doesn't like that general of a type, I take it? -- testing pattern matching and guards isXFactorOfY :: Num a => a -> a -> Bool isXFactorOfY 0 0 = True isXFactorOfY 0 y = False isXFactorOfY x 0 = False isXFactorOfY 1 1 = True isXFactorOfY 1 y = False isXFactorOfY x 1 = False isXFactorOfY x y | x > y = False | abs(x / y) > floor( abs(x)/abs(y) ) = False | otherwise = True I've tried replacing Num a with Real a, RealFrac a, and some other things I've seen. But I'm just poking blindly and don't actually understand the exact issue. I'd like some help understanding it explicitly rather than stumbling upon the compile-able code. I see the reference for floor is here: https://hackage.haskell.org/package/ClassyPrelude-0.1/docs/Prelude-Math.html#v:floor I guess I'm confused what it want's floor to take in, I figured any number that's an Int or Float would work. But I'm not sure how to define that the way it wants. Again sorry for the silly code. I am quite rusty and am having a rough time getting back into .... problem solving, and reading APIs and references it seems. Thank you in advance for any help or direction you give. The full compile error output is as follows: ghci> :l haskell_test.hs [1 of 2] Compiling Main ( haskell_test.hs, interpreted ) haskell_test.hs:3:14: error: [GHC-39999] • Could not deduce ‘Eq a’ arising from the literal ‘0’ from the context: Num a bound by the type signature for: isXFactorOfY :: forall a. Num a => a -> a -> Bool at haskell_test.hs:2:1-39 Possible fix: add (Eq a) to the context of the type signature for: isXFactorOfY :: forall a. Num a => a -> a -> Bool • In the pattern: 0 In an equation for ‘isXFactorOfY’: isXFactorOfY 0 0 = True | 3 | isXFactorOfY 0 0 = True | ^ haskell_test.hs:9:29: error: [GHC-39999] • Could not deduce ‘Ord a’ arising from a use of ‘>’ from the context: Num a bound by the type signature for: isXFactorOfY :: forall a. Num a => a -> a -> Bool at haskell_test.hs:2:1-39 Possible fix: add (Ord a) to the context of the type signature for: isXFactorOfY :: forall a. Num a => a -> a -> Bool • In the expression: x > y In a stmt of a pattern guard for an equation for ‘isXFactorOfY’: x > y In an equation for ‘isXFactorOfY’: isXFactorOfY x y | x > y = False | abs (x / y) > floor (abs (x) / abs (y)) = False | otherwise = True | 9 | isXFactorOfY x y | x > y = False | ^ haskell_test.hs:10:33: error: [GHC-39999] • Could not deduce ‘Fractional a’ arising from a use of ‘/’ from the context: Num a bound by the type signature for: isXFactorOfY :: forall a. Num a => a -> a -> Bool at haskell_test.hs:2:1-39 Possible fix: add (Fractional a) to the context of the type signature for: isXFactorOfY :: forall a. Num a => a -> a -> Bool • In the first argument of ‘abs’, namely ‘(x / y)’ In the first argument of ‘(>)’, namely ‘abs (x / y)’ In the expression: abs (x / y) > floor (abs (x) / abs (y)) | 10 | | abs(x / y) > floor( abs(x)/abs(y) ) = False | ^ haskell_test.hs:10:40: error: [GHC-39999] • Could not deduce ‘RealFrac a’ arising from a use of ‘floor’ from the context: Num a bound by the type signature for: isXFactorOfY :: forall a. Num a => a -> a -> Bool at haskell_test.hs:2:1-39 Possible fix: add (RealFrac a) to the context of the type signature for: isXFactorOfY :: forall a. Num a => a -> a -> Bool • In the second argument of ‘(>)’, namely ‘floor (abs (x) / abs (y))’ In the expression: abs (x / y) > floor (abs (x) / abs (y)) In a stmt of a pattern guard for an equation for ‘isXFactorOfY’: abs (x / y) > floor (abs (x) / abs (y)) | 10 | | abs(x / y) > floor( abs(x)/abs(y) ) = False | ^^^^^ Failed, unloaded all modules.
6

Join the discussion

You must be a member of /c/programming to comment.

Avatar
u/CuriousExplorer99 6 hr ago
10 local karma · 14 contributions here · active since May 2026
You'll notice that your code works fine without any type signature on isXFactorOfY - the compiler will figure out a general enough type on its own. The issue is that your type signature is quite restrictive; it actually tells Haskell very little about what x and y are. It knows that they're both kinds of Nums, but that's it. If you look at the docs, you'll see that all that means is that you can add them, subtract them, multiply them, negate them, take their absolute value, and convert an integer to their type. Nothing else! So the errors you're getting are all saying that you're trying to do something to these Nums that the Num type class doesn't necessarily imply that you can do. The first is saying, "hey, those patterns you wrote imply checking to see if x and y are equal to 0, but I don't know if I can do equality testing on x and y". I really meant it when I said nothing else! The second is saying, "you tried to see if x was greater than y, but I don't know if I can do that - you can't necessarily compare Nums". The third is saying, "you're trying to divide x and y, but I don't know how to do that - you can't necessarily divide values with their type". Finally the fourth is saying "x and y are Nums, but they aren't necessarily RealFracs, which is what floor wants".
1
Avatar
u/QuietReader42 6 hr ago
16 local karma · 13 contributions here · active since May 2026
commenting out a type signature, loading the module, and running :t isXFactorOfY and copy-pasting the signature into the module is a classic thing to do
1
C
u/CosmicExplorer 6 hr ago
7 local karma · 7 contributions here · active since May 2026
If you run HLS I believe it will create a codelens for top-level bindings without a signature that creates a signature with the inferred type
1
Avatar
u/code_monkey_xOP 6 hr ago
14 local karma · 8 contributions here · active since May 2026
I didn't realize the usefulness of GHCi until you pointed this out, and someone else talked about the ":info Type" outputs. I didn't know I could use GHCi in that way. I've never used a programming language with a tool that does that.
1
Avatar
u/code_monkey_xOP 6 hr ago
14 local karma · 8 contributions here · active since May 2026
To follow up this, GHCi inferred "(RealFrac a, Integral a) => a -> a -> Bool" . I would like to come to my own definition by just thinking about it ....... but for now maybe I'll just do this lol
1
Avatar
u/code_monkey_xOP 6 hr ago
14 local karma · 8 contributions here · active since May 2026
Also I ran into more errors after this lol. I will work those out myself when I get the chance.
1