|
| 1 | +----------------------------------------------------------------------- |
| 2 | +-- The Agda standard library |
| 3 | +-- |
| 4 | +-- Properties of the heterogeneous sublist relation |
| 5 | +------------------------------------------------------------------------ |
| 6 | + |
| 7 | +{-# OPTIONS --without-K --safe #-} |
| 8 | + |
| 9 | +module Data.List.Relation.Sublist.Heterogeneous.Properties where |
| 10 | + |
| 11 | +open import Data.Empty |
| 12 | +open import Data.List.Any using (Any; here; there) |
| 13 | +open import Data.List.Base as List using (List; []; _∷_; _++_; [_]; length) |
| 14 | +open import Data.List.Relation.Pointwise using (Pointwise; []; _∷_) |
| 15 | +open import Data.List.Relation.Sublist.Heterogeneous |
| 16 | +open import Data.Nat using (ℕ; _≤_); open ℕ; open _≤_ |
| 17 | +open import Data.Nat.Properties |
| 18 | + using (suc-injective; ≤-step; n≤1+n; <-irrefl; module ≤-Reasoning) |
| 19 | +open import Function |
| 20 | +open import Relation.Binary |
| 21 | +open import Relation.Binary.PropositionalEquality as P using (_≡_) |
| 22 | + |
| 23 | +module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where |
| 24 | + |
| 25 | + fromAny : ∀ {a bs} → Any (R a) bs → Sublist R [ a ] bs |
| 26 | + fromAny (here r) = r ∷ minimum _ |
| 27 | + fromAny (there p) = _ ∷ʳ fromAny p |
| 28 | + |
| 29 | + toAny : ∀ {a bs} → Sublist R [ a ] bs → Any (R a) bs |
| 30 | + toAny (y ∷ʳ rs) = there (toAny rs) |
| 31 | + toAny (r ∷ rs) = here r |
| 32 | + |
| 33 | + length-mono-Sublist-≤ : ∀ {as bs} → Sublist R as bs → length as ≤ length bs |
| 34 | + length-mono-Sublist-≤ [] = z≤n |
| 35 | + length-mono-Sublist-≤ (y ∷ʳ rs) = ≤-step (length-mono-Sublist-≤ rs) |
| 36 | + length-mono-Sublist-≤ (r ∷ rs) = s≤s (length-mono-Sublist-≤ rs) |
| 37 | + |
| 38 | + fromPointwise : Pointwise R ⇒ Sublist R |
| 39 | + fromPointwise [] = [] |
| 40 | + fromPointwise (r ∷ rs) = r ∷ fromPointwise rs |
| 41 | + |
| 42 | + toPointwise : ∀ {as bs} → length as ≡ length bs → |
| 43 | + Sublist R as bs → Pointwise R as bs |
| 44 | + toPointwise {bs = []} eq [] = [] |
| 45 | + toPointwise {bs = b ∷ bs} eq (r ∷ rs) = r ∷ toPointwise (suc-injective eq) rs |
| 46 | + toPointwise {bs = b ∷ bs} eq (.b ∷ʳ rs) = |
| 47 | + ⊥-elim $ <-irrefl eq (s≤s (length-mono-Sublist-≤ rs)) |
| 48 | + |
| 49 | +module _ {a b c r s t} {A : Set a} {B : Set b} {C : Set c} |
| 50 | + {R : REL A B r} {S : REL B C s} {T : REL A C t} where |
| 51 | + |
| 52 | + trans : Trans R S T → Trans (Sublist R) (Sublist S) (Sublist T) |
| 53 | + trans rs⇒t [] [] = [] |
| 54 | + trans rs⇒t rs (y ∷ʳ ss) = y ∷ʳ trans rs⇒t rs ss |
| 55 | + trans rs⇒t (y ∷ʳ rs) (s ∷ ss) = _ ∷ʳ trans rs⇒t rs ss |
| 56 | + trans rs⇒t (r ∷ rs) (s ∷ ss) = rs⇒t r s ∷ trans rs⇒t rs ss |
| 57 | + |
| 58 | +module _ {a b r s e} {A : Set a} {B : Set b} |
| 59 | + {R : REL A B r} {S : REL B A s} {E : REL A B e} where |
| 60 | + |
| 61 | + antisym : Antisym R S E → Antisym (Sublist R) (Sublist S) (Pointwise E) |
| 62 | + antisym rs⇒e [] [] = [] |
| 63 | + antisym rs⇒e (r ∷ rs) (s ∷ ss) = rs⇒e r s ∷ antisym rs⇒e rs ss |
| 64 | + -- impossible cases |
| 65 | + antisym rs⇒e (_∷ʳ_ {xs} {ys₁} y rs) (_∷ʳ_ {ys₂} {zs} z ss) = |
| 66 | + ⊥-elim $ <-irrefl P.refl $ begin |
| 67 | + length (y ∷ ys₁) ≤⟨ length-mono-Sublist-≤ ss ⟩ |
| 68 | + length zs ≤⟨ n≤1+n (length zs) ⟩ |
| 69 | + length (z ∷ zs) ≤⟨ length-mono-Sublist-≤ rs ⟩ |
| 70 | + length ys₁ ∎ where open ≤-Reasoning |
| 71 | + antisym rs⇒e (_∷ʳ_ {xs} {ys₁} y rs) (_∷_ {y} {ys₂} {z} {zs} s ss) = |
| 72 | + ⊥-elim $ <-irrefl P.refl $ begin |
| 73 | + length (z ∷ zs) ≤⟨ length-mono-Sublist-≤ rs ⟩ |
| 74 | + length ys₁ ≤⟨ length-mono-Sublist-≤ ss ⟩ |
| 75 | + length zs ∎ where open ≤-Reasoning |
| 76 | + antisym rs⇒e (_∷_ {x} {xs} {y} {ys₁} r rs) (_∷ʳ_ {ys₂} {zs} z ss) = |
| 77 | + ⊥-elim $ <-irrefl P.refl $ begin |
| 78 | + length (y ∷ ys₁) ≤⟨ length-mono-Sublist-≤ ss ⟩ |
| 79 | + length xs ≤⟨ length-mono-Sublist-≤ rs ⟩ |
| 80 | + length ys₁ ∎ where open ≤-Reasoning |
| 81 | + |
| 82 | +------------------------------------------------------------------------ |
| 83 | +-- _++_ |
| 84 | + |
| 85 | +module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where |
| 86 | + |
| 87 | + ++⁺ : ∀ {as bs cs ds} → Sublist R as bs → Sublist R cs ds → |
| 88 | + Sublist R (as ++ cs) (bs ++ ds) |
| 89 | + ++⁺ [] cds = cds |
| 90 | + ++⁺ (y ∷ʳ abs) cds = y ∷ʳ ++⁺ abs cds |
| 91 | + ++⁺ (ab ∷ abs) cds = ab ∷ ++⁺ abs cds |
| 92 | + |
| 93 | +------------------------------------------------------------------------ |
| 94 | +-- map |
| 95 | + |
| 96 | +module _ {a b c d r} {A : Set a} {B : Set b} {C : Set c} {D : Set d} |
| 97 | + {R : REL C D r} where |
| 98 | + |
| 99 | + map⁺ : ∀ {as bs} (f : A → C) (g : B → D) → |
| 100 | + Sublist (λ a b → R (f a) (g b)) as bs → |
| 101 | + Sublist R (List.map f as) (List.map g bs) |
| 102 | + map⁺ f g [] = [] |
| 103 | + map⁺ f g (y ∷ʳ rs) = g y ∷ʳ map⁺ f g rs |
| 104 | + map⁺ f g (r ∷ rs) = r ∷ map⁺ f g rs |
| 105 | + |
| 106 | + map⁻ : ∀ {as bs} (f : A → C) (g : B → D) → |
| 107 | + Sublist R (List.map f as) (List.map g bs) → |
| 108 | + Sublist (λ a b → R (f a) (g b)) as bs |
| 109 | + map⁻ {[]} {bs} f g rs = minimum _ |
| 110 | + map⁻ {a ∷ as} {[]} f g () |
| 111 | + map⁻ {a ∷ as} {b ∷ bs} f g (_ ∷ʳ rs) = b ∷ʳ map⁻ f g rs |
| 112 | + map⁻ {a ∷ as} {b ∷ bs} f g (r ∷ rs) = r ∷ map⁻ f g rs |
0 commit comments