Skip to content

Commit 93f5adb

Browse files
authored
[ fix #588 ] More uniform naming conventions for lookup functons (#1821)
1 parent 059b124 commit 93f5adb

File tree

6 files changed

+162
-113
lines changed

6 files changed

+162
-113
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,22 @@ Non-backwards compatible changes
555555
* Removed `m/n/o≡m/[n*o]` from `Data.Nat.Divisibility` and added a more general
556556
`m/n/o≡m/[n*o]` to `Data.Nat.DivMod` that doesn't require `n * o ∣ m`.
557557
558+
* Updated `Data.Vec.Relation.Unary.All` to match the conventions adopted in
559+
the equivalent `List` module.
560+
* Moved & renamed from `Data.Vec.Relation.Unary.All`
561+
to `Data.Vec.Relation.Unary.All.Properties`:
562+
```
563+
lookup ↦ lookup⁺
564+
tabulate ↦ lookup⁻
565+
```
566+
* Added the following new definitions to `Data.Vec.Relation.Unary.All`:
567+
```
568+
lookupAny : All P xs → (i : Any Q xs) → (P ∩ Q) (Any.lookup i)
569+
lookupWith : ∀[ P ⇒ Q ⇒ R ] → All P xs → (i : Any Q xs) → R (Any.lookup i)
570+
lookup : All P xs → (∀ {x} → x ∈ₚ xs → P x)
571+
lookupₛ : P Respects _≈_ → All P xs → (∀ {x} → x ∈ xs → P x)
572+
```
573+
558574
Major improvements
559575
------------------
560576
@@ -1666,6 +1682,11 @@ Other minor changes
16661682
xs≮[] : ∀ {n} (xs : Vec A n) → ¬ xs < []
16671683
```
16681684

1685+
* Added new functions in `Data.Vec.Relation.Unary.Any`:
1686+
```
1687+
lookup : Any P xs → A
1688+
```
1689+
16691690
* Added new functions in `Data.Vec.Relation.Unary.All`:
16701691
```
16711692
decide : Π[ P ∪ Q ] → Π[ All P ∪ Any Q ]

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,27 @@ open import Data.Fin.Base using (Fin; zero; suc)
1313
open import Data.Product as Prod using (_×_; _,_; uncurry; <_,_>)
1414
open import Data.Sum.Base as Sum using (inj₁; inj₂)
1515
open import Data.Vec.Base as Vec using (Vec; []; _∷_)
16-
open import Data.Vec.Relation.Unary.Any using (Any; here; there)
16+
open import Data.Vec.Relation.Unary.Any as Any using (Any; here; there)
17+
open import Data.Vec.Membership.Propositional renaming (_∈_ to _∈ₚ_)
18+
import Data.Vec.Membership.Setoid as SetoidMembership
1719
open import Function.Base using (_∘_)
1820
open import Level using (Level; _⊔_)
1921
open import Relation.Nullary hiding (Irrelevant)
2022
import Relation.Nullary.Decidable as Dec
2123
open import Relation.Nullary.Product using (_×-dec_)
22-
open import Relation.Unary
24+
open import Relation.Unary hiding (_∈_)
25+
open import Relation.Binary using (Setoid; _Respects_)
2326
open import Relation.Binary.PropositionalEquality as P using (subst)
2427

2528
private
2629
variable
27-
a b c p q r : Level
30+
a b c p q r : Level
2831
A : Set a
2932
B : Set b
3033
C : Set c
3134
P : Pred A p
3235
Q : Pred A q
36+
R : Pred A r
3337
n :
3438
x : A
3539
xs : Vec A n
@@ -59,14 +63,6 @@ reduce f (px ∷ pxs) = f px ∷ reduce f pxs
5963
uncons : All P (x ∷ xs) P x × All P xs
6064
uncons = < head , tail >
6165

62-
lookup : (i : Fin n) All P xs P (Vec.lookup xs i)
63-
lookup zero (px ∷ pxs) = px
64-
lookup (suc i) (px ∷ pxs) = lookup i pxs
65-
66-
tabulate : ( i P (Vec.lookup xs i)) All P xs
67-
tabulate {xs = []} pxs = []
68-
tabulate {xs = _ ∷ _} pxs = pxs zero ∷ tabulate (pxs ∘ suc)
69-
7066
map : P ⊆ Q All P ⊆ All Q {n}
7167
map g [] = []
7268
map g (px ∷ pxs) = g px ∷ map g pxs
@@ -89,6 +85,26 @@ module _ {P : Pred A p} {Q : Pred B q} {R : Pred C r} where
8985
zipWith _⊕_ {xs = x ∷ xs} {y ∷ ys} (px ∷ pxs) (qy ∷ qys) =
9086
px ⊕ qy ∷ zipWith _⊕_ pxs qys
9187

88+
------------------------------------------------------------------------
89+
-- Generalised lookup based on a proof of Any
90+
91+
lookupAny : All P xs (i : Any Q xs) (P ∩ Q) (Any.lookup i)
92+
lookupAny (px ∷ pxs) (here qx) = px , qx
93+
lookupAny (px ∷ pxs) (there i) = lookupAny pxs i
94+
95+
lookupWith : ∀[ P ⇒ Q ⇒ R ] All P xs (i : Any Q xs) R (Any.lookup i)
96+
lookupWith f pxs i = Prod.uncurry f (lookupAny pxs i)
97+
98+
lookup : All P xs ( {x} x ∈ₚ xs P x)
99+
lookup pxs = lookupWith (λ { px P.refl px }) pxs
100+
101+
module _(S : Setoid a ℓ) {P : Pred (Setoid.Carrier S) p} where
102+
open Setoid S renaming (sym to sym₁)
103+
open SetoidMembership S
104+
105+
lookupₛ : P Respects _≈_ All P xs ( {x} x ∈ xs P x)
106+
lookupₛ resp pxs = lookupWith (λ py x=y resp (sym₁ x=y) py) pxs
107+
92108
------------------------------------------------------------------------
93109
-- Properties of predicates preserved by All
94110

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

Lines changed: 75 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

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

11-
open import Data.Nat.Base using (zero; suc; _+_)
11+
open import Data.Nat.Base using (ℕ; zero; suc; _+_)
1212
open import Data.Fin.Base using (Fin; zero; suc)
1313
open import Data.List.Base using ([]; _∷_)
1414
open import Data.List.Relation.Unary.All as List using ([]; _∷_)
@@ -29,82 +29,89 @@ private
2929
a b p q : Level
3030
A : Set a
3131
B : Set b
32+
P : Pred A p
33+
Q : Pred B q
34+
m n :
35+
xs : Vec A n
3236

3337
------------------------------------------------------------------------
34-
-- map
35-
36-
module _ {P : Pred B p} {f : A B} where
37-
38-
map⁺ : {n} {xs : Vec A n} All (P ∘ f) xs All P (map f xs)
39-
map⁺ [] = []
40-
map⁺ (px ∷ pxs) = px ∷ map⁺ pxs
41-
42-
map⁻ : {n} {xs : Vec A n} All P (map f xs) All (P ∘ f) xs
43-
map⁻ {xs = []} [] = []
44-
map⁻ {xs = _ ∷ _} (px ∷ pxs) = px ∷ map⁻ pxs
38+
-- lookup
4539

46-
-- A variant of All.map
47-
48-
module _ {f : A B} {P : Pred A p} {Q : Pred B q} where
40+
lookup⁺ : (i : Fin n) All P xs P (Vec.lookup xs i)
41+
lookup⁺ zero (px ∷ pxs) = px
42+
lookup⁺ (suc i) (px ∷ pxs) = lookup⁺ i pxs
4943

50-
gmap : {n} P ⋐ Q ∘ f All P {n} ⋐ All Q {n} ∘ map f
51-
gmap g = map⁺ ∘ All.map g
44+
lookup⁻ : ( i P (Vec.lookup xs i)) All P xs
45+
lookup⁻ {xs = []} pxs = []
46+
lookup⁻ {xs = _ ∷ _} pxs = pxs zero ∷ lookup⁻ (pxs ∘ suc)
5247

5348
------------------------------------------------------------------------
54-
-- _++_
55-
56-
module _ {n} {P : Pred A p} where
57-
58-
++⁺ : {m} {xs : Vec A m} {ys : Vec A n}
59-
All P xs All P ys All P (xs ++ ys)
60-
++⁺ [] pys = pys
61-
++⁺ (px ∷ pxs) pys = px ∷ ++⁺ pxs pys
49+
-- map
6250

63-
++ˡ⁻ : {m} (xs : Vec A m) {ys : Vec A n}
64-
All P (xs ++ ys) All P xs
65-
++ˡ⁻ [] _ = []
66-
++ˡ⁻ (x ∷ xs) (px ∷ pxs) = px ∷ ++ˡ⁻ xs pxs
51+
map⁺ : {f : A B} All (P ∘ f) xs All P (map f xs)
52+
map⁺ [] = []
53+
map⁺ (px ∷ pxs) = px ∷ map⁺ pxs
6754

68-
++ʳ⁻ : {m} (xs : Vec A m) {ys : Vec A n}
69-
All P (xs ++ ys) All P ys
70-
++ʳ⁻ [] pys = pys
71-
++ʳ⁻ (x ∷ xs) (px ∷ pxs) = ++ʳ⁻ xs pxs
55+
map⁻ : {f : A B} All P (map f xs) All (P ∘ f) xs
56+
map⁻ {xs = []} [] = []
57+
map⁻ {xs = _ ∷ _} (px ∷ pxs) = px ∷ map⁻ pxs
7258

73-
++⁻ : {m} (xs : Vec A m) {ys : Vec A n}
74-
All P (xs ++ ys) All P xs × All P ys
75-
++⁻ [] p = [] , p
76-
++⁻ (x ∷ xs) (px ∷ pxs) = Prod.map₁ (px ∷_) (++⁻ _ pxs)
59+
-- A variant of All.map
7760

78-
++⁺∘++⁻ : {m} (xs : Vec A m) {ys : Vec A n}
79-
(p : All P (xs ++ ys))
80-
uncurry′ ++⁺ (++⁻ xs p) ≡ p
81-
++⁺∘++⁻ [] p = refl
82-
++⁺∘++⁻ (x ∷ xs) (px ∷ pxs) = cong (px ∷_) (++⁺∘++⁻ xs pxs)
61+
gmap : {f : A B} P ⋐ Q ∘ f All P {n} ⋐ All Q {n} ∘ map f
62+
gmap g = map⁺ ∘ All.map g
8363

84-
++⁻∘++⁺ : {m} {xs : Vec A m} {ys : Vec A n}
85-
(p : All P xs × All P ys)
86-
++⁻ xs (uncurry ++⁺ p) ≡ p
87-
++⁻∘++⁺ ([] , pys) = refl
88-
++⁻∘++⁺ (px ∷ pxs , pys) rewrite ++⁻∘++⁺ (pxs , pys) = refl
64+
------------------------------------------------------------------------
65+
-- _++_
8966

90-
++↔ : {m} {xs : Vec A m} {ys : Vec A n}
91-
(All P xs × All P ys) ↔ All P (xs ++ ys)
92-
++↔ {xs = xs} = inverse (uncurry ++⁺) (++⁻ xs) ++⁻∘++⁺ (++⁺∘++⁻ xs)
67+
++⁺ : {xs : Vec A m} {ys : Vec A n}
68+
All P xs All P ys All P (xs ++ ys)
69+
++⁺ [] pys = pys
70+
++⁺ (px ∷ pxs) pys = px ∷ ++⁺ pxs pys
71+
72+
++ˡ⁻ : (xs : Vec A m) {ys : Vec A n}
73+
All P (xs ++ ys) All P xs
74+
++ˡ⁻ [] _ = []
75+
++ˡ⁻ (x ∷ xs) (px ∷ pxs) = px ∷ ++ˡ⁻ xs pxs
76+
77+
++ʳ⁻ : (xs : Vec A m) {ys : Vec A n}
78+
All P (xs ++ ys) All P ys
79+
++ʳ⁻ [] pys = pys
80+
++ʳ⁻ (x ∷ xs) (px ∷ pxs) = ++ʳ⁻ xs pxs
81+
82+
++⁻ : (xs : Vec A m) {ys : Vec A n}
83+
All P (xs ++ ys) All P xs × All P ys
84+
++⁻ [] p = [] , p
85+
++⁻ (x ∷ xs) (px ∷ pxs) = Prod.map₁ (px ∷_) (++⁻ _ pxs)
86+
87+
++⁺∘++⁻ : (xs : Vec A m) {ys : Vec A n}
88+
(p : All P (xs ++ ys))
89+
uncurry′ ++⁺ (++⁻ xs p) ≡ p
90+
++⁺∘++⁻ [] p = refl
91+
++⁺∘++⁻ (x ∷ xs) (px ∷ pxs) = cong (px ∷_) (++⁺∘++⁻ xs pxs)
92+
93+
++⁻∘++⁺ : {xs : Vec A m} {ys : Vec A n}
94+
(p : All P xs × All P ys)
95+
++⁻ xs (uncurry ++⁺ p) ≡ p
96+
++⁻∘++⁺ ([] , pys) = refl
97+
++⁻∘++⁺ (px ∷ pxs , pys) rewrite ++⁻∘++⁺ (pxs , pys) = refl
98+
99+
++↔ : {xs : Vec A m} {ys : Vec A n}
100+
(All P xs × All P ys) ↔ All P (xs ++ ys)
101+
++↔ {xs = xs} = inverse (uncurry ++⁺) (++⁻ xs) ++⁻∘++⁺ (++⁺∘++⁻ xs)
93102

94103
------------------------------------------------------------------------
95104
-- concat
96105

97-
module _ {m} {P : Pred A p} where
98-
99-
concat⁺ : {n} {xss : Vec (Vec A m) n}
100-
All (All P) xss All P (concat xss)
101-
concat⁺ [] = []
102-
concat⁺ (pxs ∷ pxss) = ++⁺ pxs (concat⁺ pxss)
106+
concat⁺ : {xss : Vec (Vec A m) n}
107+
All (All P) xss All P (concat xss)
108+
concat⁺ [] = []
109+
concat⁺ (pxs ∷ pxss) = ++⁺ pxs (concat⁺ pxss)
103110

104-
concat⁻ : {n} (xss : Vec (Vec A m) n)
105-
All P (concat xss) All (All P) xss
106-
concat⁻ [] [] = []
107-
concat⁻ (xs ∷ xss) pxss = ++ˡ⁻ xs pxss ∷ concat⁻ xss (++ʳ⁻ xs pxss)
111+
concat⁻ : (xss : Vec (Vec A m) n)
112+
All P (concat xss) All (All P) xss
113+
concat⁻ [] [] = []
114+
concat⁻ (xs ∷ xss) pxss = ++ˡ⁻ xs pxss ∷ concat⁻ xss (++ʳ⁻ xs pxss)
108115

109116
------------------------------------------------------------------------
110117
-- swap
@@ -139,17 +146,15 @@ module _ {P : A → Set p} where
139146
------------------------------------------------------------------------
140147
-- take and drop
141148

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
149+
drop⁺ : m {xs} All P {m + n} xs All P {n} (drop m xs)
150+
drop⁺ zero pxs = pxs
151+
drop⁺ (suc m) {x ∷ xs} (px ∷ pxs)
152+
rewrite Vecₚ.unfold-drop m x xs = drop⁺ m pxs
148153

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
154+
take⁺ : m {xs} All P {m + n} xs All P {m} (take m xs)
155+
take⁺ zero pxs = []
156+
take⁺ (suc m) {x ∷ xs} (px ∷ pxs)
157+
rewrite Vecₚ.unfold-take m x xs = px ∷ take⁺ m pxs
153158

154159
------------------------------------------------------------------------
155160
-- toList

0 commit comments

Comments
 (0)