Debian Codegraph @

src/Data/Vec/Membership/Setoid.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Membership of vectors, along with some additional definitions.
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 open import Relation.Binary.Bundles using (Setoid)
10 open import Relation.Binary.Definitions using (_Respects_)
11
12 module Data.Vec.Membership.Setoid {c ℓ} (S : Setoid c ℓ) where
13
14 open import Function.Base using (_∘_; flip)
15 open import Data.Vec.Base as Vec using (Vec; []; _∷_)
16 open import Data.Vec.Relation.Unary.Any as Any
17 using (Any; here; there; index)
18 open import Data.Product.Base using (∃; _×_; _,_)
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 → ∀ {n} → Vec A n → Set _
30 x ∈ xs = Any (x ≈_) xs
31
32 _∉_ : A → ∀ {n} → Vec A n → Set _
33 x ∉ xs = ¬ x ∈ xs
34
35 ------------------------------------------------------------------------
36 -- Operations
37
38 mapWith∈ : ∀ {b} {B : Set b} {n}
39 (xs : Vec A n) → (∀ {x} → x ∈ xs → B) → Vec B n
40 mapWith∈ [] f = []
41 mapWith∈ (x ∷ xs) f = f (here refl) ∷ mapWith∈ xs (f ∘ there)
42
43 infixr 5 _∷=_
44
45 _∷=_ : ∀ {n} {xs : Vec A n} {x} → x ∈ xs → A → Vec A n
46 _∷=_ {xs = xs} x∈xs v = xs Vec.[ index x∈xs ]≔ v
47
48 ------------------------------------------------------------------------
49 -- Finding and losing witnesses
50
51 module _ {p} {P : Pred A p} where
52
53 find : ∀ {n} {xs : Vec A n} → Any P xs → ∃ λ x → x ∈ xs × P x
54 find (here px) = _ , here refl , px
55 find (there pxs) = let x , x∈xs , px = find pxs in x , there x∈xs , px
56
57 lose : P Respects _≈_ → ∀ {x n} {xs : Vec A n} → x ∈ xs → P x → Any P xs
58 lose resp x∈xs px = Any.map (flip resp px) x∈xs