[英]How to compare Vectors of Nats in Agda
I'm trying to use Decidable Equality to compare two Vectors of Nats in Agda. 我正在尝试使用可判定的相等性比较Agda中的两个Nats向量。 I've tried opening the Vector Equality module, passing the Nat DecSetoid as an argument, as follows:
我尝试打开Vector Equality模块,将Nat DecSetoid作为参数传递,如下所示:
open import Data.Nat
open import Data.Vec
open import Relation.Binary.PropositionalEquality
import Data.Vec.Equality
myFunction : {n : ℕ} -> Vec ℕ n -> Vec ℕ n -> ℕ
myFunction v1 v2
with v1 Data.Vec.Equality.DecidableEquality.≟ v2
... | _ = {!!}
where
open Data.Vec.Equality.DecidableEquality (Relation.Binary.PropositionalEquality.decSetoid Data.Nat._≟_)
However, I get the following error: 但是,出现以下错误:
Vec ℕ .n !=< .Relation.Binary.DecSetoid (_d₁_6 v1 v2) (_d₂_7 v1 v2)
of type Set
when checking that the expression v1 has type
.Relation.Binary.DecSetoid (_d₁_6 v1 v2) (_d₂_7 v1 v2)
I'm not sure what I'm doing wrong. 我不确定自己在做什么错。 Am I using the module system wrong, or do I need to use ≟ differently?
我使用的模块系统是否错误,还是需要以其他方式使用??
The problem here is that the where
clause does not bring the identifiers in scope for the expressions in the with
. 这里的问题在于,
where
子句不会将with
的标识符带入表达式的范围内。 So when you use Data.Vec.Equality.DecidableEquality.≟
, you're not refering to the one specialised to vectors of natural numbers but to the general one define in Data.Vec.Equality
. 因此,当您使用
Data.Vec.Equality.DecidableEquality.≟
,并不是指专门针对自然数向量的那个,而是指Data.Vec.Equality
定义的一般一个。 That's why Agda expects a DecSetoid
as the first argument and complains. 这就是为什么Agda期望
DecSetoid
作为第一个参数并抱怨的原因。
A possible fix is to name the module you are interested in first and then used a qualified name to refer to its _≟_
. 可能的解决方法是先命名您感兴趣的模块,然后使用限定名称来引用其
_≟_
。 I've taken the liberty of using shorter names by defining aliases via as
: 我通过使用
as
定义别名来使用较短的名称:
open import Relation.Binary.PropositionalEquality as PropEq
import Data.Vec.Equality as VecEq
module VecNatEq = VecEq.DecidableEquality (PropEq.decSetoid Data.Nat._≟_)
myFunction : {n : ℕ} -> Vec ℕ n -> Vec ℕ n -> ℕ
myFunction v1 v2
with v1 VecNatEq.≟ v2
... | _ = {!!}
You can also define, import and open modules locally: 您还可以在本地定义,导入和打开模块:
open import Data.Nat
open import Data.Vec
open import Relation.Binary.PropositionalEquality as P
import Data.Vec.Equality as VE
myFunction : {n : ℕ} -> Vec ℕ n -> Vec ℕ n -> ℕ
myFunction v1 v2 with let module DVE = VE.DecidableEquality (decSetoid _≟_) in v1 DVE.≟ v2
... | _ = {!!}
However you don't really need with
in your case — pattern matching lambda is enough: 但是你并不真的需要
with
你的情况-模式匹配的λ是不够的:
open import Function
open import Relation.Nullary
myFunction : {n : ℕ} -> Vec ℕ n -> Vec ℕ n -> ℕ
myFunction v1 v2 = case v1 DVE.≟ v2 of λ
{ (no p) -> {!!}
; (yes p) -> {!!}
}
where module DVE = VE.DecidableEquality (decSetoid _≟_)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.