Skip to content

Commit

Permalink
Refactor AsmCombinator local lemmas
Browse files Browse the repository at this point in the history
  • Loading branch information
Lysxia committed Jan 15, 2020
1 parent e43cd6d commit 75a5a71
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 49 deletions.
50 changes: 1 addition & 49 deletions tutorial/AsmCombinators.v
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ From ITree Require Import
Basics.Category
Basics.CategorySub.

Require Import Asm Utils_tutorial Fin KTreeFin.
Require Import Asm Utils_tutorial Fin KTreeFin CatTheory.

Import CatNotations.
Local Open Scope cat_scope.
Expand Down Expand Up @@ -125,10 +125,6 @@ Definition id_asm {A} : asm A A := pure_asm id.
the only tricky part is to rename all labels appropriately.
*)

(** [(A + B) + (C + D) -> (A + C) + (B + D)]*)
Notation swap4 :=
(assoc_r >>> bimap (id_ _) (assoc_l >>> bimap swap (id_ _) >>> assoc_r) >>> assoc_l).

(** Combinator to append two asm programs, preserving their internal links.
Can be thought of as a "vertical composition", or a tensor product.
*)
Expand Down Expand Up @@ -180,39 +176,6 @@ Local Open Scope cat.
(* end hide *)
Section Correctness.

Section CategoryTheory.

Context
{obj : Type} {C : obj -> obj -> Type}
{bif : obj -> obj -> obj}
{Eq2_C : Eq2 C}
`{forall a b, Equivalence (eq2 (a := a) (b := b))}
`{Category obj C (Eq2C := _)}
`{Coproduct obj C (Eq2_C := _) (Cat_C := _) bif}.

Local Lemma aux_app_asm_correct1 (I J A B : obj) :
(assoc_r >>>
bimap (id_ I) (assoc_l >>> bimap swap (id_ B) >>> assoc_r) >>>
assoc_l)
⩯ bimap swap (id_ (bif A B)) >>>
(assoc_r >>>
(bimap (id_ J) assoc_l >>>
(assoc_l >>> (bimap swap (id_ B) >>> assoc_r)))).
Proof. cat_auto. Qed.

Local Lemma aux_app_asm_correct2 (I J B D : obj) :
(assoc_r >>>
bimap (id_ I) (assoc_l >>> bimap swap (id_ D) >>> assoc_r) >>>
assoc_l)
⩯ assoc_l >>>
(bimap swap (id_ D) >>>
(assoc_r >>>
(bimap (id_ J) assoc_r >>>
(assoc_l >>> bimap swap (id_ (bif B D)))))).
Proof. cat_auto. Qed.

End CategoryTheory.

Context {E : Type -> Type}.
Context {HasRegs : Reg -< E}.
Context {HasMemory : Memory -< E}.
Expand Down Expand Up @@ -282,9 +245,6 @@ Section CategoryTheory.
rewrite bind_bind; setoid_rewrite IH; reflexivity.
Qed.

Lemma lift_ktree_inr {A B} : lift_ktree_ E A (B + A) inr = inr_.
Proof. reflexivity. Qed.

Lemma unit_l'_id_sktree {n : nat}
: (unit_l' (C := sub (ktree E) fin) (bif := Nat.add) (i := 0)) ⩯ id_ n.
Proof.
Expand Down Expand Up @@ -396,14 +356,6 @@ Section CategoryTheory.
all: reflexivity.
Qed.

Lemma subpure_swap4 {A B C D} :
subpure (E := E) (n := (A + B) + (C + D)) swap4 ⩯ swap4.
Proof.
do 2 rewrite !fmap_cat0, fmap_bimap, fmap_id0.
rewrite !fmap_assoc_r, !fmap_assoc_l, fmap_swap.
reflexivity.
Qed.

(** Correctness of the [app_asm] combinator.
The resulting [asm] gets denoted to the bimap of its subcomponent.
The proof is a bit more complicated. It makes a lot more sense if drawn.
Expand Down
63 changes: 63 additions & 0 deletions tutorial/CatTheory.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
(* begin hide *)
From Coq Require Import
Morphisms.

From ITree Require Import
Basics.Category
Basics.CategorySub.

Import CatNotations.
Local Open Scope cat.
(* end hide *)

Section CategoryTheory.

Context
{obj : Type} {C : obj -> obj -> Type}
{bif : obj -> obj -> obj}
{Eq2_C : Eq2 C}
`{forall a b, Equivalence (eq2 (a := a) (b := b))}
`{Category obj C (Eq2C := _)}
`{Coproduct obj C (Eq2_C := _) (Cat_C := _) bif}.

Lemma aux_app_asm_correct1 (I J A B : obj) :
(assoc_r >>>
bimap (id_ I) (assoc_l >>> bimap swap (id_ B) >>> assoc_r) >>>
assoc_l)
⩯ bimap swap (id_ (bif A B)) >>>
(assoc_r >>>
(bimap (id_ J) assoc_l >>>
(assoc_l >>> (bimap swap (id_ B) >>> assoc_r)))).
Proof. cat_auto. Qed.

Lemma aux_app_asm_correct2 (I J B D : obj) :
(assoc_r >>>
bimap (id_ I) (assoc_l >>> bimap swap (id_ D) >>> assoc_r) >>>
assoc_l)
⩯ assoc_l >>>
(bimap swap (id_ D) >>>
(assoc_r >>>
(bimap (id_ J) assoc_r >>>
(assoc_l >>> bimap swap (id_ (bif B D)))))).
Proof. Admitted. (* cat_auto. Qed. *)

End CategoryTheory.

(** [(A + B) + (C + D) -> (A + C) + (B + D)]*)
Notation swap4 :=
(assoc_r >>> bimap (id_ _) (assoc_l >>> bimap swap (id_ _) >>> assoc_r) >>> assoc_l).

Require Import KTreeFin.

Lemma subpure_swap4 {E A B C D} :
subpure (E := E) (n := (A + B) + (C + D)) swap4 ⩯ swap4.
Proof.
rewrite !fmap_cat0, !fmap_assoc_r, !fmap_assoc_l.
do 2 (apply category_proper_cat; try reflexivity).
rewrite fmap_bimap, fmap_id0.
rewrite fmap_cat0.
apply (bifunctor_proper_bimap _ _); try reflexivity.
rewrite fmap_cat0, fmap_assoc_l, fmap_assoc_r, fmap_bimap.
rewrite fmap_swap, fmap_id0.
reflexivity.
Qed.
1 change: 1 addition & 0 deletions tutorial/_CoqProject
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Introduction.v
Utils_tutorial.v
Fin.v
KTreeFin.v
CatTheory.v
Imp.v
Asm.v
AsmCombinators.v
Expand Down

0 comments on commit 75a5a71

Please sign in to comment.