|
| 1 | +------------------------------------------------------------------------ |
| 2 | +-- The Agda standard library |
| 3 | +-- |
| 4 | +-- Properties of the heterogeneous infix relation |
| 5 | +------------------------------------------------------------------------ |
| 6 | + |
| 7 | +{-# OPTIONS --without-K --safe #-} |
| 8 | + |
| 9 | +module Data.List.Relation.Binary.Infix.Heterogeneous.Properties where |
| 10 | + |
| 11 | +open import Level |
| 12 | +open import Data.Bool.Base using (true; false) |
| 13 | +open import Data.Empty using (⊥-elim) |
| 14 | +open import Data.List.Base as List using (List; []; _∷_; length; map; filter; replicate) |
| 15 | +open import Data.Nat.Base using (zero; suc; _≤_; s≤s) |
| 16 | +import Data.Nat.Properties as ℕₚ |
| 17 | +open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_]′) |
| 18 | +open import Function.Base using (case_of_; _$′_) |
| 19 | + |
| 20 | +open import Relation.Nullary using (¬_; yes; no; does) |
| 21 | +open import Relation.Nullary.Decidable using (map′) |
| 22 | +open import Relation.Nullary.Negation using (contradiction) |
| 23 | +open import Relation.Nullary.Sum using (_⊎-dec_) |
| 24 | +open import Relation.Unary as U using (Pred) |
| 25 | +open import Relation.Binary using (REL; _⇒_; Decidable; Trans; Antisym) |
| 26 | +open import Relation.Binary.PropositionalEquality using (_≢_; refl; cong) |
| 27 | + |
| 28 | +open import Data.List.Relation.Binary.Pointwise as Pointwise using (Pointwise) |
| 29 | +open import Data.List.Relation.Binary.Infix.Heterogeneous |
| 30 | +open import Data.List.Relation.Binary.Prefix.Heterogeneous |
| 31 | + as Prefix using (Prefix; []; _∷_) |
| 32 | +import Data.List.Relation.Binary.Prefix.Heterogeneous.Properties as Prefixₚ |
| 33 | +open import Data.List.Relation.Binary.Suffix.Heterogeneous |
| 34 | + as Suffix using (Suffix; here; there) |
| 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) = ℕₚ.≤-step (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 | + = ⊥-elim $′ ℕₚ.<-irrefl refl $′ begin-strict |
| 104 | + length as <⟨ length-mono p ⟩ |
| 105 | + length bs ≤⟨ length-mono q ⟩ |
| 106 | + length as ∎ where open ℕₚ.≤-Reasoning |
| 107 | + antisym asym {i = as} {j = b ∷ bs} (there p) q@(here _) |
| 108 | + = ⊥-elim $′ ℕₚ.<-irrefl refl $′ begin-strict |
| 109 | + length bs <⟨ length-mono q ⟩ |
| 110 | + length as ≤⟨ length-mono p ⟩ |
| 111 | + length bs ∎ where open ℕₚ.≤-Reasoning |
| 112 | + antisym asym {i = a ∷ as} {j = b ∷ bs} (there p) (there q) |
| 113 | + = ⊥-elim $′ ℕₚ.<-irrefl refl $′ begin-strict |
| 114 | + length as <⟨ length-mono p ⟩ |
| 115 | + length bs <⟨ length-mono q ⟩ |
| 116 | + length as ∎ 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) |
0 commit comments