/
DCPO.lagda.md
370 lines (298 loc) · 10.9 KB
/
DCPO.lagda.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<!--
```agda
open import Cat.Displayed.Univalence.Thin
open import Cat.Functor.Subcategory
open import Cat.Displayed.Total
open import Cat.Prelude
open import Data.Bool
open import Order.Diagram.Lub
open import Order.Univalent
open import Order.Base
import Cat.Reasoning
import Order.Reasoning
```
-->
```agda
module Order.DCPO where
```
<!--
```agda
private variable
o ℓ ℓ' : Level
Ix A B : Type o
```
-->
# Directed-complete partial orders {defines="dcpo semidirected-family"}
Let $(P, \le)$ be a [[partial order]]. A family of elements $f : I \to P$ is
a **semi-directed family** if for every $i, j$, there merely exists
some $k$ such $f i \le f k$ and $f j \le f k$. A semidirected family
$f : I \to P$ is a **directed family** when $I$ is merely inhabited.
```agda
module _ {o ℓ} (P : Poset o ℓ) where
open Order.Reasoning P
is-semidirected-family : ∀ {Ix : Type o} → (Ix → Ob) → Type _
is-semidirected-family {Ix = Ix} f = ∀ i j → ∃[ k ∈ Ix ] (f i ≤ f k × f j ≤ f k)
record is-directed-family {Ix : Type o} (f : Ix → Ob) : Type (o ⊔ ℓ) where
no-eta-equality
field
elt : ∥ Ix ∥
semidirected : is-semidirected-family f
```
Note that any family whose indexing type is a proposition is
automatically semi-directed:
```agda
prop-indexed→semidirected
: ∀ {Ix : Type o} → (s : Ix → Ob) → is-prop Ix
→ is-semidirected-family s
prop-indexed→semidirected s prop i j =
inc (i , ≤-refl , ≤-refl' (ap s (prop j i)))
```
The poset $(P, \le)$ is a **directed-complete partial order**, or DCPO,
if it has [[least upper bounds]] of all directed families.
```agda
record is-dcpo : Type (lsuc o ⊔ ℓ) where
no-eta-equality
field
directed-lubs
: ∀ {Ix : Type o} (f : Ix → Ob) → is-directed-family f → Lub P f
module ⋃ {Ix : Type o} (f : Ix → Ob) (dir : is-directed-family f) =
Lub (directed-lubs f dir)
open ⋃ using () renaming (lub to ⋃; fam≤lub to fam≤⋃; least to ⋃-least) public
```
Since least upper bounds are unique when they exist, being a DCPO is a
[[property|proposition]] of a poset, and not structure on that poset.
<!--
```agda
unquoteDecl H-Level-is-dcpo = declare-record-hlevel 1 H-Level-is-dcpo (quote is-dcpo)
```
-->
# Scott-continuous functions
Let $(P, \le)$ and $(Q, \lsq)$ be a pair of posets. A monotone map $f :
P \to Q$ is called **Scott-continuous** when it preserves all directed
lubs.
<!--
```agda
module _ {P Q : Poset o ℓ} where
private
module P = Poset P
module Q = Poset Q
open is-directed-family
open Total-hom
```
-->
```agda
is-scott-continuous : (f : Posets.Hom P Q) → Type _
is-scott-continuous f =
∀ {Ix} (s : Ix → P.Ob) (fam : is-directed-family P s)
→ ∀ x → is-lub P s x → is-lub Q (f .hom ⊙ s) (f .hom x)
is-scott-continuous-is-prop
: ∀ (f : Posets.Hom P Q) → is-prop (is-scott-continuous f)
is-scott-continuous-is-prop _ = hlevel 1
```
If $(P, \le)$ is a DCPO, then any function $f : P \to Q$ which preserves
directed lubs is automatically a monotone map, and, consequently,
Scott-continuous.
To see this, suppose we're given $x, y : P$ with $x \le y$. The family
$s : \rm{Bool} \to P$ that sends $\rm{true}$ to $x$ and $\rm{false}$ to
$y$ is directed: $\rm{Bool}$ is inhabited, and $\rm{false}$ is a least
upper bound for arbitrary values of the family. Since $f$ preserves
lubs, we have
$$
f(x) \le (\sqcup f(s)) = f(\sqcup s) = f(y)
$$
```agda
opaque
dcpo+scott→monotone
: is-dcpo P
→ (f : P.Ob → Q.Ob)
→ (∀ {Ix} (s : Ix → Poset.Ob P) (fam : is-directed-family P s)
→ ∀ x → is-lub P s x → is-lub Q (f ⊙ s) (f x))
→ ∀ {x y} → x P.≤ y → f x Q.≤ f y
dcpo+scott→monotone is-dcpo f scott {x} {y} p =
fs-lub .is-lub.fam≤lub (lift true) where
s : Lift o Bool → P.Ob
s (lift b) = if b then x else y
sx≤sfalse : ∀ b → s b P.≤ s (lift false)
sx≤sfalse (lift true) = p
sx≤sfalse (lift false) = P.≤-refl
s-directed : is-directed-family P s
s-directed .elt =
inc (lift true)
s-directed .semidirected i j =
inc (lift false , sx≤sfalse _ , sx≤sfalse _)
s-lub : is-lub P s y
s-lub .is-lub.fam≤lub = sx≤sfalse
s-lub .is-lub.least z lt = lt (lift false)
fs-lub : is-lub Q (f ⊙ s) (f y)
fs-lub = scott s s-directed y s-lub
```
Moreover, if $f : P \to Q$ is an arbitrary monotone map, and $s : I \to
P$ is a directed family, then $fs : I \to Q$ is still a directed family.
```agda
monotone∘directed
: ∀ {Ix : Type o}
→ {s : Ix → P.Ob}
→ (f : Posets.Hom P Q)
→ is-directed-family P s
→ is-directed-family Q (f .hom ⊙ s)
monotone∘directed f dir .elt = dir .elt
monotone∘directed f dir .is-directed-family.semidirected i j =
∥-∥-map (Σ-map₂ (×-map (f .pres-≤) (f .pres-≤)))
(dir .semidirected i j)
```
<!--
```agda
module _ where
open Total-hom
```
-->
The identity function is Scott-continuous.
```agda
scott-id
: ∀ {P : Poset o ℓ}
→ is-scott-continuous (Posets.id {x = P})
scott-id s fam x lub = lub
```
Scott-continuous functions are closed under composition.
```agda
scott-∘
: ∀ {P Q R : Poset o ℓ}
→ (f : Posets.Hom Q R) (g : Posets.Hom P Q)
→ is-scott-continuous f → is-scott-continuous g
→ is-scott-continuous (f Posets.∘ g)
scott-∘ f g f-scott g-scott s dir x lub =
f-scott (g .hom ⊙ s)
(monotone∘directed g dir)
(g .hom x)
(g-scott s dir x lub)
```
# The category of DCPOs
DCPOs form a [[subcategory]] of the category of posets. Furthermore,
since being a DCPO is a property, identity of DCPOs is determined by
identity of their underlying posets: thus, the category of DCPOs is
[[univalent|univalent category]].
```agda
DCPOs-subcat : ∀ (o ℓ : Level) → Subcat (Posets o ℓ) (lsuc o ⊔ ℓ) (lsuc o ⊔ ℓ)
DCPOs-subcat o ℓ .Subcat.is-ob = is-dcpo
DCPOs-subcat o ℓ .Subcat.is-hom f _ _ = is-scott-continuous f
DCPOs-subcat o ℓ .Subcat.is-hom-prop f _ _ = is-scott-continuous-is-prop f
DCPOs-subcat o ℓ .Subcat.is-hom-id _ = scott-id
DCPOs-subcat o ℓ .Subcat.is-hom-∘ {f = f} {g = g} = scott-∘ f g
DCPOs : ∀ (o ℓ : Level) → Precategory (lsuc (o ⊔ ℓ)) (lsuc o ⊔ ℓ)
DCPOs o ℓ = Subcategory (DCPOs-subcat o ℓ)
DCPOs-is-category : ∀ {o ℓ} → is-category (DCPOs o ℓ)
DCPOs-is-category = subcat-is-category Posets-is-category (λ _ → hlevel 1)
```
<!--
```agda
module DCPOs {o ℓ : Level} = Cat.Reasoning (DCPOs o ℓ)
DCPO : (o ℓ : Level) → Type _
DCPO o ℓ = DCPOs.Ob {o} {ℓ}
DCPOs↪Posets : ∀ {o ℓ} → Functor (DCPOs o ℓ) (Posets o ℓ)
DCPOs↪Posets = Forget-subcat
DCPOs↪Sets : ∀ {o ℓ} → Functor (DCPOs o ℓ) (Sets o)
DCPOs↪Sets = Posets↪Sets F∘ DCPOs↪Posets
```
-->
# Reasoning with DCPOs
The following pair of modules re-exports facts about the underlying
posets (resp. monotone maps) of DCPOs (resp. Scott-continuous
functions). They also prove a plethora of small lemmas that are useful
in formalisation but not for informal reading.
<details>
<summary>These proofs are all quite straightforward, so we omit them.
</summary>
```agda
module DCPO {o ℓ} (D : DCPO o ℓ) where
poset : Poset o ℓ
poset = D .fst
open Order.Reasoning poset public
set : Set o
set = el ⌞ D ⌟ Ob-is-set
has-dcpo : is-dcpo poset
has-dcpo = D .snd
open is-dcpo has-dcpo public
⋃-pointwise
: ∀ {Ix} {s s' : Ix → Ob}
→ {fam : is-directed-family poset s} {fam' : is-directed-family poset s'}
→ (∀ ix → s ix ≤ s' ix)
→ ⋃ s fam ≤ ⋃ s' fam'
⋃-pointwise p = ⋃.least _ _ (⋃ _ _) λ ix →
≤-trans (p ix) (⋃.fam≤lub _ _ ix)
module Scott {o ℓ} {D E : DCPO o ℓ} (f : DCPOs.Hom D E) where
private
module D = DCPO D
module E = DCPO E
mono : Posets.Hom D.poset E.poset
mono = Subcat-hom.hom f
monotone : ∀ {x y} → x D.≤ y → f # x E.≤ f # y
monotone = mono .pres-≤
opaque
pres-directed-lub
: ∀ {Ix} (s : Ix → D.Ob) → is-directed-family D.poset s
→ ∀ x → is-lub (D .fst) s x → is-lub (E .fst) (apply f ⊙ s) (f # x)
pres-directed-lub = Subcat-hom.witness f
directed
: ∀ {Ix} {s : Ix → D.Ob} → is-directed-family D.poset s
→ is-directed-family E.poset (apply f ⊙ s)
directed dir = monotone∘directed (Subcat-hom.hom f) dir
pres-⋃
: ∀ {Ix} (s : Ix → D.Ob) → (dir : is-directed-family D.poset s)
→ f # (D.⋃ s dir) ≡ E.⋃ (apply f ⊙ s) (directed dir)
pres-⋃ s dir =
E.≤-antisym
(is-lub.least (pres-directed-lub s dir (D.⋃ s dir) (D.⋃.has-lub s dir))
(E.⋃ (apply f ⊙ s) (directed dir))
(E.⋃.fam≤lub (apply f ⊙ s) (directed dir)))
(E.⋃.least (apply f ⊙ s) (directed dir) (apply f (D.⋃ s dir)) λ i →
monotone (D.⋃.fam≤lub s dir i))
```
</details>
<!--
```
module _ {o ℓ} {D E : DCPO o ℓ} where
private
module D = DCPO D
module E = DCPO E
open is-directed-family
open Total-hom
```
-->
We also provide a couple methods for constructing Scott-continuous maps.
First, we note that if a function is monotonic and commutes with some
chosen _assignment_ of least upper bounds, then it is Scott-continuous.
```agda
to-scott
: (f : D.Ob → E.Ob)
→ (∀ {x y} → x D.≤ y → f x E.≤ f y)
→ (∀ {Ix} (s : Ix → D.Ob) → (dir : is-directed-family D.poset s)
→ is-lub E.poset (f ⊙ s) (f (D.⋃ s dir)))
→ DCPOs.Hom D E
to-scott f monotone pres-⋃ = sub-hom fᵐ pres-lub where
fᵐ : Monotone D.poset E.poset
fᵐ .hom = f
fᵐ .pres-≤ = monotone
pres-lub
: ∀ {Ix} (s : Ix → D.Ob) → (dir : is-directed-family D.poset s)
→ ∀ x → is-lub D.poset s x → is-lub E.poset (f ⊙ s) (f x)
pres-lub s dir x x-lub .is-lub.fam≤lub i =
monotone (is-lub.fam≤lub x-lub i)
pres-lub s dir x x-lub .is-lub.least y le =
f x E.≤⟨ monotone (is-lub.least x-lub _ (D.⋃.fam≤lub s dir)) ⟩
f (D.⋃ s dir) E.=⟨ lub-unique (pres-⋃ s dir) (E.⋃.has-lub (f ⊙ s) (monotone∘directed fᵐ dir)) ⟩
E.⋃ (f ⊙ s) _ E.≤⟨ E.⋃.least _ _ y le ⟩
y E.≤∎
```
Furthermore, if $f$ preserves arbitrary least upper bounds, then it
is monotone, and thus Scott-continuous.
```agda
to-scott-directed
: (f : D.Ob → E.Ob)
→ (∀ {Ix} (s : Ix → D.Ob) → (dir : is-directed-family D.poset s)
→ ∀ x → is-lub D.poset s x → is-lub E.poset (f ⊙ s) (f x))
→ DCPOs.Hom D E
to-scott-directed f pres .Subcat-hom.hom .hom = f
to-scott-directed f pres .Subcat-hom.hom .pres-≤ =
dcpo+scott→monotone D.has-dcpo f pres
to-scott-directed f pres .Subcat-hom.witness = pres
```