在 Haskell 中使用 Data.Mod 进行模幂运算
Use of Data.Mod for modular exponentiation in Haskell
当使用 Math.NumberTheory.Powers.Modular 库中的 powMod
时,Haskell 编译器或解释器给出以下警告:
warning: [-Wdeprecations]
In the use of ‘powMod’
(imported from Math.NumberTheory.Powers.Modular):
Deprecated: "Use Data.Mod or Data.Mod.Word instead"
我有以下功能:
cypher n e m = powMod m e n
所以我尝试将其转换为推荐的新库Data.Mod,用(^%)
运算符替换powMod
函数:
cypher n e m = m ^% e :: Mod n
但是后来出现了下面的错误,不知如何改正:
• Couldn't match expected type ‘Mod n1’ with actual type ‘p2’
because type variable ‘n1’ would escape its scope
This (rigid, skolem) type variable is bound by
an expression type signature:
forall (n1 :: GHC.Types.Nat). Mod n1
at RSA-cyphering.hs:43:26-30
• In the first argument of ‘(^%)’, namely ‘m’
In the expression: m ^% e :: Mod n
In an equation for ‘cypher’: cypher n e m = m ^% e :: Mod n
当模块 n
取自函数的参数时,如何在函数内部使用类型 Mod n
?
您使用 SomeMod
:
cypher :: Natural -> Integer -> Integer -> SomeMod
cypher n e m = (m `modulo` n) ^ e
当使用 Math.NumberTheory.Powers.Modular 库中的 powMod
时,Haskell 编译器或解释器给出以下警告:
warning: [-Wdeprecations] In the use of ‘powMod’ (imported from Math.NumberTheory.Powers.Modular): Deprecated: "Use Data.Mod or Data.Mod.Word instead"
我有以下功能:
cypher n e m = powMod m e n
所以我尝试将其转换为推荐的新库Data.Mod,用(^%)
运算符替换powMod
函数:
cypher n e m = m ^% e :: Mod n
但是后来出现了下面的错误,不知如何改正:
• Couldn't match expected type ‘Mod n1’ with actual type ‘p2’ because type variable ‘n1’ would escape its scope This (rigid, skolem) type variable is bound by an expression type signature: forall (n1 :: GHC.Types.Nat). Mod n1 at RSA-cyphering.hs:43:26-30 • In the first argument of ‘(^%)’, namely ‘m’ In the expression: m ^% e :: Mod n In an equation for ‘cypher’: cypher n e m = m ^% e :: Mod n
当模块 n
取自函数的参数时,如何在函数内部使用类型 Mod n
?
您使用 SomeMod
:
cypher :: Natural -> Integer -> Integer -> SomeMod
cypher n e m = (m `modulo` n) ^ e