我在理解如何使用证明中在Coq中定义的某些内容时遇到了一些麻烦。我有这个定义和功能的片段:
Inductive string : Set :=
| E : string
| s : nat -> string -> string.
Inductive deduce : Set :=
|de : string -> string -> deduce.
Infix "|=" := de.
Inductive Rules : deduce -> Prop :=
| compress : forall (n : nat) (A : string), rule (( s n ( s n A)) |= ( s n A))
| transitive : forall A B C : string, rule (A |= B) -> rule (B |= C) -> rule (A |= C).
Fixpoint RepString (n m : nat): string:=
match n with
|0 => E
|S n => s m ( RepString n m)
end.
Run Code Online (Sandbox Code Playgroud)
我需要证明一些看似简单的事情,但遇到两个问题:
Lemma LongCompress (C : string)(n : nat): n >=1 -> Rules
((RepString n 0 ) |= (s 0 E) ).
Proof.
intros.
induction n.
inversion H.
simpl.
apply compress.
Run Code Online (Sandbox Code Playgroud)
所以在这里我有一个问题,我得到:
"Unable to unify "Rules (s ?M1805 (s ?M1805 ?M1806) |= s ?M1805 ?M1806)" with
"Rules (s 0 (RepString n 0) |- s 0 E)".'"
Run Code Online (Sandbox Code Playgroud)
现在,我明白了为什么会收到错误了,尽管从技术上讲RepString n 0, s 0 (s 0 (s 0( ... s 0 E)))我只是找不到找到让coq知道的方法apply compress with而已,但我尝试弄乱了10种不同的东西,但我仍然无法正确解决。我需要像这样“展开”它(当然unfold不起作用...)。
我没有主意,非常感谢您对此提供的任何意见!
从现在开始编辑。
Inductive Rules : deduce -> Prop :=
| compress : forall (n : nat) (A : string), rule (( s n ( s n A)) |= ( s n A))
| transitive : forall A B C : string, rule (A |= B) -> rule (B |= C) -> rule (A |= C)
| inspection : forall (n m : nat) (A : string), m < n -> rule ((s n A) |- (s m A)).
Definition less (n :nat ) (A B : string) := B |= (s n A).
Lemma oneLess (n m : nat): rule (less 0 (RepString n 1) (RepString m 1)) <-> n< m.
Run Code Online (Sandbox Code Playgroud)
我已经概括了安东·特鲁诺夫(Anton Trunov)帮助我证明的引理,但是现在我碰到了另一堵墙。我认为问题可能始于我编写定理本身的方式,我将不胜感激。
我会证明一些更通用的东西:对于任何两个零为s = 0000...0和t =的非空字符串00...0,如果length s > length t,则s |= t,即
forall n m,
m <> 0 ->
n > m ->
Rules (RepString n 0 |= RepString m 0).
Run Code Online (Sandbox Code Playgroud)
这是一个辅助引理:
Require Import Coq.Arith.Arith.
Require Import Coq.omega.Omega.
Hint Constructors Rules. (* add this line after the definition of `Rules` *)
Lemma LongCompress_helper (n m k : nat):
n = (S m) + k ->
Rules (RepString (S n) 0 |= RepString (S m) 0).
Proof.
generalize dependent m.
generalize dependent n.
induction k; intros n m H.
- Search (?X + 0 = ?X). rewrite Nat.add_0_r in H.
subst. simpl. eauto.
- apply (transitive _ (RepString n 0) _); simpl in H; rewrite H.
+ simpl. constructor.
+ apply IHk. omega.
Qed.
Run Code Online (Sandbox Code Playgroud)
现在,我们可以轻松证明我们宣传的一般引理:
Lemma LongCompress_general (n m : nat):
m <> 0 ->
n > m ->
Rules (RepString n 0 |= RepString m 0).
Proof.
intros Hm Hn. destruct n.
- inversion Hn.
- destruct m.
+ exfalso. now apply Hm.
+ apply LongCompress_helper with (k := n - m - 1). omega.
Qed.
Run Code Online (Sandbox Code Playgroud)
很容易看出,任何足够长的零字符串都可以压缩为单例字符串0:
Lemma LongCompress (n : nat):
n > 1 -> Rules ( RepString n 0 |= s 0 E ).
Proof.
intro H. replace (s 0 E) with (RepString 1 0) by easy.
apply LongCompress_general; auto.
Qed.
Run Code Online (Sandbox Code Playgroud)