| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- List membership and some related definitions |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Relation.Binary.Bundles using (Setoid) |
| 10 |
|
| 11 |
module Data.List.Membership.Setoid {c ℓ} (S : Setoid c ℓ) where |
| 12 |
|
| 13 |
open import Data.List.Base using (List; []; _∷_) |
| 14 |
open import Data.List.Relation.Unary.Any as Any |
| 15 |
using (Any; map; here; there) |
| 16 |
open import Data.Product.Base as Product using (∃; _×_; _,_) |
| 17 |
open import Function.Base using (_∘_; flip; const) |
| 18 |
open import Relation.Binary.Definitions using (_Respects_) |
| 19 |
open import Relation.Nullary.Negation using (¬_) |
| 20 |
open import Relation.Unary using (Pred) |
| 21 |
|
| 22 |
open Setoid S renaming (Carrier to A) |
| 23 |
|
| 24 |
------------------------------------------------------------------------ |
| 25 |
-- Definitions |
| 26 |
|
| 27 |
infix 4 _∈_ _∉_ |
| 28 |
|
| 29 |
_∈_ : A → List A → Set _ |
| 30 |
x ∈ xs = Any (x ≈_) xs |
| 31 |
|
| 32 |
_∉_ : A → List A → Set _ |
| 33 |
x ∉ xs = ¬ x ∈ xs |
| 34 |
|
| 35 |
------------------------------------------------------------------------ |
| 36 |
-- Operations |
| 37 |
|
| 38 |
_∷=_ = Any._∷=_ {A = A} |
| 39 |
_─_ = Any._─_ {A = A} |
| 40 |
|
| 41 |
mapWith∈ : ∀ {b} {B : Set b} |
| 42 |
(xs : List A) → (∀ {x} → x ∈ xs → B) → List B |
| 43 |
mapWith∈ [] f = [] |
| 44 |
mapWith∈ (x ∷ xs) f = f (here refl) ∷ mapWith∈ xs (f ∘ there) |
| 45 |
|
| 46 |
------------------------------------------------------------------------ |
| 47 |
-- Finding and losing witnesses |
| 48 |
|
| 49 |
module _ {p} {P : Pred A p} where |
| 50 |
|
| 51 |
find : ∀ {xs} → Any P xs → ∃ λ x → x ∈ xs × P x |
| 52 |
find (here px) = _ , here refl , px |
| 53 |
find (there pxs) = let x , x∈xs , px = find pxs in x , there x∈xs , px |
| 54 |
|
| 55 |
lose : P Respects _≈_ → ∀ {x xs} → x ∈ xs → P x → Any P xs |
| 56 |
lose resp x∈xs px = map (flip resp px) x∈xs |