Xash在Function to Determine if Nat is Divisible by 5 at Compile-Time上给了我一个有用的答案(我从我原来的长名字中重新命名):
onlyModBy5 : (n : Nat) -> n `modNat` 5 = 0 -> Nat
onlyModBy5 n prf = n以前的一个answer教我如何使用Refl参数在REPL上运行它:
-- 5 % 5 == 0, so it should compile
*Test> onlyModBy5 5 Refl
5 : Nat
-- 7 % 5 == 2, so it should not compile
*Test> onlyModBy5 7 Refl
(input):1:12:When checking an application of function Main.onlyModBy5:
Type mismatch between
x = x (Type of Refl)
and
modNat 7 5 = 0 (Expected type)
Specifically:
Type mismatch between
2
and
0然后,我尝试定义一个助手函数,它将为简洁性提供第二个prf (证据)参数。换句话说,我希望这个函数的调用者不必提供Refl参数。
onlyModBy5Short : (n : Nat) -> Nat
onlyModBy5Short n = onlyModBy5 n Refl但是,它没有编译:
When checking right hand side of onlyModBy5Short with expected type
Nat
When checking an application of function Main.onlyModBy5:
Type mismatch between
0 = 0 (Type of Refl)
and
modNat n 5 = 0 (Expected type)
Specifically:
Type mismatch between
0
and
Prelude.Nat.modNatNZ, mod' n 4 SIsNotZ n n 4
Holes: Main.onlyModBy5Short如果可能的话,如何编写这个函数?
发布于 2016-04-11 02:59:15
你可以把onlyModBy5的第二个论点变成 argument。
onlyModBy5 : (n : Nat) -> {auto prf : n `modNat` 5 = 0} -> Nat
onlyModBy5 n = n这是因为对于给定的n文本值,n `modNat` 5总是可以减少,所以n `modNat` 5 = 0总是会减少到0 = 0 (在这种情况下,构造函数Refl具有正确的类型),除非n确实不能被5整除。
实际上,这将允许您对打字机进行检查。
foo : Nat
foo = onlyModBy5 25但拒绝
bar : Nat
bar = onlyModBy5 4https://stackoverflow.com/questions/36539019
复制相似问题