简体   繁体   English

在小于n ^ 2的情况下,Agda中可判定的相等性?

[英]Decidable equality in Agda with less than n^2 cases?

I've got a datatype for the AST of a programming langauge that I'd like to reason about, but there are about 10 different constructors for the AST. 我有一个要推理的编程语言AST的数据类型,但是AST大约有10种不同的构造函数。

data Term : Set where
  UnitTerm : Term
  VarTerm : Var -> Term
  ...
  SeqTerm : Term -> Term -> Term

I'm trying to write a function that has decidable equality for syntax trees of this language. 我正在尝试编写一个对该语言的语法树具有可确定的相等性的函数。 In theory this is straightforward: there's nothing too complicated, it's just simple data being stored in the AST. 从理论上讲这很简单:没有什么太复杂的,只是简单的数据存储在AST中。

The problem is that writing such a function seems to require about 100 cases: for each constructor, there's 10 cases. 问题在于编写这样的函数似乎需要大约100种情况:每个构造函数有10种情况。

  eqDecide : (x : Term) -> (y : Term) -> Dec (x ≡ y)
  eqDecide UnitTerm UnitTerm = yes refl
  eqDecide UnitTerm (VarTerm x) = Generic.no (λ ())
  ...
  eqDecide UnitTerm (SeqTerm t1 t2) = Generic.no (λ ())
  EqDecide (VarTerm x) UnitTerm = Generic.no (λ ())
  ...

The problem is, there are a bunch of redundant cases. 问题是,有一堆多余的情况。 After the first pattern match where the constructors match, ideally I could match with underscore, since no possible other constructor could unify, but it doesn't appear that I can do that. 在构造函数匹配的第一个模式匹配之后,理想情况下,我可以与下划线匹配,因为其他任何构造函数都无法统一,但是看来我不能做到这一点。

I've tried and failed to use this library to derive the equality: I'm running into problems with strict positivity, as well as getting some general errors that I'm are pretty hard to debug. 我尝试使用该库来推导相等性失败,但失败了:我遇到了一些严格的正定性问题,并且遇到了一些我很难调试的常规错误。 The Agda Prelude also has some facility for this, but looks pretty immature, and is lacking some things that I need from the standard library. Agda Prelude也为此提供了一些功能,但是看起来还很不成熟,并且缺少标准库中我需要的一些东西。

How do people do decidable equality in practice? 人们在实践中如何实现可判定的平等? Do they suck it up and just write all 100 cases, or is there a trick I'm missing? 他们吸收了它,只写了全部100个案例,还是我想念的一个窍门? Is this just a place where the newness of the language is showing through? 这只是语言新颖性展现的地方吗?

If you want to avoid using reflection and still prove decidable equality in a linear number of cases, you could try the following approach: 如果要避免使用反射并仍然在线性情况下证明可判定的相等性,可以尝试以下方法:

  1. Define a function embed : Term → Nat (or to some other type for which decidable equality is easier to prove such as labelled trees). 定义一个embed : Term → Nat的函数embed : Term → Nat (或其他更容易证明可确定相等性的其他类型,例如带标签的树)。
  2. Prove that your function is indeed injective. 证明您的职能确实是内向型。
  3. Make use of the fact that your function is injective together with decidable equality on the result type to conclude decidable equality on Term s (see for example via-injection in the module Relation.Nullary.Decidable in the standard library). 利用您的函数与结果类型上的可判定相等性相结合的事实,得出Term上可判定的相等性(例如via-injection参见标准库中Relation.Nullary.Decidable模块中的via-injection )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM