2011-07-18 121 views
2

我想用where子句寫橢圓曲線點加法。我得到編譯器錯誤,但是當我在表達式中使用let編譯相同的代碼時,它工作正常。有人能告訴我這段代碼有什麼問題嗎?完整的源代碼[http://hpaste.org/49174]
謝謝
穆克什·蒂瓦里Haskell代碼中的編譯器錯誤

{-- 
--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' 

+1

你能否贊成發佈編譯器錯誤?這對於那些想要幫助你的人是非常有幫助的。 – fuz

+2

我找不到錯誤。兩種方式(第一種和第二種)在我的機器上編譯時沒有問題。 (使用GHC 7) – fuz

+0

Elliptic_Len.hs:59:8:輸入'|'的解析錯誤 失敗,已加載模塊:無。 ghc-6.12.1 –

回答

9

的問題是,where塊範圍擴展到功能守衛,所以它不可能爲每個守衛語句單獨where。當ghc在59行遇到where時,它會自動結束函數聲明,並期待一個新的聲明,這會使|出錯,因爲它不是有效的聲明。它與let-expression一起工作,因爲letwhere是該語言的不同部分。 Haskell Wiki有關於此主題的更多信息。