Skip to content

Commit 96acd8a

Browse files
authored
Add Data.Vec.Relation.Unary.AllPairs (#1257)
1 parent f5497b0 commit 96acd8a

File tree

10 files changed

+482
-0
lines changed

10 files changed

+482
-0
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ New modules
172172
Text.Printf.Generic
173173
```
174174

175+
* A predicate for vectors in which every pair of elements is related.
176+
```
177+
Data.Vec.Relation.Unary.AllPairs
178+
Data.Vec.Relation.Unary.AllPairs.Properties
179+
```
180+
181+
* A predicate for vectors in which every element is unique.
182+
```
183+
Data.Vec.Relation.Unary.Unique.Propositional
184+
Data.Vec.Relation.Unary.Unique.Propositional.Properties
185+
Data.Vec.Relation.Unary.Unique.Setoid
186+
Data.Vec.Relation.Unary.Unique.Setoid.Properties
187+
```
188+
175189
Other major changes
176190
-------------------
177191

@@ -455,6 +469,7 @@ Other minor additions
455469
* Added new proofs to `Data.Vec.Properties`:
456470
```agda
457471
unfold-take : ∀ n {m} x (xs : Vec A (n + m)) → take (suc n) (x ∷ xs) ≡ x ∷ take n xs
472+
unfold-drop : ∀ n {m} x (xs : Vec A (n + m)) → drop (suc n) (x ∷ xs) ≡ drop n xs
458473
lookup-inject≤-take : ∀ m {n} (m≤m+n : m ≤ m + n) (i : Fin m) (xs : Vec A (m + n)) →
459474
lookup xs (Fin.inject≤ i m≤m+n) ≡ lookup (take m xs) i
460475
```
@@ -477,6 +492,15 @@ Other minor additions
477492
transpose : ∀ {m n} → Vector (Vector A n) m → Vector (Vector A m) n
478493
```
479494

495+
* Added new proofs to `Data.Vec.Relation.Unary.All.Properties`:
496+
```agda
497+
All-swap : ∀ {xs ys} → All (λ x → All (x ~_) ys) xs → All (λ y → All (_~ y) xs) ys
498+
tabulate⁺ : ∀ {f : Fin n → A} → (∀ i → P (f i)) → All P (tabulate f)
499+
tabulate⁻ : ∀ {f : Fin n → A} → All P (tabulate f) → (∀ i → P (f i))
500+
drop⁺ : ∀ {n} m {xs} → All P {m + n} xs → All P {n} (drop m xs)
501+
take⁺ : ∀ {n} m {xs} → All P {m + n} xs → All P {m} (take m xs)
502+
```
503+
480504
Refactorings
481505
------------
482506

src/Data/Vec/Properties.agda

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ unfold-take : ∀ n {m} x (xs : Vec A (n + m)) → take (suc n) (x ∷ xs) ≡ x
7676
unfold-take n x xs with splitAt n xs
7777
unfold-take n x .(xs ++ ys) | xs , ys , refl = refl
7878

79+
------------------------------------------------------------------------
80+
-- drop
81+
82+
unfold-drop : n {m} x (xs : Vec A (n + m)) drop (suc n) (x ∷ xs) ≡ drop n xs
83+
unfold-drop n x xs with splitAt n xs
84+
unfold-drop n x .(xs ++ ys) | xs , ys , refl = refl
85+
7986
------------------------------------------------------------------------
8087
-- lookup
8188

src/Data/Vec/Relation/Unary/All/Properties.agda

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@
88

99
module Data.Vec.Relation.Unary.All.Properties where
1010

11+
open import Data.Nat.Base using (zero; suc; _+_)
12+
open import Data.Fin.Base using (Fin; zero; suc)
1113
open import Data.List.Base using ([]; _∷_)
1214
open import Data.List.Relation.Unary.All as List using ([]; _∷_)
1315
open import Data.Product as Prod using (_×_; _,_; uncurry; uncurry′)
1416
open import Data.Vec.Base as Vec
17+
import Data.Vec.Properties as Vecₚ
1518
open import Data.Vec.Relation.Unary.All as All using (All; []; _∷_)
1619
open import Level using (Level)
1720
open import Function.Base using (_∘_; id)
1821
open import Function.Inverse using (_↔_; inverse)
1922
open import Relation.Unary using (Pred) renaming (_⊆_ to _⋐_)
23+
open import Relation.Binary as B using (REL)
2024
open import Relation.Binary.PropositionalEquality
2125
using (_≡_; refl; cong; cong₂; →-to-⟶)
2226

@@ -102,6 +106,51 @@ module _ {m} {P : Pred A p} where
102106
concat⁻ [] [] = []
103107
concat⁻ (xs ∷ xss) pxss = ++ˡ⁻ xs pxss ∷ concat⁻ xss (++ʳ⁻ xs pxss)
104108

109+
------------------------------------------------------------------------
110+
-- swap
111+
112+
module _ {_~_ : REL A B p} where
113+
114+
All-swap : {n m xs ys}
115+
All (λ x All (x ~_) ys) {n} xs
116+
All (λ y All (_~ y) xs) {m} ys
117+
All-swap {ys = []} _ = []
118+
All-swap {ys = y ∷ ys} [] = All.universal (λ _ []) (y ∷ ys)
119+
All-swap {ys = y ∷ ys} ((x~y ∷ x~ys) ∷ pxs) =
120+
(x~y ∷ (All.map All.head pxs)) ∷
121+
All-swap (x~ys ∷ (All.map All.tail pxs))
122+
123+
124+
------------------------------------------------------------------------
125+
-- tabulate
126+
127+
module _ {P : A Set p} where
128+
129+
tabulate⁺ : {n} {f : Fin n A}
130+
( i P (f i)) All P (tabulate f)
131+
tabulate⁺ {zero} Pf = []
132+
tabulate⁺ {suc n} Pf = Pf zero ∷ tabulate⁺ (Pf ∘ suc)
133+
134+
tabulate⁻ : {n} {f : Fin n A}
135+
All P (tabulate f) ( i P (f i))
136+
tabulate⁻ {suc n} (px ∷ _) zero = px
137+
tabulate⁻ {suc n} (_ ∷ pf) (suc i) = tabulate⁻ pf i
138+
139+
------------------------------------------------------------------------
140+
-- take and drop
141+
142+
module _ {P : A Set p} where
143+
144+
drop⁺ : {n} m {xs} All P {m + n} xs All P {n} (drop m xs)
145+
drop⁺ zero pxs = pxs
146+
drop⁺ (suc m) {x ∷ xs} (px ∷ pxs)
147+
rewrite Vecₚ.unfold-drop m x xs = drop⁺ m pxs
148+
149+
take⁺ : {n} m {xs} All P {m + n} xs All P {m} (take m xs)
150+
take⁺ zero pxs = []
151+
take⁺ (suc m) {x ∷ xs} (px ∷ pxs)
152+
rewrite Vecₚ.unfold-take m x xs = px ∷ take⁺ m pxs
153+
105154
------------------------------------------------------------------------
106155
-- toList
107156

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Vectors where every pair of elements are related (symmetrically)
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
open import Relation.Binary using (Rel)
10+
11+
module Data.Vec.Relation.Unary.AllPairs
12+
{a ℓ} {A : Set a} {R : Rel A ℓ} where
13+
14+
open import Data.Vec.Base using (Vec; []; _∷_)
15+
open import Data.Vec.Relation.Unary.All as All using (All; []; _∷_)
16+
open import Data.Product as Prod using (_,_; _×_; uncurry; <_,_>)
17+
open import Function using (id; _∘_)
18+
open import Level using (_⊔_)
19+
open import Relation.Binary as B using (Rel; _⇒_)
20+
open import Relation.Binary.Construct.Intersection renaming (_∩_ to _∩ᵇ_)
21+
open import Relation.Binary.PropositionalEquality
22+
open import Relation.Unary as U renaming (_∩_ to _∩ᵘ_) hiding (_⇒_)
23+
open import Relation.Nullary using (yes; no)
24+
import Relation.Nullary.Decidable as Dec
25+
open import Relation.Nullary.Product using (_×-dec_)
26+
27+
------------------------------------------------------------------------
28+
-- Definition
29+
30+
open import Data.Vec.Relation.Unary.AllPairs.Core public
31+
32+
------------------------------------------------------------------------
33+
-- Operations
34+
35+
head : {x n} {xs : Vec A n} AllPairs R (x ∷ xs) All (R x) xs
36+
head (px ∷ pxs) = px
37+
38+
tail : {x n} {xs : Vec A n} AllPairs R (x ∷ xs) AllPairs R xs
39+
tail (px ∷ pxs) = pxs
40+
41+
uncons : {x n} {xs : Vec A n} AllPairs R (x ∷ xs) All (R x) xs × AllPairs R xs
42+
uncons = < head , tail >
43+
44+
module _ {s} {S : Rel A s} where
45+
46+
map : {n} R ⇒ S AllPairs R {n} ⊆ AllPairs S {n}
47+
map ~₁⇒~₂ [] = []
48+
map ~₁⇒~₂ (x~xs ∷ pxs) = All.map ~₁⇒~₂ x~xs ∷ (map ~₁⇒~₂ pxs)
49+
50+
module _ {s t} {S : Rel A s} {T : Rel A t} where
51+
52+
zipWith : {n} R ∩ᵇ S ⇒ T AllPairs R {n} ∩ᵘ AllPairs S {n} ⊆ AllPairs T {n}
53+
zipWith f ([] , []) = []
54+
zipWith f (px ∷ pxs , qx ∷ qxs) = All.map f (All.zip (px , qx)) ∷ zipWith f (pxs , qxs)
55+
56+
unzipWith : {n} T ⇒ R ∩ᵇ S AllPairs T {n} ⊆ AllPairs R {n} ∩ᵘ AllPairs S {n}
57+
unzipWith f [] = [] , []
58+
unzipWith f (rx ∷ rxs) = Prod.zip _∷_ _∷_ (All.unzip (All.map f rx)) (unzipWith f rxs)
59+
60+
module _ {s} {S : Rel A s} where
61+
62+
zip : {n} AllPairs R {n} ∩ᵘ AllPairs S {n} ⊆ AllPairs (R ∩ᵇ S) {n}
63+
zip = zipWith id
64+
65+
unzip : {n} AllPairs (R ∩ᵇ S) {n} ⊆ AllPairs R {n} ∩ᵘ AllPairs S {n}
66+
unzip = unzipWith id
67+
68+
------------------------------------------------------------------------
69+
-- Properties of predicates preserved by AllPairs
70+
71+
allPairs? : {n} B.Decidable R U.Decidable (AllPairs R {n})
72+
allPairs? R? [] = yes []
73+
allPairs? R? (x ∷ xs) =
74+
Dec.map′ (uncurry _∷_) uncons (All.all (R? x) xs ×-dec allPairs? R? xs)
75+
76+
irrelevant : {n} B.Irrelevant R U.Irrelevant (AllPairs R {n})
77+
irrelevant irr [] [] = refl
78+
irrelevant irr (px₁ ∷ pxs₁) (px₂ ∷ pxs₂) =
79+
cong₂ _∷_ (All.irrelevant irr px₁ px₂) (irrelevant irr pxs₁ pxs₂)
80+
81+
satisfiable : U.Satisfiable (AllPairs R)
82+
satisfiable = [] , []
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Vectors where every pair of elements are related (symmetrically)
5+
------------------------------------------------------------------------
6+
7+
-- Core modules are not meant to be used directly outside of the
8+
-- standard library.
9+
10+
-- This module should be removable if and when Agda issue
11+
-- https://github.com/agda/agda/issues/3210 is fixed
12+
13+
{-# OPTIONS --without-K --safe #-}
14+
15+
open import Relation.Binary using (Rel)
16+
17+
module Data.Vec.Relation.Unary.AllPairs.Core
18+
{a ℓ} {A : Set a} (R : Rel A ℓ) where
19+
20+
open import Level
21+
open import Data.Vec.Base
22+
open import Data.Vec.Relation.Unary.All
23+
24+
------------------------------------------------------------------------
25+
-- Definition
26+
27+
-- AllPairs R xs means that every pair of elements (x , y) in xs is a
28+
-- member of relation R (as long as x comes before y in the vector).
29+
30+
infixr 5 _∷_
31+
32+
data AllPairs : {n} Vec A n Set (a ⊔ ℓ) where
33+
[] : AllPairs []
34+
_∷_ : {n x} {xs : Vec A n} All (R x) xs AllPairs xs AllPairs (x ∷ xs)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Properties related to AllPairs
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
module Data.Vec.Relation.Unary.AllPairs.Properties where
10+
11+
open import Data.Vec
12+
import Data.Vec.Properties as Vecₚ
13+
open import Data.Vec.Relation.Unary.All as All using (All; []; _∷_)
14+
import Data.Vec.Relation.Unary.All.Properties as Allₚ
15+
open import Data.Vec.Relation.Unary.AllPairs as AllPairs using (AllPairs; []; _∷_)
16+
open import Data.Bool.Base using (true; false)
17+
open import Data.Fin.Base using (Fin)
18+
open import Data.Fin.Properties using (suc-injective)
19+
open import Data.Nat.Base using (zero; suc; _+_)
20+
open import Function using (_∘_)
21+
open import Level using (Level)
22+
open import Relation.Binary using (Rel)
23+
open import Relation.Binary.PropositionalEquality using (_≢_)
24+
25+
private
26+
variable
27+
a b c p ℓ : Level
28+
A : Set a
29+
B : Set b
30+
C : Set c
31+
32+
------------------------------------------------------------------------
33+
-- Introduction (⁺) and elimination (⁻) rules for vector operations
34+
------------------------------------------------------------------------
35+
-- map
36+
37+
module _ {R : Rel A ℓ} {f : B A} where
38+
39+
map⁺ : {n xs} AllPairs (λ x y R (f x) (f y)) {n} xs
40+
AllPairs R {n} (map f xs)
41+
map⁺ [] = []
42+
map⁺ (x∉xs ∷ xs!) = Allₚ.map⁺ x∉xs ∷ map⁺ xs!
43+
44+
------------------------------------------------------------------------
45+
-- ++
46+
47+
module _ {R : Rel A ℓ} where
48+
49+
++⁺ : {n m xs ys} AllPairs R {n} xs AllPairs R {m} ys
50+
All (λ x All (R x) ys) xs AllPairs R (xs ++ ys)
51+
++⁺ [] Rys _ = Rys
52+
++⁺ (px ∷ Rxs) Rys (Rxys ∷ Rxsys) = Allₚ.++⁺ px Rxys ∷ ++⁺ Rxs Rys Rxsys
53+
54+
------------------------------------------------------------------------
55+
-- concat
56+
57+
module _ {R : Rel A ℓ} where
58+
59+
concat⁺ : {n m xss} All (AllPairs R {n}) {m} xss
60+
AllPairs (λ xs ys All (λ x All (R x) ys) xs) xss
61+
AllPairs R (concat xss)
62+
concat⁺ [] [] = []
63+
concat⁺ (pxs ∷ pxss) (Rxsxss ∷ Rxss) = ++⁺ pxs (concat⁺ pxss Rxss)
64+
(All.map Allₚ.concat⁺ (Allₚ.All-swap Rxsxss))
65+
66+
------------------------------------------------------------------------
67+
-- take and drop
68+
69+
module _ {R : Rel A ℓ} where
70+
71+
take⁺ : {n} m {xs} AllPairs R {m + n} xs AllPairs R {m} (take m xs)
72+
take⁺ zero pxs = []
73+
take⁺ (suc m) {x ∷ xs} (px ∷ pxs)
74+
rewrite Vecₚ.unfold-take m x xs = Allₚ.take⁺ m px ∷ take⁺ m pxs
75+
76+
drop⁺ : {n} m {xs} AllPairs R {m + n} xs AllPairs R {n} (drop m xs)
77+
drop⁺ zero pxs = pxs
78+
drop⁺ (suc m) {x ∷ xs} (_ ∷ pxs)
79+
rewrite Vecₚ.unfold-drop m x xs = drop⁺ m pxs
80+
81+
------------------------------------------------------------------------
82+
-- tabulate
83+
84+
module _ {R : Rel A ℓ} where
85+
86+
tabulate⁺ : {n} {f : Fin n A} ( {i j} i ≢ j R (f i) (f j))
87+
AllPairs R (tabulate f)
88+
tabulate⁺ {zero} fᵢ~fⱼ = []
89+
tabulate⁺ {suc n} fᵢ~fⱼ =
90+
Allₚ.tabulate⁺ (λ j fᵢ~fⱼ λ()) ∷
91+
tabulate⁺ (fᵢ~fⱼ ∘ (_∘ suc-injective))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Vectors made up entirely of unique elements (propositional equality)
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
module Data.Vec.Relation.Unary.Unique.Propositional {a} {A : Set a} where
10+
11+
open import Relation.Binary.PropositionalEquality using (setoid)
12+
open import Data.Vec.Relation.Unary.Unique.Setoid as SetoidUnique
13+
14+
------------------------------------------------------------------------
15+
-- Re-export the contents of setoid uniqueness
16+
17+
open SetoidUnique (setoid A) public
18+

0 commit comments

Comments
 (0)