gpt4 book ai didi

haskell - 学习Haskell,发现一些示例代码有错误

转载 作者:行者123 更新时间:2023-12-02 21:12:41 24 4
gpt4 key购买 nike

当我学习一门新语言时,我做的第一件事就是通读快速傅里叶变换的实现并尝试让它发挥作用。这是我相当熟悉的算法 - 因此它可以帮助我理解该语言的工作原理。

目前,我正在阅读 this由 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

请注意,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) 行有关。谢谢。

最佳答案

看起来您缺少链接代码中的两部分:

{-# 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 ,内a类型为eval1指外aforall a. ... 定义.

{-# LANGUAGE ... #-}构造是 pragma (编译器指令)。

LANGUAGE pragma启用语言扩展。

参见 Language.Haskell.Extension 有关 GHC 可以理解的语言扩展列表,特别是 -XScopedTypeVariables 对于 ScopedTypeVariables .

关于haskell - 学习Haskell,发现一些示例代码有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51117947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com