Debian Codegraph @

src/Data/Vec/Relation/Unary/Any.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Vectors where at least one element satisfies a given property
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Vec.Relation.Unary.Any {a} {A : Set a} where
10
11 open import Data.Fin.Base using (Fin; zero; suc)
12 open import Data.Nat.Base using (ℕ; zero; suc; NonZero)
13 open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_]′)
14 open import Data.Vec.Base as Vec using (Vec; []; [_]; _∷_)
15 open import Data.Product.Base as Product using (∃; _,_)
16 open import Level using (Level; _⊔_)
17 open import Relation.Nullary.Negation using (¬_; contradiction)
18 open import Relation.Nullary.Decidable as Dec using (no; _⊎-dec_)
19 open import Relation.Unary
20
21 private
22 variable
23 p q : Level
24 P : Pred A p
25 Q : Pred A q
26 n : ℕ
27 xs : Vec A n
28
29 ------------------------------------------------------------------------
30 -- Any P xs means that at least one element in xs satisfies P.
31
32 data Any (P : Pred A p) : ∀ {n} → Vec A n → Set (a ⊔ p) where
33 here : ∀ {n x} {xs : Vec A n} (px : P x) → Any P (x ∷ xs)
34 there : ∀ {n x} {xs : Vec A n} (pxs : Any P xs) → Any P (x ∷ xs)
35
36 ------------------------------------------------------------------------
37 -- Operations on Any
38
39 -- If the tail does not satisfy the predicate, then the head will.
40 head : ∀ {x} → ¬ Any P xs → Any P (x ∷ xs) → P x
41 head ¬pxs (here px) = px
42 head ¬pxs (there pxs) = contradiction pxs ¬pxs
43
44 -- If the head does not satisfy the predicate, then the tail will.
45 tail : ∀ {x} → ¬ P x → Any P (x ∷ xs) → Any P xs
46 tail ¬px (here px) = contradiction px ¬px
47 tail ¬px (there pxs) = pxs
48
49 -- Convert back and forth with sum
50 toSum : ∀ {x} → Any P (x ∷ xs) → P x ⊎ Any P xs
51 toSum (here px) = inj₁ px
52 toSum (there pxs) = inj₂ pxs
53
54 fromSum : ∀ {x} → P x ⊎ Any P xs → Any P (x ∷ xs)
55 fromSum = [ here , there ]′
56
57 map : P ⊆ Q → ∀ {n} → Any P {n} ⊆ Any Q {n}
58 map g (here px) = here (g px)
59 map g (there pxs) = there (map g pxs)
60
61 index : Any P {n} xs → Fin n
62 index (here px) = zero
63 index (there pxs) = suc (index pxs)
64
65 lookup : Any P xs → A
66 lookup {xs = xs} p = Vec.lookup xs (index p)
67
68 -- If any element satisfies P, then P is satisfied.
69 satisfied : Any P xs → ∃ P
70 satisfied (here px) = _ , px
71 satisfied (there pxs) = satisfied pxs
72
73 ------------------------------------------------------------------------
74 -- Properties of predicates preserved by Any
75
76 any? : Decidable P → ∀ {n} → Decidable (Any P {n})
77 any? P? [] = no λ()
78 any? P? (x ∷ xs) = Dec.map′ fromSum toSum (P? x ⊎-dec any? P? xs)
79
80 satisfiable : Satisfiable P → ∀ {n} → Satisfiable (Any P {suc n})
81 satisfiable (x , p) {zero} = x ∷ [] , here p
82 satisfiable (x , p) {suc n} = Product.map (x ∷_) there (satisfiable (x , p))
83
84 any = any?
85 {-# WARNING_ON_USAGE any
86 "Warning: any was deprecated in v1.4.
87 Please use any? instead."
88 #-}