| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of the heterogeneous infix relation |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Relation.Binary.Infix.Heterogeneous.Properties where |
| 10 |
|
| 11 |
open import Level using (Level; _⊔_) |
| 12 |
open import Data.Bool.Base using (true; false) |
| 13 |
open import Data.List.Base as List using (List; []; _∷_; length; map; filter; replicate) |
| 14 |
open import Data.List.Relation.Binary.Infix.Heterogeneous |
| 15 |
open import Data.List.Relation.Binary.Prefix.Heterogeneous as Prefix |
| 16 |
using (Prefix; []; _∷_) |
| 17 |
open import Data.List.Relation.Binary.Pointwise.Base as Pointwise |
| 18 |
using (Pointwise) |
| 19 |
import Data.List.Relation.Binary.Prefix.Heterogeneous.Properties as Prefix |
| 20 |
open import Data.List.Relation.Binary.Suffix.Heterogeneous as Suffix |
| 21 |
using (Suffix; here; there) |
| 22 |
open import Data.Nat.Base using (zero; suc; _≤_) |
| 23 |
import Data.Nat.Properties as ℕ |
| 24 |
open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_]′) |
| 25 |
open import Function.Base using (case_of_; _$′_) |
| 26 |
open import Relation.Nullary.Decidable using (yes; no; does; map′; _⊎-dec_) |
| 27 |
open import Relation.Nullary.Negation.Core using (¬_; contradiction) |
| 28 |
open import Relation.Unary as U using (Pred) |
| 29 |
open import Relation.Binary.Core using (REL; _⇒_) |
| 30 |
open import Relation.Binary.Definitions using (Decidable; Trans; Antisym) |
| 31 |
open import Relation.Binary.PropositionalEquality.Core using (_≢_; refl; cong) |
| 32 |
open import Relation.Nullary.Negation.Core using (contradiction) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private |
| 37 |
variable |
| 38 |
a b r s : Level |
| 39 |
A : Set a |
| 40 |
B : Set b |
| 41 |
R : REL A B r |
| 42 |
S : REL A B s |
| 43 |
|
| 44 |
------------------------------------------------------------------------ |
| 45 |
-- Conversion functions |
| 46 |
|
| 47 |
fromPointwise : ∀ {as bs} → Pointwise R as bs → Infix R as bs |
| 48 |
fromPointwise pw = here (Prefix.fromPointwise pw) |
| 49 |
|
| 50 |
fromSuffix : ∀ {as bs} → Suffix R as bs → Infix R as bs |
| 51 |
fromSuffix (here pw) = fromPointwise pw |
| 52 |
fromSuffix (there p) = there (fromSuffix p) |
| 53 |
|
| 54 |
module _ {c t} {C : Set c} {T : REL A C t} where |
| 55 |
|
| 56 |
fromPrefixSuffix : Trans R S T → Trans (Prefix R) (Suffix S) (Infix T) |
| 57 |
fromPrefixSuffix tr p (here q) = here (Prefix.trans tr p (Prefix.fromPointwise q)) |
| 58 |
fromPrefixSuffix tr p (there q) = there (fromPrefixSuffix tr p q) |
| 59 |
|
| 60 |
fromSuffixPrefix : Trans R S T → Trans (Suffix R) (Prefix S) (Infix T) |
| 61 |
fromSuffixPrefix tr (here p) q = here (Prefix.trans tr (Prefix.fromPointwise p) q) |
| 62 |
fromSuffixPrefix tr (there p) (_ ∷ q) = there (fromSuffixPrefix tr p q) |
| 63 |
|
| 64 |
∷⁻ : ∀ {as b bs} → Infix R as (b ∷ bs) → Prefix R as (b ∷ bs) ⊎ Infix R as bs |
| 65 |
∷⁻ (here pref) = inj₁ pref |
| 66 |
∷⁻ (there inf) = inj₂ inf |
| 67 |
|
| 68 |
------------------------------------------------------------------------ |
| 69 |
-- length |
| 70 |
|
| 71 |
length-mono : ∀ {as bs} → Infix R as bs → length as ≤ length bs |
| 72 |
length-mono (here pref) = Prefix.length-mono pref |
| 73 |
length-mono (there p) = ℕ.m≤n⇒m≤1+n (length-mono p) |
| 74 |
|
| 75 |
------------------------------------------------------------------------ |
| 76 |
-- As an order |
| 77 |
|
| 78 |
module _ {c t} {C : Set c} {T : REL A C t} where |
| 79 |
|
| 80 |
Prefix-Infix-trans : Trans R S T → Trans (Prefix R) (Infix S) (Infix T) |
| 81 |
Prefix-Infix-trans tr p (here q) = here (Prefix.trans tr p q) |
| 82 |
Prefix-Infix-trans tr p (there q) = there (Prefix-Infix-trans tr p q) |
| 83 |
|
| 84 |
Infix-Prefix-trans : Trans R S T → Trans (Infix R) (Prefix S) (Infix T) |
| 85 |
Infix-Prefix-trans tr (here p) q = here (Prefix.trans tr p q) |
| 86 |
Infix-Prefix-trans tr (there p) (_ ∷ q) = there (Infix-Prefix-trans tr p q) |
| 87 |
|
| 88 |
Suffix-Infix-trans : Trans R S T → Trans (Suffix R) (Infix S) (Infix T) |
| 89 |
Suffix-Infix-trans tr p (here q) = fromSuffixPrefix tr p q |
| 90 |
Suffix-Infix-trans tr p (there q) = there (Suffix-Infix-trans tr p q) |
| 91 |
|
| 92 |
Infix-Suffix-trans : Trans R S T → Trans (Infix R) (Suffix S) (Infix T) |
| 93 |
Infix-Suffix-trans tr p (here q) = Infix-Prefix-trans tr p (Prefix.fromPointwise q) |
| 94 |
Infix-Suffix-trans tr p (there q) = there (Infix-Suffix-trans tr p q) |
| 95 |
|
| 96 |
trans : Trans R S T → Trans (Infix R) (Infix S) (Infix T) |
| 97 |
trans tr p (here q) = Infix-Prefix-trans tr p q |
| 98 |
trans tr p (there q) = there (trans tr p q) |
| 99 |
|
| 100 |
antisym : Antisym R S T → Antisym (Infix R) (Infix S) (Pointwise T) |
| 101 |
antisym asym (here p) (here q) = Prefix.antisym asym p q |
| 102 |
antisym asym {i = a ∷ as} {j = bs} p@(here _) (there q) |
| 103 |
= contradiction (begin-strict |
| 104 |
length as <⟨ length-mono p ⟩ |
| 105 |
length bs ≤⟨ length-mono q ⟩ |
| 106 |
length as ∎) (ℕ.<-irrefl refl) where open ℕ.≤-Reasoning |
| 107 |
antisym asym {i = as} {j = b ∷ bs} (there p) q@(here _) |
| 108 |
= contradiction (begin-strict |
| 109 |
length bs <⟨ length-mono q ⟩ |
| 110 |
length as ≤⟨ length-mono p ⟩ |
| 111 |
length bs ∎) (ℕ.<-irrefl refl) where open ℕ.≤-Reasoning |
| 112 |
antisym asym {i = a ∷ as} {j = b ∷ bs} (there p) (there q) |
| 113 |
= contradiction (begin-strict |
| 114 |
length as <⟨ length-mono p ⟩ |
| 115 |
length bs <⟨ length-mono q ⟩ |
| 116 |
length as ∎) (ℕ.<-irrefl refl) where open ℕ.≤-Reasoning |
| 117 |
|
| 118 |
------------------------------------------------------------------------ |
| 119 |
-- map |
| 120 |
|
| 121 |
module _ {c d r} {C : Set c} {D : Set d} {R : REL C D r} where |
| 122 |
|
| 123 |
map⁺ : ∀ {as bs} (f : A → C) (g : B → D) → |
| 124 |
Infix (λ a b → R (f a) (g b)) as bs → |
| 125 |
Infix R (List.map f as) (List.map g bs) |
| 126 |
map⁺ f g (here p) = here (Prefix.map⁺ f g p) |
| 127 |
map⁺ f g (there p) = there (map⁺ f g p) |
| 128 |
|
| 129 |
map⁻ : ∀ {as bs} (f : A → C) (g : B → D) → |
| 130 |
Infix R (List.map f as) (List.map g bs) → |
| 131 |
Infix (λ a b → R (f a) (g b)) as bs |
| 132 |
map⁻ {bs = []} f g (here p) = here (Prefix.map⁻ f g p) |
| 133 |
map⁻ {bs = b ∷ bs} f g (here p) = here (Prefix.map⁻ f g p) |
| 134 |
map⁻ {bs = b ∷ bs} f g (there p) = there (map⁻ f g p) |
| 135 |
|
| 136 |
------------------------------------------------------------------------ |
| 137 |
-- filter |
| 138 |
|
| 139 |
module _ {p q} {P : Pred A p} {Q : Pred B q} (P? : U.Decidable P) (Q? : U.Decidable Q) |
| 140 |
(P⇒Q : ∀ {a b} → P a → Q b) (Q⇒P : ∀ {a b} → Q b → P a) |
| 141 |
where |
| 142 |
|
| 143 |
filter⁺ : ∀ {as bs} → Infix R as bs → Infix R (filter P? as) (filter Q? bs) |
| 144 |
filter⁺ (here p) = here (Prefix.filter⁺ P? Q? (λ _ → P⇒Q) (λ _ → Q⇒P) p) |
| 145 |
filter⁺ {bs = b ∷ bs} (there p) with does (Q? b) |
| 146 |
... | true = there (filter⁺ p) |
| 147 |
... | false = filter⁺ p |
| 148 |
|
| 149 |
------------------------------------------------------------------------ |
| 150 |
-- replicate |
| 151 |
|
| 152 |
replicate⁺ : ∀ {m n a b} → m ≤ n → R a b → |
| 153 |
Infix R (replicate m a) (replicate n b) |
| 154 |
replicate⁺ m≤n r = here (Prefix.replicate⁺ m≤n r) |
| 155 |
|
| 156 |
replicate⁻ : ∀ {m n a b} → m ≢ 0 → |
| 157 |
Infix R (replicate m a) (replicate n b) → R a b |
| 158 |
replicate⁻ {m = m} {n = zero} m≢0 (here p) = Prefix.replicate⁻ m≢0 p |
| 159 |
replicate⁻ {m = m} {n = suc n} m≢0 (here p) = Prefix.replicate⁻ m≢0 p |
| 160 |
replicate⁻ {m = m} {n = suc n} m≢0 (there p) = replicate⁻ m≢0 p |
| 161 |
|
| 162 |
------------------------------------------------------------------------ |
| 163 |
-- decidability |
| 164 |
|
| 165 |
infix? : Decidable R → Decidable (Infix R) |
| 166 |
infix? R? [] [] = yes (here []) |
| 167 |
infix? R? (a ∷ as) [] = no (λ where (here ())) |
| 168 |
infix? R? as bbs@(_ ∷ bs) = |
| 169 |
map′ [ here , there ]′ ∷⁻ |
| 170 |
(Prefix.prefix? R? as bbs ⊎-dec infix? R? as bs) |