Compiler error in Haskell code
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 ( Conellipti开发者_如何转开发c 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'
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.
精彩评论