|
| 1 | +------------------------------------------------------------------------ |
| 2 | +-- The Agda standard library |
| 3 | +-- |
| 4 | +-- Properties related to All |
| 5 | +------------------------------------------------------------------------ |
| 6 | + |
| 7 | +{-# OPTIONS --cubical-compatible --safe #-} |
| 8 | + |
| 9 | +module Data.List.Relation.Unary.All.Properties.Core where |
| 10 | + |
| 11 | +open import Axiom.Extensionality.Propositional using (Extensionality) |
| 12 | +open import Data.Bool.Base using (true; false) |
| 13 | +open import Data.List.Base using (List; []; _∷_) |
| 14 | +open import Data.List.Relation.Unary.All as All using (All; []; _∷_) |
| 15 | +open import Data.List.Relation.Unary.Any as Any using (Any; here; there) |
| 16 | +open import Data.Product.Base as Product using (_,_) |
| 17 | +open import Function.Base using (_∘_; _$_) |
| 18 | +open import Function.Bundles using (_↠_; mk↠ₛ; _⇔_; mk⇔) |
| 19 | +open import Level using (Level) |
| 20 | +open import Relation.Binary.Core using (REL) |
| 21 | +open import Relation.Binary.PropositionalEquality.Core |
| 22 | + using (_≡_; refl; cong; cong₂) |
| 23 | +open import Relation.Nullary.Reflects using (invert) |
| 24 | +open import Relation.Nullary.Negation.Core using (¬_; contradiction) |
| 25 | +open import Relation.Nullary.Decidable.Core using (_because_) |
| 26 | +open import Relation.Unary using (Decidable; Pred; Universal) |
| 27 | + |
| 28 | +private |
| 29 | + variable |
| 30 | + a b p ℓ : Level |
| 31 | + A : Set a |
| 32 | + B : Set b |
| 33 | + P : Pred A p |
| 34 | + x y : A |
| 35 | + xs ys : List A |
| 36 | + |
| 37 | +------------------------------------------------------------------------ |
| 38 | +-- Lemmas relating Any, All and negation. |
| 39 | + |
| 40 | +¬Any⇒All¬ : ∀ xs → ¬ Any P xs → All (¬_ ∘ P) xs |
| 41 | +¬Any⇒All¬ [] ¬p = [] |
| 42 | +¬Any⇒All¬ (x ∷ xs) ¬p = ¬p ∘ here ∷ ¬Any⇒All¬ xs (¬p ∘ there) |
| 43 | + |
| 44 | +All¬⇒¬Any : ∀ {xs} → All (¬_ ∘ P) xs → ¬ Any P xs |
| 45 | +All¬⇒¬Any (¬p ∷ _) (here p) = ¬p p |
| 46 | +All¬⇒¬Any (_ ∷ ¬p) (there p) = All¬⇒¬Any ¬p p |
| 47 | + |
| 48 | +¬All⇒Any¬ : Decidable P → ∀ xs → ¬ All P xs → Any (¬_ ∘ P) xs |
| 49 | +¬All⇒Any¬ dec [] ¬∀ = contradiction [] ¬∀ |
| 50 | +¬All⇒Any¬ dec (x ∷ xs) ¬∀ with dec x |
| 51 | +... | true because [p] = there (¬All⇒Any¬ dec xs (¬∀ ∘ _∷_ (invert [p]))) |
| 52 | +... | false because [¬p] = here (invert [¬p]) |
| 53 | + |
| 54 | +Any¬⇒¬All : ∀ {xs} → Any (¬_ ∘ P) xs → ¬ All P xs |
| 55 | +Any¬⇒¬All (here ¬p) = ¬p ∘ All.head |
| 56 | +Any¬⇒¬All (there ¬p) = Any¬⇒¬All ¬p ∘ All.tail |
| 57 | + |
| 58 | +¬Any↠All¬ : ∀ {xs} → (¬ Any P xs) ↠ All (¬_ ∘ P) xs |
| 59 | +¬Any↠All¬ = mk↠ₛ {to = ¬Any⇒All¬ _} (λ y → All¬⇒¬Any y , to∘from y) |
| 60 | + where |
| 61 | + to∘from : ∀ {xs} (¬p : All (¬_ ∘ P) xs) → ¬Any⇒All¬ xs (All¬⇒¬Any ¬p) ≡ ¬p |
| 62 | + to∘from [] = refl |
| 63 | + to∘from (¬p ∷ ¬ps) = cong₂ _∷_ refl (to∘from ¬ps) |
| 64 | + |
| 65 | + -- If equality of functions were extensional, then the surjection |
| 66 | + -- could be strengthened to a bijection. |
| 67 | + |
| 68 | + from∘to : Extensionality _ _ → |
| 69 | + ∀ xs → (¬p : ¬ Any P xs) → All¬⇒¬Any (¬Any⇒All¬ xs ¬p) ≡ ¬p |
| 70 | + from∘to ext [] ¬p = ext λ () |
| 71 | + from∘to ext (x ∷ xs) ¬p = ext λ |
| 72 | + { (here p) → refl |
| 73 | + ; (there p) → cong (λ f → f p) $ from∘to ext xs (¬p ∘ there) |
| 74 | + } |
| 75 | + |
| 76 | +Any¬⇔¬All : ∀ {xs} → Decidable P → Any (¬_ ∘ P) xs ⇔ (¬ All P xs) |
| 77 | +Any¬⇔¬All dec = mk⇔ Any¬⇒¬All (¬All⇒Any¬ dec _) |
| 78 | + |
| 79 | +private |
| 80 | + -- If equality of functions were extensional, then the logical |
| 81 | + -- equivalence could be strengthened to a surjection. |
| 82 | + to∘from : Extensionality _ _ → (dec : Decidable P) → |
| 83 | + (¬∀ : ¬ All P xs) → Any¬⇒¬All (¬All⇒Any¬ dec xs ¬∀) ≡ ¬∀ |
| 84 | + to∘from ext P ¬∀ = ext λ ∀P → contradiction ∀P ¬∀ |
| 85 | + |
| 86 | +module _ {_~_ : REL A B ℓ} where |
| 87 | + |
| 88 | + All-swap : ∀ {xs ys} → |
| 89 | + All (λ x → All (x ~_) ys) xs → |
| 90 | + All (λ y → All (_~ y) xs) ys |
| 91 | + All-swap {ys = []} _ = [] |
| 92 | + All-swap {ys = y ∷ ys} [] = All.universal (λ _ → []) (y ∷ ys) |
| 93 | + All-swap {ys = y ∷ ys} ((x~y ∷ x~ys) ∷ pxs) = |
| 94 | + (x~y ∷ (All.map All.head pxs)) ∷ |
| 95 | + All-swap (x~ys ∷ (All.map All.tail pxs)) |
| 96 | + |
0 commit comments