【发布时间】:2016-04-22 20:42:30
【问题描述】:
我想知道是否有人可以解决 Agda 中的以下问题。我想将一个自然数 n 作为参数传递给 Agda 模块。在这个模块中,我构造了一个接受Fin n 类型参数的函数。当模式与此函数的参数匹配时,我得到n 可能是zero 的问题,因此Fin n 类型将为空。 Agda 不会接受 Fin n 的 zero 和 suc _ 构造函数。
奇怪的是,当你在函数本身中引入原始自然数时,Agda 似乎没有这个问题,并且函数编译正常。
下面是一个最小的例子:
open import Data.Nat
open import Data.Fin
open import Data.Bool
module Test (n : ℕ) where
-- Compiles
isZero₁ : ∀ {m : ℕ} → Fin m → Bool
isZero₁ zero = true
isZero₁ (suc _) = false
-- Does not compile with error: "suc n₁ != n of
-- type ℕ when checking that the pattern zero has type Fin n"
isZero₂ : Fin n → Bool
isZero₂ zero = true
isZero₂ (suc _) = false
在真正的模块中,我传入了 5 个其他参数,这些参数依赖于 n。因此我确实需要提供n 作为参数。同时,我还需要能够编写模式匹配Fin n 类型对象的函数。有人知道我该怎么做吗?
【问题讨论】:
-
不能根据
isZero₁定义isZero₂吗? -
isZero₁ zero = ...与isZero₁ {suc m} zero = ...相同。你将如何以同样的方式重写isZero₂?n在isZero₂和can't become non-free 中是免费的。 -
@user3237465
isZero₂ = isZero₁ {n}。我不知道你的实际代码是什么样的,这就是我问的原因。
标签: parameters module agda