| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties related to AllPairs |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Relation.Unary.AllPairs.Properties where |
| 10 |
|
| 11 |
open import Data.List.Base using (List; []; _∷_; map; _++_; concat; take; drop; |
| 12 |
applyUpTo; applyDownFrom; tabulate; filter; catMaybes) |
| 13 |
open import Data.List.Relation.Unary.All as All using (All; []; _∷_) |
| 14 |
open import Data.List.Relation.Unary.All.Properties as All using (Any-catMaybes⁺) |
| 15 |
open import Data.List.Relation.Unary.AllPairs as AllPairs using (AllPairs; []; _∷_) |
| 16 |
open import Data.Bool.Base using (true; false) |
| 17 |
open import Data.Maybe.Base using (Maybe; nothing; just) |
| 18 |
open import Data.Maybe.Relation.Binary.Pointwise using (pointwise⊆any; Pointwise) |
| 19 |
open import Data.Fin.Base as F using (Fin) |
| 20 |
open import Data.Fin.Properties using (suc-injective; <⇒≢) |
| 21 |
open import Data.Nat.Base using (zero; suc; _<_; z≤n; s≤s; z<s; s<s) |
| 22 |
open import Data.Nat.Properties using (≤-refl; m<n⇒m<1+n) |
| 23 |
open import Function.Base using (_∘_; flip; _on_) |
| 24 |
open import Level using (Level) |
| 25 |
open import Relation.Binary.Core using (Rel) |
| 26 |
open import Relation.Binary.Bundles using (DecSetoid) |
| 27 |
open import Relation.Binary.PropositionalEquality.Core using (_≢_) |
| 28 |
open import Relation.Unary using (Pred; Decidable; _⊆_) |
| 29 |
open import Relation.Nullary.Decidable.Core using (does) |
| 30 |
|
| 31 |
private |
| 32 |
variable |
| 33 |
a b c p ℓ : Level |
| 34 |
A : Set a |
| 35 |
B : Set b |
| 36 |
C : Set c |
| 37 |
|
| 38 |
------------------------------------------------------------------------ |
| 39 |
-- Introduction (⁺) and elimination (⁻) rules for list operations |
| 40 |
------------------------------------------------------------------------ |
| 41 |
-- map |
| 42 |
|
| 43 |
module _ {R : Rel A ℓ} {f : B → A} where |
| 44 |
|
| 45 |
map⁺ : ∀ {xs} → AllPairs (R on f) xs → AllPairs R (map f xs) |
| 46 |
map⁺ [] = [] |
| 47 |
map⁺ (x∉xs ∷ xs!) = All.map⁺ x∉xs ∷ map⁺ xs! |
| 48 |
|
| 49 |
map⁻ : ∀ {xs} → AllPairs R (map f xs) → AllPairs (R on f) xs |
| 50 |
map⁻ {[]} _ = [] |
| 51 |
map⁻ {_ ∷ _} (fx∉fxs ∷ fxs!) = All.map⁻ fx∉fxs ∷ map⁻ fxs! |
| 52 |
|
| 53 |
------------------------------------------------------------------------ |
| 54 |
-- ++ |
| 55 |
|
| 56 |
module _ {R : Rel A ℓ} where |
| 57 |
|
| 58 |
++⁺ : ∀ {xs ys} → AllPairs R xs → AllPairs R ys → |
| 59 |
All (λ x → All (R x) ys) xs → AllPairs R (xs ++ ys) |
| 60 |
++⁺ [] Rys _ = Rys |
| 61 |
++⁺ (px ∷ Rxs) Rys (Rxys ∷ Rxsys) = All.++⁺ px Rxys ∷ ++⁺ Rxs Rys Rxsys |
| 62 |
|
| 63 |
------------------------------------------------------------------------ |
| 64 |
-- concat |
| 65 |
|
| 66 |
module _ {R : Rel A ℓ} where |
| 67 |
|
| 68 |
concat⁺ : ∀ {xss} → All (AllPairs R) xss → |
| 69 |
AllPairs (λ xs ys → All (λ x → All (R x) ys) xs) xss → |
| 70 |
AllPairs R (concat xss) |
| 71 |
concat⁺ [] [] = [] |
| 72 |
concat⁺ (pxs ∷ pxss) (Rxsxss ∷ Rxss) = ++⁺ pxs (concat⁺ pxss Rxss) |
| 73 |
(All.map All.concat⁺ (All.All-swap Rxsxss)) |
| 74 |
|
| 75 |
------------------------------------------------------------------------ |
| 76 |
-- take and drop |
| 77 |
|
| 78 |
module _ {R : Rel A ℓ} where |
| 79 |
|
| 80 |
take⁺ : ∀ {xs} n → AllPairs R xs → AllPairs R (take n xs) |
| 81 |
take⁺ zero pxs = [] |
| 82 |
take⁺ (suc n) [] = [] |
| 83 |
take⁺ (suc n) (px ∷ pxs) = All.take⁺ n px ∷ take⁺ n pxs |
| 84 |
|
| 85 |
drop⁺ : ∀ {xs} n → AllPairs R xs → AllPairs R (drop n xs) |
| 86 |
drop⁺ zero pxs = pxs |
| 87 |
drop⁺ (suc n) [] = [] |
| 88 |
drop⁺ (suc n) (_ ∷ pxs) = drop⁺ n pxs |
| 89 |
|
| 90 |
------------------------------------------------------------------------ |
| 91 |
-- applyUpTo |
| 92 |
|
| 93 |
module _ {R : Rel A ℓ} where |
| 94 |
|
| 95 |
applyUpTo⁺₁ : ∀ f n → (∀ {i j} → i < j → j < n → R (f i) (f j)) → AllPairs R (applyUpTo f n) |
| 96 |
applyUpTo⁺₁ f zero Rf = [] |
| 97 |
applyUpTo⁺₁ f (suc n) Rf = |
| 98 |
All.applyUpTo⁺₁ _ n (Rf (s≤s z≤n) ∘ s≤s) ∷ |
| 99 |
applyUpTo⁺₁ _ n (λ i≤j j<n → Rf (s≤s i≤j) (s≤s j<n)) |
| 100 |
|
| 101 |
applyUpTo⁺₂ : ∀ f n → (∀ i j → R (f i) (f j)) → AllPairs R (applyUpTo f n) |
| 102 |
applyUpTo⁺₂ f n Rf = applyUpTo⁺₁ f n (λ _ _ → Rf _ _) |
| 103 |
|
| 104 |
------------------------------------------------------------------------ |
| 105 |
-- applyDownFrom |
| 106 |
|
| 107 |
module _ {R : Rel A ℓ} where |
| 108 |
|
| 109 |
applyDownFrom⁺₁ : ∀ f n → (∀ {i j} → j < i → i < n → R (f i) (f j)) → |
| 110 |
AllPairs R (applyDownFrom f n) |
| 111 |
applyDownFrom⁺₁ f zero Rf = [] |
| 112 |
applyDownFrom⁺₁ f (suc n) Rf = |
| 113 |
All.applyDownFrom⁺₁ _ n (flip Rf ≤-refl) ∷ |
| 114 |
applyDownFrom⁺₁ f n (λ j<i i<n → Rf j<i (m<n⇒m<1+n i<n)) |
| 115 |
|
| 116 |
applyDownFrom⁺₂ : ∀ f n → (∀ i j → R (f i) (f j)) → AllPairs R (applyDownFrom f n) |
| 117 |
applyDownFrom⁺₂ f n Rf = applyDownFrom⁺₁ f n (λ _ _ → Rf _ _) |
| 118 |
|
| 119 |
------------------------------------------------------------------------ |
| 120 |
-- tabulate |
| 121 |
|
| 122 |
module _ {R : Rel A ℓ} where |
| 123 |
|
| 124 |
tabulate⁺-< : ∀ {n} {f : Fin n → A} → (∀ {i j} → i F.< j → R (f i) (f j)) → |
| 125 |
AllPairs R (tabulate f) |
| 126 |
tabulate⁺-< {zero} fᵢ~fⱼ = [] |
| 127 |
tabulate⁺-< {suc n} fᵢ~fⱼ = |
| 128 |
All.tabulate⁺ (λ _ → fᵢ~fⱼ z<s) ∷ |
| 129 |
tabulate⁺-< (fᵢ~fⱼ ∘ s<s) |
| 130 |
|
| 131 |
tabulate⁺ : ∀ {n} {f : Fin n → A} → (∀ {i j} → i ≢ j → R (f i) (f j)) → |
| 132 |
AllPairs R (tabulate f) |
| 133 |
tabulate⁺ fᵢ~fⱼ = tabulate⁺-< (fᵢ~fⱼ ∘ <⇒≢) |
| 134 |
|
| 135 |
------------------------------------------------------------------------ |
| 136 |
-- filter |
| 137 |
|
| 138 |
module _ {R : Rel A ℓ} {P : Pred A p} (P? : Decidable P) where |
| 139 |
|
| 140 |
filter⁺ : ∀ {xs} → AllPairs R xs → AllPairs R (filter P? xs) |
| 141 |
filter⁺ {_} [] = [] |
| 142 |
filter⁺ {x ∷ xs} (x∉xs ∷ xs!) with does (P? x) |
| 143 |
... | false = filter⁺ xs! |
| 144 |
... | true = All.filter⁺ P? x∉xs ∷ filter⁺ xs! |
| 145 |
|
| 146 |
------------------------------------------------------------------------ |
| 147 |
-- catMaybes |
| 148 |
|
| 149 |
module _ {R : Rel A ℓ} where |
| 150 |
|
| 151 |
catMaybes⁺ : {xs : List (Maybe A)} → AllPairs (Pointwise R) xs → AllPairs R (catMaybes xs) |
| 152 |
catMaybes⁺ {xs = []} [] = [] |
| 153 |
catMaybes⁺ {xs = nothing ∷ _} (x∼xs ∷ pxs) = catMaybes⁺ pxs |
| 154 |
catMaybes⁺ {xs = just x ∷ xs} (x∼xs ∷ pxs) = Any-catMaybes⁺ (All.map pointwise⊆any x∼xs) ∷ catMaybes⁺ pxs |