Guest User

to_decimal

a guest
May 14th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. Require Import PeanoNat Lia List Wf_nat.
  2. Import ListNotations.
  3.  
  4. Fixpoint to_decimal_helper (n : nat) (dummy : nat) :=
  5. match dummy with
  6. | 0 => []
  7. | S dummy' => if n =? 0
  8. then []
  9. else (to_decimal_helper (n / 10) dummy') ++ [ n mod 10 ]
  10. end.
  11.  
  12. Definition to_decimal (n: nat) :=
  13. if n =? 0 then [0] else to_decimal_helper n n.
  14.  
  15. Lemma to_decimal_helper_0:
  16. forall m, to_decimal_helper 0 m = [].
  17. Proof.
  18. destruct m; trivial.
  19. Qed.
  20.  
  21. Lemma to_decimal_helper_step:
  22. forall n m, n <> 0 -> m <> 0 -> to_decimal_helper n m = (to_decimal_helper (n / 10) (pred m)) ++ [ n mod 10 ].
  23. Proof.
  24. destruct n, m; easy.
  25. Qed.
  26.  
  27. Lemma arithmetic_helper:
  28. forall n, n >= S n / 10.
  29. Proof.
  30. destruct n; unfold ge.
  31. easy.
  32. eapply Nat.le_trans.
  33. apply (Nat.div_le_mono _ ((S n)* 10)).
  34. 1,2: lia.
  35. rewrite Nat.div_mul.
  36. all: lia.
  37. Qed.
  38.  
  39. Lemma to_decimal_helper_large_stoping_limit:
  40. forall m n, m >= n -> to_decimal_helper n m = to_decimal_helper n n.
  41. Proof.
  42. intros m n; revert m.
  43. induction n using (well_founded_induction lt_wf).
  44. rename H into IH.
  45. destruct n.
  46. { (* case n = 0 *)
  47. intros; rewrite !to_decimal_helper_0; reflexivity.
  48. }
  49. { intros m MN.
  50. rewrite (to_decimal_helper_step _ m).
  51. 2,3: lia.
  52. rewrite (to_decimal_helper_step _ (S n)).
  53. 2,3: lia.
  54. rewrite Nat.pred_succ.
  55. rewrite IH.
  56. erewrite (IH (S n / 10) _ n).
  57. reflexivity.
  58. all: clear IH.
  59. * apply arithmetic_helper.
  60. * apply Nat.lt_succ_r, arithmetic_helper.
  61. * unfold ge in *.
  62. eapply Nat.le_trans.
  63. apply arithmetic_helper.
  64. destruct m.
  65. lia.
  66. simpl.
  67. apply le_S_n.
  68. assumption.
  69. }
  70. Unshelve.
  71. apply Nat.lt_succ_r, arithmetic_helper.
  72. Qed.
  73.  
  74. Lemma beq_0:
  75. forall n, n <> 0 -> (n =? 0) = false.
  76. Proof.
  77. intro.
  78. rewrite Nat.eqb_neq.
  79. easy.
  80. Qed.
  81.  
  82. Theorem to_decimal_recurrence:
  83. forall n, (n < 10 /\ to_decimal n = [n]) \/
  84. (n >= 10 /\ to_decimal n = to_decimal (n / 10) ++ [ n mod 10 ]).
  85. Proof.
  86. intros.
  87. destruct (Nat.lt_ge_cases n 10).
  88. { left; split; [assumption | ].
  89. destruct n; [trivial | ].
  90. unfold to_decimal.
  91. rewrite beq_0.
  92. 2: lia.
  93. rewrite to_decimal_helper_step.
  94. 2,3: lia.
  95. rewrite Nat.div_small.
  96. 2: assumption.
  97. rewrite to_decimal_helper_0.
  98. rewrite Nat.mod_small.
  99. 2: assumption.
  100. easy.
  101. }
  102. { right; split; [assumption | ].
  103. unfold to_decimal.
  104. rewrite beq_0.
  105. 2: lia.
  106. rewrite beq_0.
  107. 2:{ apply (Nat.div_le_mono _ _ 10) in H.
  108. 2: lia.
  109. remember (n / 10) as n'.
  110. clear Heqn'.
  111. simpl in H; lia.
  112. }
  113. rewrite to_decimal_helper_step.
  114. 2,3: lia.
  115. f_equal.
  116. apply to_decimal_helper_large_stoping_limit.
  117. destruct n.
  118. easy.
  119. rewrite Nat.pred_succ.
  120. apply arithmetic_helper.
  121. }
  122. Qed.
  123.  
Advertisement
Add Comment
Please, Sign In to add comment