【问题标题】:How to apply Fixpoint definitions within proofs in Coq?如何在 Coq 的证明中应用 Fixpoint 定义?
【发布时间】:2016-06-06 16:33:25
【问题描述】:

我很难理解如何在证明中使用我在 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.

我需要证明一些看似简单的事情,但我遇到了两个问题:

Lemma LongCompress (C : string)(n : nat): n >=1 -> Rules 
((RepString n 0 ) |= (s 0 E) ).
Proof.
intros.
induction n.
inversion H.
simpl.
apply compress.

所以这里我有一个问题,我得到:

"Unable to unify "Rules (s ?M1805 (s ?M1805 ?M1806) |= s ?M1805 ?M1806)" with 
"Rules (s 0 (RepString n 0) |- s 0 E)".'"

现在,我知道为什么会出现错误,虽然从技术上讲 RepString n 0s 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.

我已经概括了 Anton Trunov 帮助我证明的引理,但现在我遇到了另一面墙。我认为问题可能从我编写定理本身的方式开始,我会欣赏任何想法。

【问题讨论】:

    标签: coq


    【解决方案1】:

    我会证明一些更一般的东西:对于任何两个非空零字符串 s = 0000...0 和 t = 00...0,如果是 length s &gt; length t,那么是 s |= t,即

    forall n m,
      m <> 0 ->
      n > m -> 
      Rules (RepString n 0 |= RepString m 0).
    

    这是一个辅助引理:

    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.
    

    现在,我们可以轻松证明我们宣传的一般引理:

    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.
    

    很容易看出,任何足够长的零字符串都可以压缩成单例字符串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.
    

    【讨论】:

    • 何@"Anton Trunov",谢谢你的回答。我没有写我也有传递规则,但我现在已经添加了。所以他们的想法是“如果字符串的第一个和第二个元素相同,我可以删除其中一个”。数字:0000 |= 000 |= 00 |=0。你能给我一些关于如何以正确的方式写引理的提示吗?我没有看到如何解决它:(
    • @"Anton Trunov",感谢您更新此内容,这有助于我更好地了解情况,但我有几个问题,特别是与助手的问题。为什么我们需要搜索,它到底在做什么?我试过寻找它,但解释对我没有多大帮助。 + 和 - 对证明有什么作用?我从未见过他们,我想了解更多!我的最后一个问题是,当我进入apply (transitive _ (RepString n 0) _) 的部分时,我收到“n”不在环境中的错误,我觉得我在这里遗漏了一些东西 o.O 再次感谢您的帮助!
    • @Sara (1) Search 是一个白话命令(它不是一种策略),它可以帮助您根据某种模式找到一个引理(它在当前上下文,如导入的模块、当前文件的定义等)。在这种情况下,我想将S m + 0 重写为S m,这就是Nat.add_0_r 所做的(从Coq 8.5 开始)。 (2) +-*++--** 等被称为“子弹”,它们可以帮助您将证明构建成嵌套的子证明。同一级别的子目标必须具有相同的项目符号。例如。如果我有- destruct ?,那么嵌套子目标不能以-开头,请看答案。
    • @Sara (3) 你有没有删除- 子弹?如果eauto 策略失败,则可能会发生此失败。如果您忘记添加Hint Constructors Ruleseauto 将失败。
    • @"Anton Trunov" 对不起,我之前没有回答,我试图弄清楚但没有成功。我没有删除子弹,我确实添加了提示。我在 eauto 之后有 Error: Wrong bullet - : Current bullet - is not finished.,所以我认为你是对的,而且那里缺少一些东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多