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

Flo*_*est 2 haskell functional-programming haskell-stack

当我学习一门新语言时,我要做的第一件事就是阅读快速傅里叶变换实现并尝试使其工作.这是一个我非常熟悉的算法 - 所以它帮助我理解语言是如何工作的.

目前,我正在阅读Roman Cheplyaka的这一实施.我现在非常密切地遵循算法,一切似乎都按预期工作,但是下面的一段代码为我抛出了一堆错误:(具体来说,该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
Run Code Online (Sandbox Code Playgroud)

请注意,这Map是一个简短的Data.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
Run Code Online (Sandbox Code Playgroud)

以下是我运行后显示的错误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
Run Code Online (Sandbox Code Playgroud)

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

mel*_*ene 5

看起来你错过了链接代码中的两个部分:

{-# LANGUAGE ScopedTypeVariables #-}
Run Code Online (Sandbox Code Playgroud)

在顶部和

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]
Run Code Online (Sandbox Code Playgroud)

作为类型签名evalFourier.

没有ScopedTypeVariables,两个a类型变量(在类型evalFourier和嵌套中eval1 :: U -> Writer (Sum Int) (Complex a))是独立的.特别是,type eval1指定一个完全通用的结果类型,它与函数体不匹配.

ScopedTypeVariables,内在a的类型是eval1指外部a定义的forall a. ....

{-# LANGUAGE ... #-}构建体是编译指示(一个编译器指令).

LANGUAGEpragma支持语言扩展.

请参阅Language.Haskell.ExtensionGHC理解的语言扩展列表,特别-XScopedTypeVariablesScopedTypeVariables.