| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of operations on the Delay type |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --sized-types #-} |
| 8 |
|
| 9 |
module Codata.Sized.Delay.Properties where |
| 10 |
|
| 11 |
open import Size |
| 12 |
import Data.Sum.Base as Sum |
| 13 |
import Data.Nat.Base as ℕ |
| 14 |
open import Codata.Sized.Thunk using (Thunk; force) |
| 15 |
open import Codata.Sized.Conat using (Conat; zero; suc; infinity; _⊔_; toℕ) |
| 16 |
open import Codata.Sized.Conat.Bisimilarity as Coℕ using (zero ; suc) |
| 17 |
open import Codata.Sized.Delay |
| 18 |
open import Codata.Sized.Delay.Bisimilarity using (now; later; _⊢_≈_) |
| 19 |
open import Function.Base using (id; _∘′_) |
| 20 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 21 |
|
| 22 |
module _ {a} {A : Set a} where |
| 23 |
|
| 24 |
length-never : ∀ {i} → i Coℕ.⊢ length (never {A = A}) ≈ infinity |
| 25 |
length-never = suc λ where .force → length-never |
| 26 |
|
| 27 |
module _ {a b} {A : Set a} {B : Set b} where |
| 28 |
|
| 29 |
length-map : ∀ (f : A → B) da {i} → i Coℕ.⊢ length (map f da) ≈ length da |
| 30 |
length-map f (now a) = zero |
| 31 |
length-map f (later da) = suc λ where .force → length-map f (da .force) |
| 32 |
|
| 33 |
module _ {a b c} {A : Set a} {B : Set b} {C : Set c} where |
| 34 |
|
| 35 |
length-zipWith : ∀ (f : A → B → C) da db {i} → |
| 36 |
i Coℕ.⊢ length (zipWith f da db) ≈ length da ⊔ length db |
| 37 |
length-zipWith f (now a) db = length-map (f a) db |
| 38 |
length-zipWith f da@(later _) (now b) = length-map (λ a → f a b) da |
| 39 |
length-zipWith f (later da) (later db) = |
| 40 |
suc λ where .force → length-zipWith f (da .force) (db .force) |
| 41 |
|
| 42 |
map-id : ∀ da {i} → i ⊢ map (id {A = A}) da ≈ da |
| 43 |
map-id (now a) = now ≡.refl |
| 44 |
map-id (later da) = later λ where .force → map-id (da .force) |
| 45 |
|
| 46 |
map-∘ : ∀ (f : A → B) (g : B → C) da {i} → |
| 47 |
i ⊢ map g (map f da) ≈ map (g ∘′ f) da |
| 48 |
map-∘ f g (now a) = now ≡.refl |
| 49 |
map-∘ f g (later da) = later λ where .force → map-∘ f g (da .force) |
| 50 |
|
| 51 |
map-unfold : ∀ (f : B → C) n (s : A) {i} → |
| 52 |
i ⊢ map f (unfold n s) ≈ unfold (Sum.map id f ∘′ n) s |
| 53 |
map-unfold f n s with n s |
| 54 |
... | Sum.inj₁ s′ = later λ where .force → map-unfold f n s′ |
| 55 |
... | Sum.inj₂ b = now ≡.refl |
| 56 |
|
| 57 |
|
| 58 |
------------------------------------------------------------------------ |
| 59 |
-- ⇓ |
| 60 |
|
| 61 |
⇓-unique : ∀ {a} {A : Set a} → |
| 62 |
{d : Delay A ∞} → |
| 63 |
(d⇓₁ : d ⇓) → (d⇓₂ : d ⇓) → |
| 64 |
d⇓₁ ≡ d⇓₂ |
| 65 |
⇓-unique {d = now s} (now s) (now s) = ≡.refl |
| 66 |
⇓-unique {d = later d'} (later l) (later r) = |
| 67 |
≡.cong later (⇓-unique {d = force d'} l r) |
| 68 |
|
| 69 |
module _ {a} {A B : Set a} where |
| 70 |
|
| 71 |
bind̅₁ : (d : Delay A ∞) {f : A → Delay B ∞} → bind d f ⇓ → d ⇓ |
| 72 |
bind̅₁ (now s) _ = now s |
| 73 |
bind̅₁ (later s) (later x) = |
| 74 |
later (bind̅₁ (force s) x) |
| 75 |
|
| 76 |
bind̅₂ : (d : Delay A ∞) {f : A → Delay B ∞} → |
| 77 |
(bind⇓ : bind d f ⇓) → |
| 78 |
f (extract (bind̅₁ d bind⇓)) ⇓ |
| 79 |
bind̅₂ (now s) foo = foo |
| 80 |
bind̅₂ (later s) {f} (later foo) = |
| 81 |
bind̅₂ (force s) foo |
| 82 |
|
| 83 |
-- The extracted value of a bind is equivalent to the extracted value |
| 84 |
-- of its second element |
| 85 |
extract-bind-⇓ : {d : Delay A Size.∞} → {f : A → Delay B Size.∞} → |
| 86 |
(d⇓ : d ⇓) → (f⇓ : f (extract d⇓) ⇓) → |
| 87 |
extract (bind-⇓ d⇓ {f} f⇓) ≡ extract f⇓ |
| 88 |
extract-bind-⇓ (now a) f⇓ = ≡.refl |
| 89 |
extract-bind-⇓ (later t) f⇓ = extract-bind-⇓ t f⇓ |
| 90 |
|
| 91 |
-- If the right element of a bind returns a certain value so does the |
| 92 |
-- entire bind |
| 93 |
extract-bind̅₂-bind⇓ : |
| 94 |
(d : Delay A ∞) {f : A → Delay B ∞} → |
| 95 |
(bind⇓ : bind d f ⇓) → |
| 96 |
extract (bind̅₂ d bind⇓) ≡ extract bind⇓ |
| 97 |
extract-bind̅₂-bind⇓ (now s) bind⇓ = ≡.refl |
| 98 |
extract-bind̅₂-bind⇓ (later s) (later bind⇓) = |
| 99 |
extract-bind̅₂-bind⇓ (force s) bind⇓ |
| 100 |
|
| 101 |
-- Proof that the length of a bind-⇓ is equal to the sum of the length |
| 102 |
-- of its components. |
| 103 |
bind⇓-length : |
| 104 |
{d : Delay A ∞} {f : A → Delay B ∞} → |
| 105 |
(bind⇓ : bind d f ⇓) → |
| 106 |
(d⇓ : d ⇓) → (f⇓ : f (extract d⇓) ⇓) → |
| 107 |
toℕ (length-⇓ bind⇓) ≡ toℕ (length-⇓ d⇓) ℕ.+ toℕ (length-⇓ f⇓) |
| 108 |
bind⇓-length {f = f} bind⇓ d⇓@(now s') f⇓ = |
| 109 |
≡.cong (toℕ ∘′ length-⇓) (⇓-unique bind⇓ f⇓) |
| 110 |
bind⇓-length {d = d@(later dt)} {f = f} bind⇓@(later bind'⇓) d⇓@(later r) f⇓ = |
| 111 |
≡.cong ℕ.suc (bind⇓-length bind'⇓ r f⇓) |
| 112 |
|
| 113 |
------------------------------------------------------------------------ |
| 114 |
-- DEPRECATED |
| 115 |
------------------------------------------------------------------------ |
| 116 |
-- Please use the new names as continuing support for the old names is |
| 117 |
-- not guaranteed. |
| 118 |
|
| 119 |
-- Version 2.0 |
| 120 |
|
| 121 |
map-identity = map-id |
| 122 |
{-# WARNING_ON_USAGE map-identity |
| 123 |
"Warning: map-identity was deprecated in v2.0. |
| 124 |
Please use map-id instead." |
| 125 |
#-} |
| 126 |
|
| 127 |
map-map-fusion = map-∘ |
| 128 |
{-# WARNING_ON_USAGE map-map-fusion |
| 129 |
"Warning: map-map-fusion was deprecated in v2.0. |
| 130 |
Please use map-∘ instead." |
| 131 |
#-} |
| 132 |
|
| 133 |
map-unfold-fusion = map-unfold |
| 134 |
{-# WARNING_ON_USAGE map-unfold-fusion |
| 135 |
"Warning: map-unfold-fusion was deprecated in v2.0. |
| 136 |
Please use map-unfold instead." |
| 137 |
#-} |