我试图证明以下陈述
vecNat : ∀ {n} (xs : Vec ℕ n) → last (xs ∷ʳ 1) ≡ 1但我对(x ∷ xs)的案子感到困惑。
vecNat5 : ∀ {n} (xs : Vec ℕ n) → last (xs ∷ʳ 1) ≡ 1
vecNat5 [] = refl
vecNat5 (x ∷ xs) = {! 0!}目标是
?0 : last ((x ∷ xs) ∷ʳ 1) ≡ 1我第一次尝试使用begin
vecNat5 : ∀ {n} (xs : Vec ℕ n) → last (xs ∷ʳ 1) ≡ 1
vecNat5 [] = refl
vecNat5 (x ∷ xs) =
begin
last ((x ∷ xs) ∷ʳ 1)
≡⟨⟩
1
∎但是得到了这样的错误:
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因此,我研究了last在agda-stdlib/src/Data/Vec/Base.agda中的定义。
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)的例子。
所以我想试试这个
vecNat : ∀ {n} (xs : Vec ℕ n) → last (xs ∷ʳ 1) ≡ 1
vecNat [] = refl
vecNat (x ∷ xs) with last (xs ∷ʳ 1)
... | r = {! 0!}我的目标是:
?0 : (last (x ∷ (xs ∷ʳ 1))
| (initLast (x ∷ (xs ∷ʳ 1)) | initLast (xs ∷ʳ 1)))
≡ 1我很困惑如何在这里前进。还是我从一个错误的方向开始?
谢谢!
编辑
当我尝试
vecNat : ∀ {n} (xs : Vec ℕ n) → last (xs ∷ʳ 1) ≡ 1
vecNat [] = refl
vecNat (x ∷ xs) with initLast (xs ∷ʳ 1)
... | (xs , x , refl) = ?我得到:
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
发布于 2020-10-22 09:41:25
下面是一个可能的解决方案,我将您的1更改为任意a,并将向量的类型改为泛型:
第一,一些进口:
module Vecnat where
open import Data.Nat
open import Data.Vec
open import Relation.Binary.PropositionalEquality
open import Data.Product然后是一个简单但非常重要的属性,它声明在列表的开头添加一个元素不会改变它的最后一个元素:
prop : ∀ {a} {A : Set a} {n x} (xs : Vec A (suc n)) → last (x ∷ xs) ≡ last xs
prop xs with initLast xs
... | _ , _ , refl = refl最后,你要找的证据是:
vecNat5 : ∀ {a} {A : Set a} {l n} (xs : Vec A n) → last (xs ∷ʳ l) ≡ l
vecNat5 [] = refl
vecNat5 (_ ∷ xs) = trans (prop (xs ∷ʳ _)) (vecNat5 xs)https://stackoverflow.com/questions/64473511
复制相似问题