| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- The partiality monad |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe --guardedness #-} |
| 8 |
|
| 9 |
module Effect.Monad.Partiality where |
| 10 |
|
| 11 |
open import Codata.Musical.Notation using (∞; ♯_; ♭) |
| 12 |
open import Data.Bool.Base using (Bool; false; true) |
| 13 |
open import Data.Nat.Base using (ℕ; zero; suc; _+_) |
| 14 |
open import Data.Product as Prod using (∃; ∄; -,_; ∃₂; _,_; _×_) |
| 15 |
open import Data.Sum.Base using (_⊎_; inj₁; inj₂) |
| 16 |
open import Effect.Functor using (RawFunctor) |
| 17 |
open import Effect.Applicative using (RawApplicative) |
| 18 |
open import Effect.Monad using (RawMonad; module Join) |
| 19 |
open import Function.Base using (_∘′_; flip; id; _∘_; _$_; _⟨_⟩_) |
| 20 |
open import Function.Bundles using (_⇔_; mk⇔) |
| 21 |
open import Level using (Level; _⊔_) |
| 22 |
open import Relation.Binary.Core as B hiding (Rel; _⇔_) |
| 23 |
open import Relation.Binary.Definitions |
| 24 |
using (DecidableEquality; Reflexive; Symmetric; Transitive) |
| 25 |
open import Relation.Binary.Structures |
| 26 |
using (IsPreorder; IsEquivalence) |
| 27 |
open import Relation.Binary.Bundles |
| 28 |
using (Preorder; Setoid; Poset) |
| 29 |
import Relation.Binary.Properties.Setoid as SetoidProperties |
| 30 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 31 |
import Relation.Binary.PropositionalEquality.Properties as ≡ |
| 32 |
open import Relation.Nullary.Decidable using (yes; no; False; Dec; ¬¬-excluded-middle) |
| 33 |
open import Relation.Nullary.Negation using (¬_; ¬¬-Monad) |
| 34 |
|
| 35 |
private |
| 36 |
variable |
| 37 |
a b c f s ℓ : Level |
| 38 |
A : Set a |
| 39 |
B : Set b |
| 40 |
C : Set c |
| 41 |
|
| 42 |
------------------------------------------------------------------------ |
| 43 |
-- The partiality monad |
| 44 |
|
| 45 |
data _⊥ (A : Set a) : Set a where |
| 46 |
now : (x : A) → A ⊥ |
| 47 |
later : (x : ∞ (A ⊥)) → A ⊥ |
| 48 |
|
| 49 |
bind : A ⊥ → (A → B ⊥) → B ⊥ |
| 50 |
bind (now x) f = f x |
| 51 |
bind (later x) f = later (♯ (bind (♭ x) f)) |
| 52 |
|
| 53 |
functor : RawFunctor {ℓ} _⊥ |
| 54 |
functor = record { _<$>_ = map } where |
| 55 |
|
| 56 |
map : (A → B) → A ⊥ → B ⊥ |
| 57 |
map f (now a) = now (f a) |
| 58 |
map f (later d) = later (♯ map f (♭ d)) |
| 59 |
|
| 60 |
|
| 61 |
applicative : RawApplicative {f = f} _⊥ |
| 62 |
applicative = record |
| 63 |
{ rawFunctor = functor |
| 64 |
; pure = now |
| 65 |
; _<*>_ = λ mf mx → bind mf (λ f → bind mx (now ∘′ f)) |
| 66 |
} |
| 67 |
|
| 68 |
monad : RawMonad {f = f} _⊥ |
| 69 |
monad = record |
| 70 |
{ rawApplicative = applicative |
| 71 |
; _>>=_ = bind |
| 72 |
} |
| 73 |
|
| 74 |
join : (A ⊥) ⊥ → A ⊥ |
| 75 |
join = Join.join monad |
| 76 |
|
| 77 |
private module M {f} = RawMonad (monad {f}) |
| 78 |
|
| 79 |
-- Non-termination. |
| 80 |
|
| 81 |
never : A ⊥ |
| 82 |
never = later (♯ never) |
| 83 |
|
| 84 |
-- run x for n steps peels off at most n "later" constructors from x. |
| 85 |
|
| 86 |
run_for_steps : A ⊥ → ℕ → A ⊥ |
| 87 |
run now x for n steps = now x |
| 88 |
run later x for zero steps = later x |
| 89 |
run later x for suc n steps = run ♭ x for n steps |
| 90 |
|
| 91 |
-- Is the computation done? |
| 92 |
|
| 93 |
isNow : A ⊥ → Bool |
| 94 |
isNow (now x) = true |
| 95 |
isNow (later x) = false |
| 96 |
|
| 97 |
------------------------------------------------------------------------ |
| 98 |
-- Kinds |
| 99 |
|
| 100 |
-- The partiality monad comes with two forms of equality (weak and |
| 101 |
-- strong) and one ordering. Strong equality is stronger than the |
| 102 |
-- ordering, which is stronger than weak equality. |
| 103 |
|
| 104 |
-- The three relations are defined using a single data type, indexed |
| 105 |
-- by a "kind". |
| 106 |
|
| 107 |
data OtherKind : Set where |
| 108 |
geq weak : OtherKind |
| 109 |
|
| 110 |
data Kind : Set where |
| 111 |
strong : Kind |
| 112 |
other : (k : OtherKind) → Kind |
| 113 |
|
| 114 |
-- Kind equality is decidable. |
| 115 |
|
| 116 |
infix 4 _≟-Kind_ |
| 117 |
|
| 118 |
_≟-Kind_ : DecidableEquality Kind |
| 119 |
_≟-Kind_ strong strong = yes ≡.refl |
| 120 |
_≟-Kind_ strong (other k) = no λ() |
| 121 |
_≟-Kind_ (other k) strong = no λ() |
| 122 |
_≟-Kind_ (other geq) (other geq) = yes ≡.refl |
| 123 |
_≟-Kind_ (other geq) (other weak) = no λ() |
| 124 |
_≟-Kind_ (other weak) (other geq) = no λ() |
| 125 |
_≟-Kind_ (other weak) (other weak) = yes ≡.refl |
| 126 |
|
| 127 |
-- A predicate which is satisfied only for equalities. Note that, for |
| 128 |
-- concrete inputs, this predicate evaluates to ⊤ or ⊥. |
| 129 |
|
| 130 |
Equality : Kind → Set |
| 131 |
Equality k = False (k ≟-Kind other geq) |
| 132 |
|
| 133 |
------------------------------------------------------------------------ |
| 134 |
-- Equality/ordering |
| 135 |
|
| 136 |
module Equality {A : Set a} -- The "return type". |
| 137 |
(_∼_ : A → A → Set ℓ) where |
| 138 |
|
| 139 |
-- The three relations. |
| 140 |
|
| 141 |
data Rel : Kind → A ⊥ → A ⊥ → Set (a ⊔ ℓ) where |
| 142 |
now : ∀ {k x y} (x∼y : x ∼ y) → Rel k (now x) (now y) |
| 143 |
later : ∀ {k x y} (x∼y : ∞ (Rel k (♭ x) (♭ y))) → Rel k (later x) (later y) |
| 144 |
laterʳ : ∀ {x y} (x≈y : Rel (other weak) x (♭ y) ) → Rel (other weak) x (later y) |
| 145 |
laterˡ : ∀ {k x y} (x∼y : Rel (other k) (♭ x) y ) → Rel (other k) (later x) y |
| 146 |
|
| 147 |
infix 4 _≅_ _≳_ _≲_ _≈_ |
| 148 |
|
| 149 |
_≅_ : A ⊥ → A ⊥ → Set _ |
| 150 |
_≅_ = Rel strong |
| 151 |
|
| 152 |
_≳_ : A ⊥ → A ⊥ → Set _ |
| 153 |
_≳_ = Rel (other geq) |
| 154 |
|
| 155 |
_≲_ : A ⊥ → A ⊥ → Set _ |
| 156 |
_≲_ = flip _≳_ |
| 157 |
|
| 158 |
_≈_ : A ⊥ → A ⊥ → Set _ |
| 159 |
_≈_ = Rel (other weak) |
| 160 |
|
| 161 |
-- x ⇓ y means that x terminates with y. |
| 162 |
|
| 163 |
infix 4 _⇓[_]_ _⇓_ |
| 164 |
|
| 165 |
_⇓[_]_ : A ⊥ → Kind → A → Set _ |
| 166 |
x ⇓[ k ] y = Rel k x (now y) |
| 167 |
|
| 168 |
_⇓_ : A ⊥ → A → Set _ |
| 169 |
x ⇓ y = x ⇓[ other weak ] y |
| 170 |
|
| 171 |
-- x ⇓ means that x terminates. |
| 172 |
|
| 173 |
infix 4 _⇓ |
| 174 |
|
| 175 |
_⇓ : A ⊥ → Set _ |
| 176 |
x ⇓ = ∃ λ v → x ⇓ v |
| 177 |
|
| 178 |
-- x ⇑ means that x does not terminate. |
| 179 |
|
| 180 |
infix 4 _⇑[_] _⇑ |
| 181 |
|
| 182 |
_⇑[_] : A ⊥ → Kind → Set _ |
| 183 |
x ⇑[ k ] = Rel k x never |
| 184 |
|
| 185 |
_⇑ : A ⊥ → Set _ |
| 186 |
x ⇑ = x ⇑[ other weak ] |
| 187 |
|
| 188 |
------------------------------------------------------------------------ |
| 189 |
-- Lemmas relating the three relations |
| 190 |
|
| 191 |
module _ {A : Set a} {_∼_ : A → A → Set ℓ} where |
| 192 |
|
| 193 |
open Equality _∼_ using (Rel; _≅_; _≳_; _≲_; _≈_; _⇓[_]_; _⇑[_]) |
| 194 |
open Equality.Rel |
| 195 |
|
| 196 |
-- All relations include strong equality. |
| 197 |
|
| 198 |
≅⇒ : ∀ {k} {x y : A ⊥} → x ≅ y → Rel k x y |
| 199 |
≅⇒ (now x∼y) = now x∼y |
| 200 |
≅⇒ (later x≅y) = later (♯ ≅⇒ (♭ x≅y)) |
| 201 |
|
| 202 |
-- The weak equality includes the ordering. |
| 203 |
|
| 204 |
≳⇒ : ∀ {k} {x y : A ⊥} → x ≳ y → Rel (other k) x y |
| 205 |
≳⇒ (now x∼y) = now x∼y |
| 206 |
≳⇒ (later x≳y) = later (♯ ≳⇒ (♭ x≳y)) |
| 207 |
≳⇒ (laterˡ x≳y) = laterˡ (≳⇒ x≳y ) |
| 208 |
|
| 209 |
-- Weak equality includes the other relations. |
| 210 |
|
| 211 |
⇒≈ : ∀ {k} {x y : A ⊥} → Rel k x y → x ≈ y |
| 212 |
⇒≈ {strong} = ≅⇒ |
| 213 |
⇒≈ {other geq} = ≳⇒ |
| 214 |
⇒≈ {other weak} = id |
| 215 |
|
| 216 |
-- The relations agree for non-terminating computations. |
| 217 |
|
| 218 |
never⇒never : ∀ {k₁ k₂} {x : A ⊥} → |
| 219 |
Rel k₁ x never → Rel k₂ x never |
| 220 |
never⇒never (later x∼never) = later (♯ never⇒never (♭ x∼never)) |
| 221 |
never⇒never (laterʳ x≈never) = never⇒never x≈never |
| 222 |
never⇒never (laterˡ x∼never) = later (♯ never⇒never x∼never) |
| 223 |
|
| 224 |
-- The "other" relations agree when the right-hand side is a value. |
| 225 |
|
| 226 |
now⇒now : ∀ {k₁ k₂} {x} {y : A} → |
| 227 |
Rel (other k₁) x (now y) → Rel (other k₂) x (now y) |
| 228 |
now⇒now (now x∼y) = now x∼y |
| 229 |
now⇒now (laterˡ x∼now) = laterˡ (now⇒now x∼now) |
| 230 |
|
| 231 |
------------------------------------------------------------------------ |
| 232 |
-- Later can be dropped |
| 233 |
|
| 234 |
laterʳ⁻¹ : ∀ {k} {x : A ⊥} {y} → |
| 235 |
Rel (other k) x (later y) → Rel (other k) x (♭ y) |
| 236 |
laterʳ⁻¹ (later x∼y) = laterˡ (♭ x∼y) |
| 237 |
laterʳ⁻¹ (laterʳ x≈y) = x≈y |
| 238 |
laterʳ⁻¹ (laterˡ x∼ly) = laterˡ (laterʳ⁻¹ x∼ly) |
| 239 |
|
| 240 |
laterˡ⁻¹ : ∀ {x} {y : A ⊥} → later x ≈ y → ♭ x ≈ y |
| 241 |
laterˡ⁻¹ (later x≈y) = laterʳ (♭ x≈y) |
| 242 |
laterˡ⁻¹ (laterʳ lx≈y) = laterʳ (laterˡ⁻¹ lx≈y) |
| 243 |
laterˡ⁻¹ (laterˡ x≈y) = x≈y |
| 244 |
|
| 245 |
later⁻¹ : ∀ {k} {x y : ∞ (A ⊥)} → |
| 246 |
Rel k (later x) (later y) → Rel k (♭ x) (♭ y) |
| 247 |
later⁻¹ (later x∼y) = ♭ x∼y |
| 248 |
later⁻¹ (laterʳ lx≈y) = laterˡ⁻¹ lx≈y |
| 249 |
later⁻¹ (laterˡ x∼ly) = laterʳ⁻¹ x∼ly |
| 250 |
|
| 251 |
------------------------------------------------------------------------ |
| 252 |
-- The relations are equivalences or partial orders, given suitable |
| 253 |
-- assumptions about the underlying relation |
| 254 |
|
| 255 |
module Equivalence where |
| 256 |
|
| 257 |
-- Reflexivity. |
| 258 |
|
| 259 |
refl : Reflexive _∼_ → ∀ {k} → Reflexive (Rel k) |
| 260 |
refl refl-∼ {x = now v} = now refl-∼ |
| 261 |
refl refl-∼ {x = later x} = later (♯ refl refl-∼) |
| 262 |
|
| 263 |
-- Symmetry. |
| 264 |
|
| 265 |
sym : Symmetric _∼_ → ∀ {k} → Equality k → Symmetric (Rel k) |
| 266 |
sym sym-∼ eq (now x∼y) = now (sym-∼ x∼y) |
| 267 |
sym sym-∼ eq (later x∼y) = later (♯ sym sym-∼ eq (♭ x∼y)) |
| 268 |
sym sym-∼ eq (laterʳ x≈y) = laterˡ (sym sym-∼ eq x≈y ) |
| 269 |
sym sym-∼ eq (laterˡ {weak} x≈y) = laterʳ (sym sym-∼ eq x≈y ) |
| 270 |
|
| 271 |
-- Transitivity. |
| 272 |
|
| 273 |
private |
| 274 |
module Trans (trans-∼ : Transitive _∼_) where |
| 275 |
|
| 276 |
now-trans : ∀ {k x y} {v : A} → |
| 277 |
Rel k x y → Rel k y (now v) → Rel k x (now v) |
| 278 |
now-trans (now x∼y) (now y∼z) = now (trans-∼ x∼y y∼z) |
| 279 |
now-trans (laterˡ x∼y) y∼z = laterˡ (now-trans x∼y y∼z) |
| 280 |
now-trans x∼ly (laterˡ y∼z) = now-trans (laterʳ⁻¹ x∼ly) y∼z |
| 281 |
|
| 282 |
mutual |
| 283 |
|
| 284 |
later-trans : ∀ {k} {x y : A ⊥} {z} → |
| 285 |
Rel k x y → Rel k y (later z) → Rel k x (later z) |
| 286 |
later-trans (later x∼y) ly∼lz = later (♯ trans (♭ x∼y) (later⁻¹ ly∼lz)) |
| 287 |
later-trans (laterˡ x∼y) y∼lz = later (♯ trans x∼y (laterʳ⁻¹ y∼lz)) |
| 288 |
later-trans (laterʳ x≈y) ly≈lz = later-trans x≈y (laterˡ⁻¹ ly≈lz) |
| 289 |
later-trans x≈y (laterʳ y≈z) = laterʳ ( trans x≈y y≈z ) |
| 290 |
|
| 291 |
trans : ∀ {k} {x y z : A ⊥} → Rel k x y → Rel k y z → Rel k x z |
| 292 |
trans {z = now v} x∼y y∼v = now-trans x∼y y∼v |
| 293 |
trans {z = later z} x∼y y∼lz = later-trans x∼y y∼lz |
| 294 |
|
| 295 |
open Trans public using (trans) |
| 296 |
|
| 297 |
-- All the relations are preorders. |
| 298 |
|
| 299 |
preorder : IsPreorder _≡_ _∼_ → Kind → Preorder _ _ _ |
| 300 |
preorder pre k = record |
| 301 |
{ Carrier = A ⊥ |
| 302 |
; _≈_ = _≡_ |
| 303 |
; _≲_ = Rel k |
| 304 |
; isPreorder = record |
| 305 |
{ isEquivalence = ≡.isEquivalence |
| 306 |
; reflexive = refl′ |
| 307 |
; trans = Equivalence.trans (IsPreorder.trans pre) |
| 308 |
} |
| 309 |
} |
| 310 |
where |
| 311 |
refl′ : ∀ {k} {x y : A ⊥} → x ≡ y → Rel k x y |
| 312 |
refl′ ≡.refl = Equivalence.refl (IsPreorder.refl pre) |
| 313 |
|
| 314 |
private |
| 315 |
preorder′ : IsEquivalence _∼_ → Kind → Preorder _ _ _ |
| 316 |
preorder′ equiv = |
| 317 |
preorder (SetoidProperties.isPreorder (record { isEquivalence = equiv })) |
| 318 |
|
| 319 |
-- The two equalities are equivalence relations. |
| 320 |
|
| 321 |
setoid : IsEquivalence _∼_ → |
| 322 |
(k : Kind) {eq : Equality k} → Setoid _ _ |
| 323 |
setoid equiv k {eq} = record |
| 324 |
{ Carrier = A ⊥ |
| 325 |
; _≈_ = Rel k |
| 326 |
; isEquivalence = record |
| 327 |
{ refl = Pre.refl |
| 328 |
; sym = Equivalence.sym (IsEquivalence.sym equiv) eq |
| 329 |
; trans = Pre.trans |
| 330 |
} |
| 331 |
} where module Pre = Preorder (preorder′ equiv k) |
| 332 |
|
| 333 |
-- The order is a partial order, with strong equality as the |
| 334 |
-- underlying equality. |
| 335 |
|
| 336 |
≳-poset : IsEquivalence _∼_ → Poset _ _ _ |
| 337 |
≳-poset equiv = record |
| 338 |
{ Carrier = A ⊥ |
| 339 |
; _≈_ = _≅_ |
| 340 |
; _≤_ = _≳_ |
| 341 |
; isPartialOrder = record |
| 342 |
{ antisym = antisym |
| 343 |
; isPreorder = record |
| 344 |
{ isEquivalence = S.isEquivalence |
| 345 |
; reflexive = ≅⇒ |
| 346 |
; trans = Pre.trans |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
where |
| 351 |
module S = Setoid (setoid equiv strong) |
| 352 |
module Pre = Preorder (preorder′ equiv (other geq)) |
| 353 |
|
| 354 |
antisym : {x y : A ⊥} → x ≳ y → x ≲ y → x ≅ y |
| 355 |
antisym (now x∼y) (now _) = now x∼y |
| 356 |
antisym (later x≳y) (later x≲y) = later (♯ antisym (♭ x≳y) (♭ x≲y)) |
| 357 |
antisym (later x≳y) (laterˡ x≲ly) = later (♯ antisym (♭ x≳y) (laterʳ⁻¹ x≲ly)) |
| 358 |
antisym (laterˡ x≳ly) (later x≲y) = later (♯ antisym (laterʳ⁻¹ x≳ly) (♭ x≲y)) |
| 359 |
antisym (laterˡ x≳ly) (laterˡ x≲ly) = later (♯ antisym (laterʳ⁻¹ x≳ly) (laterʳ⁻¹ x≲ly)) |
| 360 |
|
| 361 |
-- Equational reasoning. |
| 362 |
|
| 363 |
module Reasoning (isEquivalence : IsEquivalence _∼_) where |
| 364 |
|
| 365 |
private |
| 366 |
module Pre {k} = Preorder (preorder′ isEquivalence k) |
| 367 |
module S {k eq} = Setoid (setoid isEquivalence k {eq}) |
| 368 |
|
| 369 |
infix 3 _∎ |
| 370 |
infixr 2 _≡⟨_⟩_ _≅⟨_⟩_ _≳⟨_⟩_ _≈⟨_⟩_ |
| 371 |
|
| 372 |
_≡⟨_⟩_ : ∀ {k} x {y z : A ⊥} → x ≡ y → Rel k y z → Rel k x z |
| 373 |
_ ≡⟨ ≡.refl ⟩ y∼z = y∼z |
| 374 |
|
| 375 |
_≅⟨_⟩_ : ∀ {k} x {y z : A ⊥} → x ≅ y → Rel k y z → Rel k x z |
| 376 |
_ ≅⟨ x≅y ⟩ y∼z = Pre.trans (≅⇒ x≅y) y∼z |
| 377 |
|
| 378 |
_≳⟨_⟩_ : ∀ {k} x {y z : A ⊥} → |
| 379 |
x ≳ y → Rel (other k) y z → Rel (other k) x z |
| 380 |
_ ≳⟨ x≳y ⟩ y∼z = Pre.trans (≳⇒ x≳y) y∼z |
| 381 |
|
| 382 |
_≈⟨_⟩_ : ∀ x {y z : A ⊥} → x ≈ y → y ≈ z → x ≈ z |
| 383 |
_ ≈⟨ x≈y ⟩ y≈z = Pre.trans x≈y y≈z |
| 384 |
|
| 385 |
sym : ∀ {k} {eq : Equality k} {x y : A ⊥} → |
| 386 |
Rel k x y → Rel k y x |
| 387 |
sym {eq = eq} = S.sym {eq = eq} |
| 388 |
|
| 389 |
_∎ : ∀ {k} (x : A ⊥) → Rel k x x |
| 390 |
x ∎ = Pre.refl |
| 391 |
|
| 392 |
------------------------------------------------------------------------ |
| 393 |
-- Lemmas related to now and never |
| 394 |
|
| 395 |
-- Now is not never. |
| 396 |
|
| 397 |
now≉never : ∀ {k} {x : A} → ¬ Rel k (now x) never |
| 398 |
now≉never (laterʳ hyp) = now≉never hyp |
| 399 |
|
| 400 |
-- A partial value is either now or never (classically, when the |
| 401 |
-- underlying relation is reflexive). |
| 402 |
|
| 403 |
now-or-never : Reflexive _∼_ → |
| 404 |
∀ {k} (x : A ⊥) → |
| 405 |
¬ ¬ ((∃ λ y → x ⇓[ other k ] y) ⊎ x ⇑[ other k ]) |
| 406 |
now-or-never refl x = helper <$> ¬¬-excluded-middle |
| 407 |
where |
| 408 |
open RawMonad ¬¬-Monad |
| 409 |
|
| 410 |
not-now-is-never : (x : A ⊥) → (∄ λ y → x ≳ now y) → x ≳ never |
| 411 |
not-now-is-never (now x) hyp with hyp (-, now refl) |
| 412 |
... | () |
| 413 |
not-now-is-never (later x) hyp = |
| 414 |
later (♯ not-now-is-never (♭ x) (hyp ∘ Prod.map id laterˡ)) |
| 415 |
|
| 416 |
helper : Dec (∃ λ y → x ≳ now y) → _ |
| 417 |
helper (yes ≳now) = inj₁ $ Prod.map id ≳⇒ ≳now |
| 418 |
helper (no ≵now) = inj₂ $ ≳⇒ $ not-now-is-never x ≵now |
| 419 |
|
| 420 |
------------------------------------------------------------------------ |
| 421 |
-- Map-like results |
| 422 |
|
| 423 |
-- Map. |
| 424 |
|
| 425 |
map : ∀ {_∼′_ : A → A → Set a} {k} → |
| 426 |
_∼′_ ⇒ _∼_ → Equality.Rel _∼′_ k ⇒ Equality.Rel _∼_ k |
| 427 |
map ∼′⇒∼ (now x∼y) = now (∼′⇒∼ x∼y) |
| 428 |
map ∼′⇒∼ (later x∼y) = later (♯ map ∼′⇒∼ (♭ x∼y)) |
| 429 |
map ∼′⇒∼ (laterʳ x≈y) = laterʳ (map ∼′⇒∼ x≈y) |
| 430 |
map ∼′⇒∼ (laterˡ x∼y) = laterˡ (map ∼′⇒∼ x∼y) |
| 431 |
|
| 432 |
-- If a statement can be proved using propositional equality as the |
| 433 |
-- underlying relation, then it can also be proved for any other |
| 434 |
-- reflexive underlying relation. |
| 435 |
|
| 436 |
≡⇒ : Reflexive _∼_ → |
| 437 |
∀ {k x y} → Equality.Rel _≡_ k x y → Rel k x y |
| 438 |
≡⇒ refl-∼ = map (flip (≡.subst (_∼_ _)) refl-∼) |
| 439 |
|
| 440 |
------------------------------------------------------------------------ |
| 441 |
-- Steps |
| 442 |
|
| 443 |
-- The number of later constructors (steps) in the terminating |
| 444 |
-- computation x. |
| 445 |
|
| 446 |
steps : ∀ {k} {x : A ⊥} {y} → x ⇓[ k ] y → ℕ |
| 447 |
steps (now _) = zero |
| 448 |
steps .{x = later x} (laterˡ {x = x} x⇓) = suc (steps {x = ♭ x} x⇓) |
| 449 |
|
| 450 |
module Steps {trans-∼ : Transitive _∼_} where |
| 451 |
|
| 452 |
left-identity : |
| 453 |
∀ {k x y} {z : A} |
| 454 |
(x≅y : x ≅ y) (y⇓z : y ⇓[ k ] z) → |
| 455 |
steps (Equivalence.trans trans-∼ (≅⇒ x≅y) y⇓z) ≡ steps y⇓z |
| 456 |
left-identity (now _) (now _) = ≡.refl |
| 457 |
left-identity (later x≅y) (laterˡ y⇓z) = |
| 458 |
≡.cong suc $ left-identity (♭ x≅y) y⇓z |
| 459 |
|
| 460 |
right-identity : |
| 461 |
∀ {k x} {y z : A} |
| 462 |
(x⇓y : x ⇓[ k ] y) (y≈z : now y ⇓[ k ] z) → |
| 463 |
steps (Equivalence.trans trans-∼ x⇓y y≈z) ≡ steps x⇓y |
| 464 |
right-identity (now x∼y) (now y∼z) = ≡.refl |
| 465 |
right-identity (laterˡ x∼y) (now y∼z) = |
| 466 |
≡.cong suc $ right-identity x∼y (now y∼z) |
| 467 |
|
| 468 |
------------------------------------------------------------------------ |
| 469 |
-- Laws related to bind |
| 470 |
|
| 471 |
-- Never is a left and right "zero" of bind. |
| 472 |
|
| 473 |
left-zero : (f : B → A ⊥) → let open M in |
| 474 |
(never >>= f) ≅ never |
| 475 |
left-zero f = later (♯ left-zero f) |
| 476 |
|
| 477 |
right-zero : (x : B ⊥) → let open M in |
| 478 |
(x >>= λ _ → never) ≅ never |
| 479 |
right-zero (later x) = later (♯ right-zero (♭ x)) |
| 480 |
right-zero (now x) = never≅never |
| 481 |
where never≅never : never ≅ never |
| 482 |
never≅never = later (♯ never≅never) |
| 483 |
|
| 484 |
-- Now is a left and right identity of bind (for a reflexive |
| 485 |
-- underlying relation). |
| 486 |
|
| 487 |
left-identity : Reflexive _∼_ → |
| 488 |
(x : B) (f : B → A ⊥) → let open M in |
| 489 |
(now x >>= f) ≅ f x |
| 490 |
left-identity refl-∼ x f = Equivalence.refl refl-∼ |
| 491 |
|
| 492 |
right-identity : Reflexive _∼_ → |
| 493 |
(x : A ⊥) → let open M in |
| 494 |
(x >>= now) ≅ x |
| 495 |
right-identity refl (now x) = now refl |
| 496 |
right-identity refl (later x) = later (♯ right-identity refl (♭ x)) |
| 497 |
|
| 498 |
-- Bind is associative (for a reflexive underlying relation). |
| 499 |
|
| 500 |
associative : Reflexive _∼_ → |
| 501 |
(x : C ⊥) (f : C → B ⊥) (g : B → A ⊥) → |
| 502 |
let open M in |
| 503 |
(x >>= f >>= g) ≅ (x >>= λ y → f y >>= g) |
| 504 |
associative refl-∼ (now x) f g = Equivalence.refl refl-∼ |
| 505 |
associative refl-∼ (later x) f g = |
| 506 |
later (♯ associative refl-∼ (♭ x) f g) |
| 507 |
|
| 508 |
module _ {A B : Set s} |
| 509 |
{_∼A_ : A → A → Set ℓ} |
| 510 |
{_∼B_ : B → B → Set ℓ} where |
| 511 |
|
| 512 |
open Equality |
| 513 |
private |
| 514 |
open module EqA = Equality _∼A_ using () renaming (_⇓[_]_ to _⇓[_]A_; _⇑[_] to _⇑[_]A) |
| 515 |
open module EqB = Equality _∼B_ using () renaming (_⇓[_]_ to _⇓[_]B_; _⇑[_] to _⇑[_]B) |
| 516 |
|
| 517 |
-- Bind preserves all the relations. |
| 518 |
|
| 519 |
infixl 1 _>>=-cong_ |
| 520 |
|
| 521 |
_>>=-cong_ : |
| 522 |
∀ {k} {x₁ x₂ : A ⊥} {f₁ f₂ : A → B ⊥} → let open M in |
| 523 |
Rel _∼A_ k x₁ x₂ → |
| 524 |
(∀ {x₁ x₂} → x₁ ∼A x₂ → Rel _∼B_ k (f₁ x₁) (f₂ x₂)) → |
| 525 |
Rel _∼B_ k (x₁ >>= f₁) (x₂ >>= f₂) |
| 526 |
now x₁∼x₂ >>=-cong f₁∼f₂ = f₁∼f₂ x₁∼x₂ |
| 527 |
later x₁∼x₂ >>=-cong f₁∼f₂ = later (♯ (♭ x₁∼x₂ >>=-cong f₁∼f₂)) |
| 528 |
laterʳ x₁≈x₂ >>=-cong f₁≈f₂ = laterʳ (x₁≈x₂ >>=-cong f₁≈f₂) |
| 529 |
laterˡ x₁∼x₂ >>=-cong f₁∼f₂ = laterˡ (x₁∼x₂ >>=-cong f₁∼f₂) |
| 530 |
|
| 531 |
-- Inversion lemmas for bind. |
| 532 |
|
| 533 |
>>=-inversion-⇓ : |
| 534 |
Reflexive _∼A_ → |
| 535 |
∀ {k} x {f : A → B ⊥} {y} → let open M in |
| 536 |
(x>>=f⇓ : (x >>= f) ⇓[ k ]B y) → |
| 537 |
∃ λ z → ∃₂ λ (x⇓ : x ⇓[ k ]A z) (fz⇓ : f z ⇓[ k ]B y) → |
| 538 |
steps x⇓ + steps fz⇓ ≡ steps x>>=f⇓ |
| 539 |
>>=-inversion-⇓ refl (now x) fx⇓ = |
| 540 |
(x , now refl , fx⇓ , ≡.refl) |
| 541 |
>>=-inversion-⇓ refl (later x) (laterˡ x>>=f⇓) = |
| 542 |
Prod.map id (Prod.map laterˡ (Prod.map id (≡.cong suc))) $ |
| 543 |
>>=-inversion-⇓ refl (♭ x) x>>=f⇓ |
| 544 |
|
| 545 |
>>=-inversion-⇑ : IsEquivalence _∼A_ → |
| 546 |
∀ {k} x {f : A → B ⊥} → let open M in |
| 547 |
Rel _∼B_ (other k) (x >>= f) never → |
| 548 |
¬ ¬ (x ⇑[ other k ]A ⊎ |
| 549 |
∃ λ y → x ⇓[ other k ]A y × f y ⇑[ other k ]B) |
| 550 |
>>=-inversion-⇑ eqA {k} x {f} ∼never = |
| 551 |
helper <$> now-or-never IsEqA.refl x |
| 552 |
where |
| 553 |
open RawMonad ¬¬-Monad using (_<$>_) |
| 554 |
open M using (_>>=_) |
| 555 |
open Reasoning eqA |
| 556 |
module IsEqA = IsEquivalence eqA |
| 557 |
|
| 558 |
k≳ = other geq |
| 559 |
|
| 560 |
is-never : ∀ {x y} → |
| 561 |
x ⇓[ k≳ ]A y → (x >>= f) ⇑[ k≳ ]B → |
| 562 |
∃ λ z → (y ∼A z) × f z ⇑[ k≳ ]B |
| 563 |
is-never (now x∼y) = λ fx⇑ → (_ , IsEqA.sym x∼y , fx⇑) |
| 564 |
is-never (laterˡ ≳now) = is-never ≳now ∘ later⁻¹ |
| 565 |
|
| 566 |
helper : (∃ λ y → x ⇓[ k≳ ]A y) ⊎ x ⇑[ k≳ ]A → |
| 567 |
x ⇑[ other k ]A ⊎ |
| 568 |
∃ λ y → x ⇓[ other k ]A y × f y ⇑[ other k ]B |
| 569 |
helper (inj₂ ≳never) = inj₁ (≳⇒ ≳never) |
| 570 |
helper (inj₁ (y , ≳now)) with is-never ≳now (never⇒never ∼never) |
| 571 |
... | (z , y∼z , fz⇑) = inj₂ (z , ≳⇒ (x ≳⟨ ≳now ⟩ |
| 572 |
now y ≅⟨ now y∼z ⟩ |
| 573 |
now z ∎) |
| 574 |
, ≳⇒ fz⇑) |
| 575 |
|
| 576 |
module _ {A B : Set ℓ} {_∼_ : B → B → Set ℓ} where |
| 577 |
|
| 578 |
open Equality |
| 579 |
|
| 580 |
-- A variant of _>>=-cong_. |
| 581 |
|
| 582 |
infixl 1 _≡->>=-cong_ |
| 583 |
|
| 584 |
_≡->>=-cong_ : |
| 585 |
∀ {k} {x₁ x₂ : A ⊥} {f₁ f₂ : A → B ⊥} → let open M in |
| 586 |
Rel _≡_ k x₁ x₂ → |
| 587 |
(∀ x → Rel _∼_ k (f₁ x) (f₂ x)) → |
| 588 |
Rel _∼_ k (x₁ >>= f₁) (x₂ >>= f₂) |
| 589 |
_≡->>=-cong_ {k} {f₁ = f₁} {f₂} x₁≈x₂ f₁≈f₂ = |
| 590 |
x₁≈x₂ >>=-cong λ {x} x≡x′ → |
| 591 |
≡.subst (λ y → Rel _∼_ k (f₁ x) (f₂ y)) x≡x′ (f₁≈f₂ x) |
| 592 |
|
| 593 |
------------------------------------------------------------------------ |
| 594 |
-- Productivity checker workaround |
| 595 |
|
| 596 |
-- The monad can be awkward to use, due to the limitations of guarded |
| 597 |
-- coinduction. The following code provides a (limited) workaround. |
| 598 |
|
| 599 |
module Workaround {a} where |
| 600 |
|
| 601 |
infixl 1 _>>=_ |
| 602 |
|
| 603 |
data _⊥P : Set a → Set (Level.suc a) where |
| 604 |
now : (x : A) → A ⊥P |
| 605 |
later : (x : ∞ (A ⊥P)) → A ⊥P |
| 606 |
_>>=_ : (x : A ⊥P) (f : A → B ⊥P) → B ⊥P |
| 607 |
|
| 608 |
private |
| 609 |
|
| 610 |
data _⊥W : Set a → Set (Level.suc a) where |
| 611 |
now : (x : A) → A ⊥W |
| 612 |
later : (x : A ⊥P) → A ⊥W |
| 613 |
|
| 614 |
mutual |
| 615 |
|
| 616 |
_>>=W_ : A ⊥W → (A → B ⊥P) → B ⊥W |
| 617 |
now x >>=W f = whnf (f x) |
| 618 |
later x >>=W f = later (x >>= f) |
| 619 |
|
| 620 |
whnf : A ⊥P → A ⊥W |
| 621 |
whnf (now x) = now x |
| 622 |
whnf (later x) = later (♭ x) |
| 623 |
whnf (x >>= f) = whnf x >>=W f |
| 624 |
|
| 625 |
mutual |
| 626 |
|
| 627 |
private |
| 628 |
|
| 629 |
⟦_⟧W : A ⊥W → A ⊥ |
| 630 |
⟦ now x ⟧W = now x |
| 631 |
⟦ later x ⟧W = later (♯ ⟦ x ⟧P) |
| 632 |
|
| 633 |
⟦_⟧P : A ⊥P → A ⊥ |
| 634 |
⟦ x ⟧P = ⟦ whnf x ⟧W |
| 635 |
|
| 636 |
-- The definitions above make sense. ⟦_⟧P is homomorphic with |
| 637 |
-- respect to now, later and _>>=_. |
| 638 |
|
| 639 |
module Correct where |
| 640 |
|
| 641 |
private |
| 642 |
open module Eq {A : Set a} = Equality {A = A} _≡_ |
| 643 |
open module R {A : Set a} = Reasoning (≡.isEquivalence {A = A}) |
| 644 |
|
| 645 |
now-hom : (x : A) → ⟦ now x ⟧P ≅ now x |
| 646 |
now-hom x = now x ∎ |
| 647 |
|
| 648 |
later-hom : (x : ∞ (A ⊥P)) → ⟦ later x ⟧P ≅ later (♯ ⟦ ♭ x ⟧P) |
| 649 |
later-hom x = later (♯ (⟦ ♭ x ⟧P ∎)) |
| 650 |
|
| 651 |
mutual |
| 652 |
|
| 653 |
private |
| 654 |
|
| 655 |
>>=-homW : (x : B ⊥W) (f : B → A ⊥P) → |
| 656 |
⟦ x >>=W f ⟧W ≅ M._>>=_ ⟦ x ⟧W (λ y → ⟦ f y ⟧P) |
| 657 |
>>=-homW (now x) f = ⟦ f x ⟧P ∎ |
| 658 |
>>=-homW (later x) f = later (♯ >>=-hom x f) |
| 659 |
|
| 660 |
>>=-hom : (x : B ⊥P) (f : B → A ⊥P) → |
| 661 |
⟦ x >>= f ⟧P ≅ M._>>=_ ⟦ x ⟧P (λ y → ⟦ f y ⟧P) |
| 662 |
>>=-hom x f = >>=-homW (whnf x) f |
| 663 |
|
| 664 |
------------------------------------------------------------------------ |
| 665 |
-- An alternative, but equivalent, formulation of equality/ordering |
| 666 |
|
| 667 |
module AlternativeEquality {a ℓ} where |
| 668 |
|
| 669 |
private |
| 670 |
|
| 671 |
El : Setoid a ℓ → Set _ |
| 672 |
El = Setoid.Carrier |
| 673 |
|
| 674 |
Eq : ∀ S → B.Rel (El S) _ |
| 675 |
Eq = Setoid._≈_ |
| 676 |
|
| 677 |
open Equality using (Rel) |
| 678 |
open Equality.Rel |
| 679 |
|
| 680 |
infix 4 _∣_≅P_ _∣_≳P_ _∣_≈P_ |
| 681 |
infix 3 _∎ |
| 682 |
infixr 2 _≡⟨_⟩_ _≅⟨_⟩_ _≳⟨_⟩_ _≳⟨_⟩≅_ _≳⟨_⟩≈_ _≈⟨_⟩≅_ _≈⟨_⟩≲_ |
| 683 |
infixl 1 _>>=_ |
| 684 |
|
| 685 |
mutual |
| 686 |
|
| 687 |
-- Proof "programs". |
| 688 |
|
| 689 |
_∣_≅P_ : ∀ S → B.Rel (El S ⊥) _ |
| 690 |
_∣_≅P_ = flip RelP strong |
| 691 |
|
| 692 |
_∣_≳P_ : ∀ S → B.Rel (El S ⊥) _ |
| 693 |
_∣_≳P_ = flip RelP (other geq) |
| 694 |
|
| 695 |
_∣_≈P_ : ∀ S → B.Rel (El S ⊥) _ |
| 696 |
_∣_≈P_ = flip RelP (other weak) |
| 697 |
|
| 698 |
data RelP S : Kind → B.Rel (El S ⊥) (Level.suc (a ⊔ ℓ)) where |
| 699 |
|
| 700 |
-- Congruences. |
| 701 |
|
| 702 |
now : ∀ {k x y} (xRy : x ⟨ Eq S ⟩ y) → RelP S k (now x) (now y) |
| 703 |
later : ∀ {k x y} (x∼y : ∞ (RelP S k (♭ x) (♭ y))) → |
| 704 |
RelP S k (later x) (later y) |
| 705 |
_>>=_ : ∀ {S′ : Setoid a ℓ} {k} {x₁ x₂} |
| 706 |
{f₁ f₂ : El S′ → El S ⊥} → |
| 707 |
let open M in |
| 708 |
(x₁∼x₂ : RelP S′ k x₁ x₂) |
| 709 |
(f₁∼f₂ : ∀ {x y} → x ⟨ Eq S′ ⟩ y → |
| 710 |
RelP S k (f₁ x) (f₂ y)) → |
| 711 |
RelP S k (x₁ >>= f₁) (x₂ >>= f₂) |
| 712 |
|
| 713 |
-- Ordering/weak equality. |
| 714 |
|
| 715 |
laterʳ : ∀ {x y} (x≈y : RelP S (other weak) x (♭ y)) → RelP S (other weak) x (later y) |
| 716 |
laterˡ : ∀ {k x y} (x∼y : RelP S (other k) (♭ x) y) → RelP S (other k) (later x) y |
| 717 |
|
| 718 |
-- Equational reasoning. Note that including full transitivity |
| 719 |
-- for weak equality would make _∣_≈P_ trivial; a similar |
| 720 |
-- problem applies to _∣_≳P_ (A ∣ never ≳P now x would be |
| 721 |
-- provable). Instead the definition of RelP includes limited |
| 722 |
-- notions of transitivity, similar to weak bisimulation up-to |
| 723 |
-- various things. |
| 724 |
|
| 725 |
_∎ : ∀ {k} x → RelP S k x x |
| 726 |
sym : ∀ {k x y} {eq : Equality k} (x∼y : RelP S k x y) → RelP S k y x |
| 727 |
_≡⟨_⟩_ : ∀ {k} x {y z} (x≡y : x ≡ y) (y∼z : RelP S k y z) → RelP S k x z |
| 728 |
_≅⟨_⟩_ : ∀ {k} x {y z} (x≅y : S ∣ x ≅P y) (y∼z : RelP S k y z) → RelP S k x z |
| 729 |
_≳⟨_⟩_ : let open Equality (Eq S) in |
| 730 |
∀ x {y z} (x≳y : x ≳ y) (y≳z : S ∣ y ≳P z) → S ∣ x ≳P z |
| 731 |
_≳⟨_⟩≅_ : ∀ x {y z} (x≳y : S ∣ x ≳P y) (y≅z : S ∣ y ≅P z) → S ∣ x ≳P z |
| 732 |
_≳⟨_⟩≈_ : ∀ x {y z} (x≳y : S ∣ x ≳P y) (y≈z : S ∣ y ≈P z) → S ∣ x ≈P z |
| 733 |
_≈⟨_⟩≅_ : ∀ x {y z} (x≈y : S ∣ x ≈P y) (y≅z : S ∣ y ≅P z) → S ∣ x ≈P z |
| 734 |
_≈⟨_⟩≲_ : ∀ x {y z} (x≈y : S ∣ x ≈P y) (y≲z : S ∣ z ≳P y) → S ∣ x ≈P z |
| 735 |
|
| 736 |
-- If any of the following transitivity-like rules were added to |
| 737 |
-- RelP, then RelP and Rel would no longer be equivalent: |
| 738 |
-- |
| 739 |
-- x ≳P y → y ≳P z → x ≳P z |
| 740 |
-- x ≳P y → y ≳ z → x ≳P z |
| 741 |
-- x ≲P y → y ≈P z → x ≈P z |
| 742 |
-- x ≈P y → y ≳P z → x ≈P z |
| 743 |
-- x ≲ y → y ≈P z → x ≈P z |
| 744 |
-- x ≈P y → y ≳ z → x ≈P z |
| 745 |
-- x ≈P y → y ≈P z → x ≈P z |
| 746 |
-- x ≈P y → y ≈ z → x ≈P z |
| 747 |
-- x ≈ y → y ≈P z → x ≈P z |
| 748 |
-- |
| 749 |
-- The reason is that any of these rules would make it possible |
| 750 |
-- to derive that never and now x are related. |
| 751 |
|
| 752 |
-- RelP is complete with respect to Rel. |
| 753 |
|
| 754 |
complete : ∀ {S k} {x y : El S ⊥} → |
| 755 |
Equality.Rel (Eq S) k x y → RelP S k x y |
| 756 |
complete (now xRy) = now xRy |
| 757 |
complete (later x∼y) = later (♯ complete (♭ x∼y)) |
| 758 |
complete (laterʳ x≈y) = laterʳ (complete x≈y) |
| 759 |
complete (laterˡ x∼y) = laterˡ (complete x∼y) |
| 760 |
|
| 761 |
-- RelP is sound with respect to Rel. |
| 762 |
|
| 763 |
private |
| 764 |
|
| 765 |
-- Proof WHNFs. |
| 766 |
|
| 767 |
data RelW S : Kind → B.Rel (El S ⊥) (Level.suc (a ⊔ ℓ)) where |
| 768 |
now : ∀ {k x y} (xRy : x ⟨ Eq S ⟩ y) → RelW S k (now x) (now y) |
| 769 |
later : ∀ {k x y} (x∼y : RelP S k (♭ x) (♭ y)) → RelW S k (later x) (later y) |
| 770 |
laterʳ : ∀ {x y} (x≈y : RelW S (other weak) x (♭ y)) → RelW S (other weak) x (later y) |
| 771 |
laterˡ : ∀ {k x y} (x∼y : RelW S (other k) (♭ x) y) → RelW S (other k) (later x) y |
| 772 |
|
| 773 |
-- WHNFs can be turned into programs. |
| 774 |
|
| 775 |
program : ∀ {S k x y} → RelW S k x y → RelP S k x y |
| 776 |
program (now xRy) = now xRy |
| 777 |
program (later x∼y) = later (♯ x∼y) |
| 778 |
program (laterˡ x∼y) = laterˡ (program x∼y) |
| 779 |
program (laterʳ x≈y) = laterʳ (program x≈y) |
| 780 |
|
| 781 |
-- Lemmas for WHNFs. |
| 782 |
|
| 783 |
_>>=W_ : ∀ {A B k x₁ x₂} {f₁ f₂ : El A → El B ⊥} → |
| 784 |
RelW A k x₁ x₂ → |
| 785 |
(∀ {x y} → x ⟨ Eq A ⟩ y → RelW B k (f₁ x) (f₂ y)) → |
| 786 |
RelW B k (M._>>=_ x₁ f₁) (M._>>=_ x₂ f₂) |
| 787 |
now xRy >>=W f₁∼f₂ = f₁∼f₂ xRy |
| 788 |
later x∼y >>=W f₁∼f₂ = later (x∼y >>= program ∘ f₁∼f₂) |
| 789 |
laterʳ x≈y >>=W f₁≈f₂ = laterʳ (x≈y >>=W f₁≈f₂) |
| 790 |
laterˡ x∼y >>=W f₁∼f₂ = laterˡ (x∼y >>=W f₁∼f₂) |
| 791 |
|
| 792 |
reflW : ∀ {S k} x → RelW S k x x |
| 793 |
reflW {S} (now x) = now (Setoid.refl S) |
| 794 |
reflW (later x) = later (♭ x ∎) |
| 795 |
|
| 796 |
symW : ∀ {S k x y} → Equality k → RelW S k x y → RelW S k y x |
| 797 |
symW {S} eq (now xRy) = now (Setoid.sym S xRy) |
| 798 |
symW eq (later x≈y) = later (sym {eq = eq} x≈y) |
| 799 |
symW eq (laterʳ x≈y) = laterˡ (symW eq x≈y) |
| 800 |
symW eq (laterˡ {weak} x≈y) = laterʳ (symW eq x≈y) |
| 801 |
|
| 802 |
trans≅W : ∀ {S x y z} → |
| 803 |
RelW S strong x y → RelW S strong y z → RelW S strong x z |
| 804 |
trans≅W {S} (now xRy) (now yRz) = now (Setoid.trans S xRy yRz) |
| 805 |
trans≅W (later x≅y) (later y≅z) = later (_ ≅⟨ x≅y ⟩ y≅z) |
| 806 |
|
| 807 |
trans≳-W : ∀ {S x y z} → let open Equality (Eq S) in |
| 808 |
x ≳ y → RelW S (other geq) y z → RelW S (other geq) x z |
| 809 |
trans≳-W {S} (now xRy) (now yRz) = now (Setoid.trans S xRy yRz) |
| 810 |
trans≳-W (later x≳y) (later y≳z) = later (_ ≳⟨ ♭ x≳y ⟩ y≳z) |
| 811 |
trans≳-W (later x≳y) (laterˡ y≳z) = laterˡ (trans≳-W (♭ x≳y) y≳z) |
| 812 |
trans≳-W (laterˡ x≳y) y≳z = laterˡ (trans≳-W x≳y y≳z) |
| 813 |
|
| 814 |
-- Strong equality programs can be turned into WHNFs. |
| 815 |
|
| 816 |
whnf≅ : ∀ {S x y} → S ∣ x ≅P y → RelW S strong x y |
| 817 |
whnf≅ (now xRy) = now xRy |
| 818 |
whnf≅ (later x≅y) = later (♭ x≅y) |
| 819 |
whnf≅ (x₁≅x₂ >>= f₁≅f₂) = whnf≅ x₁≅x₂ >>=W λ xRy → whnf≅ (f₁≅f₂ xRy) |
| 820 |
whnf≅ (x ∎) = reflW x |
| 821 |
whnf≅ (sym x≅y) = symW _ (whnf≅ x≅y) |
| 822 |
whnf≅ (x ≡⟨ ≡.refl ⟩ y≅z) = whnf≅ y≅z |
| 823 |
whnf≅ (x ≅⟨ x≅y ⟩ y≅z) = trans≅W (whnf≅ x≅y) (whnf≅ y≅z) |
| 824 |
|
| 825 |
-- More transitivity lemmas. |
| 826 |
|
| 827 |
_⟨_⟩≅_ : ∀ {S k} x {y z} → |
| 828 |
RelP S k x y → S ∣ y ≅P z → RelP S k x z |
| 829 |
_⟨_⟩≅_ {k = strong} x x≅y y≅z = x ≅⟨ x≅y ⟩ y≅z |
| 830 |
_⟨_⟩≅_ {k = other geq} x x≳y y≅z = x ≳⟨ x≳y ⟩≅ y≅z |
| 831 |
_⟨_⟩≅_ {k = other weak} x x≈y y≅z = x ≈⟨ x≈y ⟩≅ y≅z |
| 832 |
|
| 833 |
trans∼≅W : ∀ {S k x y z} → |
| 834 |
RelW S k x y → RelW S strong y z → RelW S k x z |
| 835 |
trans∼≅W {S} (now xRy) (now yRz) = now (Setoid.trans S xRy yRz) |
| 836 |
trans∼≅W (later x∼y) (later y≅z) = later (_ ⟨ x∼y ⟩≅ y≅z) |
| 837 |
trans∼≅W (laterʳ x≈y) (later y≅z) = laterʳ (trans∼≅W x≈y (whnf≅ y≅z)) |
| 838 |
trans∼≅W (laterˡ x∼y) y≅z = laterˡ (trans∼≅W x∼y y≅z) |
| 839 |
|
| 840 |
trans≅∼W : ∀ {S k x y z} → |
| 841 |
RelW S strong x y → RelW S k y z → RelW S k x z |
| 842 |
trans≅∼W {S} (now xRy) (now yRz) = now (Setoid.trans S xRy yRz) |
| 843 |
trans≅∼W (later x≅y) (later y∼z) = later (_ ≅⟨ x≅y ⟩ y∼z) |
| 844 |
trans≅∼W (later x≅y) (laterˡ y∼z) = laterˡ (trans≅∼W (whnf≅ x≅y) y∼z) |
| 845 |
trans≅∼W x≅y (laterʳ ly≈z) = laterʳ (trans≅∼W x≅y ly≈z) |
| 846 |
|
| 847 |
-- Order programs can be turned into WHNFs. |
| 848 |
|
| 849 |
whnf≳ : ∀ {S x y} → S ∣ x ≳P y → RelW S (other geq) x y |
| 850 |
whnf≳ (now xRy) = now xRy |
| 851 |
whnf≳ (later x∼y) = later (♭ x∼y) |
| 852 |
whnf≳ (laterˡ x≲y) = laterˡ (whnf≳ x≲y) |
| 853 |
whnf≳ (x₁∼x₂ >>= f₁∼f₂) = whnf≳ x₁∼x₂ >>=W λ xRy → whnf≳ (f₁∼f₂ xRy) |
| 854 |
whnf≳ (x ∎) = reflW x |
| 855 |
whnf≳ (x ≡⟨ ≡.refl ⟩ y≳z) = whnf≳ y≳z |
| 856 |
whnf≳ (x ≅⟨ x≅y ⟩ y≳z) = trans≅∼W (whnf≅ x≅y) (whnf≳ y≳z) |
| 857 |
whnf≳ (x ≳⟨ x≳y ⟩ y≳z) = trans≳-W x≳y (whnf≳ y≳z) |
| 858 |
whnf≳ (x ≳⟨ x≳y ⟩≅ y≅z) = trans∼≅W (whnf≳ x≳y) (whnf≅ y≅z) |
| 859 |
|
| 860 |
-- Another transitivity lemma. |
| 861 |
|
| 862 |
trans≳≈W : ∀ {S x y z} → |
| 863 |
RelW S (other geq) x y → RelW S (other weak) y z → |
| 864 |
RelW S (other weak) x z |
| 865 |
trans≳≈W {S} (now xRy) (now yRz) = now (Setoid.trans S xRy yRz) |
| 866 |
trans≳≈W (later x≳y) (later y≈z) = later (_ ≳⟨ x≳y ⟩≈ y≈z) |
| 867 |
trans≳≈W (laterˡ x≳y) y≈z = laterˡ (trans≳≈W x≳y y≈z) |
| 868 |
trans≳≈W x≳y (laterʳ y≈z) = laterʳ (trans≳≈W x≳y y≈z) |
| 869 |
trans≳≈W (later x≳y) (laterˡ y≈z) = laterˡ (trans≳≈W (whnf≳ x≳y) y≈z) |
| 870 |
|
| 871 |
-- All programs can be turned into WHNFs. |
| 872 |
|
| 873 |
whnf : ∀ {S k x y} → RelP S k x y → RelW S k x y |
| 874 |
whnf (now xRy) = now xRy |
| 875 |
whnf (later x∼y) = later (♭ x∼y) |
| 876 |
whnf (laterʳ x≈y) = laterʳ (whnf x≈y) |
| 877 |
whnf (laterˡ x∼y) = laterˡ (whnf x∼y) |
| 878 |
whnf (x₁∼x₂ >>= f₁∼f₂) = whnf x₁∼x₂ >>=W λ xRy → whnf (f₁∼f₂ xRy) |
| 879 |
whnf (x ∎) = reflW x |
| 880 |
whnf (sym {eq = eq} x≈y) = symW eq (whnf x≈y) |
| 881 |
whnf (x ≡⟨ ≡.refl ⟩ y∼z) = whnf y∼z |
| 882 |
whnf (x ≅⟨ x≅y ⟩ y∼z) = trans≅∼W (whnf x≅y) (whnf y∼z) |
| 883 |
whnf (x ≳⟨ x≳y ⟩ y≳z) = trans≳-W x≳y (whnf y≳z) |
| 884 |
whnf (x ≳⟨ x≳y ⟩≅ y≅z) = trans∼≅W (whnf x≳y) (whnf y≅z) |
| 885 |
whnf (x ≳⟨ x≳y ⟩≈ y≈z) = trans≳≈W (whnf x≳y) (whnf y≈z) |
| 886 |
whnf (x ≈⟨ x≈y ⟩≅ y≅z) = trans∼≅W (whnf x≈y) (whnf y≅z) |
| 887 |
whnf (x ≈⟨ x≈y ⟩≲ y≲z) = symW _ (trans≳≈W (whnf y≲z) (symW _ (whnf x≈y))) |
| 888 |
|
| 889 |
mutual |
| 890 |
|
| 891 |
-- Soundness. |
| 892 |
|
| 893 |
private |
| 894 |
|
| 895 |
soundW : ∀ {S k x y} → RelW S k x y → Rel (Eq S) k x y |
| 896 |
soundW (now xRy) = now xRy |
| 897 |
soundW (later x∼y) = later (♯ sound x∼y) |
| 898 |
soundW (laterʳ x≈y) = laterʳ (soundW x≈y) |
| 899 |
soundW (laterˡ x∼y) = laterˡ (soundW x∼y) |
| 900 |
|
| 901 |
sound : ∀ {S k x y} → RelP S k x y → Rel (Eq S) k x y |
| 902 |
sound x∼y = soundW (whnf x∼y) |
| 903 |
|
| 904 |
-- RelP and Rel are equivalent (when the underlying relation is an |
| 905 |
-- equivalence). |
| 906 |
|
| 907 |
correct : ∀ {S k x y} → RelP S k x y ⇔ Rel (Eq S) k x y |
| 908 |
correct = mk⇔ sound complete |
| 909 |
|
| 910 |
------------------------------------------------------------------------ |
| 911 |
-- Another lemma |
| 912 |
|
| 913 |
-- Bind is "idempotent". |
| 914 |
|
| 915 |
idempotent : |
| 916 |
(B : Setoid ℓ ℓ) → |
| 917 |
let open M; open Setoid B using (_≈_; Carrier); open Equality _≈_ in |
| 918 |
(x : A ⊥) (f : A → A → Carrier ⊥) → |
| 919 |
(x >>= λ y′ → x >>= λ y″ → f y′ y″) ≳ (x >>= λ y′ → f y′ y′) |
| 920 |
idempotent {A = A} B x f = sound (idem x) |
| 921 |
where |
| 922 |
open AlternativeEquality hiding (_>>=_) |
| 923 |
open M |
| 924 |
open Equality.Rel using (laterˡ) |
| 925 |
open Equivalence using (refl) |
| 926 |
|
| 927 |
idem : (x : A ⊥) → |
| 928 |
B ∣ (x >>= λ y′ → x >>= λ y″ → f y′ y″) ≳P |
| 929 |
(x >>= λ y′ → f y′ y′) |
| 930 |
idem (now x) = f x x ∎ |
| 931 |
idem (later x) = later (♯ ( |
| 932 |
(♭ x >>= λ y′ → later x >>= λ y″ → f y′ y″) ≳⟨ (refl ≡.refl {x = ♭ x} ≡->>=-cong λ _ → |
| 933 |
laterˡ (refl (Setoid.refl B))) ⟩ |
| 934 |
(♭ x >>= λ y′ → ♭ x >>= λ y″ → f y′ y″) ≳⟨ idem (♭ x) ⟩≅ |
| 935 |
(♭ x >>= λ y′ → f y′ y′) ∎)) |