tinyevil

Untitled

Jan 25th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1.  
  2. Inductive bin :=
  3. | zero : bin
  4. | twice : bin -> bin
  5. | twice_and_one : bin -> bin.
  6.  
  7. Fixpoint incr (b:bin) :=
  8. match b with
  9. | zero => twice_and_one zero
  10. | twice b' => twice_and_one b'
  11. | twice_and_one b' => twice (incr b')
  12. end.
  13.  
  14. Fixpoint bin_to_nat (b:bin) : nat :=
  15. match b with
  16. | zero => 0
  17. | twice b' => 2 * bin_to_nat b'
  18. | twice_and_one b' => S (2* bin_to_nat b')
  19. end.
  20.  
  21. Example test_bin_incr1 : bin_to_nat (zero) = 0.
  22. Proof. reflexivity. Qed.
  23.  
  24. Example test_bin_incr2 : bin_to_nat (incr zero) = 1.
  25. Proof. reflexivity. Qed.
  26.  
  27. Example test_bin_incr3 : bin_to_nat (incr (incr zero)) = 2.
  28. Proof. reflexivity. Qed.
  29.  
  30. Example test_bin_incr4 : bin_to_nat (incr (incr (incr zero))) = 3.
  31. Proof. reflexivity. Qed.
  32.  
  33. Example test_bin_incr5 : bin_to_nat (incr (twice_and_one (twice_and_one (twice_and_one zero)))) = 8.
  34. Proof. reflexivity. Qed.
  35.  
  36. Fixpoint nat_to_bin (n:nat) :=
  37. match n with
  38. | 0 => zero
  39. | S n' => incr (nat_to_bin n')
  40. end.
  41.  
  42. Lemma incr_increments : forall b:bin, bin_to_nat (incr b) = S (bin_to_nat b).
  43. Proof.
  44. induction b.
  45. - reflexivity.
  46. - reflexivity.
  47. - simpl.
  48. rewrite IHb.
  49. rewrite <- plus_n_O.
  50. rewrite <- plus_n_O.
  51. simpl.
  52. rewrite <- plus_n_Sm.
  53. reflexivity.
  54. Qed.
  55.  
  56. Theorem nat_to_bin_to_nat : forall n:nat, bin_to_nat (nat_to_bin n) = n.
  57. Proof.
  58. induction n.
  59. - reflexivity.
  60. - simpl.
  61. rewrite incr_increments.
  62. rewrite IHn.
  63. reflexivity.
  64. Qed.
  65.  
  66. Fact bin_to_nat_to_bin_notwork : ~ forall b:bin, nat_to_bin (bin_to_nat b) = b.
  67. Proof.
  68. intros h.
  69. assert (nat_to_bin (bin_to_nat (twice zero)) = zero).
  70. { reflexivity. }
  71. assert (nat_to_bin (bin_to_nat (twice zero)) = twice zero).
  72. { rewrite h. reflexivity. }
  73. congruence.
  74. Qed.
  75.  
  76. Fixpoint normalize (b:bin) : bin :=
  77. match b with
  78. | zero => zero
  79. | twice b' =>
  80. match normalize b' with
  81. | zero => zero
  82. | b'' => twice b''
  83. end
  84. | twice_and_one b' => twice_and_one (normalize b')
  85. end.
  86.  
  87. Lemma move_incr_out : forall b:bin, incr (normalize b) = normalize (incr b).
  88. Proof.
  89. induction b.
  90. - reflexivity.
  91. - simpl.
  92. destruct (normalize b).
  93. + reflexivity.
  94. + reflexivity.
  95. + reflexivity.
  96. - simpl.
  97. rewrite <- IHb.
  98. destruct (normalize b).
  99. + reflexivity.
  100. + reflexivity.
  101. + reflexivity.
  102. Qed.
  103.  
  104. Lemma move_twice_out : forall n:nat, nat_to_bin (2 * n) = normalize (twice (nat_to_bin n)).
  105. Proof.
  106. intros n.
  107. induction n.
  108. - reflexivity.
  109. - assert (H : nat_to_bin (2 * S n) = incr (incr (nat_to_bin (2 * n)))).
  110. simpl.
  111. rewrite <- plus_n_O.
  112. rewrite <- plus_n_Sm.
  113. simpl.
  114. reflexivity.
  115. rewrite H.
  116. rewrite IHn.
  117. rewrite move_incr_out.
  118. rewrite move_incr_out.
  119. reflexivity.
  120. Qed.
  121.  
  122. Theorem normalize_id : forall b:bin, normalize b = normalize (normalize b).
  123. Proof.
  124. induction b.
  125. - reflexivity.
  126. - simpl. destruct (normalize b).
  127. + reflexivity.
  128. + simpl.
  129. rewrite IHb.
  130. simpl.
  131. Admitted.
  132.  
  133. Theorem bin_to_nat_to_bin_norm : forall b:bin, nat_to_bin (bin_to_nat b) = normalize b.
  134. Proof.
  135. intros b.
  136. induction b.
  137. - reflexivity.
  138. - simpl.
  139. rewrite <- plus_n_O.
  140. assert (H:bin_to_nat b + bin_to_nat b = 2 * bin_to_nat b).
  141. { simpl. rewrite <- plus_n_O. reflexivity. }
  142. rewrite H.
  143. rewrite move_twice_out.
  144. simpl.
  145. rewrite IHb.
  146. rewrite <- normalize_id.
  147. reflexivity.
  148. - simpl.
  149. rewrite <- plus_n_O.
  150. assert (H:bin_to_nat b + bin_to_nat b = 2 * bin_to_nat b).
  151. { simpl. rewrite <- plus_n_O. reflexivity. }
  152. rewrite H.
  153. rewrite move_twice_out.
  154. simpl.
  155. rewrite IHb.
  156. rewrite <- normalize_id.
  157. destruct (normalize b).
  158. + reflexivity.
  159. + reflexivity.
  160. + reflexivity.
  161. Qed.
Add Comment
Please, Sign In to add comment