Reputation: 327
My code includes the following lines (some extraneous stuff taken out; w
returns something of type YN2
and g
is a RandomGen
). eps
is supposed to be a small positive real number.
data YN2 = Yes2 Integer Integer | No2 deriving (Show)
data YN4 = Yes4 Integer Integer Integer Integer | No4 deriving (Show)
f g mat eps = -- mat looks like (a,b) where a,b are from Data.Complex. returns (x0,x1,x2,x3)
case mat of
(a,b) -> let a_abs = magnitude a
search k =
let lb = 0
ub = eps*5^k
find m =
if m > ub
then No4
else case (w g m, w g (5^k-m)) of
(Yes2 x0 x1, Yes2 x2 x3) -> Yes4 x0 x1 x2 x3
_ -> find (m+1)
in case find lb of -- begins looking in [5^k] at lb, stops after ub
No4 -> search (k+1)
Yes4 x0 x1 x2 x3 -> (x0,x1,x2,x3)
in search 0 -- begins the search at k=0
func g mat eps =
case (mat, f g mat eps) of
((a,b),(x0,x1,x2,x3)) -> let c = x0 :+ x1
d = x2 :+ x3
phi = 0.5*phase c
in phi
I consistently get errors of the following form from GHCi:
code.hs:114:44: error:
• Could not deduce (Fractional Integer)
arising from the literal ‘0.5’
from the context: (RealFloat a, RandomGen g, Num p)
bound by the inferred type of
f :: (RealFloat a, RandomGen g, Num p) =>
g -> (Complex a, b) -> a -> p
at code.hs:(110,1)-(115,37)
• In the first argument of ‘(*)’, namely ‘0.5’
In the expression: 0.5*phase c
In an equation for ‘phi’: phi = 0.5*phase c
|
114 | phi = 0.5*phase c
| ^^^
code.hs:114:49: error:
• Could not deduce (RealFloat Integer) arising from a use of ‘func’
from the context: (RealFloat a, RandomGen g, Num p)
bound by the inferred type of
f :: (RealFloat a, RandomGen g, Num p) =>
g -> (Complex a, b) -> a -> p
at code.hs:(110,1)-(115,37)
• In the second argument of ‘(*)’, namely ‘phase c’
In the expression: 0.5*phase c
In an equation for ‘phi’: phi = 0.5*phase c
|
114 | phi = 0.5*phase c
| ^^^^^^^
Does anyone know what's happening? There seems to be some kind of type mismatch but I can't figure out how to get Haskell to cooperate. I've broken it down from the more complicated original code and the issue seems to lie somewhere in here, but I'm totally at a loss as to what it could be. Thanks!!!
Upvotes: 0
Views: 71
Reputation: 48612
You can't multiply integers by decimals like 0.5
in Haskell. If you're okay with the result being rounded off, then do phase c `div` 2
instead of 0.5*phase c
. If you want the result to be a floating-point number, then you need to use fromInteger
or something to convert it to one, before you multiply it.
Upvotes: 3