doc-src/TutorialI/Inductive/Even.thy
author haftmann
Fri, 17 Jun 2005 16:12:49 +0200
changeset 16417 9bc16273c2d4
parent 12328 7c4ec77a8715
child 23733 3f8ad7418e55
permissions -rw-r--r--
migrated theory headers to new format
     1 (* ID:         $Id$ *)
     2 theory Even imports Main begin
     3 
     4 
     5 consts even :: "nat set"
     6 inductive even
     7 intros
     8 zero[intro!]: "0 \<in> even"
     9 step[intro!]: "n \<in> even \<Longrightarrow> (Suc (Suc n)) \<in> even"
    10 
    11 text{*An inductive definition consists of introduction rules. 
    12 
    13 @{thm[display] even.step[no_vars]}
    14 \rulename{even.step}
    15 
    16 @{thm[display] even.induct[no_vars]}
    17 \rulename{even.induct}
    18 
    19 Attributes can be given to the introduction rules.  Here both rules are
    20 specified as \isa{intro!}
    21 
    22 Our first lemma states that numbers of the form $2\times k$ are even. *}
    23 lemma two_times_even[intro!]: "2*k \<in> even"
    24 apply (induct_tac k)
    25 txt{*
    26 The first step is induction on the natural number \isa{k}, which leaves
    27 two subgoals:
    28 @{subgoals[display,indent=0,margin=65]}
    29 Here \isa{auto} simplifies both subgoals so that they match the introduction
    30 rules, which then are applied automatically.*};
    31  apply auto
    32 done
    33 
    34 text{*Our goal is to prove the equivalence between the traditional definition
    35 of even (using the divides relation) and our inductive definition.  Half of
    36 this equivalence is trivial using the lemma just proved, whose \isa{intro!}
    37 attribute ensures it will be applied automatically.  *}
    38 
    39 lemma dvd_imp_even: "2 dvd n \<Longrightarrow> n \<in> even"
    40 by (auto simp add: dvd_def)
    41 
    42 text{*our first rule induction!*}
    43 lemma even_imp_dvd: "n \<in> even \<Longrightarrow> 2 dvd n"
    44 apply (erule even.induct)
    45 txt{*
    46 @{subgoals[display,indent=0,margin=65]}
    47 *};
    48 apply (simp_all add: dvd_def)
    49 txt{*
    50 @{subgoals[display,indent=0,margin=65]}
    51 *};
    52 apply clarify
    53 txt{*
    54 @{subgoals[display,indent=0,margin=65]}
    55 *};
    56 apply (rule_tac x = "Suc k" in exI, simp)
    57 done
    58 
    59 
    60 text{*no iff-attribute because we don't always want to use it*}
    61 theorem even_iff_dvd: "(n \<in> even) = (2 dvd n)"
    62 by (blast intro: dvd_imp_even even_imp_dvd)
    63 
    64 text{*this result ISN'T inductive...*}
    65 lemma Suc_Suc_even_imp_even: "Suc (Suc n) \<in> even \<Longrightarrow> n \<in> even"
    66 apply (erule even.induct)
    67 txt{*
    68 @{subgoals[display,indent=0,margin=65]}
    69 *};
    70 oops
    71 
    72 text{*...so we need an inductive lemma...*}
    73 lemma even_imp_even_minus_2: "n \<in> even \<Longrightarrow> n - 2 \<in> even"
    74 apply (erule even.induct)
    75 txt{*
    76 @{subgoals[display,indent=0,margin=65]}
    77 *};
    78 apply auto
    79 done
    80 
    81 text{*...and prove it in a separate step*}
    82 lemma Suc_Suc_even_imp_even: "Suc (Suc n) \<in> even \<Longrightarrow> n \<in> even"
    83 by (drule even_imp_even_minus_2, simp)
    84 
    85 
    86 lemma [iff]: "((Suc (Suc n)) \<in> even) = (n \<in> even)"
    87 by (blast dest: Suc_Suc_even_imp_even)
    88 
    89 end
    90