Debian Codegraph @

src/Data/List/Relation/Unary/Unique/Setoid/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of unique lists (setoid equality)
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Relation.Unary.Unique.Setoid.Properties where
10
11 open import Data.List.Base
12 import Data.List.Membership.Setoid as Membership
13 open import Data.List.Membership.Setoid.Properties
14 open import Data.List.Relation.Binary.Disjoint.Setoid
15 open import Data.List.Relation.Binary.Disjoint.Setoid.Properties
16 open import Data.List.Relation.Unary.Any using (here; there)
17 open import Data.List.Relation.Unary.All as All using (All; []; _∷_)
18 open import Data.List.Relation.Unary.All.Properties using (All¬⇒¬Any)
19 open import Data.List.Relation.Unary.AllPairs as AllPairs using (AllPairs)
20 open import Data.List.Relation.Unary.Unique.Setoid
21 open import Data.Product.Base using (_×_; _,_; proj₁; proj₂)
22 open import Data.Product.Relation.Binary.Pointwise.NonDependent using (_×ₛ_)
23 import Data.List.Relation.Unary.AllPairs.Properties as AllPairs
24 open import Data.Fin.Base using (Fin)
25 open import Data.Nat.Base using (_<_)
26 open import Function.Base using (_∘_; id)
27 open import Function.Definitions using (Congruent)
28 open import Level using (Level)
29 open import Relation.Binary.Core using (Rel)
30 open import Relation.Binary.Bundles using (Setoid)
31 open import Relation.Binary.PropositionalEquality.Core using (_≡_)
32 open import Relation.Unary using (Pred; Decidable)
33 open import Relation.Nullary.Negation using (¬_)
34 open import Relation.Nullary.Negation using (contraposition)
35
36 private
37 variable
38 a b c p ℓ ℓ₁ ℓ₂ ℓ₃ : Level
39
40 ------------------------------------------------------------------------
41 -- Introduction (⁺) and elimination (⁻) rules for list operations
42 ------------------------------------------------------------------------
43 -- map
44
45 module _ (S : Setoid a ℓ₁) (R : Setoid b ℓ₂) where
46
47 open Setoid S renaming (_≈_ to _≈₁_)
48 open Setoid R renaming (_≈_ to _≈₂_)
49
50 map⁺ : ∀ {f} → (∀ {x y} → f x ≈₂ f y → x ≈₁ y) →
51 ∀ {xs} → Unique S xs → Unique R (map f xs)
52 map⁺ inj xs! = AllPairs.map⁺ (AllPairs.map (contraposition inj) xs!)
53
54 map⁻ : ∀ {f} → Congruent _≈₁_ _≈₂_ f →
55 ∀ {xs} → Unique R (map f xs) → Unique S xs
56 map⁻ cong fxs! = AllPairs.map (contraposition cong) (AllPairs.map⁻ fxs!)
57
58 ------------------------------------------------------------------------
59 -- ++
60
61 module _ (S : Setoid a ℓ) where
62
63 ++⁺ : ∀ {xs ys} → Unique S xs → Unique S ys → Disjoint S xs ys → Unique S (xs ++ ys)
64 ++⁺ xs! ys! xs#ys = AllPairs.++⁺ xs! ys! (Disjoint⇒AllAll S xs#ys)
65
66 ------------------------------------------------------------------------
67 -- concat
68
69 module _ (S : Setoid a ℓ) where
70
71 concat⁺ : ∀ {xss} → All (Unique S) xss → AllPairs (Disjoint S) xss → Unique S (concat xss)
72 concat⁺ xss! xss# = AllPairs.concat⁺ xss! (AllPairs.map (Disjoint⇒AllAll S) xss#)
73
74 ------------------------------------------------------------------------
75 -- cartesianProductWith
76
77 module _ (S : Setoid a ℓ₁) (T : Setoid b ℓ₂) (U : Setoid c ℓ₃) where
78
79 open Setoid S using () renaming (_≈_ to _≈₁_)
80 open Setoid T using () renaming (_≈_ to _≈₂_)
81 open Setoid U using () renaming (_≈_ to _≈₃_; sym to sym₃; trans to trans₃)
82
83 cartesianProductWith⁺ : ∀ {xs ys} f → (∀ {w x y z} → f w y ≈₃ f x z → w ≈₁ x × y ≈₂ z) →
84 Unique S xs → Unique T ys →
85 Unique U (cartesianProductWith f xs ys)
86 cartesianProductWith⁺ {_} {_} f f-inj [] ys! = [] {S = U}
87 cartesianProductWith⁺ {x ∷ xs} {ys} f f-inj (x∉xs ∷ xs!) ys! = ++⁺ U
88 (map⁺ T U (proj₂ ∘ f-inj) ys!)
89 (cartesianProductWith⁺ f f-inj xs! ys!)
90 map#cartesianProductWith
91 where
92 map#cartesianProductWith : Disjoint U (map (f x) ys) (cartesianProductWith f xs ys)
93 map#cartesianProductWith (v∈map , v∈com) with
94 ∈-map⁻ T U v∈map | ∈-cartesianProductWith⁻ S T U f xs ys v∈com
95 ... | (c , _ , v≈fxc) | (a , b , a∈xs , _ , v≈fab) =
96 All¬⇒¬Any x∉xs (∈-resp-≈ S (proj₁ (f-inj (trans₃ (sym₃ v≈fab) v≈fxc))) a∈xs)
97
98 ------------------------------------------------------------------------
99 -- cartesianProduct
100
101 module _ (S : Setoid a ℓ₁) (T : Setoid b ℓ₂) {xs ys} where
102
103 cartesianProduct⁺ : Unique S xs → Unique T ys →
104 Unique (S ×ₛ T) (cartesianProduct xs ys)
105 cartesianProduct⁺ = cartesianProductWith⁺ S T (S ×ₛ T) _,_ id
106
107 ------------------------------------------------------------------------
108 -- take & drop
109
110 module _ (S : Setoid a ℓ) where
111
112 drop⁺ : ∀ {xs} n → Unique S xs → Unique S (drop n xs)
113 drop⁺ = AllPairs.drop⁺
114
115 take⁺ : ∀ {xs} n → Unique S xs → Unique S (take n xs)
116 take⁺ = AllPairs.take⁺
117
118 ------------------------------------------------------------------------
119 -- applyUpTo
120
121 module _ (S : Setoid a ℓ) where
122
123 open Setoid S
124
125 applyUpTo⁺₁ : ∀ f n → (∀ {i j} → i < j → j < n → f i ≉ f j) →
126 Unique S (applyUpTo f n)
127 applyUpTo⁺₁ = AllPairs.applyUpTo⁺₁
128
129 applyUpTo⁺₂ : ∀ f n → (∀ i j → f i ≉ f j) →
130 Unique S (applyUpTo f n)
131 applyUpTo⁺₂ = AllPairs.applyUpTo⁺₂
132
133 ------------------------------------------------------------------------
134 -- applyDownFrom
135
136 module _ (S : Setoid a ℓ) where
137
138 open Setoid S
139
140 applyDownFrom⁺₁ : ∀ f n → (∀ {i j} → j < i → i < n → f i ≉ f j) →
141 Unique S (applyDownFrom f n)
142 applyDownFrom⁺₁ = AllPairs.applyDownFrom⁺₁
143
144 applyDownFrom⁺₂ : ∀ f n → (∀ i j → f i ≉ f j) →
145 Unique S (applyDownFrom f n)
146 applyDownFrom⁺₂ = AllPairs.applyDownFrom⁺₂
147
148 ------------------------------------------------------------------------
149 -- tabulate
150
151 module _ (S : Setoid a ℓ) where
152
153 open Setoid S renaming (Carrier to A)
154
155 tabulate⁺ : ∀ {n} {f : Fin n → A} → (∀ {i j} → f i ≈ f j → i ≡ j) →
156 Unique S (tabulate f)
157 tabulate⁺ f-inj = AllPairs.tabulate⁺ (_∘ f-inj)
158
159 ------------------------------------------------------------------------
160 -- filter
161
162 module _ (S : Setoid a ℓ) {P : Pred _ p} (P? : Decidable P) where
163
164 filter⁺ : ∀ {xs} → Unique S xs → Unique S (filter P? xs)
165 filter⁺ = AllPairs.filter⁺ P?
166
167 ------------------------------------------------------------------------
168 -- ∷
169
170 module _ (S : Setoid a ℓ) where
171
172 open Setoid S renaming (Carrier to A)
173 open Membership S using (_∉_)
174
175 private
176 variable
177 x y : A
178 xs : List A
179
180 Unique[x∷xs]⇒x∉xs : Unique S (x ∷ xs) → x ∉ xs
181 Unique[x∷xs]⇒x∉xs ((x≉ ∷ x∉) ∷ _ ∷ uniq) = λ where
182 (here x≈) → x≉ x≈
183 (there x∈) → Unique[x∷xs]⇒x∉xs (x∉ AllPairs.∷ uniq) x∈