| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Pointwise equality over lists parameterised by a setoid |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Relation.Binary.Bundles using (Setoid) |
| 10 |
|
| 11 |
module Data.List.Relation.Binary.Equality.Setoid {a ℓ} (S : Setoid a ℓ) where |
| 12 |
open import Algebra.Core using (Op₂) |
| 13 |
open import Data.Fin.Base using (Fin) |
| 14 |
open import Data.List.Base using (List; length; map; foldr; _++_; concat; |
| 15 |
tabulate; filter; _ʳ++_; reverse) |
| 16 |
open import Data.List.Relation.Binary.Pointwise as PW using (Pointwise) |
| 17 |
open import Data.List.Relation.Unary.Unique.Setoid S using (Unique) |
| 18 |
open import Function.Base using (_∘_) |
| 19 |
open import Level using (Level; _⊔_) |
| 20 |
open import Relation.Binary.Core using (_⇒_; _Preserves_⟶_) renaming (Rel to Rel₂) |
| 21 |
open import Relation.Binary.Definitions using (Transitive; Symmetric; Reflexive; _Respects_) |
| 22 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 23 |
open import Relation.Binary.Properties.Setoid S using (≉-resp₂) |
| 24 |
open import Relation.Binary.Structures using (IsEquivalence) |
| 25 |
open import Relation.Unary as U using (Pred) |
| 26 |
|
| 27 |
open Setoid S renaming (Carrier to A) |
| 28 |
|
| 29 |
private |
| 30 |
variable |
| 31 |
p q : Level |
| 32 |
ws xs xs′ ys ys′ zs : List A |
| 33 |
xss yss : List (List A) |
| 34 |
|
| 35 |
------------------------------------------------------------------------ |
| 36 |
-- Definition of equality |
| 37 |
------------------------------------------------------------------------ |
| 38 |
|
| 39 |
infix 4 _≋_ |
| 40 |
|
| 41 |
_≋_ : Rel₂ (List A) (a ⊔ ℓ) |
| 42 |
_≋_ = Pointwise _≈_ |
| 43 |
|
| 44 |
open PW public |
| 45 |
using ([]; _∷_) |
| 46 |
|
| 47 |
------------------------------------------------------------------------ |
| 48 |
-- Relational properties |
| 49 |
------------------------------------------------------------------------ |
| 50 |
|
| 51 |
≋-setoid : Setoid _ _ |
| 52 |
≋-setoid = PW.setoid S |
| 53 |
|
| 54 |
open Setoid ≋-setoid public |
| 55 |
using () |
| 56 |
renaming ( refl to ≋-refl |
| 57 |
; reflexive to ≋-reflexive |
| 58 |
; sym to ≋-sym |
| 59 |
; trans to ≋-trans |
| 60 |
; isEquivalence to ≋-isEquivalence) |
| 61 |
|
| 62 |
------------------------------------------------------------------------ |
| 63 |
-- Relationships to predicates |
| 64 |
------------------------------------------------------------------------ |
| 65 |
|
| 66 |
open PW public |
| 67 |
using () renaming |
| 68 |
( Any-resp-Pointwise to Any-resp-≋ |
| 69 |
; All-resp-Pointwise to All-resp-≋ |
| 70 |
; AllPairs-resp-Pointwise to AllPairs-resp-≋ |
| 71 |
) |
| 72 |
|
| 73 |
Unique-resp-≋ : Unique Respects _≋_ |
| 74 |
Unique-resp-≋ = AllPairs-resp-≋ ≉-resp₂ |
| 75 |
|
| 76 |
------------------------------------------------------------------------ |
| 77 |
-- List operations |
| 78 |
------------------------------------------------------------------------ |
| 79 |
|
| 80 |
------------------------------------------------------------------------ |
| 81 |
-- length |
| 82 |
|
| 83 |
≋-length : ∀ {xs ys} → xs ≋ ys → length xs ≡ length ys |
| 84 |
≋-length = PW.Pointwise-length |
| 85 |
|
| 86 |
------------------------------------------------------------------------ |
| 87 |
-- map |
| 88 |
|
| 89 |
module _ {b ℓ₂} (T : Setoid b ℓ₂) where |
| 90 |
|
| 91 |
open Setoid T using () renaming (_≈_ to _≈′_) |
| 92 |
private |
| 93 |
_≋′_ = Pointwise _≈′_ |
| 94 |
|
| 95 |
map⁺ : ∀ {f} → f Preserves _≈_ ⟶ _≈′_ → |
| 96 |
∀ {xs ys} → xs ≋ ys → map f xs ≋′ map f ys |
| 97 |
map⁺ {f} pres xs≋ys = PW.map⁺ f f (PW.map pres xs≋ys) |
| 98 |
|
| 99 |
------------------------------------------------------------------------ |
| 100 |
-- foldr |
| 101 |
|
| 102 |
foldr⁺ : ∀ {_•_ : Op₂ A} {_◦_ : Op₂ A} → |
| 103 |
(∀ {w x y z} → w ≈ x → y ≈ z → (w • y) ≈ (x ◦ z)) → |
| 104 |
∀ {xs ys e f} → e ≈ f → xs ≋ ys → |
| 105 |
foldr _•_ e xs ≈ foldr _◦_ f ys |
| 106 |
foldr⁺ ∙⇔◦ e≈f xs≋ys = PW.foldr⁺ ∙⇔◦ e≈f xs≋ys |
| 107 |
|
| 108 |
------------------------------------------------------------------------ |
| 109 |
-- _++_ |
| 110 |
|
| 111 |
++⁺ : ws ≋ xs → ys ≋ zs → ws ++ ys ≋ xs ++ zs |
| 112 |
++⁺ = PW.++⁺ |
| 113 |
|
| 114 |
++⁺ˡ : ∀ xs → ys ≋ zs → xs ++ ys ≋ xs ++ zs |
| 115 |
++⁺ˡ xs = PW.++⁺ˡ refl xs |
| 116 |
|
| 117 |
++⁺ʳ : ∀ zs → ws ≋ xs → ws ++ zs ≋ xs ++ zs |
| 118 |
++⁺ʳ zs = PW.++⁺ʳ refl zs |
| 119 |
|
| 120 |
++-cancelˡ : ∀ xs {ys zs} → xs ++ ys ≋ xs ++ zs → ys ≋ zs |
| 121 |
++-cancelˡ xs = PW.++-cancelˡ xs |
| 122 |
|
| 123 |
++-cancelʳ : ∀ {xs} ys zs → ys ++ xs ≋ zs ++ xs → ys ≋ zs |
| 124 |
++-cancelʳ = PW.++-cancelʳ |
| 125 |
|
| 126 |
------------------------------------------------------------------------ |
| 127 |
-- concat |
| 128 |
|
| 129 |
concat⁺ : Pointwise _≋_ xss yss → concat xss ≋ concat yss |
| 130 |
concat⁺ = PW.concat⁺ |
| 131 |
|
| 132 |
------------------------------------------------------------------------ |
| 133 |
-- tabulate |
| 134 |
|
| 135 |
module _ {n} {f g : Fin n → A} |
| 136 |
where |
| 137 |
|
| 138 |
tabulate⁺ : (∀ i → f i ≈ g i) → tabulate f ≋ tabulate g |
| 139 |
tabulate⁺ = PW.tabulate⁺ |
| 140 |
|
| 141 |
tabulate⁻ : tabulate f ≋ tabulate g → (∀ i → f i ≈ g i) |
| 142 |
tabulate⁻ = PW.tabulate⁻ |
| 143 |
|
| 144 |
------------------------------------------------------------------------ |
| 145 |
-- filter |
| 146 |
|
| 147 |
module _ {P : Pred A p} (P? : U.Decidable P) (resp : P Respects _≈_) |
| 148 |
where |
| 149 |
|
| 150 |
filter⁺ : xs ≋ ys → filter P? xs ≋ filter P? ys |
| 151 |
filter⁺ xs≋ys = PW.filter⁺ P? P? resp (resp ∘ sym) xs≋ys |
| 152 |
|
| 153 |
------------------------------------------------------------------------ |
| 154 |
-- reverse |
| 155 |
|
| 156 |
ʳ++⁺ : xs ≋ xs′ → ys ≋ ys′ → xs ʳ++ ys ≋ xs′ ʳ++ ys′ |
| 157 |
ʳ++⁺ = PW.ʳ++⁺ |
| 158 |
|
| 159 |
reverse⁺ : xs ≋ ys → reverse xs ≋ reverse ys |
| 160 |
reverse⁺ = PW.reverse⁺ |