Haskell 代码中的编译器错误
我正在尝试使用 where 子句编写椭圆曲线点加法。我收到编译器错误,但是当我使用 let in expression 翻译相同的代码时,它工作正常。有人可以告诉我这段代码有什么问题吗?完整源代码 [ http://hpaste.org/49174 ]
谢谢
穆克什·蒂瓦里
{--
--add points of elliptic curve using where clause getting compiler error
addPoints :: Elliptic -> Point -> Point -> Either Point Integer
addPoints _ Identity p_2 = Left p_2
addPoints _ p_1 Identity = Left p_1
addPoints ( Conelliptic a b n ) ( Conpoint x_p y_p ) ( Conpoint x_q y_q )
| x_p /= x_q = case ( ( Conpoint x_r y_r ) , d ) of
( _ , 1 ) -> Left ( Conpoint x_r y_r )
( _ , d' ) -> Right d'
where
[ u , v , d ] = extended_gcd ( x_p - x_q ) n
s = mod ( ( y_p - y_q ) * u ) n
x_r = mod ( s*s - x_p - x_q ) n
y_r = mod ( -y_p - s * ( x_r - x_p ) ) n
| otherwise = if mod ( y_p + y_q ) n == 0 then Left Identity
else case ( ( Conpoint x_r y_r ) , d ) of
( _ , 1 ) -> Left ( Conpoint x_r y_r )
( _ , d' ) -> Right d'
where
[ u , v , d ] = extended_gcd ( 2 * y_p ) n
s = mod ( ( 3 * x_p * x_p + a ) * u ) n
x_r = mod ( s * s - 2 * x_p ) n
y_r = mod ( -y_p - s * ( x_r - x_p ) ) n
--}
--add points of elliptic curve let in clause and its working
addPoints::Elliptic->Point->Point-> Either Point Integer
addPoints _ Identity p_2 = Left p_2
addPoints _ p_1 Identity = Left p_1
addPoints ( Conelliptic a b n ) ( Conpoint x_p y_p ) ( Conpoint x_q y_q )
| x_p /= x_q = let
[ u , v , d ] = extended_gcd (x_p-x_q) n
s = mod ( ( y_p - y_q ) * u ) n
x_r = mod ( s * s - x_p - x_q ) n
y_r= mod ( -y_p - s * ( x_r - x_p ) ) n
in case ( ( Conpoint x_r y_r ) , d ) of
( _ , 1 ) -> Left ( Conpoint x_r y_r )
( _ , d' ) -> Right d'
| otherwise = if mod ( y_p + y_q ) n == 0 then Left Identity
else let
[ u , v , d ] = extended_gcd ( 2*y_p ) n
s = mod ( ( 3 * x_p * x_p + a ) * u ) n
x_r = mod ( s * s - 2 * x_p ) n
y_r = mod ( -y_p - s * ( x_r - x_p ) ) n
in case ( ( Conpoint x_r y_r ) , d ) of
( _ , 1 )-> Left (Conpoint x_r y_r)
( _ , d' ) -> Right d'
I am trying to write elliptic curve point addition using where clause. I am getting compiler error but when i translated the same code using let in expression , it works fine. Could some one please tell me what is wrong with this code. Full source code [ http://hpaste.org/49174 ]
Thank you
Mukesh Tiwari
{--
--add points of elliptic curve using where clause getting compiler error
addPoints :: Elliptic -> Point -> Point -> Either Point Integer
addPoints _ Identity p_2 = Left p_2
addPoints _ p_1 Identity = Left p_1
addPoints ( Conelliptic a b n ) ( Conpoint x_p y_p ) ( Conpoint x_q y_q )
| x_p /= x_q = case ( ( Conpoint x_r y_r ) , d ) of
( _ , 1 ) -> Left ( Conpoint x_r y_r )
( _ , d' ) -> Right d'
where
[ u , v , d ] = extended_gcd ( x_p - x_q ) n
s = mod ( ( y_p - y_q ) * u ) n
x_r = mod ( s*s - x_p - x_q ) n
y_r = mod ( -y_p - s * ( x_r - x_p ) ) n
| otherwise = if mod ( y_p + y_q ) n == 0 then Left Identity
else case ( ( Conpoint x_r y_r ) , d ) of
( _ , 1 ) -> Left ( Conpoint x_r y_r )
( _ , d' ) -> Right d'
where
[ u , v , d ] = extended_gcd ( 2 * y_p ) n
s = mod ( ( 3 * x_p * x_p + a ) * u ) n
x_r = mod ( s * s - 2 * x_p ) n
y_r = mod ( -y_p - s * ( x_r - x_p ) ) n
--}
--add points of elliptic curve let in clause and its working
addPoints::Elliptic->Point->Point-> Either Point Integer
addPoints _ Identity p_2 = Left p_2
addPoints _ p_1 Identity = Left p_1
addPoints ( Conelliptic a b n ) ( Conpoint x_p y_p ) ( Conpoint x_q y_q )
| x_p /= x_q = let
[ u , v , d ] = extended_gcd (x_p-x_q) n
s = mod ( ( y_p - y_q ) * u ) n
x_r = mod ( s * s - x_p - x_q ) n
y_r= mod ( -y_p - s * ( x_r - x_p ) ) n
in case ( ( Conpoint x_r y_r ) , d ) of
( _ , 1 ) -> Left ( Conpoint x_r y_r )
( _ , d' ) -> Right d'
| otherwise = if mod ( y_p + y_q ) n == 0 then Left Identity
else let
[ u , v , d ] = extended_gcd ( 2*y_p ) n
s = mod ( ( 3 * x_p * x_p + a ) * u ) n
x_r = mod ( s * s - 2 * x_p ) n
y_r = mod ( -y_p - s * ( x_r - x_p ) ) n
in case ( ( Conpoint x_r y_r ) , d ) of
( _ , 1 )-> Left (Conpoint x_r y_r)
( _ , d' ) -> Right d'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于
where
阻止了函数保护范围,因此不可能为每个受保护的语句创建单独的where
。当 ghc 在第 59 行遇到where
时,它会自动结束函数声明并期望后面有一个新的声明,这使得|
成为错误,因为它不是有效的声明。它与 let 表达式一起使用,因为let
和where
是语言的不同部分。 Haskell Wiki 有关于此主题的更多信息。The problem is that
where
blocks scope over function guards, so it's not possible to create a separatewhere
for each guarded statement. When ghc encounters thewhere
on line 59, it automatically ends the function declaration and expects a new declaration to follow, which makes the|
an error because it's not a valid declaration. It works with a let-expression becauselet
andwhere
are different parts of the language. The Haskell Wiki has more information on this topic.