@@ -22,33 +22,37 @@ open import Data.List.Relation.Unary.AllPairs using (AllPairs; []; _∷_)
2222open import Data.Maybe.Base as Maybe using (Maybe; just; nothing)
2323open import Data.Nat.Base using (ℕ; zero; suc)
2424open import Function.Base using (_∘′_; flip; id; _on_)
25- open import Relation.Nullary using (does)
26- open import Relation.Unary as U using (Pred)
27- open import Relation.Binary.Core using (Rel)
28- import Relation.Binary.Definitions as B using (Reflexive)
25+ open import Relation.Binary.Core using (Rel; REL)
26+ open import Relation.Binary.Definitions as Binary using (Reflexive)
2927open import Relation.Nary using (_⇒_; ∀[_])
28+ open import Relation.Nullary using (does)
29+ open import Relation.Unary as Unary using (Pred; Decidable)
3030
3131
3232private
3333 variable
3434 a b p r s : Level
3535 A : Set a
3636 B : Set b
37+ R : Rel A r
38+ S : Rel A s
39+ x y : A
40+
3741
3842------------------------------------------------------------------------
3943-- Basic type
4044
4145-- If we pick an R such that (R a b) means that a is different from b
4246-- then we have a list of distinct values.
4347
44- module _ {a} (A : Set a) (R : Rel A r) where
48+ module _ (A : Set a) (R : Rel A r) where
4549
4650 data List# : Set (a ⊔ r)
47- fresh : (a : A) (as : List#) → Set r
51+ fresh : REL A List# r
4852
4953 data List# where
5054 [] : List#
51- cons : (a : A) (as : List#) → fresh a as → List#
55+ cons : (x : A) (xs : List#) → fresh x xs → List#
5256
5357 -- Whenever R can be reconstructed by η-expansion (e.g. because it is
5458 -- the erasure ⌊_⌋ of a decidable predicate, cf. Relation.Nary) or we
@@ -62,147 +66,151 @@ module _ {a} (A : Set a) (R : Rel A r) where
6266 fresh a [] = ⊤
6367 fresh a (x ∷# xs) = R a x × fresh a xs
6468
65- -- Convenient notation for freshness making A and R implicit parameters
66- infix 5 _#_
67- _#_ : {R : Rel A r} (a : A) (as : List# A R) → Set r
68- _#_ = fresh _ _
69+ -- Convenient notation for freshness making A (and R) implicit
70+ infix 5 _#[_]_ _#_
71+
72+ _#[_]_ : A → (R : Rel A r) → Pred (List# A R) _
73+ x #[ R ] xs = fresh _ R x xs
74+
75+ _#_ : REL A (List# A R) _
76+ x # xs = x #[ _ ] xs
6977
7078------------------------------------------------------------------------
7179-- Operations for modifying fresh lists
7280
73- module _ {R : Rel A r} {S : Rel B s} (f : A → B) (R⇒S : ∀[ R ⇒ (S on f) ]) where
81+ module _ (f : A → B) (R⇒S : ∀[ R ⇒ (S on f) ]) where
7482
7583 map : List# A R → List# B S
76- map-# : ∀ {a} as → a # as → f a # map as
84+ map-# : ∀ xs → x # xs → f x # map xs
7785
7886 map [] = []
79- map (cons a as ps) = cons (f a ) (map as ) (map-# as ps)
87+ map (cons x xs ps) = cons (f x ) (map xs ) (map-# xs ps)
8088
8189 map-# [] _ = _
82- map-# (a ∷# as ) (p , ps) = R⇒S p , map-# as ps
90+ map-# (x ∷# xs ) (p , ps) = R⇒S p , map-# xs ps
8391
84- module _ {R : Rel B r} (f : A → B) where
92+ module _ (f : A → B) where
8593
8694 map₁ : List# A (R on f) → List# B R
8795 map₁ = map f id
8896
89- module _ {R : Rel A r} { S : Rel A s} (R⇒S : ∀[ R ⇒ S ]) where
97+ module _ {S : Rel A s} (R⇒S : ∀[ R ⇒ S ]) where
9098
9199 map₂ : List# A R → List# A S
92100 map₂ = map id R⇒S
93101
94102------------------------------------------------------------------------
95103-- Views
96104
97- data Empty {A : Set a} {R : Rel A r} : List# A R → Set (a ⊔ r) where
105+ data Empty {A : Set a} {R : Rel A r} : Pred ( List# A R) (a ⊔ r) where
98106 [] : Empty []
99107
100- data NonEmpty {A : Set a} {R : Rel A r} : List# A R → Set (a ⊔ r) where
108+ data NonEmpty {A : Set a} {R : Rel A r} : Pred ( List# A R) (a ⊔ r) where
101109 cons : ∀ x xs pr → NonEmpty (cons x xs pr)
102110
103111------------------------------------------------------------------------
104112-- Operations for reducing fresh lists
105113
106- length : {R : Rel A r} → List# A R → ℕ
114+ length : List# A R → ℕ
107115length [] = 0
108116length (_ ∷# xs) = suc (length xs)
109117
110118------------------------------------------------------------------------
111119-- Operations for constructing fresh lists
112120
113- pattern [_] a = a ∷# []
121+ pattern [_] x = x ∷# []
114122
115- fromMaybe : {R : Rel A r} → Maybe A → List# A R
123+ fromMaybe : Maybe A → List# A R
116124fromMaybe nothing = []
117- fromMaybe (just a ) = [ a ]
125+ fromMaybe (just x ) = [ x ]
118126
119- module _ {R : Rel A r} (R-refl : B.Reflexive R) where
127+ module _ (refl : Reflexive {A = A} R) where
120128
121129 replicate : ℕ → A → List# A R
122- replicate-# : (n : ℕ) (a : A) → a # replicate n a
130+ replicate-# : ∀ n x → x # replicate n x
123131
124- replicate zero a = []
125- replicate (suc n) a = cons a (replicate n a ) (replicate-# n a )
132+ replicate zero x = []
133+ replicate (suc n) x = cons x (replicate n x ) (replicate-# n x )
126134
127- replicate-# zero a = _
128- replicate-# (suc n) a = R- refl , replicate-# n a
135+ replicate-# zero x = _
136+ replicate-# (suc n) x = refl , replicate-# n x
129137
130138------------------------------------------------------------------------
131139-- Operations for deconstructing fresh lists
132140
133- uncons : {R : Rel A r} → List# A R → Maybe (A × List# A R)
141+ uncons : List# A R → Maybe (A × List# A R)
134142uncons [] = nothing
135- uncons (a ∷# as ) = just (a , as )
143+ uncons (x ∷# xs ) = just (x , xs )
136144
137- head : {R : Rel A r} → List# A R → Maybe A
145+ head : List# A R → Maybe A
138146head = Maybe.map proj₁ ∘′ uncons
139147
140- tail : {R : Rel A r} → List# A R → Maybe (List# A R)
148+ tail : List# A R → Maybe (List# A R)
141149tail = Maybe.map proj₂ ∘′ uncons
142150
143- take : {R : Rel A r} → ℕ → List# A R → List# A R
144- take-# : {R : Rel A r} → ∀ n a (as : List# A R) → a # as → a # take n as
151+ take : ℕ → List# A R → List# A R
152+ take-# : ∀ n y xs → y # xs → y # take {R = R} n xs
145153
146154take zero xs = []
147155take (suc n) [] = []
148- take (suc n) (cons a as ps) = cons a (take n as ) (take-# n a as ps)
156+ take (suc n) (cons x xs ps) = cons x (take n xs ) (take-# n x xs ps)
149157
150- take-# zero a xs _ = _
151- take-# (suc n) a [] ps = _
152- take-# (suc n) a (x ∷# xs) (p , ps) = p , take-# n a xs ps
158+ take-# zero y xs _ = _
159+ take-# (suc n) y [] ps = _
160+ take-# (suc n) y (x ∷# xs) (p , ps) = p , take-# n y xs ps
153161
154- drop : {R : Rel A r} → ℕ → List# A R → List# A R
155- drop zero as = as
162+ drop : ℕ → List# A R → List# A R
163+ drop zero xs = xs
156164drop (suc n) [] = []
157- drop (suc n) (a ∷# as ) = drop n as
165+ drop (suc n) (x ∷# xs ) = drop n xs
158166
159- module _ {P : Pred A p} (P? : U. Decidable P) where
167+ module _ {P : Pred A p} (P? : Decidable P) where
160168
161- takeWhile : {R : Rel A r} → List# A R → List# A R
162- takeWhile-# : ∀ {R : Rel A r} a (as : List# A R) → a # as → a # takeWhile as
169+ takeWhile : List# A R → List# A R
170+ takeWhile-# : ∀ y xs → y # xs → y # takeWhile {R = R} xs
163171
164172 takeWhile [] = []
165- takeWhile (cons a as ps) =
166- if does (P? a ) then cons a (takeWhile as ) (takeWhile-# a as ps) else []
173+ takeWhile (cons x xs ps) =
174+ if does (P? x ) then cons x (takeWhile xs ) (takeWhile-# x xs ps) else []
167175
168176 -- this 'with' is needed to cause reduction in the type of 'takeWhile (a ∷# as)'
169- takeWhile-# a [] _ = _
170- takeWhile-# a (x ∷# xs) (p , ps) with does (P? x)
171- ... | true = p , takeWhile-# a xs ps
177+ takeWhile-# y [] _ = _
178+ takeWhile-# y (x ∷# xs) (p , ps) with does (P? x)
179+ ... | true = p , takeWhile-# y xs ps
172180 ... | false = _
173181
174- dropWhile : {R : Rel A r} → List# A R → List# A R
182+ dropWhile : List# A R → List# A R
175183 dropWhile [] = []
176- dropWhile aas@(a ∷# as ) = if does (P? a ) then dropWhile as else aas
184+ dropWhile xxs@(x ∷# xs ) = if does (P? x ) then dropWhile xs else xxs
177185
178- filter : {R : Rel A r} → List# A R → List# A R
179- filter-# : ∀ {R : Rel A r} a (as : List# A R) → a # as → a # filter as
186+ filter : List# A R → List# A R
187+ filter-# : ∀ y xs → y # xs → y # filter {R = R} xs
180188
181189 filter [] = []
182- filter (cons a as ps) =
183- let l = filter as in
184- if does (P? a ) then cons a l (filter-# a as ps) else l
190+ filter (cons x xs ps) =
191+ let l = filter xs in
192+ if does (P? x ) then cons x l (filter-# x xs ps) else l
185193
186- -- this 'with' is needed to cause reduction in the type of 'filter-# a (x ∷# xs)'
187- filter-# a [] _ = _
188- filter-# a (x ∷# xs) (p , ps) with does (P? x)
189- ... | true = p , filter-# a xs ps
190- ... | false = filter-# a xs ps
194+ -- this 'with' is needed to cause reduction in the type of 'filter-# y (x ∷# xs)'
195+ filter-# y [] _ = _
196+ filter-# y (x ∷# xs) (p , ps) with does (P? x)
197+ ... | true = p , filter-# y xs ps
198+ ... | false = filter-# y xs ps
191199
192200------------------------------------------------------------------------
193201-- Relationship to List and AllPairs
194202
195- toList : {R : Rel A r} → List# A R → ∃ (AllPairs R)
196- toAll : ∀ {R : Rel A r} {a} as → fresh A R a as → All (R a ) (proj₁ (toList as ))
203+ toList : List# A R → ∃ (AllPairs R)
204+ toAll : ∀ xs → x #[ R ] xs → All (R x ) (proj₁ (toList xs ))
197205
198206toList [] = -, []
199207toList (cons x xs ps) = -, toAll xs ps ∷ proj₂ (toList xs)
200208
201209toAll [] ps = []
202- toAll (a ∷# as ) (p , ps) = p ∷ toAll as ps
210+ toAll (x ∷# xs ) (p , ps) = p ∷ toAll xs ps
203211
204- fromList : ∀ {R : Rel A r} { xs} → AllPairs R xs → List# A R
205- fromList-# : ∀ {R : Rel A r} {x xs} (ps : AllPairs R xs) →
212+ fromList : ∀ {xs} → AllPairs R xs → List# A R
213+ fromList-# : ∀ {xs} (ps : AllPairs R xs) →
206214 All (R x) xs → x # fromList ps
207215
208216fromList [] = []
0 commit comments