| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Container combinators |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Container.Combinator where |
| 10 |
|
| 11 |
open import Data.Container.Core using (Container; Shape; Position; ⟦_⟧) |
| 12 |
open import Data.Container.Relation.Unary.Any using (◇; any) |
| 13 |
open import Data.Empty.Polymorphic using (⊥; ⊥-elim) |
| 14 |
open import Data.Product.Base as Product using (_,_; <_,_>; proj₁; proj₂; ∃) |
| 15 |
open import Data.Sum.Base as Sum using ([_,_]′) |
| 16 |
open import Data.Unit.Polymorphic.Base using (⊤) |
| 17 |
import Function.Base as F |
| 18 |
open import Level using (Level; _⊔_; lower) |
| 19 |
|
| 20 |
------------------------------------------------------------------------ |
| 21 |
-- Combinators |
| 22 |
|
| 23 |
module _ {s p : Level} where |
| 24 |
|
| 25 |
-- Identity. |
| 26 |
|
| 27 |
id : Container s p |
| 28 |
id .Shape = ⊤ |
| 29 |
id .Position = F.const ⊤ |
| 30 |
|
| 31 |
to-id : ∀ {a} {A : Set a} → F.id A → ⟦ id ⟧ A |
| 32 |
to-id x = (_ , λ _ → x) |
| 33 |
|
| 34 |
from-id : ∀ {a} {A : Set a} → ⟦ id ⟧ A → F.id A |
| 35 |
from-id xs = proj₂ xs _ |
| 36 |
|
| 37 |
-- Constant. |
| 38 |
|
| 39 |
const : Set s → Container s p |
| 40 |
const A .Shape = A |
| 41 |
const A .Position = F.const ⊥ |
| 42 |
|
| 43 |
to-const : ∀ {b} (A : Set s) {B : Set b} → A → ⟦ const A ⟧ B |
| 44 |
to-const _ = _, ⊥-elim {Whatever = F.const _} |
| 45 |
|
| 46 |
from-const : ∀ {b} (A : Set s) {B : Set b} → ⟦ const A ⟧ B → A |
| 47 |
from-const _ = proj₁ |
| 48 |
|
| 49 |
module _ {s₁ s₂ p₁ p₂} (C₁ : Container s₁ p₁) (C₂ : Container s₂ p₂) where |
| 50 |
|
| 51 |
-- Composition. |
| 52 |
|
| 53 |
infixr 9 _∘_ |
| 54 |
|
| 55 |
_∘_ : Container (s₁ ⊔ s₂ ⊔ p₁) (p₁ ⊔ p₂) |
| 56 |
_∘_ .Shape = ⟦ C₁ ⟧ (Shape C₂) |
| 57 |
_∘_ .Position = ◇ C₁ (Position C₂) |
| 58 |
|
| 59 |
to-∘ : ∀ {a} {A : Set a} → ⟦ C₁ ⟧ (⟦ C₂ ⟧ A) → ⟦ _∘_ ⟧ A |
| 60 |
to-∘ (s , f) = ((s , proj₁ F.∘ f) , Product.uncurry (proj₂ F.∘ f) F.∘′ ◇.proof) |
| 61 |
|
| 62 |
from-∘ : ∀ {a} {A : Set a} → ⟦ _∘_ ⟧ A → ⟦ C₁ ⟧ (⟦ C₂ ⟧ A) |
| 63 |
from-∘ ((s , f) , g) = (s , < f , Product.curry (g F.∘′ any) >) |
| 64 |
|
| 65 |
-- Product. (Note that, up to isomorphism, this is a special case of |
| 66 |
-- indexed product.) |
| 67 |
|
| 68 |
infixr 2 _×_ |
| 69 |
|
| 70 |
_×_ : Container (s₁ ⊔ s₂) (p₁ ⊔ p₂) |
| 71 |
_×_ .Shape = Shape C₁ Product.× Shape C₂ |
| 72 |
_×_ .Position = Product.uncurry λ s₁ s₂ → (Position C₁ s₁) Sum.⊎ (Position C₂ s₂) |
| 73 |
|
| 74 |
to-× : ∀ {a} {A : Set a} → ⟦ C₁ ⟧ A Product.× ⟦ C₂ ⟧ A → ⟦ _×_ ⟧ A |
| 75 |
to-× ((s₁ , f₁) , (s₂ , f₂)) = ((s₁ , s₂) , [ f₁ , f₂ ]′) |
| 76 |
|
| 77 |
from-× : ∀ {a} {A : Set a} → ⟦ _×_ ⟧ A → ⟦ C₁ ⟧ A Product.× ⟦ C₂ ⟧ A |
| 78 |
from-× ((s₁ , s₂) , f) = ((s₁ , f F.∘ Sum.inj₁) , (s₂ , f F.∘ Sum.inj₂)) |
| 79 |
|
| 80 |
-- Indexed product. |
| 81 |
|
| 82 |
module _ {i s p} (I : Set i) (Cᵢ : I → Container s p) where |
| 83 |
|
| 84 |
Π : Container (i ⊔ s) (i ⊔ p) |
| 85 |
Π .Shape = ∀ i → Shape (Cᵢ i) |
| 86 |
Π .Position = λ s → ∃ λ i → Position (Cᵢ i) (s i) |
| 87 |
|
| 88 |
to-Π : ∀ {a} {A : Set a} → (∀ i → ⟦ Cᵢ i ⟧ A) → ⟦ Π ⟧ A |
| 89 |
to-Π f = (proj₁ F.∘ f , Product.uncurry (proj₂ F.∘ f)) |
| 90 |
|
| 91 |
from-Π : ∀ {a} {A : Set a} → ⟦ Π ⟧ A → ∀ i → ⟦ Cᵢ i ⟧ A |
| 92 |
from-Π (s , f) = λ i → (s i , λ p → f (i , p)) |
| 93 |
|
| 94 |
-- Constant exponentiation. (Note that this is a special case of |
| 95 |
-- indexed product.) |
| 96 |
|
| 97 |
infix 0 const[_]⟶_ |
| 98 |
|
| 99 |
const[_]⟶_ : ∀ {i s p} → Set i → Container s p → Container (i ⊔ s) (i ⊔ p) |
| 100 |
const[ A ]⟶ C = Π A (F.const C) |
| 101 |
|
| 102 |
-- Sum. (Note that, up to isomorphism, this is a special case of |
| 103 |
-- indexed sum.) |
| 104 |
|
| 105 |
module _ {s₁ s₂ p} (C₁ : Container s₁ p) (C₂ : Container s₂ p) where |
| 106 |
|
| 107 |
infixr 1 _⊎_ |
| 108 |
|
| 109 |
_⊎_ : Container (s₁ ⊔ s₂) p |
| 110 |
_⊎_ .Shape = (Shape C₁ Sum.⊎ Shape C₂) |
| 111 |
_⊎_ .Position = [ Position C₁ , Position C₂ ]′ |
| 112 |
|
| 113 |
to-⊎ : ∀ {a} {A : Set a} → ⟦ C₁ ⟧ A Sum.⊎ ⟦ C₂ ⟧ A → ⟦ _⊎_ ⟧ A |
| 114 |
to-⊎ = [ Product.map Sum.inj₁ F.id , Product.map Sum.inj₂ F.id ]′ |
| 115 |
|
| 116 |
from-⊎ : ∀ {a} {A : Set a} → ⟦ _⊎_ ⟧ A → ⟦ C₁ ⟧ A Sum.⊎ ⟦ C₂ ⟧ A |
| 117 |
from-⊎ (Sum.inj₁ s₁ , f) = Sum.inj₁ (s₁ , f) |
| 118 |
from-⊎ (Sum.inj₂ s₂ , f) = Sum.inj₂ (s₂ , f) |
| 119 |
|
| 120 |
-- Indexed sum. |
| 121 |
|
| 122 |
module _ {i s p} (I : Set i) (C : I → Container s p) where |
| 123 |
|
| 124 |
Σ : Container (i ⊔ s) p |
| 125 |
Σ .Shape = ∃ λ i → Shape (C i) |
| 126 |
Σ .Position = λ s → Position (C (proj₁ s)) (proj₂ s) |
| 127 |
|
| 128 |
to-Σ : ∀ {a} {A : Set a} → (∃ λ i → ⟦ C i ⟧ A) → ⟦ Σ ⟧ A |
| 129 |
to-Σ (i , (s , f)) = ((i , s) , f) |
| 130 |
|
| 131 |
from-Σ : ∀ {a} {A : Set a} → ⟦ Σ ⟧ A → ∃ λ i → ⟦ C i ⟧ A |
| 132 |
from-Σ ((i , s) , f) = (i , (s , f)) |