Skip to content

Commit 7113a58

Browse files
authored
Add deduplicate, derun functions and Grouped property for lists (#1081)
1 parent 6885294 commit 7113a58

File tree

10 files changed

+370
-15
lines changed

10 files changed

+370
-15
lines changed

CHANGELOG.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ Other major additions
294294
Codata.Cowriter.Bisimilarity
295295
296296
Data.Erased
297+
Data.List.Relation.Unary.Grouped
298+
Data.List.Relation.Unary.Grouped.Properties
297299
Data.Product.Relation.Unary.All
298300
Data.Refinement
299301
Data.Refinement.Relation.Unary.All
@@ -603,6 +605,12 @@ Other minor additions
603605
map-just : ma ≡ just a → map f ma ≡ just (f a)
604606
```
605607

608+
* Added new functions to `Data.List`:
609+
``agda
610+
derun : B.Decidable R → List A → List A
611+
deduplicate : Decidable _R_ → List A → List A
612+
```
613+
606614
* Added new proofs to `Data.List.Relation.Binary.Equality.Setoid`:
607615
```agda
608616
Any-resp-≋ : P Respects _≈_ → (Any P) Respects _≋_
@@ -643,13 +651,49 @@ Other minor additions
643651
rectangleᶜ : Char → Char → Vec String n → Vec String n
644652
```
645653

654+
* Added new proofs to `Data.List.Membership.Propositional.Properties`:
655+
```agda
656+
∈-derun⁺ : z ∈ xs → z ∈ derun R? xs
657+
∈-deduplicate⁺ : z ∈ xs → z ∈ deduplicate _≟_ xs
658+
∈-derun⁻ : z ∈ derun R? xs → z ∈ xs
659+
∈-deduplicate⁻ : z ∈ deduplicate R? xs → z ∈ xs
660+
```
661+
662+
* Added new proofs to `Data.List.Membership.Setoid.Properties`:
663+
```agda
664+
∈-derun⁺ : _≈_ Respectsʳ R → z ∈ xs → z ∈ derun R? xs
665+
∈-deduplicate⁺ : _≈_ Respectsʳ (flip R) → z ∈ xs → z ∈ deduplicate R? xs
666+
∈-derun⁻ : ∀ xs {z} → z ∈ derun R? xs → z ∈ xs
667+
∈-deduplicate⁻ : z ∈ deduplicate R? xs → z ∈ xs
668+
```
669+
646670
* Added new proofs to `Data.List.Relation.Binary.Pointwise`:
647671
```agda
648672
Any-resp-Pointwise : P Respects _∼_ → (Any P) Respects (Pointwise _∼_)
649673
All-resp-Pointwise : P Respects _∼_ → (All P) Respects (Pointwise _∼_)
650674
AllPairs-resp-Pointwise : R Respects₂ _∼_ → (AllPairs R) Respects (Pointwise _∼_)
651675
```
652676

677+
* Added new proofs to `Data.List.Relation.Unary.All.Properties`:
678+
```agda
679+
derun⁺ : All P xs → All P (derun Q? xs)
680+
deduplicate⁺ : All P xs → All P (deduplicate Q? xs)
681+
filter⁻ : All Q (filter P? xs) → All Q (filter (¬? ∘ P?) xs) → All Q xs
682+
derun⁻ : All P (derun Q? xs) → All P xs
683+
deduplicate⁻ : All P (deduplicate Q? xs) → All P xs
684+
```
685+
686+
* Added new proofs to `Data.List.Relation.Unary.Any.Properties`:
687+
```agda
688+
lookup-result : (p : Any P xs) → P (lookup p)
689+
filter⁺ : (p : Any P xs) → Any P (filter Q? xs) ⊎ ¬ Q (Any.lookup p)
690+
derun⁺ : P Respects Q → Any P xs → Any P (derun Q? xs)
691+
deduplicate⁺ : P Respects (flip Q) → Any P xs → Any P (deduplicate Q? xs)
692+
filter⁻ : Any P (filter Q? xs) → Any P xs
693+
derun⁻ : Any P (derun Q? xs) → Any P xs
694+
deduplicate⁻ : Any P (deduplicate Q? xs) → Any P xs
695+
```
696+
653697
* Added new functions to `Data.List.Relation.Binary.Permutation.Setoid`:
654698
```agda
655699
↭-prep : xs ↭ ys → x ∷ xs ↭ x ∷ ys
@@ -894,4 +938,4 @@ Version 2.6.1 changes
894938
* Added new definitions in `Relation.Binary.Core`:
895939
```agda
896940
DecidableEquality A = Decidable {A = A} _≡_
897-
```
941+
```

src/Data/List/Base.agda

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ open import Data.These.Base as These using (These; this; that; these)
2222
open import Function.Base using (id; _∘_ ; _∘′_; const; flip)
2323
open import Level using (Level)
2424
open import Relation.Nullary using (does)
25+
open import Relation.Nullary.Negation using (¬?)
2526
open import Relation.Unary using (Pred; Decidable)
2627
open import Relation.Unary.Properties using (∁?)
28+
open import Relation.Binary as B using (Rel)
2729

2830
private
2931
variable
@@ -292,6 +294,17 @@ span P? (x ∷ xs) with does (P? x)
292294
break : {P : Pred A p} Decidable P List A (List A × List A)
293295
break P? = span (∁? P?)
294296

297+
derun : {R : Rel A p} B.Decidable R List A List A
298+
derun R? [] = []
299+
derun R? (x ∷ []) = x ∷ []
300+
derun R? (x ∷ y ∷ xs) with does (R? x y) | derun R? (y ∷ xs)
301+
... | true | ys = ys
302+
... | false | ys = x ∷ ys
303+
304+
deduplicate : {R : Rel A p} B.Decidable R List A List A
305+
deduplicate R? [] = []
306+
deduplicate R? (x ∷ xs) = x ∷ filter (¬? ∘ R? x) (deduplicate R? xs)
307+
295308
------------------------------------------------------------------------
296309
-- Actions on single elements
297310

src/Data/List/Membership/Propositional/Properties.agda

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ open import Function.Inverse as Inv using (_↔_; module Inverse)
3434
import Function.Related as Related
3535
open import Function.Related.TypeIsomorphisms
3636
open import Level using (Level)
37-
open import Relation.Binary hiding (Decidable)
37+
open import Relation.Binary as B hiding (Decidable)
3838
open import Relation.Binary.PropositionalEquality as P
3939
using (_≡_; _≢_; refl; sym; trans; cong; subst; →-to-⟶; _≗_)
4040
import Relation.Binary.Properties.DecTotalOrder as DTOProperties
4141
open import Relation.Unary using (_⟨×⟩_; Decidable)
4242
open import Relation.Nullary.Reflects using (invert)
43-
open import Relation.Nullary using (¬_; Dec; does; yes; no)
43+
open import Relation.Nullary using (¬_; Dec; does; yes; no; _because_)
4444
open import Relation.Nullary.Negation
4545

4646
private
@@ -179,6 +179,25 @@ module _ {p} {P : A → Set p} (P? : Decidable P) where
179179
∈-filter⁻ : {v xs} v ∈ filter P? xs v ∈ xs × P v
180180
∈-filter⁻ = Membershipₛ.∈-filter⁻ (P.setoid A) P? (P.subst P)
181181

182+
------------------------------------------------------------------------
183+
-- derun and deduplicate
184+
185+
module _ {r} {R : Rel A r} (R? : B.Decidable R) where
186+
187+
∈-derun⁻ : xs {z} z ∈ derun R? xs z ∈ xs
188+
∈-derun⁻ xs z∈derun[R,xs] = Membershipₛ.∈-derun⁻ (P.setoid A) R? xs z∈derun[R,xs]
189+
190+
∈-deduplicate⁻ : xs {z} z ∈ deduplicate R? xs z ∈ xs
191+
∈-deduplicate⁻ xs z∈dedup[R,xs] = Membershipₛ.∈-deduplicate⁻ (P.setoid A) R? xs z∈dedup[R,xs]
192+
193+
module _ (_≈?_ : B.Decidable {A = A} _≡_) where
194+
195+
∈-derun⁺ : {xs z} z ∈ xs z ∈ derun _≈?_ xs
196+
∈-derun⁺ z∈xs = Membershipₛ.∈-derun⁺ (P.setoid A) _≈?_ (flip trans) z∈xs
197+
198+
∈-deduplicate⁺ : {xs z} z ∈ xs z ∈ deduplicate _≈?_ xs
199+
∈-deduplicate⁺ z∈xs = Membershipₛ.∈-deduplicate⁺ (P.setoid A) _≈?_ (λ c≡b a≡b trans a≡b (sym c≡b)) z∈xs
200+
182201
------------------------------------------------------------------------
183202
-- _>>=_
184203

src/Data/List/Membership/Setoid/Properties.agda

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ import Data.List.Relation.Binary.Equality.Setoid as Equality
2020
import Data.List.Relation.Unary.Unique.Setoid as Unique
2121
open import Data.Nat.Base using (suc; z≤n; s≤s; _≤_; _<_)
2222
open import Data.Nat.Properties using (≤-trans; n≤1+n)
23-
open import Data.Product as Prod using (∃; _×_; _,_ ; ∃₂)
23+
open import Data.Product as Prod using (∃; _×_; _,_ ; ∃₂; proj₁)
2424
open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
2525
open import Function using (_$_; flip; _∘_; id)
2626
open import Relation.Binary as B hiding (Decidable)
2727
open import Relation.Binary.PropositionalEquality as P using (_≡_)
2828
open import Relation.Unary as U using (Decidable; Pred)
29+
open import Relation.Nullary using (¬_; does; _because_; yes; no)
2930
open import Relation.Nullary.Reflects using (invert)
30-
open import Relation.Nullary using (¬_; does; _because_)
31-
open import Relation.Nullary.Negation using (contradiction)
31+
open import Relation.Nullary.Negation using (¬?; contradiction)
3232
open Setoid using (Carrier)
3333

3434
------------------------------------------------------------------------
@@ -243,6 +243,26 @@ module _ {c ℓ p} (S : Setoid c ℓ) {P : Pred (Carrier S) p}
243243
... | here v≈x = here v≈x , resp (sym v≈x) (invert [Px])
244244
... | there v∈fxs = Prod.map there id (∈-filter⁻ v∈fxs)
245245

246+
------------------------------------------------------------------------
247+
-- derun and deduplicate
248+
249+
module _ {c ℓ r} (S : Setoid c ℓ) {R : Rel (Carrier S) r} (R? : B.Decidable R) where
250+
251+
open Setoid S using (_≈_)
252+
open Membership S using (_∈_)
253+
254+
∈-derun⁺ : _≈_ Respectsʳ R {xs z} z ∈ xs z ∈ derun R? xs
255+
∈-derun⁺ ≈-resp-R z∈xs = Any.derun⁺ R? ≈-resp-R z∈xs
256+
257+
∈-deduplicate⁺ : _≈_ Respectsʳ (flip R) {xs z} z ∈ xs z ∈ deduplicate R? xs
258+
∈-deduplicate⁺ ≈-resp-R z∈xs = Any.deduplicate⁺ R? ≈-resp-R z∈xs
259+
260+
∈-derun⁻ : xs {z} z ∈ derun R? xs z ∈ xs
261+
∈-derun⁻ xs z∈derun[R,xs] = Any.derun⁻ R? z∈derun[R,xs]
262+
263+
∈-deduplicate⁻ : xs {z} z ∈ deduplicate R? xs z ∈ xs
264+
∈-deduplicate⁻ xs z∈dedup[R,xs] = Any.deduplicate⁻ R? z∈dedup[R,xs]
265+
246266
------------------------------------------------------------------------
247267
-- length
248268

src/Data/List/Properties.agda

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ open import Algebra.Bundles
1515
open import Algebra.Definitions as AlgebraicDefinitions using (Involutive)
1616
import Algebra.Structures as AlgebraicStructures
1717
open import Data.Bool.Base using (Bool; false; true; not; if_then_else_)
18-
open import Data.Fin.Base using (Fin; zero; suc; cast; toℕ)
18+
open import Data.Fin.Base using (Fin; zero; suc; cast; toℕ; inject₁)
1919
open import Data.List.Base as List
2020
open import Data.List.Relation.Unary.All using (All; []; _∷_)
2121
open import Data.List.Relation.Unary.Any using (Any; here; there)
@@ -30,9 +30,10 @@ open import Level using (Level)
3030
open import Relation.Binary as B using (DecidableEquality)
3131
import Relation.Binary.Reasoning.Setoid as EqR
3232
open import Relation.Binary.PropositionalEquality as P hiding ([_])
33+
open import Relation.Binary as B using (Rel)
3334
open import Relation.Nullary.Reflects using (invert)
3435
open import Relation.Nullary using (¬_; Dec; does; _because_; yes; no)
35-
open import Relation.Nullary.Negation using (contradiction)
36+
open import Relation.Nullary.Negation using (contradiction; ¬?)
3637
open import Relation.Nullary.Decidable as Decidable using (isYes; map′; ⌊_⌋)
3738
open import Relation.Nullary.Product using (_×-dec_)
3839
open import Relation.Unary using (Pred; Decidable; ∁)
@@ -752,6 +753,38 @@ module _ {P : Pred A p} (P? : Decidable P) where
752753
... | true = cong (x ∷_) (filter-++ xs ys)
753754
... | false = filter-++ xs ys
754755

756+
------------------------------------------------------------------------
757+
-- derun and deduplicate
758+
759+
module _ {R : Rel A p} (R? : B.Decidable R) where
760+
761+
length-derun : xs length (derun R? xs) ≤ length xs
762+
length-derun [] = ≤-refl
763+
length-derun (x ∷ []) = ≤-refl
764+
length-derun (x ∷ y ∷ xs) with does (R? x y) | length-derun (y ∷ xs)
765+
... | true | r = ≤-step r
766+
... | false | r = s≤s r
767+
768+
length-deduplicate : xs length (deduplicate R? xs) ≤ length xs
769+
length-deduplicate [] = z≤n
770+
length-deduplicate (x ∷ xs) = ≤-begin
771+
1 + length (filter (¬? ∘ R? x) r) ≤⟨ s≤s (length-filter (¬? ∘ R? x) r) ⟩
772+
1 + length r ≤⟨ s≤s (length-deduplicate xs) ⟩
773+
1 + length xs ≤-∎
774+
where
775+
open ≤-Reasoning renaming (begin_ to ≤-begin_; _∎ to _≤-∎)
776+
r = deduplicate R? xs
777+
778+
derun-reject : {x y} xs R x y derun R? (x ∷ y ∷ xs) ≡ derun R? (y ∷ xs)
779+
derun-reject {x} {y} xs Rxy with R? x y
780+
... | yes _ = refl
781+
... | no ¬Rxy = contradiction Rxy ¬Rxy
782+
783+
derun-accept : {x y} xs ¬ R x y derun R? (x ∷ y ∷ xs) ≡ x ∷ derun R? (y ∷ xs)
784+
derun-accept {x} {y} xs ¬Rxy with R? x y
785+
... | yes Rxy = contradiction Rxy ¬Rxy
786+
... | no _ = refl
787+
755788
------------------------------------------------------------------------
756789
-- partition
757790

src/Data/List/Relation/Binary/Subset/Propositional/Properties.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ open import Category.Monad
1515
open import Data.Bool.Base using (Bool; true; false; T)
1616
open import Data.List.Base
1717
open import Data.List.Relation.Unary.Any using (Any; here; there)
18-
open import Data.List.Relation.Unary.Any.Properties
18+
open import Data.List.Relation.Unary.Any.Properties hiding (filter⁺)
1919
open import Data.List.Categorical
2020
open import Data.List.Membership.Propositional
2121
open import Data.List.Membership.Propositional.Properties

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

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ open import Data.Fin.Base using (Fin) renaming (zero to fzero; suc to fsuc)
1616
open import Data.List.Base as List using
1717
( List; []; _∷_; [_]; _∷ʳ_; fromMaybe; null; _++_; concat; map; mapMaybe
1818
; inits; tails; drop; take; applyUpTo; applyDownFrom; replicate; tabulate
19-
; filter; zipWith; all
19+
; filter; zipWith; all; derun; deduplicate
2020
)
2121
open import Data.List.Membership.Propositional
2222
open import Data.List.Membership.Propositional.Properties
@@ -40,17 +40,18 @@ open import Function.Equivalence using (_⇔_; equivalence; Equivalence)
4040
open import Function.Inverse using (_↔_; inverse)
4141
open import Function.Surjection using (_↠_; surjection)
4242
open import Level using (Level)
43-
open import Relation.Binary using (REL; Setoid; _Respects_)
43+
open import Relation.Binary as B using (REL; Setoid; _Respects_)
4444
open import Relation.Binary.PropositionalEquality
4545
using (_≡_; refl; cong; cong₂; _≗_)
4646
open import Relation.Nullary.Reflects using (invert)
4747
open import Relation.Nullary
48+
open import Relation.Nullary.Negation using (¬?; contradiction; decidable-stable)
4849
open import Relation.Unary
4950
using (Decidable; Pred; Universal) renaming (_⊆_ to _⋐_)
5051

5152
private
5253
variable
53-
a b c p q ℓ : Level
54+
a b c p q r : Level
5455
A : Set a
5556
B : Set b
5657
C : Set c
@@ -505,6 +506,48 @@ module _ {P : A → Set p} {Q : A → Set q} (P? : Decidable P) where
505506
... | false = filter⁺ Qxs
506507
... | true = Qx ∷ filter⁺ Qxs
507508

509+
filter⁻ : {xs} All Q (filter P? xs) All Q (filter (¬? ∘ P?) xs) All Q xs
510+
filter⁻ {[]} [] [] = []
511+
filter⁻ {x ∷ xs} all⁺ all⁻ with P? x | ¬? (P? x)
512+
filter⁻ {x ∷ xs} all⁺ all⁻ | yes Px | yes ¬Px = contradiction Px ¬Px
513+
filter⁻ {x ∷ xs} (qx ∷ all⁺) all⁻ | yes Px | no ¬¬Px = qx ∷ filter⁻ all⁺ all⁻
514+
filter⁻ {x ∷ xs} all⁺ (qx ∷ all⁻) | no _ | yes ¬Px = qx ∷ filter⁻ all⁺ all⁻
515+
filter⁻ {x ∷ xs} all⁺ all⁻ | no ¬Px | no ¬¬Px = contradiction ¬Px ¬¬Px
516+
517+
------------------------------------------------------------------------
518+
-- derun and deduplicate
519+
520+
module _ {P : A Set p} {R : A A Set q} (R? : B.Decidable R) where
521+
522+
derun⁺ : {xs} All P xs All P (derun R? xs)
523+
derun⁺ {[]} [] = []
524+
derun⁺ {x ∷ []} (px ∷ []) = px ∷ []
525+
derun⁺ {x ∷ y ∷ xs} (px ∷ all[P,y∷xs]) with does (R? x y)
526+
... | false = px ∷ derun⁺ all[P,y∷xs]
527+
... | true = derun⁺ all[P,y∷xs]
528+
529+
deduplicate⁺ : {xs} All P xs All P (deduplicate R? xs)
530+
deduplicate⁺ [] = []
531+
deduplicate⁺ {x ∷ _} (px ∷ all[P,xs]) = px ∷ filter⁺ (¬? ∘ R? x) (deduplicate⁺ all[P,xs])
532+
533+
derun⁻ : P B.Respects (flip R) xs All P (derun R? xs) All P xs
534+
derun⁻ P-resp-R [] [] = []
535+
derun⁻ P-resp-R (x ∷ xs) all[P,x∷xs] = aux x xs all[P,x∷xs] where
536+
aux : x xs All P (derun R? (x ∷ xs)) All P (x ∷ xs)
537+
aux x [] (px ∷ []) = px ∷ []
538+
aux x (y ∷ xs) all[P,x∷y∷xs] with R? x y
539+
aux x (y ∷ xs) all[P,y∷xs] | yes Rxy with aux y xs all[P,y∷xs]
540+
aux x (y ∷ xs) all[P,y∷xs] | yes Rxy | r@(py ∷ _) = P-resp-R Rxy py ∷ r
541+
aux x (y ∷ xs) (px ∷ all[P,y∷xs]) | no _ = px ∷ aux y xs all[P,y∷xs]
542+
543+
module _ (P-resp-R : P B.Respects R) where
544+
545+
deduplicate⁻ : xs All P (deduplicate R? xs) All P xs
546+
deduplicate⁻ [] [] = []
547+
deduplicate⁻ (x ∷ xs) (px ∷ app[P,dedup[xs]]) = px ∷ deduplicate⁻ xs (filter⁻ (¬? ∘ R? x) app[P,dedup[xs]] (All.tabulate aux)) where
548+
aux : {z} z ∈ filter (¬? ∘ ¬? ∘ R? x) (deduplicate R? xs) P z
549+
aux {z} z∈filter = P-resp-R (decidable-stable (R? x z) (Prod.proj₂ (∈-filter⁻ (¬? ∘ ¬? ∘ R? x) {z} {deduplicate R? xs} z∈filter))) px
550+
508551
------------------------------------------------------------------------
509552
-- zipWith
510553

0 commit comments

Comments
 (0)