Debian Codegraph @

src/Effect/Monad/Partiality/All.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- An All predicate for the partiality monad
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe --guardedness #-}
8
9 module Effect.Monad.Partiality.All where
10
11 open import Effect.Monad using (RawMonad)
12 open import Effect.Monad.Partiality as Partiality using (_⊥; ⇒≈)
13 open import Codata.Musical.Notation using (♭; ∞; ♯_)
14 open import Function.Base using (flip; _∘_)
15 open import Level using (Level; _⊔_; suc)
16 open import Relation.Binary.Definitions using (_Respects_)
17 open import Relation.Binary.Structures using (IsEquivalence)
18 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
19
20 open Partiality._⊥
21 open Partiality.Equality using (Rel)
22 open Partiality.Equality.Rel
23 private
24 open module E {a} {A : Set a} = Partiality.Equality (_≡_ {A = A})
25 using (_≅_; _≳_)
26 open module M {f} = RawMonad (Partiality.monad {f = f})
27 using (_>>=_)
28
29 private
30 variable
31 a b p ℓ : Level
32 A : Set a
33 B : Set b
34
35 ------------------------------------------------------------------------
36 -- All, along with some lemmas
37
38 -- All P x means that if x terminates with the value v, then P v
39 -- holds.
40
41 data All {A : Set a} (P : A → Set p) : A ⊥ → Set (a ⊔ p) where
42 now : ∀ {v} (p : P v) → All P (now v)
43 later : ∀ {x} (p : ∞ (All P (♭ x))) → All P (later x)
44
45 -- Bind preserves All in the following way:
46
47 infixl 1 _>>=-cong_
48
49 _>>=-cong_ : ∀ {p q} {P : A → Set p} {Q : B → Set q}
50 {x : A ⊥} {f : A → B ⊥} →
51 All P x → (∀ {x} → P x → All Q (f x)) →
52 All Q (x >>= f)
53 now p >>=-cong f = f p
54 later p >>=-cong f = later (♯ (♭ p >>=-cong f))
55
56 -- All respects all the relations, given that the predicate respects
57 -- the underlying relation.
58
59 respects :
60 ∀ {k} {P : A → Set p} {_∼_ : A → A → Set ℓ} →
61 P Respects _∼_ → All P Respects Rel _∼_ k
62 respects resp (now x∼y) (now p) = now (resp x∼y p)
63 respects resp (later x∼y) (later p) = later (♯ respects resp (♭ x∼y) (♭ p))
64 respects resp (laterˡ x∼y) (later p) = respects resp x∼y (♭ p)
65 respects resp (laterʳ x≈y) p = later (♯ respects resp x≈y p)
66
67 respects-flip :
68 ∀ {k} {P : A → Set p} {_∼_ : A → A → Set ℓ} →
69 P Respects flip _∼_ → All P Respects flip (Rel _∼_ k)
70 respects-flip resp (now x∼y) (now p) = now (resp x∼y p)
71 respects-flip resp (later x∼y) (later p) = later (♯ respects-flip resp (♭ x∼y) (♭ p))
72 respects-flip resp (laterˡ x∼y) p = later (♯ respects-flip resp x∼y p)
73 respects-flip resp (laterʳ x≈y) (later p) = respects-flip resp x≈y (♭ p)
74
75 -- "Equational" reasoning.
76
77 module Reasoning {P : A → Set p}
78 {_∼_ : A → A → Set ℓ}
79 (resp : P Respects flip _∼_) where
80
81 infix 3 finally
82 infixr 2 _≡⟨_⟩_ _∼⟨_⟩_
83
84 _≡⟨_⟩_ : ∀ x {y} → x ≡ y → All P y → All P x
85 _ ≡⟨ ≡.refl ⟩ p = p
86
87 _∼⟨_⟩_ : ∀ {k} x {y} → Rel _∼_ k x y → All P y → All P x
88 _ ∼⟨ x∼y ⟩ p = respects-flip resp (⇒≈ x∼y) p
89
90 -- A cosmetic combinator.
91
92 finally : (x : A ⊥) → All P x → All P x
93 finally _ p = p
94
95 syntax finally x p = x ⟨ p ⟩
96
97 -- "Equational" reasoning with _∼_ instantiated to propositional
98 -- equality.
99
100 module Reasoning-≡ {a p} {A : Set a} {P : A → Set p}
101 = Reasoning {P = P} {_∼_ = _≡_} (≡.subst P ∘ ≡.sym)
102
103 ------------------------------------------------------------------------
104 -- An alternative, but equivalent, formulation of All
105
106 module Alternative {a p : Level} where
107
108 infix 3 _⟨_⟩P
109 infixr 2 _≅⟨_⟩P_ _≳⟨_⟩P_
110
111 -- All "programs".
112
113 data AllP {A : Set a} (P : A → Set p) : A ⊥ → Set (suc (a ⊔ p)) where
114 now : ∀ {x} (p : P x) → AllP P (now x)
115 later : ∀ {x} (p : ∞ (AllP P (♭ x))) → AllP P (later x)
116 _>>=-congP_ : ∀ {B : Set a} {Q : B → Set p} {x f}
117 (p-x : AllP Q x) (p-f : ∀ {v} → Q v → AllP P (f v)) →
118 AllP P (x >>= f)
119 _≅⟨_⟩P_ : ∀ x {y} (x≅y : x ≅ y) (p : AllP P y) → AllP P x
120 _≳⟨_⟩P_ : ∀ x {y} (x≳y : x ≳ y) (p : AllP P y) → AllP P x
121 _⟨_⟩P : ∀ x (p : AllP P x) → AllP P x
122
123 infixl 1 _>>=-congP_
124
125 private
126
127 -- WHNFs.
128
129 data AllW {A} (P : A → Set p) : A ⊥ → Set (suc (a ⊔ p)) where
130 now : ∀ {x} (p : P x) → AllW P (now x)
131 later : ∀ {x} (p : AllP P (♭ x)) → AllW P (later x)
132
133 -- A function which turns WHNFs into programs.
134
135 program : ∀ {P : A → Set p} {x} → AllW P x → AllP P x
136 program (now p) = now p
137 program (later p) = later (♯ p)
138
139 -- Functions which turn programs into WHNFs.
140
141 trans-≅ : {P : A → Set p} {x y : A ⊥} →
142 x ≅ y → AllW P y → AllW P x
143 trans-≅ (now ≡.refl) (now p) = now p
144 trans-≅ (later x≅y) (later p) = later (_ ≅⟨ ♭ x≅y ⟩P p)
145
146 trans-≳ : {P : A → Set p} {x y : A ⊥} →
147 x ≳ y → AllW P y → AllW P x
148 trans-≳ (now ≡.refl) (now p) = now p
149 trans-≳ (later x≳y) (later p) = later (_ ≳⟨ ♭ x≳y ⟩P p)
150 trans-≳ (laterˡ x≳y) p = later (_ ≳⟨ x≳y ⟩P program p)
151
152 mutual
153
154 _>>=-congW_ : ∀ {P : A → Set p} {Q : B → Set p} {x f} →
155 AllW P x → (∀ {v} → P v → AllP Q (f v)) →
156 AllW Q (x >>= f)
157 now p >>=-congW p-f = whnf (p-f p)
158 later p >>=-congW p-f = later (p >>=-congP p-f)
159
160 whnf : ∀ {P : A → Set p} {x} → AllP P x → AllW P x
161 whnf (now p) = now p
162 whnf (later p) = later (♭ p)
163 whnf (p-x >>=-congP p-f) = whnf p-x >>=-congW p-f
164 whnf (_ ≅⟨ x≅y ⟩P p) = trans-≅ x≅y (whnf p)
165 whnf (_ ≳⟨ x≳y ⟩P p) = trans-≳ x≳y (whnf p)
166 whnf (_ ⟨ p ⟩P) = whnf p
167
168 -- AllP P is sound and complete with respect to All P.
169
170 sound : ∀ {P : A → Set p} {x} → AllP P x → All P x
171 sound = λ p → soundW (whnf p)
172 where
173 soundW : ∀ {A} {P : A → Set p} {x} → AllW P x → All P x
174 soundW (now p) = now p
175 soundW (later p) = later (♯ sound p)
176
177 complete : ∀ {P : A → Set p} {x} → All P x → AllP P x
178 complete (now p) = now p
179 complete (later p) = later (♯ complete (♭ p))