繁体   English   中英

Haskell型扣问题

[英]Haskell type deduction issue

我的代码包括以下几行(取出一些无关的东西; w返回YN2类型的东西, gRandomGen )。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM