Advertisement
Benjamin_Loison

Coq

Oct 24th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.75 KB | None | 0 0
  1. Theorem plus_O_n : forall n : nat, 0 + n = n.
  2. Proof.
  3. intros n. simpl. reflexivity. Qed.
  4.  
  5. (* The _l suffix in the names of these theorems is pronounced "on the left." *)
  6.  
  7. Theorem plus_1_l : forall n:nat, 1 + n = S n.
  8. Proof.
  9. intros n. reflexivity. Qed.
  10.  
  11. Theorem mult_0_l : forall n:nat, 0 * n = 0.
  12. Proof.
  13. intros n. reflexivity. Qed.
  14.  
  15. (* The arrow symbol is pronounced "implies." *)
  16.  
  17. Theorem plus_id_example : forall n m:nat,
  18. n = m ->
  19. n + n = m + m.
  20.  
  21. (* assumptions: hypothèses *)
  22.  
  23. Proof.
  24. (* move both quantifiers into the context: *)
  25. intros n m.
  26. (* move the hypothesis into the context: *)
  27. intros H.
  28. (* rewrite the goal using the hypothesis: *)
  29. rewrite <- H.
  30. reflexivity. Qed.
  31.  
  32. (* (The arrow symbol in the rewrite has nothing to do with implication: it tells Coq to
  33. apply the rewrite from left to right. To rewrite from right to left, you can use rewrite
  34. <-. Try making this change in the above proof and see what difference it makes.) *)
  35.  
  36. Theorem plus_id_exercise : forall n m o : nat,
  37. n = m -> m = o -> n + m = m + o.
  38. Proof.
  39. intros n m o.
  40. intros H.
  41. intros H0.
  42. rewrite -> H.
  43. rewrite -> H0.
  44. reflexivity. Qed.
  45.  
  46. (* every time you say Admitted you are leaving a door open for total nonsense to enter
  47. Coq's nice, rigorous, formally checked world! *)
  48.  
  49. Theorem mult_0_plus : forall n m : nat,
  50. (0 + n) * m = n * m.
  51. Proof.
  52. intros n m.
  53. rewrite -> plus_O_n.
  54. reflexivity. Qed.
  55.  
  56.  
  57.  
  58. Theorem mult_S_1 : forall n m : nat,
  59. m = S n ->
  60. m * (1 + n) = m * m.
  61. Proof.
  62. intros n m.
  63. intros H.
  64. rewrite -> H.
  65. reflexivity. Qed.
  66.  
  67. Fixpoint eqb (n m : nat) : bool :=
  68. match n with
  69. | O => match m with
  70. | O => true
  71. | S m' => false
  72. end
  73. | S n' => match m with
  74. | O => false
  75. | S m' => eqb n' m'
  76. end
  77. end.
  78.  
  79. Notation "x =? y" := (eqb x y) (at level 70) : nat_scope.
  80.  
  81. Theorem plus_1_neq_0_firsttry : forall n : nat,
  82. (n + 1) =? 0 = false.
  83. Proof.
  84. intros n.
  85. simpl. (* does nothing! *)
  86. Abort.
  87.  
  88. Theorem plus_1_neq_0 : forall n : nat,
  89. (n + 1) =? 0 = false.
  90. Proof.
  91. intros n. destruct n as [| n'] eqn:E.
  92. - reflexivity.
  93. - reflexivity. Qed.
  94.  
  95. (* The - signs on the second and third lines are called bullets
  96. and they mark the parts of the proof that correspond to each generated subgoal. *)
  97.  
  98. (* One reasonable convention is to limit yourself to 80-character lines. *)
  99.  
  100. Theorem negb_involutive : forall b : bool,
  101. negb (negb b) = b.
  102. Proof.
  103. intros b. destruct b eqn:E.
  104. - reflexivity.
  105. - reflexivity. Qed.
  106.  
  107. (* Qed. is required to make the function defined *)
  108.  
  109. Theorem andb_commutative : forall b c, andb b c = andb c b.
  110. Proof.
  111. intros b c. destruct b eqn:Eb.
  112. - destruct c eqn:Ec.
  113. + reflexivity.
  114. + reflexivity.
  115. - destruct c eqn:Ec.
  116. + reflexivity.
  117. + reflexivity.
  118. Qed.
  119.  
  120. (* Besides - and +, we can use * (asterisk) as a third kind of bullet. or *)
  121.  
  122. Theorem andb_commutative' : forall b c, andb b c = andb c b.
  123. Proof.
  124. intros b c. destruct b eqn:Eb.
  125. { destruct c eqn:Ec.
  126. { reflexivity. }
  127. { reflexivity. } }
  128. { destruct c eqn:Ec.
  129. { reflexivity. }
  130. { reflexivity. } }
  131. Qed.
  132.  
  133. Theorem andb3_exchange :
  134. forall b c d, andb (andb b c) d = andb (andb b d) c.
  135. Proof.
  136. intros b c d. destruct b eqn:Eb.
  137. - destruct c eqn:Ec.
  138. { destruct d eqn:Ed.
  139. - reflexivity.
  140. - reflexivity. }
  141. { destruct d eqn:Ed.
  142. - reflexivity.
  143. - reflexivity. }
  144. - destruct c eqn:Ec.
  145. { destruct d eqn:Ed.
  146. - reflexivity.
  147. - reflexivity. }
  148. { destruct d eqn:Ed.
  149. - reflexivity.
  150. - reflexivity. }
  151. Qed.
  152.  
  153.  
  154. Theorem plus_1_neq_0' : forall n : nat,
  155. (n + 1) =? 0 = false.
  156. Proof.
  157. intros [|n].
  158. - reflexivity.
  159. - reflexivity. Qed.
  160.  
  161. Theorem andb_commutative'' :
  162. forall b c, andb b c = andb c b.
  163. Proof.
  164. intros [] [].
  165. - reflexivity.
  166. - reflexivity.
  167. - reflexivity.
  168. - reflexivity.
  169. Qed.
  170.  
  171. Theorem andb_true_elim2 : forall b c : bool,
  172. andb b c = true -> c = true.
  173. Proof.
  174. intros b c.
  175. intros H.
  176.  
  177. destruct b eqn:Eb.
  178. { destruct c eqn:Ec.
  179. { reflexivity. }
  180. { rewrite <- H. reflexivity. } }
  181. { destruct c eqn:Ec.
  182. { rewrite <- H. reflexivity. }
  183. { rewrite <- H. reflexivity. } }
  184. Qed.
  185.  
  186. Theorem zero_nbeq_plus_1 : forall n : nat,
  187. 0 =? (n + 1) = false.
  188. Proof.
  189. intros n. destruct n as [| n'] eqn:E.
  190. - reflexivity.
  191. - reflexivity. Qed.
  192.  
  193. Notation "x + y" := (plus x y)
  194. (at level 50, left associativity)
  195. : nat_scope.
  196. Notation "x * y" := (mult x y)
  197. (at level 40, left associativity)
  198. : nat_scope.
  199.  
  200. (*
  201.  
  202. Coq tries to guess what scope is meant from context, so when it sees S(O*O) it guesses
  203. nat_scope, but when it sees the cartesian product (tuple) type bool*bool (which we'll
  204. see in later chapters) it guesses type_scope. Occasionally, it is necessary to help it
  205. out with percent-notation by writing (x*y)%nat, and sometimes in what Coq prints it will
  206. use %nat to indicate what scope a notation is in.
  207.  
  208. 0%nat, which means O (the natural number 0 that we're using in this chapter), or 0%Z,
  209. which means the Integer zero
  210.  
  211. *)
  212.  
  213. Fixpoint plus' (n : nat) (m : nat) : nat :=
  214. match n with
  215. | O => m
  216. | S n' => S (plus' n' m)
  217. end.
  218.  
  219. (*
  220.  
  221. Coq demands that some argument of every Fixpoint definition is "decreasing."
  222. This requirement is a fundamental feature of Coq's design: In particular, it guarantees
  223. that every function that can be defined in Coq will terminate on all inputs. However,
  224. because Coq's "decreasing analysis" is not very sophisticated, it is sometimes necessary
  225. to write functions in slightly unnatural ways.
  226.  
  227. *)
  228.  
  229. (*Fixpoint plus'' (n : nat) (m : nat) : nat :=
  230. match n with
  231. | O => m
  232. | S n' => S (plus'' (S n') m)
  233. end.
  234.  
  235. Compute (plus'' 1 4).*)
  236.  
  237. (* only wants: Cannot guess decreasing argument of fix. ? *)
  238.  
  239.  
  240. Theorem identity_fn_applied_twice :
  241. forall (f : bool -> bool),
  242. (forall(x : bool), f x = x) ->
  243. forall(b : bool), f (f b) = b.
  244. Proof.
  245. intros f b.
  246. intros H.
  247. rewrite -> b. rewrite -> b.
  248. reflexivity.
  249. Qed. (*what did you expect ?*)
  250.  
  251. Theorem negation_fn_applied_twice :
  252. forall (f : bool -> bool),
  253. (forall(x : bool), f x = negb x) ->
  254. forall(b : bool), f (f b) = b.
  255. Proof.
  256. intros f b.
  257. intros H.
  258. rewrite -> b. rewrite -> b.
  259. rewrite -> negb_involutive.
  260. reflexivity.
  261. Qed.
  262.  
  263. Definition andb (b1:bool) (b2:bool) : bool :=
  264. match b1 with
  265. | true => b2
  266. | false => false
  267. end.
  268. Definition orb (b1:bool) (b2:bool) : bool :=
  269. match b1 with
  270. | true => true
  271. | false => b2
  272. end.
  273.  
  274. Compute (andb true false = orb true false).
  275.  
  276. Theorem andb_eq_orb :
  277. forall (b c : bool),
  278. (andb b c = orb b c) -> b = c.
  279. Proof.
  280. intros b c.
  281. destruct b eqn:Eb.
  282. { destruct c eqn:Ec.
  283. { reflexivity. }
  284. { simpl. intros H. rewrite -> H. reflexivity. } } (* why nope ? *)
  285. { destruct c eqn:Ec.
  286. { simpl. intros H. rewrite -> H. reflexivity. }
  287. { simpl. reflexivity. } }
  288. Qed.
  289.  
  290. Inductive bin : Type :=
  291. | Z
  292. | A (n : bin)
  293. | B (n : bin).
  294.  
  295. Fixpoint incr (m:bin) : bin := match m with
  296. | Z => B Z
  297. | A n => B n
  298. | B n => A (B n)
  299. end.
  300.  
  301. Example test_incr0: incr Z = B Z.
  302. Proof. reflexivity. Qed.
  303.  
  304. Example test_incr1: incr (B Z) = A (B Z).
  305. Proof. reflexivity. Qed.
  306.  
  307. Example test_incr2: incr (A (B Z)) = B (B Z).
  308. Proof. reflexivity. Qed.
  309.  
  310. (* Example test_incr3: incr (B (B Z)) = A (A (B Z)).
  311. Proof. reflexivity. Qed. *)
  312.  
  313. Fixpoint bin_to_nat (m:bin) : nat (* unary *)
  314. (* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.
  315.  
  316. Theorem andb_true_elim2' : forall b c : bool,
  317. andb b c = true -> c = true.
  318. Proof.
  319. intros b c.
  320. intros H.
  321.  
  322. destruct b eqn:Eb.
  323. { destruct c eqn:Ec.
  324. { reflexivity. }
  325. { rewrite <- H. reflexivity. } }
  326. { destruct c eqn:Ec.
  327. { rewrite <- H. reflexivity. }
  328. { rewrite <- H. reflexivity. } }
  329. Qed.
  330.  
  331. Theorem andb_eq_orb :
  332. forall (b c : bool),
  333. (andb b c = orb b c) ->
  334. b = c.
  335. Proof.
  336. intros b c.
  337.  
  338. reflexivity.
  339. Qed.
  340.  
  341. (* TODO last exercise *)
  342.  
  343. Theorem plus_n_O : forall n:nat, n = n + 0.
  344. Proof.
  345. intros n. induction n as [| n' IHn'].
  346. - (* n = 0 *) reflexivity.
  347. - (* n = S n' *) simpl. rewrite <- IHn'. reflexivity. Qed.
  348.  
  349. Theorem plus_n_O_bis : forall n:nat, n + 0 = n.
  350. Proof.
  351. intros n. induction n as [| n' IHn'].
  352. - (* n = 0 *) reflexivity.
  353. - (* n = S n' *) simpl. rewrite -> IHn'. reflexivity. Qed.
  354.  
  355. Theorem minus_diag : forall n,
  356. minus n n = 0.
  357. Proof.
  358. (* WORKED IN CLASS *)
  359. intros n. induction n as [| n' IHn'].
  360. - (* n = 0 *)
  361. simpl. reflexivity.
  362. - (* n = S n' *)
  363. simpl. rewrite -> IHn'. reflexivity. Qed.
  364.  
  365. Theorem mult_0_r : forall n:nat,
  366. n * 0 = 0.
  367. Proof.
  368. intros n. induction n as [| n' IHn'].
  369. - (* n = 0 *)
  370. simpl. reflexivity.
  371. - (* n = S n' *)
  372. simpl. rewrite -> IHn'. reflexivity. Qed.
  373.  
  374. Theorem plus_n_Sm : forall n m : nat,
  375. S (n + m) = n + (S m).
  376. Proof.
  377. intros n m.
  378. (*induction n as [| n' IHn'].
  379. - (* n = 0 *)
  380. simpl. reflexivity.
  381. - (* n = S n' *)
  382. simpl. rewrite -> IHn'. reflexivity.
  383. induction m as [| m' IHm'].
  384. - (* m = 0 *)
  385. simpl. reflexivity.
  386. - (* m = S m' *)
  387. simpl. rewrite -> IHm'. reflexivity.*)
  388. (*induction n as [|n'].
  389. match m with
  390. | O => reflexivity
  391. | S n' => ( simpl. rewrite <- IHn'. reflexivity.)
  392. end*)
  393. induction n. (* how is that working ? *)
  394. - simpl. reflexivity.
  395. - simpl. rewrite -> IHn. reflexivity.
  396. Qed.
  397.  
  398. Theorem plus_comm : forall n m : nat,
  399. n + m = m + n.
  400. Proof.
  401. intros n m.
  402. induction n.
  403. - simpl. rewrite-> plus_n_O_bis. reflexivity.
  404. - simpl. rewrite <- plus_n_Sm. rewrite -> IHn. reflexivity.
  405. Qed.
  406.  
  407. (*Theorem assoc : forall n m p : nat,
  408. n + m + p = n + (m + p).
  409. Proof.
  410.  
  411. Qed.*)
  412.  
  413. (* "S (n + m + p)" with "S (n + (m + p))". *)
  414.  
  415. Theorem plus_assoc : forall n m p : nat,
  416. n + (m + p) = (n + m) + p.
  417. Proof.
  418. intros n m p.
  419. induction n.
  420. - simpl. reflexivity.
  421. - simpl. rewrite -> IHn. reflexivity.
  422. Qed.
  423.  
  424. Fixpoint double (n:nat) :=
  425. match n with
  426. | O => O
  427. | S n' => S (S (double n'))
  428. end.
  429.  
  430. Lemma double_plus : forall n, double n = n + n .
  431. Proof.
  432. intros n.
  433. induction n.
  434. - simpl. reflexivity.
  435. - simpl. rewrite -> IHn. rewrite -> plus_n_Sm. reflexivity.
  436. Qed.
  437.  
  438. Fixpoint evenb (n:nat) : bool :=
  439. match n with
  440. | O => true
  441. | S O => false
  442. | S (S n') => evenb n'
  443. end.
  444.  
  445. Theorem evenb_S : forall n : nat,
  446. evenb (S n) = negb (evenb n).
  447. Proof.
  448. intros n.
  449. induction n.
  450. - simpl. reflexivity.
  451. - rewrite -> IHn. simpl. rewrite negb_involutive. reflexivity.
  452. Qed.
  453.  
  454. (* Briefly explain the difference between the tactics destruct and induction.
  455. Answer: Both are used to perform case analysis on an element of an inductively defined type; induction
  456. also generates an induction hypothesis, while destruct does not
  457. *)
  458.  
  459. Theorem plus_comme : forall n m : nat,
  460. n + m = m + n.
  461. Proof.
  462. intros n m.
  463. induction n.
  464. - simpl. rewrite-> plus_n_O_bis. reflexivity.
  465. - simpl. rewrite <- plus_n_Sm. rewrite -> IHn. reflexivity.
  466. (* S (n + m) = n + (S m). *)
  467. Qed.
  468.  
  469. Theorem plus_rearrange_firsttry : forall n m p q : nat,
  470. (n + m) + (p + q) = (m + n) + (p + q).
  471. Proof.
  472. intros n m p q.
  473. assert (H: n + m = m + n). { rewrite -> plus_comm. reflexivity. }
  474. rewrite -> H.
  475. reflexivity. Qed.
  476.  
  477. (* what did they except as informal ? (there isn't any exemple
  478. Oh I believe they want some Enlish instead of ! *)
  479.  
  480. (*
  481.  
  482. Assume n = 0:
  483. We've 0 + m = m + 0
  484. Which is clear by the definition of the + operation
  485. Assume n a non null natural number
  486. We've (S n') + m = m + (S n')
  487. Which is clear by hypothetical hypothesis (* this name *)
  488. Conclude for all n
  489. *)
  490.  
  491. (* TODO other one *)
  492.  
  493. Inductive natprod : Type :=
  494. | pair (n1 n2 : nat).
  495.  
  496. Notation "( x , y )" := (pair x y).
  497.  
  498. Definition swap_pair (p : natprod) : natprod :=
  499. match p with
  500. | (x,y) => (y,x)
  501. end.
  502.  
  503. Definition fst (p : natprod) : nat :=
  504. match p with
  505. | pair x y => x
  506. end.
  507. Definition snd (p : natprod) : nat :=
  508. match p with
  509. | pair x y => y
  510. end.
  511.  
  512. Theorem snd_fst_is_swap : forall (p : natprod),
  513. (snd p, fst p) = swap_pair p.
  514. Proof.
  515. intros p. destruct p as [n m]. simpl. reflexivity. Qed.
  516.  
  517. Theorem fst_swap_is_snd : forall (p : natprod),
  518. fst (swap_pair p) = snd p.
  519. Proof.
  520. intros p. destruct p as [n m]. simpl. reflexivity. Qed.
  521.  
  522. Inductive natlist : Type :=
  523. | nil
  524. | cons (n : nat) (l : natlist).
  525.  
  526. Notation "x :: l" := (cons x l)
  527. (at level 60, right associativity).
  528. Notation "[ ]" := nil.
  529. Notation "[ x ; .. ; y ]" := (cons x .. (cons y nil) ..).
  530.  
  531. Fixpoint repeat (n count : nat) : natlist :=
  532. match count with
  533. | O => nil
  534. | S count' => n :: (repeat n count')
  535. end.
  536.  
  537. Fixpoint length (l:natlist) : nat :=
  538. match l with
  539. | nil => O
  540. | h :: t => S (length t)
  541. end.
  542.  
  543. Fixpoint app (l1 l2 : natlist) : natlist :=
  544. match l1 with
  545. | nil => l2
  546. | h :: t => h :: (app t l2)
  547. end.
  548.  
  549. Notation "x ++ y" := (app x y)
  550. (right associativity, at level 60).
  551. Example test_app1: [1;2;3] ++ [4;5] = [1;2;3;4;5].
  552. Proof. reflexivity. Qed.
  553.  
  554. Definition hd (default:nat) (l:natlist) : nat :=
  555. match l with
  556. | nil => default
  557. | h :: t => h
  558. end.
  559. Definition tl (l:natlist) : natlist :=
  560. match l with
  561. | nil => nil
  562. | h :: t => t
  563. end.
  564. Example test_hd1: hd 0 [1;2;3] = 1.
  565. Proof. reflexivity. Qed.
  566. Example test_hd2: hd 0 [] = 0.
  567. Proof. reflexivity.
  568.  
  569. Fixpoint nonzeros (l:natlist) : natlist :=
  570. match l with
  571. | nil => nil
  572. | O::t => nonzeros t
  573. | h::t => h::(nonzeros t)
  574. end.
  575.  
  576. Example test_nonzeros:
  577. nonzeros [0;1;0;2;3;0;0] = [1;2;3].
  578. Proof. reflexivity. Qed.
  579.  
  580. Definition oddb (n:nat) : bool := (evenb n).
  581.  
  582. Fixpoint oddmembers (l:natlist) : natlist :=
  583. match l with
  584. | nil => nil
  585. | h::t => match (oddb h) with
  586. | true => h::(oddmembers t)
  587. | false => (oddmembers t)
  588. end
  589. end.
  590.  
  591. (*Example test_oddmembers:
  592. oddmembers [0;1;0;2;3;0;0] = [1;3].
  593. Proof. reflexivity. Qed.*)
  594.  
  595. Definition countoddmembers (l:natlist) : nat :=
  596. length (oddmembers l).
  597.  
  598. (*Example test_countoddmembers1:
  599. countoddmembers [1;0;3;1;4;5] = 4.
  600. Proof. reflexivity. Qed.
  601.  
  602. Example test_countoddmembers2:
  603. countoddmembers [0;2;4] = 0.
  604. Proof. reflexivity. Qed.
  605.  
  606. Example test_countoddmembers3:
  607. countoddmembers nil = 0.
  608. Proof. reflexivity. Qed.*)
  609.  
  610. Fixpoint alternate (l1 l2 : natlist) : natlist :=
  611. match l1, l2 with
  612. | [], _ => l2
  613. | t::q, y::s => t::y::(alternate q s)
  614. | _, [] => l1
  615. end.
  616.  
  617. Example test_alternate1:
  618. alternate [1;2;3] [4;5;6] = [1;4;2;5;3;6].
  619. Proof. reflexivity. Qed.
  620.  
  621. Example test_alternate2:
  622. alternate [1] [4;5;6] = [1;4;5;6].
  623. Proof. reflexivity. Qed.
  624.  
  625. Example test_alternate3:
  626. alternate [1;2;3] [4] = [1;4;2;3].
  627. Proof. reflexivity. Qed.
  628.  
  629. Example test_alternate4:
  630. alternate [] [20;30] = [20;30].
  631. Proof. reflexivity. Qed.
  632.  
  633. Definition bag := natlist.
  634.  
  635. Fixpoint count (v:nat) (s:bag) : nat :=
  636. match s with
  637. | nil => O
  638. | h::t => match h =? v with
  639. | false => count v t
  640. | true => S (count v t)
  641. end
  642. end.
  643.  
  644. Example test_count1: count 1 [1;2;3;1;4;1] = 3.
  645. Proof. reflexivity. Qed.
  646. Example test_count2: count 6 [1;2;3;1;4;1] = 0.
  647. Proof. reflexivity. Qed.
  648.  
  649. Definition sum : bag -> bag -> bag := app.
  650.  
  651. Example test_sum1: count 1 (sum [1;2;3] [1;4;1]) = 3.
  652. Proof. reflexivity. Qed.
  653.  
  654. Definition add (v:nat) (s:bag) : bag :=
  655. v::s. (* left or right, not precised *)
  656.  
  657. Example test_add1: count 1 (add 1 [1;4;1]) = 3.
  658. Proof. reflexivity. Qed.
  659. Example test_add2: count 5 (add 1 [1;4;1]) = 0.
  660. Proof. reflexivity. Qed.
  661.  
  662. (* Definition member (v:nat) (s:bag) : bool :=
  663. match s with
  664. | [] => false
  665. | h::t => match h =? v with
  666. | true => true
  667. | false => member v t
  668. end
  669. end. *)
  670.  
  671. Definition member (v:nat) (s:bag) : bool :=
  672. match count v s with
  673. | 0 => false
  674. | _ => true
  675. end.
  676.  
  677. Example test_member1: member 1 [1;4;1] = true.
  678. Proof. reflexivity. Qed.
  679. Example test_member2: member 2 [1;4;1] = false.
  680. Proof. reflexivity. Qed.
  681.  
  682. (* impossible not to supply v argument when calling remove_on *)
  683.  
  684. Fixpoint remove_one (v:nat) (s:bag) : bag :=
  685. match s with
  686. | [] => []
  687. | h::t => match h =? v with
  688. | true => t
  689. | false => h::(remove_one v t)
  690. end
  691. end.
  692.  
  693. (* Compute (remove_one []). *)
  694.  
  695. Example test_remove_one1:
  696. count 5 (remove_one 5 [2;1;5;4;1]) = 0.
  697. Proof. reflexivity. Qed.
  698.  
  699. Example test_remove_one2:
  700. count 5 (remove_one 5 [2;1;4;1]) = 0.
  701. Proof. reflexivity. Qed.
  702.  
  703. Example test_remove_one3:
  704. count 4 (remove_one 5 [2;1;4;5;1;4]) = 2.
  705. Proof. reflexivity. Qed.
  706.  
  707. Example test_remove_one4:
  708. count 5 (remove_one 5 [2;1;5;4;5;1;4]) = 1.
  709. Proof. reflexivity. Qed.
  710.  
  711.  
  712. Fixpoint remove_all (v:nat) (s:bag) : bag :=
  713. match s with
  714. | [] => []
  715. | h::t => match h =? v with
  716. | true => remove_all v t
  717. | false => h::(remove_all v t)
  718. end
  719. end.
  720.  
  721. (* count verification isn't the right way to do --' *)
  722. Example test_remove_all1: count 5 (remove_all 5 [2;1;5;4;1]) = 0.
  723. Proof. reflexivity. Qed.
  724.  
  725. Example test_remove_all2: count 5 (remove_all 5 [2;1;4;1]) = 0.
  726. Proof. reflexivity. Qed.
  727.  
  728. Example test_remove_all3: count 4 (remove_all 5 [2;1;4;5;1;4]) = 2.
  729. Proof. reflexivity. Qed.
  730.  
  731. (* Compute (remove_all 5 [2;1;5;4;5;1;4;5;1;4]). *)
  732.  
  733. Example test_remove_all4: count 5 (remove_all 5 [2;1;5;4;5;1;4;5;1;4]) = 0.
  734. Proof. reflexivity. Qed.
  735.  
  736. (* Fixpoint subset (s1:bag) (s2:bag) : bool :=
  737. match (length s1) with
  738. | 0 => true
  739. | _ => match (count (hd 0 s1) s2) with
  740. | 0 => false
  741. | _ => subset (tl s1) (remove_one (hd 0 s1) s2)
  742. end
  743. end. *) (* why nope ? *)
  744.  
  745. (* Fixpoint subset (s1:bag) (s2:bag) : bool :=
  746. match s1 with
  747. | [] => true
  748. | h::t => if (member h s2) then (subset t (remove_one h s2)) else false (* don't know this syntax at this point *)
  749. end. *)
  750.  
  751. Fixpoint subset (s1:bag) (s2:bag) : bool :=
  752. match s1 with
  753. | [] => true
  754. | x :: xs => andb (member x s2)
  755. (subset xs (remove_one x s2))
  756. end.
  757.  
  758. Example test_subset1: subset [1;2] [2;1;4;1] = true.
  759. Proof. reflexivity. Qed.
  760.  
  761. Example test_subset2: subset [1;2;2] [2;1;4;1] = false.
  762. Proof. reflexivity. Qed.
  763.  
  764. (* TODO *)
  765.  
  766. Notation "x ++ y" := (app x y)
  767. (right associativity, at level 60).
  768.  
  769. Theorem nil_app : forall l:natlist,
  770. [] ++ l = l.
  771. Proof. reflexivity. Qed.
  772.  
  773. Theorem tl_length_pred : forall l:natlist,
  774. pred (length l) = length (tl l).
  775. Proof.
  776. intros l. destruct l as [| n l'].
  777. - (* l = nil *)
  778. reflexivity.
  779. - (* l = cons n l' *)
  780. reflexivity. Qed.
  781.  
  782. Theorem app_assoc : forall l1 l2 l3 : natlist,
  783. (l1 ++ l2) ++ l3 = l1 ++ (l2 ++ l3).
  784. Proof.
  785. intros l1 l2 l3. induction l1 as [| n l1' IHl1'].
  786. - (* l1 = nil *)
  787. reflexivity.
  788. - (* l1 = cons n l1' *)
  789. simpl. rewrite -> IHl1'. reflexivity. Qed.
  790.  
  791. Fixpoint rev (l:natlist) : natlist :=
  792. match l with
  793. | nil => nil
  794. | h :: t => rev t ++ [h]
  795. end.
  796.  
  797. Example test_rev1: rev [1;2;3] = [3;2;1].
  798. Proof. reflexivity. Qed.
  799. Example test_rev2: rev nil = nil.
  800. Proof. reflexivity. Qed.
  801.  
  802. Theorem app_length : forall l1 l2 : natlist,
  803. length (l1 ++ l2) = (length l1) + (length l2).
  804. Proof.
  805. (* WORKED IN CLASS what does it mean ? *)
  806. intros l1 l2. induction l1 as [| n l1' IHl1'].
  807. - (* l1 = nil *)
  808. reflexivity.
  809. - (* l1 = cons *)
  810. simpl. rewrite -> IHl1'. reflexivity. Qed.
  811.  
  812. Theorem rev_length : forall l : natlist,
  813. length (rev l) = length l.
  814. Proof.
  815. intros l. induction l as [| n l' IHl'].
  816. - (* l = nil *)
  817. reflexivity.
  818. - (* l = cons *)
  819. simpl. rewrite -> app_length, plus_comm.
  820. simpl. rewrite -> IHl'. reflexivity. Qed.
  821.  
  822. Search rev. (* list theorems with rev *)
  823.  
  824. Theorem app_nil_r : forall l : natlist,
  825. l ++ [] = l.
  826. Proof.
  827. intros l. induction l as [| n l' IHl'].
  828. - (* l = nil *)
  829. reflexivity.
  830. - (* l = cons *)
  831. simpl. rewrite -> IHl'. reflexivity. Qed.
  832.  
  833. (*Theorem rev_lemna: forall l1 l2 : natlist,
  834. rev ()*)
  835.  
  836. Theorem rev_app_distr: forall l1 l2 : natlist,
  837. rev (l1 ++ l2) = rev l2 ++ rev l1.
  838. Proof.
  839. intros l1 l2. induction l1.
  840. - (* l = nil *)
  841. simpl. rewrite -> app_nil_r. reflexivity.
  842. - (* l = cons *)
  843. simpl. rewrite -> IHl1. rewrite -> app_assoc. reflexivity. Qed.
  844.  
  845. Theorem rev_involutive : forall l : natlist,
  846. rev (rev l) = l.
  847. Proof.
  848. intros l. induction l.
  849. - (* l = nil *)
  850. reflexivity.
  851. - (* l = cons *)
  852. simpl. rewrite -> rev_app_distr. rewrite -> IHl. reflexivity. Qed.
  853.  
  854. Theorem app_assoc4 : forall l1 l2 l3 l4 : natlist,
  855. l1 ++ (l2 ++ (l3 ++ l4)) = ((l1 ++ l2) ++ l3) ++ l4.
  856. Proof.
  857. intros l1 l2 l3 l4.
  858. rewrite -> app_assoc.
  859. rewrite -> app_assoc.
  860. reflexivity.
  861. Qed.
  862.  
  863. Lemma nonzeros_app : forall l1 l2 : natlist,
  864. nonzeros (l1 ++ l2) = (nonzeros l1) ++ (nonzeros l2).
  865. Proof.
  866. intros l1 l2. induction l1 as [| n1 l1']. (* what is n1 *)
  867. - (* l = nil *)
  868. reflexivity.
  869. - (* l = cons *)
  870. simpl. destruct n1 as [|n1']. (* quite lost here... *)
  871. + simpl. rewrite <- IHl1'. reflexivity.
  872. + simpl. rewrite <- IHl1'. reflexivity.
  873. Qed.
  874.  
  875. Fixpoint eqblist (l1 l2 : natlist) : bool
  876. := match l1, l2 with
  877. | t0::q0, t1::q1 => andb (t0 =? t1) (eqblist q0 q1)
  878. | [], [] => true
  879. | _, _ => false
  880. end.
  881.  
  882. Example test_eqblist1 :
  883. (eqblist nil nil = true).
  884. Proof. reflexivity. Qed.
  885.  
  886. Example test_eqblist2 :
  887. eqblist [1;2;3] [1;2;3] = true.
  888. Proof. reflexivity. Qed.
  889.  
  890. Example test_eqblist3 :
  891. eqblist [1;2;3] [1;2;4] = false.
  892. Proof. reflexivity. Qed.
  893.  
  894. Theorem eqb_refl : forall n:nat,
  895. true = (n =? n).
  896. Proof.
  897. intros n. induction n.
  898. - simpl. reflexivity.
  899. - simpl. rewrite <- IHn. reflexivity.
  900. Qed.
  901.  
  902. Theorem eqblist_refl : forall l:natlist,
  903. true = eqblist l l.
  904. Proof.
  905. intros l. induction l.
  906. - simpl. reflexivity.
  907. - rewrite -> IHl. simpl. rewrite <- eqb_refl. reflexivity.
  908. Qed.
  909.  
  910. Theorem silly1 : forall(n m o p : nat),
  911. n = m ->
  912. [n;o] = [n;p] ->
  913. [n;o] = [m;p].
  914. Proof.
  915. intros n m o p eq1 eq2.
  916. rewrite <- eq1.
  917. apply eq2. Qed.
  918.  
  919. Theorem silly2 : forall (n m o p : nat),
  920. n = m ->
  921. (n = m -> [n;o] = [m;p]) ->
  922. [n;o] = [m;p].
  923. Proof.
  924. intros n m o p eq1 eq2.
  925. apply eq2. apply eq1. Qed.
  926.  
  927. Theorem silly2a : forall (n m : nat),
  928. (n,n) = (m,m) ->
  929. (forall (q r : nat), (q,q) = (r,r) -> [q] = [r]) ->
  930. [n] = [m].
  931. Proof.
  932. intros n m eq1 eq2.
  933. apply eq2. apply eq1. Qed.
  934.  
  935. Theorem silly3_firsttry : forall(n : nat),
  936. true = (n =? 5) ->
  937. (S (S n)) =? 7 = true.
  938. Proof.
  939. intros n H.
  940. symmetry.
  941. apply H. Qed.
  942.  
  943. Theorem trans_eq : forall (X:Type) (n m o : X),
  944. n = m -> m = o -> n = o.
  945. Proof.
  946. intros X n m o eq1 eq2. rewrite -> eq1. rewrite <- eq2.
  947. reflexivity. Qed.
  948.  
  949. Example trans_eq_example' : forall (a b c d e f : nat),
  950. [a;b] = [c;d] ->
  951. [c;d] = [e;f] ->
  952. [a;b] = [e;f].
  953. Proof.
  954. intros a b c d e f eq1 eq2.
  955. apply trans_eq with (m:=[c;d]).
  956. apply eq1. apply eq2. Qed.
  957.  
  958. Theorem S_injective : forall (n m : nat),
  959. S n = S m ->
  960. n = m.
  961. Proof.
  962. intros n m H1.
  963. assert (H2: n = pred (S n)). { reflexivity. }
  964. rewrite H2. rewrite H1. reflexivity.
  965. Qed.
  966.  
  967. Theorem injection_ex1 : forall (n m o : nat),
  968. [n; m] = [o; o] ->
  969. [n] = [m].
  970. Proof.
  971. intros n m o H.
  972. injection H. intros H1 H2.
  973. rewrite H1. rewrite H2. reflexivity.
  974. Qed.
  975.  
  976. Theorem injection_ex2 : forall(n m : nat),
  977. [n] = [m] ->
  978. n = m.
  979. Proof.
  980. intros n m H.
  981. injection H as Hnm. rewrite Hnm.
  982. reflexivity. Qed.
  983.  
  984. Theorem eqb_0_l : forall n,
  985. 0 =? n = true -> n = 0.
  986. Proof.
  987. intros n.
  988. destruct n as [| n'] eqn:E.
  989. - (* n = 0 *)
  990. intros H. reflexivity.
  991. - (* n = S n' *)
  992. simpl.
  993. intros H. discriminate H.
  994. Qed.
  995.  
  996. Theorem discriminate_ex1 : forall (n : nat),
  997. S n = O ->
  998. 2 + 2 = 5.
  999. Proof.
  1000. intros n contra. discriminate contra. Qed.
  1001.  
  1002. Theorem discriminate_ex2 : forall (n m : nat),
  1003. false = true ->
  1004. [n] = [m].
  1005. Proof.
  1006. intros n m contra. discriminate contra. Qed.
  1007.  
  1008. Theorem S_inj : forall (n m : nat) (b : bool),
  1009. (S n) =? (S m) = b ->
  1010. n =? m = b.
  1011. Proof.
  1012. intros n m b H. simpl in H. apply H. Qed.
  1013.  
  1014. Theorem silly3'' : forall (n : nat),
  1015. (n =? 5 = true -> (S (S n)) =? 7 = true) ->
  1016. true = (n =? 5) ->
  1017. true = ((S (S n)) =? 7).
  1018. Proof.
  1019. intros n eq H.
  1020. symmetry in H. apply eq in H. symmetry in H.
  1021. apply H. Qed.
  1022.  
  1023. Theorem silly3' : forall (n : nat),
  1024. (n =? 5 = true -> (S (S n)) =? 7 = true) ->
  1025. true = (n =? 5) ->
  1026. true = ((S (S n)) =? 7).
  1027. Proof.
  1028. intros n eq H.
  1029. symmetry. apply eq. symmetry.
  1030. apply H. Qed.
  1031.  
  1032. Fixpoint double' (n:nat) :=
  1033. match n with
  1034. | O => O
  1035. | S n' => S (S (double n'))
  1036. end.
  1037.  
  1038. Theorem double_injective_FAILED : forall n m,
  1039. double n = double m ->
  1040. n = m.
  1041. Proof.
  1042. intros n m. induction n as [| n' IHn'].
  1043. - (* n = O *) simpl. intros eq. destruct m as [| m'] eqn:E.
  1044. + (* m = O *) reflexivity.
  1045. + (* m = S m' *) discriminate eq.
  1046. - (* n = S n' *) intros eq. destruct m as [| m'] eqn:E.
  1047. + (* m = O *) discriminate eq.
  1048. + (* m = S m' *) apply f_equal. Abort.
  1049.  
  1050. Theorem double_injective : forall n m,
  1051. double n = double m ->
  1052. n = m.
  1053. Proof.
  1054. intros n. induction n as [| n' IHn'].
  1055. - (* n = O *) simpl. intros m eq. destruct m as [| m'] eqn:E.
  1056. + (* m = O *) reflexivity.
  1057. + (* m = S m' *) discriminate eq.
  1058. - (* n = S n' *) simpl.
  1059. intros m eq.
  1060. destruct m as [| m'] eqn:E.
  1061. + (* m = O *) simpl.
  1062. discriminate eq.
  1063. + (* m = S m' *)
  1064. apply f_equal.
  1065. apply IHn'. injection eq as goal. apply goal. Qed.
  1066.  
  1067. (* HW: ex where we were *)
  1068.  
  1069. Fixpoint leb (n m : nat) : bool :=
  1070. match n with
  1071. | O => true
  1072. | S n' =>
  1073. match m with
  1074. | O => false
  1075. | S m' => leb n' m'
  1076. end
  1077. end.
  1078.  
  1079. Notation "x <=? y" := (leb x y) (at level 70) : nat_scope.
  1080.  
  1081. Definition lesb (n m : nat) : bool := andb (leb n m) (negb (n =? m)).
  1082.  
  1083. Definition gtb (n m : nat) : bool := negb (lesb n m).
  1084.  
  1085.  
  1086. Notation "x >=? y" := (gtb x y) (at level 70) : nat_scope.
  1087.  
  1088.  
  1089. Theorem count_member_nonzero : forall (s : bag),
  1090. 1 <=? (count 1 (1 :: s)) = true.
  1091. Proof.
  1092. intros s. induction s.
  1093. - simpl. reflexivity.
  1094. - simpl. reflexivity.
  1095. Qed.
  1096.  
  1097. Theorem leb_n_Sn : forall n,
  1098. n <=? (S n) = true.
  1099. Proof.
  1100. intros n. induction n as [| n' IHn'].
  1101. - (* 0 *)
  1102. simpl. reflexivity.
  1103. - (* S n' *)
  1104. simpl. rewrite IHn'. reflexivity. Qed.
  1105.  
  1106. (* Theorem remove_does_not_increase_count: forall (s : bag),
  1107. (count 0 (remove_one 0 s)) <=? (count 0 s) = true.
  1108. Proof.
  1109. intros s. induction s.
  1110. - simpl. reflexivity.
  1111. - simpl. rewrite -> leb_n_Sn. rewrite IHs.
  1112. Qed. *)
  1113.  
  1114. Inductive natoption : Type :=
  1115. | Some (n : nat)
  1116. | None.
  1117.  
  1118. Definition hd_error (l : natlist) : natoption :=
  1119. match l with
  1120. | t::q => Some t
  1121. | [] => None
  1122. end.
  1123.  
  1124. Example test_hd_error1 : hd_error [] = None.
  1125. Proof. reflexivity. Qed.
  1126.  
  1127. Example test_hd_error2 : hd_error [1] = Some 1.
  1128. Proof. reflexivity. Qed.
  1129.  
  1130. Example test_hd_error3 : hd_error [5;6] = Some 5.
  1131. Proof. reflexivity. Qed.
  1132.  
  1133. Definition option_elim (d : nat) (o : natoption) : nat :=
  1134. match o with
  1135. | Some n' => n'
  1136. | None => d
  1137. end.
  1138.  
  1139. Theorem option_elim_hd : forall (l:natlist) (default:nat),
  1140. hd default l = option_elim default (hd_error l).
  1141. Proof.
  1142. intros l default. induction l.
  1143. - simpl. reflexivity.
  1144. - simpl. reflexivity.
  1145. Qed.
  1146.  
  1147. Inductive id : Type :=
  1148. | Id (n : nat).
  1149.  
  1150. Definition eqb_id (x1 x2 : id) :=
  1151. match x1, x2 with
  1152. | Id n1, Id n2 => n1 =? n2
  1153. end.
  1154.  
  1155. Theorem eqb_id_refl : forall x, true = eqb_id x x.
  1156. Proof.
  1157. intros x. destruct x. simpl. rewrite <- eqb_refl. reflexivity.
  1158. Qed.
  1159.  
  1160. Inductive partial_map : Type :=
  1161. | empty
  1162. | record (i : id) (v : nat) (m : partial_map).
  1163.  
  1164. Fixpoint find (x : id) (d : partial_map) : natoption :=
  1165. match d with
  1166. | empty => None
  1167. | record y v d' => if eqb_id x y
  1168. then Some v
  1169. else find x d'
  1170. end.
  1171.  
  1172. Definition update (d : partial_map)
  1173. (x : id) (value : nat)
  1174. : partial_map :=
  1175. record x value d.
  1176.  
  1177. (* Theorem update_eq : forall (d : partial_map) (x : id) (v: nat),
  1178. find x (update d x v) = Some v.
  1179. Proof.
  1180. intros d x v. induction d.
  1181. - simpl. rewrite <- eqb_id_refl. reflexivity.
  1182. - rewrite <- IHd. simpl.
  1183. Qed.
  1184.  
  1185. Theorem update_neq :
  1186. forall (d : partial_map) (x y : id) (o: nat),
  1187. eqb_id x y = false -> find x (update d y o) = find x d.
  1188. Proof.
  1189.  
  1190. Qed. *)
  1191.  
  1192. Notation "( x , y )" := (pair x y).
  1193. Notation "X * Y" := (prod X Y) : type_scope.
  1194.  
  1195. Notation "x :: y" := (cons x y)
  1196. (at level 60, right associativity).
  1197.  
  1198. Notation "[ ]" := nil.
  1199.  
  1200. (*Fixpoint combine {X Y : Type} (lx : list X) (ly : list Y)
  1201. : list (X*Y) :=
  1202. match lx, ly with
  1203. | [], _ => []
  1204. | _, [] => []
  1205. | x :: tx, y :: ty => (x, y) :: (combine tx ty)
  1206. end.
  1207.  
  1208. Check @combine.*)
  1209.  
  1210. Notation "[ x ; .. ; y ]" := (cons x .. (cons y []) ..).
  1211.  
  1212. (*Compute (combine [1;2] [false;false;true;true]).
  1213.  
  1214. Fixpoint split {X Y : Type} (l : list (X*Y))
  1215. : (list X) * (list Y) :=
  1216. match l with
  1217. | [] => ([], [])
  1218. | (a, b)::q => (a::(fst (split q)), b::(snd (split q)))
  1219. end.
  1220.  
  1221. Example test_split:
  1222. split [(1,false);(2,false)] = ([1;2],[false;false]).
  1223. Proof. reflexivity. Qed.*)
  1224.  
  1225. (*Definition hd_error {X : Type} (l : list X) : option X :=
  1226. match l with
  1227. | [] => None
  1228. | t::q => Some t
  1229. end.
  1230.  
  1231. Check @hd_error.
  1232. Example test_hd_error1 : hd_error [1;2] = Some 1.
  1233. Proof. reflexivity. Qed.
  1234. Example test_hd_error2 : hd_error [[1];[2]] = Some [1].
  1235. Proof. reflexivity. Qed.
  1236.  
  1237. Fixpoint filter {X:Type} (test: X->bool) (l:list X)
  1238. : (list X) :=
  1239. match l with
  1240. | [] => []
  1241. | h :: t => if test h then h :: (filter test t)
  1242. else filter test t
  1243. end.
  1244.  
  1245. Definition filter_even_gt7 (l : list nat) : list nat :=
  1246. filter (fun l => andb (evenb l) (l >=? 7)) l.
  1247.  
  1248. Example test_filter_even_gt7_1 :
  1249. filter_even_gt7 [1;2;6;9;10;3;12;8] = [10;12;8].
  1250. Proof. reflexivity. Qed.
  1251.  
  1252. Example test_filter_even_gt7_2 :
  1253. filter_even_gt7 [5;2;6;19;129] = [].
  1254. Proof. reflexivity. Qed.
  1255.  
  1256. (*Fixpoint filterneg {X:Type} (test: X->bool) (l:list X)
  1257. : (list X) :=
  1258. match l with
  1259. | [] => []
  1260. | h :: t => if negb (test h) then h :: (filter test t)
  1261. else filter test t
  1262. end.
  1263.  
  1264. Definition partition {X : Type}
  1265. (test : X -> bool)
  1266. (l : list X)
  1267. : list X * list X :=
  1268. ((filter test l), (filterneg test l)).*)
  1269.  
  1270. Definition partition {X : Type}
  1271. (test : X -> bool)
  1272. (l : list X)
  1273. : list X * list X :=
  1274. (filter test l, filter (fun x => negb (test x)) l).
  1275.  
  1276. (* Example test_partition1: partition oddb [1;2;3;4;5] = ([1;3;5], [2;4]).
  1277. Proof. simpl. reflexivity. Qed.
  1278.  
  1279. Example test_partition2: partition (fun x => false) [5;9;0] = ([], [5;9;0]).
  1280. Proof. reflexivity. Qed.*)
  1281.  
  1282. Fixpoint map {X Y: Type} (f:X->Y) (l:list X) : (list Y) :=
  1283. match l with
  1284. | [] => []
  1285. | h :: t => (f h) :: (map f t)
  1286. end.
  1287.  
  1288. (*
  1289.  
  1290. *)
  1291.  
  1292. Fixpoint rev {X:Type} (l:list X) : list X :=
  1293. match l with
  1294. | nil => nil
  1295. | cons h t => app (rev t) (cons h nil)
  1296. end.
  1297.  
  1298. (* Theorem map_rev : forall (X Y : Type) (f : X -> Y) (l : list X),
  1299. map f (rev l) = rev (map f l).
  1300. Proof.
  1301. intros X Y f l. induction l.
  1302. - simpl. reflexivity.
  1303. - simpl. rewrite <- IHl. simpl. reflexivity.
  1304. Qed. *)
  1305.  
  1306. Fixpoint flat_map {X Y: Type} (f: X -> list Y) (l: list X)
  1307. : (list Y) :=
  1308. match l with
  1309. | t::q => (f t)++(flat_map f q)
  1310. | [] => []
  1311. end.
  1312.  
  1313. Example test_flat_map1:
  1314. flat_map (fun n => [n;n;n]) [1;5;4]
  1315. = [1; 1; 1; 5; 5; 5; 4; 4; 4].*)
  1316.  
  1317. (* Theorem eqb_true : forall n m,
  1318. n =? m = true -> n = m.
  1319. Proof.
  1320. intros m n H. (*Print eqb.*) generalize dependent m.
  1321. induction n.
  1322. - intros m H. simpl in H. destruct m.
  1323. + reflexivity.
  1324. + discriminate H.
  1325. - intros m H. simpl in H. destruct m.
  1326. + discriminate H.
  1327. + f_equal.
  1328. Qed. *)
  1329.  
  1330. (*
  1331.  
  1332. intros
  1333. simpl
  1334. reflexivity
  1335. rewrite <-/-> H
  1336. destruct x as ...
  1337. induction x as ...
  1338.  
  1339. simpl [in H]
  1340. symmetry [in H]
  1341. rewrite [in H]
  1342. apply [in H]
  1343. injection H [as ?]
  1344. discriminate H
  1345. generalize dependent
  1346. unfold [in H]
  1347. split
  1348. left/right
  1349.  
  1350. (apply) f_equal ?
  1351.  
  1352. *)
  1353.  
  1354. (*Definition square n := n * n.*)
  1355.  
  1356. (*Lemma square_mult : forall n m, square (n * m) = square n * square m.
  1357. Proof.
  1358. intros n m.
  1359. unfold square.
  1360. rewrite mult_assoc.
  1361. assert (H : n * m * n = n * n * m).
  1362. { rewrite mult_comm. apply mult_assoc. }
  1363. rewrite H. rewrite mult_assoc. reflexivity.
  1364. Qed.*)
  1365.  
  1366. (*Fixpoint fold {X Y: Type} (f: X->Y->Y) (l: list X) (b: Y)
  1367. : Y :=
  1368. match l with
  1369. | nil => b
  1370. | h :: t => f h (fold f t b)
  1371. end.
  1372.  
  1373. Definition fold_length {X : Type} (l : list X) : nat :=
  1374. fold (fun _ n => S n) l 0.
  1375.  
  1376. Example test_fold_length1 : fold_length [4;7;0] = 3.
  1377. Proof. reflexivity. Qed.
  1378.  
  1379. (* "to solve the goal" *)
  1380.  
  1381. Definition foo (x: nat) := 5.
  1382.  
  1383. Fact silly_fact_1 : forall m, foo m + 1 = foo (m + 1) + 1.
  1384. Proof.
  1385. intros m.
  1386. simpl.
  1387. reflexivity.
  1388. Qed.
  1389.  
  1390. Definition bar x :=
  1391. match x with
  1392. | O => 5
  1393. | S _ => 5
  1394. end.
  1395.  
  1396. Fact silly_fact_2_FAILED : forall m, bar m + 1 = bar (m + 1) + 1.
  1397. Proof.
  1398. intros m.
  1399. simpl. (* Does nothing! *)
  1400. Abort.
  1401.  
  1402. Fact silly_fact_2 : forall m, bar m + 1 = bar (m + 1) + 1.
  1403. Proof.
  1404. intros m.
  1405. destruct m eqn:E.
  1406. - simpl. reflexivity.
  1407. - simpl. reflexivity.
  1408. Qed.
  1409.  
  1410. Fact silly_fact_2' : forall m, bar m + 1 = bar (m + 1) + 1.
  1411. Proof.
  1412. intros m.
  1413. unfold bar.
  1414. destruct m eqn:E.
  1415. - reflexivity.
  1416. - reflexivity.
  1417. Qed.
  1418.  
  1419. Definition sillyfun1 (n : nat) : bool :=
  1420. if n =? 3 then true
  1421. else if n =? 5 then true
  1422. else false.
  1423.  
  1424. Theorem sillyfun1_odd_FAILED : forall (n : nat),
  1425. sillyfun1 n = true ->
  1426. oddb n = true.
  1427. Proof.
  1428. intros n eq. unfold sillyfun1 in eq.
  1429. destruct (n =? 3).
  1430. (* stuck... *)
  1431. Abort.
  1432.  
  1433. (*Theorem sillyfun1_odd : forall (n : nat),
  1434. sillyfun1 n = true ->
  1435. oddb n = true.
  1436. Proof.
  1437. intros n eq. unfold sillyfun1 in eq.
  1438. destruct (n =? 3) eqn:Heqe3.
  1439. (* Now we have the same state as at the point where we got
  1440. stuck above, except that the context contains an extra
  1441. equality assumption, which is exactly what we need to
  1442. make progress. *)
  1443. - (* e3 = true *) apply eqb_true in Heqe3.
  1444. rewrite -> Heqe3. reflexivity.
  1445. - (* e3 = false *)
  1446. (* When we come to the second equality test in the body
  1447. of the function we are reasoning about, we can use
  1448. eqn: again in the same way, allowing us to finish the
  1449. proof. *)
  1450. destruct (n =? 5) eqn:Heqe5.
  1451. + (* e5 = true *)
  1452. apply eqb_true in Heqe5.
  1453. rewrite -> Heqe5. reflexivity.
  1454. + (* e5 = false *) discriminate eq. Qed.*)
  1455.  
  1456. (*Theorem fold_length_correct : forall X (l : list X),
  1457. fold_length l = length l.
  1458. Proof.
  1459. intros l l0. induction l0.
  1460. - simpl.
  1461. Qed.*)
  1462.  
  1463. Lemma zero_or_succ :
  1464. forall n : nat, n = 0 \/ n = S (pred n).
  1465. Proof.
  1466. (* WORKED IN CLASS *)
  1467. intros [|n].
  1468. - left. reflexivity.
  1469. - right. reflexivity.
  1470. Qed.
  1471.  
  1472. Theorem ex_falso_quodlibet : forall (P:Prop),
  1473. False -> P.
  1474. Proof.
  1475. (* WORKED IN CLASS *)
  1476. intros P contra.
  1477. destruct contra. Qed.
  1478.  
  1479.  
  1480. Theorem contradiction_implies_anything : forall P Q : Prop,
  1481. (P /\ ~P) -> Q.
  1482. Proof.
  1483. (* WORKED IN CLASS *)
  1484. intros P Q [HP HNA]. unfold not in HNA.
  1485. apply HNA in HP. destruct HP. Qed.
  1486.  
  1487. Theorem silly1 : forall (n m o p : nat),
  1488. n = m ->
  1489. [n;o] = [n;p] ->
  1490. [n;o] = [m;p].
  1491. Proof.
  1492. intros n m o p eq1 eq2.
  1493. rewrite <- eq1.
  1494. apply eq2. Qed.
  1495.  
  1496. Theorem silly2 : forall (n m o p : nat),
  1497. n = m ->
  1498. (forall (q r : nat), q = r -> [q;o] = [r;p]) ->
  1499. [n;o] = [m;p].
  1500. Proof.
  1501. intros n m o p eq1 eq2.
  1502. apply eq2. apply eq1. Qed.
  1503.  
  1504. Theorem silly2a : forall (n m : nat),
  1505. (n,n) = (m,m) ->
  1506. (forall (q r : nat), (q,q) = (r,r) -> [q] = [r]) ->
  1507. [n] = [m].
  1508. Proof.
  1509. intros n m eq1 eq2.
  1510. apply eq2. apply eq1. Qed.
  1511.  
  1512. Theorem silly_ex :
  1513. (forall n, evenb n = true -> oddb (S n) = true) ->
  1514. oddb 3 = true ->
  1515. evenb 4 = true.
  1516. Proof.
  1517. intros n.
  1518. simpl. discriminate.
  1519. Qed.
  1520.  
  1521. Theorem silly3_firsttry : forall (n : nat),
  1522. true = (n =? 5) ->
  1523. (S (S n)) =? 7 = true.
  1524. Proof.
  1525. intros n H.
  1526. symmetry.
  1527. apply H. Qed.*)
  1528.  
  1529. (*Fixpoint rev (l:list nat) : list nat :=
  1530. match l with
  1531. | nil => nil
  1532. | h :: t => rev t ++ [h]
  1533. end.
  1534.  
  1535. Theorem rev_exercise1 : forall (l l' : list nat),
  1536. l = rev l' ->
  1537. l' = rev l.
  1538. Proof.
  1539. intros l l' H.
  1540. (*rewrite -> H.
  1541. Search rev.*)
  1542. Qed.*)
  1543.  
  1544. Example trans_eq_example : forall (a b c d e f : nat),
  1545. [a;b] = [c;d] ->
  1546. [c;d] = [e;f] ->
  1547. [a;b] = [e;f].
  1548. Proof.
  1549. intros a b c d e f eq1 eq2.
  1550. rewrite -> eq1. rewrite -> eq2. reflexivity. Qed.
  1551.  
  1552. Theorem trans_eq' : forall(X:Type) (n m o : X),
  1553. n = m -> m = o -> n = o.
  1554. Proof.
  1555. intros X n m o eq1 eq2. rewrite -> eq1. rewrite -> eq2.
  1556. reflexivity. Qed.
  1557.  
  1558. Example trans_eq_example'' : forall (a b c d e f : nat),
  1559. [a;b] = [c;d] ->
  1560. [c;d] = [e;f] ->
  1561. [a;b] = [e;f].
  1562. Proof.
  1563. intros a b c d e f eq1 eq2.
  1564. apply trans_eq' with (m:=[c;d]).
  1565. apply eq1. apply eq2. Qed.
  1566.  
  1567. Theorem S_injective' : forall(n m : nat),
  1568. S n = S m ->
  1569. n = m.
  1570. Proof.
  1571. intros n m H1.
  1572. assert (H2: n = pred (S n)). { reflexivity. }
  1573. rewrite H2. rewrite H1. reflexivity.
  1574. Qed.
  1575.  
  1576. Theorem S_injective'' : forall (n m : nat),
  1577. S n = S m ->
  1578. n = m.
  1579. Proof.
  1580. intros n m H.
  1581. injection H. intros Hnm. apply Hnm.
  1582. Qed.
  1583.  
  1584. Theorem injection_ex1' : forall (n m o : nat),
  1585. [n; m] = [o; o] ->
  1586. [n] = [m].
  1587. Proof.
  1588. intros n m o H.
  1589. injection H. intros H1 H2.
  1590. rewrite H1. rewrite H2. reflexivity.
  1591. Qed.
  1592.  
  1593. Theorem injection_ex2' : forall (n m : nat),
  1594. [n] = [m] ->
  1595. n = m.
  1596. Proof.
  1597. intros n m H.
  1598. injection H as Hnm. rewrite Hnm.
  1599. reflexivity. Qed.
  1600.  
  1601. Example injection_ex3 : forall (x y z : nat) (l j : list nat),
  1602. x :: y :: l = z :: j ->
  1603. y :: l = x :: j ->
  1604. x = y.
  1605. Proof.
  1606. intros x y z l j.
  1607. Qed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement