学习Haskell,发现部分示例代码有错误
Learning Haskell and found an error in some sample code
当我学习一门新语言时,我做的第一件事就是通读快速傅里叶变换的实现并尝试让它工作。这是一种我相当熟悉的算法 - 因此它可以帮助我理解该语言的工作原理。
目前,我正在阅读 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
请注意 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
这是我 运行 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\distc8418a7 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)
有关。谢谢。
您似乎遗漏了链接代码中的两部分:
{-# 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)
类型中)是独立的。特别是 eval1
的类型指定了一个完全通用的结果类型,它与函数体不匹配。
和ScopedTypeVariables
一样,eval1
类型中的内层a
指的是forall a. ...
所定义的外层a
。
{-# LANGUAGE ... #-}
构造是一个 pragma(编译器指令)。
LANGUAGE
pragma 启用语言扩展。
参见 Language.Haskell.Extension
for a list of language extensions understood by GHC, and in particular -XScopedTypeVariables
ScopedTypeVariables
。
当我学习一门新语言时,我做的第一件事就是通读快速傅里叶变换的实现并尝试让它工作。这是一种我相当熟悉的算法 - 因此它可以帮助我理解该语言的工作原理。
目前,我正在阅读 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
请注意 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
这是我 运行 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\distc8418a7 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)
有关。谢谢。
您似乎遗漏了链接代码中的两部分:
{-# 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)
类型中)是独立的。特别是 eval1
的类型指定了一个完全通用的结果类型,它与函数体不匹配。
和ScopedTypeVariables
一样,eval1
类型中的内层a
指的是forall a. ...
所定义的外层a
。
{-# LANGUAGE ... #-}
构造是一个 pragma(编译器指令)。
LANGUAGE
pragma 启用语言扩展。
参见 Language.Haskell.Extension
for a list of language extensions understood by GHC, and in particular -XScopedTypeVariables
ScopedTypeVariables
。