| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of the heterogeneous prefix relation |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Relation.Binary.Prefix.Heterogeneous.Properties where |
| 10 |
|
| 11 |
open import Level using (Level; _⊔_) |
| 12 |
open import Data.Bool.Base using (true; false) |
| 13 |
open import Data.List.Relation.Unary.All as All using (All; []; _∷_) |
| 14 |
import Data.List.Relation.Unary.All.Properties as All |
| 15 |
open import Data.List.Base as List hiding (map; uncons) |
| 16 |
open import Data.List.Membership.Propositional.Properties using ([]∈inits) |
| 17 |
open import Data.List.Relation.Binary.Pointwise.Base using (Pointwise; []; _∷_) |
| 18 |
open import Data.List.Relation.Binary.Prefix.Heterogeneous as Prefix |
| 19 |
hiding (PrefixView; _++_) |
| 20 |
open import Data.Nat.Base using (ℕ; zero; suc; _≤_; z≤n; s≤s) |
| 21 |
open import Data.Nat.Properties using (suc-injective) |
| 22 |
open import Data.Product.Base as Product |
| 23 |
using (_×_; _,_; proj₁; proj₂; uncurry) |
| 24 |
open import Function.Base using (flip; _∘_; _$_) |
| 25 |
open import Relation.Binary.Core using (Rel; REL; _⇒_) |
| 26 |
open import Relation.Binary.Definitions |
| 27 |
using (Trans; Antisym; Irrelevant; Decidable) |
| 28 |
open import Relation.Binary.PropositionalEquality.Core |
| 29 |
using (_≡_; _≢_; refl; cong₂) |
| 30 |
open import Relation.Nullary.Decidable.Core as Dec |
| 31 |
using (_×-dec_; yes; no; _because_) |
| 32 |
open import Relation.Nullary.Negation.Core using (¬_; contradiction) |
| 33 |
open import Relation.Unary as U using (Pred) |
| 34 |
|
| 35 |
private |
| 36 |
variable |
| 37 |
a b r s : Level |
| 38 |
A : Set a |
| 39 |
B : Set b |
| 40 |
R : REL A B r |
| 41 |
S : REL A B s |
| 42 |
|
| 43 |
------------------------------------------------------------------------ |
| 44 |
-- First as a decidable partial order (once made homogeneous) |
| 45 |
|
| 46 |
fromPointwise : Pointwise R ⇒ Prefix R |
| 47 |
fromPointwise [] = [] |
| 48 |
fromPointwise (r ∷ rs) = r ∷ fromPointwise rs |
| 49 |
|
| 50 |
toPointwise : ∀ {as bs} → length as ≡ length bs → |
| 51 |
Prefix R as bs → Pointwise R as bs |
| 52 |
toPointwise {bs = []} eq [] = [] |
| 53 |
toPointwise eq (r ∷ rs) = r ∷ toPointwise (suc-injective eq) rs |
| 54 |
|
| 55 |
module _ {a b c r s t} {A : Set a} {B : Set b} {C : Set c} |
| 56 |
{R : REL A B r} {S : REL B C s} {T : REL A C t} where |
| 57 |
|
| 58 |
trans : Trans R S T → Trans (Prefix R) (Prefix S) (Prefix T) |
| 59 |
trans rs⇒t [] ss = [] |
| 60 |
trans rs⇒t (r ∷ rs) (s ∷ ss) = rs⇒t r s ∷ trans rs⇒t rs ss |
| 61 |
|
| 62 |
module _ {a b r s e} {A : Set a} {B : Set b} |
| 63 |
{R : REL A B r} {S : REL B A s} {E : REL A B e} where |
| 64 |
|
| 65 |
antisym : Antisym R S E → Antisym (Prefix R) (Prefix S) (Pointwise E) |
| 66 |
antisym rs⇒e [] [] = [] |
| 67 |
antisym rs⇒e (r ∷ rs) (s ∷ ss) = rs⇒e r s ∷ antisym rs⇒e rs ss |
| 68 |
|
| 69 |
------------------------------------------------------------------------ |
| 70 |
-- length |
| 71 |
|
| 72 |
length-mono : ∀ {as bs} → Prefix R as bs → length as ≤ length bs |
| 73 |
length-mono [] = z≤n |
| 74 |
length-mono (r ∷ rs) = s≤s (length-mono rs) |
| 75 |
|
| 76 |
------------------------------------------------------------------------ |
| 77 |
-- _++_ |
| 78 |
|
| 79 |
++⁺ : ∀ {as bs cs ds} → Pointwise R as bs → |
| 80 |
Prefix R cs ds → Prefix R (as ++ cs) (bs ++ ds) |
| 81 |
++⁺ [] cs⊆ds = cs⊆ds |
| 82 |
++⁺ (r ∷ rs) cs⊆ds = r ∷ (++⁺ rs cs⊆ds) |
| 83 |
|
| 84 |
++⁻ : ∀ {as bs cs ds} → length as ≡ length bs → |
| 85 |
Prefix R (as ++ cs) (bs ++ ds) → Prefix R cs ds |
| 86 |
++⁻ {as = []} {[]} eq rs = rs |
| 87 |
++⁻ {as = _ ∷ _} {_ ∷ _} eq (_ ∷ rs) = ++⁻ (suc-injective eq) rs |
| 88 |
|
| 89 |
------------------------------------------------------------------------ |
| 90 |
-- map |
| 91 |
|
| 92 |
module _ {a b c d r} {A : Set a} {B : Set b} {C : Set c} {D : Set d} |
| 93 |
{R : REL C D r} where |
| 94 |
|
| 95 |
map⁺ : ∀ {as bs} (f : A → C) (g : B → D) → |
| 96 |
Prefix (λ a b → R (f a) (g b)) as bs → |
| 97 |
Prefix R (List.map f as) (List.map g bs) |
| 98 |
map⁺ f g [] = [] |
| 99 |
map⁺ f g (r ∷ rs) = r ∷ map⁺ f g rs |
| 100 |
|
| 101 |
map⁻ : ∀ {as bs} (f : A → C) (g : B → D) → |
| 102 |
Prefix R (List.map f as) (List.map g bs) → |
| 103 |
Prefix (λ a b → R (f a) (g b)) as bs |
| 104 |
map⁻ {[]} {bs} f g rs = [] |
| 105 |
map⁻ {a ∷ as} {b ∷ bs} f g (r ∷ rs) = r ∷ map⁻ f g rs |
| 106 |
|
| 107 |
------------------------------------------------------------------------ |
| 108 |
-- filter |
| 109 |
|
| 110 |
module _ {p q} {P : Pred A p} {Q : Pred B q} (P? : U.Decidable P) (Q? : U.Decidable Q) |
| 111 |
(P⇒Q : ∀ {a b} → R a b → P a → Q b) (Q⇒P : ∀ {a b} → R a b → Q b → P a) |
| 112 |
where |
| 113 |
|
| 114 |
filter⁺ : ∀ {as bs} → Prefix R as bs → Prefix R (filter P? as) (filter Q? bs) |
| 115 |
filter⁺ [] = [] |
| 116 |
filter⁺ {a ∷ as} {b ∷ bs} (r ∷ rs) with P? a | Q? b |
| 117 |
... | true because _ | true because _ = r ∷ filter⁺ rs |
| 118 |
... | yes pa | no ¬qb = contradiction (P⇒Q r pa) ¬qb |
| 119 |
... | no ¬pa | yes qb = contradiction (Q⇒P r qb) ¬pa |
| 120 |
... | false because _ | false because _ = filter⁺ rs |
| 121 |
|
| 122 |
------------------------------------------------------------------------ |
| 123 |
-- take |
| 124 |
|
| 125 |
take⁺ : ∀ {as bs} n → Prefix R as bs → |
| 126 |
Prefix R (take n as) (take n bs) |
| 127 |
take⁺ zero rs = [] |
| 128 |
take⁺ (suc n) [] = [] |
| 129 |
take⁺ (suc n) (r ∷ rs) = r ∷ take⁺ n rs |
| 130 |
|
| 131 |
take⁻ : ∀ {as bs} n → |
| 132 |
Prefix R (take n as) (take n bs) → |
| 133 |
Prefix R (drop n as) (drop n bs) → |
| 134 |
Prefix R as bs |
| 135 |
take⁻ zero hds tls = tls |
| 136 |
take⁻ {as = []} (suc n) hds tls = [] |
| 137 |
take⁻ {as = a ∷ as} {b ∷ bs} (suc n) (r ∷ hds) tls = r ∷ take⁻ n hds tls |
| 138 |
|
| 139 |
------------------------------------------------------------------------ |
| 140 |
-- drop |
| 141 |
|
| 142 |
drop⁺ : ∀ {as bs} n → Prefix R as bs → Prefix R (drop n as) (drop n bs) |
| 143 |
drop⁺ zero rs = rs |
| 144 |
drop⁺ (suc n) [] = [] |
| 145 |
drop⁺ (suc n) (r ∷ rs) = drop⁺ n rs |
| 146 |
|
| 147 |
drop⁻ : ∀ {as bs} n → Pointwise R (take n as) (take n bs) → |
| 148 |
Prefix R (drop n as) (drop n bs) → Prefix R as bs |
| 149 |
drop⁻ zero hds tls = tls |
| 150 |
drop⁻ {as = []} (suc n) hds tls = [] |
| 151 |
drop⁻ {as = _ ∷ _} {_ ∷ _} (suc n) (r ∷ hds) tls = r ∷ (drop⁻ n hds tls) |
| 152 |
|
| 153 |
------------------------------------------------------------------------ |
| 154 |
-- replicate |
| 155 |
|
| 156 |
replicate⁺ : ∀ {m n a b} → m ≤ n → R a b → |
| 157 |
Prefix R (replicate m a) (replicate n b) |
| 158 |
replicate⁺ z≤n r = [] |
| 159 |
replicate⁺ (s≤s m≤n) r = r ∷ replicate⁺ m≤n r |
| 160 |
|
| 161 |
replicate⁻ : ∀ {m n a b} → m ≢ 0 → |
| 162 |
Prefix R (replicate m a) (replicate n b) → R a b |
| 163 |
replicate⁻ {m = zero} {n} m≢0 r = contradiction refl m≢0 |
| 164 |
replicate⁻ {m = suc m} {suc n} m≢0 rs = Prefix.head rs |
| 165 |
|
| 166 |
------------------------------------------------------------------------ |
| 167 |
-- inits |
| 168 |
|
| 169 |
module _ {a r} {A : Set a} {R : Rel A r} where |
| 170 |
|
| 171 |
inits⁺ : ∀ {as} → Pointwise R as as → All (flip (Prefix R) as) (inits as) |
| 172 |
inits⁺ [] = [] ∷ [] |
| 173 |
inits⁺ (r ∷ rs) = [] ∷ All.map⁺ (All.map (r ∷_) (inits⁺ rs)) |
| 174 |
|
| 175 |
inits⁻ : ∀ {as} → All (flip (Prefix R) as) (inits as) → Pointwise R as as |
| 176 |
inits⁻ {as = []} rs = [] |
| 177 |
inits⁻ {as = a ∷ as} (r ∷ rs) = |
| 178 |
let (hd , tls) = All.unzip (All.map uncons (All.map⁻ rs)) in |
| 179 |
All.lookup hd ([]∈inits as) ∷ inits⁻ tls |
| 180 |
|
| 181 |
------------------------------------------------------------------------ |
| 182 |
-- zip(With) |
| 183 |
|
| 184 |
module _ {a b c} {A : Set a} {B : Set b} {C : Set c} |
| 185 |
{d e f} {D : Set d} {E : Set e} {F : Set f} |
| 186 |
{r s t} {R : REL A D r} {S : REL B E s} {T : REL C F t} where |
| 187 |
|
| 188 |
zipWith⁺ : ∀ {as bs ds es} {f : A → B → C} {g : D → E → F} → |
| 189 |
(∀ {a b c d} → R a c → S b d → T (f a b) (g c d)) → |
| 190 |
Prefix R as ds → Prefix S bs es → |
| 191 |
Prefix T (zipWith f as bs) (zipWith g ds es) |
| 192 |
zipWith⁺ f [] ss = [] |
| 193 |
zipWith⁺ f (r ∷ rs) [] = [] |
| 194 |
zipWith⁺ f (r ∷ rs) (s ∷ ss) = f r s ∷ zipWith⁺ f rs ss |
| 195 |
|
| 196 |
module _ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} |
| 197 |
{r s} {R : REL A C r} {S : REL B D s} where |
| 198 |
|
| 199 |
private |
| 200 |
R×S : REL (A × B) (C × D) _ |
| 201 |
R×S (a , b) (c , d) = R a c × S b d |
| 202 |
|
| 203 |
zip⁺ : ∀ {as bs cs ds} → Prefix R as cs → Prefix S bs ds → |
| 204 |
Prefix R×S (zip as bs) (zip cs ds) |
| 205 |
zip⁺ = zipWith⁺ _,_ |
| 206 |
|
| 207 |
------------------------------------------------------------------------ |
| 208 |
-- Irrelevance |
| 209 |
|
| 210 |
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where |
| 211 |
|
| 212 |
irrelevant : Irrelevant R → Irrelevant (Prefix R) |
| 213 |
irrelevant R-irr [] [] = refl |
| 214 |
irrelevant R-irr (r ∷ rs) (r′ ∷ rs′) = |
| 215 |
cong₂ _∷_ (R-irr r r′) (irrelevant R-irr rs rs′) |
| 216 |
|
| 217 |
------------------------------------------------------------------------ |
| 218 |
-- Decidability |
| 219 |
|
| 220 |
prefix? : Decidable R → Decidable (Prefix R) |
| 221 |
prefix? R? [] bs = yes [] |
| 222 |
prefix? R? (a ∷ as) [] = no (λ ()) |
| 223 |
prefix? R? (a ∷ as) (b ∷ bs) = Dec.map′ (uncurry _∷_) uncons |
| 224 |
$ R? a b ×-dec prefix? R? as bs |