显示 (head . init ) = Agda 中的 head

发布于 2024-09-13 08:28:17 字数 654 浏览 2 评论 0原文

我试图证明 Agda 中的一个简单引理,我认为这是正确的。

如果向量具有两个以上元素,则在获取 init 后获取其 head 与立即获取其 head 相同。< /p>

我将其表述如下:

lem-headInit : ∀{l} (xs : Vec ℕ (suc (suc l)))
                    -> head (init xs) ≡ head xs
lem-headInit (x ∷ xs) = ?

这给了我;

.l : ℕ
x  : ℕ
xs : Vec ℕ (suc .l)
------------------------------
Goal: head (init (x ∷ xs) | (initLast (x ∷ xs) | initLast xs)) ≡ x

作为回应。

我不完全理解如何读取 (init (x ∷ xs) | (initLast (x ∷ xs) | initLast xs)) 组件。我想我的问题是;是否可能,如何以及该术语的含义是什么。

非常感谢。

I'm trying to prove a simple lemma in Agda, which I think is true.

If a vector has more than two elements, taking its head following taking the init is the same as taking its head immediately.

I have formulated it as follows:

lem-headInit : ∀{l} (xs : Vec ℕ (suc (suc l)))
                    -> head (init xs) ≡ head xs
lem-headInit (x ∷ xs) = ?

Which gives me;

.l : ℕ
x  : ℕ
xs : Vec ℕ (suc .l)
------------------------------
Goal: head (init (x ∷ xs) | (initLast (x ∷ xs) | initLast xs)) ≡ x

as a response.

I do not entirely understand how to read the (init (x ∷ xs) | (initLast (x ∷ xs) | initLast xs)) component. I suppose my questions are; is it possible, how and what does that term mean.

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那伤。 2024-09-20 08:28:17

我不完全明白如何
读取 (init (x ∷ xs) | (initLast (x
∷ xs) | initLast xs))
组件。我
假设我的问题是;是吗
可能,这个术语如何以及什么意思
意思是。

这告诉您,值 init (x ∷ xs) 取决于 | 右侧所有内容的值。当您在 Agda 中的函数中证明某些内容时,您的证明必须具有原始定义的结构。

在这种情况下,您必须对 initLast 的结果进行区分,因为 initLast 的定义会在生成任何结果之前执行此操作。

init : ∀ {a n} {A : Set a} → Vec A (1 + n) → Vec A n
init xs         with initLast xs
                --  ⇧  The first thing this definition does is case on this value
init .(ys ∷ʳ y) | (ys , y , refl) = ys

这就是我们如何编写引理。

module inithead where

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

lem-headInit : {A : Set} {n : ℕ} (xs : Vec A (2 + n))
             → head (init xs) ≡ head xs

lem-headInit (x ∷ xs) with initLast xs
lem-headInit (x ∷ .(ys ∷ʳ y)) | ys , y , refl = refl

我冒昧地将你的引理推广到 Vec A ,因为引理不依赖于向量的内容。

I do not entirely understand how to
read the (init (x ∷ xs) | (initLast (x
∷ xs) | initLast xs))
component. I
suppose my questions are; is it
possible, how and what does that term
mean.

This tells you that the value init (x ∷ xs) depends on the value of everything to the right of the |. When you prove something about in a function in Agda your proof will have to have the structure of the original definition.

In this case you have to case on the result of initLast because the definition of initLast does this before producing any results.

init : ∀ {a n} {A : Set a} → Vec A (1 + n) → Vec A n
init xs         with initLast xs
                --  ⇧  The first thing this definition does is case on this value
init .(ys ∷ʳ y) | (ys , y , refl) = ys

So here is how we write the lemma.

module inithead where

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

lem-headInit : {A : Set} {n : ℕ} (xs : Vec A (2 + n))
             → head (init xs) ≡ head xs

lem-headInit (x ∷ xs) with initLast xs
lem-headInit (x ∷ .(ys ∷ʳ y)) | ys , y , refl = refl

I took the liberty of generalizing your lemma to Vec A since the lemma doesn't depend on the contents of the vector.

鹿! 2024-09-20 08:28:17

好的。我通过作弊得到了这个,我希望有人有更好的解决方案。我抛弃了从 init 中获得的所有额外信息,这些信息是根据 initLast 定义的,并创建了我自己的简单版本。

initLazy : ∀{A l} → Vec A (suc l) → Vec A l
initLazy (x ∷ []) = []
initLazy (x ∷ (y ∷ ys)) = x ∷ (initLazy (y ∷ ys))

现在引理是微不足道的。

还有其他优惠吗?

Ok. I've got this one by cheating and I'm hoping somebody has a better solution. I threw away all the extra information you get from init being defined in terms of initLast and created my own naive version.

initLazy : ∀{A l} → Vec A (suc l) → Vec A l
initLazy (x ∷ []) = []
initLazy (x ∷ (y ∷ ys)) = x ∷ (initLazy (y ∷ ys))

Now the lemma is trivial.

Any other offers?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文