Cactus演示瞭如何解決我的問題:Helper Function to Determine if Nat `mod` 5 == 0。確定是否總和平均分5?
他寫道:
onlyModBy5 : (n : Nat) -> {auto prf : n `modNat` 5 = 0} -> Nat
onlyModBy5 n = n
於是,我試圖使用功能應用onlyModBy5
到Vect n Nat
的總和。
foo : (n : Nat) -> Vect n Nat -> Nat
foo n xs = onlyModBy5 $ sum xs
但我得到這個編譯時錯誤:
When checking right hand side of foo with expected type
Nat
When checking argument prf to function Main.onlyModBy5:
Can't find a value of type
Prelude.Nat.modNatNZ, mod' (foldrImpl (\meth =>
\meth =>
plus meth meth)
0
id
xs)
4
SIsNotZ
(foldrImpl (\meth =>
\meth =>
plus meth meth)
0
id
xs)
(foldrImpl (\meth =>
\meth =>
plus meth meth)
0
id
xs)
4 =
0
Holes: Main.foo
如何使用上述onlyModBy5
功能與foo
?
我想你可以通過明確地將'prf'傳遞給'onlyModBy5 k'來強調這個事實,即證明來自現在的地方,可以稍微改進'foo'。 – Cactus