简体   繁体   English

如何比较阿格达的Nats向量

[英]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.

相关问题 如何在Agda中构造一个可能非空的Set - How to construct a possibly nonempty Set in Agda 如何避免(不必要?)在Agda中重复使用公理? - How to avoid (unnecessary?) repetitive use of axioms in Agda? 如何在 Agda 中定义归纳定义类型的子公式? - How to define a subformula of an inductively defined type in Agda? 如何在 agda 中通过 W 类型进行编码? - How to encode via W-types in agda? 如何理解 agda 中的数据与记录功能? - How does one understand Data vs Record capabilities in agda? Agda:通常/花括号相对于彼此和“:”符号的使用方式 - Agda: How usual/curly braces are used relative to each other and to ':' sign 我怎样才能说服阿格达我的职能具有一定的价值? - How can I convince Agda that my function has a certain value? Agda 模式如何与相同类型的构造函数匹配? - How does agda pattern match with same typed constructors? 我如何让 Agda 的宇宙检查器相信我所做的事情是有根据的? - How do I convince Agda's universe checker that what I'm doing is well-founded? 一个人如何编写(和调试)一个依赖于两个参数的应用程序 apd2,并使用它来证明这种 ap 在 agda 中的功能性? - How does one write (and debug) a two arguement dependent application, apd2, and use this to prove functoriality of such ap in agda?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM