学习Haskell并在一些示例代码中发现错误

时间:2018-06-30 19:35:41

标签: haskell functional-programming haskell-stack

当我学习一种新语言时,我要做的第一件事是通读快速傅立叶变换实现并尝试使其工作。这是我非常熟悉的算法-因此可以帮助我理解该语言的工作原理。

当前,我正在阅读Roman Cheplyaka的this实现。我现在已经非常严格地遵循了该算法,并且一切似乎都能按预期工作,但是下面的代码对我抛出了很多错误:(特别是squareMap部分抛出了错误)

evalFourier coeffs pts = do
  let
    squares = nub $ u_sqr <$> pts -- values of x^2
    (even_coeffs, odd_coeffs) = split coeffs
  even_values <- evalFourier even_coeffs squares
  odd_values <- evalFourier odd_coeffs squares

  let
    -- a mapping from x^2 to (A_e(x^2), A_o(x^2))
    square_map =
      Map.fromList
      . zip squares
      $ zip even_values odd_values

    -- evaluate the polynomial at a single point
    eval1 :: U -> Writer (Sum Int) (Complex a)
    eval1 x = do
      let (ye,yo) = (square_map Map.! u_sqr x)
          r = ye + toComplex x * yo
      tell $ Sum 2 -- this took two arithmetic operations
      return r

  mapM eval1 pts

请注意,MapData.Map的缩写,这是在实现中定义的一些非标准库函数:

-- | U q corresponds to the complex number exp(2 i pi q)
newtype U = U Rational
  deriving (Show, Eq, Ord)

-- | Convert a U number to the equivalent complex number
toComplex :: Floating a => U -> Complex a
toComplex (U q) = mkPolar 1 (2 * pi * realToFrac q)

-- | Smart constructor for U numbers; automatically performs normalization
mkU :: Rational -> U
mkU q = U (q - realToFrac (floor q))

-- | Raise a U number to a power
uPow :: U -> Integer -> U
uPow (U q) p = mkU (fromIntegral p*q)

-- | Square a U number
uSqr :: U -> U
uSqr x = uPow x 2

这是我运行stack build后显示的错误:

src\FFT.hs:43:13: error:
    * Couldn't match type `a' with `a1'
      `a' is a rigid type variable bound by
        the type signature for:
          evalFourier :: forall a.
                         RealFloat a =>
                         [Complex a] -> [U] -> Writer (Sum Int) [Complex a]
        at src\FFT.hs:(19,1)-(22,35)
      `a1' is a rigid type variable bound by
        the type signature for:
          eval1 :: forall a1. U -> Writer (Sum Int) (Complex a1)
        at src\FFT.hs:38:9-50
      Expected type: WriterT
                       (Sum Int) Data.Functor.Identity.Identity (Complex a1)
        Actual type: WriterT
                       (Sum Int) Data.Functor.Identity.Identity (Complex a)
    * In a stmt of a 'do' block: return r
      In the expression:
        do let (ye, yo) = (squareMap Map.! uSqr x)
               r = ye + toComplex x * yo
           tell $ Sum 2
           return r
      In an equation for `eval1':
          eval1 x
            = do let (ye, yo) = ...
                     ....
                 tell $ Sum 2
                 return r
    * Relevant bindings include
        r :: Complex a (bound at src\FFT.hs:41:17)
        ye :: Complex a (bound at src\FFT.hs:40:18)
        yo :: Complex a (bound at src\FFT.hs:40:21)
        eval1 :: U -> Writer (Sum Int) (Complex a1)
          (bound at src\FFT.hs:39:9)
        squareMap :: Map.Map U (Complex a, Complex a)
          (bound at src\FFT.hs:33:9)
        oddValues :: [Complex a] (bound at src\FFT.hs:30:5)
        (Some bindings suppressed; use -fmax-relevant-binds=N or -fno-max-relevant-binds)
   |
43 |             return r
   |             ^^^^^^^^


--  While building custom Setup.hs for package FastFourier-0.1.0.0 using:
      C:\sr\setup-exe-cache\x86_64-windows\Cabal-simple_Z6RU0evB_2.0.1.0_ghc-8.2.2.exe --builddir=.stack-work\dist\5c8418a7 build lib:FastFourier exe:FastFourier-exe --ghc-options " -ddump-hi -ddump-to-file -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

任何人都可以指出是什么导致我在这里看到的错误吗?我感觉到此错误与let (ye,yo) = (square_map Map.! u_sqr x)行有关。谢谢。

1 个答案:

答案 0 :(得分:5)

您似乎从链接代码中缺少了两段:

constructor

位于顶部,

{-# LANGUAGE ScopedTypeVariables #-}

作为evalFourier :: forall a . RealFloat a => [Complex a] -- ^ polynomial coefficients, starting from a_0 -> [U] -- ^ points at which to evaluate the polynomial -> Writer (Sum Int) [Complex a] 的类型签名。

在没有evalFourier的情况下,两个ScopedTypeVariables类型的变量(类型为a和嵌套的evalFourier)是独立的。特别是,eval1 :: U -> Writer (Sum Int) (Complex a)的类型指定了完全通用的结果类型,该函数类型与功能体不匹配。

对于eval1ScopedTypeVariables类型的内部a指的是eval1定义的外部a

forall a. ...构造是pragma(编译器指令)。

LANGUAGE pragma启用语言扩展。

请参阅Language.Haskell.Extension,以获取GHC可以理解的语言扩展列表,尤其是{-# LANGUAGE ... #-}的{​​{3}}。