doc-src/TutorialI/Inductive/AB.thy
changeset 10608 620647438780
parent 10520 bb9dfcc87951
child 10884 2995639c6a09
equal deleted inserted replaced
10607:352f6f209775 10608:620647438780
   102 right increases or decreases the difference by 1, we must have passed through
   102 right increases or decreases the difference by 1, we must have passed through
   103 1 on our way from 0 to 2. Formally, we appeal to the following discrete
   103 1 on our way from 0 to 2. Formally, we appeal to the following discrete
   104 intermediate value theorem @{thm[source]nat0_intermed_int_val}
   104 intermediate value theorem @{thm[source]nat0_intermed_int_val}
   105 @{thm[display]nat0_intermed_int_val[no_vars]}
   105 @{thm[display]nat0_intermed_int_val[no_vars]}
   106 where @{term f} is of type @{typ"nat \<Rightarrow> int"}, @{typ int} are the integers,
   106 where @{term f} is of type @{typ"nat \<Rightarrow> int"}, @{typ int} are the integers,
   107 @{term abs} is the absolute value function, and @{term"#1::int"} is the
   107 @{text"\<bar>.\<bar>"} is the absolute value function, and @{term"#1::int"} is the
   108 integer 1 (see \S\ref{sec:numbers}).
   108 integer 1 (see \S\ref{sec:numbers}).
   109 
   109 
   110 First we show that the our specific function, the difference between the
   110 First we show that the our specific function, the difference between the
   111 numbers of @{term a}'s and @{term b}'s, does indeed only change by 1 in every
   111 numbers of @{term a}'s and @{term b}'s, does indeed only change by 1 in every
   112 move to the right. At this point we also start generalizing from @{term a}'s
   112 move to the right. At this point we also start generalizing from @{term a}'s
   114 to prove the desired lemma twice, once as stated above and once with the
   114 to prove the desired lemma twice, once as stated above and once with the
   115 roles of @{term a}'s and @{term b}'s interchanged.
   115 roles of @{term a}'s and @{term b}'s interchanged.
   116 *}
   116 *}
   117 
   117 
   118 lemma step1: "\<forall>i < size w.
   118 lemma step1: "\<forall>i < size w.
   119   abs((int(size[x\<in>take (i+1) w.  P x]) -
   119   \<bar>(int(size[x\<in>take (i+1) w. P x])-int(size[x\<in>take (i+1) w. \<not>P x]))
   120        int(size[x\<in>take (i+1) w. \<not>P x]))
   120    - (int(size[x\<in>take i w. P x])-int(size[x\<in>take i w. \<not>P x]))\<bar> \<le> #1"
   121       -
       
   122       (int(size[x\<in>take i w.  P x]) -
       
   123        int(size[x\<in>take i w. \<not>P x]))) \<le> #1";
       
   124 
   121 
   125 txt{*\noindent
   122 txt{*\noindent
   126 The lemma is a bit hard to read because of the coercion function
   123 The lemma is a bit hard to read because of the coercion function
   127 @{term[source]"int::nat \<Rightarrow> int"}. It is required because @{term size} returns
   124 @{term[source]"int :: nat \<Rightarrow> int"}. It is required because @{term size} returns
   128 a natural number, but @{text-} on @{typ nat} will do the wrong thing.
   125 a natural number, but @{text-} on @{typ nat} will do the wrong thing.
   129 Function @{term take} is predefined and @{term"take i xs"} is the prefix of
   126 Function @{term take} is predefined and @{term"take i xs"} is the prefix of
   130 length @{term i} of @{term xs}; below we als need @{term"drop i xs"}, which
   127 length @{term i} of @{term xs}; below we als need @{term"drop i xs"}, which
   131 is what remains after that prefix has been dropped from @{term xs}.
   128 is what remains after that prefix has been dropped from @{term xs}.
   132 
   129 
   147 lemma part1:
   144 lemma part1:
   148  "size[x\<in>w. P x] = size[x\<in>w. \<not>P x]+2 \<Longrightarrow>
   145  "size[x\<in>w. P x] = size[x\<in>w. \<not>P x]+2 \<Longrightarrow>
   149   \<exists>i\<le>size w. size[x\<in>take i w. P x] = size[x\<in>take i w. \<not>P x]+1";
   146   \<exists>i\<le>size w. size[x\<in>take i w. P x] = size[x\<in>take i w. \<not>P x]+1";
   150 
   147 
   151 txt{*\noindent
   148 txt{*\noindent
   152 This is proved with the help of the intermediate value theorem, instantiated
   149 This is proved by force with the help of the intermediate value theorem,
   153 appropriately and with its first premise disposed of by lemma
   150 instantiated appropriately and with its first premise disposed of by lemma
   154 @{thm[source]step1}.
   151 @{thm[source]step1}:
   155 *}
   152 *}
   156 
   153 
   157 apply(insert nat0_intermed_int_val[OF step1, of "P" "w" "#1"]);
   154 apply(insert nat0_intermed_int_val[OF step1, of "P" "w" "#1"]);
   158 apply simp;
   155 by force;
   159 by(simp del:int_Suc add:zdiff_eq_eq sym[OF int_Suc]);
   156 
   160 
   157 text{*\noindent
   161 text{*\noindent
       
   162 The additional lemmas are needed to mediate between @{typ nat} and @{typ int}.
       
   163 
   158 
   164 Lemma @{thm[source]part1} tells us only about the prefix @{term"take i w"}.
   159 Lemma @{thm[source]part1} tells us only about the prefix @{term"take i w"}.
   165 The suffix @{term"drop i w"} is dealt with in the following easy lemma:
   160 The suffix @{term"drop i w"} is dealt with in the following easy lemma:
   166 *}
   161 *}
   167 
   162