HITs-Examples/FiniteSets/implementations/interface.v

343 lines
8.9 KiB
Coq
Raw Normal View History

2017-08-07 12:20:43 +02:00
Require Import HoTT.
Require Import FSets.
Section interface.
Context `{Univalence}.
Variable (T : Type -> Type)
(f : forall A, T A -> FSet A).
2017-08-08 17:06:53 +02:00
Context `{forall A, hasMembership (T A) A
, forall A, hasEmpty (T A)
, forall A, hasSingleton (T A) A
, forall A, hasUnion (T A)
, forall A, hasComprehension (T A) A}.
2017-08-07 12:20:43 +02:00
Class sets :=
{
2017-08-08 17:06:53 +02:00
f_empty : forall A, f A = ;
2017-08-07 16:49:46 +02:00
f_singleton : forall A a, f A (singleton a) = {|a|};
f_union : forall A X Y, f A (union X Y) = (f A X) (f A Y);
2017-08-08 17:06:53 +02:00
f_filter : forall A φ X, f A (filter φ X) = {| f A X & φ |};
2017-08-07 16:49:46 +02:00
f_member : forall A a X, member a X = a (f A X)
2017-08-07 12:20:43 +02:00
}.
2017-08-15 22:05:31 +02:00
2017-08-15 20:08:16 +02:00
Global Instance f_surjective A `{sets} : IsSurjection (f A).
Proof.
unfold IsSurjection.
hinduction ; try (intros ; apply path_ishprop).
- simple refine (BuildContr _ _ _).
2017-08-15 22:05:31 +02:00
* refine (tr(;_)).
2017-08-15 20:08:16 +02:00
apply f_empty.
* intros ; apply path_ishprop.
- intro a.
simple refine (BuildContr _ _ _).
* refine (tr({|a|};_)).
apply f_singleton.
* intros ; apply path_ishprop.
2017-08-15 22:05:31 +02:00
- intros Y1 Y2 [X1' ?] [X2' ?].
2017-08-15 20:08:16 +02:00
simple refine (BuildContr _ _ _).
2017-08-15 22:05:31 +02:00
* simple refine (Trunc_rec _ X1') ; intros [X1 fX1].
simple refine (Trunc_rec _ X2') ; intros [X2 fX2].
2017-08-15 20:08:16 +02:00
refine (tr(X1 X2;_)).
rewrite f_union, fX1, fX2.
reflexivity.
* intros ; apply path_ishprop.
Defined.
End interface.
2017-08-15 20:08:16 +02:00
Section quotient_surjection.
Variable (A B : Type)
(f : A -> B)
(H : IsSurjection f).
Context `{IsHSet B} `{Univalence}.
2017-08-15 20:08:16 +02:00
Definition f_eq : relation A := fun a1 a2 => f a1 = f a2.
Definition quotientB : Type := quotient f_eq.
2017-08-15 22:05:31 +02:00
2017-08-15 20:08:16 +02:00
Global Instance quotientB_recursion : HitRecursion quotientB :=
{
indTy := _;
recTy :=
forall (P : Type) (HP: IsHSet P) (u : A -> P),
(forall x y : A, f_eq x y -> u x = u y) -> quotientB -> P;
H_inductor := quotient_ind f_eq ;
H_recursor := @quotient_rec _ f_eq _
}.
2017-08-15 20:08:16 +02:00
Global Instance R_refl : Reflexive f_eq.
Proof.
intro.
reflexivity.
Defined.
2017-08-15 20:08:16 +02:00
Global Instance R_sym : Symmetric f_eq.
Proof.
intros a b Hab.
apply (Hab^).
Defined.
2017-08-08 13:18:45 +02:00
2017-08-15 20:08:16 +02:00
Global Instance R_trans : Transitive f_eq.
Proof.
2017-08-15 20:08:16 +02:00
intros a b c Hab Hbc.
apply (Hab @ Hbc).
Defined.
2017-08-15 20:08:16 +02:00
Definition quotientB_to_B : quotientB -> B.
Proof.
2017-08-15 20:08:16 +02:00
hrecursion.
- apply f.
- done.
Defined.
2017-08-15 20:08:16 +02:00
Instance quotientB_emb : IsEmbedding (quotientB_to_B).
Proof.
2017-08-15 20:08:16 +02:00
apply isembedding_isinj_hset.
unfold isinj.
hrecursion ; [ | intros; apply path_ishprop ].
intro.
hrecursion ; [ | intros; apply path_ishprop ].
intros.
by apply related_classes_eq.
Defined.
2017-08-15 22:05:31 +02:00
2017-08-15 20:08:16 +02:00
Instance quotientB_surj : IsSurjection (quotientB_to_B).
Proof.
apply BuildIsSurjection.
intros Y.
destruct (H Y).
2017-08-15 22:05:31 +02:00
simple refine (Trunc_rec _ center) ; intros [a fa].
apply (tr(class_of _ a;fa)).
Defined.
2017-08-15 20:08:16 +02:00
Definition quotient_iso : quotientB <~> B.
Proof.
2017-08-15 20:08:16 +02:00
refine (BuildEquiv _ _ quotientB_to_B _).
2017-08-15 22:05:31 +02:00
apply isequiv_surj_emb ; apply _.
Defined.
2017-08-15 20:08:16 +02:00
Definition reflect_eq : forall (X Y : A),
f X = f Y -> f_eq X Y.
Proof.
done.
Defined.
2017-08-15 20:08:16 +02:00
Lemma same_class : forall (X Y : A),
class_of f_eq X = class_of f_eq Y -> f_eq X Y.
Proof.
intros.
simple refine (classes_eq_related _ _ _ _) ; assumption.
Defined.
2017-08-15 20:08:16 +02:00
End quotient_surjection.
Ltac reduce T :=
intros ;
repeat (rewrite (f_empty T _)
|| rewrite (f_singleton T _)
|| rewrite (f_union T _)
|| rewrite (f_filter T _)
|| rewrite (f_member T _)).
Ltac simplify T := intros ; autounfold in * ; apply reflect_eq ; reduce T.
Ltac reflect_equality T := simplify T ; eauto with lattice_hints typeclass_instances.
Ltac reflect_eq T := autounfold
; repeat (hinduction ; try (intros ; apply path_ishprop) ; intro)
; apply related_classes_eq
; reflect_equality T.
Section quotient_properties.
Variable (T : Type -> Type).
Variable (f : forall {A : Type}, T A -> FSet A).
Context `{sets T f}.
2017-08-15 20:08:16 +02:00
Definition set_eq A := f_eq (T A) (FSet A) (f A).
Definition View A : Type := quotientB (T A) (FSet A) (f A).
2017-08-15 22:05:31 +02:00
2017-08-15 20:08:16 +02:00
Definition View_rec2 {A} (P : Type) (HP : IsHSet P) (u : T A -> T A -> P) :
(forall (x x' : T A), set_eq A x x' -> forall (y y' : T A), set_eq A y y' -> u x y = u x' y') ->
forall (x y : View A), P.
Proof.
intros Hresp.
assert (resp1 : forall x y y', set_eq A y y' -> u x y = u x y').
{ intros x y y'.
apply (Hresp _ _ idpath).
}
assert (resp2 : forall x x' y, set_eq A x x' -> u x y = u x' y).
{ intros x x' y Hxx'.
apply Hresp. apply Hxx'.
reflexivity. }
unfold View.
hrecursion.
- intros a.
hrecursion.
+ intros b.
apply (u a b).
+ intros b b' Hbb'. simpl.
by apply resp1.
- intros a a' Haa'. simpl.
apply path_forall. red.
hinduction.
+ intros b. apply resp2. apply Haa'.
+ intros; apply HP.
Defined.
Definition well_defined_union (A : Type) (X1 X2 Y1 Y2 : T A) :
2017-08-08 13:18:45 +02:00
set_eq A X1 Y1 -> set_eq A X2 Y2 -> set_eq A (union X1 X2) (union Y1 Y2).
Proof.
intros HXY1 HXY2.
2017-08-15 20:08:16 +02:00
simplify T.
2017-08-08 13:18:45 +02:00
by rewrite HXY1, HXY2.
Defined.
Definition well_defined_filter (A : Type) (ϕ : A -> Bool) (X Y : T A) :
2017-08-08 13:18:45 +02:00
set_eq A X Y -> set_eq A (filter ϕ X) (filter ϕ Y).
Proof.
intros HXY.
2017-08-15 20:08:16 +02:00
simplify T.
2017-08-08 13:18:45 +02:00
by rewrite HXY.
Defined.
2017-08-15 20:08:16 +02:00
Global Instance View_member A: hasMembership (View A) A.
Proof.
intros a ; unfold View.
hrecursion.
- apply (member a).
- intros X Y HXY.
reduce T.
2017-08-15 22:05:31 +02:00
apply (ap _ HXY).
2017-08-15 20:08:16 +02:00
Defined.
Global Instance View_empty A: hasEmpty (View A).
Proof.
apply (class_of _ ).
Defined.
2017-08-15 20:08:16 +02:00
Global Instance View_singleton A: hasSingleton (View A) A.
Proof.
2017-08-15 20:08:16 +02:00
intros a.
apply (class_of _ {|a|}).
Defined.
2017-08-15 20:08:16 +02:00
Instance View_max A : maximum (View A).
2017-08-08 17:06:53 +02:00
Proof.
2017-08-15 20:08:16 +02:00
simple refine (View_rec2 _ _ _ _).
- intros a b.
apply (class_of _ (union a b)).
- intros x x' Hxx' y y' Hyy' ; simpl.
apply related_classes_eq.
2017-08-15 22:05:31 +02:00
eapply well_defined_union ; eauto.
2017-08-08 17:06:53 +02:00
Defined.
2017-08-15 20:08:16 +02:00
Global Instance View_union A: hasUnion (View A).
2017-08-08 17:06:53 +02:00
Proof.
2017-08-15 20:08:16 +02:00
apply max_L.
2017-08-08 17:06:53 +02:00
Defined.
2017-08-15 20:08:16 +02:00
Global Instance View_comprehension A: hasComprehension (View A) A.
2017-08-08 17:06:53 +02:00
Proof.
2017-08-15 20:08:16 +02:00
intros ϕ ; unfold View.
hrecursion.
- intros X.
apply (class_of _ (filter ϕ X)).
- intros X X' HXX' ; simpl.
apply related_classes_eq.
2017-08-15 22:05:31 +02:00
eapply well_defined_filter ; eauto.
2017-08-08 17:06:53 +02:00
Defined.
2017-08-15 20:08:16 +02:00
Hint Unfold Commutative Associative Idempotent NeutralL NeutralR.
Instance bottom_view A : bottom (View A).
Proof.
unfold bottom.
apply .
Defined.
Global Instance view_lattice A : JoinSemiLattice (View A).
Proof.
split ; reflect_eq T.
Defined.
2017-08-08 17:06:53 +02:00
2017-08-15 20:08:16 +02:00
End quotient_properties.
2017-08-08 17:06:53 +02:00
2017-08-15 20:08:16 +02:00
Arguments set_eq {_} _ {_} _ _.
2017-08-08 17:06:53 +02:00
2017-08-15 20:08:16 +02:00
Section properties.
Context `{Univalence}.
Variable (T : Type -> Type) (f : forall A, T A -> FSet A).
Context `{sets T f}.
2017-08-08 17:06:53 +02:00
2017-08-15 20:08:16 +02:00
Definition set_subset : forall A, T A -> T A -> hProp :=
fun A X Y => (f A X) (f A Y).
2017-08-08 17:06:53 +02:00
2017-08-15 20:08:16 +02:00
Definition empty_isIn : forall (A : Type) (a : A),
a = False_hp.
Proof.
by (reduce T).
Defined.
2017-08-08 17:06:53 +02:00
2017-08-15 20:08:16 +02:00
Definition singleton_isIn : forall (A : Type) (a b : A),
a {|b|} = merely (a = b).
Proof.
by (reduce T).
Defined.
2017-08-08 17:06:53 +02:00
2017-08-15 20:08:16 +02:00
Definition union_isIn : forall (A : Type) (a : A) (X Y : T A),
a (X Y) = lor (a X) (a Y).
Proof.
by (reduce T).
Defined.
2017-08-08 17:06:53 +02:00
2017-08-15 20:08:16 +02:00
Definition filter_isIn : forall (A : Type) (a : A) (ϕ : A -> Bool) (X : T A),
member a (filter ϕ X) = if ϕ a then member a X else False_hp.
Proof.
reduce T.
apply properties.comprehension_isIn.
Defined.
2017-08-08 17:06:53 +02:00
2017-08-15 20:08:16 +02:00
Definition reflect_f_eq : forall (A : Type) (X Y : T A),
class_of (set_eq f) X = class_of (set_eq f) Y -> set_eq f X Y.
Proof.
intros.
refine (same_class _ _ _ _ _ _) ; assumption.
Defined.
2017-08-08 17:06:53 +02:00
2017-08-15 20:08:16 +02:00
Lemma class_union (A : Type) (X Y : T A) :
class_of (set_eq f) (X Y) = (class_of (set_eq f) X) (class_of (set_eq f) Y).
Proof.
reflexivity.
Defined.
2017-08-08 17:06:53 +02:00
2017-08-15 20:08:16 +02:00
Lemma class_filter (A : Type) (X : T A) (ϕ : A -> Bool) :
class_of (set_eq f) ({|X & ϕ|}) = {|(class_of (set_eq f) X) & ϕ|}.
Proof.
2017-08-08 17:06:53 +02:00
reflexivity.
2017-08-15 22:05:31 +02:00
Defined.
2017-08-15 20:08:16 +02:00
Ltac via_quotient := intros ; apply reflect_f_eq
; rewrite ?class_union, ?class_filter
; eauto with lattice_hints typeclass_instances.
Lemma union_comm : forall A (X Y : T A),
set_eq f (X Y) (Y X).
Proof.
via_quotient.
Defined.
Lemma union_assoc : forall A (X Y Z : T A),
set_eq f ((X Y) Z) (X (Y Z)).
Proof.
via_quotient.
Defined.
Lemma union_idem : forall A (X : T A),
set_eq f (X X) X.
Proof.
via_quotient.
Defined.
Lemma union_neutral : forall A (X : T A),
set_eq f ( X) X.
Proof.
via_quotient.
Defined.
End properties.