| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Core properties related to propositional list membership. |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- This file is needed to break the cyclic dependency with the proof |
| 8 |
-- `Any-cong` in `Data.List.Relation.Unary.Any.Properties` which relies |
| 9 |
-- on `Any↔` defined in this file. |
| 10 |
|
| 11 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 12 |
|
| 13 |
module Data.List.Membership.Propositional.Properties.Core where |
| 14 |
|
| 15 |
open import Data.List.Base using (List; []) |
| 16 |
open import Data.List.Membership.Propositional using (_∈_; _∉_; find; lose) |
| 17 |
open import Data.List.Relation.Unary.Any as Any using (Any; here; there) |
| 18 |
open import Data.Product.Base as Product using (_,_; ∃; _×_) |
| 19 |
open import Function.Base using (flip; id; _∘_) |
| 20 |
open import Function.Bundles using (_↔_; mk↔ₛ′) |
| 21 |
open import Level using (Level) |
| 22 |
open import Relation.Binary.PropositionalEquality.Core |
| 23 |
using (_≡_; refl; trans; cong; resp) |
| 24 |
open import Relation.Unary using (Pred; _⊆_) |
| 25 |
|
| 26 |
private |
| 27 |
variable |
| 28 |
a p q : Level |
| 29 |
A : Set a |
| 30 |
x : A |
| 31 |
xs : List A |
| 32 |
|
| 33 |
------------------------------------------------------------------------ |
| 34 |
-- Basics |
| 35 |
|
| 36 |
∉[] : x ∉ [] |
| 37 |
∉[] () |
| 38 |
|
| 39 |
------------------------------------------------------------------------ |
| 40 |
-- find satisfies a simple equality when the predicate is a |
| 41 |
-- propositional equality. |
| 42 |
|
| 43 |
find-∈ : (x∈xs : x ∈ xs) → find x∈xs ≡ (x , x∈xs , refl) |
| 44 |
find-∈ (here refl) = refl |
| 45 |
find-∈ (there x∈xs) |
| 46 |
= cong (λ where (x , x∈xs , eq) → x , there x∈xs , eq) (find-∈ x∈xs) |
| 47 |
|
| 48 |
------------------------------------------------------------------------ |
| 49 |
-- Lemmas relating map and find. |
| 50 |
|
| 51 |
module _ {P : Pred A p} where |
| 52 |
|
| 53 |
map∘find : (p : Any P xs) → let x , x∈xs , px = find p in |
| 54 |
{f : (x ≡_) ⊆ P} → f refl ≡ px → |
| 55 |
Any.map f x∈xs ≡ p |
| 56 |
map∘find (here p) hyp = cong here hyp |
| 57 |
map∘find (there p) hyp = cong there (map∘find p hyp) |
| 58 |
|
| 59 |
find∘map : ∀ {Q : Pred A q} {xs} (p : Any P xs) (f : P ⊆ Q) → |
| 60 |
let x , x∈xs , px = find p in |
| 61 |
find (Any.map f p) ≡ (x , x∈xs , f px) |
| 62 |
find∘map (here p) f = refl |
| 63 |
find∘map (there p) f |
| 64 |
= cong (λ where (x , x∈xs , eq) → x , there x∈xs , eq) (find∘map p f) |
| 65 |
|
| 66 |
------------------------------------------------------------------------ |
| 67 |
-- Any can be expressed using _∈_ |
| 68 |
|
| 69 |
module _ {P : Pred A p} where |
| 70 |
|
| 71 |
∃∈-Any : (∃ λ x → x ∈ xs × P x) → Any P xs |
| 72 |
∃∈-Any (x , x∈xs , px) = lose x∈xs px |
| 73 |
|
| 74 |
∃∈-Any∘find : (p : Any P xs) → ∃∈-Any (find p) ≡ p |
| 75 |
∃∈-Any∘find p = map∘find p refl |
| 76 |
|
| 77 |
find∘∃∈-Any : (p : ∃ λ x → x ∈ xs × P x) → find (∃∈-Any p) ≡ p |
| 78 |
find∘∃∈-Any p@(x , x∈xs , px) |
| 79 |
= trans (find∘map x∈xs (flip (resp P) px)) |
| 80 |
(cong (λ (x , x∈xs , eq) → x , x∈xs , resp P eq px) (find-∈ x∈xs)) |
| 81 |
|
| 82 |
Any↔ : (∃ λ x → x ∈ xs × P x) ↔ Any P xs |
| 83 |
Any↔ = mk↔ₛ′ ∃∈-Any find ∃∈-Any∘find find∘∃∈-Any |
| 84 |
|
| 85 |
------------------------------------------------------------------------ |
| 86 |
-- Hence, find and lose are inverses (more or less). |
| 87 |
|
| 88 |
lose∘find : ∀ {P : Pred A p} {xs} (p : Any P xs) → ∃∈-Any (find p) ≡ p |
| 89 |
lose∘find = ∃∈-Any∘find |
| 90 |
|
| 91 |
find∘lose : ∀ (P : Pred A p) {x xs} |
| 92 |
(x∈xs : x ∈ xs) (px : P x) → |
| 93 |
find (lose {P = P} x∈xs px) ≡ (x , x∈xs , px) |
| 94 |
find∘lose P {x} x∈xs px = find∘∃∈-Any (x , x∈xs , px) |