0
votes

I have some code:

seqMin a b = (floor (a / b)) - (floor (b / 2.0  ))
seqMax a b = (seqMin a b) + b
test = seqMax 6 3

I expect test to be 1 when I evaluate it interactively.

When I evaluate my code, I get an error:

$ ghci split2.hs

GHCi, version 8.6.5: http://www.haskell.org/ghc/ :? for help [1 of 1] Compiling Main ( split2.hs, interpreted )

split2.hs:3:8: error: • Ambiguous type variable ‘a0’ arising from a use of ‘seqMax’ prevents the constraint ‘(RealFrac a0)’ from being solved. Relevant bindings include test :: a0 (bound at split2.hs:3:1) Probable fix: use a type annotation to specify what ‘a0’ should be. These potential instances exist: instance RealFrac Double -- Defined in ‘GHC.Float’ instance RealFrac Float -- Defined in ‘GHC.Float’ ...plus one instance involving out-of-scope types (use -fprint-potential-instances to see them all) • In the expression: seqMax 6 3 In an equation for ‘test’: test = seqMax 6 3 | 3 | test = seqMax 6 3 | ^^^^^^^^^^

split2.hs:3:15: error: • Ambiguous type variable ‘a0’ arising from the literal ‘6’ prevents the constraint ‘(Num a0)’ from being solved. Relevant bindings include test :: a0 (bound at split2.hs:3:1) Probable fix: use a type annotation to specify what ‘a0’ should be. These potential instances exist: instance Num Integer -- Defined in ‘GHC.Num’ instance Num Double -- Defined in ‘GHC.Float’ instance Num Float -- Defined in ‘GHC.Float’ ...plus two others ...plus two instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In the first argument of ‘seqMax’, namely ‘6’ In the expression: seqMax 6 3 In an equation for ‘test’: test = seqMax 6 3 | 3 | test = seqMax 6 3 | ^ Failed, no modules loaded.

(Please forgive my formatting.)

When trying to figure out why I got the error, I came up with another function:

f x = (floor (x/2)) + x

When I evaluate it, I get an error:

> f 3

:2:1: error: • Ambiguous type variable ‘a0’ arising from a use of ‘print’ prevents the constraint ‘(Show a0)’ from being solved. Probable fix: use a type annotation to specify what ‘a0’ should be. These potential instances exist: instance Show Ordering -- Defined in ‘GHC.Show’ instance Show Integer -- Defined in ‘GHC.Show’ instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’ ...plus 22 others ...plus 19 instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In a stmt of an interactive GHCi command: print it

Why does this happen, how can I solve it, and how can I prevent it in the future? Thanks!

1
What do you expect the signature of f to be? Take a look at the type signature of floor and (/) -- what does these tell you (or really not tell you) about what x and f x must be?Brian
Have you tried adding the type annotations that the error messages suggest?Robert Harvey
Have you considered using div and/or quot if you are only interested in integer results? (Related: can a or b ever be negative?)chepner
seqMin takes two arguments of a RealFrac type and returns a value of an Integral type. seqMax tries to add that Integral result to one of the RealFrac arguments, resulting in a situation that is impossible to resolve (RealFrac and Integral are supposed to be disjoint.) Insert some numerical conversions somewhere, or don't use floor, /, and 2.0 (all of which require either RealFrac or Fractional).HTNW
@chepner I am relatively new to Haskell, so I was not aware those functions existed. a and b are expected to be positive integers such that a > b.user2350838

1 Answers

0
votes

The comments helped a lot. I changed / to div calls and it works now. Thanks everyone!