Debian Codegraph @

src/Codata/Guarded/Stream/Relation/Binary/Pointwise.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Coinductive pointwise lifting of relations to streams
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --safe --cubical-compatible --guardedness #-}
8
9 module Codata.Guarded.Stream.Relation.Binary.Pointwise where
10
11 open import Codata.Guarded.Stream as Stream using (Stream; head; tail)
12 open import Data.Nat.Base using (ℕ; zero; suc)
13 open import Function.Base using (_∘_; _on_)
14 open import Level using (Level; _⊔_)
15 open import Relation.Binary.Core using (REL; _⇒_)
16 open import Relation.Binary.Bundles using (Setoid)
17 open import Relation.Binary.Definitions
18 using (Reflexive; Sym; Trans; Antisym; Symmetric; Transitive)
19 open import Relation.Binary.Structures using (IsEquivalence)
20 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
21 import Relation.Binary.PropositionalEquality.Properties as ≡ using (setoid)
22
23 private
24 variable
25 a ℓ : Level
26 A B C D : Set a
27 R S T : REL A B ℓ
28 xs ys : Stream A
29
30 ------------------------------------------------------------------------
31 -- Bisimilarity
32
33 infixr 5 _∷_
34
35 record Pointwise (_∼_ : REL A B ℓ) (as : Stream A) (bs : Stream B) : Set ℓ where
36 coinductive
37 constructor _∷_
38 field
39 head : head as ∼ head bs
40 tail : Pointwise _∼_ (tail as) (tail bs)
41
42 open Pointwise public
43
44 lookup⁺ : ∀ {as bs} → Pointwise R as bs →
45 ∀ n → R (Stream.lookup as n) (Stream.lookup bs n)
46 lookup⁺ rs zero = rs .head
47 lookup⁺ rs (suc n) = lookup⁺ (rs .tail) n
48
49 map : R ⇒ S → Pointwise R ⇒ Pointwise S
50 head (map R⇒S xs) = R⇒S (head xs)
51 tail (map R⇒S xs) = map R⇒S (tail xs)
52
53 reflexive : Reflexive R → Reflexive (Pointwise R)
54 head (reflexive R-refl) = R-refl
55 tail (reflexive R-refl) = reflexive R-refl
56
57 symmetric : Sym R S → Sym (Pointwise R) (Pointwise S)
58 head (symmetric R-sym-S xsRys) = R-sym-S (head xsRys)
59 tail (symmetric R-sym-S xsRys) = symmetric R-sym-S (tail xsRys)
60
61 transitive : Trans R S T → Trans (Pointwise R) (Pointwise S) (Pointwise T)
62 head (transitive RST-trans xsRys ysSzs) = RST-trans (head xsRys) (head ysSzs)
63 tail (transitive RST-trans xsRys ysSzs) = transitive RST-trans (tail xsRys) (tail ysSzs)
64
65 isEquivalence : IsEquivalence R → IsEquivalence (Pointwise R)
66 isEquivalence equiv^R = record
67 { refl = reflexive equiv^R.refl
68 ; sym = symmetric equiv^R.sym
69 ; trans = transitive equiv^R.trans
70 } where module equiv^R = IsEquivalence equiv^R
71
72 setoid : Setoid a ℓ → Setoid a ℓ
73 setoid S = record
74 { isEquivalence = isEquivalence (Setoid.isEquivalence S)
75 }
76
77 antisymmetric : Antisym R S T → Antisym (Pointwise R) (Pointwise S) (Pointwise T)
78 head (antisymmetric RST-antisym xsRys ysSxs) = RST-antisym (head xsRys) (head ysSxs)
79 tail (antisymmetric RST-antisym xsRys ysSxs) = antisymmetric RST-antisym (tail xsRys) (tail ysSxs)
80
81 tabulate⁺ : ∀ {f : ℕ → A} {g : ℕ → B} →
82 (∀ i → R (f i) (g i)) → Pointwise R (Stream.tabulate f) (Stream.tabulate g)
83 head (tabulate⁺ f∼g) = f∼g 0
84 tail (tabulate⁺ f∼g) = tabulate⁺ (f∼g ∘ suc)
85
86 tabulate⁻ : ∀ {f : ℕ → A} {g : ℕ → B} →
87 Pointwise R (Stream.tabulate f) (Stream.tabulate g) → (∀ i → R (f i) (g i))
88 tabulate⁻ xsRys zero = head xsRys
89 tabulate⁻ xsRys (suc i) = tabulate⁻ (tail xsRys) i
90
91 map⁺ : ∀ (f : A → C) (g : B → D) →
92 Pointwise (λ a b → R (f a) (g b)) xs ys →
93 Pointwise R (Stream.map f xs) (Stream.map g ys)
94 head (map⁺ f g faRgb) = head faRgb
95 tail (map⁺ f g faRgb) = map⁺ f g (tail faRgb)
96
97 map⁻ : ∀ (f : A → C) (g : B → D) →
98 Pointwise R (Stream.map f xs) (Stream.map g ys) →
99 Pointwise (λ a b → R (f a) (g b)) xs ys
100 head (map⁻ f g faRgb) = head faRgb
101 tail (map⁻ f g faRgb) = map⁻ f g (tail faRgb)
102
103 drop⁺ : ∀ n → Pointwise R ⇒ (Pointwise R on Stream.drop n)
104 drop⁺ zero as≈bs = as≈bs
105 drop⁺ (suc n) as≈bs = drop⁺ n (as≈bs .tail)
106
107 ------------------------------------------------------------------------
108 -- Pointwise Equality as a Bisimilarity
109
110 module _ {A : Set a} where
111
112 infix 1 _≈_
113 _≈_ : Stream A → Stream A → Set a
114 _≈_ = Pointwise _≡_
115
116 refl : Reflexive _≈_
117 refl = reflexive ≡.refl
118
119 sym : Symmetric _≈_
120 sym = symmetric ≡.sym
121
122 trans : Transitive _≈_
123 trans = transitive ≡.trans
124
125 ≈-setoid : Setoid _ _
126 ≈-setoid = setoid (≡.setoid A)
127
128 ------------------------------------------------------------------------
129 -- Pointwise DSL
130 --
131 -- A guardedness check does not play well with compositional proofs.
132 -- Luckily we can learn from Nils Anders Danielsson's
133 -- Beating the Productivity Checker Using Embedded Languages
134 -- and design a little compositional DSL to define such proofs
135 --
136 -- NOTE: also because of the guardedness check we can't use the standard
137 -- `Relation.Binary.Reasoning.Syntax` approach.
138
139 module pw-Reasoning (S : Setoid a ℓ) where
140 private module S = Setoid S
141 open S using (Carrier) renaming (_≈_ to _∼_)
142
143 record `Pointwise∞ (as bs : Stream Carrier) : Set (a ⊔ ℓ)
144 data `Pointwise (as bs : Stream Carrier) : Set (a ⊔ ℓ)
145
146 record `Pointwise∞ as bs where
147 coinductive
148 field
149 head : (as .head) ∼ (bs .head)
150 tail : `Pointwise (as .tail) (bs .tail)
151
152 data `Pointwise as bs where
153 `lift : Pointwise _∼_ as bs → `Pointwise as bs
154 `step : `Pointwise∞ as bs → `Pointwise as bs
155 `refl : as ≡ bs → `Pointwise as bs
156 `bisim : as ≈ bs → `Pointwise as bs
157 `sym : `Pointwise bs as → `Pointwise as bs
158 `trans : ∀ {ms} → `Pointwise as ms → `Pointwise ms bs → `Pointwise as bs
159
160 open `Pointwise∞ public
161
162 `head : ∀ {as bs} → `Pointwise as bs → as .head ∼ bs .head
163 `head (`lift rs) = rs .head
164 `head (`refl eq) = S.reflexive (≡.cong head eq)
165 `head (`bisim rs) = S.reflexive (rs .head)
166 `head (`step `rs) = `rs .head
167 `head (`sym `rs) = S.sym (`head `rs)
168 `head (`trans `rs₁ `rs₂) = S.trans (`head `rs₁) (`head `rs₂)
169
170 `tail : ∀ {as bs} → `Pointwise as bs → `Pointwise (as .tail) (bs .tail)
171 `tail (`lift rs) = `lift (rs .tail)
172 `tail (`refl eq) = `refl (≡.cong tail eq)
173 `tail (`bisim rs) = `bisim (rs .tail)
174 `tail (`step `rs) = `rs .tail
175 `tail (`sym `rs) = `sym (`tail `rs)
176 `tail (`trans `rs₁ `rs₂) = `trans (`tail `rs₁) (`tail `rs₂)
177
178 run : ∀ {as bs} → `Pointwise as bs → Pointwise _∼_ as bs
179 run `rs .head = `head `rs
180 run `rs .tail = run (`tail `rs)
181
182 infix 1 begin_
183 infixr 2 _↺⟨_⟩_ _↺⟨_⟨_ _∼⟨_⟩_ _∼⟨_⟨_ _≈⟨_⟩_ _≈⟨_⟨_ _≡⟨_⟩_ _≡⟨_⟨_ _≡⟨⟩_
184 infix 3 _∎
185
186 -- Beginning of a proof
187 begin_ : ∀ {as bs} → `Pointwise∞ as bs → Pointwise _∼_ as bs
188 (begin `rs) .head = `rs .head
189 (begin `rs) .tail = run (`rs .tail)
190
191 pattern _↺⟨_⟩_ as as∼bs bs∼cs = `trans {as = as} (`step as∼bs) bs∼cs
192 pattern _↺⟨_⟨_ as bs∼as bs∼cs = `trans {as = as} (`sym (`step bs∼as)) bs∼cs
193 pattern _∼⟨_⟩_ as as∼bs bs∼cs = `trans {as = as} (`lift as∼bs) bs∼cs
194 pattern _∼⟨_⟨_ as bs∼as bs∼cs = `trans {as = as} (`sym (`lift bs∼as)) bs∼cs
195 pattern _≈⟨_⟩_ as as∼bs bs∼cs = `trans {as = as} (`bisim as∼bs) bs∼cs
196 pattern _≈⟨_⟨_ as bs∼as bs∼cs = `trans {as = as} (`sym (`bisim bs∼as)) bs∼cs
197 pattern _≡⟨_⟩_ as as∼bs bs∼cs = `trans {as = as} (`refl as∼bs) bs∼cs
198 pattern _≡⟨_⟨_ as bs∼as bs∼cs = `trans {as = as} (`sym (`refl bs∼as)) bs∼cs
199 pattern _≡⟨⟩_ as as∼bs = `trans {as = as} (`refl ≡.refl) as∼bs
200 pattern _∎ as = `refl {as = as} ≡.refl
201
202
203 -- Deprecated v2.0
204 infixr 2 _↺˘⟨_⟩_ _∼˘⟨_⟩_ _≈˘⟨_⟩_ _≡˘⟨_⟩_
205 pattern _↺˘⟨_⟩_ as bs∼as bs∼cs = `trans {as = as} (`sym (`step bs∼as)) bs∼cs
206 pattern _∼˘⟨_⟩_ as bs∼as bs∼cs = `trans {as = as} (`sym (`lift bs∼as)) bs∼cs
207 pattern _≈˘⟨_⟩_ as bs∼as bs∼cs = `trans {as = as} (`sym (`bisim bs∼as)) bs∼cs
208 pattern _≡˘⟨_⟩_ as bs∼as bs∼cs = `trans {as = as} (`sym (`refl bs∼as)) bs∼cs
209 {-# WARNING_ON_USAGE _↺˘⟨_⟩_
210 "Warning: _↺˘⟨_⟩_ was deprecated in v2.0.
211 Please use _↺⟨_⟨_ instead."
212 #-}
213 {-# WARNING_ON_USAGE _∼˘⟨_⟩_
214 "Warning: _∼˘⟨_⟩_ was deprecated in v2.0.
215 Please use _∼⟨_⟨_ instead."
216 #-}
217 {-# WARNING_ON_USAGE _≈˘⟨_⟩_
218 "Warning: _≈˘⟨_⟩_ was deprecated in v2.0.
219 Please use _≈⟨_⟨_ instead."
220 #-}
221 {-# WARNING_ON_USAGE _≡˘⟨_⟩_
222 "Warning: _≡˘⟨_⟩_ was deprecated in v2.0.
223 Please use _≡⟨_⟨_ instead."
224 #-}
225
226
227 module ≈-Reasoning {a} {A : Set a} where
228
229 open pw-Reasoning (≡.setoid A) public
230
231 infix 4 _≈∞_
232 _≈∞_ = `Pointwise∞