Debian Codegraph @

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

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Lists where at least one element satisfies a given property
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Relation.Unary.Any where
10
11 open import Data.Fin.Base using (Fin; zero; suc)
12 open import Data.List.Base as List using (List; []; [_]; _∷_; removeAt)
13 open import Data.Product.Base as Product using (∃; _,_)
14 open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂)
15 open import Level using (Level; _⊔_)
16 open import Relation.Nullary.Decidable.Core as Dec using (no; _⊎-dec_)
17 open import Relation.Nullary.Negation using (¬_; contradiction)
18 open import Relation.Unary using (Pred; _⊆_; Decidable; Satisfiable)
19
20 private
21 variable
22 a p q : Level
23 A : Set a
24 P Q : Pred A p
25 x : A
26 xs : List A
27
28 ------------------------------------------------------------------------
29 -- Definition
30
31 -- Given a predicate P, then Any P xs means that at least one element
32 -- in xs satisfies P. See `Relation.Unary` for an explanation of
33 -- predicates.
34
35 data Any {A : Set a} (P : Pred A p) : Pred (List A) (a ⊔ p) where
36 here : ∀ {x xs} (px : P x) → Any P (x ∷ xs)
37 there : ∀ {x xs} (pxs : Any P xs) → Any P (x ∷ xs)
38
39 ------------------------------------------------------------------------
40 -- Operations on Any
41
42 head : ¬ Any P xs → Any P (x ∷ xs) → P x
43 head ¬pxs (here px) = px
44 head ¬pxs (there pxs) = contradiction pxs ¬pxs
45
46 tail : ¬ P x → Any P (x ∷ xs) → Any P xs
47 tail ¬px (here px) = contradiction px ¬px
48 tail ¬px (there pxs) = pxs
49
50 map : P ⊆ Q → Any P ⊆ Any Q
51 map g (here px) = here (g px)
52 map g (there pxs) = there (map g pxs)
53
54 -- `index x∈xs` is the list position (zero-based) which `x∈xs` points to.
55
56 index : Any P xs → Fin (List.length xs)
57 index (here px) = zero
58 index (there pxs) = suc (index pxs)
59
60 lookup : {P : Pred A p} → Any P xs → A
61 lookup {xs = xs} p = List.lookup xs (index p)
62
63 infixr 5 _∷=_
64
65 _∷=_ : {P : Pred A p} → Any P xs → A → List A
66 _∷=_ {xs = xs} x∈xs v = xs List.[ index x∈xs ]∷= v
67
68 infixl 4 _─_
69 _─_ : {P : Pred A p} → ∀ xs → Any P xs → List A
70 xs ─ x∈xs = removeAt xs (index x∈xs)
71
72 -- If any element satisfies P, then P is satisfied.
73
74 satisfied : Any P xs → ∃ P
75 satisfied (here px) = _ , px
76 satisfied (there pxs) = satisfied pxs
77
78 toSum : Any P (x ∷ xs) → P x ⊎ Any P xs
79 toSum (here px) = inj₁ px
80 toSum (there pxs) = inj₂ pxs
81
82 fromSum : P x ⊎ Any P xs → Any P (x ∷ xs)
83 fromSum (inj₁ px) = here px
84 fromSum (inj₂ pxs) = there pxs
85
86 ------------------------------------------------------------------------
87 -- Properties of predicates preserved by Any
88
89 any? : Decidable P → Decidable (Any P)
90 any? P? [] = no λ()
91 any? P? (x ∷ xs) = Dec.map′ fromSum toSum (P? x ⊎-dec any? P? xs)
92
93 satisfiable : Satisfiable P → Satisfiable (Any P)
94 satisfiable (x , Px) = [ x ] , here Px
95
96
97 ------------------------------------------------------------------------
98 -- DEPRECATED
99 ------------------------------------------------------------------------
100 -- Please use the new names as continuing support for the old names is
101 -- not guaranteed.
102
103 -- Version 1.4
104
105 any = any?
106 {-# WARNING_ON_USAGE any
107 "Warning: any was deprecated in v1.4.
108 Please use any? instead."
109 #-}