Мой код содержит следующие строки (некоторые посторонние вещи удалены; w
возвращает что-то типа YN2
, а g
- это RandomGen
). eps
должно быть небольшим положительным вещественным числом.
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
Я постоянно получаю ошибки следующей формы от 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
| ^^^^^^^
Кто-нибудь знает, что происходит? Кажется, есть какое-то несоответствие типов, но я не могу понять, как заставить Haskell сотрудничать. Я разбил его на более сложный исходный код, и проблема, кажется, где-то здесь, но я совершенно не понимаю, что это может быть. Спасибо !!!