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!
f
to be? Take a look at the type signature offloor
and(/)
-- what does these tell you (or really not tell you) about whatx
andf x
must be? – Briandiv
and/orquot
if you are only interested in integer results? (Related: cana
orb
ever be negative?) – chepnerseqMin
takes two arguments of aRealFrac
type and returns a value of anIntegral
type.seqMax
tries to add thatIntegral
result to one of theRealFrac
arguments, resulting in a situation that is impossible to resolve (RealFrac
andIntegral
are supposed to be disjoint.) Insert some numerical conversions somewhere, or don't usefloor
,/
, and2.0
(all of which require eitherRealFrac
orFractional
). – HTNW