| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Sized W-types |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --sized-types #-} |
| 8 |
|
| 9 |
module Data.W.Sized where |
| 10 |
|
| 11 |
open import Level using (Level; _⊔_) |
| 12 |
open import Size using (Size; ↑_; ∞) |
| 13 |
open import Function.Base using (_$_; _∘_; const) |
| 14 |
open import Data.Product.Base using (_,_; -,_; proj₂) |
| 15 |
open import Data.Container.Core as Container using (Container; ⟦_⟧; Shape; Position; _⇒_; ⟪_⟫) |
| 16 |
open import Data.Container.Relation.Unary.All using (□; all) |
| 17 |
open import Relation.Nullary.Negation using (¬_) |
| 18 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) |
| 19 |
|
| 20 |
private |
| 21 |
variable |
| 22 |
i : Size |
| 23 |
s p s₁ s₂ p₁ p₂ ℓ : Level |
| 24 |
C : Container s p |
| 25 |
C₁ : Container s₁ p₁ |
| 26 |
C₂ : Container s₂ p₂ |
| 27 |
|
| 28 |
-- The family of W-types. |
| 29 |
|
| 30 |
data W (C : Container s p) : Size → Set (s ⊔ p) where |
| 31 |
sup : ⟦ C ⟧ (W C i) → W C (↑ i) |
| 32 |
|
| 33 |
sup-injective₁ : ∀ {s t : Shape C} {f : Position C s → W C i} {g} → |
| 34 |
sup (s , f) ≡ sup (t , g) → s ≡ t |
| 35 |
sup-injective₁ refl = refl |
| 36 |
|
| 37 |
-- See also Data.W.WithK.sup-injective₂. |
| 38 |
|
| 39 |
-- Projections. |
| 40 |
|
| 41 |
head : W C i → Shape C |
| 42 |
head (sup (x , f)) = x |
| 43 |
|
| 44 |
tail : (x : W C i) → Position C (head x) → W C i |
| 45 |
tail (sup (x , f)) = f |
| 46 |
|
| 47 |
-- map |
| 48 |
|
| 49 |
map : (m : C₁ ⇒ C₂) → W C₁ i → W C₂ i |
| 50 |
map m (sup c) = sup (⟪ m ⟫ (Container.map (map m) c)) |
| 51 |
|
| 52 |
-- induction |
| 53 |
|
| 54 |
module _ (P : W C ∞ → Set ℓ) |
| 55 |
(alg : ∀ {t} → □ C P t → P (sup t)) where |
| 56 |
|
| 57 |
induction : (w : W C _) → P w |
| 58 |
induction (sup (s , f)) = alg $ all (induction ∘ f) |
| 59 |
|
| 60 |
module _ {P : Set ℓ} (alg : ⟦ C ⟧ P → P) where |
| 61 |
|
| 62 |
foldr : W C _ → P |
| 63 |
foldr = induction (const P) (alg ∘ -,_ ∘ □.proof) |
| 64 |
|
| 65 |
-- If Position is always inhabited, then W_C is empty. |
| 66 |
|
| 67 |
inhabited⇒empty : (∀ s → Position C s) → ¬ W C i |
| 68 |
inhabited⇒empty b = foldr ((_$ b _) ∘ proj₂) |