Haskell 代码中的编译器错误

发布于 2024-11-25 01:24:13 字数 2691 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千笙结 2024-12-02 01:24:13

问题在于 where 阻止了函数保护范围,因此不可能为每个受保护的语句创建单独的 where。当 ghc 在第 59 行遇到 where 时,它会自动结束函数声明并期望后面有一个新的声明,这使得 | 成为错误,因为它不是有效的声明。它与 let 表达式一起使用,因为 letwhere 是语言的不同部分。 Haskell Wiki 有关于此主题的更多信息。

The problem is that where blocks scope over function guards, so it's not possible to create a separate where for each guarded statement. When ghc encounters the where 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 because let and where are different parts of the language. The Haskell Wiki has more information on this topic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文