Skip to content

Commit e0bd51a

Browse files
Update variable policy in style guide (agda#1379)
1 parent 9e28c6e commit e0bd51a

File tree

6 files changed

+708
-777
lines changed

6 files changed

+708
-777
lines changed

fix-whitespace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ included-files:
99

1010
excluded-files:
1111
- "README/Text/Tabular.agda"
12-
12+
- CHANGELOG.md

notes/style-guide.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,26 @@ line of code, indented by two spaces.
311311
* If there are lots of implicit arguments that are common to a collection
312312
of proofs they should be extracted by using an anonymous module.
313313

314-
* Implicit of type `Level` and `Set` can be generalized using the keyword
315-
`variable`. At the moment the policy is *not* to generalize over any other
316-
types to minimize the amount of information that users have to keep in
317-
their head concurrently.
314+
#### Variables
315+
316+
* `Level` and `Set`s can always be generalized using the keyword `variable`.
317+
318+
* A file may only declare variables of other types if those types are used
319+
in the definition of the main type that the file concerns itself with.
320+
At the moment the policy is *not* to generalize over any other types to
321+
minimize the amount of information that users have to keep in their head
322+
concurrently.
323+
324+
* Example 1: the main type in `Data.List.Properties` is `List A` where `A : Set a`.
325+
Therefore it may declare variables over `Level`, `Set a`, `A`, `List A`. It may
326+
not declare variables, for example, over predicates (e.g. `P : Pred A p`) as
327+
predicates are not used in the definition of `List`, even though they are used
328+
in may list functions such as `filter`.
329+
330+
* Example 2: the main type in `Data.List.Relation.Unary.All` is `All P xs` where
331+
`A : Set a`, `P : Pred A p`, `xs : List A`. It therefore may declare variables
332+
over `Level`, `Set a`, `A`, `List A`, `Pred A p`. It may not declare, for example,
333+
variables of type `Rel` or `Vec`.
318334

319335
## Naming conventions
320336

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

Lines changed: 75 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ private
3131
a b p q r ℓ : Level
3232
A : Set a
3333
B : Set b
34+
P Q R : Pred A p
35+
x : A
36+
xs : List A
3437

3538
------------------------------------------------------------------------
3639
-- Definitions
@@ -67,89 +70,77 @@ Null = All (λ _ → ⊥)
6770
------------------------------------------------------------------------
6871
-- Operations on All
6972

70-
module _ {P : Pred A p} where
73+
uncons : All P (x ∷ xs) P x × All P xs
74+
uncons (px ∷ pxs) = px , pxs
7175

72-
uncons : {x xs} All P (x ∷ xs) P x × All P xs
73-
uncons (px ∷ pxs) = px , pxs
76+
head : All P (x ∷ xs) P x
77+
head = proj₁ ∘ uncons
7478

75-
head : {x xs} All P (x ∷ xs) P x
76-
head = proj ∘ uncons
79+
tail : All P (x ∷ xs) All P xs
80+
tail = proj ∘ uncons
7781

78-
tail : {x xs} All P (x ∷ xs) All P xs
79-
tail = proj₂ ∘ uncons
82+
reduce : (f : {x} P x B) All P xs List B
83+
reduce f [] = []
84+
reduce f (px ∷ pxs) = f px ∷ reduce f pxs
8085

81-
reduce : (f : {x} P x B) {xs} All P xs List B
82-
reduce f [] = []
83-
reduce f (pxpxs) = f px ∷ reduce f pxs
86+
construct : (f : B ∃ P) (xs : List B) ∃ (All P)
87+
construct f [] = [] , []
88+
construct f (xxs) = Prod.zip _∷_ _∷_ (f x) (construct f xs)
8489

85-
construct : (f : B ∃ P) (xs : List B) ∃ (All P)
86-
construct f [] = [] , []
87-
construct f (x ∷ xs) = Prod.zip _∷_ _∷_ (f x) (construct f xs)
90+
fromList : (xs : List (∃ P)) All P (List.map proj₁ xs)
91+
fromList [] = []
92+
fromList ((x , p) ∷ xps) = p ∷ fromList xps
8893

89-
fromList : (xs : List (∃ P)) All P (List.map proj₁ xs)
90-
fromList [] = []
91-
fromList ((x , p) ∷ xps) = p ∷ fromList xps
94+
toList : All P xs List (∃ P)
95+
toList pxs = reduce (λ {x} px x , px) pxs
9296

93-
toList : {xs} All P xs List (∃ P)
94-
toList pxs = reduce (λ {x} px x , px) pxs
97+
map : P ⊆ Q All P ⊆ All Q
98+
map g [] = []
99+
map g (px ∷ pxs) = g px ∷ map g pxs
95100

96-
module _ {P : Pred A p} {Q : Pred A q} where
101+
zipWith : P ∩ Q ⊆ R All P ∩ All Q ⊆ All R
102+
zipWith f ([] , []) = []
103+
zipWith f (px ∷ pxs , qx ∷ qxs) = f (px , qx) ∷ zipWith f (pxs , qxs)
97104

98-
map : P Q All P ⊆ All Q
99-
map g [] = []
100-
map g (pxpxs) = g px ∷ map g pxs
105+
unzipWith : R ⊆ P Q All R ⊆ All P ∩ All Q
106+
unzipWith f [] = [] , []
107+
unzipWith f (rxrxs) = Prod.zip _∷_ _∷_ (f rx) (unzipWith f rxs)
101108

102-
module _ {P : Pred A p} {Q : Pred A q} {R : Pred A r} where
109+
zip : All P ∩ All Q ⊆ All (P ∩ Q)
110+
zip = zipWith id
103111

104-
zipWith : P ∩ Q ⊆ R All P ∩ All Q ⊆ All R
105-
zipWith f ([] , []) = []
106-
zipWith f (px ∷ pxs , qx ∷ qxs) = f (px , qx) ∷ zipWith f (pxs , qxs)
107-
108-
unzipWith : R ⊆ P ∩ Q All R ⊆ All P ∩ All Q
109-
unzipWith f [] = [] , []
110-
unzipWith f (rx ∷ rxs) = Prod.zip _∷_ _∷_ (f rx) (unzipWith f rxs)
111-
112-
module _ {P : Pred A p} {Q : Pred A q} where
113-
114-
zip : All P ∩ All Q ⊆ All (P ∩ Q)
115-
zip = zipWith id
116-
117-
unzip : All (P ∩ Q) ⊆ All P ∩ All Q
118-
unzip = unzipWith id
112+
unzip : All (P ∩ Q) ⊆ All P ∩ All Q
113+
unzip = unzipWith id
119114

120115
module _(S : Setoid a ℓ) {P : Pred (Setoid.Carrier S) p} where
121116
open Setoid S renaming (Carrier to C; refl to refl₁)
122117
open SetoidMembership S
123118

124-
tabulateₛ : {xs} ( {x} x ∈ xs P x) All P xs
119+
tabulateₛ : ( {x} x ∈ xs P x) All P xs
125120
tabulateₛ {[]} hyp = []
126121
tabulateₛ {x ∷ xs} hyp = hyp (here refl₁) ∷ tabulateₛ (hyp ∘ there)
127122

128-
module _ {P : Pred A p} where
129-
130-
tabulate : {xs} ( {x} x ∈ₚ xs P x) All P xs
131-
tabulate = tabulateₛ (P.setoid A)
123+
tabulate : ( {x} x ∈ₚ xs P x) All P xs
124+
tabulate = tabulateₛ (P.setoid _)
132125

133126
self : {xs : List A} All (const A) xs
134127
self = tabulate (λ {x} _ x)
135128

136129
------------------------------------------------------------------------
137130
-- (weak) updateAt
138131

139-
module _ {P : Pred A p} where
140-
141-
infixl 6 _[_]%=_ _[_]≔_
132+
infixl 6 _[_]%=_ _[_]≔_
142133

143-
updateAt : {x xs} x ∈ₚ xs (P x P x) All P xs All P xs
144-
updateAt () f []
145-
updateAt (here refl) f (px ∷ pxs) = f px ∷ pxs
146-
updateAt (there i) f (px ∷ pxs) = px ∷ updateAt i f pxs
134+
updateAt : x ∈ₚ xs (P x P x) All P xs All P xs
135+
updateAt () f []
136+
updateAt (here refl) f (px ∷ pxs) = f px ∷ pxs
137+
updateAt (there i) f (px ∷ pxs) = px ∷ updateAt i f pxs
147138

148-
_[_]%=_ : {x xs} All P xs x ∈ₚ xs (P x P x) All P xs
149-
pxs [ i ]%= f = updateAt i f pxs
139+
_[_]%=_ : All P xs x ∈ₚ xs (P x P x) All P xs
140+
pxs [ i ]%= f = updateAt i f pxs
150141

151-
_[_]≔_ : {x xs} All P xs x ∈ₚ xs P x All P xs
152-
pxs [ i ]≔ px = pxs [ i ]%= const px
142+
_[_]≔_ : All P xs x ∈ₚ xs P x All P xs
143+
pxs [ i ]≔ px = pxs [ i ]%= const px
153144

154145
------------------------------------------------------------------------
155146
-- Traversable-like functions
@@ -168,7 +159,7 @@ module _ (p : Level) {A : Set a} {P : Pred A (a ⊔ p)}
168159
mapA : {Q : Pred A q} (Q ⊆ F ∘′ P) All Q ⊆ (F ∘′ All P)
169160
mapA f = sequenceA ∘′ map f
170161

171-
forA : {Q : Pred A q} {xs} All Q xs (Q ⊆ F ∘′ P) F (All P xs)
162+
forA : {Q : Pred A q} All Q xs (Q ⊆ F ∘′ P) F (All P xs)
172163
forA qxs f = mapA f qxs
173164

174165
module _ (p : Level) {A : Set a} {P : Pred A (a ⊔ p)}
@@ -184,56 +175,56 @@ module _ (p : Level) {A : Set a} {P : Pred A (a ⊔ p)}
184175
mapM : {Q : Pred A q} (Q ⊆ M ∘′ P) All Q ⊆ (M ∘′ All P)
185176
mapM = mapA p App
186177

187-
forM : {Q : Pred A q} {xs} All Q xs (Q ⊆ M ∘′ P) M (All P xs)
178+
forM : {Q : Pred A q} All Q xs (Q ⊆ M ∘′ P) M (All P xs)
188179
forM = forA p App
189180

190181
------------------------------------------------------------------------
191182
-- Generalised lookup based on a proof of Any
192183

193-
module _ {P : Pred A p} {Q : Pred A q} where
194-
195-
lookupAny : {xs} All P xs (i : Any Q xs) (P ∩ Q) (Any.lookup i)
196-
lookupAny (px ∷ pxs) (here qx) = px , qx
197-
lookupAny (px ∷ pxs) (there i) = lookupAny pxs i
184+
lookupAny : All P xs (i : Any Q xs) (P ∩ Q) (Any.lookup i)
185+
lookupAny (px ∷ pxs) (here qx) = px , qx
186+
lookupAny (px ∷ pxs) (there i) = lookupAny pxs i
198187

199-
module _ {P : Pred A p} {Q : Pred A q} {R : Pred A r} where
188+
lookupWith : ∀[ P ⇒ Q ⇒ R ] All P xs (i : Any Q xs) R (Any.lookup i)
189+
lookupWith f pxs i = Prod.uncurry f (lookupAny pxs i)
200190

201-
lookupWith : ∀[ P ⇒ Q ⇒ R ] {xs} All P xs (i : Any Q xs)
202-
R (Any.lookup i)
203-
lookupWith f pxs i = Prod.uncurry f (lookupAny pxs i)
204-
205-
module _ {P : Pred A p} where
206-
207-
lookup : {xs} All P xs ( {x} x ∈ₚ xs P x)
208-
lookup pxs = lookupWith (λ { px refl px }) pxs
191+
lookup : All P xs ( {x} x ∈ₚ xs P x)
192+
lookup pxs = lookupWith (λ { px refl px }) pxs
209193

210194
module _(S : Setoid a ℓ) {P : Pred (Setoid.Carrier S) p} where
211195
open Setoid S renaming (sym to sym₁)
212196
open SetoidMembership S
213197

214-
lookupₛ : {xs} P Respects _≈_ All P xs ( {x} x ∈ xs P x)
198+
lookupₛ : P Respects _≈_ All P xs ( {x} x ∈ xs P x)
215199
lookupₛ resp pxs = lookupWith (λ py x=y resp (sym₁ x=y) py) pxs
216200

217201
------------------------------------------------------------------------
218202
-- Properties of predicates preserved by All
219203

220-
module _ {P : Pred A p} where
204+
all? : Decidable P Decidable (All P)
205+
all? p [] = yes []
206+
all? p (x ∷ xs) = Dec.map′ (uncurry _∷_) uncons (p x ×-dec all? p xs)
207+
208+
universal : Universal P Universal (All P)
209+
universal u [] = []
210+
universal u (x ∷ xs) = u x ∷ universal u xs
221211

222-
all? : Decidable P Decidable (All P)
223-
all? p [] = yes []
224-
all? p (x ∷ xs) = Dec.map′ (uncurry _∷_) uncons (p x ×-dec all? p xs)
212+
irrelevant : Irrelevant P Irrelevant (All P)
213+
irrelevant irr [] [] = P.refl
214+
irrelevant irr (px₁ ∷ pxs₁) (px₂ ∷ pxs₂) =
215+
P.cong₂ _∷_ (irr px₁ px₂) (irrelevant irr pxs₁ pxs₂)
225216

226-
universal : Universal P Universal (All P)
227-
universal u [] = []
228-
universal u (x ∷ xs) = u x ∷ universal u xs
217+
satisfiable : Satisfiable (All P)
218+
satisfiable = [] , []
229219

230-
irrelevant : Irrelevant P Irrelevant (All P)
231-
irrelevant irr [] [] = P.refl
232-
irrelevant irr (px₁ ∷ pxs₁) (px₂ ∷ pxs₂) =
233-
P.cong₂ _∷_ (irr px₁ px₂) (irrelevant irr pxs₁ pxs₂)
234220

235-
satisfiable : Satisfiable (All P)
236-
satisfiable = [] , []
221+
------------------------------------------------------------------------
222+
-- DEPRECATED
223+
------------------------------------------------------------------------
224+
-- Please use the new names as continuing support for the old names is
225+
-- not guaranteed.
226+
227+
-- Version 1.4
237228

238229
all = all?
239230
{-# WARNING_ON_USAGE all

0 commit comments

Comments
 (0)