-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsomorphism.agda
473 lines (372 loc) · 13.3 KB
/
Isomorphism.agda
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
module plfa.part1.Isomorphism where
-- This section introduces *isomorphism* as a way of asserting that two types are equal,
-- and *embedding* as a way of asserting that one type is smaller than another.
--
-- We apply isomorphisms in the next chapter to demonstrate that operations on types
-- such as product and sum satisfy properties akin to associativity, commutativity, and distributivity.
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl; cong; cong-app)
open Eq.≡-Reasoning
open import Data.Nat using (ℕ; zero; suc; _+_)
open import Data.Nat.Properties using (+-comm)
---------------------------------------------------------------------
-- Lambda expressions
{-
Lambda expressions provide a compact way to define functions without naming them. A term of the form
λ{ P₁ → N₁; ⋯ ; Pₙ → Nₙ }
is equivalent to a function f defined by the equations
f P₁ = N₁
⋯
f Pₙ = Nₙ
where the Pₙ are patterns (left-hand sides of an equation) and the Nₙ are expressions (right-hand side of an equation).
In the case that there is one equation and the pattern is a variable, we may also use the syntax
λ x → N
or
λ (x : A) → N
both of which are equivalent to λ{x → N}.
-}
---------------------------------------------------------------------
-- Function composition
_∘_ : ∀ {A B C : Set} → (B → C) → (A → B) → (A → C)
(g ∘ f) x = g (f x)
-- An equivalent definition, exploiting lambda expressions, is as follows:
_∘′_ : ∀ {A B C : Set} → (B → C) → (A → B) → (A → C)
g ∘′ f = λ x → g (f x)
---------------------------------------------------------------------
-- Extensionality
-- *Extensionality* asserts that the only way to distinguish functions is by applying them;
--
-- if two functions applied to the same argument always yield the same result, then they are the same function.
--
-- It is the converse of cong-app, as introduced earlier.
-- Agda does not presume extensionality, but we can postulate that it holds:
postulate
extensionality : ∀ {A B : Set} {f g : A → B}
→ (∀ (x : A) → f x ≡ g x)
-----------------------
→ f ≡ g
-- Postulating extensionality does not lead to difficulties, as it is known to be consistent with the theory that underlies Agda.
-- As an example, consider that we need results from two libraries, one where
-- addition is defined, as in Chapter Naturals, and one where it is defined the other way around.
_+′_ : ℕ → ℕ → ℕ
m +′ zero = m
m +′ suc n = suc (m +′ n)
-- Applying commutativity, it is easy to show that both operators always return the same result given the same arguments:
same-app : ∀ (m n : ℕ) → m +′ n ≡ m + n
same-app m n rewrite +-comm m n = helper m n
where
helper : ∀ (m n : ℕ) → m +′ n ≡ n + m
helper m zero = refl
helper m (suc n) = cong suc (helper m n)
-- However, it might be convenient to assert that the two operators are actually indistinguishable.
-- This we can do via two applications of extensionality:
same : _+′_ ≡ _+_
same = extensionality (λ m → extensionality (λ n → same-app m n))
-- More generally, we may wish to postulate extensionality for dependent functions.
postulate
∀-extensionality : ∀ {A : Set} {B : A → Set} {f g : ∀(x : A) → B x}
→ (∀ (x : A) → f x ≡ g x)
------------------------
→ f ≡ g
-- Here the type of f and g has changed from A → B to ∀ (x : A) → B x,
-- generalising ordinary functions to dependent functions.
---------------------------------------------------------------------
-- Isomorphism
-- Two sets are isomorphic if they are in one-to-one correspondence.
--
-- Here is a formal definition of isomorphism:
infix 0 _≃_
record _≃_ (A B : Set) : Set where
field
to : A → B
from : B → A
from∘to : ∀ (x : A) → from (to x) ≡ x
to∘from : ∀ (y : B) → to (from y) ≡ y
open _≃_
{-
An isomorphism between sets A and B consists of four things:
- A function to from A to B,
- A function from from B back to A
- Evidence from∘to asserting that from is a left-inverse for to
- Evidence to∘from asserting that from is a right-inverse for to.
In particular, the third asserts that from ∘ to is the identity,
and the fourth that to ∘ from is the identity, hence the names.
The declaration open _≃_ makes available the names to, from, from∘to, and to∘from,
otherwise we would need to write _≃_.to and so on.
-}
-- The above is our first use of records.
--
-- A record declaration behaves similar to a single-constructor data declaration
-- (there are minor differences, which we discuss in Connectives):
data _≃′_ (A B : Set): Set where
mk-≃′ : ∀ (to : A → B) →
∀ (from : B → A) →
∀ (from∘to : (∀ (x : A) → from (to x) ≡ x)) →
∀ (to∘from : (∀ (y : B) → to (from y) ≡ y)) →
A ≃′ B
to′ : ∀ {A B : Set} → (A ≃′ B) → (A → B)
to′ (mk-≃′ f g g∘f f∘g) = f
from′ : ∀ {A B : Set} → (A ≃′ B) → (B → A)
from′ (mk-≃′ f g g∘f f∘g) = g
from∘to′ : ∀ {A B : Set} → (A≃B : A ≃′ B) → (∀ (x : A) → from′ A≃B (to′ A≃B x) ≡ x)
from∘to′ (mk-≃′ f g g∘f f∘g) = g∘f
to∘from′ : ∀ {A B : Set} → (A≃B : A ≃′ B) → (∀ (y : B) → to′ A≃B (from′ A≃B y) ≡ y)
to∘from′ (mk-≃′ f g g∘f f∘g) = f∘g
{-
We construct values of the record type with the syntax
record
{ to = f
; from = g
; from∘to = g∘f
; to∘from = f∘g
}
which corresponds to using the constructor of the corresponding inductive type
mk-≃′ f g g∘f f∘g
where f, g, g∘f, and f∘g are values of suitable types.
-}
---------------------------------------------------------------------
-- Isomorphism is an equivalence
-- Isomorphism is an equivalence, meaning that it is reflexive, symmetric, and transitive.
--
-- To show isomorphism is reflexive, we take both to and from to be the identity function:
≃-refl : ∀ {A : Set}
-----
→ A ≃ A
≃-refl =
record
{ to = λ{x → x}
; from = λ{y → y}
; from∘to = λ{x → refl}
; to∘from = λ{y → refl}
}
-- To show isomorphism is symmetric, we simply swap the roles of to and from, and from∘to and to∘from:
≃-sym : ∀ {A B : Set}
→ A ≃ B
-----
→ B ≃ A
≃-sym A≃B =
record
{ to = from A≃B
; from = to A≃B
; from∘to = to∘from A≃B
; to∘from = from∘to A≃B
}
-- To show isomorphism is transitive, we compose the to and from functions,
-- and use equational reasoning to combine the inverses:
≃-trans : ∀ {A B C : Set}
→ A ≃ B
→ B ≃ C
-----
→ A ≃ C
≃-trans A≃B B≃C =
record
{ to = to B≃C ∘ to A≃B
; from = from A≃B ∘ from B≃C
; from∘to = λ{x →
begin
(from A≃B ∘ from B≃C) ((to B≃C ∘ to A≃B) x)
≡⟨⟩
from A≃B (from B≃C (to B≃C (to A≃B x)))
≡⟨ cong (from A≃B) (from∘to B≃C (to A≃B x)) ⟩
from A≃B (to A≃B x)
≡⟨ from∘to A≃B x ⟩
x
∎}
; to∘from = λ{y →
begin
(to B≃C ∘ to A≃B) ((from A≃B ∘ from B≃C) y)
≡⟨⟩
to B≃C (to A≃B (from A≃B (from B≃C y)))
≡⟨ cong (to B≃C) (to∘from A≃B (from B≃C y)) ⟩
to B≃C (from B≃C y)
≡⟨ to∘from B≃C y ⟩
y
∎}
}
---------------------------------------------------------------------
-- Equational reasoning for isomorphism
-- It is straightforward to support a variant of equational reasoning for isomorphism.
-- We essentially copy the previous definition of equality for isomorphism.
-- We omit the form that corresponds to _≡⟨⟩_, since trivial isomorphisms arise far less often than trivial equalities:
module ≃-Reasoning where
infix 1 ≃-begin_
infixr 2 _≃⟨_⟩_
infix 3 _≃-∎
≃-begin_ : ∀ {A B : Set}
→ A ≃ B
-----
→ A ≃ B
≃-begin A≃B = A≃B
_≃⟨_⟩_ : ∀ (A : Set) {B C : Set}
→ A ≃ B
→ B ≃ C
-----
→ A ≃ C
A ≃⟨ A≃B ⟩ B≃C = ≃-trans A≃B B≃C
_≃-∎ : ∀ (A : Set)
-----
→ A ≃ A
A ≃-∎ = ≃-refl
open ≃-Reasoning
---------------------------------------------------------------------
-- Embedding
-- We also need the notion of *embedding,* which is a weakening of isomorphism.
-- While an isomorphism shows that two types are in one-to-one correspondence,
-- an embedding shows that the first type is included in the second; or, equivalently,
-- that there is a many-to-one correspondence between the second type and the first.
-- Here is the formal definition of embedding:
infix 0 _≲_
record _≲_ (A B : Set) : Set where
field
to : A → B
from : B → A
from∘to : ∀ (x : A) → from (to x) ≡ x
open _≲_
-- Hence, we know that from is left-inverse to to, but not that from is right-inverse to to.
-- Embedding is reflexive and transitive, but not symmetric.
≲-refl : ∀ {A : Set} → A ≲ A
≲-refl =
record
{ to = λ{x → x}
; from = λ{y → y}
; from∘to = λ{x → refl}
}
≲-trans : ∀ {A B C : Set} → A ≲ B → B ≲ C → A ≲ C
≲-trans A≲B B≲C =
record
{ to = λ{x → to B≲C (to A≲B x)}
; from = λ{y → from A≲B (from B≲C y)}
; from∘to = λ{x →
begin
from A≲B (from B≲C (to B≲C (to A≲B x)))
≡⟨ cong (from A≲B) (from∘to B≲C (to A≲B x)) ⟩
from A≲B (to A≲B x)
≡⟨ from∘to A≲B x ⟩
x
∎}
}
-- It is also easy to see that if two types embed in each other,
-- and the embedding functions correspond, then they are isomorphic.
--
-- This is a weak form of anti-symmetry:
≲-antisym : ∀ {A B : Set}
→ (A≲B : A ≲ B)
→ (B≲A : B ≲ A)
→ (to A≲B ≡ from B≲A)
→ (from A≲B ≡ to B≲A)
-------------------
→ A ≃ B
≲-antisym A≲B B≲A to≡from from≡to =
record
{ to = to A≲B
; from = from A≲B
; from∘to = from∘to A≲B
; to∘from = λ{y →
begin
to A≲B (from A≲B y)
≡⟨ cong (to A≲B) (cong-app from≡to y) ⟩
to A≲B (to B≲A y)
≡⟨ cong-app to≡from (to B≲A y) ⟩
from B≲A (to B≲A y)
≡⟨ from∘to B≲A y ⟩
y
∎}
}
---------------------------------------------------------------------
-- Equational reasoning for embedding
module ≲-Reasoning where
infix 1 ≲-begin_
infixr 2 _≲⟨_⟩_
infix 3 _≲-∎
≲-begin_ : ∀ {A B : Set}
→ A ≲ B
-----
→ A ≲ B
≲-begin A≲B = A≲B
_≲⟨_⟩_ : ∀ (A : Set) {B C : Set}
→ A ≲ B
→ B ≲ C
-----
→ A ≲ C
A ≲⟨ A≲B ⟩ B≲C = ≲-trans A≲B B≲C
_≲-∎ : ∀ (A : Set)
-----
→ A ≲ A
A ≲-∎ = ≲-refl
open ≲-Reasoning
---------------------------------------------------------------------
-- Exercises
≃-implies-≲ : ∀ {A B : Set}
→ A ≃ B
-----
→ A ≲ B
≃-implies-≲ A≃B =
record
{ to = to A≃B
; from = from A≃B
; from∘to = from∘to A≃B
}
-- Equivalence of preprositions (also known as "if and only if"):
infix 0 _⇔_
record _⇔_ (A B : Set) : Set where
field
to : A → B
from : B → A
open _⇔_
-- Show that equivalence is reflexive, symmetric, and transitive.
⇔-refl : ∀ {A : Set}
------
→ A ⇔ A
⇔-refl =
record
{ to = λ{x → x}
; from = λ{x → x}
}
⇔-sym : ∀ {A B : Set}
→ A ⇔ B
------
→ B ⇔ A
⇔-sym A⇔B =
record
{ to = from A⇔B
; from = to A⇔B
}
⇔-trans : ∀ {A B C : Set}
→ A ⇔ B
→ B ⇔ C
------
→ A ⇔ C
⇔-trans A⇔B B⇔C =
record
{ to = to B⇔C ∘ to A⇔B
; from = from A⇔B ∘ from B⇔C
}
{-
Recall that Exercises Bin and Bin-laws define a datatype Bin of bitstrings representing natural numbers,
and asks you to define the following functions and predicates:
to : ℕ → Bin
from : Bin → ℕ
which satisfy the following property:
from (to n) ≡ n
Using the above, establish that there is an embedding of ℕ into Bin.
-}
open import plfa.part1.Induction using (Bin; ⟨⟩; _O; _I; inc; to; from; from∘to)
ℕ≲Bin : ℕ ≲ Bin
ℕ≲Bin = record
{ to = plfa.part1.Induction.to
; from = plfa.part1.Induction.from
; from∘to = plfa.part1.Induction.from∘to
}
-- Why do to and from not form an isomorphism?
-- Only canonical bitstrings are isomorphic to natural numbers:
--
-- ℕ≲Can : ∀ (b : Bin)
-- → ℕ ≲ Can b
---------------------------------------------------------------------
-- Standard library
-- The standard library _↔_ and _↞_ correspond to our _≃_ and _≲_, respectively,
-- but those in the standard library are less convenient, since they depend on a nested record structure
-- and are parameterised with regard to an arbitrary notion of equivalence.
import Function using (_∘_)
import Function.Inverse using (_↔_)
import Function.LeftInverse using (_↞_)