summaryrefslogtreecommitdiff
path: root/theories/IntMap/Addr.v
blob: 727117b3f8e190fca98ed4d7a4c703e13f4e7a04 (plain)
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
(*   \VV/  **************************************************************)
(*    //   *      This file is distributed under the terms of the       *)
(*         *       GNU Lesser General Public License Version 2.1        *)
(************************************************************************)
(*i 	$Id: Addr.v 5920 2004-07-16 20:01:26Z herbelin $	 i*)

(** Representation of adresses by the [positive] type of binary numbers *)

Require Import Bool.
Require Import ZArith.

Inductive ad : Set :=
  | ad_z : ad
  | ad_x : positive -> ad.

Lemma ad_sum : forall a:ad, {p : positive | a = ad_x p} + {a = ad_z}.
Proof.
  destruct a; auto.
  left; exists p; trivial.
Qed.

Fixpoint p_xor (p p2:positive) {struct p} : ad :=
  match p with
  | xH =>
      match p2 with
      | xH => ad_z
      | xO p'2 => ad_x (xI p'2)
      | xI p'2 => ad_x (xO p'2)
      end
  | xO p' =>
      match p2 with
      | xH => ad_x (xI p')
      | xO p'2 =>
          match p_xor p' p'2 with
          | ad_z => ad_z
          | ad_x p'' => ad_x (xO p'')
          end
      | xI p'2 =>
          match p_xor p' p'2 with
          | ad_z => ad_x 1
          | ad_x p'' => ad_x (xI p'')
          end
      end
  | xI p' =>
      match p2 with
      | xH => ad_x (xO p')
      | xO p'2 =>
          match p_xor p' p'2 with
          | ad_z => ad_x 1
          | ad_x p'' => ad_x (xI p'')
          end
      | xI p'2 =>
          match p_xor p' p'2 with
          | ad_z => ad_z
          | ad_x p'' => ad_x (xO p'')
          end
      end
  end.

Definition ad_xor (a a':ad) :=
  match a with
  | ad_z => a'
  | ad_x p => match a' with
              | ad_z => a
              | ad_x p' => p_xor p p'
              end
  end.

Lemma ad_xor_neutral_left : forall a:ad, ad_xor ad_z a = a.
Proof.
  trivial.
Qed.

Lemma ad_xor_neutral_right : forall a:ad, ad_xor a ad_z = a.
Proof.
  destruct a; trivial.
Qed.

Lemma ad_xor_comm : forall a a':ad, ad_xor a a' = ad_xor a' a.
Proof.
  destruct a; destruct a'; simpl in |- *; auto.
  generalize p0; clear p0; induction p as [p Hrecp| p Hrecp| ]; simpl in |- *;
   auto.
  destruct p0; simpl in |- *; trivial; intros.
  rewrite Hrecp; trivial.
  rewrite Hrecp; trivial.
  destruct p0; simpl in |- *; trivial; intros.
  rewrite Hrecp; trivial.
  rewrite Hrecp; trivial.
  destruct p0 as [p| p| ]; simpl in |- *; auto.
Qed.

Lemma ad_xor_nilpotent : forall a:ad, ad_xor a a = ad_z.
Proof.
  destruct a; trivial.
  simpl in |- *. induction p as [p IHp| p IHp| ]; trivial.
  simpl in |- *. rewrite IHp; reflexivity.
  simpl in |- *. rewrite IHp; reflexivity.
Qed.

Fixpoint ad_bit_1 (p:positive) : nat -> bool :=
  match p with
  | xH => fun n:nat => match n with
                       | O => true
                       | S _ => false
                       end
  | xO p =>
      fun n:nat => match n with
                   | O => false
                   | S n' => ad_bit_1 p n'
                   end
  | xI p => fun n:nat => match n with
                         | O => true
                         | S n' => ad_bit_1 p n'
                         end
  end.

Definition ad_bit (a:ad) :=
  match a with
  | ad_z => fun _:nat => false
  | ad_x p => ad_bit_1 p
  end.

Definition eqf (f g:nat -> bool) := forall n:nat, f n = g n.

Lemma ad_faithful_1 : forall a:ad, eqf (ad_bit ad_z) (ad_bit a) -> ad_z = a.
Proof.
  destruct a. trivial.
  induction p as [p IHp| p IHp| ]; intro H. absurd (ad_z = ad_x p). discriminate.
  exact (IHp (fun n:nat => H (S n))).
  absurd (ad_z = ad_x p). discriminate.
  exact (IHp (fun n:nat => H (S n))).
  absurd (false = true). discriminate.
  exact (H 0).
Qed.

Lemma ad_faithful_2 :
 forall a:ad, eqf (ad_bit (ad_x 1)) (ad_bit a) -> ad_x 1 = a.
Proof.
  destruct a. intros. absurd (true = false). discriminate.
  exact (H 0).
  destruct p. intro H. absurd (ad_z = ad_x p). discriminate.
  exact (ad_faithful_1 (ad_x p) (fun n:nat => H (S n))).
  intros. absurd (true = false). discriminate.
  exact (H 0).
  trivial.
Qed.

Lemma ad_faithful_3 :
 forall (a:ad) (p:positive),
   (forall p':positive, eqf (ad_bit (ad_x p)) (ad_bit (ad_x p')) -> p = p') ->
   eqf (ad_bit (ad_x (xO p))) (ad_bit a) -> ad_x (xO p) = a.
Proof.
  destruct a. intros. cut (eqf (ad_bit ad_z) (ad_bit (ad_x (xO p)))).
  intro. rewrite (ad_faithful_1 (ad_x (xO p)) H1). reflexivity.
  unfold eqf in |- *. intro. unfold eqf in H0. rewrite H0. reflexivity.
  case p. intros. absurd (false = true). discriminate.
  exact (H0 0).
  intros. rewrite (H p0 (fun n:nat => H0 (S n))). reflexivity.
  intros. absurd (false = true). discriminate.
  exact (H0 0).
Qed.

Lemma ad_faithful_4 :
 forall (a:ad) (p:positive),
   (forall p':positive, eqf (ad_bit (ad_x p)) (ad_bit (ad_x p')) -> p = p') ->
   eqf (ad_bit (ad_x (xI p))) (ad_bit a) -> ad_x (xI p) = a.
Proof.
  destruct a. intros. cut (eqf (ad_bit ad_z) (ad_bit (ad_x (xI p)))).
  intro. rewrite (ad_faithful_1 (ad_x (xI p)) H1). reflexivity.
  unfold eqf in |- *. intro. unfold eqf in H0. rewrite H0. reflexivity.
  case p. intros. rewrite (H p0 (fun n:nat => H0 (S n))). reflexivity.
  intros. absurd (true = false). discriminate.
  exact (H0 0).
  intros. absurd (ad_z = ad_x p0). discriminate.
  cut (eqf (ad_bit (ad_x 1)) (ad_bit (ad_x (xI p0)))).
  intro. exact (ad_faithful_1 (ad_x p0) (fun n:nat => H1 (S n))).
  unfold eqf in |- *. unfold eqf in H0. intro. rewrite H0. reflexivity.
Qed.

Lemma ad_faithful : forall a a':ad, eqf (ad_bit a) (ad_bit a') -> a = a'.
Proof.
  destruct a. exact ad_faithful_1.
  induction p. intros a' H. apply ad_faithful_4. intros. cut (ad_x p = ad_x p').
  intro. inversion H1. reflexivity.
  exact (IHp (ad_x p') H0).
  assumption.
  intros. apply ad_faithful_3. intros. cut (ad_x p = ad_x p'). intro. inversion H1. reflexivity.
  exact (IHp (ad_x p') H0).
  assumption.
  exact ad_faithful_2.
Qed.

Definition adf_xor (f g:nat -> bool) (n:nat) := xorb (f n) (g n).

Lemma ad_xor_sem_1 : forall a':ad, ad_bit (ad_xor ad_z a') 0 = ad_bit a' 0.
Proof.
  trivial.
Qed.

Lemma ad_xor_sem_2 :
 forall a':ad, ad_bit (ad_xor (ad_x 1) a') 0 = negb (ad_bit a' 0).
Proof.
  intro. case a'. trivial.
  simpl in |- *. intro. 
  case p; trivial.
Qed.

Lemma ad_xor_sem_3 :
 forall (p:positive) (a':ad),
   ad_bit (ad_xor (ad_x (xO p)) a') 0 = ad_bit a' 0.
Proof.
  intros. case a'. trivial.
  simpl in |- *. intro. 
  case p0; trivial. intro. 
  case (p_xor p p1); trivial.
  intro. case (p_xor p p1); trivial.
Qed.

Lemma ad_xor_sem_4 :
 forall (p:positive) (a':ad),
   ad_bit (ad_xor (ad_x (xI p)) a') 0 = negb (ad_bit a' 0).
Proof.
  intros. case a'. trivial.
  simpl in |- *. intro. case p0; trivial. intro. 
  case (p_xor p p1); trivial.
  intro. 
  case (p_xor p p1); trivial.
Qed.

Lemma ad_xor_sem_5 :
 forall a a':ad, ad_bit (ad_xor a a') 0 = adf_xor (ad_bit a) (ad_bit a') 0.
Proof.
  destruct a. intro. change (ad_bit a' 0 = xorb false (ad_bit a' 0)) in |- *. rewrite false_xorb. trivial.
  case p. exact ad_xor_sem_4.
  intros. change (ad_bit (ad_xor (ad_x (xO p0)) a') 0 = xorb false (ad_bit a' 0))
  in |- *.
  rewrite false_xorb. apply ad_xor_sem_3. exact ad_xor_sem_2.
Qed.

Lemma ad_xor_sem_6 :
 forall n:nat,
   (forall a a':ad, ad_bit (ad_xor a a') n = adf_xor (ad_bit a) (ad_bit a') n) ->
   forall a a':ad,
     ad_bit (ad_xor a a') (S n) = adf_xor (ad_bit a) (ad_bit a') (S n).
Proof.
  intros. case a. unfold adf_xor in |- *. unfold ad_bit at 2 in |- *. rewrite false_xorb. reflexivity.
  case a'. unfold adf_xor in |- *. unfold ad_bit at 3 in |- *. intro. rewrite xorb_false. reflexivity.
  intros. case p0. case p. intros.
  change
    (ad_bit (ad_xor (ad_x (xI p2)) (ad_x (xI p1))) (S n) =
     adf_xor (ad_bit (ad_x p2)) (ad_bit (ad_x p1)) n) 
   in |- *.
  rewrite <- H. simpl in |- *. 
  case (p_xor p2 p1); trivial.
  intros.
  change
    (ad_bit (ad_xor (ad_x (xI p2)) (ad_x (xO p1))) (S n) =
     adf_xor (ad_bit (ad_x p2)) (ad_bit (ad_x p1)) n) 
   in |- *.
  rewrite <- H. simpl in |- *. 
  case (p_xor p2 p1); trivial.
  intro. unfold adf_xor in |- *. unfold ad_bit at 3 in |- *. unfold ad_bit_1 in |- *. rewrite xorb_false. reflexivity.
  case p. intros.
  change
    (ad_bit (ad_xor (ad_x (xO p2)) (ad_x (xI p1))) (S n) =
     adf_xor (ad_bit (ad_x p2)) (ad_bit (ad_x p1)) n) 
   in |- *.
  rewrite <- H. simpl in |- *. 
  case (p_xor p2 p1); trivial.
  intros.
  change
    (ad_bit (ad_xor (ad_x (xO p2)) (ad_x (xO p1))) (S n) =
     adf_xor (ad_bit (ad_x p2)) (ad_bit (ad_x p1)) n) 
   in |- *.
  rewrite <- H. simpl in |- *. 
  case (p_xor p2 p1); trivial.
  intro. unfold adf_xor in |- *. unfold ad_bit at 3 in |- *. unfold ad_bit_1 in |- *. rewrite xorb_false. reflexivity.
  unfold adf_xor in |- *. unfold ad_bit at 2 in |- *. unfold ad_bit_1 in |- *. rewrite false_xorb. simpl in |- *.   case p; trivial.
Qed.

Lemma ad_xor_semantics :
 forall a a':ad, eqf (ad_bit (ad_xor a a')) (adf_xor (ad_bit a) (ad_bit a')).
Proof.
  unfold eqf in |- *. intros. generalize a a'. elim n. exact ad_xor_sem_5.
  exact ad_xor_sem_6.
Qed.

Lemma eqf_sym : forall f f':nat -> bool, eqf f f' -> eqf f' f.
Proof.
  unfold eqf in |- *. intros. rewrite H. reflexivity.
Qed.

Lemma eqf_refl : forall f:nat -> bool, eqf f f.
Proof.
  unfold eqf in |- *. trivial.
Qed.

Lemma eqf_trans :
 forall f f' f'':nat -> bool, eqf f f' -> eqf f' f'' -> eqf f f''.
Proof.
  unfold eqf in |- *. intros. rewrite H. exact (H0 n).
Qed.

Lemma adf_xor_eq :
 forall f f':nat -> bool, eqf (adf_xor f f') (fun n:nat => false) -> eqf f f'.
Proof.
  unfold eqf in |- *. unfold adf_xor in |- *. intros. apply xorb_eq. apply H.
Qed.

Lemma ad_xor_eq : forall a a':ad, ad_xor a a' = ad_z -> a = a'.
Proof.
  intros. apply ad_faithful. apply adf_xor_eq. apply eqf_trans with (f' := ad_bit (ad_xor a a')).
  apply eqf_sym. apply ad_xor_semantics.
  rewrite H. unfold eqf in |- *. trivial.
Qed.

Lemma adf_xor_assoc :
 forall f f' f'':nat -> bool,
   eqf (adf_xor (adf_xor f f') f'') (adf_xor f (adf_xor f' f'')).
Proof.
  unfold eqf in |- *. unfold adf_xor in |- *. intros. apply xorb_assoc.
Qed.

Lemma eqf_xor_1 :
 forall f f' f'' f''':nat -> bool,
   eqf f f' -> eqf f'' f''' -> eqf (adf_xor f f'') (adf_xor f' f''').
Proof.
  unfold eqf in |- *. intros. unfold adf_xor in |- *. rewrite H. rewrite H0. reflexivity.
Qed.

Lemma ad_xor_assoc :
 forall a a' a'':ad, ad_xor (ad_xor a a') a'' = ad_xor a (ad_xor a' a'').
Proof.
  intros. apply ad_faithful.
  apply eqf_trans with
   (f' := adf_xor (adf_xor (ad_bit a) (ad_bit a')) (ad_bit a'')).
  apply eqf_trans with (f' := adf_xor (ad_bit (ad_xor a a')) (ad_bit a'')).
  apply ad_xor_semantics.
  apply eqf_xor_1. apply ad_xor_semantics.
  apply eqf_refl.
  apply eqf_trans with
   (f' := adf_xor (ad_bit a) (adf_xor (ad_bit a') (ad_bit a''))).
  apply adf_xor_assoc.
  apply eqf_trans with (f' := adf_xor (ad_bit a) (ad_bit (ad_xor a' a''))).
  apply eqf_xor_1. apply eqf_refl.
  apply eqf_sym. apply ad_xor_semantics.
  apply eqf_sym. apply ad_xor_semantics.
