-
Notifications
You must be signed in to change notification settings - Fork 251
Add (propositional) equational reasoning combinators for vectors #2067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MatthewDaggitt
merged 20 commits into
agda:master
from
shhyou:vec-equational-reasoning-rebased
Oct 5, 2023
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
db26bb9
Add combinators for equational reasoning of vector
shhyou 48f12f4
review: rename module
shhyou 31f7917
review: wrap comments
shhyou da40fb8
review: only open CastReasoning locally
shhyou bef3328
Remove duplicate definition of cast-{is-id,trans}
shhyou 22a8d7e
review: use _≈[_]_ as the name of the equality
shhyou 4275945
review: remove extra parenthesis
shhyou eca0dfb
slightly cleanup the symmetric combinators
shhyou ce31244
Merge branch 'master' into vec-equational-reasoning-rebased
shhyou e11cbc5
Fixup of bef3328: cong _ refl is refl
shhyou db5c229
Add README for the library
shhyou ec17b45
Remove the syntax declaration of `_≈cong[_]_`
shhyou 38512c0
Merge branch 'master' into vec-equational-reasoning-rebased
shhyou 9da2ed3
delete git merge annotations
shhyou 47d0ff3
review: open ≡-Reasoning locally to avoid renaming CastReasoning
shhyou 01d7b1b
review: re-export cast-is-id and cast-trans
shhyou 90a124d
review: place reasoning combinators in a module
shhyou 1b7777d
cosmetic changes: fix README empty lines
shhyou 2be93c9
review: re-export ==-Reasoning
shhyou 7cb7559
Add pointers to the README before cast
shhyou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
src/Data/Vec/Relation/Binary/Reasoning/Propositional.agda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- The basic code for equational reasoning with displayed propositional equality over vectors | ||
shhyou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
module Data.Vec.Relation.Binary.Reasoning.Propositional {a} {A : Set a} where | ||
shhyou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
open import Data.Nat.Base using (ℕ; zero; suc) | ||
open import Data.Nat.Properties using (suc-injective) | ||
open import Data.Vec.Base | ||
open import Relation.Binary.Core using (REL; _⇒_) | ||
open import Relation.Binary.Definitions using (Sym; Trans) | ||
open import Relation.Binary.PropositionalEquality.Core | ||
using (_≡_; refl; trans; sym; cong; module ≡-Reasoning) | ||
|
||
private | ||
variable | ||
l m n o : ℕ | ||
xs ys zs : Vec A n | ||
|
||
-- Duplicate definition of cast-is-id and cast-trans to avoid circular dependency | ||
shhyou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private | ||
cast-is-id : .(eq : m ≡ m) (xs : Vec A m) → cast eq xs ≡ xs | ||
cast-is-id eq [] = refl | ||
cast-is-id eq (x ∷ xs) = cong (x ∷_) (cast-is-id (suc-injective eq) xs) | ||
|
||
cast-trans : .(eq₁ : m ≡ n) .(eq₂ : n ≡ o) (xs : Vec A m) → | ||
cast eq₂ (cast eq₁ xs) ≡ cast (trans eq₁ eq₂) xs | ||
cast-trans {m = zero} {n = zero} {o = zero} eq₁ eq₂ [] = refl | ||
cast-trans {m = suc _} {n = suc _} {o = suc _} eq₁ eq₂ (x ∷ xs) = | ||
cong (x ∷_) (cast-trans (suc-injective eq₁) (suc-injective eq₂) xs) | ||
|
||
|
||
|
||
≈-by : .(eq : n ≡ m) → REL (Vec A n) (Vec A m) _ | ||
≈-by eq xs ys = (cast eq xs ≡ ys) | ||
|
||
infix 3 ≈-by | ||
syntax ≈-by n≡m xs ys = xs ≈[ n≡m ] ys | ||
shhyou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
---------------------------------------------------------------- | ||
shhyou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- ≈-by is ‘reflexive’, ‘symmetric’ and ‘transitive’ | ||
|
||
≈-reflexive : ∀ {n} → _≡_ ⇒ ≈-by {n} refl | ||
≈-reflexive {x = x} eq = trans (cast-is-id refl x) eq | ||
|
||
≈-sym : .{m≡n : m ≡ n} → Sym (≈-by m≡n) (≈-by (sym m≡n)) | ||
≈-sym {m≡n = m≡n} {xs} {ys} xs≈ys = begin | ||
cast (sym m≡n) ys ≡˘⟨ cong (cast (sym m≡n)) xs≈ys ⟩ | ||
cast (sym m≡n) (cast m≡n xs) ≡⟨ cast-trans m≡n (sym m≡n) xs ⟩ | ||
cast (trans m≡n (sym m≡n)) xs ≡⟨ cast-is-id (trans m≡n (sym m≡n)) xs ⟩ | ||
xs ∎ | ||
where open ≡-Reasoning | ||
|
||
≈-trans : ∀ .{m≡n : m ≡ n} .{n≡o : n ≡ o} → Trans (≈-by m≡n) (≈-by n≡o) (≈-by (trans m≡n n≡o)) | ||
≈-trans {m≡n = m≡n} {n≡o} {xs} {ys} {zs} xs≈ys ys≈zs = begin | ||
cast (trans m≡n n≡o) xs ≡˘⟨ cast-trans m≡n n≡o xs ⟩ | ||
cast n≡o (cast m≡n xs) ≡⟨ cong (cast n≡o) xs≈ys ⟩ | ||
cast n≡o ys ≡⟨ ys≈zs ⟩ | ||
zs ∎ | ||
where open ≡-Reasoning | ||
|
||
------------------------------------------------------------------------ | ||
-- Reasoning combinators | ||
|
||
begin_ : ∀ .{m≡n : m ≡ n} {xs : Vec A m} {ys} → xs ≈[ m≡n ] ys → cast m≡n xs ≡ ys | ||
begin xs≈ys = xs≈ys | ||
|
||
_∎ : (xs : Vec A n) → cast refl xs ≡ xs | ||
_∎ xs = ≈-reflexive refl | ||
|
||
_≈⟨⟩_ : ∀ .{m≡n : m ≡ n} (xs : Vec A m) {ys} → (xs ≈[ m≡n ] ys) → (xs ≈[ m≡n ] ys) | ||
shhyou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
xs ≈⟨⟩ xs≈ys = xs≈ys | ||
|
||
-- composition of _≈[_]_ | ||
step-≈ : ∀ .{m≡n : m ≡ n}.{m≡o : m ≡ o} (xs : Vec A m) {ys : Vec A n} {zs : Vec A o} → | ||
(ys ≈[ trans (sym m≡n) m≡o ] zs) → (xs ≈[ m≡n ] ys) → (xs ≈[ m≡o ] zs) | ||
step-≈ xs ys≈zs xs≈ys = ≈-trans xs≈ys ys≈zs | ||
|
||
-- composition of the equality type on the right-hand side of _≈[_]_, or escaping to ordinary _≡_ | ||
step-≃ : ∀ .{m≡n : m ≡ n} (xs : Vec A m) {ys zs} → (ys ≡ zs) → (xs ≈[ m≡n ] ys) → (xs ≈[ m≡n ] zs) | ||
step-≃ xs ys≡zs xs≈ys = ≈-trans xs≈ys (≈-reflexive ys≡zs) | ||
|
||
-- composition of the equality type on the left-hand side of _≈[_]_ | ||
step-≂ : ∀ .{m≡n : m ≡ n} (xs : Vec A m) {ys zs} → (ys ≈[ m≡n ] zs) → (xs ≡ ys) → (xs ≈[ m≡n ] zs) | ||
step-≂ xs ys≈zs xs≡ys = ≈-trans (≈-reflexive xs≡ys) ys≈zs | ||
|
||
-- `cong` after a `_≈[_]_` step that exposes the `cast` to the `cong` operation | ||
≈-cong : ∀ .{l≡o : l ≡ o} .{m≡n : m ≡ n} {xs : Vec A m} {ys zs} (f : Vec A o → Vec A n) → | ||
(ys ≈[ l≡o ] zs) → (xs ≈[ m≡n ] f (cast l≡o ys)) → (xs ≈[ m≡n ] f zs) | ||
≈-cong f ys≈zs xs≈fys = trans xs≈fys (cong f ys≈zs) | ||
shhyou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
-- symmetric version of each of the operator | ||
step-≈˘ : ∀ .{n≡m : n ≡ m}.{m≡o : m ≡ o} (xs : Vec A m) {ys : Vec A n} {zs : Vec A o} → | ||
(ys ≈[ trans n≡m m≡o ] zs) → (ys ≈[ n≡m ] xs) → (xs ≈[ m≡o ] zs) | ||
step-≈˘ xs ys≈zs ys≈xs = ≈-trans (≈-sym ys≈xs) ys≈zs | ||
|
||
step-≃˘ : ∀ .{m≡n : m ≡ n} (xs : Vec A m) {ys zs} → (ys ≡ zs) → (ys ≈[ sym m≡n ] xs) → (xs ≈[ m≡n ] zs) | ||
step-≃˘ xs ys≡zs ys≈xs = trans (≈-sym ys≈xs) ys≡zs | ||
|
||
step-≂˘ : ∀ .{m≡n : m ≡ n} (xs : Vec A m) {ys zs} → (ys ≈[ m≡n ] zs) → (ys ≡ xs) → (xs ≈[ m≡n ] zs) | ||
step-≂˘ xs ys≈zs ys≡xs = step-≂ xs ys≈zs (sym ys≡xs) | ||
|
||
|
||
---------------------------------------------------------------- | ||
-- convenient syntax for ‘equational’ reasoning | ||
|
||
infix 1 begin_ | ||
infixr 2 step-≃ step-≂ step-≃˘ step-≂˘ step-≈ step-≈˘ _≈⟨⟩_ ≈-cong | ||
infix 3 _∎ | ||
|
||
syntax step-≃ xs ys≡zs xs≈ys = xs ≃⟨ xs≈ys ⟩ ys≡zs | ||
syntax step-≃˘ xs ys≡zs xs≈ys = xs ≃˘⟨ xs≈ys ⟩ ys≡zs | ||
syntax step-≂ xs ys≈zs xs≡ys = xs ≂⟨ xs≡ys ⟩ ys≈zs | ||
syntax step-≂˘ xs ys≈zs ys≡xs = xs ≂˘⟨ ys≡xs ⟩ ys≈zs | ||
syntax step-≈ xs ys≈zs xs≈ys = xs ≈⟨ xs≈ys ⟩ ys≈zs | ||
syntax step-≈˘ xs ys≈zs ys≈xs = xs ≈˘⟨ ys≈xs ⟩ ys≈zs | ||
syntax ≈-cong f ys≈zs xs≈fys = xs≈fys ≈cong[ f ] ys≈zs | ||
|
||
{- | ||
-- An equational reasoning example, demonstrating nested uses of the cong operator | ||
|
||
cast-++ˡ : ∀ .(eq : n ≡ o) (xs : Vec A n) {ys : Vec A m} → | ||
cast (cong (_+ m) eq) (xs ++ ys) ≡ cast eq xs ++ ys | ||
|
||
example : ∀ .(eq : (m + 1) + n ≡ n + suc m) a (xs : Vec A m) (ys : Vec A n) → | ||
cast eq (reverse ((xs ++ [ a ]) ++ ys)) ≡ ys ʳ++ reverse (xs ∷ʳ a) | ||
example {m = m} {n} eq a xs ys = begin | ||
reverse ((xs ++ [ a ]) ++ ys) ≈˘⟨ cast-reverse (cong (_+ n) (ℕₚ.+-comm 1 m)) ((xs ∷ʳ a) ++ ys) | ||
≈cong[ reverse ] cast-++ˡ (ℕₚ.+-comm 1 m) (xs ∷ʳ a) | ||
≈cong[ (_++ ys) ] unfold-∷ʳ _ a xs ⟩ | ||
reverse ((xs ∷ʳ a) ++ ys) ≈⟨ reverse-++ (ℕₚ.+-comm (suc m) n) (xs ∷ʳ a) ys ⟩ | ||
reverse ys ++ reverse (xs ∷ʳ a) ≂˘⟨ unfold-ʳ++ ys (reverse (xs ∷ʳ a)) ⟩ | ||
ys ʳ++ reverse (xs ∷ʳ a) ∎ | ||
-} | ||
shhyou marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.