nat元组的字典比较

jca*_*cai 1 coq

我正在使用nats的元组(特别是三元组nat*nat*nat),并且希望有一种按字典顺序比较元组的方法。与此等效:

Inductive lt3 : nat*nat*nat -> nat*nat*nat -> Prop :=
| lt3_1 : forall n1 n2 n3 m1 m2 m3, n1 < m1 -> lt3 (n1,n2,n3) (m1,m2,m3)
| lt3_2 : forall n1 n2 n3    m2 m3, n2 < m2 -> lt3 (n1,n2,n3) (n1,m2,m3)
| lt3_3 : forall n1 n2 n3       m3, n3 < m3 -> lt3 (n1,n2,n3) (n1,n2,m3).
Run Code Online (Sandbox Code Playgroud)

我想证明一些基本特性,例如传递性和充分根据。标准库中是否有可以完成大部分工作的内容?如果没有,我对有根据的最感兴趣。我将如何证明呢?

Art*_*rim 5

标准库对词典产品有自己的定义,并有充分根据的证明。但是,该定义的问题在于它是为相关对声明的:

lexprod : forall (A : Type) (B : A -> Type), relation {x : A & B x}
Run Code Online (Sandbox Code Playgroud)

如果需要,可以B使用form的常量类型族实例化fun _ => B',因为类型A * B'{x : A & B'}是同构的。但是,如果您想直接使用Coq类型的常规对,则可以简单地将证明复制为词典产品的更受限版本。证明不是很复杂,但是它需要对定义有充分根据的可访问性谓词进行嵌套归纳。

Require Import
  Coq.Relations.Relation_Definitions
  Coq.Relations.Relation_Operators.

Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.

Section Lexicographic.

Variables (A B : Type) (leA : relation A) (leB : relation B).

Inductive lexprod : A * B -> A * B -> Prop :=
| left_lex  : forall x x' y y', leA x x' -> lexprod (x, y) (x', y')
| right_lex : forall x y y',    leB y y' -> lexprod (x, y) (x, y').

Theorem wf_trans :
  transitive _ leA ->
  transitive _ leB ->
  transitive _ lexprod.
Proof.
intros tA tB [x1 y1] [x2 y2] [x3 y3] H.
inversion H; subst; clear H.
- intros H.
  inversion H; subst; clear H; apply left_lex; now eauto.
- intros H.
  inversion H; subst; clear H.
  + now apply left_lex.
  + now apply right_lex; eauto.
Qed.

Theorem wf_lexprod :
  well_founded leA ->
  well_founded leB ->
  well_founded lexprod.
Proof.
intros wfA wfB [x].
induction (wfA x) as [x _ IHx]; clear wfA.
intros y.
induction (wfB y) as [y _ IHy]; clear wfB.
constructor.
intros [x' y'] H.
now inversion H; subst; clear H; eauto.
Qed.

End Lexicographic.
Run Code Online (Sandbox Code Playgroud)

然后,您可以实例化此通用版本以恢复,例如,您对字典产品的自然数三进制定义:

Require Import Coq.Arith.Wf_nat.

Definition myrel : relation (nat * nat * nat) :=
  lexprod (lexprod lt lt) lt.

Lemma wf_myrel : well_founded myrel.
Proof. repeat apply wf_lexprod; apply lt_wf. Qed.
Run Code Online (Sandbox Code Playgroud)