-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpairing.v
254 lines (219 loc) · 6.33 KB
/
pairing.v
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
(* Copyright (c) 2014, Robert Dockins *)
Require Import NArith.
Local Open Scope N_scope.
(** * Bitwise pairing function on [N].
Here we define a "bitwise" pairing isomorphism
on the nonnegative integers. This is one of
many different ways to witness the isormporphism
between [N] and [N×N].
We will use the pairing function to define the union
of countable sets and other similar constructions.
The details of the isomorphism are unimportant
once the isomorphism is defined. We prove this particular
isomorphism because it requires almost no facts about
arithmetic, and the proofs go by simple inductions on the
binary representation of positives.
*)
Fixpoint inflate (x:positive) : positive :=
match x with
| xH => xO xH
| xO x' => xO (xO (inflate x'))
| xI x' => xO (xI (inflate x'))
end.
Fixpoint inflate' (x:positive) : positive :=
match x with
| xH => xH
| xO x' => xO (xO (inflate' x'))
| xI x' => xI (xO (inflate' x'))
end.
Fixpoint deflate (x:positive) : N :=
match x with
| xH => N0
| xO xH => Npos xH
| xI xH => Npos xH
| xO (xO x') => match deflate x' with N0 => N0 | Npos q => Npos (xO q) end
| xI (xO x') => match deflate x' with N0 => N0 | Npos q => Npos (xO q) end
| xO (xI x') => match deflate x' with N0 => Npos xH | Npos q => Npos (xI q) end
| xI (xI x') => match deflate x' with N0 => Npos xH | Npos q => Npos (xI q) end
end.
Fixpoint deflate' (x:positive) : N :=
match x with
| xH => Npos xH
| xO xH => N0
| xI xH => Npos xH
| xO (xO x') => match deflate' x' with N0 => N0 | Npos q => Npos (xO q) end
| xI (xO x') => match deflate' x' with N0 => Npos xH | Npos q => Npos (xI q) end
| xO (xI x') => match deflate' x' with N0 => N0 | Npos q => Npos (xO q) end
| xI (xI x') => match deflate' x' with N0 => Npos xH | Npos q => Npos (xI q) end
end.
Lemma deflate_inflate0 : forall x,
deflate (inflate x) = Npos x.
Proof.
induction x; simpl; intros; auto.
- rewrite IHx; auto.
- rewrite IHx; auto.
Qed.
Lemma deflate_inflate0' : forall y,
deflate' (inflate' y) = Npos y.
Proof.
induction y; simpl; intros; auto.
- rewrite IHy; auto.
- rewrite IHy; auto.
Qed.
Lemma deflate_inflate1 : forall y,
deflate (inflate' y) = 0.
Proof.
induction y; simpl; auto.
- rewrite IHy; auto.
- rewrite IHy; auto.
Qed.
Lemma deflate_inflate1' : forall x,
deflate' (inflate x) = 0.
Proof.
induction x; simpl; auto.
- rewrite IHx; auto.
- rewrite IHx; auto.
Qed.
Lemma deflate_inflate : forall x y,
deflate (inflate x + inflate' y) = Npos x.
Proof.
induction x; simpl; intros.
- destruct y; simpl; f_equal; auto.
+ rewrite IHx. auto.
+ rewrite IHx. auto.
+ rewrite deflate_inflate0. auto.
- destruct y; simpl; f_equal; auto.
+ rewrite IHx. auto.
+ rewrite IHx. auto.
+ rewrite deflate_inflate0. auto.
- destruct y; simpl; f_equal; auto.
+ rewrite deflate_inflate1. auto.
+ rewrite deflate_inflate1. auto.
Qed.
Lemma deflate_inflate' : forall y x,
deflate' (inflate x + inflate' y) = Npos y.
Proof.
induction y; simpl; intros; auto.
- destruct x; simpl.
+ rewrite IHy. auto.
+ rewrite IHy. auto.
+ rewrite deflate_inflate0'. auto.
- destruct x; simpl.
+ rewrite IHy. auto.
+ rewrite IHy. auto.
+ rewrite deflate_inflate0'. auto.
- destruct x; simpl; auto.
+ rewrite deflate_inflate1'. auto.
+ rewrite deflate_inflate1'. auto.
Qed.
Lemma deflate00 : forall p, deflate (p~0~0) = 2*(deflate p).
Proof.
intros. simpl.
case_eq (deflate p); auto.
Qed.
Lemma deflate01 : forall p, deflate (p~0~1) = 2*(deflate p).
Proof.
intros. simpl.
case_eq (deflate p); auto.
Qed.
Lemma deflate10 : forall p, deflate (p~1~0) = 2*(deflate p) + 1 .
Proof.
intros. simpl.
case_eq (deflate p); auto.
Qed.
Lemma deflate11 : forall p, deflate (p~1~1) = 2*(deflate p) + 1 .
Proof.
intros. simpl.
case_eq (deflate p); auto.
Qed.
Lemma deflate00' : forall p, deflate' (p~0~0) = 2*(deflate' p).
Proof.
intros. simpl.
case_eq (deflate' p); auto.
Qed.
Lemma deflate01' : forall p, deflate' (p~0~1) = 2*(deflate' p)+1.
Proof.
intros. simpl.
case_eq (deflate' p); auto.
Qed.
Lemma deflate10' : forall p, deflate' (p~1~0) = 2*(deflate' p).
Proof.
intros. simpl.
case_eq (deflate' p); auto.
Qed.
Lemma deflate11' : forall p, deflate' (p~1~1) = 2*(deflate' p) + 1 .
Proof.
intros. simpl.
case_eq (deflate' p); auto.
Qed.
Definition pairing (p:N*N) : N :=
match p with
| (N0, N0) => N0
| (N0, Npos y) => Npos (inflate' y)
| (Npos x, N0) => Npos (inflate x)
| (Npos x, Npos y) => Npos (inflate x + inflate' y)
end.
Definition unpairing (z:N) : N*N :=
match z with
| N0 => (N0,N0)
| Npos z => (deflate z, deflate' z)
end.
Lemma pairing00 : forall p q,
pairing (2*p, 2*q) = 4*pairing (p,q).
Proof.
simpl; intros.
destruct p; destruct q; simpl; auto.
Qed.
Lemma pairing10 : forall p q,
pairing (2*p + 1, 2*q) = 4*pairing (p,q)+2.
Proof.
simpl; intros.
destruct p; destruct q; simpl; auto.
Qed.
Lemma pairing01 : forall p q,
pairing (2*p, 2*q + 1) = 4*pairing (p,q)+1.
Proof.
simpl; intros.
destruct p; destruct q; simpl; auto.
Qed.
Lemma pairing11 : forall p q,
pairing (2*p + 1, 2*q + 1) = 4*pairing (p,q)+3.
Proof.
simpl; intros.
destruct p; destruct q; simpl; auto.
Qed.
Lemma unpairing_pairing : forall p, unpairing (pairing p) = p.
Proof.
intros [x y].
destruct x; destruct y; simpl; auto.
- rewrite deflate_inflate1. rewrite deflate_inflate0'. auto.
- rewrite deflate_inflate1'. rewrite deflate_inflate0. auto.
- rewrite deflate_inflate.
rewrite deflate_inflate'.
auto.
Qed.
Lemma pairing_unpairing : forall z, pairing (unpairing z) = z.
Proof.
intro z. destruct z. simpl; auto.
unfold unpairing.
revert p. fix pairing_unpairing 1. intro p.
destruct p.
- destruct p.
+ rewrite deflate11. rewrite deflate11'.
rewrite pairing11. rewrite pairing_unpairing.
auto.
+ rewrite deflate01. rewrite deflate01'.
rewrite pairing01. rewrite pairing_unpairing.
auto.
+ simpl. auto.
- destruct p.
+ rewrite deflate10. rewrite deflate10'.
rewrite pairing10. rewrite pairing_unpairing.
auto.
+ rewrite deflate00. rewrite deflate00'.
rewrite pairing00. rewrite pairing_unpairing.
auto.
+ simpl. auto.
- simpl. auto.
Qed.
Global Opaque unpairing pairing.