Debian Codegraph @

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

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Maybes where one of the elements satisfies a given property
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Maybe.Relation.Unary.Any where
10
11 open import Data.Maybe.Base using (Maybe; just; nothing)
12 open import Data.Product.Base as Product using (∃; _,_; -,_)
13 open import Function.Base using (id)
14 open import Function.Bundles using (_⇔_; mk⇔)
15 open import Level using (Level; _⊔_)
16 open import Relation.Binary.PropositionalEquality.Core using (_≡_; cong)
17 open import Relation.Unary
18 using (Pred; _⊆_; _∩_; Decidable; Irrelevant; Satisfiable)
19 open import Relation.Nullary.Decidable as Dec using (Dec; yes; no)
20
21 ------------------------------------------------------------------------
22 -- Definition
23
24 data Any {a p} {A : Set a} (P : Pred A p) : Pred (Maybe A) (a ⊔ p) where
25 just : ∀ {x} → P x → Any P (just x)
26
27 ------------------------------------------------------------------------
28 -- Basic operations
29
30 module _ {a p} {A : Set a} {P : Pred A p} where
31
32 drop-just : ∀ {x} → Any P (just x) → P x
33 drop-just (just px) = px
34
35 just-equivalence : ∀ {x} → P x ⇔ Any P (just x)
36 just-equivalence = mk⇔ just drop-just
37
38 map : ∀ {q} {Q : Pred A q} → P ⊆ Q → Any P ⊆ Any Q
39 map f (just px) = just (f px)
40
41 satisfied : ∀ {x} → Any P x → ∃ P
42 satisfied (just p) = -, p
43
44 ------------------------------------------------------------------------
45 -- (un/)zip(/With)
46
47 module _ {a p q r} {A : Set a} {P : Pred A p} {Q : Pred A q} {R : Pred A r} where
48
49 zipWith : P ∩ Q ⊆ R → Any P ∩ Any Q ⊆ Any R
50 zipWith f (just px , just qx) = just (f (px , qx))
51
52 unzipWith : P ⊆ Q ∩ R → Any P ⊆ Any Q ∩ Any R
53 unzipWith f (just px) = Product.map just just (f px)
54
55 module _ {a p q} {A : Set a} {P : Pred A p} {Q : Pred A q} where
56
57 zip : Any P ∩ Any Q ⊆ Any (P ∩ Q)
58 zip = zipWith id
59
60 unzip : Any (P ∩ Q) ⊆ Any P ∩ Any Q
61 unzip = unzipWith id
62
63 ------------------------------------------------------------------------
64 -- Seeing Any as a predicate transformer
65
66 module _ {a p} {A : Set a} {P : Pred A p} where
67
68 dec : Decidable P → Decidable (Any P)
69 dec P-dec nothing = no λ ()
70 dec P-dec (just x) = Dec.map just-equivalence (P-dec x)
71
72 irrelevant : Irrelevant P → Irrelevant (Any P)
73 irrelevant P-irrelevant (just p) (just q) = cong just (P-irrelevant p q)
74
75 satisfiable : Satisfiable P → Satisfiable (Any P)
76 satisfiable P-satisfiable = Product.map just just P-satisfiable