| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of Any predicate transformer for fresh lists |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Fresh.Relation.Unary.Any.Properties where |
| 10 |
|
| 11 |
open import Data.Bool.Base using (true; false) |
| 12 |
open import Data.List.Fresh using (List#; _∷#_; _#_; NonEmpty; cons; length; []) |
| 13 |
open import Data.List.Fresh.Relation.Unary.All using (All; _∷_; append; []) |
| 14 |
open import Data.List.Fresh.Relation.Unary.Any using (Any; here; there; _─_) |
| 15 |
open import Data.Nat.Base using (ℕ; zero; suc) |
| 16 |
open import Data.Product.Base using (_,_) |
| 17 |
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂) |
| 18 |
open import Function.Base using (_∘′_) |
| 19 |
open import Level using (Level; _⊔_; Lift) |
| 20 |
open import Relation.Nullary.Reflects using (invert) |
| 21 |
open import Relation.Nullary.Decidable.Core |
| 22 |
open import Relation.Unary as U using (Pred) |
| 23 |
open import Relation.Binary.Core using (Rel) |
| 24 |
open import Relation.Nary using (∀[_]; _⇒_; ∁; Decidable) |
| 25 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong) |
| 26 |
open import Relation.Nullary.Negation.Core using (contradiction; ¬_) |
| 27 |
|
| 28 |
private |
| 29 |
variable |
| 30 |
a b p q r s : Level |
| 31 |
A : Set a |
| 32 |
B : Set b |
| 33 |
|
| 34 |
------------------------------------------------------------------------ |
| 35 |
-- NonEmpty |
| 36 |
|
| 37 |
module _ {R : Rel A r} {P : Pred A p} where |
| 38 |
|
| 39 |
Any⇒NonEmpty : {xs : List# A R} → Any P xs → NonEmpty xs |
| 40 |
Any⇒NonEmpty {xs = cons x xs pr} p = cons x xs pr |
| 41 |
|
| 42 |
------------------------------------------------------------------------ |
| 43 |
-- Correspondence between Any and All |
| 44 |
|
| 45 |
module _ {R : Rel A r} {P : Pred A p} {Q : Pred A q} (P⇒¬Q : ∀[ P ⇒ ∁ Q ]) where |
| 46 |
|
| 47 |
Any⇒¬All : {xs : List# A R} → Any P xs → ¬ (All Q xs) |
| 48 |
Any⇒¬All (here p) (q ∷ _) = P⇒¬Q p q |
| 49 |
Any⇒¬All (there ps) (_ ∷ qs) = Any⇒¬All ps qs |
| 50 |
|
| 51 |
All⇒¬Any : {xs : List# A R} → All P xs → ¬ (Any Q xs) |
| 52 |
All⇒¬Any (p ∷ _) (here q) = P⇒¬Q p q |
| 53 |
All⇒¬Any (_ ∷ ps) (there qs) = All⇒¬Any ps qs |
| 54 |
|
| 55 |
module _ {R : Rel A r} {P : Pred A p} {Q : Pred A q} (P? : Decidable P) where |
| 56 |
|
| 57 |
¬All⇒Any : {xs : List# A R} → ¬ (All P xs) → Any (∁ P) xs |
| 58 |
¬All⇒Any {xs = []} ¬ps = contradiction [] ¬ps |
| 59 |
¬All⇒Any {xs = x ∷# xs} ¬ps with P? x |
| 60 |
... | true because [p] = there (¬All⇒Any (¬ps ∘′ (invert [p] ∷_))) |
| 61 |
... | false because [¬p] = here (invert [¬p]) |
| 62 |
|
| 63 |
¬Any⇒All : {xs : List# A R} → ¬ (Any P xs) → All (∁ P) xs |
| 64 |
¬Any⇒All {xs = []} ¬ps = [] |
| 65 |
¬Any⇒All {xs = x ∷# xs} ¬ps with P? x |
| 66 |
... | true because [p] = contradiction (here (invert [p])) ¬ps |
| 67 |
... | false because [¬p] = invert [¬p] ∷ ¬Any⇒All (¬ps ∘′ there) |
| 68 |
|
| 69 |
------------------------------------------------------------------------ |
| 70 |
-- remove |
| 71 |
|
| 72 |
module _ {R : Rel A r} {P : Pred A p} where |
| 73 |
|
| 74 |
length-remove : {xs : List# A R} (k : Any P xs) → |
| 75 |
length xs ≡ suc (length (xs ─ k)) |
| 76 |
length-remove (here _) = refl |
| 77 |
length-remove (there p) = cong suc (length-remove p) |
| 78 |
|
| 79 |
------------------------------------------------------------------------ |
| 80 |
-- append |
| 81 |
|
| 82 |
module _ {R : Rel A r} {P : Pred A p} where |
| 83 |
|
| 84 |
append⁺ˡ : {xs ys : List# A R} {ps : All (_# ys) xs} → |
| 85 |
Any P xs → Any P (append xs ys ps) |
| 86 |
append⁺ˡ (here px) = here px |
| 87 |
append⁺ˡ (there p) = there (append⁺ˡ p) |
| 88 |
|
| 89 |
append⁺ʳ : {xs ys : List# A R} {ps : All (_# ys) xs} → |
| 90 |
Any P ys → Any P (append xs ys ps) |
| 91 |
append⁺ʳ {xs = []} p = p |
| 92 |
append⁺ʳ {xs = x ∷# xs} p = there (append⁺ʳ p) |
| 93 |
|
| 94 |
append⁻ : ∀ xs {ys : List# A R} {ps : All (_# ys) xs} → |
| 95 |
Any P (append xs ys ps) → Any P xs ⊎ Any P ys |
| 96 |
append⁻ [] p = inj₂ p |
| 97 |
append⁻ (x ∷# xs) (here px) = inj₁ (here px) |
| 98 |
append⁻ (x ∷# xs) (there p) = Sum.map₁ there (append⁻ xs p) |