Qed.

Definition ad_double (a:ad) :=
  match a with
  | ad_z => ad_z
  | ad_x p => ad_x (xO p)
  end.

Definition ad_double_plus_un (a:ad) :=
  match a with
  | ad_z => ad_x 1
  | ad_x p => ad_x (xI p)
  end.

Definition ad_div_2 (a:ad) :=
  match a with
  | ad_z => ad_z
  | ad_x xH => ad_z
  | ad_x (xO p) => ad_x p
  | ad_x (xI p) => ad_x p
  end.

Lemma ad_double_div_2 : forall a:ad, ad_div_2 (ad_double a) = a.
Proof.
  destruct a; trivial.
Qed.

Lemma ad_double_plus_un_div_2 :
 forall a:ad, ad_div_2 (ad_double_plus_un a) = a.
Proof.
  destruct a; trivial.
Qed.

Lemma ad_double_inj : forall a0 a1:ad, ad_double a0 = ad_double a1 -> a0 = a1.
Proof.
  intros. rewrite <- (ad_double_div_2 a0). rewrite H. apply ad_double_div_2.
Qed.

Lemma ad_double_plus_un_inj :
 forall a0 a1:ad, ad_double_plus_un a0 = ad_double_plus_un a1 -> a0 = a1.
Proof.
  intros. rewrite <- (ad_double_plus_un_div_2 a0). rewrite H. apply ad_double_plus_un_div_2.
