Advertisement
Guest User

asgaf

a guest
Jul 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. Require Import Coq.omega.Omega.
  2.  
  3. (** an integer as a pair of nats *)
  4. Inductive integer : Type :=
  5. | pair : nat -> nat -> integer.
  6.  
  7. Notation "( x , y )" := (pair x y).
  8.  
  9. (** first component of the pair *)
  10. Definition fst (x: integer): nat :=
  11. match x with
  12. | (m, n) => m
  13. end.
  14.  
  15. (** second component of the pair *)
  16. Definition snd (x: integer): nat :=
  17. match x with
  18. | (m, n) => n
  19. end.
  20.  
  21. (** equality as an axiom *)
  22. Axiom Z_eq: forall (x y: integer), fst x + snd y = snd x + fst y <-> x = y.
  23.  
  24. (** addition *)
  25. Definition Z_plus (x: integer) (y: integer): integer :=
  26. match x with
  27. | (a1, a2) =>
  28. match y with
  29. | (b1, b2) => (a1 + b1, a2 + b2)
  30. end
  31. end.
  32.  
  33. Notation "z '+Z' w" := (Z_plus z w) (at level 50, left associativity) : type_scope.
  34.  
  35. (** subtraction *)
  36. Definition Z_minus (x: integer) (y: integer): integer :=
  37. match x with
  38. | (a1, a2) =>
  39. match y with
  40. | (b1, b2) => (a1 + b2, a2 + b1)
  41. end
  42. end.
  43.  
  44. Notation "z '-Z' w" := (Z_minus z w) (at level 50, left associativity) : type_scope.
  45.  
  46. (** negation of an integer: z |-> -z *)
  47. Definition Z_neg (x: integer): integer :=
  48. match x with
  49. | (a1, a2) => (a2, a1)
  50. end.
  51.  
  52. Notation "'-Z' w" := (Z_neg w) (at level 35, right associativity) : type_scope.
  53.  
  54. (** multiplication *)
  55. Definition Z_mult (x: integer) (y: integer): integer :=
  56. match x with
  57. | (a1, a2) =>
  58. match y with
  59. | (b1, b2) => (a1 * b1 + a2 * b2, a1 * b2 + a2 * b1)
  60. end
  61. end.
  62.  
  63. Notation "z '*Z' w" := (Z_mult z w) (at level 40, left associativity) : type_scope.
  64.  
  65. (** zero and one *)
  66. Definition Z0: integer := (0, 0).
  67. Definition Z1: integer := (1, 0).
  68.  
  69. (** plus_assoc *)
  70. Theorem Z_1: forall x y z: integer, (x +Z y) +Z z = x +Z (y +Z z).
  71. Proof. destruct x, y, z. simpl. rewrite <- Z_eq. simpl. omega. Qed.
  72.  
  73. (** plus_comm *)
  74. Theorem Z_2: forall x y: integer, x +Z y = y +Z x.
  75. Proof. destruct x, y. simpl. rewrite <- Z_eq. simpl. omega. Qed.
  76.  
  77. (** zero as an identity for plus *)
  78. Theorem Z_3: forall x: integer, x +Z Z0 = x.
  79. Proof. destruct x. simpl. rewrite <- Z_eq. simpl. omega. Qed.
  80.  
  81. (** inverse element for plus *)
  82. Theorem Z_4: forall x: integer, x +Z -Z x = Z0.
  83. Proof. destruct x. simpl. rewrite <- Z_eq. simpl. omega. Qed.
  84.  
  85. Lemma dist_l: forall a b c: nat, c * (a + b) = c * a + c * b.
  86. Proof. induction c.
  87. - auto.
  88. - simpl. rewrite -> plus_assoc. rewrite <- (plus_assoc a (c * a) b). rewrite (plus_comm (c * a) b). omega.
  89. Qed.
  90.  
  91. Lemma dist_r: forall a b c: nat, (a + b) * c = a * c + b * c.
  92. Proof. induction a.
  93. - auto.
  94. - simpl. intros. rewrite <- plus_assoc. rewrite <- (IHa b c). reflexivity.
  95. Qed.
  96.  
  97. (** mult_assoc *)
  98. Theorem Z_5: forall x y z: integer, (x *Z y) *Z z = x *Z (y *Z z).
  99. Proof. destruct x, y, z. simpl. rewrite <- Z_eq. simpl.
  100. repeat rewrite dist_l. repeat rewrite dist_r. repeat rewrite plus_assoc. repeat rewrite mult_assoc. omega.
  101. Qed.
  102.  
  103. (** mult_comm *)
  104. Theorem Z_6: forall x y: integer, x *Z y = y *Z x.
  105. Proof. destruct x, y. simpl. rewrite <- Z_eq. simpl. repeat rewrite plus_assoc.
  106. rewrite (mult_comm n1 n), (mult_comm n0 n2), (mult_comm n1 n0), (mult_comm n n2). omega. Qed.
  107.  
  108. (** one as an identity for mult *)
  109. Theorem Z_7: forall x: integer, x *Z Z1 = x.
  110. Proof. destruct x. simpl. rewrite <- Z_eq. simpl. omega. Qed.
  111.  
  112. (** left distribution law *)
  113. Theorem Z_8: forall x y z: integer, x *Z (y +Z z) = x *Z y +Z x *Z z.
  114. Proof. destruct x, y, z. simpl. rewrite <- Z_eq. simpl.
  115. repeat rewrite dist_l. repeat rewrite dist_r. repeat rewrite plus_assoc. repeat rewrite mult_assoc. omega. Qed.
  116.  
  117. (** Z is an integral domain *)
  118. Theorem Z_9: forall x y: integer, x *Z y = Z0 -> x = Z0 \/ y = Z0.
  119. Proof. destruct x. remember (beq_nat n n0) as b. destruct b.
  120. - intros. left. rewrite <- Z_eq. simpl. symmetry in Heqb.
  121. rewrite beq_nat_true_iff in Heqb. rewrite Heqb. omega.
  122. - intros. right. rewrite <- Z_eq. simpl. symmetry in Heqb.
  123. rewrite beq_nat_false_iff in Heqb. rewrite <- Z_eq in H.
  124. destruct y. simpl in H. repeat (rewrite <- plus_n_O in H).
  125. simpl. repeat (rewrite <- plus_n_O).
  126. assert (forall a b: nat, a <> b -> minus a b <> 0 \/ minus b a <> 0).
  127. { induction a.
  128. - intros. simpl. right. omega.
  129. - destruct b. intros. left. omega. intros.
  130. assert (forall c d: nat, c = d -> S c = S d).
  131. { intros. rewrite H1. reflexivity. }
  132. assert (a <> b).
  133. { unfold not. intros. apply H1 in H2. apply H0 in H2. inversion H2. }
  134. apply IHa in H2. destruct H2. left. omega. right. omega. }
  135.  
  136. apply H0 in Heqb.
  137. assert (forall q r, q - r <> 0 /\ q * n1 + r * n2 = q * n2 + r * n1 -> n1 = n2).
  138. { intros. destruct H1.
  139. assert (q * n1 - r * n1 = q * n2 - r * n2). { omega. }
  140. assert (forall n n0, n - n0 <> 0 -> (n - n0) + n0 = n).
  141. { intros. omega. }
  142. rewrite <- (H4 q r) in H3.
  143. assert (forall x y: nat, x + y - y = x).
  144. { intros. omega. }
  145. repeat rewrite dist_r in H3.
  146. repeat rewrite H5 in H3.
  147. assert (forall z x y: nat, S z * x = S z * y -> x = y).
  148. { assert (S_inj: forall k l: nat, S k = S l -> k = l). { intros. omega. }
  149. assert (S_surj: forall k l: nat, k = l -> S k = S l). { intros. rewrite H6. reflexivity. }
  150. induction z. intros. omega. induction x. simpl. destruct y. reflexivity. intros.
  151. rewrite <- mult_n_O in H6. inversion H6.
  152. assert (forall a b, a * S b = a + a * b).
  153. { intros. induction a. omega. simpl. rewrite IHa. omega. }
  154. destruct y. intros. rewrite <- mult_n_O in H7. inversion H7.
  155. repeat rewrite (H6 (S(S z)) _). intros.
  156. apply S_surj. apply IHx. omega. }
  157. remember (q - r) as k. destruct k. omega. apply (H6 k). apply H3.
  158. apply H1.
  159. }
  160. destruct Heqb.
  161. apply (H1 n n0). split. apply H2. apply H.
  162. apply (H1 n0 n). split. apply H2. omega. Qed.
  163.  
  164. (** natural order for Z *)
  165. Definition Z_leb (x y: integer) := (** x <= y iff *)
  166. match x with
  167. | (a1, a2) =>
  168. match y with
  169. | (b1, b2) => (a1 + b2 <= b1 + a2)
  170. end
  171. end.
  172.  
  173. Notation "z '<=Z' w" := (Z_leb z w) (at level 70, no associativity) : type_scope.
  174. Notation "z '<Z' w" := (~ Z_leb w z) (at level 70, no associativity) : type_scope.
  175. Notation "z '>=Z' w" := (Z_leb w z) (at level 70, no associativity) : type_scope.
  176. Notation "z '>Z' w" := (~ Z_leb z w) (at level 70, no associativity) : type_scope.
  177.  
  178. Lemma LT: forall x y: integer, x -Z y <Z Z0 <-> x <Z y.
  179. Proof. intros. destruct x, y. split; unfold Z_leb; simpl; omega. Qed.
  180. Lemma SM: forall x y: integer, x -Z y = Z0 <-> x = y.
  181. Proof. intros. destruct x, y. split; repeat rewrite <- Z_eq; simpl; omega. Qed.
  182. Lemma GT: forall x y: integer, x -Z y >Z Z0 <-> x >Z y.
  183. Proof. intros. destruct x, y. split; unfold Z_leb; simpl; omega. Qed.
  184.  
  185.  
  186. (** trichotomy *)
  187. Theorem Z_10: forall x y: integer,
  188. ( x <Z y /\ ~ x = y /\ ~ x >Z y) \/
  189. (~ x <Z y /\ x = y /\ ~ x >Z y) \/
  190. (~ x <Z y /\ ~ x = y /\ x >Z y).
  191. Proof.
  192. assert (Z_10_0: forall x: integer,
  193. ( x <Z Z0 /\ ~ x = Z0 /\ ~ x >Z Z0) \/
  194. (~ x <Z Z0 /\ x = Z0 /\ ~ x >Z Z0) \/
  195. (~ x <Z Z0 /\ ~ x = Z0 /\ x >Z Z0)).
  196. { destruct x. remember (n - n0) as d1. destruct d1.
  197. - remember (n0 - n) as d2. destruct d2.
  198. + right. left. split.
  199. unfold Z_leb. simpl. omega.
  200. split. rewrite <- Z_eq. simpl. omega.
  201. unfold Z_leb. simpl. omega.
  202. + left. split.
  203. unfold Z_leb. simpl. omega.
  204. split. unfold not. rewrite <- Z_eq. simpl. omega.
  205. unfold Z_leb. simpl. omega.
  206. - right. right. split.
  207. unfold Z_leb. simpl. omega.
  208. split. unfold not. rewrite<- Z_eq. simpl. omega.
  209. unfold Z_leb. simpl. omega.
  210. }
  211.  
  212. intros. rewrite <- LT. rewrite <- SM. rewrite <- (GT x y).
  213. apply Z_10_0.
  214. Qed.
  215.  
  216. (** transitivity *)
  217. Theorem Z_11: forall x y z: integer, x <Z y /\ y <Z z -> x <Z z.
  218. Proof. intros x y z. rewrite <- (LT x y). rewrite <- (LT x z). rewrite <- (LT y z).
  219. assert (forall a b: integer, a <Z Z0 /\ b <Z Z0 -> a +Z b <Z Z0).
  220. { destruct a, b. intros. simpl in H; destruct H. simpl. omega. }
  221. assert ((x -Z y) +Z (y -Z z) = x -Z z).
  222. { destruct x, y, z. simpl. rewrite <- Z_eq. simpl. omega. }
  223. intros. rewrite <- H0. apply (H (x -Z y) (y -Z z)). apply H1. Qed.
  224.  
  225. (** addition preserves the order *)
  226. Theorem Z_12: forall x y z: integer, x <Z y -> x +Z z <Z y +Z z.
  227. Proof. intros x y z. rewrite <- GT. destruct x, y, z. simpl. omega. Qed.
  228.  
  229. (** mult by positive number preserves the order *)
  230. Theorem Z_13: forall x y z: integer, x <Z y /\ z >Z Z0 -> x *Z z <Z y *Z z.
  231. Proof.
  232. intros x y z. rewrite <- GT. rewrite <- (GT (y *Z z) _).
  233. assert (forall n m: nat, ~ n <= m <-> n > m).
  234. { induction n. intros. omega. intros. destruct m. omega. omega. }
  235. assert (L: forall a b c d: nat, a >= b /\ c > d -> a + c > b + d).
  236. { induction a. intros. destruct H0. inversion H0. destruct b. intros.
  237. simpl. apply H1. simpl. apply H1.
  238. assert (forall a b: nat, S a >= S b <-> a >= b). { intros. omega. }
  239. destruct b. intros. simpl. omega.
  240. intros. rewrite H0 in H1. simpl. apply H0. apply IHa. apply H1. }
  241. assert (forall a b, a * S b = a + a * b).
  242. { intros. induction a. omega. simpl. rewrite IHa. omega. }
  243. assert (C: forall a b c: nat, a > b -> a * S c > b * S c).
  244. { induction c. omega. intros.
  245. rewrite H0. rewrite (H0 _ (S c)). assert (a * S c > b * S c). apply IHc in H1; apply H1.
  246. apply (L a b _ _). split. omega. apply H2. }
  247. assert (forall a b: integer, a >Z Z0 /\ b >Z Z0 -> a *Z b >Z Z0).
  248. { destruct a, b. simpl. repeat rewrite <- plus_n_O.
  249. repeat rewrite H.
  250. generalize dependent n0.
  251. induction n. intros. inversion H1. inversion H2. simpl. intros.
  252. destruct H1. destruct n0. simpl. repeat rewrite <- plus_n_O.
  253. rewrite mult_comm. rewrite (mult_comm n n2). repeat rewrite <- H0.
  254. auto. simpl. repeat rewrite -> plus_assoc.
  255. rewrite -> (plus_comm _ n2). repeat rewrite <- (plus_assoc n2 _ _).
  256. rewrite -> (plus_comm _ n1). repeat rewrite -> plus_assoc.
  257. repeat rewrite <- (plus_assoc (n2 + n1)).
  258. apply L. split. omega. apply IHn. split. omega. omega. }
  259. assert (forall v w: integer, v +Z -Z w = v -Z w).
  260. { destruct v, w. simpl. rewrite <- Z_eq. simpl. omega. }
  261. assert (forall v w: integer, v *Z -Z w = -Z (v *Z w)).
  262. { destruct v, w. simpl. rewrite <- Z_eq. simpl. omega. }
  263. assert (y *Z z -Z x *Z z = (y -Z x) *Z z).
  264. { rewrite (Z_6 y z), (Z_6 x z), (Z_6 _ z).
  265. repeat rewrite <- H2. symmetry. rewrite <- H3. apply Z_8. }
  266. rewrite H4. apply H1. Qed.
  267.  
  268.  
  269. (** Z is not a trivial ring *)
  270. Theorem Z_14: Z0 <> Z1.
  271. Proof. unfold not. rewrite <- Z_eq. simpl. intros. inversion H. Qed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement