[英]What does it mean when Coq expands a function as part of the goal?
我试图解决以下定理,并陷入最后的simpl.
:
Lemma nonzeros_app : forall l1 l2 : natlist,
nonzeros (l1 ++ l2) = (nonzeros l1) ++ (nonzeros l2).
Proof.
intros l1 l2. induction l1 as [| n' l' IHl'].
-simpl. reflexivity.
-simpl.
Qed.
那时,Coq将目标从:
1 subgoal (ID 170)
n' : nat
l', l2 : natlist
IHl' : nonzeros (l' ++ l2) = nonzeros l' ++ nonzeros l2
============================
nonzeros ((n' :: l') ++ l2) = nonzeros (n' :: l') ++ nonzeros l2
至:
1 subgoal (ID 185)
n' : nat
l', l2 : natlist
IHl' : nonzeros (l' ++ l2) = nonzeros l' ++ nonzeros l2
============================
match n' with
| 0 => nonzeros (l' ++ l2)
| S _ => n' :: nonzeros (l' ++ l2)
end =
match n' with
| 0 => nonzeros l'
| S _ => n' :: nonzeros l'
end ++ nonzeros l2
在我看来,这完全是个谜。 Coq只是复制将函数的定义粘贴到我的目标中是什么意思? 我什至要怎么办?
问题背景:
有人告诉我解决方案是:
Lemma nonzeros_app : forall l1 l2 : natlist,
nonzeros (l1 ++ l2) = (nonzeros l1) ++ (nonzeros l2).
Proof.
intros l1 l2. induction l1.
- simpl. reflexivity.
- simpl. { induction n.
- ...
- ... }
Qed.
这让我想了解为什么他们在n
上使用归纳法,因为我觉得在那里永远不会发生归纳法。 所以我问,为什么? 但是我意识到,在问起我为什么之前甚至不了解证明状态之前,我就意识到了,因为这似乎只是将粘贴复制到了证明状态(这对我来说没有意义)。 因此,在我问为什么使用归纳法之前,我必须问一下在此之前证明状态是什么,也许这将阐明为什么对n
归纳法。
我假设您已通过以下方式(或类似方式)定义了nonzeros
:
Require Import List.
Import ListNotations.
Definition natlist := list nat.
Fixpoint nonzeros (l : natlist) :=
match l with
| [] => []
| 0 :: xs => nonzeros xs
| x :: xs => x :: nonzeros xs
end.
因此, nonzeros
是递归的,结构递减为l
。 Coq的simpl
策略采用了一种启发式方法,在该方法中,如果将固定点的定义应用于以构造函数作为开头符号的术语,则会展开固定点的定义。 在您的情况下,例如nonzeros (n' :: l')
,常量nonzeros
后跟由构造函数Cons
(= ::
nonzeros
构成的项。 Coq执行所谓的“减少增量”,用其定义替换nonzero
的出现。 由于该定义是match
,因此您将获得一个match
作为新术语。 进一步的替换确实使它简化了一点,但是不能消除两种情况:一种表示零水头,一种表示非零水头。
出现的nonzeros ((n' :: l') ++ l2)
发生同样的情况,首先将其简化为nonzeros (n' :: (l' ++ l2))
,因此参数的开头为Cons
如果要避免在简化时公开match
表达式,可以将以下伪指令放在nonzeros
的定义nonzeros
:
Arguments nonzeros l : simpl nomatch.
这特别是告诉simpl
,如果会最终在更改位置公开match
项,则避免扩展术语。
至于您的朋友在这里使用的induction
:它用于强制将案例拆分为n'
,以便可以分别处理每个案例( n' = 0
, n' = S _
)。 实际上,这里不需要归纳法。 一个简单的案例拆分( case n'
)将执行相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.