src/HOL/Isar_examples/Puzzle.thy
author kleing
Thu, 17 Jan 2002 15:06:36 +0100
changeset 12791 ccc0f45ad2c4
parent 10436 98c421dd5972
child 12997 80dec7322a8c
permissions -rw-r--r--
registered directly executable version with the code generator
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8020
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
     1
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
     2
header {* An old chestnut *}
8020
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
     3
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
     4
theory Puzzle = Main:
8020
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
     5
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
     6
text_raw {*
10436
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
     7
 \footnote{A question from ``Bundeswettbewerb Mathematik''.  Original
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
     8
 pen-and-paper proof due to Herbert Ehler; Isabelle tactic script by
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
     9
 Tobias Nipkow.}
8020
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
    10
10436
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    11
 \medskip \textbf{Problem.}  Given some function $f\colon \Nat \to
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    12
 \Nat$ such that $f \ap (f \ap n) < f \ap (\idt{Suc} \ap n)$ for all
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    13
 $n$.  Demonstrate that $f$ is the identity.
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    14
*}
8020
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
    15
10436
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    16
theorem "(!!n::nat. f (f n) < f (Suc n)) ==> f n = n"
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    17
proof (rule order_antisym)
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    18
  assume f_ax: "!!n. f (f n) < f (Suc n)"
8020
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
    19
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
    20
  txt {*
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
    21
    Note that the generalized form of $n \le f \ap n$ is required
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
    22
    later for monotonicity as well.
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    23
  *}
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    24
  show ge: "!!n. n <= f n"
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    25
  proof -
10436
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    26
    fix k show "!!n. k == f n ==> n <= k" (is "PROP ?P k")
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    27
    proof (induct k
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    28
        rule: nat_less_induct [rule_format])
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    29
      fix k assume hyp: "!!m. m < k ==> PROP ?P m"
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    30
      fix n assume k_def: "k == f n"
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    31
      show "n <= k"
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    32
      proof (cases n)
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    33
        assume "n = 0" thus ?thesis by simp
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    34
      next
10436
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    35
        fix m assume Suc: "n = Suc m"
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    36
        from f_ax have "f (f m) < f (Suc m)" .
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    37
        with hyp k_def Suc have "f m <= f (f m)" by simp
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    38
        also from f_ax have "... < f (Suc m)" .
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    39
        finally have less: "f m < f (Suc m)" .
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    40
        with hyp k_def Suc have "m <= f m" by simp
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    41
        also note less
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    42
        finally have "m < f (Suc m)" .
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    43
        hence "n <= f n" by (simp only: Suc)
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    44
        thus ?thesis by (simp only: k_def)
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    45
      qed
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    46
    qed
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    47
  qed
8020
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
    48
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
    49
  txt {*
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
    50
    In order to show the other direction, we first establish
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
    51
    monotonicity of $f$.
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    52
  *}
10436
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    53
  {
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    54
    fix m n
10436
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    55
    have "m <= n \<Longrightarrow> f m <= f n" (is "PROP ?P n")
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    56
    proof (induct n)
10436
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    57
      assume "m <= 0" hence "m = 0" by simp
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    58
      thus "f m <= f 0" by simp
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    59
    next
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    60
      fix n assume hyp: "PROP ?P n"
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    61
      assume "m <= Suc n"
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    62
      thus "f m <= f (Suc n)"
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    63
      proof (rule le_SucE)
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    64
        assume "m <= n"
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    65
        with hyp have "f m <= f n" .
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    66
        also from ge f_ax have "... < f (Suc n)"
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    67
          by (rule le_less_trans)
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    68
        finally show ?thesis by simp
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    69
      next
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    70
        assume "m = Suc n"
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    71
        thus ?thesis by simp
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    72
      qed
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    73
    qed
10436
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    74
  } note mono = this
8020
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
    75
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    76
  show "f n <= n"
10436
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    77
  proof -
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    78
    have "~ n < f n"
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    79
    proof
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    80
      assume "n < f n"
10436
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    81
      hence "Suc n <= f n" by simp
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    82
      hence "f (Suc n) <= f (f n)" by (rule mono)
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    83
      also have "... < f (Suc n)" by (rule f_ax)
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    84
      finally have "... < ..." . thus False ..
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    85
    qed
10436
98c421dd5972 simplified induction;
wenzelm
parents: 10007
diff changeset
    86
    thus ?thesis by simp
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    87
  qed
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    88
qed
8020
2823ce1753a5 added Isar_examples/Puzzle.thy;
wenzelm
parents:
diff changeset
    89
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9941
diff changeset
    90
end