| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Applicative functors |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- Note that currently the applicative functor laws are not included |
| 8 |
-- here. |
| 9 |
|
| 10 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 11 |
|
| 12 |
module Effect.Applicative where |
| 13 |
|
| 14 |
open import Data.Bool.Base using (Bool; true; false) |
| 15 |
open import Data.Product.Base using (_×_; _,_) |
| 16 |
open import Data.Unit.Polymorphic.Base using (⊤) |
| 17 |
open import Effect.Choice using (RawChoice) |
| 18 |
open import Effect.Empty using (RawEmpty) |
| 19 |
open import Effect.Functor as Fun using (RawFunctor) |
| 20 |
open import Function.Base using (const; flip; _∘′_) |
| 21 |
open import Level using (Level; suc; _⊔_) |
| 22 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_) |
| 23 |
|
| 24 |
private |
| 25 |
variable |
| 26 |
f g : Level |
| 27 |
A B C : Set f |
| 28 |
------------------------------------------------------------------------ |
| 29 |
-- The type of raw applicatives |
| 30 |
|
| 31 |
record RawApplicative (F : Set f → Set g) : Set (suc f ⊔ g) where |
| 32 |
infixl 4 _<*>_ _<*_ _*>_ |
| 33 |
infixl 4 _⊛_ _<⊛_ _⊛>_ |
| 34 |
infix 4 _⊗_ |
| 35 |
field |
| 36 |
rawFunctor : RawFunctor F |
| 37 |
pure : A → F A |
| 38 |
_<*>_ : F (A → B) → F A → F B |
| 39 |
|
| 40 |
open RawFunctor rawFunctor public |
| 41 |
|
| 42 |
_<*_ : F A → F B → F A |
| 43 |
a <* b = const <$> a <*> b |
| 44 |
|
| 45 |
_*>_ : F A → F B → F B |
| 46 |
a *> b = flip const <$> a <*> b |
| 47 |
|
| 48 |
zipWith : (A → B → C) → F A → F B → F C |
| 49 |
zipWith f x y = f <$> x <*> y |
| 50 |
|
| 51 |
zip : F A → F B → F (A × B) |
| 52 |
zip = zipWith _,_ |
| 53 |
|
| 54 |
-- Haskell-style alternative name for pure |
| 55 |
return : A → F A |
| 56 |
return = pure |
| 57 |
|
| 58 |
-- backwards compatibility: unicode variants |
| 59 |
_⊛_ : F (A → B) → F A → F B |
| 60 |
_⊛_ = _<*>_ |
| 61 |
|
| 62 |
_<⊛_ : F A → F B → F A |
| 63 |
_<⊛_ = _<*_ |
| 64 |
|
| 65 |
_⊛>_ : F A → F B → F B |
| 66 |
_⊛>_ = _*>_ |
| 67 |
|
| 68 |
_⊗_ : F A → F B → F (A × B) |
| 69 |
_⊗_ = zip |
| 70 |
|
| 71 |
module _ where |
| 72 |
|
| 73 |
open RawApplicative |
| 74 |
open RawFunctor |
| 75 |
|
| 76 |
-- Smart constructor |
| 77 |
mkRawApplicative : |
| 78 |
(F : Set f → Set g) → |
| 79 |
(pure : ∀ {A} → A → F A) → |
| 80 |
(app : ∀ {A B} → F (A → B) → F A → F B) → |
| 81 |
RawApplicative F |
| 82 |
mkRawApplicative F pure app .rawFunctor ._<$>_ = app ∘′ pure |
| 83 |
mkRawApplicative F pure app .pure = pure |
| 84 |
mkRawApplicative F pure app ._<*>_ = app |
| 85 |
|
| 86 |
------------------------------------------------------------------------ |
| 87 |
-- The type of raw applicatives with a zero |
| 88 |
|
| 89 |
record RawApplicativeZero (F : Set f → Set g) : Set (suc f ⊔ g) where |
| 90 |
field |
| 91 |
rawApplicative : RawApplicative F |
| 92 |
rawEmpty : RawEmpty F |
| 93 |
|
| 94 |
open RawApplicative rawApplicative public |
| 95 |
open RawEmpty rawEmpty public |
| 96 |
|
| 97 |
guard : Bool → F ⊤ |
| 98 |
guard true = pure _ |
| 99 |
guard false = empty |
| 100 |
|
| 101 |
------------------------------------------------------------------------ |
| 102 |
-- The type of raw alternative applicatives |
| 103 |
|
| 104 |
record RawAlternative (F : Set f → Set g) : Set (suc f ⊔ g) where |
| 105 |
field |
| 106 |
rawApplicativeZero : RawApplicativeZero F |
| 107 |
rawChoice : RawChoice F |
| 108 |
|
| 109 |
open RawApplicativeZero rawApplicativeZero public |
| 110 |
open RawChoice rawChoice public |
| 111 |
|
| 112 |
------------------------------------------------------------------------ |
| 113 |
-- The type of applicative morphisms |
| 114 |
|
| 115 |
record Morphism {F₁ F₂ : Set f → Set g} |
| 116 |
(A₁ : RawApplicative F₁) |
| 117 |
(A₂ : RawApplicative F₂) : Set (suc f ⊔ g) where |
| 118 |
module A₁ = RawApplicative A₁ |
| 119 |
module A₂ = RawApplicative A₂ |
| 120 |
field |
| 121 |
functorMorphism : Fun.Morphism A₁.rawFunctor A₂.rawFunctor |
| 122 |
|
| 123 |
open Fun.Morphism functorMorphism public |
| 124 |
|
| 125 |
field |
| 126 |
op-pure : (x : A) → op (A₁.pure x) ≡ A₂.pure x |
| 127 |
op-<*> : (f : F₁ (A → B)) (x : F₁ A) → |
| 128 |
op (f A₁.⊛ x) ≡ (op f A₂.⊛ op x) |
| 129 |
|
| 130 |
-- backwards compatibility: unicode variants |
| 131 |
op-⊛ = op-<*> |