2 theory Even imports Main begin
5 consts even :: "nat set"
8 zero[intro!]: "0 \<in> even"
9 step[intro!]: "n \<in> even \<Longrightarrow> (Suc (Suc n)) \<in> even"
11 text{*An inductive definition consists of introduction rules.
13 @{thm[display] even.step[no_vars]}
16 @{thm[display] even.induct[no_vars]}
17 \rulename{even.induct}
19 Attributes can be given to the introduction rules. Here both rules are
20 specified as \isa{intro!}
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"
26 The first step is induction on the natural number \isa{k}, which leaves
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.*};
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. *}
39 lemma dvd_imp_even: "2 dvd n \<Longrightarrow> n \<in> even"
40 by (auto simp add: dvd_def)
42 text{*our first rule induction!*}
43 lemma even_imp_dvd: "n \<in> even \<Longrightarrow> 2 dvd n"
44 apply (erule even.induct)
46 @{subgoals[display,indent=0,margin=65]}
48 apply (simp_all add: dvd_def)
50 @{subgoals[display,indent=0,margin=65]}
54 @{subgoals[display,indent=0,margin=65]}
56 apply (rule_tac x = "Suc k" in exI, simp)
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)
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)
68 @{subgoals[display,indent=0,margin=65]}
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)
76 @{subgoals[display,indent=0,margin=65]}
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)
86 lemma [iff]: "((Suc (Suc n)) \<in> even) = (n \<in> even)"
87 by (blast dest: Suc_Suc_even_imp_even)