[英]Haskell type deduction issue
我的代碼包括以下幾行(取出一些無關的東西; 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 合作。 我已經從更復雜的原始代碼中分解了它,問題似乎在這里的某個地方,但我完全不知道它可能是什么。 謝謝!!!
在 Haskell 中,您不能將整數乘以小數,例如0.5
。 如果您對四舍五入的結果感到滿意,請執行phase c `div` 2
而不是0.5*phase c
。 如果您希望結果為浮點數,則需要在乘以之前使用fromInteger
或其他東西將其轉換為 1。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.