| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Indexed 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.Indexed where |
| 13 |
|
| 14 |
open import Effect.Functor using (RawFunctor) |
| 15 |
open import Data.Product.Base using (_×_; _,_) |
| 16 |
open import Function.Base using (const; constᵣ) |
| 17 |
open import Level using (Level; suc; _⊔_) |
| 18 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong₂) |
| 19 |
open import Relation.Binary.PropositionalEquality.Properties |
| 20 |
using (module ≡-Reasoning) |
| 21 |
|
| 22 |
private |
| 23 |
variable |
| 24 |
a b c i f : Level |
| 25 |
A : Set a |
| 26 |
B : Set b |
| 27 |
C : Set c |
| 28 |
|
| 29 |
IFun : Set i → (ℓ : Level) → Set (i ⊔ suc ℓ) |
| 30 |
IFun I ℓ = I → I → Set ℓ → Set ℓ |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
-- Type, and usual combinators |
| 34 |
|
| 35 |
record RawIApplicative {I : Set i} (F : IFun I f) : |
| 36 |
Set (i ⊔ suc f) where |
| 37 |
infixl 4 _⊛_ _<⊛_ _⊛>_ |
| 38 |
infix 4 _⊗_ |
| 39 |
|
| 40 |
field |
| 41 |
pure : ∀ {i} → A → F i i A |
| 42 |
_⊛_ : ∀ {i j k} → F i j (A → B) → F j k A → F i k B |
| 43 |
|
| 44 |
rawFunctor : ∀ {i j} → RawFunctor (F i j) |
| 45 |
rawFunctor = record |
| 46 |
{ _<$>_ = λ g x → pure g ⊛ x |
| 47 |
} |
| 48 |
|
| 49 |
private |
| 50 |
open module RF {i j : I} = |
| 51 |
RawFunctor (rawFunctor {i = i} {j = j}) |
| 52 |
public |
| 53 |
|
| 54 |
_<⊛_ : ∀ {i j k} → F i j A → F j k B → F i k A |
| 55 |
x <⊛ y = const <$> x ⊛ y |
| 56 |
|
| 57 |
_⊛>_ : ∀ {i j k} → F i j A → F j k B → F i k B |
| 58 |
x ⊛> y = constᵣ <$> x ⊛ y |
| 59 |
|
| 60 |
_⊗_ : ∀ {i j k} → F i j A → F j k B → F i k (A × B) |
| 61 |
x ⊗ y = (_,_) <$> x ⊛ y |
| 62 |
|
| 63 |
zipWith : ∀ {i j k} → (A → B → C) → F i j A → F j k B → F i k C |
| 64 |
zipWith f x y = f <$> x ⊛ y |
| 65 |
|
| 66 |
zip : ∀ {i j k} → F i j A → F j k B → F i k (A × B) |
| 67 |
zip = zipWith _,_ |
| 68 |
|
| 69 |
------------------------------------------------------------------------ |
| 70 |
-- Applicative with a zero |
| 71 |
|
| 72 |
record RawIApplicativeZero |
| 73 |
{I : Set i} (F : IFun I f) : |
| 74 |
Set (i ⊔ suc f) where |
| 75 |
field |
| 76 |
applicative : RawIApplicative F |
| 77 |
∅ : ∀ {i j} → F i j A |
| 78 |
|
| 79 |
open RawIApplicative applicative public |
| 80 |
|
| 81 |
------------------------------------------------------------------------ |
| 82 |
-- Alternative functors: `F i j A` is a monoid |
| 83 |
|
| 84 |
record RawIAlternative |
| 85 |
{I : Set i} (F : IFun I f) : |
| 86 |
Set (i ⊔ suc f) where |
| 87 |
infixr 3 _∣_ |
| 88 |
field |
| 89 |
applicativeZero : RawIApplicativeZero F |
| 90 |
_∣_ : ∀ {i j} → F i j A → F i j A → F i j A |
| 91 |
|
| 92 |
open RawIApplicativeZero applicativeZero public |
| 93 |
|
| 94 |
|
| 95 |
------------------------------------------------------------------------ |
| 96 |
-- Applicative functor morphisms, specialised to propositional |
| 97 |
-- equality. |
| 98 |
|
| 99 |
record Morphism {I : Set i} {F₁ F₂ : IFun I f} |
| 100 |
(A₁ : RawIApplicative F₁) |
| 101 |
(A₂ : RawIApplicative F₂) : Set (i ⊔ suc f) where |
| 102 |
module A₁ = RawIApplicative A₁ |
| 103 |
module A₂ = RawIApplicative A₂ |
| 104 |
field |
| 105 |
op : ∀ {i j} → F₁ i j A → F₂ i j A |
| 106 |
op-pure : ∀ {i} (x : A) → op (A₁.pure {i = i} x) ≡ A₂.pure x |
| 107 |
op-⊛ : ∀ {i j k} (f : F₁ i j (A → B)) (x : F₁ j k A) → |
| 108 |
op (f A₁.⊛ x) ≡ (op f A₂.⊛ op x) |
| 109 |
|
| 110 |
op-<$> : ∀ {i j} (f : A → B) (x : F₁ i j A) → |
| 111 |
op (f A₁.<$> x) ≡ (f A₂.<$> op x) |
| 112 |
op-<$> f x = begin |
| 113 |
op (A₁._⊛_ (A₁.pure f) x) ≡⟨ op-⊛ _ _ ⟩ |
| 114 |
A₂._⊛_ (op (A₁.pure f)) (op x) ≡⟨ cong₂ A₂._⊛_ (op-pure _) refl ⟩ |
| 115 |
A₂._⊛_ (A₂.pure f) (op x) ∎ |
| 116 |
where open ≡-Reasoning |