Debian Codegraph @

src/Data/Maybe/Relation/Binary/Pointwise.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Pointwise lifting of relations to maybes
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Maybe.Relation.Binary.Pointwise where
10
11 open import Data.Product.Base using (∃; _×_; -,_; _,_)
12 open import Data.Maybe.Base using (Maybe; just; nothing)
13 open import Data.Maybe.Relation.Unary.Any using (Any; just)
14 open import Function.Bundles using (_⇔_; mk⇔)
15 open import Level using (Level; _⊔_)
16 open import Relation.Binary.Bundles using (Setoid; DecSetoid)
17 open import Relation.Binary.Core using (REL; Rel; _⇒_)
18 open import Relation.Binary.Definitions using (Reflexive; Sym; Trans; Decidable)
19 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
20 open import Relation.Binary.Structures using (IsEquivalence; IsDecEquivalence)
21 open import Relation.Nullary.Negation.Core using (¬_)
22 open import Relation.Unary using (_⊆_)
23 open import Relation.Nullary.Decidable as Dec using (yes; no; map)
24
25 ------------------------------------------------------------------------
26 -- Definition
27
28 data Pointwise
29 {a b ℓ} {A : Set a} {B : Set b}
30 (R : REL A B ℓ) : REL (Maybe A) (Maybe B) (a ⊔ b ⊔ ℓ) where
31 just : ∀ {x y} → R x y → Pointwise R (just x) (just y)
32 nothing : Pointwise R nothing nothing
33
34 ------------------------------------------------------------------------
35 -- Properties
36
37 module _ {a b ℓ} {A : Set a} {B : Set b} {R : REL A B ℓ} where
38
39 drop-just : ∀ {x y} → Pointwise R (just x) (just y) → R x y
40 drop-just (just p) = p
41
42 just-equivalence : ∀ {x y} → R x y ⇔ Pointwise R (just x) (just y)
43 just-equivalence = mk⇔ just drop-just
44
45 nothing-inv : ∀ {x} → Pointwise R nothing x → x ≡ nothing
46 nothing-inv nothing = ≡.refl
47
48 just-inv : ∀ {x y} → Pointwise R (just x) y → ∃ λ z → y ≡ just z × R x z
49 just-inv (just r) = -, ≡.refl , r
50
51 ------------------------------------------------------------------------
52 -- Relational properties
53
54 module _ {a r} {A : Set a} {R : Rel A r} where
55
56 refl : Reflexive R → Reflexive (Pointwise R)
57 refl R-refl {just _} = just R-refl
58 refl R-refl {nothing} = nothing
59
60 reflexive : _≡_ ⇒ R → _≡_ ⇒ Pointwise R
61 reflexive reflexive ≡.refl = refl (reflexive ≡.refl)
62
63 module _ {a b r₁ r₂} {A : Set a} {B : Set b}
64 {R : REL A B r₁} {S : REL B A r₂} where
65
66 sym : Sym R S → Sym (Pointwise R) (Pointwise S)
67 sym R-sym (just p) = just (R-sym p)
68 sym R-sym nothing = nothing
69
70 module _ {a b c r₁ r₂ r₃} {A : Set a} {B : Set b} {C : Set c}
71 {R : REL A B r₁} {S : REL B C r₂} {T : REL A C r₃} where
72
73 trans : Trans R S T → Trans (Pointwise R) (Pointwise S) (Pointwise T)
74 trans R-trans (just p) (just q) = just (R-trans p q)
75 trans R-trans nothing nothing = nothing
76
77 module _ {a r} {A : Set a} {R : Rel A r} where
78
79 dec : Decidable R → Decidable (Pointwise R)
80 dec R-dec (just x) (just y) = Dec.map just-equivalence (R-dec x y)
81 dec R-dec (just x) nothing = no (λ ())
82 dec R-dec nothing (just y) = no (λ ())
83 dec R-dec nothing nothing = yes nothing
84
85 isEquivalence : IsEquivalence R → IsEquivalence (Pointwise R)
86 isEquivalence R-isEquivalence = record
87 { refl = refl R.refl
88 ; sym = sym R.sym
89 ; trans = trans R.trans
90 } where module R = IsEquivalence R-isEquivalence
91
92 isDecEquivalence : IsDecEquivalence R → IsDecEquivalence (Pointwise R)
93 isDecEquivalence R-isDecEquivalence = record
94 { isEquivalence = isEquivalence R.isEquivalence
95 ; _≟_ = dec R._≟_
96 } where module R = IsDecEquivalence R-isDecEquivalence
97
98 pointwise⊆any : ∀ {x} → Pointwise R (just x) ⊆ Any (R x)
99 pointwise⊆any (just Rxy) = just Rxy
100
101 module _ {c ℓ} where
102
103 setoid : Setoid c ℓ → Setoid c (c ⊔ ℓ)
104 setoid S = record
105 { isEquivalence = isEquivalence S.isEquivalence
106 } where module S = Setoid S
107
108 decSetoid : DecSetoid c ℓ → DecSetoid c (c ⊔ ℓ)
109 decSetoid S = record
110 { isDecEquivalence = isDecEquivalence S.isDecEquivalence
111 } where module S = DecSetoid S