Haskell тип вычета - PullRequest
       10

Haskell тип вычета

0 голосов
/ 03 мая 2020

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

1 Ответ

4 голосов
/ 03 мая 2020

Вы не можете умножать целые числа на десятичные, например 0.5 в Haskell. Если вы согласны с округлением результата, тогда сделайте phase c `div` 2 вместо 0.5*phase c. Если вы хотите, чтобы результатом было число с плавающей запятой, вам нужно использовать fromInteger или что-то еще, чтобы преобразовать его в единицу, прежде чем умножить его.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...