Debian Codegraph @

src/Codata/Sized/Stream/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of operations on the Stream type
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --sized-types #-}
8
9 module Codata.Sized.Stream.Properties where
10
11 open import Codata.Sized.Stream using (Stream; _∷_; _++_; map; repeat; ap;
12 lookup; splitAt; cycle; iterate; take; _⁺++_)
13 open import Codata.Sized.Stream.Bisimilarity
14 open import Codata.Sized.Thunk as Thunk using (Thunk; force)
15 open import Data.List.Base as List using ([]; _∷_)
16 open import Data.List.NonEmpty as List⁺ using (List⁺; _∷_)
17 open import Data.List.Relation.Binary.Equality.Propositional as ≋
18 open import Data.Nat.Base using (ℕ; zero; suc; pred; _+_)
19 open import Data.Nat.GeneralisedArithmetic using (fold; fold-pull)
20 open import Data.Product.Base as Product using (_,_)
21 open import Data.Vec.Base as Vec using (_∷_)
22 open import Function.Base using (id; _$_; _∘′_; const)
23 open import Level using (Level)
24 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_; _≢_)
25 open import Relation.Binary.PropositionalEquality.Properties
26 using (module ≡-Reasoning)
27 open import Size using (Size; ∞)
28
29 private
30 variable
31 a b c : Level
32 A : Set a
33 B : Set b
34 C : Set c
35 i : Size
36
37 ------------------------------------------------------------------------
38 -- repeat
39
40 lookup-repeat-identity : (n : ℕ) (a : A) → lookup (repeat a) n ≡ a
41 lookup-repeat-identity zero a = ≡.refl
42 lookup-repeat-identity (suc n) a = lookup-repeat-identity n a
43
44 take-repeat-identity : (n : ℕ) (a : A) → take n (repeat a) ≡ Vec.replicate n a
45 take-repeat-identity zero a = ≡.refl
46 take-repeat-identity (suc n) a = ≡.cong (a Vec.∷_) (take-repeat-identity n a)
47
48 splitAt-repeat-identity : (n : ℕ) (a : A) → splitAt n (repeat a) ≡ (Vec.replicate n a , repeat a)
49 splitAt-repeat-identity zero a = ≡.refl
50 splitAt-repeat-identity (suc n) a = ≡.cong (Product.map₁ (a ∷_)) (splitAt-repeat-identity n a)
51
52 replicate-repeat : ∀ {i} (n : ℕ) (a : A) → i ⊢ List.replicate n a ++ repeat a ≈ repeat a
53 replicate-repeat zero a = refl
54 replicate-repeat (suc n) a = ≡.refl ∷ λ where .force → replicate-repeat n a
55
56 cycle-replicate : ∀ {i} (n : ℕ) (n≢0 : n ≢ 0) (a : A) → i ⊢ cycle (List⁺.replicate n n≢0 a) ≈ repeat a
57 cycle-replicate {i} n n≢0 a = let as = List⁺.replicate n n≢0 a in begin
58 cycle as ≡⟨⟩
59 as ⁺++ _ ≈⟨ ⁺++⁺ ≋.≋-refl (λ where .force → cycle-replicate n n≢0 a) ⟩
60 as ⁺++ (λ where .force → repeat a) ≈⟨ ≡.refl ∷ (λ where .force → replicate-repeat (pred n) a) ⟩
61 repeat a ∎ where open ≈-Reasoning
62
63 module _ {a b} {A : Set a} {B : Set b} where
64
65 map-repeat : ∀ (f : A → B) a {i} → i ⊢ map f (repeat a) ≈ repeat (f a)
66 map-repeat f a = ≡.refl ∷ λ where .force → map-repeat f a
67
68 ap-repeat : ∀ (f : A → B) a {i} → i ⊢ ap (repeat f) (repeat a) ≈ repeat (f a)
69 ap-repeat f a = ≡.refl ∷ λ where .force → ap-repeat f a
70
71 ap-repeatˡ : ∀ (f : A → B) as {i} → i ⊢ ap (repeat f) as ≈ map f as
72 ap-repeatˡ f (a ∷ as) = ≡.refl ∷ λ where .force → ap-repeatˡ f (as .force)
73
74 ap-repeatʳ : ∀ (fs : Stream (A → B) ∞) (a : A) {i} → i ⊢ ap fs (repeat a) ≈ map (_$ a) fs
75 ap-repeatʳ (f ∷ fs) a = ≡.refl ∷ λ where .force → ap-repeatʳ (fs .force) a
76
77 map-++ : ∀ {i} (f : A → B) as xs → i ⊢ map f (as ++ xs) ≈ List.map f as ++ map f xs
78 map-++ f [] xs = refl
79 map-++ f (a ∷ as) xs = ≡.refl ∷ λ where .force → map-++ f as xs
80
81 map-⁺++ : ∀ {i} (f : A → B) as xs → i ⊢ map f (as ⁺++ xs) ≈ List⁺.map f as ⁺++ Thunk.map (map f) xs
82 map-⁺++ f (a ∷ as) xs = ≡.refl ∷ (λ where .force → map-++ f as (xs .force))
83
84 map-cycle : ∀ {i} (f : A → B) as → i ⊢ map f (cycle as) ≈ cycle (List⁺.map f as)
85 map-cycle f as = begin
86 map f (cycle as) ≈⟨ map-⁺++ f as _ ⟩
87 List⁺.map f as ⁺++ _ ≈⟨ ⁺++⁺ ≋.≋-refl (λ where .force → map-cycle f as) ⟩
88 cycle (List⁺.map f as) ∎ where open ≈-Reasoning
89
90 ------------------------------------------------------------------------
91 -- Functor laws
92
93 map-id : ∀ (as : Stream A ∞) → i ⊢ map id as ≈ as
94 map-id (a ∷ as) = ≡.refl ∷ λ where .force → map-id (as .force)
95
96 map-∘ : ∀ (f : A → B) (g : B → C) as → i ⊢ map g (map f as) ≈ map (g ∘′ f) as
97 map-∘ f g (a ∷ as) = ≡.refl ∷ λ where .force → map-∘ f g (as .force)
98
99
100 ------------------------------------------------------------------------
101 -- splitAt
102
103 splitAt-map : ∀ n (f : A → B) xs →
104 splitAt n (map f xs) ≡ Product.map (Vec.map f) (map f) (splitAt n xs)
105 splitAt-map zero f xs = ≡.refl
106 splitAt-map (suc n) f (x ∷ xs) =
107 ≡.cong (Product.map₁ (f x Vec.∷_)) (splitAt-map n f (xs .force))
108
109 ------------------------------------------------------------------------
110 -- iterate
111
112 lookup-iterate-identity : ∀ n f (a : A) → lookup (iterate f a) n ≡ fold a f n
113 lookup-iterate-identity zero f a = ≡.refl
114 lookup-iterate-identity (suc n) f a = begin
115 lookup (iterate f a) (suc n) ≡⟨⟩
116 lookup (iterate f (f a)) n ≡⟨ lookup-iterate-identity n f (f a) ⟩
117 fold (f a) f n ≡⟨ fold-pull a f (const ∘′ f) (f a) ≡.refl (λ _ → ≡.refl) n ⟩
118 f (fold a f n) ≡⟨⟩
119 fold a f (suc n) ∎ where open ≡-Reasoning
120
121 ------------------------------------------------------------------------
122 -- DEPRECATED
123 ------------------------------------------------------------------------
124 -- Please use the new names as continuing support for the old names is
125 -- not guaranteed.
126
127 -- Version 2.0
128
129 map-identity = map-id
130 {-# WARNING_ON_USAGE map-identity
131 "Warning: map-identity was deprecated in v2.0.
132 Please use map-id instead."
133 #-}
134
135 map-map-fusion = map-∘
136 {-# WARNING_ON_USAGE map-map-fusion
137 "Warning: map-map-fusion was deprecated in v2.0.
138 Please use map-∘ instead."
139 #-}