| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Maybes where all the elements satisfy a given property |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Maybe.Relation.Unary.All where |
| 10 |
|
| 11 |
open import Data.Maybe.Base using (Maybe; just; nothing) |
| 12 |
open import Data.Maybe.Relation.Unary.Any using (Any; just) |
| 13 |
open import Data.Product.Base as Product using (_,_) |
| 14 |
open import Effect.Applicative using (RawApplicative) |
| 15 |
open import Effect.Monad using (RawMonad) |
| 16 |
open import Function.Base using (id; _∘′_) |
| 17 |
open import Function.Bundles using (_⇔_; mk⇔) |
| 18 |
open import Level using (Level; _⊔_) |
| 19 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong) |
| 20 |
open import Relation.Nullary hiding (Irrelevant) |
| 21 |
import Relation.Nullary.Decidable as Dec using (Dec; yes; no; map) |
| 22 |
open import Relation.Unary |
| 23 |
using (Pred; _⊆_; _∩_; _∪_; Decidable; Universal; Irrelevant; Satisfiable) |
| 24 |
|
| 25 |
------------------------------------------------------------------------ |
| 26 |
-- Definition |
| 27 |
|
| 28 |
data All {a p} {A : Set a} (P : Pred A p) : Pred (Maybe A) (a ⊔ p) where |
| 29 |
just : ∀ {x} → P x → All P (just x) |
| 30 |
nothing : All P nothing |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
-- Basic operations |
| 34 |
|
| 35 |
module _ {a p} {A : Set a} {P : Pred A p} where |
| 36 |
|
| 37 |
drop-just : ∀ {x} → All P (just x) → P x |
| 38 |
drop-just (just px) = px |
| 39 |
|
| 40 |
just-equivalence : ∀ {x} → P x ⇔ All P (just x) |
| 41 |
just-equivalence = mk⇔ just drop-just |
| 42 |
|
| 43 |
map : ∀ {q} {Q : Pred A q} → P ⊆ Q → All P ⊆ All Q |
| 44 |
map f (just px) = just (f px) |
| 45 |
map f nothing = nothing |
| 46 |
|
| 47 |
fromAny : Any P ⊆ All P |
| 48 |
fromAny (just px) = just px |
| 49 |
|
| 50 |
------------------------------------------------------------------------ |
| 51 |
-- (un/)zip(/With) |
| 52 |
|
| 53 |
module _ {a p q r} {A : Set a} {P : Pred A p} {Q : Pred A q} {R : Pred A r} where |
| 54 |
|
| 55 |
zipWith : P ∩ Q ⊆ R → All P ∩ All Q ⊆ All R |
| 56 |
zipWith f (just px , just qx) = just (f (px , qx)) |
| 57 |
zipWith f (nothing , nothing) = nothing |
| 58 |
|
| 59 |
unzipWith : P ⊆ Q ∩ R → All P ⊆ All Q ∩ All R |
| 60 |
unzipWith f (just px) = Product.map just just (f px) |
| 61 |
unzipWith f nothing = nothing , nothing |
| 62 |
|
| 63 |
module _ {a p q} {A : Set a} {P : Pred A p} {Q : Pred A q} where |
| 64 |
|
| 65 |
zip : All P ∩ All Q ⊆ All (P ∩ Q) |
| 66 |
zip = zipWith id |
| 67 |
|
| 68 |
unzip : All (P ∩ Q) ⊆ All P ∩ All Q |
| 69 |
unzip = unzipWith id |
| 70 |
|
| 71 |
------------------------------------------------------------------------ |
| 72 |
-- Traversable-like functions |
| 73 |
|
| 74 |
module _ {a f} p {A : Set a} {P : Pred A (a ⊔ p)} {F} |
| 75 |
(App : RawApplicative {a ⊔ p} {f} F) where |
| 76 |
|
| 77 |
open RawApplicative App |
| 78 |
|
| 79 |
sequenceA : All (F ∘′ P) ⊆ F ∘′ All P |
| 80 |
sequenceA nothing = pure nothing |
| 81 |
sequenceA (just px) = just <$> px |
| 82 |
|
| 83 |
mapA : ∀ {q} {Q : Pred A q} → (Q ⊆ F ∘′ P) → All Q ⊆ (F ∘′ All P) |
| 84 |
mapA f = sequenceA ∘′ map f |
| 85 |
|
| 86 |
forA : ∀ {q} {Q : Pred A q} {xs} → All Q xs → (Q ⊆ F ∘′ P) → F (All P xs) |
| 87 |
forA qxs f = mapA f qxs |
| 88 |
|
| 89 |
module _ {a f} p {A : Set a} {P : Pred A (a ⊔ p)} {M} |
| 90 |
(Mon : RawMonad {a ⊔ p} {f} M) where |
| 91 |
|
| 92 |
private App = RawMonad.rawApplicative Mon |
| 93 |
|
| 94 |
sequenceM : All (M ∘′ P) ⊆ M ∘′ All P |
| 95 |
sequenceM = sequenceA p App |
| 96 |
|
| 97 |
mapM : ∀ {q} {Q : Pred A q} → (Q ⊆ M ∘′ P) → All Q ⊆ (M ∘′ All P) |
| 98 |
mapM = mapA p App |
| 99 |
|
| 100 |
forM : ∀ {q} {Q : Pred A q} {xs} → All Q xs → (Q ⊆ M ∘′ P) → M (All P xs) |
| 101 |
forM = forA p App |
| 102 |
|
| 103 |
------------------------------------------------------------------------ |
| 104 |
-- Seeing All as a predicate transformer |
| 105 |
|
| 106 |
module _ {a p} {A : Set a} {P : Pred A p} where |
| 107 |
|
| 108 |
dec : Decidable P → Decidable (All P) |
| 109 |
dec P-dec nothing = yes nothing |
| 110 |
dec P-dec (just x) = Dec.map just-equivalence (P-dec x) |
| 111 |
|
| 112 |
universal : Universal P → Universal (All P) |
| 113 |
universal P-universal (just x) = just (P-universal x) |
| 114 |
universal P-universal nothing = nothing |
| 115 |
|
| 116 |
irrelevant : Irrelevant P → Irrelevant (All P) |
| 117 |
irrelevant P-irrelevant (just p) (just q) = cong just (P-irrelevant p q) |
| 118 |
irrelevant P-irrelevant nothing nothing = refl |
| 119 |
|
| 120 |
satisfiable : Satisfiable (All P) |
| 121 |
satisfiable = nothing , nothing |