| 58882 |      1 | section \<open>An old chestnut\<close>
 | 
| 8020 |      2 | 
 | 
| 31758 |      3 | theory Puzzle
 | 
| 63583 |      4 |   imports Main
 | 
| 31758 |      5 | begin
 | 
| 8020 |      6 | 
 | 
| 61932 |      7 | text_raw \<open>\<^footnote>\<open>A question from ``Bundeswettbewerb Mathematik''. Original
 | 
|  |      8 |   pen-and-paper proof due to Herbert Ehler; Isabelle tactic script by Tobias
 | 
|  |      9 |   Nipkow.\<close>\<close>
 | 
| 8020 |     10 | 
 | 
| 61932 |     11 | text \<open>
 | 
|  |     12 |   \<^bold>\<open>Problem.\<close> Given some function \<open>f: \<nat> \<rightarrow> \<nat>\<close> such that \<open>f (f n) < f (Suc n)\<close>
 | 
|  |     13 |   for all \<open>n\<close>. Demonstrate that \<open>f\<close> is the identity.
 | 
|  |     14 | \<close>
 | 
| 8020 |     15 | 
 | 
| 18191 |     16 | theorem
 | 
|  |     17 |   assumes f_ax: "\<And>n. f (f n) < f (Suc n)"
 | 
|  |     18 |   shows "f n = n"
 | 
| 10436 |     19 | proof (rule order_antisym)
 | 
| 60410 |     20 |   show ge: "n \<le> f n" for n
 | 
|  |     21 |   proof (induct "f n" arbitrary: n rule: less_induct)
 | 
|  |     22 |     case less
 | 
|  |     23 |     show "n \<le> f n"
 | 
|  |     24 |     proof (cases n)
 | 
|  |     25 |       case (Suc m)
 | 
|  |     26 |       from f_ax have "f (f m) < f n" by (simp only: Suc)
 | 
|  |     27 |       with less have "f m \<le> f (f m)" .
 | 
|  |     28 |       also from f_ax have "\<dots> < f n" by (simp only: Suc)
 | 
|  |     29 |       finally have "f m < f n" .
 | 
|  |     30 |       with less have "m \<le> f m" .
 | 
|  |     31 |       also note \<open>\<dots> < f n\<close>
 | 
|  |     32 |       finally have "m < f n" .
 | 
|  |     33 |       then have "n \<le> f n" by (simp only: Suc)
 | 
|  |     34 |       then show ?thesis .
 | 
|  |     35 |     next
 | 
|  |     36 |       case 0
 | 
|  |     37 |       then show ?thesis by simp
 | 
| 10007 |     38 |     qed
 | 
| 60410 |     39 |   qed
 | 
| 8020 |     40 | 
 | 
| 60410 |     41 |   have mono: "m \<le> n \<Longrightarrow> f m \<le> f n" for m n :: nat
 | 
|  |     42 |   proof (induct n)
 | 
|  |     43 |     case 0
 | 
|  |     44 |     then have "m = 0" by simp
 | 
|  |     45 |     then show ?case by simp
 | 
|  |     46 |   next
 | 
|  |     47 |     case (Suc n)
 | 
|  |     48 |     from Suc.prems show "f m \<le> f (Suc n)"
 | 
|  |     49 |     proof (rule le_SucE)
 | 
|  |     50 |       assume "m \<le> n"
 | 
|  |     51 |       with Suc.hyps have "f m \<le> f n" .
 | 
|  |     52 |       also from ge f_ax have "\<dots> < f (Suc n)"
 | 
|  |     53 |         by (rule le_less_trans)
 | 
|  |     54 |       finally show ?thesis by simp
 | 
| 10436 |     55 |     next
 | 
| 60410 |     56 |       assume "m = Suc n"
 | 
|  |     57 |       then show ?thesis by simp
 | 
| 10007 |     58 |     qed
 | 
| 60410 |     59 |   qed
 | 
| 8020 |     60 | 
 | 
| 18191 |     61 |   show "f n \<le> n"
 | 
| 10436 |     62 |   proof -
 | 
| 18191 |     63 |     have "\<not> n < f n"
 | 
| 10007 |     64 |     proof
 | 
|  |     65 |       assume "n < f n"
 | 
| 18191 |     66 |       then have "Suc n \<le> f n" by simp
 | 
|  |     67 |       then have "f (Suc n) \<le> f (f n)" by (rule mono)
 | 
|  |     68 |       also have "\<dots> < f (Suc n)" by (rule f_ax)
 | 
|  |     69 |       finally have "\<dots> < \<dots>" . then show False ..
 | 
| 10007 |     70 |     qed
 | 
| 18191 |     71 |     then show ?thesis by simp
 | 
| 10007 |     72 |   qed
 | 
|  |     73 | qed
 | 
| 8020 |     74 | 
 | 
| 10007 |     75 | end
 |