Qed.

Definition ad_bit_0 (a:ad) :=
  match a with
  | ad_z => false
  | ad_x (xO _) => false
  | _ => true
  end.

Lemma ad_double_bit_0 : forall a:ad, ad_bit_0 (ad_double a) = false.
Proof.
  destruct a; trivial.
Qed.

Lemma ad_double_plus_un_bit_0 :
 forall a:ad, ad_bit_0 (ad_double_plus_un a) = true.
Proof.
  destruct a; trivial.
Qed.

Lemma ad_div_2_double :
 forall a:ad, ad_bit_0 a = false -> ad_double (ad_div_2 a) = a.
Proof.
  destruct a. trivial. destruct p. intro H. discriminate H.
  intros. reflexivity.
  intro H. discriminate H.
Qed.

Lemma ad_div_2_double_plus_un :
 forall a:ad, ad_bit_0 a = true -> ad_double_plus_un (ad_div_2 a) = a.
Proof.
  destruct a. intro. discriminate H.
  destruct p. intros. reflexivity.
  intro H. discriminate H.
  intro. reflexivity.
Qed.

Lemma ad_bit_0_correct : forall a:ad, ad_bit a 0 = ad_bit_0 a.
Proof.
  destruct a; trivial.
  destruct p; trivial.
Qed.

