| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Maybe-related properties |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Maybe.Properties where |
| 10 |
|
| 11 |
open import Algebra.Bundles using (Semigroup; Monoid) |
| 12 |
import Algebra.Structures as Structures using (IsMagma; IsSemigroup; IsMonoid) |
| 13 |
import Algebra.Definitions as Definitions |
| 14 |
using (Associative; LeftIdentity; RightIdentity; Identity; Idempotent) |
| 15 |
open import Data.Maybe.Base |
| 16 |
using (Maybe; just; nothing; map; _<∣>_; maybe; maybe′) |
| 17 |
open import Data.Maybe.Relation.Unary.All using (All; just; nothing) |
| 18 |
open import Data.Product.Base using (_,_) |
| 19 |
open import Function.Base using (_∋_; id; _∘_; _∘′_) |
| 20 |
open import Function.Definitions using (Injective) |
| 21 |
open import Level using (Level) |
| 22 |
open import Relation.Binary.PropositionalEquality.Core |
| 23 |
using (_≡_; refl; cong; cong₂; _≗_) |
| 24 |
open import Relation.Binary.PropositionalEquality.Properties |
| 25 |
using (isEquivalence) |
| 26 |
open import Relation.Binary.Definitions using (DecidableEquality) |
| 27 |
open import Relation.Nullary.Decidable using (yes; no; map′) |
| 28 |
|
| 29 |
private |
| 30 |
variable |
| 31 |
a b c : Level |
| 32 |
A : Set a |
| 33 |
B : Set b |
| 34 |
C : Set c |
| 35 |
|
| 36 |
------------------------------------------------------------------------ |
| 37 |
-- Equality |
| 38 |
|
| 39 |
just-injective : ∀ {x y} → (Maybe A ∋ just x) ≡ just y → x ≡ y |
| 40 |
just-injective refl = refl |
| 41 |
|
| 42 |
≡-dec : DecidableEquality A → DecidableEquality (Maybe A) |
| 43 |
≡-dec _≟_ nothing nothing = yes refl |
| 44 |
≡-dec _≟_ (just x) nothing = no λ() |
| 45 |
≡-dec _≟_ nothing (just y) = no λ() |
| 46 |
≡-dec _≟_ (just x) (just y) = map′ (cong just) just-injective (x ≟ y) |
| 47 |
|
| 48 |
------------------------------------------------------------------------ |
| 49 |
-- map |
| 50 |
|
| 51 |
map-id : map id ≗ id {A = Maybe A} |
| 52 |
map-id (just x) = refl |
| 53 |
map-id nothing = refl |
| 54 |
|
| 55 |
map-id-local : ∀ {f : A → A} {mx} → All (λ x → f x ≡ x) mx → map f mx ≡ mx |
| 56 |
map-id-local (just eq) = cong just eq |
| 57 |
map-id-local nothing = refl |
| 58 |
|
| 59 |
map-<∣> : ∀ (f : A → B) mx my → |
| 60 |
map f (mx <∣> my) ≡ map f mx <∣> map f my |
| 61 |
map-<∣> f (just x) my = refl |
| 62 |
map-<∣> f nothing my = refl |
| 63 |
|
| 64 |
map-cong : {f g : A → B} → f ≗ g → map f ≗ map g |
| 65 |
map-cong f≗g (just x) = cong just (f≗g x) |
| 66 |
map-cong f≗g nothing = refl |
| 67 |
|
| 68 |
map-cong-local : ∀ {f g : A → B} {mx} → |
| 69 |
All (λ x → f x ≡ g x) mx → map f mx ≡ map g mx |
| 70 |
map-cong-local (just eq) = cong just eq |
| 71 |
map-cong-local nothing = refl |
| 72 |
|
| 73 |
map-injective : ∀ {f : A → B} → Injective _≡_ _≡_ f → Injective _≡_ _≡_ (map f) |
| 74 |
map-injective f-inj {nothing} {nothing} p = refl |
| 75 |
map-injective f-inj {just x} {just y} p = cong just (f-inj (just-injective p)) |
| 76 |
|
| 77 |
map-∘ : {g : B → C} {f : A → B} → map (g ∘ f) ≗ map g ∘ map f |
| 78 |
map-∘ (just x) = refl |
| 79 |
map-∘ nothing = refl |
| 80 |
|
| 81 |
map-nothing : ∀ {f : A → B} {ma} → ma ≡ nothing → map f ma ≡ nothing |
| 82 |
map-nothing refl = refl |
| 83 |
|
| 84 |
map-just : ∀ {f : A → B} {ma a} → ma ≡ just a → map f ma ≡ just (f a) |
| 85 |
map-just refl = refl |
| 86 |
|
| 87 |
------------------------------------------------------------------------ |
| 88 |
-- maybe |
| 89 |
|
| 90 |
maybe-map : ∀ {C : Maybe B → Set c} |
| 91 |
(j : (x : B) → C (just x)) (n : C nothing) (f : A → B) ma → |
| 92 |
maybe {B = C} j n (map f ma) ≡ maybe {B = C ∘ map f} (j ∘ f) n ma |
| 93 |
maybe-map j n f (just x) = refl |
| 94 |
maybe-map j n f nothing = refl |
| 95 |
|
| 96 |
maybe′-map : ∀ j (n : C) (f : A → B) ma → |
| 97 |
maybe′ j n (map f ma) ≡ maybe′ (j ∘′ f) n ma |
| 98 |
maybe′-map = maybe-map |
| 99 |
|
| 100 |
maybe′-∘ : ∀ {b} (f : B → C) (g : A → B) → f ∘ (maybe′ g b) ≗ maybe′ (f ∘ g) (f b) |
| 101 |
maybe′-∘ _ _ = maybe (λ _ → refl) refl |
| 102 |
|
| 103 |
------------------------------------------------------------------------ |
| 104 |
-- _<∣>_ |
| 105 |
|
| 106 |
module _ {A : Set a} where |
| 107 |
|
| 108 |
open Definitions {A = Maybe A} _≡_ |
| 109 |
|
| 110 |
<∣>-assoc : Associative _<∣>_ |
| 111 |
<∣>-assoc (just x) y z = refl |
| 112 |
<∣>-assoc nothing y z = refl |
| 113 |
|
| 114 |
<∣>-identityˡ : LeftIdentity nothing _<∣>_ |
| 115 |
<∣>-identityˡ (just x) = refl |
| 116 |
<∣>-identityˡ nothing = refl |
| 117 |
|
| 118 |
<∣>-identityʳ : RightIdentity nothing _<∣>_ |
| 119 |
<∣>-identityʳ (just x) = refl |
| 120 |
<∣>-identityʳ nothing = refl |
| 121 |
|
| 122 |
<∣>-identity : Identity nothing _<∣>_ |
| 123 |
<∣>-identity = <∣>-identityˡ , <∣>-identityʳ |
| 124 |
|
| 125 |
<∣>-idem : Idempotent _<∣>_ |
| 126 |
<∣>-idem (just x) = refl |
| 127 |
<∣>-idem nothing = refl |
| 128 |
|
| 129 |
module _ (A : Set a) where |
| 130 |
|
| 131 |
open Structures {A = Maybe A} _≡_ |
| 132 |
|
| 133 |
<∣>-isMagma : IsMagma _<∣>_ |
| 134 |
<∣>-isMagma = record |
| 135 |
{ isEquivalence = isEquivalence |
| 136 |
; ∙-cong = cong₂ _<∣>_ |
| 137 |
} |
| 138 |
|
| 139 |
<∣>-isSemigroup : IsSemigroup _<∣>_ |
| 140 |
<∣>-isSemigroup = record |
| 141 |
{ isMagma = <∣>-isMagma |
| 142 |
; assoc = <∣>-assoc |
| 143 |
} |
| 144 |
|
| 145 |
<∣>-isMonoid : IsMonoid _<∣>_ nothing |
| 146 |
<∣>-isMonoid = record |
| 147 |
{ isSemigroup = <∣>-isSemigroup |
| 148 |
; identity = <∣>-identity |
| 149 |
} |
| 150 |
|
| 151 |
<∣>-semigroup : Semigroup a a |
| 152 |
<∣>-semigroup = record |
| 153 |
{ isSemigroup = <∣>-isSemigroup |
| 154 |
} |
| 155 |
|
| 156 |
<∣>-monoid : Monoid a a |
| 157 |
<∣>-monoid = record |
| 158 |
{ isMonoid = <∣>-isMonoid |
| 159 |
} |
| 160 |
|
| 161 |
------------------------------------------------------------------------ |
| 162 |
-- DEPRECATED |
| 163 |
------------------------------------------------------------------------ |
| 164 |
-- Please use the new names as continuing support for the old names is |
| 165 |
-- not guaranteed. |
| 166 |
|
| 167 |
-- Version 2.0 |
| 168 |
|
| 169 |
map-id₂ = map-id-local |
| 170 |
{-# WARNING_ON_USAGE map-id₂ |
| 171 |
"Warning: map-id₂ was deprecated in v2.0. |
| 172 |
Please use map-id-local instead." |
| 173 |
#-} |
| 174 |
|
| 175 |
map-cong₂ = map-cong-local |
| 176 |
{-# WARNING_ON_USAGE map-id₂ |
| 177 |
"Warning: map-cong₂ was deprecated in v2.0. |
| 178 |
Please use map-cong-local instead." |
| 179 |
#-} |
| 180 |
|
| 181 |
map-compose = map-∘ |
| 182 |
{-# WARNING_ON_USAGE map-compose |
| 183 |
"Warning: map-compose was deprecated in v2.0. |
| 184 |
Please use map-∘ instead." |
| 185 |
#-} |
| 186 |
|
| 187 |
map-<∣>-commute = map-<∣> |
| 188 |
{-# WARNING_ON_USAGE map-<∣>-commute |
| 189 |
"Warning: map-<∣>-commute was deprecated in v2.0. |
| 190 |
Please use map-<∣> instead." |
| 191 |
#-} |