首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Agda:关于使用“`with`”的“`` `last`‘的证明

Agda:关于使用“`with`”的“`` `last`‘的证明
EN

Stack Overflow用户
提问于 2020-10-22 00:25:06
回答 1查看 70关注 0票数 0

我试图证明以下陈述

代码语言:javascript
复制
vecNat : ∀ {n} (xs : Vec ℕ n) → last (xs ∷ʳ 1) ≡ 1

但我对(x ∷ xs)的案子感到困惑。

代码语言:javascript
复制
vecNat5 : ∀ {n} (xs : Vec ℕ n) → last (xs ∷ʳ 1) ≡ 1
vecNat5 []       = refl
vecNat5 (x ∷ xs) = {!  0!}

目标是

代码语言:javascript
复制
?0 : last ((x ∷ xs) ∷ʳ 1) ≡ 1

我第一次尝试使用begin

代码语言:javascript
复制
vecNat5 : ∀ {n} (xs : Vec ℕ n) → last (xs ∷ʳ 1) ≡ 1
vecNat5 []       = refl
vecNat5 (x ∷ xs) =
  begin
    last ((x ∷ xs) ∷ʳ 1)
  ≡⟨⟩
    1
  ∎

但是得到了这样的错误:

代码语言:javascript
复制
1 !=
(last (x ∷ (xs ∷ʳ 1))
 | (initLast (x ∷ (xs ∷ʳ 1)) | initLast (xs ∷ʳ 1)))
of type ℕ
when checking that the expression 1 ∎ has type
last ((x ∷ xs) ∷ʳ 1) ≡ 1

因此,我研究了lastagda-stdlib/src/Data/Vec/Base.agda中的定义。

代码语言:javascript
复制
last : ∀ {n} → Vec A (1 + n) → A
last xs         with initLast xs
last .(ys ∷ʳ y) | (ys , y , refl) = y

注意到了with子句,所以我想我可以用with来验证一下。我还在https://agda.readthedocs.io/en/v2.6.1.1/language/with-abstraction.html?highlight=with#generalisation中看到了一个使用with的证明(涉及到filter)的例子。

所以我想试试这个

代码语言:javascript
复制
vecNat : ∀ {n} (xs : Vec ℕ n) → last (xs ∷ʳ 1) ≡ 1
vecNat []       = refl
vecNat (x ∷ xs) with last (xs ∷ʳ 1)
...                 | r = {!  0!}

我的目标是:

代码语言:javascript
复制
?0 : (last (x ∷ (xs ∷ʳ 1))
     | (initLast (x ∷ (xs ∷ʳ 1)) | initLast (xs ∷ʳ 1)))
    ≡ 1

我很困惑如何在这里前进。还是我从一个错误的方向开始?

谢谢!

编辑

当我尝试

代码语言:javascript
复制
vecNat : ∀ {n} (xs : Vec ℕ n) → last (xs ∷ʳ 1) ≡ 1
vecNat []                               = refl
vecNat (x ∷ xs)         with initLast (xs ∷ʳ 1)
...                         | (xs , x , refl) = ?

我得到:

代码语言:javascript
复制
I'm not sure if there should be a case for the constructor refl,
because I get stuck when trying to solve the following unification
problems (inferred index ≟ expected index):
  xs ∷ʳ 1 ≟ xs₁ ∷ʳ 1
when checking that the pattern refl has type xs ∷ʳ 1 ≡ xs₁ ∷ʳ 1

不太清楚为什么现在有xs₁,为什么不只是xs

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-22 09:41:25

下面是一个可能的解决方案,我将您的1更改为任意a,并将向量的类型改为泛型:

第一,一些进口:

代码语言:javascript
复制
module Vecnat where

open import Data.Nat
open import Data.Vec
open import Relation.Binary.PropositionalEquality
open import Data.Product

然后是一个简单但非常重要的属性,它声明在列表的开头添加一个元素不会改变它的最后一个元素:

代码语言:javascript
复制
prop : ∀ {a} {A : Set a} {n x} (xs : Vec A (suc n)) → last (x ∷ xs) ≡ last xs
prop xs with initLast xs
... | _ , _ , refl = refl

最后,你要找的证据是:

代码语言:javascript
复制
vecNat5 : ∀ {a} {A : Set a} {l n} (xs : Vec A n) → last (xs ∷ʳ l) ≡ l
vecNat5 [] = refl
vecNat5 (_ ∷ xs) = trans (prop (xs ∷ʳ _)) (vecNat5 xs)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64473511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档