Advertisement
Guest User

relative_integers_in_coq

a guest
Oct 8th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.77 KB | None | 0 0
  1. (* compiles with COQ 8.7*)
  2. Theorem nsr_plus_associative: forall a b c:nat, a+(b+c) = (a+b)+c.
  3. Proof.
  4. induction a.
  5. intros.
  6. simpl.
  7. reflexivity.
  8. intros.
  9. simpl.
  10. rewrite IHa.
  11. reflexivity.
  12. Qed.
  13.  
  14. Theorem nsr_plus_commutative: forall x y:nat, x+y = y + x.
  15. Proof.
  16. induction x.
  17. intro.
  18. rewrite <-plus_n_O.
  19. simpl.
  20. reflexivity.
  21. intro.
  22. simpl.
  23. rewrite <-plus_n_Sm.
  24. rewrite IHx.
  25. reflexivity.
  26. Qed.
  27.  
  28. Theorem nsr_right_distributivity: forall x y z:nat, (x+y)*z = (x*z)+(y*z).
  29. Proof.
  30. induction x.
  31. simpl.
  32. reflexivity.
  33. simpl.
  34. intros.
  35. assert (z+ x * z + y * z = z + (x * z + y * z)).
  36. apply eq_sym.
  37. apply nsr_plus_associative.
  38. rewrite H.
  39. apply f_equal.
  40. apply IHx.
  41. Qed.
  42.  
  43. Theorem nsr_mult_commutative: forall x y:nat, x*y = y*x.
  44. Proof.
  45. induction x.
  46. simpl.
  47. intro.
  48. rewrite <- mult_n_O.
  49. reflexivity.
  50. simpl.
  51. assert (forall a b:nat, b * (S a) = b + b * a).
  52. induction b.
  53. simpl.
  54. reflexivity.
  55. simpl.
  56. rewrite IHb.
  57. apply f_equal.
  58. rewrite nsr_plus_associative.
  59. rewrite nsr_plus_associative.
  60. rewrite nsr_plus_commutative with (x:=a) (y:=b).
  61. reflexivity.
  62. intro.
  63. apply eq_sym.
  64. rewrite IHx.
  65. apply H.
  66. Qed.
  67.  
  68. Theorem nsr_left_distributivity: forall x y z:nat, z*(x+y) = (z*x)+(z*y).
  69. Proof.
  70. intros.
  71. rewrite nsr_mult_commutative with (x:=z) (y:=x+y).
  72. rewrite nsr_mult_commutative with (x:=z) (y:=x).
  73. rewrite nsr_mult_commutative with (x:=z) (y:=y).
  74. apply nsr_right_distributivity.
  75. Qed.
  76.  
  77. Theorem nsr_mult_associative: forall x y z:nat, x*(y*z)=(x*y)*z.
  78. Proof.
  79. induction x.
  80. intros.
  81. simpl.
  82. reflexivity.
  83. simpl.
  84. intros.
  85. rewrite nsr_right_distributivity.
  86. rewrite IHx.
  87. reflexivity.
  88. Qed.
  89.  
  90. Theorem nsr_O_absorbs: forall n:nat, 0 * n = 0.
  91. Proof.
  92. intros.
  93. simpl.
  94. reflexivity.
  95. Qed.
  96.  
  97. Theorem nsr_I_mult_neutral: forall n:nat, 1 * n = n.
  98. Proof.
  99. cut(forall n:nat, n * 1 =n ).
  100. intros.
  101. rewrite nsr_mult_commutative.
  102. apply H.
  103. induction n.
  104. simpl.
  105. reflexivity.
  106. simpl.
  107. rewrite IHn.
  108. reflexivity.
  109. Qed.
  110.  
  111. Theorem nsr_O_plus_neutral: forall n:nat, n = 0 + n.
  112. Proof.
  113. intros.
  114. simpl.
  115. reflexivity.
  116. Qed.
  117.  
  118. Lemma nat_sum_regularity: forall x y z:nat, x+ y = x + z -> y = z.
  119. Proof.
  120. induction x.
  121. simpl.
  122. intros.
  123. assumption.
  124. intro.
  125. intro.
  126. rewrite plus_Sn_m.
  127. rewrite plus_Sn_m.
  128. intro.
  129. apply IHx.
  130. apply eq_add_S.
  131. assumption.
  132. Qed.
  133.  
  134. Lemma eq_prod: forall a a' b b':nat, a=a' -> b=b' -> (a,b)=(a',b').
  135. Proof.
  136. intros.
  137. rewrite H.
  138. rewrite H0.
  139. reflexivity.
  140. Qed.
  141.  
  142. Notation Relative_integer := (prod nat nat).
  143.  
  144. Definition eqz (a b:Relative_integer):Prop := fst a + snd b = fst b + snd a.
  145.  
  146. Lemma eqz_reflexive: forall a:Relative_integer, eqz a a.
  147. Proof.
  148. intro.
  149. destruct a.
  150. unfold eqz.
  151. reflexivity.
  152. Qed.
  153.  
  154. Lemma eqz_symmetric: forall a b:Relative_integer, eqz a b -> eqz b a.
  155. Proof.
  156. unfold eqz.
  157. intros.
  158. rewrite H.
  159. reflexivity.
  160. Qed.
  161.  
  162. Lemma eqz_transitive: forall a b c:Relative_integer, eqz a b -> eqz b c -> eqz a c.
  163. Proof.
  164. unfold eqz.
  165. intro.
  166. intro.
  167. intro.
  168. destruct a as (p_a,q_a).
  169. destruct b as (p_b,q_b).
  170. destruct c as (p_c,q_c).
  171. simpl.
  172. intros.
  173. apply nat_sum_regularity with (x:= q_b).
  174. rewrite nsr_plus_associative.
  175. rewrite nsr_plus_associative.
  176. assert (q_b+p_a=p_a+q_b).
  177. apply nsr_plus_commutative.
  178. rewrite H1.
  179. rewrite H.
  180. assert (q_b + p_c = p_c + q_b).
  181. apply nsr_plus_commutative.
  182. rewrite H2.
  183. rewrite <- H0.
  184. rewrite <- nsr_plus_associative.
  185. rewrite <- nsr_plus_associative.
  186. apply f_equal.
  187. apply nsr_plus_commutative.
  188. Qed.
  189.  
  190. Definition Z_plus (a b:Relative_integer):Relative_integer := (fst a + fst b, snd a + snd b).
  191. Definition Z_zero:Relative_integer:= (0,0).
  192. Definition Z_un:Relative_integer:= (1,0).
  193.  
  194. Lemma Z_plus_associative_equal:
  195. forall (a b c:Relative_integer), Z_plus a (Z_plus b c) = Z_plus (Z_plus a b) c.
  196. Proof.
  197. intro.
  198. destruct a as (p_a,q_a).
  199. intro.
  200. destruct b as (p_b,q_b).
  201. intro.
  202. destruct c as (p_c,q_c).
  203. unfold Z_plus.
  204. simpl.
  205. assert (forall u v w x:nat, u=w -> v = x -> (u,v)=(w,x)) as LEMMA.
  206. intros.
  207. rewrite H.
  208. rewrite H0.
  209. reflexivity.
  210. apply LEMMA.
  211. apply nsr_plus_associative.
  212. apply nsr_plus_associative.
  213. Qed.
  214.  
  215. Theorem Z_plus_associative: forall (a b c:Relative_integer),
  216. eqz ( Z_plus a (Z_plus b c)) ( Z_plus (Z_plus a b) c).
  217. Proof.
  218. intros.
  219. rewrite Z_plus_associative_equal.
  220. apply eqz_reflexive.
  221. Qed.
  222.  
  223. Notation "a +Z b":= (Z_plus a b) (left associativity, at level 61).
  224. Notation "a =Z b":= (eqz a b) (left associativity, at level 71).
  225.  
  226. Definition Z_mult (x y:Relative_integer):Relative_integer:=
  227. ((fst x)*(fst y) + (snd x)* (snd y), (fst x)*(snd y)+(snd x)*(fst y)).
  228.  
  229. Notation "a *Z b":= (Z_mult a b) (left associativity, at level 59).
  230.  
  231. (*the following are abreviations used in manually done computations *)
  232. Ltac arl p q r := rewrite nsr_plus_associative with (a:=p) (b:=q) (c:=r).
  233. Ltac alr p q r := rewrite <- nsr_plus_associative with (a:=p) (b:=q) (c:=r).
  234. Ltac com p q := rewrite nsr_plus_commutative with (x:= p) (y:=q).
  235.  
  236. Ltac mrl p q r := rewrite nsr_mult_associative with (x:=p) (y:=q) (z:=r).
  237. Ltac mlr p q r := rewrite <- nsr_mult_associative with (x:=p) (y:=q) (z:=r).
  238. Ltac mom p q := rewrite nsr_mult_commutative with (x:= p) (y:=q).
  239.  
  240. Ltac dir p q r := rewrite nsr_right_distributivity with (x:=p) (y:=q)(z:=r).
  241. Ltac dil p q r := rewrite nsr_left_distributivity with (x:=q) (y:=r) (z:=p).
  242. Ltac fir p q r := rewrite <- nsr_right_distributivity with (x:=p) (y:=q)(z:=r).
  243. Ltac fil p q r := rewrite <- nsr_left_distributivity with (x:=q) (y:=r) (z:=p).
  244.  
  245.  
  246. Theorem Z_mult_associative: forall x y z:Relative_integer, x *Z (y *Z z) =Z x *Z y *Z z.
  247. Proof.
  248. intro.
  249. destruct x as (a,b).
  250. intro.
  251. destruct y as (c,d).
  252. intro.
  253. destruct z as (e,f).
  254. unfold Z_mult.
  255. unfold eqz.
  256. simpl.
  257. rewrite nsr_left_distributivity with (z:=a) (x:=c * e) (y:= d * f).
  258. rewrite nsr_mult_associative with (x:= a) (y:=c) (z:=e).
  259. rewrite nsr_mult_associative with (x:= a) (y:=d) (z:=f).
  260. rewrite nsr_left_distributivity with (z:=b) (x:=c * f) (y:= d * e).
  261. rewrite nsr_mult_associative with (x:= b) (y:=c) (z:=f).
  262. rewrite nsr_mult_associative with (x:= b) (y:=d) (z:=e).
  263. rewrite nsr_left_distributivity with (z:=a) (x:=c * f) (y:= d * e).
  264. rewrite nsr_mult_associative with (x:= a) (y:=c) (z:=f).
  265. rewrite nsr_mult_associative with (x:= a) (y:=d) (z:=e).
  266. rewrite nsr_left_distributivity with (z:=b) (x:=c * e) (y:= d * f).
  267. rewrite nsr_mult_associative with (x:= b) (y:=c) (z:=e).
  268. rewrite nsr_mult_associative with (x:= b) (y:=d) (z:=f).
  269. rewrite nsr_right_distributivity with (x:= a * c) (y:= b * d) (z:=f).
  270. rewrite nsr_right_distributivity with (x:= a * d) (y:= b * c) (z:=e).
  271. rewrite nsr_right_distributivity with (x:= a * c) (y:= b * d) (z:=e).
  272. rewrite nsr_right_distributivity with (x:= a * d) (y:= b * c) (z:=f).
  273. assert (forall (ace adf bcf bde acf bdf ade bce:nat),
  274. ace + adf + (bcf + bde) + (acf + bdf + (ade + bce)) =
  275. ace + bde + (adf + bcf) + (acf + ade + (bce + bdf))) as L.
  276. intros.
  277. com bcf bde.
  278. alr ace adf (bde + bcf).
  279. arl adf bde bcf.
  280. com adf bde.
  281. alr ace bde (adf + bcf).
  282. alr bde adf bcf.
  283. com bce bdf.
  284. arl (acf + ade) bdf bce.
  285. alr acf ade bdf.
  286. com ade bdf.
  287. arl acf bdf ade.
  288. alr (acf + bdf) ade bce.
  289. reflexivity.
  290. apply L.
  291. Qed.
  292.  
  293. Theorem Z_plus_commutative: forall a b:Relative_integer, a +Z b =Z b +Z a.
  294. Proof.
  295. intros.
  296. unfold eqz.
  297. unfold Z_plus.
  298. simpl.
  299. com (fst a) (fst b).
  300. com (snd a) (snd b).
  301. reflexivity.
  302. Qed.
  303.  
  304. Theorem Z_mult_commutative: forall a b:Relative_integer, a *Z b =Z b *Z a.
  305. Proof.
  306. intros.
  307. unfold eqz.
  308. unfold Z_mult.
  309. simpl.
  310. mom (fst a) (fst b).
  311. mom (snd a) (snd b).
  312. mom (fst a) (snd b).
  313. mom (fst b) (snd a).
  314. com (snd a * fst b) (snd b * fst a).
  315. reflexivity.
  316. Qed.
  317.  
  318. Theorem Z_right_distributivity: forall a b c:Relative_integer, (a +Z b) *Z c =Z (a *Z c) +Z (b *Z c).
  319. Proof.
  320. intros.
  321. destruct a as (p,q).
  322. destruct b as (r,s).
  323. destruct c as (t,u).
  324. unfold eqz.
  325. unfold Z_plus.
  326. unfold Z_mult.
  327. simpl.
  328. dir p r t.
  329. dir q s u.
  330. dir p r u.
  331. dir q s t.
  332. assert (forall (a b c d:nat), a + b + (c + d) = a + c + (b + d)) as L.
  333. intros.
  334. alr a b (c+d).
  335. arl b c d.
  336. com b c.
  337. alr c b d.
  338. arl a c (b + d).
  339. reflexivity.
  340. rewrite L with (a:= p * t) (b:= r * t) (c:= q * u) (d:= s * u).
  341. apply f_equal.
  342. apply L.
  343. Qed.
  344.  
  345. Theorem Z_zero_plus_neutral: forall a:Relative_integer, Z_zero +Z a =Z a.
  346. Proof.
  347. intro.
  348. unfold eqz.
  349. unfold Z_plus.
  350. unfold Z_zero.
  351. simpl.
  352. reflexivity.
  353. Qed.
  354.  
  355. Notation O_Z:= (Z_zero).
  356. Notation I_Z := (Z_un).
  357.  
  358. Theorem Z_un_neutral: forall a:Relative_integer, I_Z *Z a =Z a.
  359. Proof.
  360. intros.
  361. destruct a as (x,y).
  362. unfold eqz.
  363. unfold Z_mult.
  364. unfold Z_un.
  365. simpl.
  366. alr x 0 0.
  367. alr x (0 + 0) y.
  368. com (0+0) y.
  369. arl y 0 0.
  370. reflexivity.
  371. Qed.
  372.  
  373. Definition Z_opp (a:Relative_integer):Relative_integer:= (snd a,fst a).
  374. Definition Z_minus (a b:Relative_integer):Relative_integer:= ((fst a) + (snd b), (snd a)+ (fst b)).
  375.  
  376. Notation "a -Z b" := (Z_minus a b) (at level 69).
  377.  
  378. Theorem Z_opp_group: forall x:Relative_integer, x +Z (Z_opp x) =Z O_Z.
  379. Proof.
  380. intros.
  381. destruct x as (p,q).
  382. unfold eqz.
  383. unfold Z_plus.
  384. unfold Z_opp.
  385. simpl.
  386. com p q.
  387. com (q + p) 0.
  388. simpl.
  389. reflexivity.
  390. Qed.
  391.  
  392. Theorem Z_minus_opp: forall x y:Relative_integer, x -Z y =Z x +Z (Z_opp y).
  393. Proof.
  394. intros.
  395. unfold eqz.
  396. unfold Z_plus.
  397. unfold Z_minus.
  398. unfold Z_opp.
  399. simpl.
  400. reflexivity.
  401. Qed.
  402.  
  403. Theorem Z_sum_compatibility: forall x x0 y y0:Relative_integer,
  404. x =Z y -> x0 =Z y0 -> (x +Z x0) =Z (y +Z y0).
  405. Proof.
  406. intro.
  407. intro.
  408. intro.
  409. intro.
  410. unfold eqz.
  411. unfold Z_plus.
  412. destruct x as (a,b).
  413. destruct y as (c,d).
  414. destruct x0 as (e,f).
  415. destruct y0 as (g,h).
  416. simpl.
  417. intros.
  418. arl (a+e) d h.
  419. alr a e d.
  420. com e d.
  421. arl a d e.
  422. rewrite H.
  423. alr (c+b) e h.
  424. rewrite H0.
  425. arl (c + b) g f.
  426. arl (c + g) b f.
  427. alr c b g.
  428. alr c g b.
  429. com b g.
  430. reflexivity.
  431. Qed.
  432.  
  433. Theorem Z_mult_compatibility: forall x x0 y y0:Relative_integer,
  434. x =Z y -> x0 =Z y0 -> (x *Z x0) =Z (y *Z y0).
  435. Proof.
  436. assert (forall p q r:Relative_integer, eqz p q -> eqz (p *Z r) (q *Z r)) as L.
  437. unfold eqz.
  438. unfold Z_mult.
  439. intro.
  440. intro.
  441. intro.
  442. destruct p as (a,b).
  443. destruct q as (c,d).
  444. destruct r as (x,y).
  445. simpl.
  446. intros.
  447. alr (a * x) (b * y) (c * y + d * x).
  448. arl (b * y) (c * y) (d * x).
  449. fir b c y.
  450. com b c.
  451. rewrite <- H.
  452. com (a * y) (b * x).
  453. com (c * x) (d * y).
  454. alr (d * y) (c * x) (b * x + a * y).
  455. arl (c * x) (b * x) (a * y).
  456. fir c b x.
  457. rewrite <- H.
  458. dir a d x.
  459. dir a d y.
  460. alr (a * x) (d * x) (a * y).
  461. com (d * x) (a * y).
  462. arl (a * x) (a * y) (d * x).
  463. arl (d * y) (a * x + a * y) (d * x).
  464. arl (d * y) (a * x) (a * y).
  465. com (d * y) (a * x).
  466. alr (a * x) (d * y) (a * y).
  467. arl (a * x) (a * y + d * y) (d * x).
  468. com (a * y) (d * y).
  469. reflexivity.
  470. intros.
  471. apply eqz_transitive with (b:= y *Z x0).
  472. apply L.
  473. assumption.
  474. apply eqz_transitive with (b:= x0 *Z y).
  475. apply Z_mult_commutative.
  476. apply eqz_transitive with (b:= y0 *Z y).
  477. apply L.
  478. assumption.
  479. apply Z_mult_commutative.
  480. Qed.
  481.  
  482. Theorem Z_opp_compatibility: forall x y:Relative_integer, x =Z y -> Z_opp x =Z Z_opp y.
  483. Proof.
  484. unfold eqz.
  485. unfold Z_opp.
  486. intros.
  487. simpl.
  488. rewrite nsr_plus_commutative.
  489. rewrite <- H.
  490. apply nsr_plus_commutative.
  491. Qed.
  492.  
  493. Theorem Z_left_distributivity: forall a b c:Relative_integer, c *Z (a +Z b) =Z (c *Z a) +Z (c *Z b).
  494. Proof.
  495. intros.
  496. apply eqz_transitive with (b:= (a +Z b) *Z c).
  497. apply Z_mult_commutative.
  498. apply eqz_transitive with (b:= (a *Z c)+Z (b *Z c)).
  499. apply Z_right_distributivity.
  500. apply Z_sum_compatibility.
  501. apply Z_mult_commutative.
  502. apply Z_mult_commutative.
  503. Qed.
  504.  
  505. Theorem Z_sum_regularity: forall x y z:Relative_integer, x +Z z =Z y +Z z -> x =Z y.
  506. Proof.
  507. intros.
  508. assert (forall p r:Relative_integer, p =Z (p +Z r) +Z (Z_opp r))as LEMMA1.
  509. intros.
  510. apply eqz_transitive with (b:= p +Z (r +Z (Z_opp r))).
  511. apply eqz_transitive with (b:= p +Z O_Z).
  512. apply eqz_transitive with (b:= O_Z +Z p).
  513. apply eqz_symmetric.
  514. apply Z_zero_plus_neutral.
  515. apply Z_plus_commutative.
  516. apply Z_sum_compatibility.
  517. apply eqz_reflexive.
  518. apply eqz_symmetric.
  519. apply Z_opp_group.
  520. apply Z_plus_associative.
  521. intros.
  522. apply eqz_transitive with (b:= (x +Z z) +Z (Z_opp z)).
  523. apply LEMMA1.
  524. apply eqz_transitive with (b:= (y +Z z) +Z (Z_opp z)).
  525. apply Z_sum_compatibility.
  526. assumption.
  527. apply eqz_reflexive.
  528. apply eqz_symmetric.
  529. apply LEMMA1.
  530. Qed.
  531.  
  532. Theorem Z_zero_product:forall x:Relative_integer, x *Z O_Z =Z O_Z.
  533. (*we show this property is a consequence of ring axioms as coq intended them, rather
  534. than using the specific structure of Relative_integer defined above*)
  535. Proof.
  536. intros.
  537. apply Z_sum_regularity with (z:= x *Z I_Z).
  538. apply eqz_transitive with (b:= x *Z (O_Z +Z I_Z)).
  539. apply eqz_symmetric.
  540. apply Z_left_distributivity.
  541. apply eqz_transitive with (b:= x *Z I_Z).
  542. apply Z_mult_compatibility.
  543. apply eqz_reflexive.
  544. apply Z_zero_plus_neutral.
  545. apply eqz_symmetric.
  546. apply Z_zero_plus_neutral.
  547. Qed.
  548.  
  549. Theorem sign_rule:forall x y:Relative_integer,
  550. (Z_opp x) *Z (Z_opp y) =Z x *Z y.
  551. Proof.
  552. intros.
  553. assert (forall p r:Relative_integer, p =Z (p +Z r) +Z (Z_opp r))as LEMMA1.
  554. intros.
  555. apply eqz_transitive with (b:= p +Z (r +Z (Z_opp r))).
  556. apply eqz_transitive with (b:= p +Z O_Z).
  557. apply eqz_transitive with (b:= O_Z +Z p).
  558. apply eqz_symmetric.
  559. apply Z_zero_plus_neutral.
  560. apply Z_plus_commutative.
  561. apply Z_sum_compatibility.
  562. apply eqz_reflexive.
  563. apply eqz_symmetric.
  564. apply Z_opp_group.
  565. apply Z_plus_associative.
  566. assert (forall p q r:Relative_integer, p +Z r =Z q +Z r -> p =Z q) as LEMMA2.
  567. intros.
  568. apply eqz_transitive with (b:= (p +Z r) +Z (Z_opp r)).
  569. apply LEMMA1.
  570. apply eqz_transitive with (b:= (q +Z r) +Z (Z_opp r)).
  571. apply Z_sum_compatibility.
  572. assumption.
  573. apply eqz_reflexive.
  574. apply eqz_symmetric.
  575. apply LEMMA1.
  576. apply LEMMA2 with (r:= (Z_opp x) *Z y).
  577. apply eqz_transitive with (b:= O_Z).
  578. apply eqz_transitive with (b:= (Z_opp x) *Z ((Z_opp y +Z y))).
  579. apply eqz_symmetric.
  580. apply Z_left_distributivity.
  581. apply eqz_transitive with (b:= Z_opp x *Z O_Z).
  582. apply Z_mult_compatibility.
  583. apply eqz_reflexive.
  584. apply eqz_transitive with (b:= y +Z (Z_opp y)).
  585. apply Z_plus_commutative.
  586. apply Z_opp_group.
  587. apply Z_zero_product.
  588. apply eqz_transitive with (b:= O_Z *Z y).
  589. apply eqz_symmetric.
  590. apply eqz_transitive with (b:= y *Z O_Z).
  591. apply eqz_symmetric.
  592. apply Z_mult_commutative.
  593. apply Z_zero_product.
  594. apply eqz_transitive with (b:= (x +Z (Z_opp x)) *Z y).
  595. apply Z_mult_compatibility.
  596. apply eqz_symmetric.
  597. apply Z_opp_group.
  598. apply eqz_reflexive.
  599. apply Z_right_distributivity.
  600. Qed.
  601.  
  602. Ltac zer := apply eqz_reflexive.
  603. Ltac zes := apply eqz_symmetric.
  604. Ltac zet p:= apply eqz_transitive with (b:=p).
  605. Ltac zcom:= apply Z_mult_commutative.
  606.  
  607. Section Signs_of_relative_integers.
  608. Fixpoint zsign_nat (p q:nat):Relative_integer:=
  609. match p with
  610. |0 => match q with
  611. |0 => O_Z
  612. |S t => (0,1)
  613. end
  614. |S r => match q with
  615. |0 => I_Z
  616. |S u => zsign_nat r u
  617. end
  618. end.
  619.  
  620. Definition zsign (x:Relative_integer):Relative_integer:= zsign_nat (fst x) (snd x).
  621.  
  622. Lemma calc_AOO: forall x:nat, zsign_nat x x = O_Z.
  623. Proof.
  624. induction x.
  625. simpl.
  626. reflexivity.
  627. simpl.
  628. apply IHx.
  629. Qed.
  630.  
  631. Lemma calc_AOS: forall x y:nat, zsign_nat x (S (y + x)) = (0,1).
  632. Proof.
  633. induction x.
  634. intro.
  635. simpl.
  636. reflexivity.
  637. intros.
  638. simpl.
  639. rewrite <- plus_n_Sm with (n:=y) (m:=x).
  640. apply IHx.
  641. Qed.
  642.  
  643. Lemma calc_ASO: forall x y:nat, zsign_nat (S (y + x)) x = (1,0).
  644. Proof.
  645. induction x.
  646. intro.
  647. simpl.
  648. reflexivity.
  649. intro.
  650. simpl.
  651. rewrite <- plus_n_Sm with (n:= y) (m:= x).
  652. apply IHx.
  653. Qed.
  654.  
  655.  
  656. Lemma zsign_compat: forall (x y:Relative_integer), x =Z y -> zsign x = zsign y.
  657. Proof.
  658. assert (forall a b c d:nat, a+d=b+c -> zsign_nat a b = zsign_nat c d) as L.
  659. induction a.
  660. intros.
  661. induction b.
  662. rewrite plus_O_n in H.
  663. rewrite plus_O_n in H.
  664. rewrite H.
  665. rewrite calc_AOO.
  666. rewrite calc_AOO.
  667. reflexivity.
  668. rewrite plus_O_n in H.
  669. rewrite H.
  670. simpl.
  671. rewrite calc_AOS.
  672. reflexivity.
  673. induction b.
  674. intros.
  675. rewrite plus_O_n in H.
  676. rewrite plus_Sn_m in H.
  677. rewrite <- H.
  678. rewrite calc_ASO.
  679. simpl.
  680. reflexivity.
  681. intros.
  682. simpl.
  683. rewrite plus_Sn_m with (n:=a) (m:=d) in H.
  684. rewrite plus_Sn_m with (n:=b) (m:=c) in H.
  685. apply eq_add_S in H.
  686. apply IHa.
  687. assumption.
  688. intro.
  689. intro.
  690. unfold eqz.
  691. unfold zsign.
  692. intros.
  693. apply L.
  694. rewrite H.
  695. apply nsr_plus_commutative.
  696. Qed.
  697.  
  698. Lemma cases_sign: forall x:Relative_integer,
  699. (x =Z O_Z) \/ (exists p:nat, x =Z (S p,0)) \/ (exists q:nat, x=Z (0,S q)).
  700. Proof.
  701. assert (forall a b:nat,
  702. a + 0 = 0 + b \/
  703. (exists c:nat, a + 0 = S c + b) \/
  704. (exists d:nat, a + S d = 0 + b)) as L.
  705. induction a.
  706. induction b.
  707. left.
  708. simpl.
  709. reflexivity.
  710. simpl in IHb.
  711. destruct IHb.
  712. rewrite <- H.
  713. right.
  714. right.
  715. exists 0.
  716. reflexivity.
  717. right.
  718. right.
  719. exists b.
  720. reflexivity.
  721. induction b.
  722. right.
  723. left.
  724. exists a.
  725. reflexivity.
  726. destruct IHb.
  727. right.
  728. right.
  729. exists 0.
  730. rewrite <- plus_n_Sm with (n:=S a) (m:=0).
  731. rewrite <- plus_n_Sm with (n:=0) (m:=b).
  732. apply eq_S.
  733. assumption.
  734. destruct H.
  735. destruct H as (e,He).
  736. induction e.
  737. left.
  738. rewrite <- plus_n_Sm with (n:= 0) (m:=b).
  739. rewrite <- plus_Sn_m with (n:=0) (m:=b).
  740. assumption.
  741. right.
  742. left.
  743. exists e.
  744. rewrite <- plus_n_Sm with (n:= S e) (m:=b).
  745. rewrite <- plus_Sn_m with (n:=S e) (m:=b).
  746. assumption.
  747. right.
  748. right.
  749. destruct H as (f,Hf).
  750. exists (S f).
  751. rewrite <- plus_n_Sm with (n:= S a) (m:= S f).
  752. rewrite <- plus_n_Sm with (n:= 0) (m:= b).
  753. apply eq_S.
  754. assumption.
  755. intro.
  756. unfold eqz.
  757. simpl.
  758. simpl in L.
  759. apply L.
  760. Qed.
  761.  
  762. Lemma zsign_zero: zsign O_Z = O_Z.
  763. Proof.
  764. simpl.
  765. reflexivity.
  766. Qed.
  767.  
  768. Lemma zsign_positive: forall n:nat, zsign (S n, 0) = (1,0).
  769. Proof.
  770. intros.
  771. unfold zsign.
  772. simpl.
  773. reflexivity.
  774. Qed.
  775.  
  776. Lemma zsign_negative: forall n:nat, zsign (0, S n) = (0,1).
  777. Proof.
  778. intros.
  779. unfold zsign.
  780. simpl.
  781. reflexivity.
  782. Qed.
  783.  
  784. Ltac zrac m n:= rewrite zsign_compat with (x:=m) (y:=n).
  785. Lemma zsign_morphism: forall x y:Relative_integer, (zsign (x *Z y)) = (zsign x) *Z (zsign y).
  786. Proof.
  787. intros.
  788. destruct cases_sign with (x:=x).
  789. apply eq_trans with (y:= zsign (O_Z *Z y)).
  790. apply zsign_compat.
  791. apply Z_mult_compatibility.
  792. assumption.
  793. zer.
  794. rewrite zsign_compat with (x:=x) (y:= O_Z).
  795. rewrite zsign_compat with (x:= O_Z *Z y) (y:= O_Z).
  796. rewrite zsign_zero.
  797. unfold Z_mult.
  798. simpl.
  799. reflexivity.
  800. zet (y *Z O_Z).
  801. zcom.
  802. apply Z_zero_product.
  803. assumption.
  804. destruct cases_sign with (x:=y).
  805. apply eq_trans with (y:= zsign (x *Z O_Z)).
  806. apply zsign_compat.
  807. apply Z_mult_compatibility.
  808. zer.
  809. assumption.
  810. rewrite zsign_compat with (x:=y) (y:= O_Z).
  811. rewrite zsign_compat with (x:= x *Z O_Z) (y:= O_Z).
  812. unfold Z_mult.
  813. simpl.
  814. mom (fst (zsign x)) 0.
  815. mom (snd (zsign x)) 0.
  816. simpl.
  817. rewrite zsign_zero.
  818. reflexivity.
  819. apply Z_zero_product.
  820. assumption.
  821. destruct H.
  822. destruct H as(p,Hp).
  823. destruct H0.
  824. destruct H as (q, Hq).
  825. zrac x (S p,0).
  826. zrac y (S q,0).
  827. zrac (x *Z y) ((S p, 0) *Z (S q, 0)).
  828. unfold Z_mult.
  829. simpl.
  830. mom p 0.
  831. simpl.
  832. apply zsign_positive.
  833. apply Z_mult_compatibility.
  834. assumption.
  835. assumption.
  836. assumption.
  837. assumption.
  838. destruct H as (q, Hq).
  839. zrac x (S p,0).
  840. zrac y (0,S q).
  841. zrac (x *Z y) ((S p, 0) *Z (0, S q)).
  842. unfold Z_mult.
  843. simpl.
  844. mom p 0.
  845. simpl.
  846. apply zsign_negative.
  847. apply Z_mult_compatibility.
  848. assumption.
  849. assumption.
  850. assumption.
  851. assumption.
  852. destruct H as(p,Hp).
  853. destruct H0.
  854. destruct H as (q, Hq).
  855. zrac x (0, S p).
  856. zrac y (S q,0).
  857. zrac (x *Z y) ((0, S p) *Z (S q, 0)).
  858. unfold Z_mult.
  859. simpl.
  860. mom p 0.
  861. simpl.
  862. apply zsign_negative.
  863. apply Z_mult_compatibility.
  864. assumption.
  865. assumption.
  866. assumption.
  867. assumption.
  868. destruct H as (q, Hq).
  869. zrac x (0, S p).
  870. zrac y (0,S q).
  871. zrac (x *Z y) ((0, S p) *Z (0, S q)).
  872. unfold Z_mult.
  873. simpl.
  874. mom p 0.
  875. simpl.
  876. apply zsign_positive.
  877. apply Z_mult_compatibility.
  878. assumption.
  879. assumption.
  880. assumption.
  881. assumption.
  882. Qed.
  883.  
  884. Lemma zsign_involution: forall x:Relative_integer, zsign (zsign x) = (zsign x).
  885. Proof.
  886. intros.
  887. destruct cases_sign with (x:=x).
  888. zrac x O_Z.
  889. compute.
  890. reflexivity.
  891. assumption.
  892. destruct H.
  893. destruct H as (p,Hp).
  894. zrac x (S p,0).
  895. compute.
  896. reflexivity.
  897. assumption.
  898. destruct H as (q,Hq).
  899. zrac x (0,S q).
  900. compute.
  901. reflexivity.
  902. assumption.
  903. Qed.
  904.  
  905. End Signs_of_relative_integers.
  906. (* ****** BONUS ******
  907. In the section below, we build a counter-example to the following fallacy seen on a forum:
  908. "the distributivity of the product over the sum is equivalent to the sign rule"
  909. *)
  910.  
  911. Section bonus_signs.
  912.  
  913. Let D_map (x y:Relative_integer):= zsign x *Z zsign y.
  914.  
  915. Ltac zrac m n:= rewrite zsign_compat with (x:=m) (y:=n).
  916.  
  917. Lemma D_associative: forall x y z:Relative_integer, D_map (D_map x y) z = D_map x (D_map y z).
  918. Proof.
  919. intros.
  920. unfold D_map.
  921. apply eq_trans with (y:= zsign (zsign x *Z zsign y) *Z (zsign (zsign z))).
  922. apply f_equal.
  923. apply eq_sym.
  924. apply zsign_involution.
  925. apply eq_trans with (y:= zsign ((zsign x *Z zsign y) *Z (zsign z))).
  926. apply eq_sym.
  927. apply zsign_morphism.
  928. apply eq_trans with (y:= zsign (zsign x) *Z (zsign (zsign y *Z zsign z))).
  929. apply eq_trans with (y:= zsign ((zsign x) *Z (zsign y *Z zsign z))).
  930. apply zsign_compat.
  931. zes.
  932. apply Z_mult_associative.
  933. apply zsign_morphism.
  934. rewrite zsign_involution with (x:=x).
  935. reflexivity.
  936. Qed.
  937.  
  938. Lemma D_commutative: forall x y:Relative_integer, D_map x y = D_map y x.
  939. Proof.
  940. intros.
  941. unfold D_map.
  942. rewrite <- zsign_morphism.
  943. rewrite <- zsign_morphism.
  944. apply zsign_compat.
  945. zcom.
  946. Qed.
  947.  
  948. Lemma D_zero: forall x:Relative_integer, D_map x O_Z = O_Z.
  949. Proof.
  950. intros.
  951. unfold D_map.
  952. rewrite <- zsign_morphism.
  953. zrac (x *Z O_Z) O_Z.
  954. apply zsign_zero.
  955. apply Z_zero_product.
  956. Qed.
  957.  
  958. Lemma D_sign_rule: forall x y:Relative_integer, D_map x y = D_map (Z_opp x) (Z_opp y).
  959. Proof.
  960. intros.
  961. unfold D_map.
  962. rewrite <- zsign_morphism.
  963. rewrite <- zsign_morphism.
  964. apply zsign_compat.
  965. zes.
  966. apply sign_rule.
  967. Qed.
  968.  
  969. Lemma D_compat:forall x x' y y':Relative_integer, ((x =Z x')/\(y =Z y'))-> D_map x y = D_map x' y'.
  970. Proof.
  971. intros.
  972. unfold D_map.
  973. rewrite <- zsign_morphism.
  974. rewrite <- zsign_morphism.
  975. apply zsign_compat.
  976. apply Z_mult_compatibility.
  977. apply H.
  978. apply H.
  979. Qed.
  980.  
  981.  
  982. Theorem sign_counter_example: exists D: Relative_integer -> Relative_integer -> Relative_integer,
  983. (forall x x' y y':Relative_integer, ((x =Z x')/\(y =Z y'))-> D x y =Z D x' y')/\
  984. (forall x y:Relative_integer, D x y =Z D(Z_opp x) (Z_opp y)) /\
  985. (forall p q r:Relative_integer, D p (D q r) =Z D (D p q) r) /\
  986. (forall x y:Relative_integer, D x y =Z D y x) /\
  987. (forall x:Relative_integer, D x (0,0) =Z (0,0)) /\
  988. (~(D (I_Z +Z I_Z) I_Z) =Z (D I_Z I_Z) +Z (D I_Z I_Z)).
  989. Proof.
  990. assert (forall x y:Relative_integer, x = y -> x =Z y) as R.
  991. intros.
  992. rewrite H.
  993. zer.
  994. exists D_map.
  995. split.
  996. intros.
  997. apply R.
  998. unfold D_map.
  999. apply D_compat.
  1000. assumption.
  1001. split.
  1002. intros.
  1003. apply R.
  1004. apply D_sign_rule.
  1005. split.
  1006. intros.
  1007. apply R.
  1008. apply eq_sym.
  1009. apply D_associative.
  1010. split.
  1011. intros.
  1012. apply R.
  1013. apply D_commutative.
  1014. split.
  1015. intros.
  1016. apply R.
  1017. apply D_zero.
  1018. compute.
  1019. discriminate.
  1020. Qed.
  1021.  
  1022. End bonus_signs.
  1023.  
  1024. Section nat_embedding.
  1025.  
  1026. Definition n_to_z (x:nat):Relative_integer:= (x,0).
  1027.  
  1028. Theorem n_to_z_injective: forall x y:nat, (n_to_z x) =Z (n_to_z y) -> x = y.
  1029. Proof.
  1030. intro.
  1031. intro.
  1032. unfold n_to_z.
  1033. unfold eqz.
  1034. simpl.
  1035. com x 0.
  1036. com y 0.
  1037. simpl.
  1038. intro.
  1039. assumption.
  1040. Qed.
  1041.  
  1042. Theorem n_to_z_zero: n_to_z 0 =Z O_Z.
  1043. Proof.
  1044. unfold n_to_z.
  1045. unfold eqz.
  1046. simpl.
  1047. reflexivity.
  1048. Qed.
  1049.  
  1050. Theorem n_to_z_one: n_to_z 1 =Z I_Z.
  1051. Proof.
  1052. unfold n_to_z.
  1053. unfold eqz.
  1054. simpl.
  1055. reflexivity.
  1056. Qed.
  1057.  
  1058. Theorem n_to_z_sum: forall x y:nat, n_to_z (x + y) =Z (n_to_z x) +Z (n_to_z y).
  1059. Proof.
  1060. unfold n_to_z.
  1061. unfold eqz.
  1062. intros.
  1063. simpl.
  1064. reflexivity.
  1065. Qed.
  1066.  
  1067. Theorem n_to_z_product: forall x y:nat, n_to_z (x * y) =Z (n_to_z x) *Z (n_to_z y).
  1068. Proof.
  1069. unfold n_to_z.
  1070. unfold eqz.
  1071. intros.
  1072. simpl.
  1073. mom x 0.
  1074. simpl.
  1075. com (x * y + 0) 0.
  1076. simpl.
  1077. reflexivity.
  1078. Qed.
  1079.  
  1080. Theorem n_generates_z: forall x:Relative_integer, exists m n:nat, x +Z (n_to_z m) =Z (n_to_z n).
  1081. Proof.
  1082. intros.
  1083. destruct x as (p,q).
  1084. exists q.
  1085. exists p.
  1086. unfold n_to_z.
  1087. unfold eqz.
  1088. simpl.
  1089. alr p q 0.
  1090. reflexivity.
  1091. Qed.
  1092. (*The above theorem states that every relative integer is the difference of two natural
  1093. integers.*)
  1094.  
  1095. End nat_embedding.
  1096. (*
  1097. (*Uncomment this part if you want to use the ring library, declare nat as a semi ring
  1098. and Relative_integer as a ring and incorporate rings tactics *)
  1099.  
  1100. Require Import Ring.
  1101.  
  1102. Theorem nat_semi_ring: @semi_ring_theory nat 0 1 plus mult eq.
  1103. Proof.
  1104. split.
  1105. apply nsr_O_plus_neutral.
  1106. apply nsr_plus_commutative.
  1107. apply nsr_plus_associative.
  1108. apply nsr_I_mult_neutral.
  1109. apply nsr_O_absorbs.
  1110. apply nsr_mult_commutative.
  1111. apply nsr_mult_associative.
  1112. apply nsr_right_distributivity.
  1113. Qed.
  1114.  
  1115. Add Ring the_set_of_natural_integers_is_a_semi_ring: nat_semi_ring.
  1116.  
  1117.  
  1118. Require Import Setoid.
  1119.  
  1120. Lemma equiv_eqz: Equivalence eqz.
  1121. Proof.
  1122. split.
  1123. unfold Reflexive.
  1124. apply eqz_reflexive.
  1125. unfold Symmetric.
  1126. apply eqz_symmetric.
  1127. unfold Transitive.
  1128. apply eqz_transitive.
  1129. Qed.
  1130.  
  1131. Theorem Z_ring: @ring_theory Relative_integer O_Z I_Z Z_plus Z_mult Z_minus Z_opp eqz.
  1132. Proof.
  1133. split.
  1134. apply Z_zero_plus_neutral.
  1135. apply Z_plus_commutative.
  1136. apply Z_plus_associative.
  1137. apply Z_un_neutral.
  1138. apply Z_mult_commutative.
  1139. apply Z_mult_associative.
  1140. apply Z_right_distributivity.
  1141. apply Z_minus_opp.
  1142. apply Z_opp_group.
  1143. Qed.
  1144.  
  1145. Lemma Z_compatibility: ring_eq_ext Z_plus Z_mult Z_opp eqz.
  1146. Proof.
  1147. unfold Morphisms.Proper.
  1148. unfold Morphisms.respectful.
  1149. split.
  1150. unfold Morphisms.Proper.
  1151. unfold Morphisms.respectful.
  1152. intro.
  1153. intro.
  1154. intros.
  1155. apply Z_sum_compatibility.
  1156. assumption.
  1157. assumption.
  1158. unfold Morphisms.Proper.
  1159. unfold Morphisms.respectful.
  1160. intros.
  1161. apply Z_mult_compatibility.
  1162. assumption.
  1163. assumption.
  1164. unfold Morphisms.Proper.
  1165. unfold Morphisms.respectful.
  1166. apply Z_opp_compatibility.
  1167. Qed.
  1168. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement