Inductive subset of an inductive set in Coq

感情迁移 提交于 2019-12-01 00:40:24

Note that you will have to prove that isB mf enjoys proof irrelevance in your setting, otherwise Coq won't know that the projection mf is injective. Usually, you'd like equality in MF to imply equality in your subtype B.

I suggest the following variation:

Require Import Bool ZArith Eqdep_dec.

Inductive MF : Set :=
  | D : MF
  | cn : MF -> MF -> MF
  | dn : Z -> MF -> MF.

Inductive isB : MF -> Prop :=
  | DIsOK  : isB D
  | dnIsOK : forall z mf, isB mf -> isB (dn z mf).

Fixpoint isBb (mf : MF) : bool :=
  match mf with
  | D       => true
  | dn _ mf => isBb mf
  | _       => false
  end.

Lemma mfP mf : reflect (isB mf) (isBb mf).
Proof.
apply iff_reflect; split.
+ elim mf; auto; simpl; intros mf1 ihmf1 mf2 ihmf2.
  - now intros hisB; inversion hisB.
  - now inversion ihmf2; rewrite mf2.
+ now elim mf; simpl; try repeat (auto||constructor||congruence).
Qed.

Record B := mkB
  { mf  : MF
  ; prf : isBb mf = true
  }.

Coercion mf : B >-> MF.

(* From http://cstheory.stackexchange.com/questions/5158/prove-proof-irrelevance-in-coq *)
Theorem bool_pirrel : forall (b : bool) (p1 p2 : b = true), p1 = p2.
Proof.
intros; apply Eqdep_dec.eq_proofs_unicity; intros.
now destruct (Bool.bool_dec x y); tauto.
Qed.

Lemma valB b1 b2 : mf b1 = mf b2 -> b1 = b2.
Proof.
destruct b1, b2; simpl; intros ->.
now rewrite (bool_pirrel (isBb mf1) prf0 prf1).
Qed.

The math-comp library has great and systematic support for subtypes over boolean predicates, you may want to give it a go if you find yourself dealing with many subtypes.

You can define B as a record packaging an MF element together with a proof that it is built using only D and dn. To this end, you need to start by defining a predicate isB : MF -> Prop describing the elements of MF which are Bs.

Require Import ZArith.

Inductive MF : Set :=
  | D : MF
  | cn : MF -> MF -> MF
  | dn : Z -> MF -> MF.

Inductive isB : MF -> Prop :=
  | DIsOK  : isB D
  | dnIsOK : forall z mf, isB mf -> isB (dn z mf).

Record B := mkB
  { mf  : MF
  ; prf : isB mf
  }.

Coercion mf : B >-> MF.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!