| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of the heterogeneous suffix relation |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Relation.Binary.Suffix.Heterogeneous.Properties where |
| 10 |
|
| 11 |
open import Data.Bool.Base using (true; false) |
| 12 |
open import Data.List.Base as List |
| 13 |
using (List; []; _∷_; _++_; length; filter; replicate; reverse; reverseAcc) |
| 14 |
open import Data.List.Relation.Binary.Pointwise as Pw |
| 15 |
using (Pointwise; []; _∷_; Pointwise-length) |
| 16 |
open import Data.List.Relation.Binary.Suffix.Heterogeneous as Suffix |
| 17 |
using (Suffix; here; there; tail) |
| 18 |
open import Data.List.Relation.Binary.Prefix.Heterogeneous as Prefix |
| 19 |
using (Prefix) |
| 20 |
open import Data.Nat.Base |
| 21 |
open import Data.Nat.Properties |
| 22 |
open import Function.Base using (_$_; flip) |
| 23 |
open import Relation.Nullary using (Dec; does; ¬_) |
| 24 |
import Relation.Nullary.Decidable as Dec |
| 25 |
open import Relation.Unary as U using (Pred) |
| 26 |
open import Relation.Nullary.Negation using (contradiction) |
| 27 |
open import Relation.Binary.Core using (REL; Rel; _⇒_) |
| 28 |
open import Relation.Binary.Definitions as B |
| 29 |
using (Trans; Antisym; Irrelevant) |
| 30 |
open import Relation.Binary.PropositionalEquality.Core |
| 31 |
using (_≡_; _≢_; refl; sym; cong; subst; subst₂) |
| 32 |
|
| 33 |
import Data.List.Properties as List |
| 34 |
import Data.List.Relation.Binary.Prefix.Heterogeneous.Properties as Prefix |
| 35 |
|
| 36 |
------------------------------------------------------------------------ |
| 37 |
-- Suffix and Prefix are linked via reverse |
| 38 |
|
| 39 |
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where |
| 40 |
|
| 41 |
fromPrefix : ∀ {as bs} → Prefix R as bs → |
| 42 |
Suffix R (reverse as) (reverse bs) |
| 43 |
fromPrefix {as} {bs} p with Prefix._++_ {cs} rs ds ← Prefix.toView p = |
| 44 |
subst (Suffix R (reverse as)) |
| 45 |
(sym (List.reverse-++ cs ds)) |
| 46 |
(Suffix.fromView (reverse ds Suffix.++ Pw.reverse⁺ rs)) |
| 47 |
|
| 48 |
fromPrefix-rev : ∀ {as bs} → Prefix R (reverse as) (reverse bs) → |
| 49 |
Suffix R as bs |
| 50 |
fromPrefix-rev pre = |
| 51 |
subst₂ (Suffix R) |
| 52 |
(List.reverse-involutive _) |
| 53 |
(List.reverse-involutive _) |
| 54 |
(fromPrefix pre) |
| 55 |
|
| 56 |
toPrefix-rev : ∀ {as bs} → Suffix R as bs → |
| 57 |
Prefix R (reverse as) (reverse bs) |
| 58 |
toPrefix-rev {as} {bs} s with Suffix._++_ cs {ds} rs ← Suffix.toView s = |
| 59 |
subst (Prefix R (reverse as)) |
| 60 |
(sym (List.reverse-++ cs ds)) |
| 61 |
(Prefix.fromView (Pw.reverse⁺ rs Prefix.++ reverse cs)) |
| 62 |
|
| 63 |
toPrefix : ∀ {as bs} → Suffix R (reverse as) (reverse bs) → |
| 64 |
Prefix R as bs |
| 65 |
toPrefix suf = |
| 66 |
subst₂ (Prefix R) |
| 67 |
(List.reverse-involutive _) |
| 68 |
(List.reverse-involutive _) |
| 69 |
(toPrefix-rev suf) |
| 70 |
|
| 71 |
------------------------------------------------------------------------ |
| 72 |
-- length |
| 73 |
|
| 74 |
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where |
| 75 |
|
| 76 |
length-mono : ∀ {as bs} → Suffix R as bs → length as ≤ length bs |
| 77 |
length-mono (here rs) = ≤-reflexive (Pointwise-length rs) |
| 78 |
length-mono (there suf) = m≤n⇒m≤1+n (length-mono suf) |
| 79 |
|
| 80 |
S[as][bs]⇒∣as∣≢1+∣bs∣ : ∀ {as bs} → Suffix R as bs → |
| 81 |
length as ≢ suc (length bs) |
| 82 |
S[as][bs]⇒∣as∣≢1+∣bs∣ suf eq = <⇒≱ (≤-reflexive (sym eq)) (length-mono suf) |
| 83 |
|
| 84 |
------------------------------------------------------------------------ |
| 85 |
-- Pointwise conversion |
| 86 |
|
| 87 |
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where |
| 88 |
|
| 89 |
fromPointwise : Pointwise R ⇒ Suffix R |
| 90 |
fromPointwise = here |
| 91 |
|
| 92 |
toPointwise : ∀ {as bs} → length as ≡ length bs → |
| 93 |
Suffix R as bs → Pointwise R as bs |
| 94 |
toPointwise eq (here rs) = rs |
| 95 |
toPointwise eq (there suf) = contradiction eq (S[as][bs]⇒∣as∣≢1+∣bs∣ suf) |
| 96 |
|
| 97 |
------------------------------------------------------------------------ |
| 98 |
-- Suffix as a partial order |
| 99 |
|
| 100 |
module _ {a b c r s t} {A : Set a} {B : Set b} {C : Set c} |
| 101 |
{R : REL A B r} {S : REL B C s} {T : REL A C t} where |
| 102 |
|
| 103 |
trans : Trans R S T → Trans (Suffix R) (Suffix S) (Suffix T) |
| 104 |
trans rs⇒t (here rs) (here ss) = here (Pw.transitive rs⇒t rs ss) |
| 105 |
trans rs⇒t (here rs) (there ssuf) = there (trans rs⇒t (here rs) ssuf) |
| 106 |
trans rs⇒t (there rsuf) ssuf = trans rs⇒t rsuf (tail ssuf) |
| 107 |
|
| 108 |
module _ {a b e r s} {A : Set a} {B : Set b} |
| 109 |
{R : REL A B r} {S : REL B A s} {E : REL A B e} where |
| 110 |
|
| 111 |
antisym : Antisym R S E → Antisym (Suffix R) (Suffix S) (Pointwise E) |
| 112 |
antisym rs⇒e rsuf ssuf = Pw.antisymmetric |
| 113 |
rs⇒e |
| 114 |
(toPointwise eq rsuf) |
| 115 |
(toPointwise (sym eq) ssuf) |
| 116 |
where eq = ≤-antisym (length-mono rsuf) (length-mono ssuf) |
| 117 |
|
| 118 |
------------------------------------------------------------------------ |
| 119 |
-- _++_ |
| 120 |
|
| 121 |
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where |
| 122 |
|
| 123 |
++⁺ : ∀ {as bs cs ds} → Suffix R as bs → Pointwise R cs ds → |
| 124 |
Suffix R (as ++ cs) (bs ++ ds) |
| 125 |
++⁺ (here rs) rs′ = here (Pw.++⁺ rs rs′) |
| 126 |
++⁺ (there suf) rs′ = there (++⁺ suf rs′) |
| 127 |
|
| 128 |
++⁻ : ∀ {as bs cs ds} → length cs ≡ length ds → |
| 129 |
Suffix R (as ++ cs) (bs ++ ds) → Pointwise R cs ds |
| 130 |
++⁻ {_ ∷ _} {_} {_} {_} eq suf = ++⁻ eq (tail suf) |
| 131 |
++⁻ {[]} {[]} {_} {_} eq suf = toPointwise eq suf |
| 132 |
++⁻ {[]} {b ∷ bs} {_} {_} eq (there suf) = ++⁻ eq suf |
| 133 |
++⁻ {[]} {b ∷ bs} {cs} {ds} eq (here rs) = contradiction (sym eq) (<⇒≢ ds<cs) |
| 134 |
where |
| 135 |
open ≤-Reasoning |
| 136 |
ds<cs : length ds < length cs |
| 137 |
ds<cs = begin-strict |
| 138 |
length ds ≤⟨ m≤n+m (length ds) (length bs) ⟩ |
| 139 |
length bs + length ds <⟨ ≤-refl ⟩ |
| 140 |
suc (length bs + length ds) ≡⟨ sym $ List.length-++ (b ∷ bs) ⟩ |
| 141 |
length (b ∷ bs ++ ds) ≡⟨ sym $ Pointwise-length rs ⟩ |
| 142 |
length cs ∎ |
| 143 |
|
| 144 |
------------------------------------------------------------------------ |
| 145 |
-- map |
| 146 |
|
| 147 |
module _ {a b c d r} {A : Set a} {B : Set b} {C : Set c} {D : Set d} |
| 148 |
{R : REL C D r} where |
| 149 |
|
| 150 |
map⁺ : ∀ {as bs} (f : A → C) (g : B → D) → |
| 151 |
Suffix (λ a b → R (f a) (g b)) as bs → |
| 152 |
Suffix R (List.map f as) (List.map g bs) |
| 153 |
map⁺ f g (here rs) = here (Pw.map⁺ f g rs) |
| 154 |
map⁺ f g (there suf) = there (map⁺ f g suf) |
| 155 |
|
| 156 |
map⁻ : ∀ {as bs} (f : A → C) (g : B → D) → |
| 157 |
Suffix R (List.map f as) (List.map g bs) → |
| 158 |
Suffix (λ a b → R (f a) (g b)) as bs |
| 159 |
map⁻ {as} {b ∷ bs} f g (here rs) = here (Pw.map⁻ f g rs) |
| 160 |
map⁻ {as} {b ∷ bs} f g (there suf) = there (map⁻ f g suf) |
| 161 |
map⁻ {x ∷ as} {[]} f g suf = contradiction (length-mono suf) λ() |
| 162 |
map⁻ {[]} {[]} f g suf = here [] |
| 163 |
|
| 164 |
------------------------------------------------------------------------ |
| 165 |
-- filter |
| 166 |
|
| 167 |
module _ {a b r p q} {A : Set a} {B : Set b} {R : REL A B r} |
| 168 |
{P : Pred A p} {Q : Pred B q} |
| 169 |
(P? : U.Decidable P) (Q? : U.Decidable Q) |
| 170 |
(P⇒Q : ∀ {a b} → R a b → P a → Q b) |
| 171 |
(Q⇒P : ∀ {a b} → R a b → Q b → P a) |
| 172 |
where |
| 173 |
|
| 174 |
filter⁺ : ∀ {as bs} → Suffix R as bs → |
| 175 |
Suffix R (filter P? as) (filter Q? bs) |
| 176 |
filter⁺ (here rs) = here (Pw.filter⁺ P? Q? P⇒Q Q⇒P rs) |
| 177 |
filter⁺ (there {a} suf) with does (Q? a) |
| 178 |
... | true = there (filter⁺ suf) |
| 179 |
... | false = filter⁺ suf |
| 180 |
|
| 181 |
------------------------------------------------------------------------ |
| 182 |
-- replicate |
| 183 |
|
| 184 |
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where |
| 185 |
|
| 186 |
replicate⁺ : ∀ {m n a b} → m ≤ n → R a b → |
| 187 |
Suffix R (replicate m a) (replicate n b) |
| 188 |
replicate⁺ {a = a} {b = b} m≤n r = repl (≤⇒≤′ m≤n) |
| 189 |
where |
| 190 |
repl : ∀ {m n} → m ≤′ n → Suffix R (replicate m a) (replicate n b) |
| 191 |
repl ≤′-refl = here (Pw.replicate⁺ r _) |
| 192 |
repl (≤′-step m≤n) = there (repl m≤n) |
| 193 |
|
| 194 |
------------------------------------------------------------------------ |
| 195 |
-- Irrelevant |
| 196 |
|
| 197 |
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where |
| 198 |
|
| 199 |
irrelevant : Irrelevant R → Irrelevant (Suffix R) |
| 200 |
irrelevant irr (here rs) (here rs₁) = cong here $ Pw.irrelevant irr rs rs₁ |
| 201 |
irrelevant irr (here rs) (there rsuf) = contradiction (Pointwise-length rs) (S[as][bs]⇒∣as∣≢1+∣bs∣ rsuf) |
| 202 |
irrelevant irr (there rsuf) (here rs) = contradiction (Pointwise-length rs) (S[as][bs]⇒∣as∣≢1+∣bs∣ rsuf) |
| 203 |
irrelevant irr (there rsuf) (there rsuf₁) = cong there $ irrelevant irr rsuf rsuf₁ |
| 204 |
|
| 205 |
------------------------------------------------------------------------ |
| 206 |
-- Decidability |
| 207 |
|
| 208 |
suffix? : B.Decidable R → B.Decidable (Suffix R) |
| 209 |
suffix? R? as bs = Dec.map′ fromPrefix-rev toPrefix-rev |
| 210 |
$ Prefix.prefix? R? (reverse as) (reverse bs) |