微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Haskell 中使用 Data.Mod 进行模幂

如何解决在 Haskell 中使用 Data.Mod 进行模幂

使用 Math.NumberTheory.Powers.Modular 库中的 powMod 时,Haskell 编译器或解释器会给出以下警告:

警告:[-Wdeprecations] 使用‘powMod’ (从 Math.NumberTheory.Powers.Modular 导入): 已弃用:“改用 Data.Mod 或 Data.Mod.Word”

我有以下功能

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

当模块 Mod n 取自函数的参数时,如何在函数内部使用类型 n

解决方法

您使用 SomeMod

cypher :: Natural -> Integer -> Integer -> SomeMod
cypher n e m = (m `modulo` n) ^ e

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。