| 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.Propositional.Properties where |
| 10 |
|
| 11 |
open import Data.List.Base |
| 12 |
using ( List; _∷_; map; _++_; concat; cartesianProductWith |
| 13 |
; cartesianProduct; drop; take; applyUpTo; upTo; applyDownFrom; downFrom |
| 14 |
; tabulate; allFin; filter |
| 15 |
) |
| 16 |
open import Data.List.Membership.Propositional using (_∉_) |
| 17 |
open import Data.List.Relation.Binary.Disjoint.Propositional |
| 18 |
using (Disjoint) |
| 19 |
open import Data.List.Relation.Unary.All as All using (All; []; _∷_) |
| 20 |
open import Data.List.Relation.Unary.AllPairs as AllPairs using (AllPairs) |
| 21 |
open import Data.List.Relation.Unary.Unique.Propositional using (Unique) |
| 22 |
import Data.List.Relation.Unary.Unique.Setoid.Properties as Setoid |
| 23 |
open import Data.Fin.Base using (Fin) |
| 24 |
open import Data.Nat.Base using (_<_) |
| 25 |
open import Data.Nat.Properties using (<⇒≢) |
| 26 |
open import Data.Product.Base using (_×_; _,_) |
| 27 |
open import Data.Product.Relation.Binary.Pointwise.NonDependent using (≡⇒≡×≡) |
| 28 |
open import Function.Base using (id; _∘_) |
| 29 |
open import Level using (Level) |
| 30 |
open import Relation.Binary.Core using (Rel) |
| 31 |
open import Relation.Binary.Bundles using (Setoid) |
| 32 |
open import Relation.Binary.PropositionalEquality.Core |
| 33 |
using (refl; _≡_; _≢_; sym; cong) |
| 34 |
open import Relation.Binary.PropositionalEquality.Properties using (setoid) |
| 35 |
open import Relation.Unary using (Pred; Decidable) |
| 36 |
open import Relation.Nullary.Negation using (¬_) |
| 37 |
|
| 38 |
private |
| 39 |
variable |
| 40 |
a b c p : Level |
| 41 |
A : Set a |
| 42 |
x y : A |
| 43 |
xs : List A |
| 44 |
|
| 45 |
------------------------------------------------------------------------ |
| 46 |
-- Introduction (⁺) and elimination (⁻) rules for list operations |
| 47 |
------------------------------------------------------------------------ |
| 48 |
-- map |
| 49 |
|
| 50 |
module _ {A : Set a} {B : Set b} where |
| 51 |
|
| 52 |
map⁺ : ∀ {f} → (∀ {x y} → f x ≡ f y → x ≡ y) → |
| 53 |
∀ {xs} → Unique xs → Unique (map f xs) |
| 54 |
map⁺ = Setoid.map⁺ (setoid A) (setoid B) |
| 55 |
|
| 56 |
map⁻ : ∀ {f xs} → Unique (map f xs) → Unique xs |
| 57 |
map⁻ = Setoid.map⁻ (setoid A) (setoid B) (cong _) |
| 58 |
|
| 59 |
------------------------------------------------------------------------ |
| 60 |
-- ++ |
| 61 |
|
| 62 |
module _ {A : Set a} {xs ys} where |
| 63 |
|
| 64 |
++⁺ : Unique xs → Unique ys → Disjoint xs ys → Unique (xs ++ ys) |
| 65 |
++⁺ = Setoid.++⁺ (setoid A) |
| 66 |
|
| 67 |
------------------------------------------------------------------------ |
| 68 |
-- concat |
| 69 |
|
| 70 |
module _ {A : Set a} {xss} where |
| 71 |
|
| 72 |
concat⁺ : All Unique xss → AllPairs Disjoint xss → Unique (concat xss) |
| 73 |
concat⁺ = Setoid.concat⁺ (setoid A) |
| 74 |
|
| 75 |
------------------------------------------------------------------------ |
| 76 |
-- cartesianProductWith |
| 77 |
|
| 78 |
module _ {A : Set a} {B : Set b} {C : Set c} {xs ys} where |
| 79 |
|
| 80 |
cartesianProductWith⁺ : ∀ f → (∀ {w x y z} → f w y ≡ f x z → w ≡ x × y ≡ z) → |
| 81 |
Unique xs → Unique ys → |
| 82 |
Unique (cartesianProductWith f xs ys) |
| 83 |
cartesianProductWith⁺ = Setoid.cartesianProductWith⁺ (setoid A) (setoid B) (setoid C) |
| 84 |
|
| 85 |
------------------------------------------------------------------------ |
| 86 |
-- cartesianProduct |
| 87 |
|
| 88 |
module _ {A : Set a} {B : Set b} where |
| 89 |
|
| 90 |
cartesianProduct⁺ : ∀ {xs ys} → Unique xs → Unique ys → |
| 91 |
Unique (cartesianProduct xs ys) |
| 92 |
cartesianProduct⁺ xs ys = AllPairs.map (_∘ ≡⇒≡×≡) |
| 93 |
(Setoid.cartesianProduct⁺ (setoid A) (setoid B) xs ys) |
| 94 |
|
| 95 |
------------------------------------------------------------------------ |
| 96 |
-- take & drop |
| 97 |
|
| 98 |
module _ {A : Set a} where |
| 99 |
|
| 100 |
drop⁺ : ∀ {xs} n → Unique xs → Unique (drop n xs) |
| 101 |
drop⁺ = Setoid.drop⁺ (setoid A) |
| 102 |
|
| 103 |
take⁺ : ∀ {xs} n → Unique xs → Unique (take n xs) |
| 104 |
take⁺ = Setoid.take⁺ (setoid A) |
| 105 |
|
| 106 |
------------------------------------------------------------------------ |
| 107 |
-- applyUpTo & upTo |
| 108 |
|
| 109 |
module _ {A : Set a} where |
| 110 |
|
| 111 |
applyUpTo⁺₁ : ∀ f n → (∀ {i j} → i < j → j < n → f i ≢ f j) → |
| 112 |
Unique (applyUpTo f n) |
| 113 |
applyUpTo⁺₁ = Setoid.applyUpTo⁺₁ (setoid A) |
| 114 |
|
| 115 |
applyUpTo⁺₂ : ∀ f n → (∀ i j → f i ≢ f j) → |
| 116 |
Unique (applyUpTo f n) |
| 117 |
applyUpTo⁺₂ = Setoid.applyUpTo⁺₂ (setoid A) |
| 118 |
|
| 119 |
------------------------------------------------------------------------ |
| 120 |
-- upTo |
| 121 |
|
| 122 |
upTo⁺ : ∀ n → Unique (upTo n) |
| 123 |
upTo⁺ n = applyUpTo⁺₁ id n (λ i<j _ → <⇒≢ i<j) |
| 124 |
|
| 125 |
------------------------------------------------------------------------ |
| 126 |
-- applyDownFrom |
| 127 |
|
| 128 |
module _ {A : Set a} where |
| 129 |
|
| 130 |
applyDownFrom⁺₁ : ∀ f n → (∀ {i j} → j < i → i < n → f i ≢ f j) → |
| 131 |
Unique (applyDownFrom f n) |
| 132 |
applyDownFrom⁺₁ = Setoid.applyDownFrom⁺₁ (setoid A) |
| 133 |
|
| 134 |
applyDownFrom⁺₂ : ∀ f n → (∀ i j → f i ≢ f j) → |
| 135 |
Unique (applyDownFrom f n) |
| 136 |
applyDownFrom⁺₂ = Setoid.applyDownFrom⁺₂ (setoid A) |
| 137 |
|
| 138 |
------------------------------------------------------------------------ |
| 139 |
-- downFrom |
| 140 |
|
| 141 |
downFrom⁺ : ∀ n → Unique (downFrom n) |
| 142 |
downFrom⁺ n = applyDownFrom⁺₁ id n (λ j<i _ → <⇒≢ j<i ∘ sym) |
| 143 |
|
| 144 |
------------------------------------------------------------------------ |
| 145 |
-- tabulate |
| 146 |
|
| 147 |
module _ {A : Set a} where |
| 148 |
|
| 149 |
tabulate⁺ : ∀ {n} {f : Fin n → A} → (∀ {i j} → f i ≡ f j → i ≡ j) → |
| 150 |
Unique (tabulate f) |
| 151 |
tabulate⁺ = Setoid.tabulate⁺ (setoid A) |
| 152 |
|
| 153 |
------------------------------------------------------------------------ |
| 154 |
-- allFin |
| 155 |
|
| 156 |
allFin⁺ : ∀ n → Unique (allFin n) |
| 157 |
allFin⁺ n = tabulate⁺ id |
| 158 |
|
| 159 |
------------------------------------------------------------------------ |
| 160 |
-- filter |
| 161 |
|
| 162 |
module _ {A : Set a} {P : Pred _ p} (P? : Decidable P) where |
| 163 |
|
| 164 |
filter⁺ : ∀ {xs} → Unique xs → Unique (filter P? xs) |
| 165 |
filter⁺ = Setoid.filter⁺ (setoid A) P? |
| 166 |
|
| 167 |
------------------------------------------------------------------------ |
| 168 |
-- ∷ |
| 169 |
|
| 170 |
Unique[x∷xs]⇒x∉xs : Unique (x ∷ xs) → x ∉ xs |
| 171 |
Unique[x∷xs]⇒x∉xs = Setoid.Unique[x∷xs]⇒x∉xs (setoid _) |