【问题标题】:How to prove StronglySorted list consing in coq?如何在coq中证明强排序列表consing?
【发布时间】:2018-06-20 02:12:24
【问题描述】:

我正在尝试在 Coq 中制作河内塔作为学习练习。经过数小时徒劳无功的尝试,我在第一次证明中被困在最后一个目标上。

您能否解释一下我的程序失败的原因,以及如何纠正它?

编辑:回头看代码,看来我需要先证明StronglySorted le (l:list nat)才能证明ordered_stacking,不是吗?

Require Import List.
Require Import Arith.
Require Import Coq.Sorting.Sorting.

Definition stack_disk :=
  fun (n:nat) (l:list nat) =>
    match l with
      | nil => n::nil
      | n'::l' =>
          if n' <? n
          then n::l
          else l
    end.

Eval compute in (stack_disk 2 (1::0::nil)).
Eval compute in (stack_disk 2 (2::1::0::nil)).

Lemma ordered_stacking: forall (n:nat) (l:list nat),
  StronglySorted le l -> StronglySorted le (stack_disk n l) -> StronglySorted le (n::l).
  Proof.
    intros n l H.
    induction l as [|hl tl];simpl;auto.
    destruct (hl <? n).
    auto.
    constructor.
    apply H.

输出:

1 subgoal
n, hl : nat
tl : list nat
H : StronglySorted le (hl :: tl)
IHtl : StronglySorted le tl ->
       StronglySorted le (stack_disk n tl) -> StronglySorted le (n :: tl)
H0 : StronglySorted le (hl :: tl)
______________________________________(1/1)
Forall (le n) (hl :: tl)

【问题讨论】:

    标签: coq proof formal-verification


    【解决方案1】:

    问题是您没有记录 n &lt;= hl 在销毁该布尔值之后的事实。这是一个解决方案:

    Require Import List.
    Require Import Arith.
    Require Import Coq.Sorting.Sorting.
    
    Definition stack_disk :=
      fun (n:nat) (l:list nat) =>
        match l with
          | nil => n::nil
          | n'::l' =>
              if n' <? n
              then n::l
              else l
        end.
    
    Lemma ordered_stacking: forall (n:nat) (l:list nat),
      StronglySorted le l -> StronglySorted le (stack_disk n l) -> StronglySorted le (n::l).
    Proof.
      intros n [|m l].
      - intros _ _; repeat constructor.
      - simpl. intros H1 H2.
        destruct (Nat.ltb_spec m n); trivial.
        constructor; trivial.
        apply StronglySorted_inv in H1.
        destruct H1 as [_ H1].
        constructor; trivial.
        revert H1; apply Forall_impl.
        now intros p; apply Nat.le_trans.
    Qed.
    

    【讨论】:

      猜你喜欢
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多