Extract / use membership proof from a ssreflect finset

EDIT : I have made the example more minimal by introducing a structure that augments an element by carrying a proof of the membership of the said element to a set:

Structure elem_and_in_proof {T : finType} (s : {set T}) := {el :> T ; Helin : el \in s}.

Canonical eip_subType {T : finType} (s : {set T}) := Eval hnf in [subType for (@el T s)].
Canonical eip_eqType {T : finType} (s : {set T}) := Eval hnf in EqType _ [eqMixin of (elem_and_in_proof s) by <:].
Canonical eip_choiceType {T : finType} (s : {set T}) := Eval hnf in ChoiceType _ [choiceMixin of (elem_and_in_proof s) by <:].
Canonical eip_countType {T : finType} (s : {set T}) := Eval hnf in CountType _ [countMixin of (elem_and_in_proof s) by <:].
Canonical eip_subCountType {T : finType} (s : {set T}) := Eval hnf in [subCountType of (elem_and_in_proof s)].
Canonical eip_finType {T : finType} (s : {set T}) := Eval hnf in FinType _ [finMixin of (elem_and_in_proof s) by <:].

However, I need to apply this transformation to any given set, and I cannot seem to do it either :

Lemma equip_set_with_Helin {ft : finType} (s : {set ft}) : {set (elem_and_in_proof s)}.
Proof.
Admitted.

Since equip_set_with_Helin allows me to write the uniq_cons function, any help with either this or the original question would be greatly appreciated. Thanks!


I have a type dbranch consisting of a sequence over a finite type ft and a proof of uniq of this sequence. I want to be able, given an element t of type ft and a finset over dbranch to return the same set, where all dbranches have been "cons-ed" with t.

I needed to write a cons function for branches that takes as argument the hypothesis that the given element is not already part of the branch. I did not know how to write it as a usual function, so I approached it as a proof.

I then wrote uniq_cons, which tries to lift dcons to a set of dbranches. However, the application of its argument H requires a proof of b \in t (that's the placeholder in my code).

From mathcomp
Require Import ssreflect ssrbool ssrfun eqtype ssrnat seq choice fintype.
From mathcomp
Require Import tuple finfun bigop finset.

Variable ft : finType.

Structure dbranch := {branch :> seq ft ; buniq : uniq branch}.

Canonical dbranch_subType := Eval hnf in [subType for branch].
Canonical dbranch_eqType := Eval hnf in EqType _ [eqMixin of dbranch by <:].
Canonical dbranch_choiceType := Eval hnf in ChoiceType _ [choiceMixin of dbranch by <:].
Canonical dbranch_countType := Eval hnf in CountType _ [countMixin of dbranch by <:].
Canonical dbranch_subCountType := Eval hnf in [subCountType of dbranch].
Lemma dbranchFin : Finite.mixin_of [eqType of dbranch].
Admitted. 
Canonical dbranch_finType := Eval hnf in FinType _ dbranchFin.

Definition dcons (t : ft) (b : dbranch) (H : t \notin (branch b)) : dbranch.
Proof.
exists (t :: b) ; apply/andP ; split.
  - apply H.
  - apply (buniq b).
Qed.  

Definition uniq_cons (al : ft) (t : {set dbranch}) (H : forall b, (b \in t -> al \notin (branch b))) := 
  [set (dcons al b (H b _)) | b in t].

The fact that b is in t should be obvious as it appears directly in the comprehensive notation. However, I could not find in finset.v a way to "extract" or use that information, and Coq is not able to do it by itself. Thank you in advance for your help.

Upvotes: 1

Views: 210

Answers (1)

Arthur Azevedo De Amorim
Arthur Azevedo De Amorim

Reputation: 23592

To answer your amended question, you can do something like this:

From mathcomp
Require Import ssreflect ssrbool ssrfun eqtype ssrnat seq choice fintype finset.

Definition equip (T : finType) (A : {set T}) : {set {x : T | x \in A}} :=
  [set x in pmap insub (enum A)].

The sequence enum A enumerates all the elements of the set A. The insub : T -> option sT function coerces an element x : T to a subtype sT : subType P of T, provided that the predicate P holds of x. Given that all elements of enum A are in A by definition, this function simply behaves as Some. Finally, pmap maps a partial function over a sequence, discarding all the None results.

Upvotes: 2

Related Questions