在 Coq 中证明共归纳惰性列表的相等性

Cry*_*sis 3 coq lazy-sequences coinduction

我正在试验 Coq Coinductive 类型。我使用 Coq'Art 书(第 13.1.4 节)中的惰性列表类型:

Set Implicit Arguments.

CoInductive LList (A:Set) : Set :=
| LNil : LList A
| LCons : A -> LList A -> LList A.
Implicit Arguments LNil [A].

CoFixpoint LAppend (A:Set) (u v:LList A) : LList A :=
  match u with
  | LNil => v
  | LCons a u' => LCons a (LAppend u' v)
  end.
Run Code Online (Sandbox Code Playgroud)

为了匹配保护条件,我还使用了本书中的以下分解函数:

Definition LList_decomp (A:Set) (l:LList A) : LList A :=
  match l with
  | LNil => LNil
  | LCons a l' => LCons a l'
  end.


Lemma LList_decompose : forall (A:Set) (l:LList A), l = LList_decomp l.
Proof.
 intros.
 case l.
 simpl.
 reflexivity.
 intros.
 simpl. 
 reflexivity.
Qed.
Run Code Online (Sandbox Code Playgroud)

LNil左中性的引理很容易证明:

Lemma LAppend_LNil : forall (A:Set) (v:LList A), LAppend LNil v = v.
Proof.
 intros A v.
 rewrite LList_decompose with (l:= LAppend LNil v).
 case v.
 simpl.
 reflexivity.
 intros.
 simpl.
 reflexivity.
Qed.
Run Code Online (Sandbox Code Playgroud)

但是我被证明这LNil也是正确的中立的:

Lemma LAppend_v_LNil : forall (A:Set) (v:LList A), LAppend v LNil = v.
Run Code Online (Sandbox Code Playgroud)

在亚瑟的回答之后,我尝试了新的平等:

Lemma LAppend_v_LNil : forall (A:Set) (v:LList A), LListEq  (LAppend v LNil)  v.
Proof.
 intros.
 cofix.
 destruct v.
 rewrite LAppend_LNil.
 apply LNilEq.
Run Code Online (Sandbox Code Playgroud)

我被困在这里了。Coq 的回答是:

1 subgoal
A : Set
a : A
v : LList A
LAppend_v_LNil : LListEq (LAppend (LCons a v) LNil) (LCons a v)
______________________________________(1/1)
LListEq (LAppend (LCons a v) LNil) (LCons a v)
Run Code Online (Sandbox Code Playgroud)

在 Eponier 的回答之后,我想通过引入扩展公理来给它最后的润色:

Axiom LList_ext: forall (A:Set)(l1 l2: LList A), (LListEq l1 l2 ) -> l1 = l2.
Run Code Online (Sandbox Code Playgroud)

有了这个公理,我得到了引理的最终剪辑:

Lemma LAppend_v_LNil : forall (A:Set) (v:LList A), (LAppend v LNil) = v.
Proof.
 intros.
 apply LList_ext.
 revert v.
   cofix.
   intros.
   destruct v. Guarded. (* now we can safely destruct v *)
   - rewrite LAppend_LNil.
     constructor.
   - rewrite (LList_decompose (LAppend _ _)).
     simpl. constructor. apply LAppend_v_LNil.
Qed.
Run Code Online (Sandbox Code Playgroud)

现在,这是我对这个线程的最后一个问题:

  • 某些 Coq 库中是否已经存在这样的公理?
  • 该公理与 Coq 一致吗?
  • 该公理与 Coq 的哪些标准公理(例如,classic、UIP、fun ext、Streicher K)不一致?

Art*_*rim 5

你猜对了:就像函数一样,Coq 的通用相等概念太弱,对大多数共归纳类型都没有用。如果你想证明你的结果,你需要用eq列表相等的合归纳概念代替;例如:

CoInductive LListEq (A:Set) : LList A -> LList A -> Prop :=
| LNilEq : LListEq A LNil LNil
| LConsEq x lst1 lst2 : LListEq A lst1 lst2 -> 
  LListEq A (LCons x lst1) (LCons x lst2).
Run Code Online (Sandbox Code Playgroud)

操作无限对象是 Coq 中的一个庞大主题。如果您想了解更多信息,Adam Chlipala 的 CPDT 有一整关于联合归纳。