Lemma ad_div_2_correct :
 forall (a:ad) (n:nat), ad_bit (ad_div_2 a) n = ad_bit a (S n).
Proof.
  destruct a; trivial.
  destruct p; trivial.
Qed.

Lemma ad_xor_bit_0 :
 forall a a':ad, ad_bit_0 (ad_xor a a') = xorb (ad_bit_0 a) (ad_bit_0 a').
Proof.
  intros. rewrite <- ad_bit_0_correct. rewrite (ad_xor_semantics a a' 0).
  unfold adf_xor in |- *. rewrite ad_bit_0_correct. rewrite ad_bit_0_correct. reflexivity.
Qed.

Lemma ad_xor_div_2 :
 forall a a':ad, ad_div_2 (ad_xor a a') = ad_xor (ad_div_2 a) (ad_div_2 a').
Proof.
  intros. apply ad_faithful. unfold eqf in |- *. intro.
  rewrite (ad_xor_semantics (ad_div_2 a) (ad_div_2 a') n).
  rewrite ad_div_2_correct.
  rewrite (ad_xor_semantics a a' (S n)).
  unfold adf_xor in |- *. rewrite ad_div_2_correct. rewrite ad_div_2_correct.
  reflexivity.
Qed.

Lemma ad_neg_bit_0 :
 forall a a':ad,
   ad_bit_0 (ad_xor a a') = true -> ad_bit_0 a = negb (ad_bit_0 a').
Proof.
  intros. rewrite <- true_xorb. rewrite <- H. rewrite ad_xor_bit_0.
  rewrite xorb_assoc. rewrite xorb_nilpotent. rewrite xorb_false. reflexivity.
Qed.

Lemma ad_neg_bit_0_1 :
 forall a a':ad, ad_xor a a' = ad_x 1 -> ad_bit_0 a = negb (ad_bit_0 a').
Proof.
  intros. apply ad_neg_bit_0. rewrite H. reflexivity.
Qed.

Lemma ad_neg_bit_0_2 :
 forall (a a':ad) (p:positive),
   ad_xor a a' = ad_x (xI p) -> ad_bit_0 a = negb (ad_bit_0 a').
Proof.
  intros. apply ad_neg_bit_0. rewrite H. reflexivity.
Qed.

Lemma ad_same_bit_0 :
 forall (a a':ad) (p:positive),
   ad_xor a a' = ad_x (xO p) -> ad_bit_0 a = ad_bit_0 a'.
Proof.
  intros. rewrite <- (xorb_false (ad_bit_0 a)). cut (ad_bit_0 (ad_x (xO p)) = false).
  intro. rewrite <- H0. rewrite <- H. rewrite ad_xor_bit_0. rewrite <- xorb_assoc.
  rewrite xorb_nilpotent. rewrite false_xorb. reflexivity.
  reflexivity.
Qed.