Advertisement
elvecent

CoqInduction

Mar 27th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.46 KB | None | 0 0
  1. Require Export Utf8.
  2.  
  3.  
  4. (** * Induction: Proof by Induction *)
  5.  
  6. (** Before getting started, we need to import all of our
  7. definitions from the previous chapter: *)
  8.  
  9. Require Export Basics.
  10.  
  11. Module Induction.
  12.  
  13. (** For the [Require Export] to work, you first need to use
  14. [coqc] to compile [Basics.v] into [Basics.vo]. This is like
  15. making a .class file from a .java file, or a .o file from a .c
  16. file. There are two ways to do it:
  17.  
  18. - In CoqIDE:
  19.  
  20. Open [Basics.v]. In the "Compile" menu, click on "Compile
  21. Buffer".
  22.  
  23. - From the command line:
  24.  
  25. [coqc Basics.v]
  26.  
  27. If you have trouble (e.g., if you get complaints about missing
  28. identifiers later in the file), it may be because the "load path"
  29. for Coq is not set up correctly. The [Print LoadPath.] command may
  30. be helpful in sorting out such issues. *)
  31.  
  32. (* ################################################################# *)
  33. (** * Proof by Induction *)
  34.  
  35. (** We proved in the last chapter that [0] is a neutral element
  36. for [+] on the left, using an easy argument based on
  37. simplification. We also observed that proving the fact that it is
  38. also a neutral element on the _right_... *)
  39.  
  40. Theorem plus_n_O_firsttry : forall n:nat,
  41. n = n + 0.
  42.  
  43. (** ... can't be done in the same simple way. Just applying
  44. [reflexivity] doesn't work, since the [n] in [n + 0] is an arbitrary
  45. unknown number, so the [match] in the definition of [+] can't be
  46. simplified. *)
  47.  
  48. Proof.
  49. intros n.
  50. simpl. (* Does nothing! *)
  51. Abort.
  52.  
  53. (** And reasoning by cases using [destruct n] doesn't get us much
  54. further: the branch of the case analysis where we assume [n = 0]
  55. goes through fine, but in the branch where [n = S n'] for some [n'] we
  56. get stuck in exactly the same way. *)
  57.  
  58. Theorem plus_n_O_secondtry : forall n:nat,
  59. n = n + 0.
  60. Proof.
  61. intros n. destruct n as [| n'].
  62. - (* n = 0 *)
  63. reflexivity. (* so far so good... *)
  64. - (* n = S n' *)
  65. simpl. (* ...but here we are stuck again *)
  66. Abort.
  67.  
  68. (** We could use [destruct n'] to get one step further, but,
  69. since [n] can be arbitrarily large, if we just go on like this
  70. we'll never finish. *)
  71.  
  72. (** To prove interesting facts about numbers, lists, and other
  73. inductively defined sets, we usually need a more powerful
  74. reasoning principle: _induction_.
  75.  
  76. Recall (from high school, a discrete math course, etc.) the
  77. _principle of induction over natural numbers_: If [P(n)] is some
  78. proposition involving a natural number [n] and we want to show
  79. that [P] holds for all numbers [n], we can reason like this:
  80. - show that [P(O)] holds;
  81. - show that, for any [n'], if [P(n')] holds, then so does
  82. [P(S n')];
  83. - conclude that [P(n)] holds for all [n].
  84.  
  85. In Coq, the steps are the same: we begin with the goal of proving
  86. [P(n)] for all [n] and break it down (by applying the [induction]
  87. tactic) into two separate subgoals: one where we must show [P(O)]
  88. and another where we must show [P(n') -> P(S n')]. Here's how
  89. this works for the theorem at hand: *)
  90.  
  91. Theorem plus_n_O : forall n:nat, n = n + 0.
  92. Proof.
  93. intros n. induction n as [| n' IHn'].
  94. - (* n = 0 *) reflexivity.
  95. - (* n = S n' *) simpl. rewrite <- IHn'. reflexivity. Qed.
  96.  
  97. (** Like [destruct], the [induction] tactic takes an [as...]
  98. clause that specifies the names of the variables to be introduced
  99. in the subgoals. Since there are two subgoals, the [as...] clause
  100. has two parts, separated by [|]. (Strictly speaking, we can omit
  101. the [as...] clause and Coq will choose names for us. In practice,
  102. this is a bad idea, as Coq's automatic choices tend to be
  103. confusing.)
  104.  
  105. In the first subgoal, [n] is replaced by [0]. No new variables
  106. are introduced (so the first part of the [as...] is empty), and
  107. the goal becomes [0 + 0 = 0], which follows by simplification.
  108.  
  109. In the second subgoal, [n] is replaced by [S n'], and the
  110. assumption [n' + 0 = n'] is added to the context with the name
  111. [IHn'] (i.e., the Induction Hypothesis for [n']). These two names
  112. are specified in the second part of the [as...] clause. The goal
  113. in this case becomes [(S n') + 0 = S n'], which simplifies to
  114. [S (n' + 0) = S n'], which in turn follows from [IHn']. *)
  115.  
  116. Theorem minus_diag : forall n,
  117. minus n n = 0.
  118. Proof.
  119. (* WORKED IN CLASS *)
  120. intros n. induction n as [| n' IHn'].
  121. - (* n = 0 *)
  122. simpl. reflexivity.
  123. - (* n = S n' *)
  124. simpl. rewrite -> IHn'. reflexivity. Qed.
  125.  
  126. (** (The use of the [intros] tactic in these proofs is actually
  127. redundant. When applied to a goal that contains quantified
  128. variables, the [induction] tactic will automatically move them
  129. into the context as needed.) *)
  130.  
  131. (** **** Exercise: 2 stars, recommended (basic_induction) *)
  132. (** Prove the following using induction. You might need previously
  133. proven results. *)
  134.  
  135. Theorem mult_0_r : forall n:nat,
  136. n * 0 = 0.
  137. Proof.
  138. induction n as [| n' IHn'].
  139. - reflexivity.
  140. - simpl. rewrite IHn'. reflexivity. Qed.
  141.  
  142. Theorem plus_n_Sm : forall n m : nat,
  143. S (n + m) = n + (S m).
  144. Proof.
  145. intros n m.
  146. induction n as [| n' IHn'].
  147. - simpl. reflexivity.
  148. - simpl. rewrite -> IHn'. reflexivity. Qed.
  149.  
  150. Theorem plus_comm : forall n m : nat,
  151. n + m = m + n.
  152. Proof.
  153. intros.
  154. induction m as [| m' IHm'].
  155. - simpl. rewrite <- plus_n_O. reflexivity.
  156. - simpl. rewrite <- IHm'.
  157. rewrite -> plus_n_Sm. reflexivity. Qed.
  158.  
  159. Theorem plus_assoc : forall n m p : nat,
  160. n + (m + p) = (n + m) + p.
  161. Proof.
  162. intros.
  163. induction n as [| n' IHn'].
  164. - simpl. reflexivity.
  165. - simpl. rewrite -> IHn'. reflexivity. Qed.
  166. (** [] *)
  167.  
  168. (** **** Exercise: 2 stars (double_plus) *)
  169. (** Consider the following function, which doubles its argument: *)
  170.  
  171. Fixpoint double (n:nat) :=
  172. match n with
  173. | O => O
  174. | S n' => S (S (double n'))
  175. end.
  176.  
  177. (** Use induction to prove this simple fact about [double]: *)
  178.  
  179. Lemma double_plus : forall n, double n = n + n .
  180. Proof.
  181. intros.
  182. induction n as [| n' IHn'].
  183. - reflexivity.
  184. - simpl. rewrite -> IHn'.
  185. rewrite -> plus_n_Sm. reflexivity. Qed.
  186. (** [] *)
  187.  
  188. Fixpoint evenb (n:nat) : bool :=
  189. match n with
  190. | O => true
  191. | S O => false
  192. | S (S n') => evenb n'
  193. end.
  194.  
  195. (** **** Exercise: 2 stars, optional (evenb_S) *)
  196. (** One inconveninent aspect of our definition of [evenb n] is the
  197. recursive call on [n - 2]. This makes proofs about [evenb n]
  198. harder when done by induction on [n], since we may need an
  199. induction hypothesis about [n - 2]. The following lemma gives an
  200. alternative characterization of [evenb (S n)] that works better
  201. with induction: *)
  202.  
  203. Lemma double_negb : ∀ (b1:bool), b1=negb (negb b1).
  204. Proof.
  205. Admitted.
  206.  
  207. Theorem evenb_S : forall n : nat,
  208. evenb n = negb (evenb (S n)).
  209. Proof.
  210. intros.
  211. induction n as [| n' IHn'].
  212. - simpl. reflexivity.
  213. - rewrite -> double_negb with (b1:=evenb (S n')).
  214. rewrite <- IHn'. reflexivity.
  215. Qed.
  216. (** [] *)
  217.  
  218. (** **** Exercise: 1 starM (destruct_induction) *)
  219. (** Briefly explain the difference between the tactics [destruct]
  220. and [induction].
  221.  
  222. (* FILL IN HERE *)
  223. *)
  224. (** [] *)
  225.  
  226. (* ################################################################# *)
  227. (** * Proofs Within Proofs *)
  228.  
  229. (** In Coq, as in informal mathematics, large proofs are often
  230. broken into a sequence of theorems, with later proofs referring to
  231. earlier theorems. But sometimes a proof will require some
  232. miscellaneous fact that is too trivial and of too little general
  233. interest to bother giving it its own top-level name. In such
  234. cases, it is convenient to be able to simply state and prove the
  235. needed "sub-theorem" right at the point where it is used. The
  236. [assert] tactic allows us to do this. For example, our earlier
  237. proof of the [mult_0_plus] theorem referred to a previous theorem
  238. named [plus_O_n]. We could instead use [assert] to state and
  239. prove [plus_O_n] in-line: *)
  240.  
  241. Theorem mult_0_plus' : forall n m : nat,
  242. (0 + n) * m = n * m.
  243. Proof.
  244. intros n m.
  245. assert (H: 0 + n = n). { reflexivity. }
  246. rewrite -> H.
  247. reflexivity. Qed.
  248.  
  249. (** The [assert] tactic introduces two sub-goals. The first is
  250. the assertion itself; by prefixing it with [H:] we name the
  251. assertion [H]. (We can also name the assertion with [as] just as
  252. we did above with [destruct] and [induction], i.e., [assert (0 + n
  253. = n) as H].) Note that we surround the proof of this assertion
  254. with curly braces [{ ... }], both for readability and so that,
  255. when using Coq interactively, we can see more easily when we have
  256. finished this sub-proof. The second goal is the same as the one
  257. at the point where we invoke [assert] except that, in the context,
  258. we now have the assumption [H] that [0 + n = n]. That is,
  259. [assert] generates one subgoal where we must prove the asserted
  260. fact and a second subgoal where we can use the asserted fact to
  261. make progress on whatever we were trying to prove in the first
  262. place. *)
  263.  
  264. (** Another example of [assert]... *)
  265.  
  266. (** For example, suppose we want to prove that [(n + m) + (p + q)
  267. = (m + n) + (p + q)]. The only difference between the two sides of
  268. the [=] is that the arguments [m] and [n] to the first inner [+]
  269. are swapped, so it seems we should be able to use the
  270. commutativity of addition ([plus_comm]) to rewrite one into the
  271. other. However, the [rewrite] tactic is not very smart about
  272. _where_ it applies the rewrite. There are three uses of [+] here,
  273. and it turns out that doing [rewrite -> plus_comm] will affect
  274. only the _outer_ one... *)
  275.  
  276. Theorem plus_rearrange_firsttry : forall n m p q : nat,
  277. (n + m) + (p + q) = (m + n) + (p + q).
  278. Proof.
  279. intros n m p q.
  280. (* We just need to swap (n + m) for (m + n)... seems
  281. like plus_comm should do the trick! *)
  282. rewrite -> plus_comm.
  283. (* Doesn't work...Coq rewrote the wrong plus! *)
  284. Abort.
  285.  
  286. (** To use [plus_comm] at the point where we need it, we can introduce
  287. a local lemma stating that [n + m = m + n] (for the particular [m]
  288. and [n] that we are talking about here), prove this lemma using
  289. [plus_comm], and then use it to do the desired rewrite. *)
  290.  
  291. Theorem plus_rearrange : forall n m p q : nat,
  292. (n + m) + (p + q) = (m + n) + (p + q).
  293. Proof.
  294. intros n m p q.
  295. assert (H: n + m = m + n).
  296. { rewrite -> plus_comm. reflexivity. }
  297. rewrite -> H. reflexivity. Qed.
  298.  
  299. (* ################################################################# *)
  300. (** * Formal vs. Informal Proof *)
  301.  
  302. (** "_Informal proofs are algorithms; formal proofs are code_." *)
  303.  
  304. (** What constitutes a successful proof of a mathematical claim?
  305. The question has challenged philosophers for millennia, but a
  306. rough and ready definition could be this: A proof of a
  307. mathematical proposition [P] is a written (or spoken) text that
  308. instills in the reader or hearer the certainty that [P] is true --
  309. an unassailable argument for the truth of [P]. That is, a proof
  310. is an act of communication.
  311.  
  312. Acts of communication may involve different sorts of readers. On
  313. one hand, the "reader" can be a program like Coq, in which case
  314. the "belief" that is instilled is that [P] can be mechanically
  315. derived from a certain set of formal logical rules, and the proof
  316. is a recipe that guides the program in checking this fact. Such
  317. recipes are _formal_ proofs.
  318.  
  319. Alternatively, the reader can be a human being, in which case the
  320. proof will be written in English or some other natural language,
  321. and will thus necessarily be _informal_. Here, the criteria for
  322. success are less clearly specified. A "valid" proof is one that
  323. makes the reader believe [P]. But the same proof may be read by
  324. many different readers, some of whom may be convinced by a
  325. particular way of phrasing the argument, while others may not be.
  326. Some readers may be particularly pedantic, inexperienced, or just
  327. plain thick-headed; the only way to convince them will be to make
  328. the argument in painstaking detail. But other readers, more
  329. familiar in the area, may find all this detail so overwhelming
  330. that they lose the overall thread; all they want is to be told the
  331. main ideas, since it is easier for them to fill in the details for
  332. themselves than to wade through a written presentation of them.
  333. Ultimately, there is no universal standard, because there is no
  334. single way of writing an informal proof that is guaranteed to
  335. convince every conceivable reader.
  336.  
  337. In practice, however, mathematicians have developed a rich set of
  338. conventions and idioms for writing about complex mathematical
  339. objects that -- at least within a certain community -- make
  340. communication fairly reliable. The conventions of this stylized
  341. form of communication give a fairly clear standard for judging
  342. proofs good or bad.
  343.  
  344. Because we are using Coq in this course, we will be working
  345. heavily with formal proofs. But this doesn't mean we can
  346. completely forget about informal ones! Formal proofs are useful
  347. in many ways, but they are _not_ very efficient ways of
  348. communicating ideas between human beings. *)
  349.  
  350. (** For example, here is a proof that addition is associative: *)
  351.  
  352. Theorem plus_assoc' : forall n m p : nat,
  353. n + (m + p) = (n + m) + p.
  354. Proof. intros n m p. induction n as [| n' IHn']. reflexivity.
  355. simpl. rewrite -> IHn'. reflexivity. Qed.
  356.  
  357. (** Coq is perfectly happy with this. For a human, however, it
  358. is difficult to make much sense of it. We can use comments and
  359. bullets to show the structure a little more clearly... *)
  360.  
  361. Theorem plus_assoc'' : forall n m p : nat,
  362. n + (m + p) = (n + m) + p.
  363. Proof.
  364. intros n m p. induction n as [| n' IHn'].
  365. - (* n = 0 *)
  366. reflexivity.
  367. - (* n = S n' *)
  368. simpl. rewrite -> IHn'. reflexivity. Qed.
  369.  
  370. (** ... and if you're used to Coq you may be able to step
  371. through the tactics one after the other in your mind and imagine
  372. the state of the context and goal stack at each point, but if the
  373. proof were even a little bit more complicated this would be next
  374. to impossible.
  375.  
  376. A (pedantic) mathematician might write the proof something like
  377. this: *)
  378.  
  379. (** - _Theorem_: For any [n], [m] and [p],
  380.  
  381. n + (m + p) = (n + m) + p.
  382.  
  383. _Proof_: By induction on [n].
  384.  
  385. - First, suppose [n = 0]. We must show
  386.  
  387. 0 + (m + p) = (0 + m) + p.
  388.  
  389. This follows directly from the definition of [+].
  390.  
  391. - Next, suppose [n = S n'], where
  392.  
  393. n' + (m + p) = (n' + m) + p.
  394.  
  395. We must show
  396.  
  397. (S n') + (m + p) = ((S n') + m) + p.
  398.  
  399. By the definition of [+], this follows from
  400.  
  401. S (n' + (m + p)) = S ((n' + m) + p),
  402.  
  403. which is immediate from the induction hypothesis. _Qed_. *)
  404.  
  405. (** The overall form of the proof is basically similar, and of
  406. course this is no accident: Coq has been designed so that its
  407. [induction] tactic generates the same sub-goals, in the same
  408. order, as the bullet points that a mathematician would write. But
  409. there are significant differences of detail: the formal proof is
  410. much more explicit in some ways (e.g., the use of [reflexivity])
  411. but much less explicit in others (in particular, the "proof state"
  412. at any given point in the Coq proof is completely implicit,
  413. whereas the informal proof reminds the reader several times where
  414. things stand). *)
  415.  
  416. (** **** Exercise: 2 stars, advanced, recommendedM (plus_comm_informal) *)
  417. (** Translate your solution for [plus_comm] into an informal proof:
  418.  
  419. Theorem: Addition is commutative.
  420.  
  421. Proof: (* FILL IN HERE *)
  422. *)
  423. (** [] *)
  424.  
  425. (** **** Exercise: 2 stars, optionalM (beq_nat_refl_informal) *)
  426. (** Write an informal proof of the following theorem, using the
  427. informal proof of [plus_assoc] as a model. Don't just
  428. paraphrase the Coq tactics into English!
  429.  
  430. Theorem: [true = beq_nat n n] for any [n].
  431.  
  432. Proof: (* FILL IN HERE *)
  433. [] *)
  434.  
  435. (* ################################################################# *)
  436. (** * More Exercises *)
  437.  
  438. (** **** Exercise: 3 stars, recommended (mult_comm) *)
  439. (** Use [assert] to help prove this theorem. You shouldn't need to
  440. use induction on [plus_swap]. *)
  441.  
  442. Theorem plus_swap : forall n m p : nat,
  443. n + (m + p) = m + (n + p).
  444. Proof.
  445. intros.
  446. rewrite -> plus_assoc.
  447. assert (H: n + m = m + n).
  448. { rewrite -> plus_comm. reflexivity. }
  449. rewrite -> H.
  450. rewrite <- plus_assoc. reflexivity.
  451. Qed.
  452.  
  453. (** Now prove commutativity of multiplication. (You will probably
  454. need to define and prove a separate subsidiary theorem to be used
  455. in the proof of this one. You may find that [plus_swap] comes in
  456. handy.) *)
  457.  
  458. Lemma mult_s : ∀ n m : nat, n * (S m) = n * m + n.
  459. Proof.
  460. intros.
  461. induction n.
  462. - reflexivity.
  463. - simpl. rewrite -> IHn.
  464. rewrite -> plus_swap.
  465. rewrite -> plus_n_Sm.
  466. rewrite -> plus_n_Sm.
  467. rewrite -> plus_swap.
  468. rewrite -> plus_assoc.
  469. reflexivity.
  470. Qed.
  471.  
  472. Fixpoint leb (n m : nat) : bool :=
  473. match n with
  474. | O => true
  475. | S n' =>
  476. match m with
  477. | O => false
  478. | S m' => leb n' m'
  479. end
  480. end.
  481.  
  482. Theorem mult_comm : forall m n : nat,
  483. m * n = n * m.
  484. Proof.
  485. induction n.
  486. - simpl. rewrite -> mult_0_r. reflexivity.
  487. - simpl.
  488. rewrite -> mult_s.
  489. rewrite -> plus_comm.
  490. rewrite -> IHn.
  491. reflexivity.
  492. Qed.
  493.  
  494. (** [] *)
  495.  
  496. (** **** Exercise: 3 stars, optional (more_exercises) *)
  497. (** Take a piece of paper. For each of the following theorems, first
  498. _think_ about whether (a) it can be proved using only
  499. simplification and rewriting, (b) it also requires case
  500. analysis ([destruct]), or (c) it also requires induction. Write
  501. down your prediction. Then fill in the proof. (There is no need
  502. to turn in your piece of paper; this is just to encourage you to
  503. reflect before you hack!) *)
  504.  
  505. Theorem leb_refl : forall n:nat,
  506. true = leb n n.
  507. Proof.
  508. induction n as [|n' IH].
  509. - reflexivity.
  510. - simpl. rewrite <- IH. reflexivity.
  511. Qed.
  512.  
  513. Fixpoint beq_nat (n m : nat) : bool :=
  514. match n with
  515. | O => match m with
  516. | O => true
  517. | S m' => false
  518. end
  519. | S n' => match m with
  520. | O => false
  521. | S m' => beq_nat n' m'
  522. end
  523. end.
  524.  
  525. Theorem zero_nbeq_S : forall n:nat,
  526. beq_nat 0 (S n) = false.
  527. Proof.
  528. intros. simpl. reflexivity. Qed.
  529.  
  530. Theorem andb_false_r : forall b : bool,
  531. andb b false = false.
  532. Proof.
  533. intros.
  534. destruct b; reflexivity.
  535. Qed.
  536.  
  537. Theorem plus_ble_compat_l : forall n m p : nat,
  538. leb n m = true -> leb (p + n) (p + m) = true.
  539. Proof.
  540. intros.
  541. induction p.
  542. - simpl. apply H.
  543. - simpl. apply IHp.
  544. Qed.
  545.  
  546. Theorem S_nbeq_0 : forall n:nat,
  547. beq_nat (S n) 0 = false.
  548. Proof.
  549. intros.
  550. reflexivity.
  551. Qed.
  552.  
  553. Theorem mult_1_l : forall n:nat, 1 * n = n.
  554. Proof.
  555. intros.
  556. simpl.
  557. rewrite -> plus_n_O.
  558. reflexivity.
  559. Qed.
  560.  
  561. Theorem all3_spec : forall b c : bool,
  562. orb
  563. (andb b c)
  564. (orb (negb b)
  565. (negb c))
  566. = true.
  567. Proof.
  568. intros.
  569. destruct b; simpl.
  570. - destruct c; reflexivity.
  571. - reflexivity.
  572. Qed.
  573.  
  574. Theorem mult_plus_distr_r : forall n m p : nat,
  575. (n + m) * p = (n * p) + (m * p).
  576. Proof.
  577. intros.
  578. induction n.
  579. - simpl. reflexivity.
  580. - simpl. rewrite <- plus_assoc.
  581. rewrite -> IHn. reflexivity. Qed.
  582.  
  583. Theorem mult_assoc : forall n m p : nat,
  584. n * (m * p) = (n * m) * p.
  585. Proof.
  586. intros.
  587. induction n.
  588. - reflexivity.
  589. - simpl. rewrite -> mult_plus_distr_r.
  590. rewrite -> IHn. reflexivity. Qed.
  591. (** [] *)
  592.  
  593. (** **** Exercise: 2 stars, optional (beq_nat_refl) *)
  594. (** Prove the following theorem. (Putting the [true] on the left-hand
  595. side of the equality may look odd, but this is how the theorem is
  596. stated in the Coq standard library, so we follow suit. Rewriting
  597. works equally well in either direction, so we will have no problem
  598. using the theorem no matter which way we state it.) *)
  599.  
  600. Theorem beq_nat_refl : forall n : nat,
  601. true = beq_nat n n.
  602. Proof.
  603. intros.
  604. induction n as [| n' IH].
  605. - reflexivity.
  606. - simpl. apply IH.
  607. Qed.
  608. (** [] *)
  609.  
  610. (** **** Exercise: 2 stars, optional (plus_swap') *)
  611. (** The [replace] tactic allows you to specify a particular subterm to
  612. rewrite and what you want it rewritten to: [replace (t) with (u)]
  613. replaces (all copies of) expression [t] in the goal by expression
  614. [u], and generates [t = u] as an additional subgoal. This is often
  615. useful when a plain [rewrite] acts on the wrong part of the goal.
  616.  
  617. Use the [replace] tactic to do a proof of [plus_swap'], just like
  618. [plus_swap] but without needing [assert (n + m = m + n)]. *)
  619.  
  620. Theorem plus_swap' : forall n m p : nat,
  621. n + (m + p) = m + (n + p).
  622. Proof.
  623. intros.
  624. induction n as [| n' IH].
  625. - reflexivity.
  626. - simpl. rewrite -> IH.
  627. rewrite -> plus_n_Sm.
  628. reflexivity.
  629. Qed.
  630. (** [] *)
  631.  
  632. (** **** Exercise: 3 stars, recommendedM (binary_commute) *)
  633. (** Recall the [incr] and [bin_to_nat] functions that you
  634. wrote for the [binary] exercise in the [Basics] chapter. Prove
  635. that the following diagram commutes:
  636.  
  637. incr
  638. bin ----------------------> bin
  639. | |
  640. bin_to_nat | | bin_to_nat
  641. | |
  642. v v
  643. nat ----------------------> nat
  644. S
  645.  
  646. That is, incrementing a binary number and then converting it to
  647. a (unary) natural number yields the same result as first converting
  648. it to a natural number and then incrementing.
  649. Name your theorem [bin_to_nat_pres_incr] ("pres" for "preserves").
  650.  
  651. Before you start working on this exercise, copy the definitions
  652. from your solution to the [binary] exercise here so that this file
  653. can be graded on its own. If you want to change your original
  654. definitions to make the property easier to prove, feel free to
  655. do so! *)
  656.  
  657. Lemma plus_1_s : ∀ n : nat, n + 1 = S n.
  658. Proof.
  659. induction n as [| n' IH].
  660. - reflexivity.
  661. - simpl. rewrite -> IH. reflexivity. Qed.
  662.  
  663. Inductive bin :=
  664. | OO : bin
  665. | D : bin → bin
  666. | SD : bin → bin.
  667.  
  668. Fixpoint incr (b:bin) : bin :=
  669. match b with
  670. | OO => SD OO
  671. | D b' => SD b'
  672. | SD b' => D (incr b')
  673. end.
  674.  
  675. Fixpoint bin_to_nat (b:bin) : nat :=
  676. match b with
  677. | OO => O
  678. | D b' => (bin_to_nat b') * 2
  679. | SD b' => (bin_to_nat b') * 2 + 1
  680. end.
  681.  
  682. Theorem binary_commute : ∀ b : bin, bin_to_nat(incr b)=S (bin_to_nat b).
  683. Proof.
  684. induction b as [| b' IH | b' IH].
  685. - reflexivity.
  686. - simpl. rewrite -> plus_1_s. reflexivity.
  687. - simpl. rewrite -> IH.
  688. rewrite -> plus_1_s.
  689. simpl. reflexivity.
  690. Qed.
  691.  
  692. (** [] *)
  693.  
  694. (** **** Exercise: 5 stars, advancedM (binary_inverse) *)
  695. (** This exercise is a continuation of the previous exercise about
  696. binary numbers. You will need your definitions and theorems from
  697. there to complete this one; please copy them to this file to make
  698. it self contained for grading.
  699.  
  700. (a) First, write a function to convert natural numbers to binary
  701. numbers. Then prove that starting with any natural number,
  702. converting to binary, then converting back yields the same
  703. natural number you started with.
  704.  
  705. (b) You might naturally think that we should also prove the
  706. opposite direction: that starting with a binary number,
  707. converting to a natural, and then back to binary yields the
  708. same number we started with. However, this is not true!
  709. Explain what the problem is.
  710.  
  711. (c) Define a "direct" normalization function -- i.e., a function
  712. [normalize] from binary numbers to binary numbers such that,
  713. for any binary number b, converting to a natural and then back
  714. to binary yields [(normalize b)]. Prove it. (Warning: This
  715. part is tricky!)
  716.  
  717. Again, feel free to change your earlier definitions if this helps
  718. here. *)
  719.  
  720. Fixpoint nat_to_bin (n:nat) : bin :=
  721. match n with
  722. | O => OO
  723. | S n' => incr (nat_to_bin n')
  724. end.
  725.  
  726. Theorem nat_bin : ∀ n : nat, bin_to_nat(nat_to_bin n) = n.
  727. Proof.
  728. induction n as [| n' IH].
  729. - reflexivity.
  730. - simpl. rewrite -> binary_commute.
  731. rewrite -> IH. reflexivity. Qed.
  732.  
  733. Fixpoint twice (b:bin) : bin :=
  734. match b with
  735. | OO => OO
  736. | b' => D b'
  737. end.
  738.  
  739. Fixpoint bin_norm (b:bin) : bin :=
  740. match b with
  741. | OO => OO
  742. | D b' => twice (bin_norm b')
  743. | SD b' => incr (twice (bin_norm b'))
  744. end.
  745.  
  746. Lemma incr_twice : ∀ b:bin, incr(incr(twice b))=twice(incr b).
  747. intro.
  748. destruct b; reflexivity.
  749. Qed.
  750.  
  751. Lemma norm_incr : ∀ b:bin, incr(bin_norm b)=bin_norm(incr b).
  752. Proof.
  753. induction b.
  754. - reflexivity.
  755. - reflexivity.
  756. - simpl. rewrite -> incr_twice.
  757. rewrite -> IHb. reflexivity.
  758. Qed.
  759.  
  760. Lemma mul_2 : ∀ n, nat_to_bin (n * 2) = twice (nat_to_bin n).
  761. Proof.
  762. induction n as [| n' IH].
  763. - reflexivity.
  764. - simpl. rewrite -> IH.
  765. rewrite -> incr_twice.
  766. reflexivity.
  767. Qed.
  768.  
  769. Theorem bin_nat : ∀ b:bin, nat_to_bin(bin_to_nat b) = bin_norm b.
  770. Proof.
  771. induction b as [| b' IH| b' IH].
  772. - reflexivity.
  773. - simpl. rewrite <- IH.
  774. rewrite <- mul_2. reflexivity.
  775. - simpl. rewrite <- IH.
  776. rewrite <- mul_2.
  777. rewrite -> plus_1_s.
  778. simpl. reflexivity.
  779. Qed.
  780.  
  781.  
  782. (** [] *)
  783.  
  784. (** $Date: 2016-10-07 14:01:19 -0400 (Fri, 07 Oct 2016) $ *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement