Skip to content

Commit 3a64d8e

Browse files
committed
Lists: lemmas about deduplicate
1 parent 7689668 commit 3a64d8e

File tree

5 files changed

+195
-20
lines changed

5 files changed

+195
-20
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ New modules
9898
Data.List.Relation.Unary.All.Properties.Core
9999
```
100100

101+
* `Data.List.Relation.Binary.Disjoint.Propositional.Properties`:
102+
Propositional counterpart to `Data.List.Relation.Binary.Disjoint.Setoid.Properties`
103+
104+
* `Data.List.Relation.Binary.Permutation.Propositional.Properties.WithK`
105+
101106
Additions to existing modules
102107
-----------------------------
103108

@@ -142,6 +147,7 @@ Additions to existing modules
142147
∈-map∘filter⁺ : (∃[ x ] x ∈ xs × y ≡ f x × P x) → y ∈ map f (filter P? xs)
143148
∈-concatMap⁺ : Any ((y ∈_) ∘ f) xs → y ∈ concatMap f xs
144149
∈-concatMap⁻ : y ∈ concatMap f xs → Any ((y ∈_) ∘ f) xs
150+
∈-++ : v ∈ xs ++ ys ⇔ (v ∈ xs ⊎ v ∈ ys)
145151
```
146152

147153
* In `Data.List.Membership.Propositional.Properties.WithK`:
@@ -194,6 +200,24 @@ Additions to existing modules
194200
[]⊆-universal : Universal ([] ⊆_)
195201
```
196202

203+
* In `Data.List.Relation.Binary.Disjoint.Setoid.Properties`:
204+
```agda
205+
deduplicate⁺ : Disjoint S xs ys → Disjoint S (deduplicate _≟_ xs) (deduplicate _≟_ ys)
206+
∈-dedup : x ∈ xs ⇔ x ∈ deduplicate _≟_ xs
207+
```
208+
209+
* In `Data.List.Relation.Binary.Disjoint.Propositional.Properties`:
210+
```agda
211+
deduplicate⁺ : Disjoint xs ys → Disjoint (deduplicate _≟_ xs) (deduplicate _≟_ ys)
212+
∈-dedup : x ∈ xs ⇔ x ∈ deduplicate _≟_ xs
213+
```
214+
215+
* In `Data.List.Relation.Binary.Permutation.Propositional.Properties.WithK`:
216+
```agda
217+
dedup-++-↭ : Disjoint xs ys →
218+
deduplicate _≟_ (xs ++ ys) ↭ deduplicate _≟_ xs ++ deduplicate _≟_ ys
219+
```
220+
197221
* In `Data.Maybe.Properties`:
198222
```agda
199223
maybe′-∘ : ∀ f g → f ∘ (maybe′ g b) ≗ maybe′ (f ∘ g) (f b)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ open import Effect.Monad using (RawMonad)
3333
open import Function.Base using (_∘_; _∘′_; _$_; id; flip; _⟨_⟩_)
3434
open import Function.Definitions using (Injective)
3535
import Function.Related.Propositional as Related
36-
open import Function.Bundles using (_↔_; _↣_; Injection)
36+
open import Function.Bundles using (_↔_; _↣_; Injection; _⇔_; mk⇔)
3737
open import Function.Related.TypeIsomorphisms using (×-comm; ∃∃↔∃∃)
3838
open import Function.Construct.Identity using (↔-id)
3939
open import Level using (Level)
@@ -123,6 +123,9 @@ module _ {v : A} where
123123
∈-++⁻ : xs {ys} v ∈ xs ++ ys (v ∈ xs) ⊎ (v ∈ ys)
124124
∈-++⁻ = Membership.∈-++⁻ (≡.setoid A)
125125

126+
∈-++ : {xs ys} v ∈ xs ++ ys ⇔ (v ∈ xs ⊎ v ∈ ys)
127+
∈-++ = mk⇔ (∈-++⁻ _) Sum.[ ∈-++⁺ˡ , ∈-++⁺ʳ _ ]
128+
126129
∈-insert : xs {ys} v ∈ xs ++ [ v ] ++ ys
127130
∈-insert xs = Membership.∈-insert (≡.setoid A) xs refl
128131

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Properties of disjoint lists (propositional equality)
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --cubical-compatible --safe #-}
8+
9+
module Data.List.Relation.Binary.Disjoint.Propositional.Properties where
10+
11+
open import Level using (Level)
12+
open import Function.Base using (_∘_)
13+
open import Function.Bundles using (_⇔_)
14+
open import Data.List.Base
15+
open import Data.List.Relation.Binary.Disjoint.Propositional
16+
import Data.List.Relation.Unary.Any as Any
17+
open import Data.List.Relation.Unary.All as All
18+
open import Data.List.Relation.Unary.All.Properties using (¬Any⇒All¬)
19+
open import Data.List.Relation.Unary.Any.Properties using (++⁻)
20+
open import Data.List.Membership.Propositional using (_∈_)
21+
open import Data.Product.Base as Product using (_,_)
22+
open import Data.Sum.Base using (inj₁; inj₂)
23+
open import Relation.Binary.Bundles using (Setoid)
24+
open import Relation.Binary.Definitions using (Symmetric; DecidableEquality)
25+
open import Relation.Binary.PropositionalEquality.Core using (_≡_)
26+
open import Relation.Binary.PropositionalEquality.Properties as ≡
27+
open import Relation.Nullary.Negation using (¬_)
28+
29+
import Data.List.Relation.Binary.Disjoint.Setoid.Properties as S
30+
31+
module _ {a} {A : Set a} where
32+
33+
private
34+
Sᴬ = setoid A
35+
variable
36+
x : A
37+
xs ys vs : List A
38+
xss : List (List A)
39+
40+
------------------------------------------------------------------------
41+
-- Relational properties
42+
------------------------------------------------------------------------
43+
44+
sym : Symmetric Disjoint
45+
sym = S.sym Sᴬ
46+
47+
------------------------------------------------------------------------
48+
-- Relationship with other predicates
49+
------------------------------------------------------------------------
50+
51+
Disjoint⇒AllAll : Disjoint xs ys All (λ x All (λ y ¬ x ≡ y) ys) xs
52+
Disjoint⇒AllAll = S.Disjoint⇒AllAll Sᴬ
53+
54+
------------------------------------------------------------------------
55+
-- Introduction (⁺) and elimination (⁻) rules for list operations
56+
------------------------------------------------------------------------
57+
-- concat
58+
59+
concat⁺ʳ : All (Disjoint vs) xss Disjoint vs (concat xss)
60+
concat⁺ʳ = S.concat⁺ʳ (setoid _)
61+
62+
-- deduplicate
63+
module _ (_≟_ : DecidableEquality A) where
64+
private dedup≡ = deduplicate _≟_
65+
66+
deduplicate⁺ : Disjoint xs ys Disjoint (dedup≡ xs) (dedup≡ ys)
67+
deduplicate⁺ = S.deduplicate⁺ Sᴬ _≟_
68+
69+
∈-dedup : x ∈ xs ⇔ x ∈ dedup≡ xs
70+
∈-dedup = S.∈-dedup Sᴬ _≟_

src/Data/List/Relation/Binary/Disjoint/Setoid/Properties.agda

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

99
module Data.List.Relation.Binary.Disjoint.Setoid.Properties where
1010

11+
open import Function.Base using (_∘_)
12+
open import Function.Bundles using (_⇔_; mk⇔)
1113
open import Data.List.Base
1214
open import Data.List.Relation.Binary.Disjoint.Setoid
1315
import Data.List.Relation.Unary.Any as Any
1416
open import Data.List.Relation.Unary.All as All
1517
open import Data.List.Relation.Unary.All.Properties using (¬Any⇒All¬)
1618
open import Data.List.Relation.Unary.Any.Properties using (++⁻)
17-
open import Data.Product.Base using (_,_)
19+
import Data.List.Membership.Setoid.Properties as Mem
20+
open import Data.Product.Base as Product using (_,_)
1821
open import Data.Sum.Base using (inj₁; inj₂)
1922
open import Relation.Binary.Bundles using (Setoid)
20-
open import Relation.Binary.Definitions using (Symmetric)
23+
open import Relation.Binary.Definitions using (Symmetric; DecidableEquality)
24+
open import Relation.Binary.PropositionalEquality.Core using (_≡_)
2125
open import Relation.Nullary.Negation using (¬_)
2226

23-
------------------------------------------------------------------------
24-
-- Relational properties
25-
------------------------------------------------------------------------
26-
2727
module _ {c ℓ} (S : Setoid c ℓ) where
2828

29-
sym : Symmetric (Disjoint S)
30-
sym xs#ys (v∈ys , v∈xs) = xs#ys (v∈xs , v∈ys)
29+
open Setoid S using (_≈_; reflexive) renaming (Carrier to A)
3130

32-
------------------------------------------------------------------------
33-
-- Relationship with other predicates
34-
------------------------------------------------------------------------
31+
------------------------------------------------------------------------
32+
-- Relational properties
33+
------------------------------------------------------------------------
3534

36-
module _ {c ℓ} (S : Setoid c ℓ) where
35+
sym : Symmetric (Disjoint S)
36+
sym xs#ys (v∈ys , v∈xs) = xs#ys (v∈xs , v∈ys)
3737

38-
open Setoid S
38+
------------------------------------------------------------------------
39+
-- Relationship with other predicates
40+
------------------------------------------------------------------------
3941

4042
Disjoint⇒AllAll : {xs ys} Disjoint S xs ys
4143
All (λ x All (λ y ¬ x ≈ y) ys) xs
4244
Disjoint⇒AllAll xs#ys = All.map (¬Any⇒All¬ _)
4345
(All.tabulate (λ v∈xs v∈ys xs#ys (Any.map reflexive v∈xs , v∈ys)))
4446

45-
------------------------------------------------------------------------
46-
-- Introduction (⁺) and elimination (⁻) rules for list operations
47-
------------------------------------------------------------------------
48-
-- concat
49-
50-
module _ {c ℓ} (S : Setoid c ℓ) where
47+
------------------------------------------------------------------------
48+
-- Introduction (⁺) and elimination (⁻) rules for list operations
49+
------------------------------------------------------------------------
50+
-- concat
5151

5252
concat⁺ʳ : {vs xss} All (Disjoint S vs) xss Disjoint S vs (concat xss)
5353
concat⁺ʳ {xss = xs ∷ xss} (vs#xs ∷ vs#xss) (v∈vs , v∈xs++concatxss)
5454
with ++⁻ xs v∈xs++concatxss
5555
... | inj₁ v∈xs = vs#xs (v∈vs , v∈xs)
5656
... | inj₂ v∈xss = concat⁺ʳ vs#xss (v∈vs , v∈xss)
57+
58+
-- deduplicate
59+
module _ (_≟_ : DecidableEquality A) where
60+
61+
open import Data.List.Membership.Setoid S using (_∈_)
62+
63+
private
64+
dedup≡ = deduplicate _≟_
65+
∈-dedup⁺ = Mem.∈-deduplicate⁺ S _≟_
66+
∈-dedup⁻ = Mem.∈-deduplicate⁻ S _≟_
67+
68+
deduplicate⁺ : {xs ys} Disjoint S xs ys Disjoint S (dedup≡ xs) (dedup≡ ys)
69+
deduplicate⁺ = _∘ Product.map (∈-dedup⁻ _) (∈-dedup⁻ _)
70+
71+
∈-dedup : {x xs} x ∈ xs ⇔ x ∈ dedup≡ xs
72+
∈-dedup = mk⇔ (∈-dedup⁺ λ where _≡_.refl x≈ x≈) (∈-dedup⁻ _)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Properties of permutation (with K)
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --safe --with-K #-}
8+
9+
module Data.List.Relation.Binary.Permutation.Propositional.Properties.WithK where
10+
11+
open import Function.Base using (_$_)
12+
open import Function.Bundles using (_⇔_; mk⇔)
13+
open import Function.Related.Propositional using (SK-sym; module EquationalReasoning)
14+
15+
open import Data.List.Base using (deduplicate; _++_)
16+
open import Data.List.Membership.Propositional using (_∈_)
17+
open import Data.List.Membership.Propositional.Properties
18+
using (∈-++⁻; ∈-++⁺ˡ; ∈-++⁺ʳ; ∈-++; ∈-deduplicate⁻)
19+
open import Data.List.Membership.Propositional.Properties.WithK
20+
using (unique∧set⇒bag)
21+
open import Data.List.Relation.Unary.Unique.Propositional.Properties using (++⁺)
22+
open import Data.List.Relation.Binary.Disjoint.Propositional using (Disjoint)
23+
open import Data.List.Relation.Binary.Disjoint.Propositional.Properties
24+
using (deduplicate⁺; ∈-dedup)
25+
open import Data.List.Relation.Binary.Permutation.Propositional using (_↭_)
26+
open import Data.List.Relation.Binary.BagAndSetEquality using (∼bag⇒↭)
27+
28+
open import Data.Sum as Sum using (_⊎_)
29+
open import Data.Sum.Function.Propositional using (_⊎-cong_)
30+
31+
open import Relation.Binary.Definitions using (DecidableEquality)
32+
33+
------------------------------------------------------------------------
34+
-- deduplicate
35+
36+
module _ {a}{A : Set a} (_≟_ : DecidableEquality A) where
37+
38+
private
39+
dedup≡ = deduplicate _≟_
40+
∈-dedup≡ = ∈-dedup _≟_
41+
42+
open import Data.List.Relation.Unary.Unique.DecPropositional.Properties _≟_
43+
using (deduplicate-!)
44+
45+
dedup-++-↭ : {xs ys} Disjoint xs ys dedup≡ (xs ++ ys) ↭ dedup≡ xs ++ dedup≡ ys
46+
dedup-++-↭ {xs}{ys} disj
47+
= ∼bag⇒↭
48+
$ unique∧set⇒bag
49+
(deduplicate-! _)
50+
(++⁺ (deduplicate-! _) (deduplicate-! _) (deduplicate⁺ _≟_ disj))
51+
λ {x}
52+
begin
53+
x ∈ dedup≡ (xs ++ ys)
54+
∼⟨ SK-sym ∈-dedup≡ ⟩
55+
x ∈ xs ++ ys
56+
∼⟨ ∈-++ ⟩
57+
(x ∈ xs ⊎ x ∈ ys)
58+
∼⟨ ∈-dedup≡ ⊎-cong ∈-dedup≡ ⟩
59+
(x ∈ dedup≡ xs ⊎ x ∈ dedup≡ ys)
60+
∼⟨ SK-sym ∈-++ ⟩
61+
x ∈ dedup≡ xs ++ dedup≡ ys
62+
where open EquationalReasoning

0 commit comments

Comments
 (0)