misc tuning and modernization;
authorwenzelm
Wed Aug 10 22:05:36 2016 +0200 (2016-08-10)
changeset 63654f90e3926e627
parent 63653 4453cfb745e5
child 63655 d31650b377c4
misc tuning and modernization;
src/HOL/Fun_Def.thy
src/HOL/Groups_Big.thy
src/HOL/Num.thy
src/HOL/Parity.thy
src/HOL/Power.thy
     1.1 --- a/src/HOL/Fun_Def.thy	Wed Aug 10 22:05:00 2016 +0200
     1.2 +++ b/src/HOL/Fun_Def.thy	Wed Aug 10 22:05:36 2016 +0200
     1.3 @@ -5,32 +5,29 @@
     1.4  section \<open>Function Definitions and Termination Proofs\<close>
     1.5  
     1.6  theory Fun_Def
     1.7 -imports Basic_BNF_LFPs Partial_Function SAT
     1.8 -keywords
     1.9 -  "function" "termination" :: thy_goal and
    1.10 -  "fun" "fun_cases" :: thy_decl
    1.11 +  imports Basic_BNF_LFPs Partial_Function SAT
    1.12 +  keywords
    1.13 +    "function" "termination" :: thy_goal and
    1.14 +    "fun" "fun_cases" :: thy_decl
    1.15  begin
    1.16  
    1.17  subsection \<open>Definitions with default value\<close>
    1.18  
    1.19 -definition
    1.20 -  THE_default :: "'a \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a" where
    1.21 -  "THE_default d P = (if (\<exists>!x. P x) then (THE x. P x) else d)"
    1.22 +definition THE_default :: "'a \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a"
    1.23 +  where "THE_default d P = (if (\<exists>!x. P x) then (THE x. P x) else d)"
    1.24  
    1.25  lemma THE_defaultI': "\<exists>!x. P x \<Longrightarrow> P (THE_default d P)"
    1.26    by (simp add: theI' THE_default_def)
    1.27  
    1.28 -lemma THE_default1_equality:
    1.29 -    "\<lbrakk>\<exists>!x. P x; P a\<rbrakk> \<Longrightarrow> THE_default d P = a"
    1.30 +lemma THE_default1_equality: "\<exists>!x. P x \<Longrightarrow> P a \<Longrightarrow> THE_default d P = a"
    1.31    by (simp add: the1_equality THE_default_def)
    1.32  
    1.33 -lemma THE_default_none:
    1.34 -    "\<not>(\<exists>!x. P x) \<Longrightarrow> THE_default d P = d"
    1.35 -  by (simp add:THE_default_def)
    1.36 +lemma THE_default_none: "\<not> (\<exists>!x. P x) \<Longrightarrow> THE_default d P = d"
    1.37 +  by (simp add: THE_default_def)
    1.38  
    1.39  
    1.40  lemma fundef_ex1_existence:
    1.41 -  assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    1.42 +  assumes f_def: "f \<equiv> (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    1.43    assumes ex1: "\<exists>!y. G x y"
    1.44    shows "G x (f x)"
    1.45    apply (simp only: f_def)
    1.46 @@ -39,7 +36,7 @@
    1.47    done
    1.48  
    1.49  lemma fundef_ex1_uniqueness:
    1.50 -  assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    1.51 +  assumes f_def: "f \<equiv> (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    1.52    assumes ex1: "\<exists>!y. G x y"
    1.53    assumes elm: "G x (h x)"
    1.54    shows "h x = f x"
    1.55 @@ -50,7 +47,7 @@
    1.56    done
    1.57  
    1.58  lemma fundef_ex1_iff:
    1.59 -  assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    1.60 +  assumes f_def: "f \<equiv> (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    1.61    assumes ex1: "\<exists>!y. G x y"
    1.62    shows "(G x y) = (f x = y)"
    1.63    apply (auto simp:ex1 f_def THE_default1_equality)
    1.64 @@ -59,7 +56,7 @@
    1.65    done
    1.66  
    1.67  lemma fundef_default_value:
    1.68 -  assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    1.69 +  assumes f_def: "f \<equiv> (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    1.70    assumes graph: "\<And>x y. G x y \<Longrightarrow> D x"
    1.71    assumes "\<not> D x"
    1.72    shows "f x = d x"
    1.73 @@ -67,21 +64,17 @@
    1.74    have "\<not>(\<exists>y. G x y)"
    1.75    proof
    1.76      assume "\<exists>y. G x y"
    1.77 -    hence "D x" using graph ..
    1.78 +    then have "D x" using graph ..
    1.79      with \<open>\<not> D x\<close> show False ..
    1.80    qed
    1.81 -  hence "\<not>(\<exists>!y. G x y)" by blast
    1.82 -
    1.83 -  thus ?thesis
    1.84 -    unfolding f_def
    1.85 -    by (rule THE_default_none)
    1.86 +  then have "\<not>(\<exists>!y. G x y)" by blast
    1.87 +  then show ?thesis
    1.88 +    unfolding f_def by (rule THE_default_none)
    1.89  qed
    1.90  
    1.91 -definition in_rel_def[simp]:
    1.92 -  "in_rel R x y == (x, y) \<in> R"
    1.93 +definition in_rel_def[simp]: "in_rel R x y \<equiv> (x, y) \<in> R"
    1.94  
    1.95 -lemma wf_in_rel:
    1.96 -  "wf R \<Longrightarrow> wfP (in_rel R)"
    1.97 +lemma wf_in_rel: "wf R \<Longrightarrow> wfP (in_rel R)"
    1.98    by (simp add: wfP_def)
    1.99  
   1.100  ML_file "Tools/Function/function_core.ML"
   1.101 @@ -112,18 +105,19 @@
   1.102  subsection \<open>Measure functions\<close>
   1.103  
   1.104  inductive is_measure :: "('a \<Rightarrow> nat) \<Rightarrow> bool"
   1.105 -where is_measure_trivial: "is_measure f"
   1.106 +  where is_measure_trivial: "is_measure f"
   1.107  
   1.108  named_theorems measure_function "rules that guide the heuristic generation of measure functions"
   1.109  ML_file "Tools/Function/measure_functions.ML"
   1.110  
   1.111  lemma measure_size[measure_function]: "is_measure size"
   1.112 -by (rule is_measure_trivial)
   1.113 +  by (rule is_measure_trivial)
   1.114  
   1.115  lemma measure_fst[measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (fst p))"
   1.116 -by (rule is_measure_trivial)
   1.117 +  by (rule is_measure_trivial)
   1.118 +
   1.119  lemma measure_snd[measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (snd p))"
   1.120 -by (rule is_measure_trivial)
   1.121 +  by (rule is_measure_trivial)
   1.122  
   1.123  ML_file "Tools/Function/lexicographic_order.ML"
   1.124  
   1.125 @@ -135,8 +129,7 @@
   1.126  
   1.127  subsection \<open>Congruence rules\<close>
   1.128  
   1.129 -lemma let_cong [fundef_cong]:
   1.130 -  "M = N \<Longrightarrow> (\<And>x. x = N \<Longrightarrow> f x = g x) \<Longrightarrow> Let M f = Let N g"
   1.131 +lemma let_cong [fundef_cong]: "M = N \<Longrightarrow> (\<And>x. x = N \<Longrightarrow> f x = g x) \<Longrightarrow> Let M f = Let N g"
   1.132    unfolding Let_def by blast
   1.133  
   1.134  lemmas [fundef_cong] =
   1.135 @@ -144,13 +137,11 @@
   1.136    bex_cong ball_cong imp_cong map_option_cong Option.bind_cong
   1.137  
   1.138  lemma split_cong [fundef_cong]:
   1.139 -  "(\<And>x y. (x, y) = q \<Longrightarrow> f x y = g x y) \<Longrightarrow> p = q
   1.140 -    \<Longrightarrow> case_prod f p = case_prod g q"
   1.141 +  "(\<And>x y. (x, y) = q \<Longrightarrow> f x y = g x y) \<Longrightarrow> p = q \<Longrightarrow> case_prod f p = case_prod g q"
   1.142    by (auto simp: split_def)
   1.143  
   1.144 -lemma comp_cong [fundef_cong]:
   1.145 -  "f (g x) = f' (g' x') \<Longrightarrow> (f o g) x = (f' o g') x'"
   1.146 -  unfolding o_apply .
   1.147 +lemma comp_cong [fundef_cong]: "f (g x) = f' (g' x') \<Longrightarrow> (f \<circ> g) x = (f' \<circ> g') x'"
   1.148 +  by (simp only: o_apply)
   1.149  
   1.150  
   1.151  subsection \<open>Simp rules for termination proofs\<close>
   1.152 @@ -163,31 +154,25 @@
   1.153    less_imp_le_nat[termination_simp]
   1.154    le_imp_less_Suc[termination_simp]
   1.155  
   1.156 -lemma size_prod_simp[termination_simp]:
   1.157 -  "size_prod f g p = f (fst p) + g (snd p) + Suc 0"
   1.158 -by (induct p) auto
   1.159 +lemma size_prod_simp[termination_simp]: "size_prod f g p = f (fst p) + g (snd p) + Suc 0"
   1.160 +  by (induct p) auto
   1.161  
   1.162  
   1.163  subsection \<open>Decomposition\<close>
   1.164  
   1.165 -lemma less_by_empty:
   1.166 -  "A = {} \<Longrightarrow> A \<subseteq> B"
   1.167 -and  union_comp_emptyL:
   1.168 -  "\<lbrakk> A O C = {}; B O C = {} \<rbrakk> \<Longrightarrow> (A \<union> B) O C = {}"
   1.169 -and union_comp_emptyR:
   1.170 -  "\<lbrakk> A O B = {}; A O C = {} \<rbrakk> \<Longrightarrow> A O (B \<union> C) = {}"
   1.171 -and wf_no_loop:
   1.172 -  "R O R = {} \<Longrightarrow> wf R"
   1.173 -by (auto simp add: wf_comp_self[of R])
   1.174 +lemma less_by_empty: "A = {} \<Longrightarrow> A \<subseteq> B"
   1.175 +  and union_comp_emptyL: "A O C = {} \<Longrightarrow> B O C = {} \<Longrightarrow> (A \<union> B) O C = {}"
   1.176 +  and union_comp_emptyR: "A O B = {} \<Longrightarrow> A O C = {} \<Longrightarrow> A O (B \<union> C) = {}"
   1.177 +  and wf_no_loop: "R O R = {} \<Longrightarrow> wf R"
   1.178 +  by (auto simp add: wf_comp_self [of R])
   1.179  
   1.180  
   1.181  subsection \<open>Reduction pairs\<close>
   1.182  
   1.183 -definition
   1.184 -  "reduction_pair P = (wf (fst P) \<and> fst P O snd P \<subseteq> fst P)"
   1.185 +definition "reduction_pair P \<longleftrightarrow> wf (fst P) \<and> fst P O snd P \<subseteq> fst P"
   1.186  
   1.187  lemma reduction_pairI[intro]: "wf R \<Longrightarrow> R O S \<subseteq> R \<Longrightarrow> reduction_pair (R, S)"
   1.188 -unfolding reduction_pair_def by auto
   1.189 +  by (auto simp: reduction_pair_def)
   1.190  
   1.191  lemma reduction_pair_lemma:
   1.192    assumes rp: "reduction_pair P"
   1.193 @@ -204,13 +189,10 @@
   1.194    ultimately show ?thesis by (rule wf_subset)
   1.195  qed
   1.196  
   1.197 -definition
   1.198 -  "rp_inv_image = (\<lambda>(R,S) f. (inv_image R f, inv_image S f))"
   1.199 +definition "rp_inv_image = (\<lambda>(R,S) f. (inv_image R f, inv_image S f))"
   1.200  
   1.201 -lemma rp_inv_image_rp:
   1.202 -  "reduction_pair P \<Longrightarrow> reduction_pair (rp_inv_image P f)"
   1.203 -  unfolding reduction_pair_def rp_inv_image_def split_def
   1.204 -  by force
   1.205 +lemma rp_inv_image_rp: "reduction_pair P \<Longrightarrow> reduction_pair (rp_inv_image P f)"
   1.206 +  unfolding reduction_pair_def rp_inv_image_def split_def by force
   1.207  
   1.208  
   1.209  subsection \<open>Concrete orders for SCNP termination proofs\<close>
   1.210 @@ -230,70 +212,70 @@
   1.211    and pair_leqI2: "a \<le> b \<Longrightarrow> s \<le> t \<Longrightarrow> ((a, s), (b, t)) \<in> pair_leq"
   1.212    and pair_lessI1: "a < b  \<Longrightarrow> ((a, s), (b, t)) \<in> pair_less"
   1.213    and pair_lessI2: "a \<le> b \<Longrightarrow> s < t \<Longrightarrow> ((a, s), (b, t)) \<in> pair_less"
   1.214 -  unfolding pair_leq_def pair_less_def by auto
   1.215 +  by (auto simp: pair_leq_def pair_less_def)
   1.216  
   1.217  text \<open>Introduction rules for max\<close>
   1.218 -lemma smax_emptyI:
   1.219 -  "finite Y \<Longrightarrow> Y \<noteq> {} \<Longrightarrow> ({}, Y) \<in> max_strict"
   1.220 +lemma smax_emptyI: "finite Y \<Longrightarrow> Y \<noteq> {} \<Longrightarrow> ({}, Y) \<in> max_strict"
   1.221    and smax_insertI:
   1.222 -  "\<lbrakk>y \<in> Y; (x, y) \<in> pair_less; (X, Y) \<in> max_strict\<rbrakk> \<Longrightarrow> (insert x X, Y) \<in> max_strict"
   1.223 -  and wmax_emptyI:
   1.224 -  "finite X \<Longrightarrow> ({}, X) \<in> max_weak"
   1.225 +    "y \<in> Y \<Longrightarrow> (x, y) \<in> pair_less \<Longrightarrow> (X, Y) \<in> max_strict \<Longrightarrow> (insert x X, Y) \<in> max_strict"
   1.226 +  and wmax_emptyI: "finite X \<Longrightarrow> ({}, X) \<in> max_weak"
   1.227    and wmax_insertI:
   1.228 -  "\<lbrakk>y \<in> YS; (x, y) \<in> pair_leq; (XS, YS) \<in> max_weak\<rbrakk> \<Longrightarrow> (insert x XS, YS) \<in> max_weak"
   1.229 -unfolding max_strict_def max_weak_def by (auto elim!: max_ext.cases)
   1.230 +    "y \<in> YS \<Longrightarrow> (x, y) \<in> pair_leq \<Longrightarrow> (XS, YS) \<in> max_weak \<Longrightarrow> (insert x XS, YS) \<in> max_weak"
   1.231 +  by (auto simp: max_strict_def max_weak_def elim!: max_ext.cases)
   1.232  
   1.233  text \<open>Introduction rules for min\<close>
   1.234 -lemma smin_emptyI:
   1.235 -  "X \<noteq> {} \<Longrightarrow> (X, {}) \<in> min_strict"
   1.236 +lemma smin_emptyI: "X \<noteq> {} \<Longrightarrow> (X, {}) \<in> min_strict"
   1.237    and smin_insertI:
   1.238 -  "\<lbrakk>x \<in> XS; (x, y) \<in> pair_less; (XS, YS) \<in> min_strict\<rbrakk> \<Longrightarrow> (XS, insert y YS) \<in> min_strict"
   1.239 -  and wmin_emptyI:
   1.240 -  "(X, {}) \<in> min_weak"
   1.241 +    "x \<in> XS \<Longrightarrow> (x, y) \<in> pair_less \<Longrightarrow> (XS, YS) \<in> min_strict \<Longrightarrow> (XS, insert y YS) \<in> min_strict"
   1.242 +  and wmin_emptyI: "(X, {}) \<in> min_weak"
   1.243    and wmin_insertI:
   1.244 -  "\<lbrakk>x \<in> XS; (x, y) \<in> pair_leq; (XS, YS) \<in> min_weak\<rbrakk> \<Longrightarrow> (XS, insert y YS) \<in> min_weak"
   1.245 -by (auto simp: min_strict_def min_weak_def min_ext_def)
   1.246 +    "x \<in> XS \<Longrightarrow> (x, y) \<in> pair_leq \<Longrightarrow> (XS, YS) \<in> min_weak \<Longrightarrow> (XS, insert y YS) \<in> min_weak"
   1.247 +  by (auto simp: min_strict_def min_weak_def min_ext_def)
   1.248  
   1.249 -text \<open>Reduction Pairs\<close>
   1.250 +text \<open>Reduction Pairs.\<close>
   1.251  
   1.252  lemma max_ext_compat:
   1.253    assumes "R O S \<subseteq> R"
   1.254 -  shows "max_ext R O (max_ext S \<union> {({},{})}) \<subseteq> max_ext R"
   1.255 -using assms
   1.256 -apply auto
   1.257 -apply (elim max_ext.cases)
   1.258 -apply rule
   1.259 -apply auto[3]
   1.260 -apply (drule_tac x=xa in meta_spec)
   1.261 -apply simp
   1.262 -apply (erule bexE)
   1.263 -apply (drule_tac x=xb in meta_spec)
   1.264 -by auto
   1.265 +  shows "max_ext R O (max_ext S \<union> {({}, {})}) \<subseteq> max_ext R"
   1.266 +  using assms
   1.267 +  apply auto
   1.268 +  apply (elim max_ext.cases)
   1.269 +  apply rule
   1.270 +     apply auto[3]
   1.271 +  apply (drule_tac x=xa in meta_spec)
   1.272 +  apply simp
   1.273 +  apply (erule bexE)
   1.274 +  apply (drule_tac x=xb in meta_spec)
   1.275 +  apply auto
   1.276 +  done
   1.277  
   1.278  lemma max_rpair_set: "reduction_pair (max_strict, max_weak)"
   1.279    unfolding max_strict_def max_weak_def
   1.280 -apply (intro reduction_pairI max_ext_wf)
   1.281 -apply simp
   1.282 -apply (rule max_ext_compat)
   1.283 -by (auto simp: pair_less_def pair_leq_def)
   1.284 +  apply (intro reduction_pairI max_ext_wf)
   1.285 +   apply simp
   1.286 +  apply (rule max_ext_compat)
   1.287 +  apply (auto simp: pair_less_def pair_leq_def)
   1.288 +  done
   1.289  
   1.290  lemma min_ext_compat:
   1.291    assumes "R O S \<subseteq> R"
   1.292    shows "min_ext R O  (min_ext S \<union> {({},{})}) \<subseteq> min_ext R"
   1.293 -using assms
   1.294 -apply (auto simp: min_ext_def)
   1.295 -apply (drule_tac x=ya in bspec, assumption)
   1.296 -apply (erule bexE)
   1.297 -apply (drule_tac x=xc in bspec)
   1.298 -apply assumption
   1.299 -by auto
   1.300 +  using assms
   1.301 +  apply (auto simp: min_ext_def)
   1.302 +  apply (drule_tac x=ya in bspec, assumption)
   1.303 +  apply (erule bexE)
   1.304 +  apply (drule_tac x=xc in bspec)
   1.305 +   apply assumption
   1.306 +  apply auto
   1.307 +  done
   1.308  
   1.309  lemma min_rpair_set: "reduction_pair (min_strict, min_weak)"
   1.310    unfolding min_strict_def min_weak_def
   1.311 -apply (intro reduction_pairI min_ext_wf)
   1.312 -apply simp
   1.313 -apply (rule min_ext_compat)
   1.314 -by (auto simp: pair_less_def pair_leq_def)
   1.315 +  apply (intro reduction_pairI min_ext_wf)
   1.316 +   apply simp
   1.317 +  apply (rule min_ext_compat)
   1.318 +  apply (auto simp: pair_less_def pair_leq_def)
   1.319 +  done
   1.320  
   1.321  
   1.322  subsection \<open>Tool setup\<close>
     2.1 --- a/src/HOL/Groups_Big.thy	Wed Aug 10 22:05:00 2016 +0200
     2.2 +++ b/src/HOL/Groups_Big.thy	Wed Aug 10 22:05:36 2016 +0200
     2.3 @@ -1,12 +1,14 @@
     2.4  (*  Title:      HOL/Groups_Big.thy
     2.5 -    Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
     2.6 -                with contributions by Jeremy Avigad
     2.7 +    Author:     Tobias Nipkow
     2.8 +    Author:     Lawrence C Paulson
     2.9 +    Author:     Markus Wenzel
    2.10 +    Author:     Jeremy Avigad
    2.11  *)
    2.12  
    2.13  section \<open>Big sum and product over finite (non-empty) sets\<close>
    2.14  
    2.15  theory Groups_Big
    2.16 -imports Finite_Set Power
    2.17 +  imports Finite_Set Power
    2.18  begin
    2.19  
    2.20  subsection \<open>Generic monoid operation over a set\<close>
    2.21 @@ -21,60 +23,53 @@
    2.22    by (fact comp_comp_fun_commute)
    2.23  
    2.24  definition F :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
    2.25 -where
    2.26 -  eq_fold: "F g A = Finite_Set.fold (f \<circ> g) \<^bold>1 A"
    2.27 +  where eq_fold: "F g A = Finite_Set.fold (f \<circ> g) \<^bold>1 A"
    2.28  
    2.29 -lemma infinite [simp]:
    2.30 -  "\<not> finite A \<Longrightarrow> F g A = \<^bold>1"
    2.31 +lemma infinite [simp]: "\<not> finite A \<Longrightarrow> F g A = \<^bold>1"
    2.32    by (simp add: eq_fold)
    2.33  
    2.34 -lemma empty [simp]:
    2.35 -  "F g {} = \<^bold>1"
    2.36 +lemma empty [simp]: "F g {} = \<^bold>1"
    2.37    by (simp add: eq_fold)
    2.38  
    2.39 -lemma insert [simp]:
    2.40 -  assumes "finite A" and "x \<notin> A"
    2.41 -  shows "F g (insert x A) = g x \<^bold>* F g A"
    2.42 -  using assms by (simp add: eq_fold)
    2.43 +lemma insert [simp]: "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> F g (insert x A) = g x \<^bold>* F g A"
    2.44 +  by (simp add: eq_fold)
    2.45  
    2.46  lemma remove:
    2.47    assumes "finite A" and "x \<in> A"
    2.48    shows "F g A = g x \<^bold>* F g (A - {x})"
    2.49  proof -
    2.50 -  from \<open>x \<in> A\<close> obtain B where A: "A = insert x B" and "x \<notin> B"
    2.51 +  from \<open>x \<in> A\<close> obtain B where B: "A = insert x B" and "x \<notin> B"
    2.52      by (auto dest: mk_disjoint_insert)
    2.53 -  moreover from \<open>finite A\<close> A have "finite B" by simp
    2.54 +  moreover from \<open>finite A\<close> B have "finite B" by simp
    2.55    ultimately show ?thesis by simp
    2.56  qed
    2.57  
    2.58 -lemma insert_remove:
    2.59 -  assumes "finite A"
    2.60 -  shows "F g (insert x A) = g x \<^bold>* F g (A - {x})"
    2.61 -  using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
    2.62 +lemma insert_remove: "finite A \<Longrightarrow> F g (insert x A) = g x \<^bold>* F g (A - {x})"
    2.63 +  by (cases "x \<in> A") (simp_all add: remove insert_absorb)
    2.64  
    2.65 -lemma neutral:
    2.66 -  assumes "\<forall>x\<in>A. g x = \<^bold>1"
    2.67 -  shows "F g A = \<^bold>1"
    2.68 -  using assms by (induct A rule: infinite_finite_induct) simp_all
    2.69 +lemma neutral: "\<forall>x\<in>A. g x = \<^bold>1 \<Longrightarrow> F g A = \<^bold>1"
    2.70 +  by (induct A rule: infinite_finite_induct) simp_all
    2.71  
    2.72 -lemma neutral_const [simp]:
    2.73 -  "F (\<lambda>_. \<^bold>1) A = \<^bold>1"
    2.74 +lemma neutral_const [simp]: "F (\<lambda>_. \<^bold>1) A = \<^bold>1"
    2.75    by (simp add: neutral)
    2.76  
    2.77  lemma union_inter:
    2.78    assumes "finite A" and "finite B"
    2.79    shows "F g (A \<union> B) \<^bold>* F g (A \<inter> B) = F g A \<^bold>* F g B"
    2.80    \<comment> \<open>The reversed orientation looks more natural, but LOOPS as a simprule!\<close>
    2.81 -using assms proof (induct A)
    2.82 -  case empty then show ?case by simp
    2.83 +  using assms
    2.84 +proof (induct A)
    2.85 +  case empty
    2.86 +  then show ?case by simp
    2.87  next
    2.88 -  case (insert x A) then show ?case
    2.89 -    by (auto simp add: insert_absorb Int_insert_left commute [of _ "g x"] assoc left_commute)
    2.90 +  case (insert x A)
    2.91 +  then show ?case
    2.92 +    by (auto simp: insert_absorb Int_insert_left commute [of _ "g x"] assoc left_commute)
    2.93  qed
    2.94  
    2.95  corollary union_inter_neutral:
    2.96    assumes "finite A" and "finite B"
    2.97 -  and I0: "\<forall>x \<in> A \<inter> B. g x = \<^bold>1"
    2.98 +    and "\<forall>x \<in> A \<inter> B. g x = \<^bold>1"
    2.99    shows "F g (A \<union> B) = F g A \<^bold>* F g B"
   2.100    using assms by (simp add: union_inter [symmetric] neutral)
   2.101  
   2.102 @@ -90,7 +85,8 @@
   2.103  proof -
   2.104    have "A \<union> B = A - B \<union> (B - A) \<union> A \<inter> B"
   2.105      by auto
   2.106 -  with assms show ?thesis by simp (subst union_disjoint, auto)+
   2.107 +  with assms show ?thesis
   2.108 +    by simp (subst union_disjoint, auto)+
   2.109  qed
   2.110  
   2.111  lemma subset_diff:
   2.112 @@ -116,9 +112,15 @@
   2.113  proof -
   2.114    from assms have "\<exists>a\<in>A. g a \<noteq> \<^bold>1"
   2.115    proof (induct A rule: infinite_finite_induct)
   2.116 +    case infinite
   2.117 +    then show ?case by simp
   2.118 +  next
   2.119 +    case empty
   2.120 +    then show ?case by simp
   2.121 +  next
   2.122      case (insert a A)
   2.123 -    then show ?case by simp (rule, simp)
   2.124 -  qed simp_all
   2.125 +    then show ?case by fastforce
   2.126 +  qed
   2.127    with that show thesis by blast
   2.128  qed
   2.129  
   2.130 @@ -127,9 +129,11 @@
   2.131    shows "F g (h ` A) = F (g \<circ> h) A"
   2.132  proof (cases "finite A")
   2.133    case True
   2.134 -  with assms show ?thesis by (simp add: eq_fold fold_image comp_assoc)
   2.135 +  with assms show ?thesis
   2.136 +    by (simp add: eq_fold fold_image comp_assoc)
   2.137  next
   2.138 -  case False with assms have "\<not> finite (h ` A)" by (blast dest: finite_imageD)
   2.139 +  case False
   2.140 +  with assms have "\<not> finite (h ` A)" by (blast dest: finite_imageD)
   2.141    with False show ?thesis by simp
   2.142  qed
   2.143  
   2.144 @@ -143,7 +147,7 @@
   2.145  lemma strong_cong [cong]:
   2.146    assumes "A = B" "\<And>x. x \<in> B =simp=> g x = h x"
   2.147    shows "F (\<lambda>x. g x) A = F (\<lambda>x. h x) B"
   2.148 -  by (rule cong) (insert assms, simp_all add: simp_implies_def)
   2.149 +  by (rule cong) (use assms in \<open>simp_all add: simp_implies_def\<close>)
   2.150  
   2.151  lemma reindex_cong:
   2.152    assumes "inj_on l B"
   2.153 @@ -154,55 +158,64 @@
   2.154  
   2.155  lemma UNION_disjoint:
   2.156    assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
   2.157 -  and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
   2.158 +    and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
   2.159    shows "F g (UNION I A) = F (\<lambda>x. F g (A x)) I"
   2.160 -apply (insert assms)
   2.161 -apply (induct rule: finite_induct)
   2.162 -apply simp
   2.163 -apply atomize
   2.164 -apply (subgoal_tac "\<forall>i\<in>Fa. x \<noteq> i")
   2.165 - prefer 2 apply blast
   2.166 -apply (subgoal_tac "A x Int UNION Fa A = {}")
   2.167 - prefer 2 apply blast
   2.168 -apply (simp add: union_disjoint)
   2.169 -done
   2.170 +  apply (insert assms)
   2.171 +  apply (induct rule: finite_induct)
   2.172 +   apply simp
   2.173 +  apply atomize
   2.174 +  apply (subgoal_tac "\<forall>i\<in>Fa. x \<noteq> i")
   2.175 +   prefer 2 apply blast
   2.176 +  apply (subgoal_tac "A x \<inter> UNION Fa A = {}")
   2.177 +   prefer 2 apply blast
   2.178 +  apply (simp add: union_disjoint)
   2.179 +  done
   2.180  
   2.181  lemma Union_disjoint:
   2.182    assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A \<inter> B = {}"
   2.183    shows "F g (\<Union>C) = (F \<circ> F) g C"
   2.184 -proof cases
   2.185 -  assume "finite C"
   2.186 -  from UNION_disjoint [OF this assms]
   2.187 -  show ?thesis by simp
   2.188 -qed (auto dest: finite_UnionD intro: infinite)
   2.189 +proof (cases "finite C")
   2.190 +  case True
   2.191 +  from UNION_disjoint [OF this assms] show ?thesis by simp
   2.192 +next
   2.193 +  case False
   2.194 +  then show ?thesis by (auto dest: finite_UnionD intro: infinite)
   2.195 +qed
   2.196  
   2.197 -lemma distrib:
   2.198 -  "F (\<lambda>x. g x \<^bold>* h x) A = F g A \<^bold>* F h A"
   2.199 +lemma distrib: "F (\<lambda>x. g x \<^bold>* h x) A = F g A \<^bold>* F h A"
   2.200    by (induct A rule: infinite_finite_induct) (simp_all add: assoc commute left_commute)
   2.201  
   2.202  lemma Sigma:
   2.203    "finite A \<Longrightarrow> \<forall>x\<in>A. finite (B x) \<Longrightarrow> F (\<lambda>x. F (g x) (B x)) A = F (case_prod g) (SIGMA x:A. B x)"
   2.204 -apply (subst Sigma_def)
   2.205 -apply (subst UNION_disjoint, assumption, simp)
   2.206 - apply blast
   2.207 -apply (rule cong)
   2.208 -apply rule
   2.209 -apply (simp add: fun_eq_iff)
   2.210 -apply (subst UNION_disjoint, simp, simp)
   2.211 - apply blast
   2.212 -apply (simp add: comp_def)
   2.213 -done
   2.214 +  apply (subst Sigma_def)
   2.215 +  apply (subst UNION_disjoint)
   2.216 +     apply assumption
   2.217 +    apply simp
   2.218 +   apply blast
   2.219 +  apply (rule cong)
   2.220 +   apply rule
   2.221 +  apply (simp add: fun_eq_iff)
   2.222 +  apply (subst UNION_disjoint)
   2.223 +     apply simp
   2.224 +    apply simp
   2.225 +   apply blast
   2.226 +  apply (simp add: comp_def)
   2.227 +  done
   2.228  
   2.229  lemma related:
   2.230    assumes Re: "R \<^bold>1 \<^bold>1"
   2.231 -  and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 \<^bold>* y1) (x2 \<^bold>* y2)"
   2.232 -  and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
   2.233 +    and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 \<^bold>* y1) (x2 \<^bold>* y2)"
   2.234 +    and fin: "finite S"
   2.235 +    and R_h_g: "\<forall>x\<in>S. R (h x) (g x)"
   2.236    shows "R (F h S) (F g S)"
   2.237 -  using fS by (rule finite_subset_induct) (insert assms, auto)
   2.238 +  using fin by (rule finite_subset_induct) (use assms in auto)
   2.239  
   2.240  lemma mono_neutral_cong_left:
   2.241 -  assumes "finite T" and "S \<subseteq> T" and "\<forall>i \<in> T - S. h i = \<^bold>1"
   2.242 -  and "\<And>x. x \<in> S \<Longrightarrow> g x = h x" shows "F g S = F h T"
   2.243 +  assumes "finite T"
   2.244 +    and "S \<subseteq> T"
   2.245 +    and "\<forall>i \<in> T - S. h i = \<^bold>1"
   2.246 +    and "\<And>x. x \<in> S \<Longrightarrow> g x = h x"
   2.247 +  shows "F g S = F h T"
   2.248  proof-
   2.249    have eq: "T = S \<union> (T - S)" using \<open>S \<subseteq> T\<close> by blast
   2.250    have d: "S \<inter> (T - S) = {}" using \<open>S \<subseteq> T\<close> by blast
   2.251 @@ -213,16 +226,14 @@
   2.252  qed
   2.253  
   2.254  lemma mono_neutral_cong_right:
   2.255 -  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. g i = \<^bold>1; \<And>x. x \<in> S \<Longrightarrow> g x = h x \<rbrakk>
   2.256 -   \<Longrightarrow> F g T = F h S"
   2.257 +  "finite T \<Longrightarrow> S \<subseteq> T \<Longrightarrow> \<forall>i \<in> T - S. g i = \<^bold>1 \<Longrightarrow> (\<And>x. x \<in> S \<Longrightarrow> g x = h x) \<Longrightarrow>
   2.258 +    F g T = F h S"
   2.259    by (auto intro!: mono_neutral_cong_left [symmetric])
   2.260  
   2.261 -lemma mono_neutral_left:
   2.262 -  "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. g i = \<^bold>1 \<rbrakk> \<Longrightarrow> F g S = F g T"
   2.263 +lemma mono_neutral_left: "finite T \<Longrightarrow> S \<subseteq> T \<Longrightarrow> \<forall>i \<in> T - S. g i = \<^bold>1 \<Longrightarrow> F g S = F g T"
   2.264    by (blast intro: mono_neutral_cong_left)
   2.265  
   2.266 -lemma mono_neutral_right:
   2.267 -  "\<lbrakk> finite T;  S \<subseteq> T;  \<forall>i \<in> T - S. g i = \<^bold>1 \<rbrakk> \<Longrightarrow> F g T = F g S"
   2.268 +lemma mono_neutral_right: "finite T \<Longrightarrow> S \<subseteq> T \<Longrightarrow> \<forall>i \<in> T - S. g i = \<^bold>1 \<Longrightarrow> F g T = F g S"
   2.269    by (blast intro!: mono_neutral_left [symmetric])
   2.270  
   2.271  lemma reindex_bij_betw: "bij_betw h S T \<Longrightarrow> F (\<lambda>x. g (h x)) S = F g T"
   2.272 @@ -256,10 +267,9 @@
   2.273  proof -
   2.274    have [simp]: "finite S \<longleftrightarrow> finite T"
   2.275      using bij_betw_finite[OF bij] fin by auto
   2.276 -
   2.277    show ?thesis
   2.278 -  proof cases
   2.279 -    assume "finite S"
   2.280 +  proof (cases "finite S")
   2.281 +    case True
   2.282      with nn have "F (\<lambda>x. g (h x)) S = F (\<lambda>x. g (h x)) (S - S')"
   2.283        by (intro mono_neutral_cong_right) auto
   2.284      also have "\<dots> = F g (T - T')"
   2.285 @@ -267,17 +277,20 @@
   2.286      also have "\<dots> = F g T"
   2.287        using nn \<open>finite S\<close> by (intro mono_neutral_cong_left) auto
   2.288      finally show ?thesis .
   2.289 -  qed simp
   2.290 +  next
   2.291 +    case False
   2.292 +    then show ?thesis by simp
   2.293 +  qed
   2.294  qed
   2.295  
   2.296  lemma reindex_nontrivial:
   2.297    assumes "finite A"
   2.298 -  and nz: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<noteq> y \<Longrightarrow> h x = h y \<Longrightarrow> g (h x) = \<^bold>1"
   2.299 +    and nz: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<noteq> y \<Longrightarrow> h x = h y \<Longrightarrow> g (h x) = \<^bold>1"
   2.300    shows "F g (h ` A) = F (g \<circ> h) A"
   2.301  proof (subst reindex_bij_betw_not_neutral [symmetric])
   2.302    show "bij_betw h (A - {x \<in> A. (g \<circ> h) x = \<^bold>1}) (h ` A - h ` {x \<in> A. (g \<circ> h) x = \<^bold>1})"
   2.303      using nz by (auto intro!: inj_onI simp: bij_betw_def)
   2.304 -qed (insert \<open>finite A\<close>, auto)
   2.305 +qed (use \<open>finite A\<close> in auto)
   2.306  
   2.307  lemma reindex_bij_witness_not_neutral:
   2.308    assumes fin: "finite S'" "finite T'"
   2.309 @@ -305,69 +318,66 @@
   2.310  lemma delta:
   2.311    assumes fS: "finite S"
   2.312    shows "F (\<lambda>k. if k = a then b k else \<^bold>1) S = (if a \<in> S then b a else \<^bold>1)"
   2.313 -proof-
   2.314 -  let ?f = "(\<lambda>k. if k=a then b k else \<^bold>1)"
   2.315 -  { assume a: "a \<notin> S"
   2.316 -    hence "\<forall>k\<in>S. ?f k = \<^bold>1" by simp
   2.317 -    hence ?thesis  using a by simp }
   2.318 -  moreover
   2.319 -  { assume a: "a \<in> S"
   2.320 +proof -
   2.321 +  let ?f = "(\<lambda>k. if k = a then b k else \<^bold>1)"
   2.322 +  show ?thesis
   2.323 +  proof (cases "a \<in> S")
   2.324 +    case False
   2.325 +    then have "\<forall>k\<in>S. ?f k = \<^bold>1" by simp
   2.326 +    with False show ?thesis by simp
   2.327 +  next
   2.328 +    case True
   2.329      let ?A = "S - {a}"
   2.330      let ?B = "{a}"
   2.331 -    have eq: "S = ?A \<union> ?B" using a by blast
   2.332 +    from True have eq: "S = ?A \<union> ?B" by blast
   2.333      have dj: "?A \<inter> ?B = {}" by simp
   2.334      from fS have fAB: "finite ?A" "finite ?B" by auto
   2.335      have "F ?f S = F ?f ?A \<^bold>* F ?f ?B"
   2.336 -      using union_disjoint [OF fAB dj, of ?f, unfolded eq [symmetric]]
   2.337 -      by simp
   2.338 -    then have ?thesis using a by simp }
   2.339 -  ultimately show ?thesis by blast
   2.340 +      using union_disjoint [OF fAB dj, of ?f, unfolded eq [symmetric]] by simp
   2.341 +    with True show ?thesis by simp
   2.342 +  qed
   2.343  qed
   2.344  
   2.345  lemma delta':
   2.346 -  assumes fS: "finite S"
   2.347 +  assumes fin: "finite S"
   2.348    shows "F (\<lambda>k. if a = k then b k else \<^bold>1) S = (if a \<in> S then b a else \<^bold>1)"
   2.349 -  using delta [OF fS, of a b, symmetric] by (auto intro: cong)
   2.350 +  using delta [OF fin, of a b, symmetric] by (auto intro: cong)
   2.351  
   2.352  lemma If_cases:
   2.353    fixes P :: "'b \<Rightarrow> bool" and g h :: "'b \<Rightarrow> 'a"
   2.354 -  assumes fA: "finite A"
   2.355 -  shows "F (\<lambda>x. if P x then h x else g x) A =
   2.356 -    F h (A \<inter> {x. P x}) \<^bold>* F g (A \<inter> - {x. P x})"
   2.357 +  assumes fin: "finite A"
   2.358 +  shows "F (\<lambda>x. if P x then h x else g x) A = F h (A \<inter> {x. P x}) \<^bold>* F g (A \<inter> - {x. P x})"
   2.359  proof -
   2.360 -  have a: "A = A \<inter> {x. P x} \<union> A \<inter> -{x. P x}"
   2.361 -          "(A \<inter> {x. P x}) \<inter> (A \<inter> -{x. P x}) = {}"
   2.362 +  have a: "A = A \<inter> {x. P x} \<union> A \<inter> -{x. P x}" "(A \<inter> {x. P x}) \<inter> (A \<inter> -{x. P x}) = {}"
   2.363      by blast+
   2.364 -  from fA
   2.365 -  have f: "finite (A \<inter> {x. P x})" "finite (A \<inter> -{x. P x})" by auto
   2.366 +  from fin have f: "finite (A \<inter> {x. P x})" "finite (A \<inter> -{x. P x})" by auto
   2.367    let ?g = "\<lambda>x. if P x then h x else g x"
   2.368 -  from union_disjoint [OF f a(2), of ?g] a(1)
   2.369 -  show ?thesis
   2.370 +  from union_disjoint [OF f a(2), of ?g] a(1) show ?thesis
   2.371      by (subst (1 2) cong) simp_all
   2.372  qed
   2.373  
   2.374 -lemma cartesian_product:
   2.375 -   "F (\<lambda>x. F (g x) B) A = F (case_prod g) (A \<times> B)"
   2.376 -apply (rule sym)
   2.377 -apply (cases "finite A")
   2.378 - apply (cases "finite B")
   2.379 -  apply (simp add: Sigma)
   2.380 - apply (cases "A={}", simp)
   2.381 - apply simp
   2.382 -apply (auto intro: infinite dest: finite_cartesian_productD2)
   2.383 -apply (cases "B = {}") apply (auto intro: infinite dest: finite_cartesian_productD1)
   2.384 -done
   2.385 +lemma cartesian_product: "F (\<lambda>x. F (g x) B) A = F (case_prod g) (A \<times> B)"
   2.386 +  apply (rule sym)
   2.387 +  apply (cases "finite A")
   2.388 +   apply (cases "finite B")
   2.389 +    apply (simp add: Sigma)
   2.390 +   apply (cases "A = {}")
   2.391 +    apply simp
   2.392 +   apply simp
   2.393 +   apply (auto intro: infinite dest: finite_cartesian_productD2)
   2.394 +  apply (cases "B = {}")
   2.395 +   apply (auto intro: infinite dest: finite_cartesian_productD1)
   2.396 +  done
   2.397  
   2.398  lemma inter_restrict:
   2.399    assumes "finite A"
   2.400    shows "F g (A \<inter> B) = F (\<lambda>x. if x \<in> B then g x else \<^bold>1) A"
   2.401  proof -
   2.402    let ?g = "\<lambda>x. if x \<in> A \<inter> B then g x else \<^bold>1"
   2.403 -  have "\<forall>i\<in>A - A \<inter> B. (if i \<in> A \<inter> B then g i else \<^bold>1) = \<^bold>1"
   2.404 -   by simp
   2.405 +  have "\<forall>i\<in>A - A \<inter> B. (if i \<in> A \<inter> B then g i else \<^bold>1) = \<^bold>1" by simp
   2.406    moreover have "A \<inter> B \<subseteq> A" by blast
   2.407 -  ultimately have "F ?g (A \<inter> B) = F ?g A" using \<open>finite A\<close>
   2.408 -    by (intro mono_neutral_left) auto
   2.409 +  ultimately have "F ?g (A \<inter> B) = F ?g A"
   2.410 +    using \<open>finite A\<close> by (intro mono_neutral_left) auto
   2.411    then show ?thesis by simp
   2.412  qed
   2.413  
   2.414 @@ -377,27 +387,28 @@
   2.415  
   2.416  lemma Union_comp:
   2.417    assumes "\<forall>A \<in> B. finite A"
   2.418 -    and "\<And>A1 A2 x. A1 \<in> B \<Longrightarrow> A2 \<in> B  \<Longrightarrow> A1 \<noteq> A2 \<Longrightarrow> x \<in> A1 \<Longrightarrow> x \<in> A2 \<Longrightarrow> g x = \<^bold>1"
   2.419 +    and "\<And>A1 A2 x. A1 \<in> B \<Longrightarrow> A2 \<in> B \<Longrightarrow> A1 \<noteq> A2 \<Longrightarrow> x \<in> A1 \<Longrightarrow> x \<in> A2 \<Longrightarrow> g x = \<^bold>1"
   2.420    shows "F g (\<Union>B) = (F \<circ> F) g B"
   2.421 -using assms proof (induct B rule: infinite_finite_induct)
   2.422 +  using assms
   2.423 +proof (induct B rule: infinite_finite_induct)
   2.424    case (infinite A)
   2.425    then have "\<not> finite (\<Union>A)" by (blast dest: finite_UnionD)
   2.426    with infinite show ?case by simp
   2.427  next
   2.428 -  case empty then show ?case by simp
   2.429 +  case empty
   2.430 +  then show ?case by simp
   2.431  next
   2.432    case (insert A B)
   2.433    then have "finite A" "finite B" "finite (\<Union>B)" "A \<notin> B"
   2.434      and "\<forall>x\<in>A \<inter> \<Union>B. g x = \<^bold>1"
   2.435 -    and H: "F g (\<Union>B) = (F o F) g B" by auto
   2.436 +    and H: "F g (\<Union>B) = (F \<circ> F) g B" by auto
   2.437    then have "F g (A \<union> \<Union>B) = F g A \<^bold>* F g (\<Union>B)"
   2.438      by (simp add: union_inter_neutral)
   2.439    with \<open>finite B\<close> \<open>A \<notin> B\<close> show ?case
   2.440      by (simp add: H)
   2.441  qed
   2.442  
   2.443 -lemma commute:
   2.444 -  "F (\<lambda>i. F (g i) B) A = F (\<lambda>j. F (\<lambda>i. g i j) A) B"
   2.445 +lemma commute: "F (\<lambda>i. F (g i) B) A = F (\<lambda>j. F (\<lambda>i. g i j) A) B"
   2.446    unfolding cartesian_product
   2.447    by (rule reindex_bij_witness [where i = "\<lambda>(i, j). (j, i)" and j = "\<lambda>(i, j). (j, i)"]) auto
   2.448  
   2.449 @@ -412,13 +423,11 @@
   2.450    shows "F g (A <+> B) = F (g \<circ> Inl) A \<^bold>* F (g \<circ> Inr) B"
   2.451  proof -
   2.452    have "A <+> B = Inl ` A \<union> Inr ` B" by auto
   2.453 -  moreover from fin have "finite (Inl ` A :: ('b + 'c) set)" "finite (Inr ` B :: ('b + 'c) set)"
   2.454 -    by auto
   2.455 -  moreover have "Inl ` A \<inter> Inr ` B = ({} :: ('b + 'c) set)" by auto
   2.456 -  moreover have "inj_on (Inl :: 'b \<Rightarrow> 'b + 'c) A" "inj_on (Inr :: 'c \<Rightarrow> 'b + 'c) B"
   2.457 -    by (auto intro: inj_onI)
   2.458 -  ultimately show ?thesis using fin
   2.459 -    by (simp add: union_disjoint reindex)
   2.460 +  moreover from fin have "finite (Inl ` A)" "finite (Inr ` B)" by auto
   2.461 +  moreover have "Inl ` A \<inter> Inr ` B = {}" by auto
   2.462 +  moreover have "inj_on Inl A" "inj_on Inr B" by (auto intro: inj_onI)
   2.463 +  ultimately show ?thesis
   2.464 +    using fin by (simp add: union_disjoint reindex)
   2.465  qed
   2.466  
   2.467  lemma same_carrier:
   2.468 @@ -427,22 +436,22 @@
   2.469    assumes trivial: "\<And>a. a \<in> C - A \<Longrightarrow> g a = \<^bold>1" "\<And>b. b \<in> C - B \<Longrightarrow> h b = \<^bold>1"
   2.470    shows "F g A = F h B \<longleftrightarrow> F g C = F h C"
   2.471  proof -
   2.472 -  from \<open>finite C\<close> subset have
   2.473 -    "finite A" and "finite B" and "finite (C - A)" and "finite (C - B)"
   2.474 -    by (auto elim: finite_subset)
   2.475 +  have "finite A" and "finite B" and "finite (C - A)" and "finite (C - B)"
   2.476 +    using \<open>finite C\<close> subset by (auto elim: finite_subset)
   2.477    from subset have [simp]: "A - (C - A) = A" by auto
   2.478    from subset have [simp]: "B - (C - B) = B" by auto
   2.479    from subset have "C = A \<union> (C - A)" by auto
   2.480    then have "F g C = F g (A \<union> (C - A))" by simp
   2.481    also have "\<dots> = F g (A - (C - A)) \<^bold>* F g (C - A - A) \<^bold>* F g (A \<inter> (C - A))"
   2.482      using \<open>finite A\<close> \<open>finite (C - A)\<close> by (simp only: union_diff2)
   2.483 -  finally have P: "F g C = F g A" using trivial by simp
   2.484 +  finally have *: "F g C = F g A" using trivial by simp
   2.485    from subset have "C = B \<union> (C - B)" by auto
   2.486    then have "F h C = F h (B \<union> (C - B))" by simp
   2.487    also have "\<dots> = F h (B - (C - B)) \<^bold>* F h (C - B - B) \<^bold>* F h (B \<inter> (C - B))"
   2.488      using \<open>finite B\<close> \<open>finite (C - B)\<close> by (simp only: union_diff2)
   2.489 -  finally have Q: "F h C = F h B" using trivial by simp
   2.490 -  from P Q show ?thesis by simp
   2.491 +  finally have "F h C = F h B"
   2.492 +    using trivial by simp
   2.493 +  with * show ?thesis by simp
   2.494  qed
   2.495  
   2.496  lemma same_carrierI:
   2.497 @@ -462,8 +471,7 @@
   2.498  begin
   2.499  
   2.500  sublocale setsum: comm_monoid_set plus 0
   2.501 -defines
   2.502 -  setsum = setsum.F ..
   2.503 +  defines setsum = setsum.F ..
   2.504  
   2.505  abbreviation Setsum ("\<Sum>_" [1000] 999)
   2.506    where "\<Sum>A \<equiv> setsum (\<lambda>x. x) A"
   2.507 @@ -504,27 +512,28 @@
   2.508  in [(@{const_syntax setsum}, K setsum_tr')] end
   2.509  \<close>
   2.510  
   2.511 -text \<open>TODO generalization candidates\<close>
   2.512 +(* TODO generalization candidates *)
   2.513  
   2.514  lemma (in comm_monoid_add) setsum_image_gen:
   2.515 -  assumes fS: "finite S"
   2.516 +  assumes fin: "finite S"
   2.517    shows "setsum g S = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
   2.518 -proof-
   2.519 -  { fix x assume "x \<in> S" then have "{y. y\<in> f`S \<and> f x = y} = {f x}" by auto }
   2.520 -  hence "setsum g S = setsum (\<lambda>x. setsum (\<lambda>y. g x) {y. y\<in> f`S \<and> f x = y}) S"
   2.521 +proof -
   2.522 +  have "{y. y\<in> f`S \<and> f x = y} = {f x}" if "x \<in> S" for x
   2.523 +    using that by auto
   2.524 +  then have "setsum g S = setsum (\<lambda>x. setsum (\<lambda>y. g x) {y. y\<in> f`S \<and> f x = y}) S"
   2.525      by simp
   2.526    also have "\<dots> = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
   2.527 -    by (rule setsum.commute_restrict [OF fS finite_imageI [OF fS]])
   2.528 +    by (rule setsum.commute_restrict [OF fin finite_imageI [OF fin]])
   2.529    finally show ?thesis .
   2.530  qed
   2.531  
   2.532  
   2.533  subsubsection \<open>Properties in more restricted classes of structures\<close>
   2.534  
   2.535 -lemma setsum_Un: "finite A ==> finite B ==>
   2.536 -  (setsum f (A Un B) :: 'a :: ab_group_add) =
   2.537 -   setsum f A + setsum f B - setsum f (A Int B)"
   2.538 -by (subst setsum.union_inter [symmetric], auto simp add: algebra_simps)
   2.539 +lemma setsum_Un:
   2.540 +  "finite A \<Longrightarrow> finite B \<Longrightarrow> setsum f (A \<union> B) = setsum f A + setsum f B - setsum f (A \<inter> B)"
   2.541 +  for f :: "'b \<Rightarrow> 'a::ab_group_add"
   2.542 +  by (subst setsum.union_inter [symmetric]) (auto simp add: algebra_simps)
   2.543  
   2.544  lemma setsum_Un2:
   2.545    assumes "finite (A \<union> B)"
   2.546 @@ -532,26 +541,30 @@
   2.547  proof -
   2.548    have "A \<union> B = A - B \<union> (B - A) \<union> A \<inter> B"
   2.549      by auto
   2.550 -  with assms show ?thesis by simp (subst setsum.union_disjoint, auto)+
   2.551 +  with assms show ?thesis
   2.552 +    by simp (subst setsum.union_disjoint, auto)+
   2.553  qed
   2.554  
   2.555 -lemma setsum_diff1: "finite A \<Longrightarrow>
   2.556 -  (setsum f (A - {a}) :: ('a::ab_group_add)) =
   2.557 -  (if a:A then setsum f A - f a else setsum f A)"
   2.558 -by (erule finite_induct) (auto simp add: insert_Diff_if)
   2.559 +lemma setsum_diff1:
   2.560 +  fixes f :: "'b \<Rightarrow> 'a::ab_group_add"
   2.561 +  assumes "finite A"
   2.562 +  shows "setsum f (A - {a}) = (if a \<in> A then setsum f A - f a else setsum f A)"
   2.563 +  using assms by induct (auto simp: insert_Diff_if)
   2.564  
   2.565  lemma setsum_diff:
   2.566 -  assumes le: "finite A" "B \<subseteq> A"
   2.567 -  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))"
   2.568 +  fixes f :: "'b \<Rightarrow> 'a::ab_group_add"
   2.569 +  assumes "finite A" "B \<subseteq> A"
   2.570 +  shows "setsum f (A - B) = setsum f A - setsum f B"
   2.571  proof -
   2.572 -  from le have finiteB: "finite B" using finite_subset by auto
   2.573 -  show ?thesis using finiteB le
   2.574 +  from assms(2,1) have "finite B" by (rule finite_subset)
   2.575 +  from this \<open>B \<subseteq> A\<close>
   2.576 +  show ?thesis
   2.577    proof induct
   2.578      case empty
   2.579 -    thus ?case by auto
   2.580 +    thus ?case by simp
   2.581    next
   2.582      case (insert x F)
   2.583 -    thus ?case using le finiteB
   2.584 +    with \<open>finite A\<close> \<open>finite B\<close> show ?case
   2.585        by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)
   2.586    qed
   2.587  qed
   2.588 @@ -561,45 +574,52 @@
   2.589    shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
   2.590  proof (cases "finite K")
   2.591    case True
   2.592 -  thus ?thesis using le
   2.593 +  from this le show ?thesis
   2.594    proof induct
   2.595      case empty
   2.596 -    thus ?case by simp
   2.597 +    then show ?case by simp
   2.598    next
   2.599      case insert
   2.600 -    thus ?case using add_mono by fastforce
   2.601 +    then show ?case using add_mono by fastforce
   2.602    qed
   2.603  next
   2.604 -  case False then show ?thesis by simp
   2.605 +  case False
   2.606 +  then show ?thesis by simp
   2.607  qed
   2.608  
   2.609  lemma (in strict_ordered_comm_monoid_add) setsum_strict_mono:
   2.610 -  assumes "finite A"  "A \<noteq> {}" and "\<And>x. x \<in> A \<Longrightarrow> f x < g x"
   2.611 +  assumes "finite A" "A \<noteq> {}"
   2.612 +    and "\<And>x. x \<in> A \<Longrightarrow> f x < g x"
   2.613    shows "setsum f A < setsum g A"
   2.614    using assms
   2.615  proof (induct rule: finite_ne_induct)
   2.616 -  case singleton thus ?case by simp
   2.617 +  case singleton
   2.618 +  then show ?case by simp
   2.619  next
   2.620 -  case insert thus ?case by (auto simp: add_strict_mono)
   2.621 +  case insert
   2.622 +  then show ?case by (auto simp: add_strict_mono)
   2.623  qed
   2.624  
   2.625  lemma setsum_strict_mono_ex1:
   2.626    fixes f g :: "'i \<Rightarrow> 'a::ordered_cancel_comm_monoid_add"
   2.627 -  assumes "finite A" and "\<forall>x\<in>A. f x \<le> g x" and "\<exists>a\<in>A. f a < g a"
   2.628 +  assumes "finite A"
   2.629 +    and "\<forall>x\<in>A. f x \<le> g x"
   2.630 +    and "\<exists>a\<in>A. f a < g a"
   2.631    shows "setsum f A < setsum g A"
   2.632  proof-
   2.633 -  from assms(3) obtain a where a: "a:A" "f a < g a" by blast
   2.634 -  have "setsum f A = setsum f ((A-{a}) \<union> {a})"
   2.635 -    by(simp add:insert_absorb[OF \<open>a:A\<close>])
   2.636 -  also have "\<dots> = setsum f (A-{a}) + setsum f {a}"
   2.637 +  from assms(3) obtain a where a: "a \<in> A" "f a < g a" by blast
   2.638 +  have "setsum f A = setsum f ((A - {a}) \<union> {a})"
   2.639 +    by(simp add: insert_absorb[OF \<open>a \<in> A\<close>])
   2.640 +  also have "\<dots> = setsum f (A - {a}) + setsum f {a}"
   2.641      using \<open>finite A\<close> by(subst setsum.union_disjoint) auto
   2.642 -  also have "setsum f (A-{a}) \<le> setsum g (A-{a})"
   2.643 -    by(rule setsum_mono)(simp add: assms(2))
   2.644 -  also have "setsum f {a} < setsum g {a}" using a by simp
   2.645 -  also have "setsum g (A - {a}) + setsum g {a} = setsum g((A-{a}) \<union> {a})"
   2.646 -    using \<open>finite A\<close> by(subst setsum.union_disjoint[symmetric]) auto
   2.647 -  also have "\<dots> = setsum g A" by(simp add:insert_absorb[OF \<open>a:A\<close>])
   2.648 -  finally show ?thesis by (auto simp add: add_right_mono add_strict_left_mono)
   2.649 +  also have "setsum f (A - {a}) \<le> setsum g (A - {a})"
   2.650 +    by (rule setsum_mono) (simp add: assms(2))
   2.651 +  also from a have "setsum f {a} < setsum g {a}" by simp
   2.652 +  also have "setsum g (A - {a}) + setsum g {a} = setsum g((A - {a}) \<union> {a})"
   2.653 +    using \<open>finite A\<close> by (subst setsum.union_disjoint[symmetric]) auto
   2.654 +  also have "\<dots> = setsum g A" by (simp add: insert_absorb[OF \<open>a \<in> A\<close>])
   2.655 +  finally show ?thesis
   2.656 +    by (auto simp add: add_right_mono add_strict_left_mono)
   2.657  qed
   2.658  
   2.659  lemma setsum_mono_inv:
   2.660 @@ -609,51 +629,67 @@
   2.661    assumes i: "i \<in> I"
   2.662    assumes I: "finite I"
   2.663    shows "f i = g i"
   2.664 -proof(rule ccontr)
   2.665 -  assume "f i \<noteq> g i"
   2.666 +proof (rule ccontr)
   2.667 +  assume "\<not> ?thesis"
   2.668    with le[OF i] have "f i < g i" by simp
   2.669 -  hence "\<exists>i\<in>I. f i < g i" using i ..
   2.670 -  from setsum_strict_mono_ex1[OF I _ this] le have "setsum f I < setsum g I" by blast
   2.671 +  with i have "\<exists>i\<in>I. f i < g i" ..
   2.672 +  from setsum_strict_mono_ex1[OF I _ this] le have "setsum f I < setsum g I"
   2.673 +    by blast
   2.674    with eq show False by simp
   2.675  qed
   2.676  
   2.677 -lemma setsum_negf: "(\<Sum>x\<in>A. - f x::'a::ab_group_add) = - (\<Sum>x\<in>A. f x)"
   2.678 +lemma setsum_negf: "(\<Sum>x\<in>A. - f x) = - (\<Sum>x\<in>A. f x)"
   2.679 +  for f :: "'b \<Rightarrow> 'a::ab_group_add"
   2.680  proof (cases "finite A")
   2.681 -  case True thus ?thesis by (induct set: finite) auto
   2.682 +  case True
   2.683 +  then show ?thesis by (induct set: finite) auto
   2.684  next
   2.685 -  case False thus ?thesis by simp
   2.686 +  case False
   2.687 +  then show ?thesis by simp
   2.688  qed
   2.689  
   2.690 -lemma setsum_subtractf: "(\<Sum>x\<in>A. f x - g x::'a::ab_group_add) = (\<Sum>x\<in>A. f x) - (\<Sum>x\<in>A. g x)"
   2.691 +lemma setsum_subtractf: "(\<Sum>x\<in>A. f x - g x) = (\<Sum>x\<in>A. f x) - (\<Sum>x\<in>A. g x)"
   2.692 +  for f g :: "'b \<Rightarrow>'a::ab_group_add"
   2.693    using setsum.distrib [of f "- g" A] by (simp add: setsum_negf)
   2.694  
   2.695  lemma setsum_subtractf_nat:
   2.696 -  "(\<And>x. x \<in> A \<Longrightarrow> g x \<le> f x) \<Longrightarrow> (\<Sum>x\<in>A. f x - g x::nat) = (\<Sum>x\<in>A. f x) - (\<Sum>x\<in>A. g x)"
   2.697 -  by (induction A rule: infinite_finite_induct) (auto simp: setsum_mono)
   2.698 +  "(\<And>x. x \<in> A \<Longrightarrow> g x \<le> f x) \<Longrightarrow> (\<Sum>x\<in>A. f x - g x) = (\<Sum>x\<in>A. f x) - (\<Sum>x\<in>A. g x)"
   2.699 +  for f g :: "'a \<Rightarrow> nat"
   2.700 +  by (induct A rule: infinite_finite_induct) (auto simp: setsum_mono)
   2.701  
   2.702 -lemma (in ordered_comm_monoid_add) setsum_nonneg:
   2.703 +context ordered_comm_monoid_add
   2.704 +begin
   2.705 +
   2.706 +lemma setsum_nonneg:
   2.707    assumes nn: "\<forall>x\<in>A. 0 \<le> f x"
   2.708    shows "0 \<le> setsum f A"
   2.709  proof (cases "finite A")
   2.710 -  case True thus ?thesis using nn
   2.711 +  case True
   2.712 +  then show ?thesis
   2.713 +    using nn
   2.714    proof induct
   2.715 -    case empty then show ?case by simp
   2.716 +    case empty
   2.717 +    then show ?case by simp
   2.718    next
   2.719      case (insert x F)
   2.720      then have "0 + 0 \<le> f x + setsum f F" by (blast intro: add_mono)
   2.721      with insert show ?case by simp
   2.722    qed
   2.723  next
   2.724 -  case False thus ?thesis by simp
   2.725 +  case False
   2.726 +  then show ?thesis by simp
   2.727  qed
   2.728  
   2.729 -lemma (in ordered_comm_monoid_add) setsum_nonpos:
   2.730 +lemma setsum_nonpos:
   2.731    assumes np: "\<forall>x\<in>A. f x \<le> 0"
   2.732    shows "setsum f A \<le> 0"
   2.733  proof (cases "finite A")
   2.734 -  case True thus ?thesis using np
   2.735 +  case True
   2.736 +  then show ?thesis
   2.737 +    using np
   2.738    proof induct
   2.739 -    case empty then show ?case by simp
   2.740 +    case empty
   2.741 +    then show ?case by simp
   2.742    next
   2.743      case (insert x F)
   2.744      then have "f x + setsum f F \<le> 0 + 0" by (blast intro: add_mono)
   2.745 @@ -663,232 +699,259 @@
   2.746    case False thus ?thesis by simp
   2.747  qed
   2.748  
   2.749 -lemma (in ordered_comm_monoid_add) setsum_nonneg_eq_0_iff:
   2.750 +lemma setsum_nonneg_eq_0_iff:
   2.751    "finite A \<Longrightarrow> \<forall>x\<in>A. 0 \<le> f x \<Longrightarrow> setsum f A = 0 \<longleftrightarrow> (\<forall>x\<in>A. f x = 0)"
   2.752 -  by (induct set: finite, simp) (simp add: add_nonneg_eq_0_iff setsum_nonneg)
   2.753 +  by (induct set: finite) (simp_all add: add_nonneg_eq_0_iff setsum_nonneg)
   2.754  
   2.755 -lemma (in ordered_comm_monoid_add) setsum_nonneg_0:
   2.756 +lemma setsum_nonneg_0:
   2.757    "finite s \<Longrightarrow> (\<And>i. i \<in> s \<Longrightarrow> f i \<ge> 0) \<Longrightarrow> (\<Sum> i \<in> s. f i) = 0 \<Longrightarrow> i \<in> s \<Longrightarrow> f i = 0"
   2.758    by (simp add: setsum_nonneg_eq_0_iff)
   2.759  
   2.760 -lemma (in ordered_comm_monoid_add) setsum_nonneg_leq_bound:
   2.761 +lemma setsum_nonneg_leq_bound:
   2.762    assumes "finite s" "\<And>i. i \<in> s \<Longrightarrow> f i \<ge> 0" "(\<Sum>i \<in> s. f i) = B" "i \<in> s"
   2.763    shows "f i \<le> B"
   2.764  proof -
   2.765 -  have "f i \<le> f i + (\<Sum>i \<in> s - {i}. f i)"
   2.766 -    using assms by (intro add_increasing2 setsum_nonneg) auto
   2.767 +  from assms have "f i \<le> f i + (\<Sum>i \<in> s - {i}. f i)"
   2.768 +    by (intro add_increasing2 setsum_nonneg) auto
   2.769    also have "\<dots> = B"
   2.770      using setsum.remove[of s i f] assms by simp
   2.771    finally show ?thesis by auto
   2.772  qed
   2.773  
   2.774 -lemma (in ordered_comm_monoid_add) setsum_mono2:
   2.775 -  assumes fin: "finite B" and sub: "A \<subseteq> B" and nn: "\<And>b. b \<in> B-A \<Longrightarrow> 0 \<le> f b"
   2.776 +lemma setsum_mono2:
   2.777 +  assumes fin: "finite B"
   2.778 +    and sub: "A \<subseteq> B"
   2.779 +    and nn: "\<And>b. b \<in> B-A \<Longrightarrow> 0 \<le> f b"
   2.780    shows "setsum f A \<le> setsum f B"
   2.781  proof -
   2.782    have "setsum f A \<le> setsum f A + setsum f (B-A)"
   2.783      by(simp add: add_increasing2[OF setsum_nonneg] nn Ball_def)
   2.784 -  also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
   2.785 -    by (simp add: setsum.union_disjoint del:Un_Diff_cancel)
   2.786 -  also have "A \<union> (B-A) = B" using sub by blast
   2.787 +  also from fin finite_subset[OF sub fin] have "\<dots> = setsum f (A \<union> (B-A))"
   2.788 +    by (simp add: setsum.union_disjoint del: Un_Diff_cancel)
   2.789 +  also from sub have "A \<union> (B-A) = B" by blast
   2.790    finally show ?thesis .
   2.791  qed
   2.792  
   2.793 -lemma (in ordered_comm_monoid_add) setsum_le_included:
   2.794 +lemma setsum_le_included:
   2.795    assumes "finite s" "finite t"
   2.796    and "\<forall>y\<in>t. 0 \<le> g y" "(\<forall>x\<in>s. \<exists>y\<in>t. i y = x \<and> f x \<le> g y)"
   2.797    shows "setsum f s \<le> setsum g t"
   2.798  proof -
   2.799    have "setsum f s \<le> setsum (\<lambda>y. setsum g {x. x\<in>t \<and> i x = y}) s"
   2.800    proof (rule setsum_mono)
   2.801 -    fix y assume "y \<in> s"
   2.802 +    fix y
   2.803 +    assume "y \<in> s"
   2.804      with assms obtain z where z: "z \<in> t" "y = i z" "f y \<le> g z" by auto
   2.805      with assms show "f y \<le> setsum g {x \<in> t. i x = y}" (is "?A y \<le> ?B y")
   2.806        using order_trans[of "?A (i z)" "setsum g {z}" "?B (i z)", intro]
   2.807        by (auto intro!: setsum_mono2)
   2.808    qed
   2.809 -  also have "... \<le> setsum (\<lambda>y. setsum g {x. x\<in>t \<and> i x = y}) (i ` t)"
   2.810 +  also have "\<dots> \<le> setsum (\<lambda>y. setsum g {x. x\<in>t \<and> i x = y}) (i ` t)"
   2.811      using assms(2-4) by (auto intro!: setsum_mono2 setsum_nonneg)
   2.812 -  also have "... \<le> setsum g t"
   2.813 +  also have "\<dots> \<le> setsum g t"
   2.814      using assms by (auto simp: setsum_image_gen[symmetric])
   2.815    finally show ?thesis .
   2.816  qed
   2.817  
   2.818 -lemma (in ordered_comm_monoid_add) setsum_mono3:
   2.819 -  "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> \<forall>x\<in>B - A. 0 \<le> f x \<Longrightarrow> setsum f A \<le> setsum f B"
   2.820 +lemma setsum_mono3: "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> \<forall>x\<in>B - A. 0 \<le> f x \<Longrightarrow> setsum f A \<le> setsum f B"
   2.821    by (rule setsum_mono2) auto
   2.822  
   2.823 +end
   2.824 +
   2.825  lemma (in canonically_ordered_monoid_add) setsum_eq_0_iff [simp]:
   2.826    "finite F \<Longrightarrow> (setsum f F = 0) = (\<forall>a\<in>F. f a = 0)"
   2.827    by (intro ballI setsum_nonneg_eq_0_iff zero_le)
   2.828  
   2.829  lemma setsum_right_distrib:
   2.830 -  fixes f :: "'a => ('b::semiring_0)"
   2.831 -  shows "r * setsum f A = setsum (%n. r * f n) A"
   2.832 +  fixes f :: "'a \<Rightarrow> 'b::semiring_0"
   2.833 +  shows "r * setsum f A = setsum (\<lambda>n. r * f n) A"
   2.834  proof (cases "finite A")
   2.835    case True
   2.836 -  thus ?thesis
   2.837 +  then show ?thesis
   2.838    proof induct
   2.839 -    case empty thus ?case by simp
   2.840 +    case empty
   2.841 +    then show ?case by simp
   2.842    next
   2.843 -    case (insert x A) thus ?case by (simp add: distrib_left)
   2.844 +    case insert
   2.845 +    then show ?case by (simp add: distrib_left)
   2.846    qed
   2.847  next
   2.848 -  case False thus ?thesis by simp
   2.849 +  case False
   2.850 +  then show ?thesis by simp
   2.851  qed
   2.852  
   2.853 -lemma setsum_left_distrib:
   2.854 -  "setsum f A * (r::'a::semiring_0) = (\<Sum>n\<in>A. f n * r)"
   2.855 +lemma setsum_left_distrib: "setsum f A * r = (\<Sum>n\<in>A. f n * r)"
   2.856 +  for r :: "'a::semiring_0"
   2.857  proof (cases "finite A")
   2.858    case True
   2.859    then show ?thesis
   2.860    proof induct
   2.861 -    case empty thus ?case by simp
   2.862 +    case empty
   2.863 +    then show ?case by simp
   2.864    next
   2.865 -    case (insert x A) thus ?case by (simp add: distrib_right)
   2.866 +    case insert
   2.867 +    then show ?case by (simp add: distrib_right)
   2.868 +  qed
   2.869 +next
   2.870 +  case False
   2.871 +  then show ?thesis by simp
   2.872 +qed
   2.873 +
   2.874 +lemma setsum_divide_distrib: "setsum f A / r = (\<Sum>n\<in>A. f n / r)"
   2.875 +  for r :: "'a::field"
   2.876 +proof (cases "finite A")
   2.877 +  case True
   2.878 +  then show ?thesis
   2.879 +  proof induct
   2.880 +    case empty
   2.881 +    then show ?case by simp
   2.882 +  next
   2.883 +    case insert
   2.884 +    then show ?case by (simp add: add_divide_distrib)
   2.885    qed
   2.886  next
   2.887 -  case False thus ?thesis by simp
   2.888 +  case False
   2.889 +  then show ?thesis by simp
   2.890  qed
   2.891  
   2.892 -lemma setsum_divide_distrib:
   2.893 -  "setsum f A / (r::'a::field) = (\<Sum>n\<in>A. f n / r)"
   2.894 +lemma setsum_abs[iff]: "\<bar>setsum f A\<bar> \<le> setsum (\<lambda>i. \<bar>f i\<bar>) A"
   2.895 +  for f :: "'a \<Rightarrow> 'b::ordered_ab_group_add_abs"
   2.896 +proof (cases "finite A")
   2.897 +  case True
   2.898 +  then show ?thesis
   2.899 +  proof induct
   2.900 +    case empty
   2.901 +    then show ?case by simp
   2.902 +  next
   2.903 +    case insert
   2.904 +    then show ?case by (auto intro: abs_triangle_ineq order_trans)
   2.905 +  qed
   2.906 +next
   2.907 +  case False
   2.908 +  then show ?thesis by simp
   2.909 +qed
   2.910 +
   2.911 +lemma setsum_abs_ge_zero[iff]: "0 \<le> setsum (\<lambda>i. \<bar>f i\<bar>) A"
   2.912 +  for f :: "'a \<Rightarrow> 'b::ordered_ab_group_add_abs"
   2.913 +  by (simp add: setsum_nonneg)
   2.914 +
   2.915 +lemma abs_setsum_abs[simp]: "\<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar> = (\<Sum>a\<in>A. \<bar>f a\<bar>)"
   2.916 +  for f :: "'a \<Rightarrow> 'b::ordered_ab_group_add_abs"
   2.917  proof (cases "finite A")
   2.918    case True
   2.919    then show ?thesis
   2.920    proof induct
   2.921 -    case empty thus ?case by simp
   2.922 -  next
   2.923 -    case (insert x A) thus ?case by (simp add: add_divide_distrib)
   2.924 -  qed
   2.925 -next
   2.926 -  case False thus ?thesis by simp
   2.927 -qed
   2.928 -
   2.929 -lemma setsum_abs[iff]:
   2.930 -  fixes f :: "'a => ('b::ordered_ab_group_add_abs)"
   2.931 -  shows "\<bar>setsum f A\<bar> \<le> setsum (%i. \<bar>f i\<bar>) A"
   2.932 -proof (cases "finite A")
   2.933 -  case True
   2.934 -  thus ?thesis
   2.935 -  proof induct
   2.936 -    case empty thus ?case by simp
   2.937 -  next
   2.938 -    case (insert x A)
   2.939 -    thus ?case by (auto intro: abs_triangle_ineq order_trans)
   2.940 -  qed
   2.941 -next
   2.942 -  case False thus ?thesis by simp
   2.943 -qed
   2.944 -
   2.945 -lemma setsum_abs_ge_zero[iff]:
   2.946 -  fixes f :: "'a => ('b::ordered_ab_group_add_abs)"
   2.947 -  shows "0 \<le> setsum (%i. \<bar>f i\<bar>) A"
   2.948 -  by (simp add: setsum_nonneg)
   2.949 -
   2.950 -lemma abs_setsum_abs[simp]:
   2.951 -  fixes f :: "'a => ('b::ordered_ab_group_add_abs)"
   2.952 -  shows "\<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar> = (\<Sum>a\<in>A. \<bar>f a\<bar>)"
   2.953 -proof (cases "finite A")
   2.954 -  case True
   2.955 -  thus ?thesis
   2.956 -  proof induct
   2.957 -    case empty thus ?case by simp
   2.958 +    case empty
   2.959 +    then show ?case by simp
   2.960    next
   2.961      case (insert a A)
   2.962 -    hence "\<bar>\<Sum>a\<in>insert a A. \<bar>f a\<bar>\<bar> = \<bar>\<bar>f a\<bar> + (\<Sum>a\<in>A. \<bar>f a\<bar>)\<bar>" by simp
   2.963 -    also have "\<dots> = \<bar>\<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>\<bar>"  using insert by simp
   2.964 -    also have "\<dots> = \<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>"
   2.965 -      by (simp del: abs_of_nonneg)
   2.966 -    also have "\<dots> = (\<Sum>a\<in>insert a A. \<bar>f a\<bar>)" using insert by simp
   2.967 +    then have "\<bar>\<Sum>a\<in>insert a A. \<bar>f a\<bar>\<bar> = \<bar>\<bar>f a\<bar> + (\<Sum>a\<in>A. \<bar>f a\<bar>)\<bar>" by simp
   2.968 +    also from insert have "\<dots> = \<bar>\<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>\<bar>" by simp
   2.969 +    also have "\<dots> = \<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>" by (simp del: abs_of_nonneg)
   2.970 +    also from insert have "\<dots> = (\<Sum>a\<in>insert a A. \<bar>f a\<bar>)" by simp
   2.971      finally show ?case .
   2.972    qed
   2.973  next
   2.974 -  case False thus ?thesis by simp
   2.975 +  case False
   2.976 +  then show ?thesis by simp
   2.977  qed
   2.978  
   2.979 -lemma setsum_diff1_ring: assumes "finite A" "a \<in> A"
   2.980 -  shows "setsum f (A - {a}) = setsum f A - (f a::'a::ring)"
   2.981 +lemma setsum_diff1_ring:
   2.982 +  fixes f :: "'b \<Rightarrow> 'a::ring"
   2.983 +  assumes "finite A" "a \<in> A"
   2.984 +  shows "setsum f (A - {a}) = setsum f A - (f a)"
   2.985    unfolding setsum.remove [OF assms] by auto
   2.986  
   2.987  lemma setsum_product:
   2.988 -  fixes f :: "'a => ('b::semiring_0)"
   2.989 +  fixes f :: "'a \<Rightarrow> 'b::semiring_0"
   2.990    shows "setsum f A * setsum g B = (\<Sum>i\<in>A. \<Sum>j\<in>B. f i * g j)"
   2.991    by (simp add: setsum_right_distrib setsum_left_distrib) (rule setsum.commute)
   2.992  
   2.993  lemma setsum_mult_setsum_if_inj:
   2.994 -fixes f :: "'a => ('b::semiring_0)"
   2.995 -shows "inj_on (%(a,b). f a * g b) (A \<times> B) ==>
   2.996 -  setsum f A * setsum g B = setsum id {f a * g b|a b. a:A & b:B}"
   2.997 -by(auto simp: setsum_product setsum.cartesian_product
   2.998 -        intro!:  setsum.reindex_cong[symmetric])
   2.999 +  fixes f :: "'a \<Rightarrow> 'b::semiring_0"
  2.1000 +  shows "inj_on (\<lambda>(a, b). f a * g b) (A \<times> B) \<Longrightarrow>
  2.1001 +    setsum f A * setsum g B = setsum id {f a * g b |a b. a \<in> A \<and> b \<in> B}"
  2.1002 +  by(auto simp: setsum_product setsum.cartesian_product intro!: setsum.reindex_cong[symmetric])
  2.1003  
  2.1004 -lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
  2.1005 -apply (case_tac "finite A")
  2.1006 - prefer 2 apply simp
  2.1007 -apply (erule rev_mp)
  2.1008 -apply (erule finite_induct, auto)
  2.1009 -done
  2.1010 +lemma setsum_SucD:
  2.1011 +  assumes "setsum f A = Suc n"
  2.1012 +  shows "\<exists>a\<in>A. 0 < f a"
  2.1013 +proof (cases "finite A")
  2.1014 +  case True
  2.1015 +  from this assms show ?thesis by induct auto
  2.1016 +next
  2.1017 +  case False
  2.1018 +  with assms show ?thesis by simp
  2.1019 +qed
  2.1020  
  2.1021 -lemma setsum_eq_Suc0_iff: "finite A \<Longrightarrow>
  2.1022 -  setsum f A = Suc 0 \<longleftrightarrow> (EX a:A. f a = Suc 0 & (ALL b:A. a\<noteq>b \<longrightarrow> f b = 0))"
  2.1023 -apply(erule finite_induct)
  2.1024 -apply (auto simp add:add_is_1)
  2.1025 -done
  2.1026 +lemma setsum_eq_Suc0_iff:
  2.1027 +  assumes "finite A"
  2.1028 +  shows "setsum f A = Suc 0 \<longleftrightarrow> (\<exists>a\<in>A. f a = Suc 0 \<and> (\<forall>b\<in>A. a \<noteq> b \<longrightarrow> f b = 0))"
  2.1029 +  using assms by induct (auto simp add:add_is_1)
  2.1030  
  2.1031  lemmas setsum_eq_1_iff = setsum_eq_Suc0_iff[simplified One_nat_def[symmetric]]
  2.1032  
  2.1033 -lemma setsum_Un_nat: "finite A ==> finite B ==>
  2.1034 -  (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
  2.1035 +lemma setsum_Un_nat:
  2.1036 +  "finite A \<Longrightarrow> finite B \<Longrightarrow> setsum f (A \<union> B) = setsum f A + setsum f B - setsum f (A \<inter> B)"
  2.1037 +  for f :: "'a \<Rightarrow> nat"
  2.1038    \<comment> \<open>For the natural numbers, we have subtraction.\<close>
  2.1039 -by (subst setsum.union_inter [symmetric], auto simp add: algebra_simps)
  2.1040 +  by (subst setsum.union_inter [symmetric]) (auto simp: algebra_simps)
  2.1041  
  2.1042 -lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
  2.1043 -  (if a:A then setsum f A - f a else setsum f A)"
  2.1044 -apply (case_tac "finite A")
  2.1045 - prefer 2 apply simp
  2.1046 -apply (erule finite_induct)
  2.1047 - apply (auto simp add: insert_Diff_if)
  2.1048 -apply (drule_tac a = a in mk_disjoint_insert, auto)
  2.1049 -done
  2.1050 +lemma setsum_diff1_nat: "setsum f (A - {a}) = (if a \<in> A then setsum f A - f a else setsum f A)"
  2.1051 +  for f :: "'a \<Rightarrow> nat"
  2.1052 +proof (cases "finite A")
  2.1053 +  case True
  2.1054 +  then show ?thesis
  2.1055 +    apply induct
  2.1056 +     apply (auto simp: insert_Diff_if)
  2.1057 +    apply (drule mk_disjoint_insert)
  2.1058 +    apply auto
  2.1059 +    done
  2.1060 +next
  2.1061 +  case False
  2.1062 +  then show ?thesis by simp
  2.1063 +qed
  2.1064  
  2.1065  lemma setsum_diff_nat:
  2.1066 -assumes "finite B" and "B \<subseteq> A"
  2.1067 -shows "(setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
  2.1068 -using assms
  2.1069 +  fixes f :: "'a \<Rightarrow> nat"
  2.1070 +  assumes "finite B" and "B \<subseteq> A"
  2.1071 +  shows "setsum f (A - B) = setsum f A - setsum f B"
  2.1072 +  using assms
  2.1073  proof induct
  2.1074 -  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
  2.1075 +  case empty
  2.1076 +  then show ?case by simp
  2.1077  next
  2.1078 -  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
  2.1079 -    and xFinA: "insert x F \<subseteq> A"
  2.1080 -    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
  2.1081 -  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
  2.1082 -  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
  2.1083 +  case (insert x F)
  2.1084 +  note IH = \<open>F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F\<close>
  2.1085 +  from \<open>x \<notin> F\<close> \<open>insert x F \<subseteq> A\<close> have "x \<in> A - F" by simp
  2.1086 +  then have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
  2.1087      by (simp add: setsum_diff1_nat)
  2.1088 -  from xFinA have "F \<subseteq> A" by simp
  2.1089 +  from \<open>insert x F \<subseteq> A\<close> have "F \<subseteq> A" by simp
  2.1090    with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
  2.1091    with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
  2.1092      by simp
  2.1093 -  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
  2.1094 +  from \<open>x \<notin> F\<close> have "A - insert x F = (A - F) - {x}" by auto
  2.1095    with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
  2.1096      by simp
  2.1097 -  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
  2.1098 +  from \<open>finite F\<close> \<open>x \<notin> F\<close> have "setsum f (insert x F) = setsum f F + f x"
  2.1099 +    by simp
  2.1100    with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
  2.1101      by simp
  2.1102 -  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
  2.1103 +  then show ?case by simp
  2.1104  qed
  2.1105  
  2.1106  lemma setsum_comp_morphism:
  2.1107    assumes "h 0 = 0" and "\<And>x y. h (x + y) = h x + h y"
  2.1108    shows "setsum (h \<circ> g) A = h (setsum g A)"
  2.1109  proof (cases "finite A")
  2.1110 -  case False then show ?thesis by (simp add: assms)
  2.1111 +  case False
  2.1112 +  then show ?thesis by (simp add: assms)
  2.1113  next
  2.1114 -  case True then show ?thesis by (induct A) (simp_all add: assms)
  2.1115 +  case True
  2.1116 +  then show ?thesis by (induct A) (simp_all add: assms)
  2.1117  qed
  2.1118  
  2.1119 -lemma (in comm_semiring_1) dvd_setsum:
  2.1120 -  "(\<And>a. a \<in> A \<Longrightarrow> d dvd f a) \<Longrightarrow> d dvd setsum f A"
  2.1121 +lemma (in comm_semiring_1) dvd_setsum: "(\<And>a. a \<in> A \<Longrightarrow> d dvd f a) \<Longrightarrow> d dvd setsum f A"
  2.1122    by (induct A rule: infinite_finite_induct) simp_all
  2.1123  
  2.1124  lemma (in ordered_comm_monoid_add) setsum_pos:
  2.1125 @@ -908,17 +971,18 @@
  2.1126  
  2.1127  lemma setsum_cong_Suc:
  2.1128    assumes "0 \<notin> A" "\<And>x. Suc x \<in> A \<Longrightarrow> f (Suc x) = g (Suc x)"
  2.1129 -  shows   "setsum f A = setsum g A"
  2.1130 +  shows "setsum f A = setsum g A"
  2.1131  proof (rule setsum.cong)
  2.1132 -  fix x assume "x \<in> A"
  2.1133 -  with assms(1) show "f x = g x" by (cases x) (auto intro!: assms(2))
  2.1134 +  fix x
  2.1135 +  assume "x \<in> A"
  2.1136 +  with assms(1) show "f x = g x"
  2.1137 +    by (cases x) (auto intro!: assms(2))
  2.1138  qed simp_all
  2.1139  
  2.1140  
  2.1141  subsubsection \<open>Cardinality as special case of @{const setsum}\<close>
  2.1142  
  2.1143 -lemma card_eq_setsum:
  2.1144 -  "card A = setsum (\<lambda>x. 1) A"
  2.1145 +lemma card_eq_setsum: "card A = setsum (\<lambda>x. 1) A"
  2.1146  proof -
  2.1147    have "plus \<circ> (\<lambda>_. Suc 0) = (\<lambda>_. Suc)"
  2.1148      by (simp add: fun_eq_iff)
  2.1149 @@ -926,45 +990,53 @@
  2.1150      by (rule arg_cong)
  2.1151    then have "Finite_Set.fold (plus \<circ> (\<lambda>_. Suc 0)) 0 A = Finite_Set.fold (\<lambda>_. Suc) 0 A"
  2.1152      by (blast intro: fun_cong)
  2.1153 -  then show ?thesis by (simp add: card.eq_fold setsum.eq_fold)
  2.1154 +  then show ?thesis
  2.1155 +    by (simp add: card.eq_fold setsum.eq_fold)
  2.1156  qed
  2.1157  
  2.1158 -lemma setsum_constant [simp]:
  2.1159 -  "(\<Sum>x \<in> A. y) = of_nat (card A) * y"
  2.1160 -apply (cases "finite A")
  2.1161 -apply (erule finite_induct)
  2.1162 -apply (auto simp add: algebra_simps)
  2.1163 -done
  2.1164 +lemma setsum_constant [simp]: "(\<Sum>x \<in> A. y) = of_nat (card A) * y"
  2.1165 +proof (cases "finite A")
  2.1166 +  case True
  2.1167 +  then show ?thesis by induct (auto simp: algebra_simps)
  2.1168 +next
  2.1169 +  case False
  2.1170 +  then show ?thesis by simp
  2.1171 +qed
  2.1172  
  2.1173  lemma setsum_Suc: "setsum (\<lambda>x. Suc(f x)) A = setsum f A + card A"
  2.1174 -  using setsum.distrib[of f "\<lambda>_. 1" A]
  2.1175 -  by simp
  2.1176 +  using setsum.distrib[of f "\<lambda>_. 1" A] by simp
  2.1177  
  2.1178  lemma setsum_bounded_above:
  2.1179 -  assumes le: "\<And>i. i\<in>A \<Longrightarrow> f i \<le> (K::'a::{semiring_1, ordered_comm_monoid_add})"
  2.1180 +  fixes K :: "'a::{semiring_1,ordered_comm_monoid_add}"
  2.1181 +  assumes le: "\<And>i. i\<in>A \<Longrightarrow> f i \<le> K"
  2.1182    shows "setsum f A \<le> of_nat (card A) * K"
  2.1183  proof (cases "finite A")
  2.1184    case True
  2.1185 -  thus ?thesis using le setsum_mono[where K=A and g = "%x. K"] by simp
  2.1186 +  then show ?thesis
  2.1187 +    using le setsum_mono[where K=A and g = "\<lambda>x. K"] by simp
  2.1188  next
  2.1189 -  case False thus ?thesis by simp
  2.1190 +  case False
  2.1191 +  then show ?thesis by simp
  2.1192  qed
  2.1193  
  2.1194  lemma setsum_bounded_above_strict:
  2.1195 -  assumes "\<And>i. i\<in>A \<Longrightarrow> f i < (K::'a::{ordered_cancel_comm_monoid_add,semiring_1})"
  2.1196 -          "card A > 0"
  2.1197 +  fixes K :: "'a::{ordered_cancel_comm_monoid_add,semiring_1}"
  2.1198 +  assumes "\<And>i. i\<in>A \<Longrightarrow> f i < K" "card A > 0"
  2.1199    shows "setsum f A < of_nat (card A) * K"
  2.1200 -using assms setsum_strict_mono[where A=A and g = "%x. K"]
  2.1201 -by (simp add: card_gt_0_iff)
  2.1202 +  using assms setsum_strict_mono[where A=A and g = "\<lambda>x. K"]
  2.1203 +  by (simp add: card_gt_0_iff)
  2.1204  
  2.1205  lemma setsum_bounded_below:
  2.1206 -  assumes le: "\<And>i. i\<in>A \<Longrightarrow> (K::'a::{semiring_1, ordered_comm_monoid_add}) \<le> f i"
  2.1207 +  fixes K :: "'a::{semiring_1,ordered_comm_monoid_add}"
  2.1208 +  assumes le: "\<And>i. i\<in>A \<Longrightarrow> K \<le> f i"
  2.1209    shows "of_nat (card A) * K \<le> setsum f A"
  2.1210  proof (cases "finite A")
  2.1211    case True
  2.1212 -  thus ?thesis using le setsum_mono[where K=A and f = "%x. K"] by simp
  2.1213 +  then show ?thesis
  2.1214 +    using le setsum_mono[where K=A and f = "%x. K"] by simp
  2.1215  next
  2.1216 -  case False thus ?thesis by simp
  2.1217 +  case False
  2.1218 +  then show ?thesis by simp
  2.1219  qed
  2.1220  
  2.1221  lemma card_UN_disjoint:
  2.1222 @@ -972,24 +1044,26 @@
  2.1223      and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
  2.1224    shows "card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
  2.1225  proof -
  2.1226 -  have "(\<Sum>i\<in>I. card (A i)) = (\<Sum>i\<in>I. \<Sum>x\<in>A i. 1)" by simp
  2.1227 -  with assms show ?thesis by (simp add: card_eq_setsum setsum.UNION_disjoint del: setsum_constant)
  2.1228 +  have "(\<Sum>i\<in>I. card (A i)) = (\<Sum>i\<in>I. \<Sum>x\<in>A i. 1)"
  2.1229 +    by simp
  2.1230 +  with assms show ?thesis
  2.1231 +    by (simp add: card_eq_setsum setsum.UNION_disjoint del: setsum_constant)
  2.1232  qed
  2.1233  
  2.1234  lemma card_Union_disjoint:
  2.1235 -  "finite C ==> (ALL A:C. finite A) ==>
  2.1236 -   (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {})
  2.1237 -   ==> card (\<Union>C) = setsum card C"
  2.1238 -apply (frule card_UN_disjoint [of C id])
  2.1239 -apply simp_all
  2.1240 -done
  2.1241 +  "finite C \<Longrightarrow> \<forall>A\<in>C. finite A \<Longrightarrow> \<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A \<inter> B = {} \<Longrightarrow>
  2.1242 +    card (\<Union>C) = setsum card C"
  2.1243 +  by (frule card_UN_disjoint [of C id]) simp_all
  2.1244  
  2.1245  lemma setsum_multicount_gen:
  2.1246    assumes "finite s" "finite t" "\<forall>j\<in>t. (card {i\<in>s. R i j} = k j)"
  2.1247 -  shows "setsum (\<lambda>i. (card {j\<in>t. R i j})) s = setsum k t" (is "?l = ?r")
  2.1248 +  shows "setsum (\<lambda>i. (card {j\<in>t. R i j})) s = setsum k t"
  2.1249 +    (is "?l = ?r")
  2.1250  proof-
  2.1251 -  have "?l = setsum (\<lambda>i. setsum (\<lambda>x.1) {j\<in>t. R i j}) s" by auto
  2.1252 -  also have "\<dots> = ?r" unfolding setsum.commute_restrict [OF assms(1-2)]
  2.1253 +  have "?l = setsum (\<lambda>i. setsum (\<lambda>x.1) {j\<in>t. R i j}) s"
  2.1254 +    by auto
  2.1255 +  also have "\<dots> = ?r"
  2.1256 +    unfolding setsum.commute_restrict [OF assms(1-2)]
  2.1257      using assms(3) by auto
  2.1258    finally show ?thesis .
  2.1259  qed
  2.1260 @@ -998,17 +1072,18 @@
  2.1261    assumes "finite S" "finite T" "\<forall>j\<in>T. (card {i\<in>S. R i j} = k)"
  2.1262    shows "setsum (\<lambda>i. card {j\<in>T. R i j}) S = k * card T" (is "?l = ?r")
  2.1263  proof-
  2.1264 -  have "?l = setsum (\<lambda>i. k) T" by (rule setsum_multicount_gen) (auto simp: assms)
  2.1265 +  have "?l = setsum (\<lambda>i. k) T"
  2.1266 +    by (rule setsum_multicount_gen) (auto simp: assms)
  2.1267    also have "\<dots> = ?r" by (simp add: mult.commute)
  2.1268    finally show ?thesis by auto
  2.1269  qed
  2.1270  
  2.1271 +
  2.1272  subsubsection \<open>Cardinality of products\<close>
  2.1273  
  2.1274  lemma card_SigmaI [simp]:
  2.1275 -  "\<lbrakk> finite A; ALL a:A. finite (B a) \<rbrakk>
  2.1276 -  \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
  2.1277 -by(simp add: card_eq_setsum setsum.Sigma del:setsum_constant)
  2.1278 +  "finite A \<Longrightarrow> \<forall>a\<in>A. finite (B a) \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
  2.1279 +  by (simp add: card_eq_setsum setsum.Sigma del: setsum_constant)
  2.1280  
  2.1281  (*
  2.1282  lemma SigmaI_insert: "y \<notin> A ==>
  2.1283 @@ -1016,12 +1091,12 @@
  2.1284    by auto
  2.1285  *)
  2.1286  
  2.1287 -lemma card_cartesian_product: "card (A \<times> B) = card(A) * card(B)"
  2.1288 +lemma card_cartesian_product: "card (A \<times> B) = card A * card B"
  2.1289    by (cases "finite A \<and> finite B")
  2.1290      (auto simp add: card_eq_0_iff dest: finite_cartesian_productD1 finite_cartesian_productD2)
  2.1291  
  2.1292 -lemma card_cartesian_product_singleton:  "card({x} \<times> A) = card(A)"
  2.1293 -by (simp add: card_cartesian_product)
  2.1294 +lemma card_cartesian_product_singleton:  "card ({x} \<times> A) = card A"
  2.1295 +  by (simp add: card_cartesian_product)
  2.1296  
  2.1297  
  2.1298  subsection \<open>Generalized product over a set\<close>
  2.1299 @@ -1030,12 +1105,10 @@
  2.1300  begin
  2.1301  
  2.1302  sublocale setprod: comm_monoid_set times 1
  2.1303 -defines
  2.1304 -  setprod = setprod.F ..
  2.1305 +  defines setprod = setprod.F ..
  2.1306  
  2.1307 -abbreviation
  2.1308 -  Setprod ("\<Prod>_" [1000] 999) where
  2.1309 -  "\<Prod>A \<equiv> setprod (\<lambda>x. x) A"
  2.1310 +abbreviation Setprod ("\<Prod>_" [1000] 999)
  2.1311 +  where "\<Prod>A \<equiv> setprod (\<lambda>x. x) A"
  2.1312  
  2.1313  end
  2.1314  
  2.1315 @@ -1058,22 +1131,26 @@
  2.1316  context comm_monoid_mult
  2.1317  begin
  2.1318  
  2.1319 -lemma setprod_dvd_setprod:
  2.1320 -  "(\<And>a. a \<in> A \<Longrightarrow> f a dvd g a) \<Longrightarrow> setprod f A dvd setprod g A"
  2.1321 +lemma setprod_dvd_setprod: "(\<And>a. a \<in> A \<Longrightarrow> f a dvd g a) \<Longrightarrow> setprod f A dvd setprod g A"
  2.1322  proof (induct A rule: infinite_finite_induct)
  2.1323 -  case infinite then show ?case by (auto intro: dvdI)
  2.1324 +  case infinite
  2.1325 +  then show ?case by (auto intro: dvdI)
  2.1326 +next
  2.1327 +  case empty
  2.1328 +  then show ?case by (auto intro: dvdI)
  2.1329  next
  2.1330 -  case empty then show ?case by (auto intro: dvdI)
  2.1331 -next
  2.1332 -  case (insert a A) then
  2.1333 -  have "f a dvd g a" and "setprod f A dvd setprod g A" by simp_all
  2.1334 -  then obtain r s where "g a = f a * r" and "setprod g A = setprod f A * s" by (auto elim!: dvdE)
  2.1335 -  then have "g a * setprod g A = f a * setprod f A * (r * s)" by (simp add: ac_simps)
  2.1336 -  with insert.hyps show ?case by (auto intro: dvdI)
  2.1337 +  case (insert a A)
  2.1338 +  then have "f a dvd g a" and "setprod f A dvd setprod g A"
  2.1339 +    by simp_all
  2.1340 +  then obtain r s where "g a = f a * r" and "setprod g A = setprod f A * s"
  2.1341 +    by (auto elim!: dvdE)
  2.1342 +  then have "g a * setprod g A = f a * setprod f A * (r * s)"
  2.1343 +    by (simp add: ac_simps)
  2.1344 +  with insert.hyps show ?case
  2.1345 +    by (auto intro: dvdI)
  2.1346  qed
  2.1347  
  2.1348 -lemma setprod_dvd_setprod_subset:
  2.1349 -  "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> setprod f A dvd setprod f B"
  2.1350 +lemma setprod_dvd_setprod_subset: "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> setprod f A dvd setprod f B"
  2.1351    by (auto simp add: setprod.subset_diff ac_simps intro: dvdI)
  2.1352  
  2.1353  end
  2.1354 @@ -1090,21 +1167,23 @@
  2.1355  proof -
  2.1356    from \<open>finite A\<close> have "setprod f (insert a (A - {a})) = f a * setprod f (A - {a})"
  2.1357      by (intro setprod.insert) auto
  2.1358 -  also from \<open>a \<in> A\<close> have "insert a (A - {a}) = A" by blast
  2.1359 +  also from \<open>a \<in> A\<close> have "insert a (A - {a}) = A"
  2.1360 +    by blast
  2.1361    finally have "setprod f A = f a * setprod f (A - {a})" .
  2.1362 -  with \<open>b = f a\<close> show ?thesis by simp
  2.1363 +  with \<open>b = f a\<close> show ?thesis
  2.1364 +    by simp
  2.1365  qed
  2.1366  
  2.1367 -lemma dvd_setprodI [intro]:
  2.1368 -  assumes "finite A" and "a \<in> A"
  2.1369 -  shows "f a dvd setprod f A"
  2.1370 -  using assms by auto
  2.1371 +lemma dvd_setprodI [intro]: "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> f a dvd setprod f A"
  2.1372 +  by auto
  2.1373  
  2.1374  lemma setprod_zero:
  2.1375    assumes "finite A" and "\<exists>a\<in>A. f a = 0"
  2.1376    shows "setprod f A = 0"
  2.1377 -using assms proof (induct A)
  2.1378 -  case empty then show ?case by simp
  2.1379 +  using assms
  2.1380 +proof (induct A)
  2.1381 +  case empty
  2.1382 +  then show ?case by simp
  2.1383  next
  2.1384    case (insert a A)
  2.1385    then have "f a = 0 \<or> (\<exists>a\<in>A. f a = 0)" by simp
  2.1386 @@ -1126,71 +1205,73 @@
  2.1387  end
  2.1388  
  2.1389  lemma setprod_zero_iff [simp]:
  2.1390 +  fixes f :: "'b \<Rightarrow> 'a::semidom"
  2.1391    assumes "finite A"
  2.1392 -  shows "setprod f A = (0::'a::semidom) \<longleftrightarrow> (\<exists>a\<in>A. f a = 0)"
  2.1393 +  shows "setprod f A = 0 \<longleftrightarrow> (\<exists>a\<in>A. f a = 0)"
  2.1394    using assms by (induct A) (auto simp: no_zero_divisors)
  2.1395  
  2.1396  lemma (in semidom_divide) setprod_diff1:
  2.1397    assumes "finite A" and "f a \<noteq> 0"
  2.1398    shows "setprod f (A - {a}) = (if a \<in> A then setprod f A div f a else setprod f A)"
  2.1399  proof (cases "a \<notin> A")
  2.1400 -  case True then show ?thesis by simp
  2.1401 +  case True
  2.1402 +  then show ?thesis by simp
  2.1403  next
  2.1404 -  case False with assms show ?thesis
  2.1405 -  proof (induct A rule: finite_induct)
  2.1406 -    case empty then show ?case by simp
  2.1407 +  case False
  2.1408 +  with assms show ?thesis
  2.1409 +  proof induct
  2.1410 +    case empty
  2.1411 +    then show ?case by simp
  2.1412    next
  2.1413      case (insert b B)
  2.1414      then show ?case
  2.1415      proof (cases "a = b")
  2.1416 -      case True with insert show ?thesis by simp
  2.1417 +      case True
  2.1418 +      with insert show ?thesis by simp
  2.1419      next
  2.1420 -      case False with insert have "a \<in> B" by simp
  2.1421 +      case False
  2.1422 +      with insert have "a \<in> B" by simp
  2.1423        define C where "C = B - {a}"
  2.1424 -      with \<open>finite B\<close> \<open>a \<in> B\<close>
  2.1425 -        have *: "B = insert a C" "finite C" "a \<notin> C" by auto
  2.1426 -      with insert show ?thesis by (auto simp add: insert_commute ac_simps)
  2.1427 +      with \<open>finite B\<close> \<open>a \<in> B\<close> have "B = insert a C" "finite C" "a \<notin> C"
  2.1428 +        by auto
  2.1429 +      with insert show ?thesis
  2.1430 +        by (auto simp add: insert_commute ac_simps)
  2.1431      qed
  2.1432    qed
  2.1433  qed
  2.1434  
  2.1435 -lemma setsum_zero_power [simp]:
  2.1436 -  fixes c :: "nat \<Rightarrow> 'a::division_ring"
  2.1437 -  shows "(\<Sum>i\<in>A. c i * 0^i) = (if finite A \<and> 0 \<in> A then c 0 else 0)"
  2.1438 -apply (cases "finite A")
  2.1439 -  by (induction A rule: finite_induct) auto
  2.1440 +lemma setsum_zero_power [simp]: "(\<Sum>i\<in>A. c i * 0^i) = (if finite A \<and> 0 \<in> A then c 0 else 0)"
  2.1441 +  for c :: "nat \<Rightarrow> 'a::division_ring"
  2.1442 +  by (induct A rule: infinite_finite_induct) auto
  2.1443  
  2.1444  lemma setsum_zero_power' [simp]:
  2.1445 -  fixes c :: "nat \<Rightarrow> 'a::field"
  2.1446 -  shows "(\<Sum>i\<in>A. c i * 0^i / d i) = (if finite A \<and> 0 \<in> A then c 0 / d 0 else 0)"
  2.1447 -  using setsum_zero_power [of "\<lambda>i. c i / d i" A]
  2.1448 -  by auto
  2.1449 +  "(\<Sum>i\<in>A. c i * 0^i / d i) = (if finite A \<and> 0 \<in> A then c 0 / d 0 else 0)"
  2.1450 +  for c :: "nat \<Rightarrow> 'a::field"
  2.1451 +  using setsum_zero_power [of "\<lambda>i. c i / d i" A] by auto
  2.1452  
  2.1453  lemma (in field) setprod_inversef:
  2.1454    "finite A \<Longrightarrow> setprod (inverse \<circ> f) A = inverse (setprod f A)"
  2.1455    by (induct A rule: finite_induct) simp_all
  2.1456  
  2.1457 -lemma (in field) setprod_dividef:
  2.1458 -  "finite A \<Longrightarrow> (\<Prod>x\<in>A. f x / g x) = setprod f A / setprod g A"
  2.1459 +lemma (in field) setprod_dividef: "finite A \<Longrightarrow> (\<Prod>x\<in>A. f x / g x) = setprod f A / setprod g A"
  2.1460    using setprod_inversef [of A g] by (simp add: divide_inverse setprod.distrib)
  2.1461  
  2.1462  lemma setprod_Un:
  2.1463    fixes f :: "'b \<Rightarrow> 'a :: field"
  2.1464    assumes "finite A" and "finite B"
  2.1465 -  and "\<forall>x\<in>A \<inter> B. f x \<noteq> 0"
  2.1466 +    and "\<forall>x\<in>A \<inter> B. f x \<noteq> 0"
  2.1467    shows "setprod f (A \<union> B) = setprod f A * setprod f B / setprod f (A \<inter> B)"
  2.1468  proof -
  2.1469    from assms have "setprod f A * setprod f B = setprod f (A \<union> B) * setprod f (A \<inter> B)"
  2.1470      by (simp add: setprod.union_inter [symmetric, of A B])
  2.1471 -  with assms show ?thesis by simp
  2.1472 +  with assms show ?thesis
  2.1473 +    by simp
  2.1474  qed
  2.1475  
  2.1476 -lemma (in linordered_semidom) setprod_nonneg:
  2.1477 -  "(\<forall>a\<in>A. 0 \<le> f a) \<Longrightarrow> 0 \<le> setprod f A"
  2.1478 +lemma (in linordered_semidom) setprod_nonneg: "(\<forall>a\<in>A. 0 \<le> f a) \<Longrightarrow> 0 \<le> setprod f A"
  2.1479    by (induct A rule: infinite_finite_induct) simp_all
  2.1480  
  2.1481 -lemma (in linordered_semidom) setprod_pos:
  2.1482 -  "(\<forall>a\<in>A. 0 < f a) \<Longrightarrow> 0 < setprod f A"
  2.1483 +lemma (in linordered_semidom) setprod_pos: "(\<forall>a\<in>A. 0 < f a) \<Longrightarrow> 0 < setprod f A"
  2.1484    by (induct A rule: infinite_finite_induct) simp_all
  2.1485  
  2.1486  lemma (in linordered_semidom) setprod_mono:
  2.1487 @@ -1198,71 +1279,69 @@
  2.1488    by (induct A rule: infinite_finite_induct) (auto intro!: setprod_nonneg mult_mono)
  2.1489  
  2.1490  lemma (in linordered_semidom) setprod_mono_strict:
  2.1491 -    assumes"finite A" "\<forall>i\<in>A. 0 \<le> f i \<and> f i < g i" "A \<noteq> {}"
  2.1492 -    shows "setprod f A < setprod g A"
  2.1493 -using assms
  2.1494 -apply (induct A rule: finite_induct)
  2.1495 -apply (simp add: )
  2.1496 -apply (force intro: mult_strict_mono' setprod_nonneg)
  2.1497 -done
  2.1498 +  assumes "finite A" "\<forall>i\<in>A. 0 \<le> f i \<and> f i < g i" "A \<noteq> {}"
  2.1499 +  shows "setprod f A < setprod g A"
  2.1500 +  using assms
  2.1501 +proof (induct A rule: finite_induct)
  2.1502 +  case empty
  2.1503 +  then show ?case by simp
  2.1504 +next
  2.1505 +  case insert
  2.1506 +  then show ?case by (force intro: mult_strict_mono' setprod_nonneg)
  2.1507 +qed
  2.1508  
  2.1509 -lemma (in linordered_field) abs_setprod:
  2.1510 -  "\<bar>setprod f A\<bar> = (\<Prod>x\<in>A. \<bar>f x\<bar>)"
  2.1511 +lemma (in linordered_field) abs_setprod: "\<bar>setprod f A\<bar> = (\<Prod>x\<in>A. \<bar>f x\<bar>)"
  2.1512    by (induct A rule: infinite_finite_induct) (simp_all add: abs_mult)
  2.1513  
  2.1514 -lemma setprod_eq_1_iff [simp]:
  2.1515 -  "finite A \<Longrightarrow> setprod f A = 1 \<longleftrightarrow> (\<forall>a\<in>A. f a = (1::nat))"
  2.1516 +lemma setprod_eq_1_iff [simp]: "finite A \<Longrightarrow> setprod f A = 1 \<longleftrightarrow> (\<forall>a\<in>A. f a = 1)"
  2.1517 +  for f :: "'a \<Rightarrow> nat"
  2.1518    by (induct A rule: finite_induct) simp_all
  2.1519  
  2.1520 -lemma setprod_pos_nat_iff [simp]:
  2.1521 -  "finite A \<Longrightarrow> setprod f A > 0 \<longleftrightarrow> (\<forall>a\<in>A. f a > (0::nat))"
  2.1522 +lemma setprod_pos_nat_iff [simp]: "finite A \<Longrightarrow> setprod f A > 0 \<longleftrightarrow> (\<forall>a\<in>A. f a > 0)"
  2.1523 +  for f :: "'a \<Rightarrow> nat"
  2.1524    using setprod_zero_iff by (simp del: neq0_conv add: zero_less_iff_neq_zero)
  2.1525  
  2.1526 -lemma setprod_constant:
  2.1527 -  "(\<Prod>x\<in> A. (y::'a::comm_monoid_mult)) = y ^ card A"
  2.1528 +lemma setprod_constant: "(\<Prod>x\<in> A. y) = y ^ card A"
  2.1529 +  for y :: "'a::comm_monoid_mult"
  2.1530    by (induct A rule: infinite_finite_induct) simp_all
  2.1531  
  2.1532 -lemma setprod_power_distrib:
  2.1533 -  fixes f :: "'a \<Rightarrow> 'b::comm_semiring_1"
  2.1534 -  shows "setprod f A ^ n = setprod (\<lambda>x. (f x) ^ n) A"
  2.1535 -proof (cases "finite A")
  2.1536 -  case True then show ?thesis
  2.1537 -    by (induct A rule: finite_induct) (auto simp add: power_mult_distrib)
  2.1538 -next
  2.1539 -  case False then show ?thesis
  2.1540 -    by simp
  2.1541 -qed
  2.1542 +lemma setprod_power_distrib: "setprod f A ^ n = setprod (\<lambda>x. (f x) ^ n) A"
  2.1543 +  for f :: "'a \<Rightarrow> 'b::comm_semiring_1"
  2.1544 +  by (induct A rule: infinite_finite_induct) (auto simp add: power_mult_distrib)
  2.1545  
  2.1546 -lemma power_setsum:
  2.1547 -  "c ^ (\<Sum>a\<in>A. f a) = (\<Prod>a\<in>A. c ^ f a)"
  2.1548 +lemma power_setsum: "c ^ (\<Sum>a\<in>A. f a) = (\<Prod>a\<in>A. c ^ f a)"
  2.1549    by (induct A rule: infinite_finite_induct) (simp_all add: power_add)
  2.1550  
  2.1551  lemma setprod_gen_delta:
  2.1552 -  assumes fS: "finite S"
  2.1553 -  shows "setprod (\<lambda>k. if k=a then b k else c) S = (if a \<in> S then (b a ::'a::comm_monoid_mult) * c^ (card S - 1) else c^ card S)"
  2.1554 -proof-
  2.1555 +  fixes b :: "'b \<Rightarrow> 'a::comm_monoid_mult"
  2.1556 +  assumes fin: "finite S"
  2.1557 +  shows "setprod (\<lambda>k. if k = a then b k else c) S =
  2.1558 +    (if a \<in> S then b a * c ^ (card S - 1) else c ^ card S)"
  2.1559 +proof -
  2.1560    let ?f = "(\<lambda>k. if k=a then b k else c)"
  2.1561 -  {assume a: "a \<notin> S"
  2.1562 -    hence "\<forall> k\<in> S. ?f k = c" by simp
  2.1563 -    hence ?thesis using a setprod_constant by simp }
  2.1564 -  moreover
  2.1565 -  {assume a: "a \<in> S"
  2.1566 +  show ?thesis
  2.1567 +  proof (cases "a \<in> S")
  2.1568 +    case False
  2.1569 +    then have "\<forall> k\<in> S. ?f k = c" by simp
  2.1570 +    with False show ?thesis by (simp add: setprod_constant)
  2.1571 +  next
  2.1572 +    case True
  2.1573      let ?A = "S - {a}"
  2.1574      let ?B = "{a}"
  2.1575 -    have eq: "S = ?A \<union> ?B" using a by blast
  2.1576 -    have dj: "?A \<inter> ?B = {}" by simp
  2.1577 -    from fS have fAB: "finite ?A" "finite ?B" by auto
  2.1578 -    have fA0:"setprod ?f ?A = setprod (\<lambda>i. c) ?A"
  2.1579 +    from True have eq: "S = ?A \<union> ?B" by blast
  2.1580 +    have disjoint: "?A \<inter> ?B = {}" by simp
  2.1581 +    from fin have fin': "finite ?A" "finite ?B" by auto
  2.1582 +    have f_A0: "setprod ?f ?A = setprod (\<lambda>i. c) ?A"
  2.1583        by (rule setprod.cong) auto
  2.1584 -    have cA: "card ?A = card S - 1" using fS a by auto
  2.1585 -    have fA1: "setprod ?f ?A = c ^ card ?A"
  2.1586 -      unfolding fA0 by (rule setprod_constant)
  2.1587 +    from fin True have card_A: "card ?A = card S - 1" by auto
  2.1588 +    have f_A1: "setprod ?f ?A = c ^ card ?A"
  2.1589 +      unfolding f_A0 by (rule setprod_constant)
  2.1590      have "setprod ?f ?A * setprod ?f ?B = setprod ?f S"
  2.1591 -      using setprod.union_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
  2.1592 +      using setprod.union_disjoint[OF fin' disjoint, of ?f, unfolded eq[symmetric]]
  2.1593        by simp
  2.1594 -    then have ?thesis using a cA
  2.1595 -      by (simp add: fA1 field_simps cong add: setprod.cong cong del: if_weak_cong)}
  2.1596 -  ultimately show ?thesis by blast
  2.1597 +    with True card_A show ?thesis
  2.1598 +      by (simp add: f_A1 field_simps cong add: setprod.cong cong del: if_weak_cong)
  2.1599 +  qed
  2.1600  qed
  2.1601  
  2.1602  end
     3.1 --- a/src/HOL/Num.thy	Wed Aug 10 22:05:00 2016 +0200
     3.2 +++ b/src/HOL/Num.thy	Wed Aug 10 22:05:36 2016 +0200
     3.3 @@ -6,7 +6,7 @@
     3.4  section \<open>Binary Numerals\<close>
     3.5  
     3.6  theory Num
     3.7 -imports BNF_Least_Fixpoint
     3.8 +  imports BNF_Least_Fixpoint
     3.9  begin
    3.10  
    3.11  subsection \<open>The \<open>num\<close> type\<close>
    3.12 @@ -15,21 +15,24 @@
    3.13  
    3.14  text \<open>Increment function for type @{typ num}\<close>
    3.15  
    3.16 -primrec inc :: "num \<Rightarrow> num" where
    3.17 -  "inc One = Bit0 One" |
    3.18 -  "inc (Bit0 x) = Bit1 x" |
    3.19 -  "inc (Bit1 x) = Bit0 (inc x)"
    3.20 +primrec inc :: "num \<Rightarrow> num"
    3.21 +  where
    3.22 +    "inc One = Bit0 One"
    3.23 +  | "inc (Bit0 x) = Bit1 x"
    3.24 +  | "inc (Bit1 x) = Bit0 (inc x)"
    3.25  
    3.26  text \<open>Converting between type @{typ num} and type @{typ nat}\<close>
    3.27  
    3.28 -primrec nat_of_num :: "num \<Rightarrow> nat" where
    3.29 -  "nat_of_num One = Suc 0" |
    3.30 -  "nat_of_num (Bit0 x) = nat_of_num x + nat_of_num x" |
    3.31 -  "nat_of_num (Bit1 x) = Suc (nat_of_num x + nat_of_num x)"
    3.32 +primrec nat_of_num :: "num \<Rightarrow> nat"
    3.33 +  where
    3.34 +    "nat_of_num One = Suc 0"
    3.35 +  | "nat_of_num (Bit0 x) = nat_of_num x + nat_of_num x"
    3.36 +  | "nat_of_num (Bit1 x) = Suc (nat_of_num x + nat_of_num x)"
    3.37  
    3.38 -primrec num_of_nat :: "nat \<Rightarrow> num" where
    3.39 -  "num_of_nat 0 = One" |
    3.40 -  "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)"
    3.41 +primrec num_of_nat :: "nat \<Rightarrow> num"
    3.42 +  where
    3.43 +    "num_of_nat 0 = One"
    3.44 +  | "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)"
    3.45  
    3.46  lemma nat_of_num_pos: "0 < nat_of_num x"
    3.47    by (induct x) simp_all
    3.48 @@ -40,14 +43,10 @@
    3.49  lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)"
    3.50    by (induct x) simp_all
    3.51  
    3.52 -lemma num_of_nat_double:
    3.53 -  "0 < n \<Longrightarrow> num_of_nat (n + n) = Bit0 (num_of_nat n)"
    3.54 +lemma num_of_nat_double: "0 < n \<Longrightarrow> num_of_nat (n + n) = Bit0 (num_of_nat n)"
    3.55    by (induct n) simp_all
    3.56  
    3.57 -text \<open>
    3.58 -  Type @{typ num} is isomorphic to the strictly positive
    3.59 -  natural numbers.
    3.60 -\<close>
    3.61 +text \<open>Type @{typ num} is isomorphic to the strictly positive natural numbers.\<close>
    3.62  
    3.63  lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x"
    3.64    by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos)
    3.65 @@ -68,10 +67,11 @@
    3.66    shows "P x"
    3.67  proof -
    3.68    obtain n where n: "Suc n = nat_of_num x"
    3.69 -    by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0)
    3.70 +    by (cases "nat_of_num x") (simp_all add: nat_of_num_neq_0)
    3.71    have "P (num_of_nat (Suc n))"
    3.72    proof (induct n)
    3.73 -    case 0 show ?case using One by simp
    3.74 +    case 0
    3.75 +    from One show ?case by simp
    3.76    next
    3.77      case (Suc n)
    3.78      then have "P (inc (num_of_nat (Suc n)))" by (rule inc)
    3.79 @@ -82,9 +82,9 @@
    3.80  qed
    3.81  
    3.82  text \<open>
    3.83 -  From now on, there are two possible models for @{typ num}:
    3.84 -  as positive naturals (rule \<open>num_induct\<close>)
    3.85 -  and as digit representation (rules \<open>num.induct\<close>, \<open>num.cases\<close>).
    3.86 +  From now on, there are two possible models for @{typ num}: as positive
    3.87 +  naturals (rule \<open>num_induct\<close>) and as digit representation (rules
    3.88 +  \<open>num.induct\<close>, \<open>num.cases\<close>).
    3.89  \<close>
    3.90  
    3.91  
    3.92 @@ -93,17 +93,13 @@
    3.93  instantiation num :: "{plus,times,linorder}"
    3.94  begin
    3.95  
    3.96 -definition [code del]:
    3.97 -  "m + n = num_of_nat (nat_of_num m + nat_of_num n)"
    3.98 +definition [code del]: "m + n = num_of_nat (nat_of_num m + nat_of_num n)"
    3.99  
   3.100 -definition [code del]:
   3.101 -  "m * n = num_of_nat (nat_of_num m * nat_of_num n)"
   3.102 +definition [code del]: "m * n = num_of_nat (nat_of_num m * nat_of_num n)"
   3.103  
   3.104 -definition [code del]:
   3.105 -  "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"
   3.106 +definition [code del]: "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"
   3.107  
   3.108 -definition [code del]:
   3.109 -  "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"
   3.110 +definition [code del]: "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"
   3.111  
   3.112  instance
   3.113    by standard (auto simp add: less_num_def less_eq_num_def num_eq_iff)
   3.114 @@ -137,8 +133,7 @@
   3.115    "Bit0 m * Bit1 n = Bit0 (m * Bit1 n)"
   3.116    "Bit1 m * Bit0 n = Bit0 (Bit1 m * n)"
   3.117    "Bit1 m * Bit1 n = Bit1 (m + n + Bit0 (m * n))"
   3.118 -  by (simp_all add: num_eq_iff nat_of_num_add
   3.119 -    nat_of_num_mult distrib_right distrib_left)
   3.120 +  by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult distrib_right distrib_left)
   3.121  
   3.122  lemma eq_num_simps:
   3.123    "One = One \<longleftrightarrow> True"
   3.124 @@ -175,9 +170,9 @@
   3.125    by (auto simp add: less_eq_num_def less_num_def)
   3.126  
   3.127  lemma le_num_One_iff: "x \<le> num.One \<longleftrightarrow> x = num.One"
   3.128 -by (simp add: antisym_conv)
   3.129 +  by (simp add: antisym_conv)
   3.130  
   3.131 -text \<open>Rules using \<open>One\<close> and \<open>inc\<close> as constructors\<close>
   3.132 +text \<open>Rules using \<open>One\<close> and \<open>inc\<close> as constructors.\<close>
   3.133  
   3.134  lemma add_One: "x + One = inc x"
   3.135    by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
   3.136 @@ -191,22 +186,22 @@
   3.137  lemma mult_inc: "x * inc y = x * y + x"
   3.138    by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
   3.139  
   3.140 -text \<open>The @{const num_of_nat} conversion\<close>
   3.141 +text \<open>The @{const num_of_nat} conversion.\<close>
   3.142  
   3.143 -lemma num_of_nat_One:
   3.144 -  "n \<le> 1 \<Longrightarrow> num_of_nat n = One"
   3.145 +lemma num_of_nat_One: "n \<le> 1 \<Longrightarrow> num_of_nat n = One"
   3.146    by (cases n) simp_all
   3.147  
   3.148  lemma num_of_nat_plus_distrib:
   3.149    "0 < m \<Longrightarrow> 0 < n \<Longrightarrow> num_of_nat (m + n) = num_of_nat m + num_of_nat n"
   3.150    by (induct n) (auto simp add: add_One add_One_commute add_inc)
   3.151  
   3.152 -text \<open>A double-and-decrement function\<close>
   3.153 +text \<open>A double-and-decrement function.\<close>
   3.154  
   3.155 -primrec BitM :: "num \<Rightarrow> num" where
   3.156 -  "BitM One = One" |
   3.157 -  "BitM (Bit0 n) = Bit1 (BitM n)" |
   3.158 -  "BitM (Bit1 n) = Bit1 (Bit0 n)"
   3.159 +primrec BitM :: "num \<Rightarrow> num"
   3.160 +  where
   3.161 +    "BitM One = One"
   3.162 +  | "BitM (Bit0 n) = Bit1 (BitM n)"
   3.163 +  | "BitM (Bit1 n) = Bit1 (Bit0 n)"
   3.164  
   3.165  lemma BitM_plus_one: "BitM n + One = Bit0 n"
   3.166    by (induct n) simp_all
   3.167 @@ -214,20 +209,22 @@
   3.168  lemma one_plus_BitM: "One + BitM n = Bit0 n"
   3.169    unfolding add_One_commute BitM_plus_one ..
   3.170  
   3.171 -text \<open>Squaring and exponentiation\<close>
   3.172 +text \<open>Squaring and exponentiation.\<close>
   3.173  
   3.174 -primrec sqr :: "num \<Rightarrow> num" where
   3.175 -  "sqr One = One" |
   3.176 -  "sqr (Bit0 n) = Bit0 (Bit0 (sqr n))" |
   3.177 -  "sqr (Bit1 n) = Bit1 (Bit0 (sqr n + n))"
   3.178 +primrec sqr :: "num \<Rightarrow> num"
   3.179 +  where
   3.180 +    "sqr One = One"
   3.181 +  | "sqr (Bit0 n) = Bit0 (Bit0 (sqr n))"
   3.182 +  | "sqr (Bit1 n) = Bit1 (Bit0 (sqr n + n))"
   3.183  
   3.184 -primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" where
   3.185 -  "pow x One = x" |
   3.186 -  "pow x (Bit0 y) = sqr (pow x y)" |
   3.187 -  "pow x (Bit1 y) = sqr (pow x y) * x"
   3.188 +primrec pow :: "num \<Rightarrow> num \<Rightarrow> num"
   3.189 +  where
   3.190 +    "pow x One = x"
   3.191 +  | "pow x (Bit0 y) = sqr (pow x y)"
   3.192 +  | "pow x (Bit1 y) = sqr (pow x y) * x"
   3.193  
   3.194  lemma nat_of_num_sqr: "nat_of_num (sqr x) = nat_of_num x * nat_of_num x"
   3.195 -  by (induct x, simp_all add: algebra_simps nat_of_num_add)
   3.196 +  by (induct x) (simp_all add: algebra_simps nat_of_num_add)
   3.197  
   3.198  lemma sqr_conv_mult: "sqr x = x * x"
   3.199    by (simp add: num_eq_iff nat_of_num_sqr nat_of_num_mult)
   3.200 @@ -243,32 +240,44 @@
   3.201  class numeral = one + semigroup_add
   3.202  begin
   3.203  
   3.204 -primrec numeral :: "num \<Rightarrow> 'a" where
   3.205 -  numeral_One: "numeral One = 1" |
   3.206 -  numeral_Bit0: "numeral (Bit0 n) = numeral n + numeral n" |
   3.207 -  numeral_Bit1: "numeral (Bit1 n) = numeral n + numeral n + 1"
   3.208 +primrec numeral :: "num \<Rightarrow> 'a"
   3.209 +  where
   3.210 +    numeral_One: "numeral One = 1"
   3.211 +  | numeral_Bit0: "numeral (Bit0 n) = numeral n + numeral n"
   3.212 +  | numeral_Bit1: "numeral (Bit1 n) = numeral n + numeral n + 1"
   3.213  
   3.214  lemma numeral_code [code]:
   3.215    "numeral One = 1"
   3.216    "numeral (Bit0 n) = (let m = numeral n in m + m)"
   3.217    "numeral (Bit1 n) = (let m = numeral n in m + m + 1)"
   3.218    by (simp_all add: Let_def)
   3.219 -  
   3.220 +
   3.221  lemma one_plus_numeral_commute: "1 + numeral x = numeral x + 1"
   3.222 -  apply (induct x)
   3.223 -  apply simp
   3.224 -  apply (simp add: add.assoc [symmetric], simp add: add.assoc)
   3.225 -  apply (simp add: add.assoc [symmetric], simp add: add.assoc)
   3.226 -  done
   3.227 +proof (induct x)
   3.228 +  case One
   3.229 +  then show ?case by simp
   3.230 +next
   3.231 +  case Bit0
   3.232 +  then show ?case by (simp add: add.assoc [symmetric]) (simp add: add.assoc)
   3.233 +next
   3.234 +  case Bit1
   3.235 +  then show ?case by (simp add: add.assoc [symmetric]) (simp add: add.assoc)
   3.236 +qed
   3.237  
   3.238  lemma numeral_inc: "numeral (inc x) = numeral x + 1"
   3.239  proof (induct x)
   3.240 +  case One
   3.241 +  then show ?case by simp
   3.242 +next
   3.243 +  case Bit0
   3.244 +  then show ?case by simp
   3.245 +next
   3.246    case (Bit1 x)
   3.247    have "numeral x + (1 + numeral x) + 1 = numeral x + (numeral x + 1) + 1"
   3.248      by (simp only: one_plus_numeral_commute)
   3.249    with Bit1 show ?case
   3.250      by (simp add: add.assoc)
   3.251 -qed simp_all
   3.252 +qed
   3.253  
   3.254  declare numeral.simps [simp del]
   3.255  
   3.256 @@ -320,9 +329,8 @@
   3.257  
   3.258  subsection \<open>Class-specific numeral rules\<close>
   3.259  
   3.260 -text \<open>
   3.261 -  @{const numeral} is a morphism.
   3.262 -\<close>
   3.263 +text \<open>@{const numeral} is a morphism.\<close>
   3.264 +
   3.265  
   3.266  subsubsection \<open>Structures with addition: class \<open>numeral\<close>\<close>
   3.267  
   3.268 @@ -331,7 +339,7 @@
   3.269  
   3.270  lemma numeral_add: "numeral (m + n) = numeral m + numeral n"
   3.271    by (induct n rule: num_induct)
   3.272 -     (simp_all only: numeral_One add_One add_inc numeral_inc add.assoc)
   3.273 +    (simp_all only: numeral_One add_One add_inc numeral_inc add.assoc)
   3.274  
   3.275  lemma numeral_plus_numeral: "numeral m + numeral n = numeral (m + n)"
   3.276    by (rule numeral_add [symmetric])
   3.277 @@ -350,44 +358,43 @@
   3.278  
   3.279  end
   3.280  
   3.281 -subsubsection \<open>
   3.282 -  Structures with negation: class \<open>neg_numeral\<close>
   3.283 -\<close>
   3.284 +
   3.285 +subsubsection \<open>Structures with negation: class \<open>neg_numeral\<close>\<close>
   3.286  
   3.287  class neg_numeral = numeral + group_add
   3.288  begin
   3.289  
   3.290 -lemma uminus_numeral_One:
   3.291 -  "- Numeral1 = - 1"
   3.292 +lemma uminus_numeral_One: "- Numeral1 = - 1"
   3.293    by (simp add: numeral_One)
   3.294  
   3.295  text \<open>Numerals form an abelian subgroup.\<close>
   3.296  
   3.297 -inductive is_num :: "'a \<Rightarrow> bool" where
   3.298 -  "is_num 1" |
   3.299 -  "is_num x \<Longrightarrow> is_num (- x)" |
   3.300 -  "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> is_num (x + y)"
   3.301 +inductive is_num :: "'a \<Rightarrow> bool"
   3.302 +  where
   3.303 +    "is_num 1"
   3.304 +  | "is_num x \<Longrightarrow> is_num (- x)"
   3.305 +  | "is_num x \<Longrightarrow> is_num y \<Longrightarrow> is_num (x + y)"
   3.306  
   3.307  lemma is_num_numeral: "is_num (numeral k)"
   3.308 -  by (induct k, simp_all add: numeral.simps is_num.intros)
   3.309 +  by (induct k) (simp_all add: numeral.simps is_num.intros)
   3.310  
   3.311 -lemma is_num_add_commute:
   3.312 -  "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> x + y = y + x"
   3.313 +lemma is_num_add_commute: "is_num x \<Longrightarrow> is_num y \<Longrightarrow> x + y = y + x"
   3.314    apply (induct x rule: is_num.induct)
   3.315 -  apply (induct y rule: is_num.induct)
   3.316 -  apply simp
   3.317 -  apply (rule_tac a=x in add_left_imp_eq)
   3.318 -  apply (rule_tac a=x in add_right_imp_eq)
   3.319 +    apply (induct y rule: is_num.induct)
   3.320 +      apply simp
   3.321 +     apply (rule_tac a=x in add_left_imp_eq)
   3.322 +     apply (rule_tac a=x in add_right_imp_eq)
   3.323 +     apply (simp add: add.assoc)
   3.324 +    apply (simp add: add.assoc [symmetric])
   3.325 +    apply (simp add: add.assoc)
   3.326 +   apply (rule_tac a=x in add_left_imp_eq)
   3.327 +   apply (rule_tac a=x in add_right_imp_eq)
   3.328 +   apply (simp add: add.assoc)
   3.329    apply (simp add: add.assoc)
   3.330 -  apply (simp add: add.assoc [symmetric], simp add: add.assoc)
   3.331 -  apply (rule_tac a=x in add_left_imp_eq)
   3.332 -  apply (rule_tac a=x in add_right_imp_eq)
   3.333 -  apply (simp add: add.assoc)
   3.334 -  apply (simp add: add.assoc, simp add: add.assoc [symmetric])
   3.335 +  apply (simp add: add.assoc [symmetric])
   3.336    done
   3.337  
   3.338 -lemma is_num_add_left_commute:
   3.339 -  "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> x + (y + z) = y + (x + z)"
   3.340 +lemma is_num_add_left_commute: "is_num x \<Longrightarrow> is_num y \<Longrightarrow> x + (y + z) = y + (x + z)"
   3.341    by (simp only: add.assoc [symmetric] is_num_add_commute)
   3.342  
   3.343  lemmas is_num_normalize =
   3.344 @@ -395,12 +402,17 @@
   3.345    is_num.intros is_num_numeral
   3.346    minus_add
   3.347  
   3.348 -definition dbl :: "'a \<Rightarrow> 'a" where "dbl x = x + x"
   3.349 -definition dbl_inc :: "'a \<Rightarrow> 'a" where "dbl_inc x = x + x + 1"
   3.350 -definition dbl_dec :: "'a \<Rightarrow> 'a" where "dbl_dec x = x + x - 1"
   3.351 +definition dbl :: "'a \<Rightarrow> 'a"
   3.352 +  where "dbl x = x + x"
   3.353 +
   3.354 +definition dbl_inc :: "'a \<Rightarrow> 'a"
   3.355 +  where "dbl_inc x = x + x + 1"
   3.356  
   3.357 -definition sub :: "num \<Rightarrow> num \<Rightarrow> 'a" where
   3.358 -  "sub k l = numeral k - numeral l"
   3.359 +definition dbl_dec :: "'a \<Rightarrow> 'a"
   3.360 +  where "dbl_dec x = x + x - 1"
   3.361 +
   3.362 +definition sub :: "num \<Rightarrow> num \<Rightarrow> 'a"
   3.363 +  where "sub k l = numeral k - numeral l"
   3.364  
   3.365  lemma numeral_BitM: "numeral (BitM n) = numeral (Bit0 n) - 1"
   3.366    by (simp only: BitM_plus_one [symmetric] numeral_add numeral_One eq_diff_eq)
   3.367 @@ -419,7 +431,8 @@
   3.368    "dbl_inc 1 = 3"
   3.369    "dbl_inc (- 1) = - 1"
   3.370    "dbl_inc (numeral k) = numeral (Bit1 k)"
   3.371 -  by (simp_all add: dbl_inc_def dbl_dec_def numeral.simps numeral_BitM is_num_normalize algebra_simps del: add_uminus_conv_diff)
   3.372 +  by (simp_all add: dbl_inc_def dbl_dec_def numeral.simps numeral_BitM is_num_normalize algebra_simps
   3.373 +      del: add_uminus_conv_diff)
   3.374  
   3.375  lemma dbl_dec_simps [simp]:
   3.376    "dbl_dec (- numeral k) = - dbl_inc (numeral k)"
   3.377 @@ -447,7 +460,7 @@
   3.378    "- numeral m + numeral n = sub n m"
   3.379    "- numeral m + - numeral n = - (numeral m + numeral n)"
   3.380    by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize
   3.381 -    del: add_uminus_conv_diff add: diff_conv_add_uminus)
   3.382 +      del: add_uminus_conv_diff add: diff_conv_add_uminus)
   3.383  
   3.384  lemma add_neg_numeral_special:
   3.385    "1 + - numeral m = sub One m"
   3.386 @@ -460,7 +473,7 @@
   3.387    "- 1 + 1 = 0"
   3.388    "- 1 + - 1 = - 2"
   3.389    by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize right_minus numeral_inc
   3.390 -    del: add_uminus_conv_diff add: diff_conv_add_uminus)
   3.391 +      del: add_uminus_conv_diff add: diff_conv_add_uminus)
   3.392  
   3.393  lemma diff_numeral_simps:
   3.394    "numeral m - numeral n = sub m n"
   3.395 @@ -468,7 +481,7 @@
   3.396    "- numeral m - numeral n = - numeral (m + n)"
   3.397    "- numeral m - - numeral n = sub n m"
   3.398    by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize
   3.399 -    del: add_uminus_conv_diff add: diff_conv_add_uminus)
   3.400 +      del: add_uminus_conv_diff add: diff_conv_add_uminus)
   3.401  
   3.402  lemma diff_numeral_special:
   3.403    "1 - numeral n = sub One n"
   3.404 @@ -484,13 +497,12 @@
   3.405    "1 - - 1 = 2"
   3.406    "- 1 - - 1 = 0"
   3.407    by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize numeral_inc
   3.408 -    del: add_uminus_conv_diff add: diff_conv_add_uminus)
   3.409 +      del: add_uminus_conv_diff add: diff_conv_add_uminus)
   3.410  
   3.411  end
   3.412  
   3.413 -subsubsection \<open>
   3.414 -  Structures with multiplication: class \<open>semiring_numeral\<close>
   3.415 -\<close>
   3.416 +
   3.417 +subsubsection \<open>Structures with multiplication: class \<open>semiring_numeral\<close>\<close>
   3.418  
   3.419  class semiring_numeral = semiring + monoid_mult
   3.420  begin
   3.421 @@ -498,25 +510,22 @@
   3.422  subclass numeral ..
   3.423  
   3.424  lemma numeral_mult: "numeral (m * n) = numeral m * numeral n"
   3.425 -  apply (induct n rule: num_induct)
   3.426 -  apply (simp add: numeral_One)
   3.427 -  apply (simp add: mult_inc numeral_inc numeral_add distrib_left)
   3.428 -  done
   3.429 +  by (induct n rule: num_induct)
   3.430 +    (simp_all add: numeral_One mult_inc numeral_inc numeral_add distrib_left)
   3.431  
   3.432  lemma numeral_times_numeral: "numeral m * numeral n = numeral (m * n)"
   3.433    by (rule numeral_mult [symmetric])
   3.434  
   3.435  lemma mult_2: "2 * z = z + z"
   3.436 -  unfolding one_add_one [symmetric] distrib_right by simp
   3.437 +  by (simp add: one_add_one [symmetric] distrib_right)
   3.438  
   3.439  lemma mult_2_right: "z * 2 = z + z"
   3.440 -  unfolding one_add_one [symmetric] distrib_left by simp
   3.441 +  by (simp add: one_add_one [symmetric] distrib_left)
   3.442  
   3.443  end
   3.444  
   3.445 -subsubsection \<open>
   3.446 -  Structures with a zero: class \<open>semiring_1\<close>
   3.447 -\<close>
   3.448 +
   3.449 +subsubsection \<open>Structures with a zero: class \<open>semiring_1\<close>\<close>
   3.450  
   3.451  context semiring_1
   3.452  begin
   3.453 @@ -524,18 +533,17 @@
   3.454  subclass semiring_numeral ..
   3.455  
   3.456  lemma of_nat_numeral [simp]: "of_nat (numeral n) = numeral n"
   3.457 -  by (induct n,
   3.458 -    simp_all only: numeral.simps numeral_class.numeral.simps of_nat_add of_nat_1)
   3.459 +  by (induct n) (simp_all only: numeral.simps numeral_class.numeral.simps of_nat_add of_nat_1)
   3.460  
   3.461  end
   3.462  
   3.463 -lemma nat_of_num_numeral [code_abbrev]:
   3.464 -  "nat_of_num = numeral"
   3.465 +lemma nat_of_num_numeral [code_abbrev]: "nat_of_num = numeral"
   3.466  proof
   3.467    fix n
   3.468    have "numeral n = nat_of_num n"
   3.469      by (induct n) (simp_all add: numeral.simps)
   3.470 -  then show "nat_of_num n = numeral n" by simp
   3.471 +  then show "nat_of_num n = numeral n"
   3.472 +    by simp
   3.473  qed
   3.474  
   3.475  lemma nat_of_num_code [code]:
   3.476 @@ -544,16 +552,15 @@
   3.477    "nat_of_num (Bit1 n) = (let m = nat_of_num n in Suc (m + m))"
   3.478    by (simp_all add: Let_def)
   3.479  
   3.480 -subsubsection \<open>
   3.481 -  Equality: class \<open>semiring_char_0\<close>
   3.482 -\<close>
   3.483 +
   3.484 +subsubsection \<open>Equality: class \<open>semiring_char_0\<close>\<close>
   3.485  
   3.486  context semiring_char_0
   3.487  begin
   3.488  
   3.489  lemma numeral_eq_iff: "numeral m = numeral n \<longleftrightarrow> m = n"
   3.490 -  unfolding of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
   3.491 -    of_nat_eq_iff num_eq_iff ..
   3.492 +  by (simp only: of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
   3.493 +    of_nat_eq_iff num_eq_iff)
   3.494  
   3.495  lemma numeral_eq_one_iff: "numeral n = 1 \<longleftrightarrow> n = One"
   3.496    by (rule numeral_eq_iff [of n One, unfolded numeral_One])
   3.497 @@ -562,8 +569,7 @@
   3.498    by (rule numeral_eq_iff [of One n, unfolded numeral_One])
   3.499  
   3.500  lemma numeral_neq_zero: "numeral n \<noteq> 0"
   3.501 -  unfolding of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
   3.502 -  by (simp add: nat_of_num_pos)
   3.503 +  by (simp add: of_nat_numeral [symmetric] nat_of_num_numeral [symmetric] nat_of_num_pos)
   3.504  
   3.505  lemma zero_neq_numeral: "0 \<noteq> numeral n"
   3.506    unfolding eq_commute [of 0] by (rule numeral_neq_zero)
   3.507 @@ -577,9 +583,8 @@
   3.508  
   3.509  end
   3.510  
   3.511 -subsubsection \<open>
   3.512 -  Comparisons: class \<open>linordered_semidom\<close>
   3.513 -\<close>
   3.514 +
   3.515 +subsubsection \<open>Comparisons: class \<open>linordered_semidom\<close>\<close>
   3.516  
   3.517  text \<open>Could be perhaps more general than here.\<close>
   3.518  
   3.519 @@ -589,15 +594,15 @@
   3.520  lemma numeral_le_iff: "numeral m \<le> numeral n \<longleftrightarrow> m \<le> n"
   3.521  proof -
   3.522    have "of_nat (numeral m) \<le> of_nat (numeral n) \<longleftrightarrow> m \<le> n"
   3.523 -    unfolding less_eq_num_def nat_of_num_numeral of_nat_le_iff ..
   3.524 +    by (simp only: less_eq_num_def nat_of_num_numeral of_nat_le_iff)
   3.525    then show ?thesis by simp
   3.526  qed
   3.527  
   3.528  lemma one_le_numeral: "1 \<le> numeral n"
   3.529 -using numeral_le_iff [of One n] by (simp add: numeral_One)
   3.530 +  using numeral_le_iff [of One n] by (simp add: numeral_One)
   3.531  
   3.532  lemma numeral_le_one_iff: "numeral n \<le> 1 \<longleftrightarrow> n \<le> One"
   3.533 -using numeral_le_iff [of n One] by (simp add: numeral_One)
   3.534 +  using numeral_le_iff [of n One] by (simp add: numeral_One)
   3.535  
   3.536  lemma numeral_less_iff: "numeral m < numeral n \<longleftrightarrow> m < n"
   3.537  proof -
   3.538 @@ -647,30 +652,33 @@
   3.539    not_numeral_less_zero
   3.540  
   3.541  lemma min_0_1 [simp]:
   3.542 -  fixes min' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" defines "min' \<equiv> min" shows
   3.543 -  "min' 0 1 = 0"
   3.544 -  "min' 1 0 = 0"
   3.545 -  "min' 0 (numeral x) = 0"
   3.546 -  "min' (numeral x) 0 = 0"
   3.547 -  "min' 1 (numeral x) = 1"
   3.548 -  "min' (numeral x) 1 = 1"
   3.549 -by(simp_all add: min'_def min_def le_num_One_iff)
   3.550 +  fixes min' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
   3.551 +  defines "min' \<equiv> min"
   3.552 +  shows
   3.553 +    "min' 0 1 = 0"
   3.554 +    "min' 1 0 = 0"
   3.555 +    "min' 0 (numeral x) = 0"
   3.556 +    "min' (numeral x) 0 = 0"
   3.557 +    "min' 1 (numeral x) = 1"
   3.558 +    "min' (numeral x) 1 = 1"
   3.559 +  by (simp_all add: min'_def min_def le_num_One_iff)
   3.560  
   3.561 -lemma max_0_1 [simp]: 
   3.562 -  fixes max' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" defines "max' \<equiv> max" shows
   3.563 -  "max' 0 1 = 1"
   3.564 -  "max' 1 0 = 1"
   3.565 -  "max' 0 (numeral x) = numeral x"
   3.566 -  "max' (numeral x) 0 = numeral x"
   3.567 -  "max' 1 (numeral x) = numeral x"
   3.568 -  "max' (numeral x) 1 = numeral x"
   3.569 -by(simp_all add: max'_def max_def le_num_One_iff)
   3.570 +lemma max_0_1 [simp]:
   3.571 +  fixes max' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
   3.572 +  defines "max' \<equiv> max"
   3.573 +  shows
   3.574 +    "max' 0 1 = 1"
   3.575 +    "max' 1 0 = 1"
   3.576 +    "max' 0 (numeral x) = numeral x"
   3.577 +    "max' (numeral x) 0 = numeral x"
   3.578 +    "max' 1 (numeral x) = numeral x"
   3.579 +    "max' (numeral x) 1 = numeral x"
   3.580 +  by (simp_all add: max'_def max_def le_num_One_iff)
   3.581  
   3.582  end
   3.583  
   3.584 -subsubsection \<open>
   3.585 -  Multiplication and negation: class \<open>ring_1\<close>
   3.586 -\<close>
   3.587 +
   3.588 +subsubsection \<open>Multiplication and negation: class \<open>ring_1\<close>\<close>
   3.589  
   3.590  context ring_1
   3.591  begin
   3.592 @@ -681,20 +689,18 @@
   3.593    "- numeral m * - numeral n = numeral (m * n)"
   3.594    "- numeral m * numeral n = - numeral (m * n)"
   3.595    "numeral m * - numeral n = - numeral (m * n)"
   3.596 -  unfolding mult_minus_left mult_minus_right
   3.597 -  by (simp_all only: minus_minus numeral_mult)
   3.598 +  by (simp_all only: mult_minus_left mult_minus_right minus_minus numeral_mult)
   3.599  
   3.600  lemma mult_minus1 [simp]: "- 1 * z = - z"
   3.601 -  unfolding numeral.simps mult_minus_left by simp
   3.602 +  by (simp add: numeral.simps)
   3.603  
   3.604  lemma mult_minus1_right [simp]: "z * - 1 = - z"
   3.605 -  unfolding numeral.simps mult_minus_right by simp
   3.606 +  by (simp add: numeral.simps)
   3.607  
   3.608  end
   3.609  
   3.610 -subsubsection \<open>
   3.611 -  Equality using \<open>iszero\<close> for rings with non-zero characteristic
   3.612 -\<close>
   3.613 +
   3.614 +subsubsection \<open>Equality using \<open>iszero\<close> for rings with non-zero characteristic\<close>
   3.615  
   3.616  context ring_1
   3.617  begin
   3.618 @@ -717,23 +723,22 @@
   3.619  lemma not_iszero_neg_Numeral1: "\<not> iszero (- Numeral1)"
   3.620    by (simp add: numeral_One)
   3.621  
   3.622 -lemma iszero_neg_numeral [simp]:
   3.623 -  "iszero (- numeral w) \<longleftrightarrow> iszero (numeral w)"
   3.624 -  unfolding iszero_def
   3.625 -  by (rule neg_equal_0_iff_equal)
   3.626 +lemma iszero_neg_numeral [simp]: "iszero (- numeral w) \<longleftrightarrow> iszero (numeral w)"
   3.627 +  unfolding iszero_def by (rule neg_equal_0_iff_equal)
   3.628  
   3.629  lemma eq_iff_iszero_diff: "x = y \<longleftrightarrow> iszero (x - y)"
   3.630    unfolding iszero_def by (rule eq_iff_diff_eq_0)
   3.631  
   3.632 -text \<open>The \<open>eq_numeral_iff_iszero\<close> lemmas are not declared
   3.633 -\<open>[simp]\<close> by default, because for rings of characteristic zero,
   3.634 -better simp rules are possible. For a type like integers mod \<open>n\<close>, type-instantiated versions of these rules should be added to the
   3.635 -simplifier, along with a type-specific rule for deciding propositions
   3.636 -of the form \<open>iszero (numeral w)\<close>.
   3.637 +text \<open>
   3.638 +  The \<open>eq_numeral_iff_iszero\<close> lemmas are not declared \<open>[simp]\<close> by default,
   3.639 +  because for rings of characteristic zero, better simp rules are possible.
   3.640 +  For a type like integers mod \<open>n\<close>, type-instantiated versions of these rules
   3.641 +  should be added to the simplifier, along with a type-specific rule for
   3.642 +  deciding propositions of the form \<open>iszero (numeral w)\<close>.
   3.643  
   3.644 -bh: Maybe it would not be so bad to just declare these as simp
   3.645 -rules anyway? I should test whether these rules take precedence over
   3.646 -the \<open>ring_char_0\<close> rules in the simplifier.
   3.647 +  bh: Maybe it would not be so bad to just declare these as simp rules anyway?
   3.648 +  I should test whether these rules take precedence over the \<open>ring_char_0\<close>
   3.649 +  rules in the simplifier.
   3.650  \<close>
   3.651  
   3.652  lemma eq_numeral_iff_iszero:
   3.653 @@ -754,9 +759,8 @@
   3.654  
   3.655  end
   3.656  
   3.657 -subsubsection \<open>
   3.658 -  Equality and negation: class \<open>ring_char_0\<close>
   3.659 -\<close>
   3.660 +
   3.661 +subsubsection \<open>Equality and negation: class \<open>ring_char_0\<close>\<close>
   3.662  
   3.663  context ring_char_0
   3.664  begin
   3.665 @@ -768,17 +772,16 @@
   3.666    by simp
   3.667  
   3.668  lemma numeral_neq_neg_numeral: "numeral m \<noteq> - numeral n"
   3.669 -  unfolding eq_neg_iff_add_eq_0
   3.670 -  by (simp add: numeral_plus_numeral)
   3.671 +  by (simp add: eq_neg_iff_add_eq_0 numeral_plus_numeral)
   3.672  
   3.673  lemma neg_numeral_neq_numeral: "- numeral m \<noteq> numeral n"
   3.674    by (rule numeral_neq_neg_numeral [symmetric])
   3.675  
   3.676  lemma zero_neq_neg_numeral: "0 \<noteq> - numeral n"
   3.677 -  unfolding neg_0_equal_iff_equal by simp
   3.678 +  by simp
   3.679  
   3.680  lemma neg_numeral_neq_zero: "- numeral n \<noteq> 0"
   3.681 -  unfolding neg_equal_0_iff_equal by simp
   3.682 +  by simp
   3.683  
   3.684  lemma one_neq_neg_numeral: "1 \<noteq> - numeral n"
   3.685    using numeral_neq_neg_numeral [of One n] by (simp add: numeral_One)
   3.686 @@ -786,36 +789,28 @@
   3.687  lemma neg_numeral_neq_one: "- numeral n \<noteq> 1"
   3.688    using neg_numeral_neq_numeral [of n One] by (simp add: numeral_One)
   3.689  
   3.690 -lemma neg_one_neq_numeral:
   3.691 -  "- 1 \<noteq> numeral n"
   3.692 +lemma neg_one_neq_numeral: "- 1 \<noteq> numeral n"
   3.693    using neg_numeral_neq_numeral [of One n] by (simp add: numeral_One)
   3.694  
   3.695 -lemma numeral_neq_neg_one:
   3.696 -  "numeral n \<noteq> - 1"
   3.697 +lemma numeral_neq_neg_one: "numeral n \<noteq> - 1"
   3.698    using numeral_neq_neg_numeral [of n One] by (simp add: numeral_One)
   3.699  
   3.700 -lemma neg_one_eq_numeral_iff:
   3.701 -  "- 1 = - numeral n \<longleftrightarrow> n = One"
   3.702 +lemma neg_one_eq_numeral_iff: "- 1 = - numeral n \<longleftrightarrow> n = One"
   3.703    using neg_numeral_eq_iff [of One n] by (auto simp add: numeral_One)
   3.704  
   3.705 -lemma numeral_eq_neg_one_iff:
   3.706 -  "- numeral n = - 1 \<longleftrightarrow> n = One"
   3.707 +lemma numeral_eq_neg_one_iff: "- numeral n = - 1 \<longleftrightarrow> n = One"
   3.708    using neg_numeral_eq_iff [of n One] by (auto simp add: numeral_One)
   3.709  
   3.710 -lemma neg_one_neq_zero:
   3.711 -  "- 1 \<noteq> 0"
   3.712 +lemma neg_one_neq_zero: "- 1 \<noteq> 0"
   3.713    by simp
   3.714  
   3.715 -lemma zero_neq_neg_one:
   3.716 -  "0 \<noteq> - 1"
   3.717 +lemma zero_neq_neg_one: "0 \<noteq> - 1"
   3.718    by simp
   3.719  
   3.720 -lemma neg_one_neq_one:
   3.721 -  "- 1 \<noteq> 1"
   3.722 +lemma neg_one_neq_one: "- 1 \<noteq> 1"
   3.723    using neg_numeral_neq_numeral [of One One] by (simp only: numeral_One not_False_eq_True)
   3.724  
   3.725 -lemma one_neq_neg_one:
   3.726 -  "1 \<noteq> - 1"
   3.727 +lemma one_neq_neg_one: "1 \<noteq> - 1"
   3.728    using numeral_neq_neg_numeral [of One One] by (simp only: numeral_One not_False_eq_True)
   3.729  
   3.730  lemmas eq_neg_numeral_simps [simp] =
   3.731 @@ -831,9 +826,7 @@
   3.732  end
   3.733  
   3.734  
   3.735 -subsubsection \<open>
   3.736 -  Structures with negation and order: class \<open>linordered_idom\<close>
   3.737 -\<close>
   3.738 +subsubsection \<open>Structures with negation and order: class \<open>linordered_idom\<close>\<close>
   3.739  
   3.740  context linordered_idom
   3.741  begin
   3.742 @@ -869,7 +862,7 @@
   3.743  
   3.744  lemma not_numeral_le_neg_numeral: "\<not> numeral m \<le> - numeral n"
   3.745    by (simp only: not_le neg_numeral_less_numeral)
   3.746 -  
   3.747 +
   3.748  lemma neg_numeral_less_one: "- numeral m < 1"
   3.749    by (rule neg_numeral_less_numeral [of m One, unfolded numeral_One])
   3.750  
   3.751 @@ -906,20 +899,16 @@
   3.752  lemma not_neg_one_le_neg_numeral_iff: "\<not> - 1 \<le> - numeral m \<longleftrightarrow> m \<noteq> One"
   3.753    by (cases m) simp_all
   3.754  
   3.755 -lemma sub_non_negative:
   3.756 -  "sub n m \<ge> 0 \<longleftrightarrow> n \<ge> m"
   3.757 +lemma sub_non_negative: "sub n m \<ge> 0 \<longleftrightarrow> n \<ge> m"
   3.758    by (simp only: sub_def le_diff_eq) simp
   3.759  
   3.760 -lemma sub_positive:
   3.761 -  "sub n m > 0 \<longleftrightarrow> n > m"
   3.762 +lemma sub_positive: "sub n m > 0 \<longleftrightarrow> n > m"
   3.763    by (simp only: sub_def less_diff_eq) simp
   3.764  
   3.765 -lemma sub_non_positive:
   3.766 -  "sub n m \<le> 0 \<longleftrightarrow> n \<le> m"
   3.767 +lemma sub_non_positive: "sub n m \<le> 0 \<longleftrightarrow> n \<le> m"
   3.768    by (simp only: sub_def diff_le_eq) simp
   3.769  
   3.770 -lemma sub_negative:
   3.771 -  "sub n m < 0 \<longleftrightarrow> n < m"
   3.772 +lemma sub_negative: "sub n m < 0 \<longleftrightarrow> n < m"
   3.773    by (simp only: sub_def diff_less_eq) simp
   3.774  
   3.775  lemmas le_neg_numeral_simps [simp] =
   3.776 @@ -963,9 +952,8 @@
   3.777  
   3.778  end
   3.779  
   3.780 -subsubsection \<open>
   3.781 -  Natural numbers
   3.782 -\<close>
   3.783 +
   3.784 +subsubsection \<open>Natural numbers\<close>
   3.785  
   3.786  lemma Suc_1 [simp]: "Suc 1 = 2"
   3.787    unfolding Suc_eq_plus1 by (rule one_add_one)
   3.788 @@ -977,7 +965,7 @@
   3.789    where [code del]: "pred_numeral k = numeral k - 1"
   3.790  
   3.791  lemma numeral_eq_Suc: "numeral k = Suc (pred_numeral k)"
   3.792 -  unfolding pred_numeral_def by simp
   3.793 +  by (simp add: pred_numeral_def)
   3.794  
   3.795  lemma eval_nat_numeral:
   3.796    "numeral One = Suc 0"
   3.797 @@ -989,8 +977,7 @@
   3.798    "pred_numeral One = 0"
   3.799    "pred_numeral (Bit0 k) = numeral (BitM k)"
   3.800    "pred_numeral (Bit1 k) = numeral (Bit0 k)"
   3.801 -  unfolding pred_numeral_def eval_nat_numeral
   3.802 -  by (simp_all only: diff_Suc_Suc diff_0)
   3.803 +  by (simp_all only: pred_numeral_def eval_nat_numeral diff_Suc_Suc diff_0)
   3.804  
   3.805  lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
   3.806    by (simp add: eval_nat_numeral)
   3.807 @@ -1001,12 +988,11 @@
   3.808  lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
   3.809    by (simp only: numeral_One One_nat_def)
   3.810  
   3.811 -lemma Suc_nat_number_of_add:
   3.812 -  "Suc (numeral v + n) = numeral (v + One) + n"
   3.813 +lemma Suc_nat_number_of_add: "Suc (numeral v + n) = numeral (v + One) + n"
   3.814    by simp
   3.815  
   3.816 -(*Maps #n to n for n = 1, 2*)
   3.817 -lemmas numerals = numeral_One [where 'a=nat] numeral_2_eq_2
   3.818 +lemma numerals: "Numeral1 = (1::nat)" "2 = Suc (Suc 0)"
   3.819 +  by (rule numeral_One) (rule numeral_2_eq_2)
   3.820  
   3.821  text \<open>Comparisons involving @{term Suc}.\<close>
   3.822  
   3.823 @@ -1034,26 +1020,21 @@
   3.824  lemma diff_numeral_Suc [simp]: "numeral k - Suc n = pred_numeral k - n"
   3.825    by (simp add: numeral_eq_Suc)
   3.826  
   3.827 -lemma max_Suc_numeral [simp]:
   3.828 -  "max (Suc n) (numeral k) = Suc (max n (pred_numeral k))"
   3.829 +lemma max_Suc_numeral [simp]: "max (Suc n) (numeral k) = Suc (max n (pred_numeral k))"
   3.830    by (simp add: numeral_eq_Suc)
   3.831  
   3.832 -lemma max_numeral_Suc [simp]:
   3.833 -  "max (numeral k) (Suc n) = Suc (max (pred_numeral k) n)"
   3.834 +lemma max_numeral_Suc [simp]: "max (numeral k) (Suc n) = Suc (max (pred_numeral k) n)"
   3.835    by (simp add: numeral_eq_Suc)
   3.836  
   3.837 -lemma min_Suc_numeral [simp]:
   3.838 -  "min (Suc n) (numeral k) = Suc (min n (pred_numeral k))"
   3.839 +lemma min_Suc_numeral [simp]: "min (Suc n) (numeral k) = Suc (min n (pred_numeral k))"
   3.840    by (simp add: numeral_eq_Suc)
   3.841  
   3.842 -lemma min_numeral_Suc [simp]:
   3.843 -  "min (numeral k) (Suc n) = Suc (min (pred_numeral k) n)"
   3.844 +lemma min_numeral_Suc [simp]: "min (numeral k) (Suc n) = Suc (min (pred_numeral k) n)"
   3.845    by (simp add: numeral_eq_Suc)
   3.846  
   3.847  text \<open>For @{term case_nat} and @{term rec_nat}.\<close>
   3.848  
   3.849 -lemma case_nat_numeral [simp]:
   3.850 -  "case_nat a f (numeral v) = (let pv = pred_numeral v in f pv)"
   3.851 +lemma case_nat_numeral [simp]: "case_nat a f (numeral v) = (let pv = pred_numeral v in f pv)"
   3.852    by (simp add: numeral_eq_Suc)
   3.853  
   3.854  lemma case_nat_add_eq_if [simp]:
   3.855 @@ -1061,21 +1042,18 @@
   3.856    by (simp add: numeral_eq_Suc)
   3.857  
   3.858  lemma rec_nat_numeral [simp]:
   3.859 -  "rec_nat a f (numeral v) =
   3.860 -    (let pv = pred_numeral v in f pv (rec_nat a f pv))"
   3.861 +  "rec_nat a f (numeral v) = (let pv = pred_numeral v in f pv (rec_nat a f pv))"
   3.862    by (simp add: numeral_eq_Suc Let_def)
   3.863  
   3.864  lemma rec_nat_add_eq_if [simp]:
   3.865 -  "rec_nat a f (numeral v + n) =
   3.866 -    (let pv = pred_numeral v in f (pv + n) (rec_nat a f (pv + n)))"
   3.867 +  "rec_nat a f (numeral v + n) = (let pv = pred_numeral v in f (pv + n) (rec_nat a f (pv + n)))"
   3.868    by (simp add: numeral_eq_Suc Let_def)
   3.869  
   3.870 -text \<open>Case analysis on @{term "n < 2"}\<close>
   3.871 -
   3.872 +text \<open>Case analysis on @{term "n < 2"}.\<close>
   3.873  lemma less_2_cases: "n < 2 \<Longrightarrow> n = 0 \<or> n = Suc 0"
   3.874    by (auto simp add: numeral_2_eq_2)
   3.875  
   3.876 -text \<open>Removal of Small Numerals: 0, 1 and (in additive positions) 2\<close>
   3.877 +text \<open>Removal of Small Numerals: 0, 1 and (in additive positions) 2.\<close>
   3.878  text \<open>bh: Are these rules really a good idea?\<close>
   3.879  
   3.880  lemma add_2_eq_Suc [simp]: "2 + n = Suc (Suc n)"
   3.881 @@ -1085,7 +1063,6 @@
   3.882    by simp
   3.883  
   3.884  text \<open>Can be used to eliminate long strings of Sucs, but not by default.\<close>
   3.885 -
   3.886  lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n"
   3.887    by simp
   3.888  
   3.889 @@ -1099,12 +1076,10 @@
   3.890  
   3.891  subclass field_char_0 ..
   3.892  
   3.893 -lemma half_gt_zero_iff:
   3.894 -  "0 < a / 2 \<longleftrightarrow> 0 < a" (is "?P \<longleftrightarrow> ?Q")
   3.895 +lemma half_gt_zero_iff: "0 < a / 2 \<longleftrightarrow> 0 < a"
   3.896    by (auto simp add: field_simps)
   3.897  
   3.898 -lemma half_gt_zero [simp]:
   3.899 -  "0 < a \<Longrightarrow> 0 < a / 2"
   3.900 +lemma half_gt_zero [simp]: "0 < a \<Longrightarrow> 0 < a / 2"
   3.901    by (simp add: half_gt_zero_iff)
   3.902  
   3.903  end
   3.904 @@ -1124,50 +1099,52 @@
   3.905  
   3.906  subsection \<open>Setting up simprocs\<close>
   3.907  
   3.908 -lemma mult_numeral_1: "Numeral1 * a = (a::'a::semiring_numeral)"
   3.909 +lemma mult_numeral_1: "Numeral1 * a = a"
   3.910 +  for a :: "'a::semiring_numeral"
   3.911    by simp
   3.912  
   3.913 -lemma mult_numeral_1_right: "a * Numeral1 = (a::'a::semiring_numeral)"
   3.914 +lemma mult_numeral_1_right: "a * Numeral1 = a"
   3.915 +  for a :: "'a::semiring_numeral"
   3.916    by simp
   3.917  
   3.918 -lemma divide_numeral_1: "a / Numeral1 = (a::'a::field)"
   3.919 +lemma divide_numeral_1: "a / Numeral1 = a"
   3.920 +  for a :: "'a::field"
   3.921    by simp
   3.922  
   3.923 -lemma inverse_numeral_1:
   3.924 -  "inverse Numeral1 = (Numeral1::'a::division_ring)"
   3.925 +lemma inverse_numeral_1: "inverse Numeral1 = (Numeral1::'a::division_ring)"
   3.926    by simp
   3.927  
   3.928 -text\<open>Theorem lists for the cancellation simprocs. The use of a binary
   3.929 -numeral for 1 reduces the number of special cases.\<close>
   3.930 +text \<open>
   3.931 +  Theorem lists for the cancellation simprocs. The use of a binary
   3.932 +  numeral for 1 reduces the number of special cases.
   3.933 +\<close>
   3.934  
   3.935  lemma mult_1s:
   3.936 -  fixes a :: "'a::semiring_numeral"
   3.937 -    and b :: "'b::ring_1"
   3.938 -  shows "Numeral1 * a = a"
   3.939 -    "a * Numeral1 = a"
   3.940 -    "- Numeral1 * b = - b"
   3.941 -    "b * - Numeral1 = - b"
   3.942 +  "Numeral1 * a = a"
   3.943 +  "a * Numeral1 = a"
   3.944 +  "- Numeral1 * b = - b"
   3.945 +  "b * - Numeral1 = - b"
   3.946 +  for a :: "'a::semiring_numeral" and b :: "'b::ring_1"
   3.947    by simp_all
   3.948  
   3.949  setup \<open>
   3.950    Reorient_Proc.add
   3.951      (fn Const (@{const_name numeral}, _) $ _ => true
   3.952 -    | Const (@{const_name uminus}, _) $ (Const (@{const_name numeral}, _) $ _) => true
   3.953 -    | _ => false)
   3.954 +      | Const (@{const_name uminus}, _) $ (Const (@{const_name numeral}, _) $ _) => true
   3.955 +      | _ => false)
   3.956  \<close>
   3.957  
   3.958 -simproc_setup reorient_numeral
   3.959 -  ("numeral w = x" | "- numeral w = y") = Reorient_Proc.proc
   3.960 +simproc_setup reorient_numeral ("numeral w = x" | "- numeral w = y") =
   3.961 +  Reorient_Proc.proc
   3.962  
   3.963  
   3.964 -subsubsection \<open>Simplification of arithmetic operations on integer constants.\<close>
   3.965 +subsubsection \<open>Simplification of arithmetic operations on integer constants\<close>
   3.966  
   3.967  lemmas arith_special = (* already declared simp above *)
   3.968    add_numeral_special add_neg_numeral_special
   3.969    diff_numeral_special
   3.970  
   3.971 -(* rules already in simpset *)
   3.972 -lemmas arith_extra_simps =
   3.973 +lemmas arith_extra_simps = (* rules already in simpset *)
   3.974    numeral_plus_numeral add_neg_numeral_simps add_0_left add_0_right
   3.975    minus_zero
   3.976    diff_numeral_simps diff_0 diff_0_right
   3.977 @@ -1195,7 +1172,7 @@
   3.978  lemmas of_nat_simps =
   3.979    of_nat_0 of_nat_1 of_nat_Suc of_nat_add of_nat_mult
   3.980  
   3.981 -text \<open>Simplification of relational operations\<close>
   3.982 +text \<open>Simplification of relational operations.\<close>
   3.983  
   3.984  lemmas eq_numeral_extra =
   3.985    zero_neq_one one_neq_zero
   3.986 @@ -1215,34 +1192,28 @@
   3.987    unfolding Let_def ..
   3.988  
   3.989  declaration \<open>
   3.990 -let 
   3.991 +let
   3.992    fun number_of ctxt T n =
   3.993      if not (Sign.of_sort (Proof_Context.theory_of ctxt) (T, @{sort numeral}))
   3.994      then raise CTERM ("number_of", [])
   3.995      else Numeral.mk_cnumber (Thm.ctyp_of ctxt T) n;
   3.996  in
   3.997    K (
   3.998 -    Lin_Arith.add_simps (@{thms arith_simps} @ @{thms more_arith_simps}
   3.999 -      @ @{thms rel_simps}
  3.1000 -      @ @{thms pred_numeral_simps}
  3.1001 -      @ @{thms arith_special numeral_One}
  3.1002 -      @ @{thms of_nat_simps})
  3.1003 -    #> Lin_Arith.add_simps [@{thm Suc_numeral},
  3.1004 -      @{thm Let_numeral}, @{thm Let_neg_numeral}, @{thm Let_0}, @{thm Let_1},
  3.1005 -      @{thm le_Suc_numeral}, @{thm le_numeral_Suc},
  3.1006 -      @{thm less_Suc_numeral}, @{thm less_numeral_Suc},
  3.1007 -      @{thm Suc_eq_numeral}, @{thm eq_numeral_Suc},
  3.1008 -      @{thm mult_Suc}, @{thm mult_Suc_right},
  3.1009 -      @{thm of_nat_numeral}]
  3.1010 +    Lin_Arith.add_simps
  3.1011 +      @{thms arith_simps more_arith_simps rel_simps pred_numeral_simps
  3.1012 +        arith_special numeral_One of_nat_simps}
  3.1013 +    #> Lin_Arith.add_simps
  3.1014 +      @{thms Suc_numeral Let_numeral Let_neg_numeral Let_0 Let_1
  3.1015 +        le_Suc_numeral le_numeral_Suc less_Suc_numeral less_numeral_Suc
  3.1016 +        Suc_eq_numeral eq_numeral_Suc mult_Suc mult_Suc_right of_nat_numeral}
  3.1017      #> Lin_Arith.set_number_of number_of)
  3.1018  end
  3.1019  \<close>
  3.1020  
  3.1021  
  3.1022 -subsubsection \<open>Simplification of arithmetic when nested to the right.\<close>
  3.1023 +subsubsection \<open>Simplification of arithmetic when nested to the right\<close>
  3.1024  
  3.1025 -lemma add_numeral_left [simp]:
  3.1026 -  "numeral v + (numeral w + z) = (numeral(v + w) + z)"
  3.1027 +lemma add_numeral_left [simp]: "numeral v + (numeral w + z) = (numeral(v + w) + z)"
  3.1028    by (simp_all add: add.assoc [symmetric])
  3.1029  
  3.1030  lemma add_neg_numeral_left [simp]:
  3.1031 @@ -1261,7 +1232,7 @@
  3.1032  hide_const (open) One Bit0 Bit1 BitM inc pow sqr sub dbl dbl_inc dbl_dec
  3.1033  
  3.1034  
  3.1035 -subsection \<open>code module namespace\<close>
  3.1036 +subsection \<open>Code module namespace\<close>
  3.1037  
  3.1038  code_identifier
  3.1039    code_module Num \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
     4.1 --- a/src/HOL/Parity.thy	Wed Aug 10 22:05:00 2016 +0200
     4.2 +++ b/src/HOL/Parity.thy	Wed Aug 10 22:05:36 2016 +0200
     4.3 @@ -6,7 +6,7 @@
     4.4  section \<open>Parity in rings and semirings\<close>
     4.5  
     4.6  theory Parity
     4.7 -imports Nat_Transfer
     4.8 +  imports Nat_Transfer
     4.9  begin
    4.10  
    4.11  subsection \<open>Ring structures with parity and \<open>even\<close>/\<open>odd\<close> predicates\<close>
    4.12 @@ -21,19 +21,15 @@
    4.13  subclass semiring_numeral ..
    4.14  
    4.15  abbreviation even :: "'a \<Rightarrow> bool"
    4.16 -where
    4.17 -  "even a \<equiv> 2 dvd a"
    4.18 +  where "even a \<equiv> 2 dvd a"
    4.19  
    4.20  abbreviation odd :: "'a \<Rightarrow> bool"
    4.21 -where
    4.22 -  "odd a \<equiv> \<not> 2 dvd a"
    4.23 +  where "odd a \<equiv> \<not> 2 dvd a"
    4.24  
    4.25 -lemma even_zero [simp]:
    4.26 -  "even 0"
    4.27 +lemma even_zero [simp]: "even 0"
    4.28    by (fact dvd_0_right)
    4.29  
    4.30 -lemma even_plus_one_iff [simp]:
    4.31 -  "even (a + 1) \<longleftrightarrow> odd a"
    4.32 +lemma even_plus_one_iff [simp]: "even (a + 1) \<longleftrightarrow> odd a"
    4.33    by (auto simp add: dvd_add_right_iff intro: odd_even_add)
    4.34  
    4.35  lemma evenE [elim?]:
    4.36 @@ -53,13 +49,11 @@
    4.37    with * have "a = 2 * c + 1" by simp
    4.38    with that show thesis .
    4.39  qed
    4.40 - 
    4.41 -lemma even_times_iff [simp]:
    4.42 -  "even (a * b) \<longleftrightarrow> even a \<or> even b"
    4.43 +
    4.44 +lemma even_times_iff [simp]: "even (a * b) \<longleftrightarrow> even a \<or> even b"
    4.45    by (auto dest: even_multD)
    4.46  
    4.47 -lemma even_numeral [simp]:
    4.48 -  "even (numeral (Num.Bit0 n))"
    4.49 +lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))"
    4.50  proof -
    4.51    have "even (2 * numeral n)"
    4.52      unfolding even_times_iff by simp
    4.53 @@ -69,8 +63,7 @@
    4.54      unfolding numeral.simps .
    4.55  qed
    4.56  
    4.57 -lemma odd_numeral [simp]:
    4.58 -  "odd (numeral (Num.Bit1 n))"
    4.59 +lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))"
    4.60  proof
    4.61    assume "even (numeral (num.Bit1 n))"
    4.62    then have "even (numeral n + numeral n + 1)"
    4.63 @@ -79,22 +72,18 @@
    4.64      unfolding mult_2 .
    4.65    then have "2 dvd numeral n * 2 + 1"
    4.66      by (simp add: ac_simps)
    4.67 -  with dvd_add_times_triv_left_iff [of 2 "numeral n" 1]
    4.68 -    have "2 dvd 1"
    4.69 -    by simp
    4.70 +  then have "2 dvd 1"
    4.71 +    using dvd_add_times_triv_left_iff [of 2 "numeral n" 1] by simp
    4.72    then show False by simp
    4.73  qed
    4.74  
    4.75 -lemma even_add [simp]:
    4.76 -  "even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"
    4.77 +lemma even_add [simp]: "even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"
    4.78    by (auto simp add: dvd_add_right_iff dvd_add_left_iff odd_even_add)
    4.79  
    4.80 -lemma odd_add [simp]:
    4.81 -  "odd (a + b) \<longleftrightarrow> (\<not> (odd a \<longleftrightarrow> odd b))"
    4.82 +lemma odd_add [simp]: "odd (a + b) \<longleftrightarrow> (\<not> (odd a \<longleftrightarrow> odd b))"
    4.83    by simp
    4.84  
    4.85 -lemma even_power [simp]:
    4.86 -  "even (a ^ n) \<longleftrightarrow> even a \<and> n > 0"
    4.87 +lemma even_power [simp]: "even (a ^ n) \<longleftrightarrow> even a \<and> n > 0"
    4.88    by (induct n) auto
    4.89  
    4.90  end
    4.91 @@ -104,12 +93,10 @@
    4.92  
    4.93  subclass comm_ring_1 ..
    4.94  
    4.95 -lemma even_minus [simp]:
    4.96 -  "even (- a) \<longleftrightarrow> even a"
    4.97 +lemma even_minus [simp]: "even (- a) \<longleftrightarrow> even a"
    4.98    by (fact dvd_minus_iff)
    4.99  
   4.100 -lemma even_diff [simp]:
   4.101 -  "even (a - b) \<longleftrightarrow> even (a + b)"
   4.102 +lemma even_diff [simp]: "even (a - b) \<longleftrightarrow> even (a + b)"
   4.103    using even_add [of a "- b"] by simp
   4.104  
   4.105  end
   4.106 @@ -117,17 +104,14 @@
   4.107  
   4.108  subsection \<open>Instances for @{typ nat} and @{typ int}\<close>
   4.109  
   4.110 -lemma even_Suc_Suc_iff [simp]:
   4.111 -  "2 dvd Suc (Suc n) \<longleftrightarrow> 2 dvd n"
   4.112 +lemma even_Suc_Suc_iff [simp]: "2 dvd Suc (Suc n) \<longleftrightarrow> 2 dvd n"
   4.113    using dvd_add_triv_right_iff [of 2 n] by simp
   4.114  
   4.115 -lemma even_Suc [simp]:
   4.116 -  "2 dvd Suc n \<longleftrightarrow> \<not> 2 dvd n"
   4.117 +lemma even_Suc [simp]: "2 dvd Suc n \<longleftrightarrow> \<not> 2 dvd n"
   4.118    by (induct n) auto
   4.119  
   4.120 -lemma even_diff_nat [simp]:
   4.121 -  fixes m n :: nat
   4.122 -  shows "2 dvd (m - n) \<longleftrightarrow> m < n \<or> 2 dvd (m + n)"
   4.123 +lemma even_diff_nat [simp]: "2 dvd (m - n) \<longleftrightarrow> m < n \<or> 2 dvd (m + n)"
   4.124 +  for m n :: nat
   4.125  proof (cases "n \<le> m")
   4.126    case True
   4.127    then have "m - n + n * 2 = m + n" by (simp add: mult_2_right)
   4.128 @@ -137,8 +121,8 @@
   4.129  next
   4.130    case False
   4.131    then show ?thesis by simp
   4.132 -qed 
   4.133 -  
   4.134 +qed
   4.135 +
   4.136  instance nat :: semiring_parity
   4.137  proof
   4.138    show "\<not> 2 dvd (1 :: nat)"
   4.139 @@ -165,7 +149,8 @@
   4.140      then obtain r where "Suc n = 2 * r" ..
   4.141      moreover from * obtain s where "m * n = 2 * s" ..
   4.142      then have "2 * s + m = m * Suc n" by simp
   4.143 -    ultimately have " 2 * s + m = 2 * (m * r)" by (simp add: algebra_simps)
   4.144 +    ultimately have " 2 * s + m = 2 * (m * r)"
   4.145 +      by (simp add: algebra_simps)
   4.146      then have "m = 2 * (m * r - s)" by simp
   4.147      then show "2 dvd m" ..
   4.148    qed
   4.149 @@ -176,13 +161,12 @@
   4.150      by (cases n) simp_all
   4.151  qed
   4.152  
   4.153 -lemma odd_pos: 
   4.154 -  "odd (n :: nat) \<Longrightarrow> 0 < n"
   4.155 +lemma odd_pos: "odd n \<Longrightarrow> 0 < n"
   4.156 +  for n :: nat
   4.157    by (auto elim: oddE)
   4.158  
   4.159 -lemma Suc_double_not_eq_double:
   4.160 -  fixes m n :: nat
   4.161 -  shows "Suc (2 * m) \<noteq> 2 * n"
   4.162 +lemma Suc_double_not_eq_double: "Suc (2 * m) \<noteq> 2 * n"
   4.163 +  for m n :: nat
   4.164  proof
   4.165    assume "Suc (2 * m) = 2 * n"
   4.166    moreover have "odd (Suc (2 * m))" and "even (2 * n)"
   4.167 @@ -190,37 +174,34 @@
   4.168    ultimately show False by simp
   4.169  qed
   4.170  
   4.171 -lemma double_not_eq_Suc_double:
   4.172 -  fixes m n :: nat
   4.173 -  shows "2 * m \<noteq> Suc (2 * n)"
   4.174 +lemma double_not_eq_Suc_double: "2 * m \<noteq> Suc (2 * n)"
   4.175 +  for m n :: nat
   4.176    using Suc_double_not_eq_double [of n m] by simp
   4.177  
   4.178 -lemma even_diff_iff [simp]:
   4.179 -  fixes k l :: int
   4.180 -  shows "2 dvd (k - l) \<longleftrightarrow> 2 dvd (k + l)"
   4.181 +lemma even_diff_iff [simp]: "2 dvd (k - l) \<longleftrightarrow> 2 dvd (k + l)"
   4.182 +  for k l :: int
   4.183    using dvd_add_times_triv_right_iff [of 2 "k - l" l] by (simp add: mult_2_right)
   4.184  
   4.185 -lemma even_abs_add_iff [simp]:
   4.186 -  fixes k l :: int
   4.187 -  shows "2 dvd (\<bar>k\<bar> + l) \<longleftrightarrow> 2 dvd (k + l)"
   4.188 +lemma even_abs_add_iff [simp]: "2 dvd (\<bar>k\<bar> + l) \<longleftrightarrow> 2 dvd (k + l)"
   4.189 +  for k l :: int
   4.190    by (cases "k \<ge> 0") (simp_all add: ac_simps)
   4.191  
   4.192 -lemma even_add_abs_iff [simp]:
   4.193 -  fixes k l :: int
   4.194 -  shows "2 dvd (k + \<bar>l\<bar>) \<longleftrightarrow> 2 dvd (k + l)"
   4.195 +lemma even_add_abs_iff [simp]: "2 dvd (k + \<bar>l\<bar>) \<longleftrightarrow> 2 dvd (k + l)"
   4.196 +  for k l :: int
   4.197    using even_abs_add_iff [of l k] by (simp add: ac_simps)
   4.198  
   4.199 -lemma odd_Suc_minus_one [simp]:
   4.200 -  "odd n \<Longrightarrow> Suc (n - Suc 0) = n"
   4.201 +lemma odd_Suc_minus_one [simp]: "odd n \<Longrightarrow> Suc (n - Suc 0) = n"
   4.202    by (auto elim: oddE)
   4.203  
   4.204  instance int :: ring_parity
   4.205  proof
   4.206 -  show "\<not> 2 dvd (1 :: int)" by (simp add: dvd_int_unfold_dvd_nat)
   4.207 +  show "\<not> 2 dvd (1 :: int)"
   4.208 +    by (simp add: dvd_int_unfold_dvd_nat)
   4.209 +next
   4.210    fix k l :: int
   4.211    assume "\<not> 2 dvd k"
   4.212    moreover assume "\<not> 2 dvd l"
   4.213 -  ultimately have "2 dvd (nat \<bar>k\<bar> + nat \<bar>l\<bar>)" 
   4.214 +  ultimately have "2 dvd (nat \<bar>k\<bar> + nat \<bar>l\<bar>)"
   4.215      by (auto simp add: dvd_int_unfold_dvd_nat intro: odd_even_add)
   4.216    then have "2 dvd (\<bar>k\<bar> + \<bar>l\<bar>)"
   4.217      by (simp add: dvd_int_unfold_dvd_nat nat_add_distrib)
   4.218 @@ -237,12 +218,10 @@
   4.219    then show "\<exists>l. k = l + 1" ..
   4.220  qed
   4.221  
   4.222 -lemma even_int_iff [simp]:
   4.223 -  "even (int n) \<longleftrightarrow> even n"
   4.224 +lemma even_int_iff [simp]: "even (int n) \<longleftrightarrow> even n"
   4.225    by (simp add: dvd_int_iff)
   4.226  
   4.227 -lemma even_nat_iff:
   4.228 -  "0 \<le> k \<Longrightarrow> even (nat k) \<longleftrightarrow> even k"
   4.229 +lemma even_nat_iff: "0 \<le> k \<Longrightarrow> even (nat k) \<longleftrightarrow> even k"
   4.230    by (simp add: even_int_iff [symmetric])
   4.231  
   4.232  
   4.233 @@ -251,58 +230,47 @@
   4.234  context ring_1
   4.235  begin
   4.236  
   4.237 -lemma power_minus_even [simp]:
   4.238 -  "even n \<Longrightarrow> (- a) ^ n = a ^ n"
   4.239 +lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n"
   4.240    by (auto elim: evenE)
   4.241  
   4.242 -lemma power_minus_odd [simp]:
   4.243 -  "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"
   4.244 +lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"
   4.245    by (auto elim: oddE)
   4.246  
   4.247 -lemma neg_one_even_power [simp]:
   4.248 -  "even n \<Longrightarrow> (- 1) ^ n = 1"
   4.249 +lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
   4.250    by simp
   4.251  
   4.252 -lemma neg_one_odd_power [simp]:
   4.253 -  "odd n \<Longrightarrow> (- 1) ^ n = - 1"
   4.254 +lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
   4.255    by simp
   4.256  
   4.257 -end  
   4.258 +end
   4.259  
   4.260  context linordered_idom
   4.261  begin
   4.262  
   4.263 -lemma zero_le_even_power:
   4.264 -  "even n \<Longrightarrow> 0 \<le> a ^ n"
   4.265 +lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n"
   4.266    by (auto elim: evenE)
   4.267  
   4.268 -lemma zero_le_odd_power:
   4.269 -  "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a"
   4.270 +lemma zero_le_odd_power: "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a"
   4.271    by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE)
   4.272  
   4.273 -lemma zero_le_power_eq:
   4.274 -  "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a"
   4.275 +lemma zero_le_power_eq: "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a"
   4.276    by (auto simp add: zero_le_even_power zero_le_odd_power)
   4.277 -  
   4.278 -lemma zero_less_power_eq:
   4.279 -  "0 < a ^ n \<longleftrightarrow> n = 0 \<or> even n \<and> a \<noteq> 0 \<or> odd n \<and> 0 < a"
   4.280 +
   4.281 +lemma zero_less_power_eq: "0 < a ^ n \<longleftrightarrow> n = 0 \<or> even n \<and> a \<noteq> 0 \<or> odd n \<and> 0 < a"
   4.282  proof -
   4.283    have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"
   4.284      unfolding power_eq_0_iff [of a n, symmetric] by blast
   4.285    show ?thesis
   4.286 -  unfolding less_le zero_le_power_eq by auto
   4.287 +    unfolding less_le zero_le_power_eq by auto
   4.288  qed
   4.289  
   4.290 -lemma power_less_zero_eq [simp]:
   4.291 -  "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0"
   4.292 +lemma power_less_zero_eq [simp]: "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0"
   4.293    unfolding not_le [symmetric] zero_le_power_eq by auto
   4.294 -  
   4.295 -lemma power_le_zero_eq:
   4.296 -  "a ^ n \<le> 0 \<longleftrightarrow> n > 0 \<and> (odd n \<and> a \<le> 0 \<or> even n \<and> a = 0)"
   4.297 -  unfolding not_less [symmetric] zero_less_power_eq by auto 
   4.298  
   4.299 -lemma power_even_abs:
   4.300 -  "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n"
   4.301 +lemma power_le_zero_eq: "a ^ n \<le> 0 \<longleftrightarrow> n > 0 \<and> (odd n \<and> a \<le> 0 \<or> even n \<and> a = 0)"
   4.302 +  unfolding not_less [symmetric] zero_less_power_eq by auto
   4.303 +
   4.304 +lemma power_even_abs: "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n"
   4.305    using power_abs [of a n] by (simp add: zero_le_even_power)
   4.306  
   4.307  lemma power_mono_even:
   4.308 @@ -310,30 +278,35 @@
   4.309    shows "a ^ n \<le> b ^ n"
   4.310  proof -
   4.311    have "0 \<le> \<bar>a\<bar>" by auto
   4.312 -  with \<open>\<bar>a\<bar> \<le> \<bar>b\<bar>\<close>
   4.313 -  have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n" by (rule power_mono)
   4.314 -  with \<open>even n\<close> show ?thesis by (simp add: power_even_abs)  
   4.315 +  with \<open>\<bar>a\<bar> \<le> \<bar>b\<bar>\<close> have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n"
   4.316 +    by (rule power_mono)
   4.317 +  with \<open>even n\<close> show ?thesis
   4.318 +    by (simp add: power_even_abs)
   4.319  qed
   4.320  
   4.321  lemma power_mono_odd:
   4.322    assumes "odd n" and "a \<le> b"
   4.323    shows "a ^ n \<le> b ^ n"
   4.324  proof (cases "b < 0")
   4.325 -  case True with \<open>a \<le> b\<close> have "- b \<le> - a" and "0 \<le> - b" by auto
   4.326 -  hence "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)
   4.327 +  case True
   4.328 +  with \<open>a \<le> b\<close> have "- b \<le> - a" and "0 \<le> - b" by auto
   4.329 +  then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)
   4.330    with \<open>odd n\<close> show ?thesis by simp
   4.331  next
   4.332 -  case False then have "0 \<le> b" by auto
   4.333 +  case False
   4.334 +  then have "0 \<le> b" by auto
   4.335    show ?thesis
   4.336    proof (cases "a < 0")
   4.337 -    case True then have "n \<noteq> 0" and "a \<le> 0" using \<open>odd n\<close> [THEN odd_pos] by auto
   4.338 +    case True
   4.339 +    then have "n \<noteq> 0" and "a \<le> 0" using \<open>odd n\<close> [THEN odd_pos] by auto
   4.340      then have "a ^ n \<le> 0" unfolding power_le_zero_eq using \<open>odd n\<close> by auto
   4.341 -    moreover
   4.342 -    from \<open>0 \<le> b\<close> have "0 \<le> b ^ n" by auto
   4.343 +    moreover from \<open>0 \<le> b\<close> have "0 \<le> b ^ n" by auto
   4.344      ultimately show ?thesis by auto
   4.345    next
   4.346 -    case False then have "0 \<le> a" by auto
   4.347 -    with \<open>a \<le> b\<close> show ?thesis using power_mono by auto
   4.348 +    case False
   4.349 +    then have "0 \<le> a" by auto
   4.350 +    with \<open>a \<le> b\<close> show ?thesis
   4.351 +      using power_mono by auto
   4.352    qed
   4.353  qed
   4.354  
   4.355 @@ -347,13 +320,16 @@
   4.356    by (fact zero_le_power_eq)
   4.357  
   4.358  lemma zero_less_power_eq_numeral [simp]:
   4.359 -  "0 < a ^ numeral w \<longleftrightarrow> numeral w = (0 :: nat)
   4.360 -    \<or> even (numeral w :: nat) \<and> a \<noteq> 0 \<or> odd (numeral w :: nat) \<and> 0 < a"
   4.361 +  "0 < a ^ numeral w \<longleftrightarrow>
   4.362 +    numeral w = (0 :: nat) \<or>
   4.363 +    even (numeral w :: nat) \<and> a \<noteq> 0 \<or>
   4.364 +    odd (numeral w :: nat) \<and> 0 < a"
   4.365    by (fact zero_less_power_eq)
   4.366  
   4.367  lemma power_le_zero_eq_numeral [simp]:
   4.368 -  "a ^ numeral w \<le> 0 \<longleftrightarrow> (0 :: nat) < numeral w
   4.369 -    \<and> (odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)"
   4.370 +  "a ^ numeral w \<le> 0 \<longleftrightarrow>
   4.371 +    (0 :: nat) < numeral w \<and>
   4.372 +    (odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)"
   4.373    by (fact power_le_zero_eq)
   4.374  
   4.375  lemma power_less_zero_eq_numeral [simp]:
   4.376 @@ -367,10 +343,8 @@
   4.377  end
   4.378  
   4.379  
   4.380 -subsubsection \<open>Tools setup\<close>
   4.381 +subsubsection \<open>Tool setup\<close>
   4.382  
   4.383 -declare transfer_morphism_int_nat [transfer add return:
   4.384 -  even_int_iff
   4.385 -]
   4.386 +declare transfer_morphism_int_nat [transfer add return: even_int_iff]
   4.387  
   4.388  end
     5.1 --- a/src/HOL/Power.thy	Wed Aug 10 22:05:00 2016 +0200
     5.2 +++ b/src/HOL/Power.thy	Wed Aug 10 22:05:36 2016 +0200
     5.3 @@ -6,7 +6,7 @@
     5.4  section \<open>Exponentiation\<close>
     5.5  
     5.6  theory Power
     5.7 -imports Num
     5.8 +  imports Num
     5.9  begin
    5.10  
    5.11  subsection \<open>Powers for Arbitrary Monoids\<close>
    5.12 @@ -15,9 +15,9 @@
    5.13  begin
    5.14  
    5.15  primrec power :: "'a \<Rightarrow> nat \<Rightarrow> 'a"  (infixr "^" 80)
    5.16 -where
    5.17 -  power_0: "a ^ 0 = 1"
    5.18 -| power_Suc: "a ^ Suc n = a * a ^ n"
    5.19 +  where
    5.20 +    power_0: "a ^ 0 = 1"
    5.21 +  | power_Suc: "a ^ Suc n = a * a ^ n"
    5.22  
    5.23  notation (latex output)
    5.24    power ("(_\<^bsup>_\<^esup>)" [1000] 1000)
    5.25 @@ -33,32 +33,25 @@
    5.26  
    5.27  subclass power .
    5.28  
    5.29 -lemma power_one [simp]:
    5.30 -  "1 ^ n = 1"
    5.31 +lemma power_one [simp]: "1 ^ n = 1"
    5.32    by (induct n) simp_all
    5.33  
    5.34 -lemma power_one_right [simp]:
    5.35 -  "a ^ 1 = a"
    5.36 +lemma power_one_right [simp]: "a ^ 1 = a"
    5.37    by simp
    5.38  
    5.39 -lemma power_Suc0_right [simp]:
    5.40 -  "a ^ Suc 0 = a"
    5.41 +lemma power_Suc0_right [simp]: "a ^ Suc 0 = a"
    5.42    by simp
    5.43  
    5.44 -lemma power_commutes:
    5.45 -  "a ^ n * a = a * a ^ n"
    5.46 +lemma power_commutes: "a ^ n * a = a * a ^ n"
    5.47    by (induct n) (simp_all add: mult.assoc)
    5.48  
    5.49 -lemma power_Suc2:
    5.50 -  "a ^ Suc n = a ^ n * a"
    5.51 +lemma power_Suc2: "a ^ Suc n = a ^ n * a"
    5.52    by (simp add: power_commutes)
    5.53  
    5.54 -lemma power_add:
    5.55 -  "a ^ (m + n) = a ^ m * a ^ n"
    5.56 +lemma power_add: "a ^ (m + n) = a ^ m * a ^ n"
    5.57    by (induct m) (simp_all add: algebra_simps)
    5.58  
    5.59 -lemma power_mult:
    5.60 -  "a ^ (m * n) = (a ^ m) ^ n"
    5.61 +lemma power_mult: "a ^ (m * n) = (a ^ m) ^ n"
    5.62    by (induct n) (simp_all add: power_add)
    5.63  
    5.64  lemma power2_eq_square: "a\<^sup>2 = a * a"
    5.65 @@ -67,51 +60,49 @@
    5.66  lemma power3_eq_cube: "a ^ 3 = a * a * a"
    5.67    by (simp add: numeral_3_eq_3 mult.assoc)
    5.68  
    5.69 -lemma power_even_eq:
    5.70 -  "a ^ (2 * n) = (a ^ n)\<^sup>2"
    5.71 +lemma power_even_eq: "a ^ (2 * n) = (a ^ n)\<^sup>2"
    5.72    by (subst mult.commute) (simp add: power_mult)
    5.73  
    5.74 -lemma power_odd_eq:
    5.75 -  "a ^ Suc (2*n) = a * (a ^ n)\<^sup>2"
    5.76 +lemma power_odd_eq: "a ^ Suc (2*n) = a * (a ^ n)\<^sup>2"
    5.77    by (simp add: power_even_eq)
    5.78  
    5.79 -lemma power_numeral_even:
    5.80 -  "z ^ numeral (Num.Bit0 w) = (let w = z ^ (numeral w) in w * w)"
    5.81 -  unfolding numeral_Bit0 power_add Let_def ..
    5.82 +lemma power_numeral_even: "z ^ numeral (Num.Bit0 w) = (let w = z ^ (numeral w) in w * w)"
    5.83 +  by (simp only: numeral_Bit0 power_add Let_def)
    5.84  
    5.85 -lemma power_numeral_odd:
    5.86 -  "z ^ numeral (Num.Bit1 w) = (let w = z ^ (numeral w) in z * w * w)"
    5.87 -  unfolding numeral_Bit1 One_nat_def add_Suc_right add_0_right
    5.88 -  unfolding power_Suc power_add Let_def mult.assoc ..
    5.89 +lemma power_numeral_odd: "z ^ numeral (Num.Bit1 w) = (let w = z ^ (numeral w) in z * w * w)"
    5.90 +  by (simp only: numeral_Bit1 One_nat_def add_Suc_right add_0_right
    5.91 +      power_Suc power_add Let_def mult.assoc)
    5.92  
    5.93 -lemma funpow_times_power:
    5.94 -  "(times x ^^ f x) = times (x ^ f x)"
    5.95 +lemma funpow_times_power: "(times x ^^ f x) = times (x ^ f x)"
    5.96  proof (induct "f x" arbitrary: f)
    5.97 -  case 0 then show ?case by (simp add: fun_eq_iff)
    5.98 +  case 0
    5.99 +  then show ?case by (simp add: fun_eq_iff)
   5.100  next
   5.101    case (Suc n)
   5.102    define g where "g x = f x - 1" for x
   5.103    with Suc have "n = g x" by simp
   5.104    with Suc have "times x ^^ g x = times (x ^ g x)" by simp
   5.105    moreover from Suc g_def have "f x = g x + 1" by simp
   5.106 -  ultimately show ?case by (simp add: power_add funpow_add fun_eq_iff mult.assoc)
   5.107 +  ultimately show ?case
   5.108 +    by (simp add: power_add funpow_add fun_eq_iff mult.assoc)
   5.109  qed
   5.110  
   5.111  lemma power_commuting_commutes:
   5.112    assumes "x * y = y * x"
   5.113    shows "x ^ n * y = y * x ^n"
   5.114  proof (induct n)
   5.115 +  case 0
   5.116 +  then show ?case by simp
   5.117 +next
   5.118    case (Suc n)
   5.119    have "x ^ Suc n * y = x ^ n * y * x"
   5.120      by (subst power_Suc2) (simp add: assms ac_simps)
   5.121    also have "\<dots> = y * x ^ Suc n"
   5.122 -    unfolding Suc power_Suc2
   5.123 -    by (simp add: ac_simps)
   5.124 +    by (simp only: Suc power_Suc2) (simp add: ac_simps)
   5.125    finally show ?case .
   5.126 -qed simp
   5.127 +qed
   5.128  
   5.129 -lemma power_minus_mult:
   5.130 -  "0 < n \<Longrightarrow> a ^ (n - 1) * a = a ^ n"
   5.131 +lemma power_minus_mult: "0 < n \<Longrightarrow> a ^ (n - 1) * a = a ^ n"
   5.132    by (simp add: power_commutes split: nat_diff_split)
   5.133  
   5.134  end
   5.135 @@ -119,29 +110,25 @@
   5.136  context comm_monoid_mult
   5.137  begin
   5.138  
   5.139 -lemma power_mult_distrib [field_simps]:
   5.140 -  "(a * b) ^ n = (a ^ n) * (b ^ n)"
   5.141 +lemma power_mult_distrib [field_simps]: "(a * b) ^ n = (a ^ n) * (b ^ n)"
   5.142    by (induct n) (simp_all add: ac_simps)
   5.143  
   5.144  end
   5.145  
   5.146 -text\<open>Extract constant factors from powers\<close>
   5.147 +text \<open>Extract constant factors from powers.\<close>
   5.148  declare power_mult_distrib [where a = "numeral w" for w, simp]
   5.149  declare power_mult_distrib [where b = "numeral w" for w, simp]
   5.150  
   5.151 -lemma power_add_numeral [simp]:
   5.152 -  fixes a :: "'a :: monoid_mult"
   5.153 -  shows "a^numeral m * a^numeral n = a^numeral (m + n)"
   5.154 +lemma power_add_numeral [simp]: "a^numeral m * a^numeral n = a^numeral (m + n)"
   5.155 +  for a :: "'a::monoid_mult"
   5.156    by (simp add: power_add [symmetric])
   5.157  
   5.158 -lemma power_add_numeral2 [simp]:
   5.159 -  fixes a :: "'a :: monoid_mult"
   5.160 -  shows "a^numeral m * (a^numeral n * b) = a^numeral (m + n) * b"
   5.161 +lemma power_add_numeral2 [simp]: "a^numeral m * (a^numeral n * b) = a^numeral (m + n) * b"
   5.162 +  for a :: "'a::monoid_mult"
   5.163    by (simp add: mult.assoc [symmetric])
   5.164  
   5.165 -lemma power_mult_numeral [simp]:
   5.166 -  fixes a :: "'a :: monoid_mult"
   5.167 -  shows"(a^numeral m)^numeral n = a^numeral (m * n)"
   5.168 +lemma power_mult_numeral [simp]: "(a^numeral m)^numeral n = a^numeral (m * n)"
   5.169 +  for a :: "'a::monoid_mult"
   5.170    by (simp only: numeral_mult power_mult)
   5.171  
   5.172  context semiring_numeral
   5.173 @@ -151,8 +138,9 @@
   5.174    by (simp only: sqr_conv_mult numeral_mult)
   5.175  
   5.176  lemma numeral_pow: "numeral (Num.pow k l) = numeral k ^ numeral l"
   5.177 -  by (induct l, simp_all only: numeral_class.numeral.simps pow.simps
   5.178 -    numeral_sqr numeral_mult power_add power_one_right)
   5.179 +  by (induct l)
   5.180 +    (simp_all only: numeral_class.numeral.simps pow.simps
   5.181 +      numeral_sqr numeral_mult power_add power_one_right)
   5.182  
   5.183  lemma power_numeral [simp]: "numeral k ^ numeral l = numeral (Num.pow k l)"
   5.184    by (rule numeral_pow [symmetric])
   5.185 @@ -162,16 +150,13 @@
   5.186  context semiring_1
   5.187  begin
   5.188  
   5.189 -lemma of_nat_power [simp]:
   5.190 -  "of_nat (m ^ n) = of_nat m ^ n"
   5.191 +lemma of_nat_power [simp]: "of_nat (m ^ n) = of_nat m ^ n"
   5.192    by (induct n) simp_all
   5.193  
   5.194 -lemma zero_power:
   5.195 -  "0 < n \<Longrightarrow> 0 ^ n = 0"
   5.196 +lemma zero_power: "0 < n \<Longrightarrow> 0 ^ n = 0"
   5.197    by (cases n) simp_all
   5.198  
   5.199 -lemma power_zero_numeral [simp]:
   5.200 -  "0 ^ numeral k = 0"
   5.201 +lemma power_zero_numeral [simp]: "0 ^ numeral k = 0"
   5.202    by (simp add: numeral_eq_Suc)
   5.203  
   5.204  lemma zero_power2: "0\<^sup>2 = 0" (* delete? *)
   5.205 @@ -180,13 +165,11 @@
   5.206  lemma one_power2: "1\<^sup>2 = 1" (* delete? *)
   5.207    by (rule power_one)
   5.208  
   5.209 -lemma power_0_Suc [simp]:
   5.210 -  "0 ^ Suc n = 0"
   5.211 +lemma power_0_Suc [simp]: "0 ^ Suc n = 0"
   5.212    by simp
   5.213  
   5.214 -text\<open>It looks plausible as a simprule, but its effect can be strange.\<close>
   5.215 -lemma power_0_left:
   5.216 -  "0 ^ n = (if n = 0 then 1 else 0)"
   5.217 +text \<open>It looks plausible as a simprule, but its effect can be strange.\<close>
   5.218 +lemma power_0_left: "0 ^ n = (if n = 0 then 1 else 0)"
   5.219    by (cases n) simp_all
   5.220  
   5.221  end
   5.222 @@ -194,34 +177,32 @@
   5.223  context comm_semiring_1
   5.224  begin
   5.225  
   5.226 -text \<open>The divides relation\<close>
   5.227 +text \<open>The divides relation.\<close>
   5.228  
   5.229  lemma le_imp_power_dvd:
   5.230 -  assumes "m \<le> n" shows "a ^ m dvd a ^ n"
   5.231 +  assumes "m \<le> n"
   5.232 +  shows "a ^ m dvd a ^ n"
   5.233  proof
   5.234 -  have "a ^ n = a ^ (m + (n - m))"
   5.235 -    using \<open>m \<le> n\<close> by simp
   5.236 -  also have "\<dots> = a ^ m * a ^ (n - m)"
   5.237 -    by (rule power_add)
   5.238 +  from assms have "a ^ n = a ^ (m + (n - m))" by simp
   5.239 +  also have "\<dots> = a ^ m * a ^ (n - m)" by (rule power_add)
   5.240    finally show "a ^ n = a ^ m * a ^ (n - m)" .
   5.241  qed
   5.242  
   5.243 -lemma power_le_dvd:
   5.244 -  "a ^ n dvd b \<Longrightarrow> m \<le> n \<Longrightarrow> a ^ m dvd b"
   5.245 +lemma power_le_dvd: "a ^ n dvd b \<Longrightarrow> m \<le> n \<Longrightarrow> a ^ m dvd b"
   5.246    by (rule dvd_trans [OF le_imp_power_dvd])
   5.247  
   5.248 -lemma dvd_power_same:
   5.249 -  "x dvd y \<Longrightarrow> x ^ n dvd y ^ n"
   5.250 +lemma dvd_power_same: "x dvd y \<Longrightarrow> x ^ n dvd y ^ n"
   5.251    by (induct n) (auto simp add: mult_dvd_mono)
   5.252  
   5.253 -lemma dvd_power_le:
   5.254 -  "x dvd y \<Longrightarrow> m \<ge> n \<Longrightarrow> x ^ n dvd y ^ m"
   5.255 +lemma dvd_power_le: "x dvd y \<Longrightarrow> m \<ge> n \<Longrightarrow> x ^ n dvd y ^ m"
   5.256    by (rule power_le_dvd [OF dvd_power_same])
   5.257  
   5.258  lemma dvd_power [simp]:
   5.259 -  assumes "n > (0::nat) \<or> x = 1"
   5.260 +  fixes n :: nat
   5.261 +  assumes "n > 0 \<or> x = 1"
   5.262    shows "x dvd (x ^ n)"
   5.263 -using assms proof
   5.264 +  using assms
   5.265 +proof
   5.266    assume "0 < n"
   5.267    then have "x ^ n = x ^ Suc (n - 1)" by simp
   5.268    then show "x dvd (x ^ n)" by simp
   5.269 @@ -237,16 +218,13 @@
   5.270  
   5.271  subclass power .
   5.272  
   5.273 -lemma power_eq_0_iff [simp]:
   5.274 -  "a ^ n = 0 \<longleftrightarrow> a = 0 \<and> n > 0"
   5.275 +lemma power_eq_0_iff [simp]: "a ^ n = 0 \<longleftrightarrow> a = 0 \<and> n > 0"
   5.276    by (induct n) auto
   5.277  
   5.278 -lemma power_not_zero:
   5.279 -  "a \<noteq> 0 \<Longrightarrow> a ^ n \<noteq> 0"
   5.280 +lemma power_not_zero: "a \<noteq> 0 \<Longrightarrow> a ^ n \<noteq> 0"
   5.281    by (induct n) auto
   5.282  
   5.283 -lemma zero_eq_power2 [simp]:
   5.284 -  "a\<^sup>2 = 0 \<longleftrightarrow> a = 0"
   5.285 +lemma zero_eq_power2 [simp]: "a\<^sup>2 = 0 \<longleftrightarrow> a = 0"
   5.286    unfolding power2_eq_square by simp
   5.287  
   5.288  end
   5.289 @@ -254,45 +232,42 @@
   5.290  context ring_1
   5.291  begin
   5.292  
   5.293 -lemma power_minus:
   5.294 -  "(- a) ^ n = (- 1) ^ n * a ^ n"
   5.295 +lemma power_minus: "(- a) ^ n = (- 1) ^ n * a ^ n"
   5.296  proof (induct n)
   5.297 -  case 0 show ?case by simp
   5.298 +  case 0
   5.299 +  show ?case by simp
   5.300  next
   5.301 -  case (Suc n) then show ?case
   5.302 +  case (Suc n)
   5.303 +  then show ?case
   5.304      by (simp del: power_Suc add: power_Suc2 mult.assoc)
   5.305  qed
   5.306  
   5.307  lemma power_minus': "NO_MATCH 1 x \<Longrightarrow> (-x) ^ n = (-1)^n * x ^ n"
   5.308    by (rule power_minus)
   5.309  
   5.310 -lemma power_minus_Bit0:
   5.311 -  "(- x) ^ numeral (Num.Bit0 k) = x ^ numeral (Num.Bit0 k)"
   5.312 +lemma power_minus_Bit0: "(- x) ^ numeral (Num.Bit0 k) = x ^ numeral (Num.Bit0 k)"
   5.313    by (induct k, simp_all only: numeral_class.numeral.simps power_add
   5.314      power_one_right mult_minus_left mult_minus_right minus_minus)
   5.315  
   5.316 -lemma power_minus_Bit1:
   5.317 -  "(- x) ^ numeral (Num.Bit1 k) = - (x ^ numeral (Num.Bit1 k))"
   5.318 +lemma power_minus_Bit1: "(- x) ^ numeral (Num.Bit1 k) = - (x ^ numeral (Num.Bit1 k))"
   5.319    by (simp only: eval_nat_numeral(3) power_Suc power_minus_Bit0 mult_minus_left)
   5.320  
   5.321 -lemma power2_minus [simp]:
   5.322 -  "(- a)\<^sup>2 = a\<^sup>2"
   5.323 +lemma power2_minus [simp]: "(- a)\<^sup>2 = a\<^sup>2"
   5.324    by (fact power_minus_Bit0)
   5.325  
   5.326 -lemma power_minus1_even [simp]:
   5.327 -  "(- 1) ^ (2*n) = 1"
   5.328 +lemma power_minus1_even [simp]: "(- 1) ^ (2*n) = 1"
   5.329  proof (induct n)
   5.330 -  case 0 show ?case by simp
   5.331 +  case 0
   5.332 +  show ?case by simp
   5.333  next
   5.334 -  case (Suc n) then show ?case by (simp add: power_add power2_eq_square)
   5.335 +  case (Suc n)
   5.336 +  then show ?case by (simp add: power_add power2_eq_square)
   5.337  qed
   5.338  
   5.339 -lemma power_minus1_odd:
   5.340 -  "(- 1) ^ Suc (2*n) = -1"
   5.341 +lemma power_minus1_odd: "(- 1) ^ Suc (2*n) = -1"
   5.342    by simp
   5.343  
   5.344 -lemma power_minus_even [simp]:
   5.345 -  "(-a) ^ (2*n) = a ^ (2*n)"
   5.346 +lemma power_minus_even [simp]: "(-a) ^ (2*n) = a ^ (2*n)"
   5.347    by (simp add: power_minus [of a])
   5.348  
   5.349  end
   5.350 @@ -300,8 +275,7 @@
   5.351  context ring_1_no_zero_divisors
   5.352  begin
   5.353  
   5.354 -lemma power2_eq_1_iff:
   5.355 -  "a\<^sup>2 = 1 \<longleftrightarrow> a = 1 \<or> a = - 1"
   5.356 +lemma power2_eq_1_iff: "a\<^sup>2 = 1 \<longleftrightarrow> a = 1 \<or> a = - 1"
   5.357    using square_eq_1_iff [of a] by (simp add: power2_eq_square)
   5.358  
   5.359  end
   5.360 @@ -317,13 +291,10 @@
   5.361  context algebraic_semidom
   5.362  begin
   5.363  
   5.364 -lemma div_power:
   5.365 -  assumes "b dvd a"
   5.366 -  shows "(a div b) ^ n = a ^ n div b ^ n"
   5.367 -  using assms by (induct n) (simp_all add: div_mult_div_if_dvd dvd_power_same)
   5.368 +lemma div_power: "b dvd a \<Longrightarrow> (a div b) ^ n = a ^ n div b ^ n"
   5.369 +  by (induct n) (simp_all add: div_mult_div_if_dvd dvd_power_same)
   5.370  
   5.371 -lemma is_unit_power_iff:
   5.372 -  "is_unit (a ^ n) \<longleftrightarrow> is_unit a \<or> n = 0"
   5.373 +lemma is_unit_power_iff: "is_unit (a ^ n) \<longleftrightarrow> is_unit a \<or> n = 0"
   5.374    by (induct n) (auto simp add: is_unit_mult_iff)
   5.375  
   5.376  end
   5.377 @@ -331,12 +302,10 @@
   5.378  context normalization_semidom
   5.379  begin
   5.380  
   5.381 -lemma normalize_power:
   5.382 -  "normalize (a ^ n) = normalize a ^ n"
   5.383 +lemma normalize_power: "normalize (a ^ n) = normalize a ^ n"
   5.384    by (induct n) (simp_all add: normalize_mult)
   5.385  
   5.386 -lemma unit_factor_power:
   5.387 -  "unit_factor (a ^ n) = unit_factor a ^ n"
   5.388 +lemma unit_factor_power: "unit_factor (a ^ n) = unit_factor a ^ n"
   5.389    by (induct n) (simp_all add: unit_factor_mult)
   5.390  
   5.391  end
   5.392 @@ -344,19 +313,19 @@
   5.393  context division_ring
   5.394  begin
   5.395  
   5.396 -text\<open>Perhaps these should be simprules.\<close>
   5.397 -lemma power_inverse [field_simps, divide_simps]:
   5.398 -  "inverse a ^ n = inverse (a ^ n)"
   5.399 +text \<open>Perhaps these should be simprules.\<close>
   5.400 +lemma power_inverse [field_simps, divide_simps]: "inverse a ^ n = inverse (a ^ n)"
   5.401  proof (cases "a = 0")
   5.402 -  case True then show ?thesis by (simp add: power_0_left)
   5.403 +  case True
   5.404 +  then show ?thesis by (simp add: power_0_left)
   5.405  next
   5.406 -  case False then have "inverse (a ^ n) = inverse a ^ n"
   5.407 +  case False
   5.408 +  then have "inverse (a ^ n) = inverse a ^ n"
   5.409      by (induct n) (simp_all add: nonzero_inverse_mult_distrib power_commutes)
   5.410    then show ?thesis by simp
   5.411  qed
   5.412  
   5.413 -lemma power_one_over [field_simps, divide_simps]:
   5.414 -  "(1 / a) ^ n = 1 / a ^ n"
   5.415 +lemma power_one_over [field_simps, divide_simps]: "(1 / a) ^ n = 1 / a ^ n"
   5.416    using power_inverse [of a] by (simp add: divide_inverse)
   5.417  
   5.418  end
   5.419 @@ -365,12 +334,11 @@
   5.420  begin
   5.421  
   5.422  lemma power_diff:
   5.423 -  assumes nz: "a \<noteq> 0"
   5.424 +  assumes "a \<noteq> 0"
   5.425    shows "n \<le> m \<Longrightarrow> a ^ (m - n) = a ^ m / a ^ n"
   5.426 -  by (induct m n rule: diff_induct) (simp_all add: nz power_not_zero)
   5.427 +  by (induct m n rule: diff_induct) (simp_all add: assms power_not_zero)
   5.428  
   5.429 -lemma power_divide [field_simps, divide_simps]:
   5.430 -  "(a / b) ^ n = a ^ n / b ^ n"
   5.431 +lemma power_divide [field_simps, divide_simps]: "(a / b) ^ n = a ^ n / b ^ n"
   5.432    by (induct n) simp_all
   5.433  
   5.434  end
   5.435 @@ -381,22 +349,19 @@
   5.436  context linordered_semidom
   5.437  begin
   5.438  
   5.439 -lemma zero_less_power [simp]:
   5.440 -  "0 < a \<Longrightarrow> 0 < a ^ n"
   5.441 +lemma zero_less_power [simp]: "0 < a \<Longrightarrow> 0 < a ^ n"
   5.442    by (induct n) simp_all
   5.443  
   5.444 -lemma zero_le_power [simp]:
   5.445 -  "0 \<le> a \<Longrightarrow> 0 \<le> a ^ n"
   5.446 +lemma zero_le_power [simp]: "0 \<le> a \<Longrightarrow> 0 \<le> a ^ n"
   5.447    by (induct n) simp_all
   5.448  
   5.449 -lemma power_mono:
   5.450 -  "a \<le> b \<Longrightarrow> 0 \<le> a \<Longrightarrow> a ^ n \<le> b ^ n"
   5.451 +lemma power_mono: "a \<le> b \<Longrightarrow> 0 \<le> a \<Longrightarrow> a ^ n \<le> b ^ n"
   5.452    by (induct n) (auto intro: mult_mono order_trans [of 0 a b])
   5.453  
   5.454  lemma one_le_power [simp]: "1 \<le> a \<Longrightarrow> 1 \<le> a ^ n"
   5.455    using power_mono [of 1 a n] by simp
   5.456  
   5.457 -lemma power_le_one: "\<lbrakk>0 \<le> a; a \<le> 1\<rbrakk> \<Longrightarrow> a ^ n \<le> 1"
   5.458 +lemma power_le_one: "0 \<le> a \<Longrightarrow> a \<le> 1 \<Longrightarrow> a ^ n \<le> 1"
   5.459    using power_mono [of a 1 n] by simp
   5.460  
   5.461  lemma power_gt1_lemma:
   5.462 @@ -405,19 +370,16 @@
   5.463  proof -
   5.464    from gt1 have "0 \<le> a"
   5.465      by (fact order_trans [OF zero_le_one less_imp_le])
   5.466 -  have "1 * 1 < a * 1" using gt1 by simp
   5.467 -  also have "\<dots> \<le> a * a ^ n" using gt1
   5.468 -    by (simp only: mult_mono \<open>0 \<le> a\<close> one_le_power order_less_imp_le
   5.469 -        zero_le_one order_refl)
   5.470 +  from gt1 have "1 * 1 < a * 1" by simp
   5.471 +  also from gt1 have "\<dots> \<le> a * a ^ n"
   5.472 +    by (simp only: mult_mono \<open>0 \<le> a\<close> one_le_power order_less_imp_le zero_le_one order_refl)
   5.473    finally show ?thesis by simp
   5.474  qed
   5.475  
   5.476 -lemma power_gt1:
   5.477 -  "1 < a \<Longrightarrow> 1 < a ^ Suc n"
   5.478 +lemma power_gt1: "1 < a \<Longrightarrow> 1 < a ^ Suc n"
   5.479    by (simp add: power_gt1_lemma)
   5.480  
   5.481 -lemma one_less_power [simp]:
   5.482 -  "1 < a \<Longrightarrow> 0 < n \<Longrightarrow> 1 < a ^ n"
   5.483 +lemma one_less_power [simp]: "1 < a \<Longrightarrow> 0 < n \<Longrightarrow> 1 < a ^ n"
   5.484    by (cases n) (simp_all add: power_gt1_lemma)
   5.485  
   5.486  lemma power_le_imp_le_exp:
   5.487 @@ -431,123 +393,122 @@
   5.488    show ?case
   5.489    proof (cases n)
   5.490      case 0
   5.491 -    with Suc.prems Suc.hyps have "a * a ^ m \<le> 1" by simp
   5.492 +    with Suc have "a * a ^ m \<le> 1" by simp
   5.493      with gt1 show ?thesis
   5.494 -      by (force simp only: power_gt1_lemma
   5.495 -          not_less [symmetric])
   5.496 +      by (force simp only: power_gt1_lemma not_less [symmetric])
   5.497    next
   5.498      case (Suc n)
   5.499      with Suc.prems Suc.hyps show ?thesis
   5.500 -      by (force dest: mult_left_le_imp_le
   5.501 -          simp add: less_trans [OF zero_less_one gt1])
   5.502 +      by (force dest: mult_left_le_imp_le simp add: less_trans [OF zero_less_one gt1])
   5.503    qed
   5.504  qed
   5.505  
   5.506 -lemma of_nat_zero_less_power_iff [simp]:
   5.507 -  "of_nat x ^ n > 0 \<longleftrightarrow> x > 0 \<or> n = 0"
   5.508 +lemma of_nat_zero_less_power_iff [simp]: "of_nat x ^ n > 0 \<longleftrightarrow> x > 0 \<or> n = 0"
   5.509    by (induct n) auto
   5.510  
   5.511 -text\<open>Surely we can strengthen this? It holds for \<open>0<a<1\<close> too.\<close>
   5.512 -lemma power_inject_exp [simp]:
   5.513 -  "1 < a \<Longrightarrow> a ^ m = a ^ n \<longleftrightarrow> m = n"
   5.514 +text \<open>Surely we can strengthen this? It holds for \<open>0<a<1\<close> too.\<close>
   5.515 +lemma power_inject_exp [simp]: "1 < a \<Longrightarrow> a ^ m = a ^ n \<longleftrightarrow> m = n"
   5.516    by (force simp add: order_antisym power_le_imp_le_exp)
   5.517  
   5.518 -text\<open>Can relax the first premise to @{term "0<a"} in the case of the
   5.519 -natural numbers.\<close>
   5.520 -lemma power_less_imp_less_exp:
   5.521 -  "1 < a \<Longrightarrow> a ^ m < a ^ n \<Longrightarrow> m < n"
   5.522 -  by (simp add: order_less_le [of m n] less_le [of "a^m" "a^n"]
   5.523 -    power_le_imp_le_exp)
   5.524 +text \<open>
   5.525 +  Can relax the first premise to @{term "0<a"} in the case of the
   5.526 +  natural numbers.
   5.527 +\<close>
   5.528 +lemma power_less_imp_less_exp: "1 < a \<Longrightarrow> a ^ m < a ^ n \<Longrightarrow> m < n"
   5.529 +  by (simp add: order_less_le [of m n] less_le [of "a^m" "a^n"] power_le_imp_le_exp)
   5.530  
   5.531 -lemma power_strict_mono [rule_format]:
   5.532 -  "a < b \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 < n \<longrightarrow> a ^ n < b ^ n"
   5.533 -  by (induct n)
   5.534 -   (auto simp add: mult_strict_mono le_less_trans [of 0 a b])
   5.535 +lemma power_strict_mono [rule_format]: "a < b \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 < n \<longrightarrow> a ^ n < b ^ n"
   5.536 +  by (induct n) (auto simp: mult_strict_mono le_less_trans [of 0 a b])
   5.537  
   5.538  text\<open>Lemma for \<open>power_strict_decreasing\<close>\<close>
   5.539 -lemma power_Suc_less:
   5.540 -  "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> a * a ^ n < a ^ n"
   5.541 -  by (induct n)
   5.542 -    (auto simp add: mult_strict_left_mono)
   5.543 +lemma power_Suc_less: "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> a * a ^ n < a ^ n"
   5.544 +  by (induct n) (auto simp: mult_strict_left_mono)
   5.545  
   5.546 -lemma power_strict_decreasing [rule_format]:
   5.547 -  "n < N \<Longrightarrow> 0 < a \<Longrightarrow> a < 1 \<longrightarrow> a ^ N < a ^ n"
   5.548 +lemma power_strict_decreasing [rule_format]: "n < N \<Longrightarrow> 0 < a \<Longrightarrow> a < 1 \<longrightarrow> a ^ N < a ^ n"
   5.549  proof (induct N)
   5.550 -  case 0 then show ?case by simp
   5.551 +  case 0
   5.552 +  then show ?case by simp
   5.553  next
   5.554 -  case (Suc N) then show ?case
   5.555 -  apply (auto simp add: power_Suc_less less_Suc_eq)
   5.556 -  apply (subgoal_tac "a * a^N < 1 * a^n")
   5.557 -  apply simp
   5.558 -  apply (rule mult_strict_mono) apply auto
   5.559 -  done
   5.560 +  case (Suc N)
   5.561 +  then show ?case
   5.562 +    apply (auto simp add: power_Suc_less less_Suc_eq)
   5.563 +    apply (subgoal_tac "a * a^N < 1 * a^n")
   5.564 +     apply simp
   5.565 +    apply (rule mult_strict_mono)
   5.566 +       apply auto
   5.567 +    done
   5.568  qed
   5.569  
   5.570 -text\<open>Proof resembles that of \<open>power_strict_decreasing\<close>\<close>
   5.571 -lemma power_decreasing [rule_format]:
   5.572 -  "n \<le> N \<Longrightarrow> 0 \<le> a \<Longrightarrow> a \<le> 1 \<longrightarrow> a ^ N \<le> a ^ n"
   5.573 +text \<open>Proof resembles that of \<open>power_strict_decreasing\<close>.\<close>
   5.574 +lemma power_decreasing: "n \<le> N \<Longrightarrow> 0 \<le> a \<Longrightarrow> a \<le> 1 \<Longrightarrow> a ^ N \<le> a ^ n"
   5.575  proof (induct N)
   5.576 -  case 0 then show ?case by simp
   5.577 +  case 0
   5.578 +  then show ?case by simp
   5.579  next
   5.580 -  case (Suc N) then show ?case
   5.581 -  apply (auto simp add: le_Suc_eq)
   5.582 -  apply (subgoal_tac "a * a^N \<le> 1 * a^n", simp)
   5.583 -  apply (rule mult_mono) apply auto
   5.584 -  done
   5.585 +  case (Suc N)
   5.586 +  then show ?case
   5.587 +    apply (auto simp add: le_Suc_eq)
   5.588 +    apply (subgoal_tac "a * a^N \<le> 1 * a^n")
   5.589 +     apply simp
   5.590 +    apply (rule mult_mono)
   5.591 +       apply auto
   5.592 +    done
   5.593  qed
   5.594  
   5.595 -lemma power_Suc_less_one:
   5.596 -  "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> a ^ Suc n < 1"
   5.597 +lemma power_Suc_less_one: "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> a ^ Suc n < 1"
   5.598    using power_strict_decreasing [of 0 "Suc n" a] by simp
   5.599  
   5.600 -text\<open>Proof again resembles that of \<open>power_strict_decreasing\<close>\<close>
   5.601 -lemma power_increasing [rule_format]:
   5.602 -  "n \<le> N \<Longrightarrow> 1 \<le> a \<Longrightarrow> a ^ n \<le> a ^ N"
   5.603 +text \<open>Proof again resembles that of \<open>power_strict_decreasing\<close>.\<close>
   5.604 +lemma power_increasing: "n \<le> N \<Longrightarrow> 1 \<le> a \<Longrightarrow> a ^ n \<le> a ^ N"
   5.605  proof (induct N)
   5.606 -  case 0 then show ?case by simp
   5.607 +  case 0
   5.608 +  then show ?case by simp
   5.609  next
   5.610 -  case (Suc N) then show ?case
   5.611 -  apply (auto simp add: le_Suc_eq)
   5.612 -  apply (subgoal_tac "1 * a^n \<le> a * a^N", simp)
   5.613 -  apply (rule mult_mono) apply (auto simp add: order_trans [OF zero_le_one])
   5.614 -  done
   5.615 +  case (Suc N)
   5.616 +  then show ?case
   5.617 +    apply (auto simp add: le_Suc_eq)
   5.618 +    apply (subgoal_tac "1 * a^n \<le> a * a^N")
   5.619 +     apply simp
   5.620 +    apply (rule mult_mono)
   5.621 +       apply (auto simp add: order_trans [OF zero_le_one])
   5.622 +    done
   5.623  qed
   5.624  
   5.625 -text\<open>Lemma for \<open>power_strict_increasing\<close>\<close>
   5.626 -lemma power_less_power_Suc:
   5.627 -  "1 < a \<Longrightarrow> a ^ n < a * a ^ n"
   5.628 -  by (induct n) (auto simp add: mult_strict_left_mono less_trans [OF zero_less_one])
   5.629 +text \<open>Lemma for \<open>power_strict_increasing\<close>.\<close>
   5.630 +lemma power_less_power_Suc: "1 < a \<Longrightarrow> a ^ n < a * a ^ n"
   5.631 +  by (induct n) (auto simp: mult_strict_left_mono less_trans [OF zero_less_one])
   5.632  
   5.633 -lemma power_strict_increasing [rule_format]:
   5.634 -  "n < N \<Longrightarrow> 1 < a \<longrightarrow> a ^ n < a ^ N"
   5.635 +lemma power_strict_increasing: "n < N \<Longrightarrow> 1 < a \<Longrightarrow> a ^ n < a ^ N"
   5.636  proof (induct N)
   5.637 -  case 0 then show ?case by simp
   5.638 +  case 0
   5.639 +  then show ?case by simp
   5.640  next
   5.641 -  case (Suc N) then show ?case
   5.642 -  apply (auto simp add: power_less_power_Suc less_Suc_eq)
   5.643 -  apply (subgoal_tac "1 * a^n < a * a^N", simp)
   5.644 -  apply (rule mult_strict_mono) apply (auto simp add: less_trans [OF zero_less_one] less_imp_le)
   5.645 -  done
   5.646 +  case (Suc N)
   5.647 +  then show ?case
   5.648 +    apply (auto simp add: power_less_power_Suc less_Suc_eq)
   5.649 +    apply (subgoal_tac "1 * a^n < a * a^N")
   5.650 +     apply simp
   5.651 +    apply (rule mult_strict_mono)
   5.652 +    apply (auto simp add: less_trans [OF zero_less_one] less_imp_le)
   5.653 +    done
   5.654  qed
   5.655  
   5.656 -lemma power_increasing_iff [simp]:
   5.657 -  "1 < b \<Longrightarrow> b ^ x \<le> b ^ y \<longleftrightarrow> x \<le> y"
   5.658 +lemma power_increasing_iff [simp]: "1 < b \<Longrightarrow> b ^ x \<le> b ^ y \<longleftrightarrow> x \<le> y"
   5.659    by (blast intro: power_le_imp_le_exp power_increasing less_imp_le)
   5.660  
   5.661 -lemma power_strict_increasing_iff [simp]:
   5.662 -  "1 < b \<Longrightarrow> b ^ x < b ^ y \<longleftrightarrow> x < y"
   5.663 -by (blast intro: power_less_imp_less_exp power_strict_increasing)
   5.664 +lemma power_strict_increasing_iff [simp]: "1 < b \<Longrightarrow> b ^ x < b ^ y \<longleftrightarrow> x < y"
   5.665 +  by (blast intro: power_less_imp_less_exp power_strict_increasing)
   5.666  
   5.667  lemma power_le_imp_le_base:
   5.668    assumes le: "a ^ Suc n \<le> b ^ Suc n"
   5.669 -    and ynonneg: "0 \<le> b"
   5.670 +    and "0 \<le> b"
   5.671    shows "a \<le> b"
   5.672  proof (rule ccontr)
   5.673 -  assume "~ a \<le> b"
   5.674 +  assume "\<not> ?thesis"
   5.675    then have "b < a" by (simp only: linorder_not_le)
   5.676    then have "b ^ Suc n < a ^ Suc n"
   5.677 -    by (simp only: assms power_strict_mono)
   5.678 -  from le and this show False
   5.679 +    by (simp only: assms(2) power_strict_mono)
   5.680 +  with le show False
   5.681      by (simp add: linorder_not_less [symmetric])
   5.682  qed
   5.683  
   5.684 @@ -556,38 +517,31 @@
   5.685    assumes nonneg: "0 \<le> b"
   5.686    shows "a < b"
   5.687  proof (rule contrapos_pp [OF less])
   5.688 -  assume "~ a < b"
   5.689 -  hence "b \<le> a" by (simp only: linorder_not_less)
   5.690 -  hence "b ^ n \<le> a ^ n" using nonneg by (rule power_mono)
   5.691 -  thus "\<not> a ^ n < b ^ n" by (simp only: linorder_not_less)
   5.692 +  assume "\<not> ?thesis"
   5.693 +  then have "b \<le> a" by (simp only: linorder_not_less)
   5.694 +  from this nonneg have "b ^ n \<le> a ^ n" by (rule power_mono)
   5.695 +  then show "\<not> a ^ n < b ^ n" by (simp only: linorder_not_less)
   5.696  qed
   5.697  
   5.698 -lemma power_inject_base:
   5.699 -  "a ^ Suc n = b ^ Suc n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> a = b"
   5.700 -by (blast intro: power_le_imp_le_base antisym eq_refl sym)
   5.701 +lemma power_inject_base: "a ^ Suc n = b ^ Suc n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> a = b"
   5.702 +  by (blast intro: power_le_imp_le_base antisym eq_refl sym)
   5.703  
   5.704 -lemma power_eq_imp_eq_base:
   5.705 -  "a ^ n = b ^ n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 < n \<Longrightarrow> a = b"
   5.706 +lemma power_eq_imp_eq_base: "a ^ n = b ^ n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 < n \<Longrightarrow> a = b"
   5.707    by (cases n) (simp_all del: power_Suc, rule power_inject_base)
   5.708  
   5.709 -lemma power_eq_iff_eq_base:
   5.710 -  "0 < n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> a ^ n = b ^ n \<longleftrightarrow> a = b"
   5.711 +lemma power_eq_iff_eq_base: "0 < n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> a ^ n = b ^ n \<longleftrightarrow> a = b"
   5.712    using power_eq_imp_eq_base [of a n b] by auto
   5.713  
   5.714 -lemma power2_le_imp_le:
   5.715 -  "x\<^sup>2 \<le> y\<^sup>2 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x \<le> y"
   5.716 +lemma power2_le_imp_le: "x\<^sup>2 \<le> y\<^sup>2 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x \<le> y"
   5.717    unfolding numeral_2_eq_2 by (rule power_le_imp_le_base)
   5.718  
   5.719 -lemma power2_less_imp_less:
   5.720 -  "x\<^sup>2 < y\<^sup>2 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x < y"
   5.721 +lemma power2_less_imp_less: "x\<^sup>2 < y\<^sup>2 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x < y"
   5.722    by (rule power_less_imp_less_base)
   5.723  
   5.724 -lemma power2_eq_imp_eq:
   5.725 -  "x\<^sup>2 = y\<^sup>2 \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> x = y"
   5.726 +lemma power2_eq_imp_eq: "x\<^sup>2 = y\<^sup>2 \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> x = y"
   5.727    unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base) simp
   5.728  
   5.729 -lemma power_Suc_le_self:
   5.730 -  shows "0 \<le> a \<Longrightarrow> a \<le> 1 \<Longrightarrow> a ^ Suc n \<le> a"
   5.731 +lemma power_Suc_le_self: "0 \<le> a \<Longrightarrow> a \<le> 1 \<Longrightarrow> a ^ Suc n \<le> a"
   5.732    using power_decreasing [of 1 "Suc n" a] by simp
   5.733  
   5.734  end
   5.735 @@ -595,16 +549,13 @@
   5.736  context linordered_ring_strict
   5.737  begin
   5.738  
   5.739 -lemma sum_squares_eq_zero_iff:
   5.740 -  "x * x + y * y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   5.741 +lemma sum_squares_eq_zero_iff: "x * x + y * y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   5.742    by (simp add: add_nonneg_eq_0_iff)
   5.743  
   5.744 -lemma sum_squares_le_zero_iff:
   5.745 -  "x * x + y * y \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   5.746 +lemma sum_squares_le_zero_iff: "x * x + y * y \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   5.747    by (simp add: le_less not_sum_squares_lt_zero sum_squares_eq_zero_iff)
   5.748  
   5.749 -lemma sum_squares_gt_zero_iff:
   5.750 -  "0 < x * x + y * y \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
   5.751 +lemma sum_squares_gt_zero_iff: "0 < x * x + y * y \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
   5.752    by (simp add: not_le [symmetric] sum_squares_le_zero_iff)
   5.753  
   5.754  end
   5.755 @@ -620,28 +571,26 @@
   5.756  
   5.757  lemma zero_less_power_abs_iff [simp]: "0 < \<bar>a\<bar> ^ n \<longleftrightarrow> a \<noteq> 0 \<or> n = 0"
   5.758  proof (induct n)
   5.759 -  case 0 show ?case by simp
   5.760 +  case 0
   5.761 +  show ?case by simp
   5.762  next
   5.763 -  case (Suc n) show ?case by (auto simp add: Suc zero_less_mult_iff)
   5.764 +  case Suc
   5.765 +  then show ?case by (auto simp: zero_less_mult_iff)
   5.766  qed
   5.767  
   5.768  lemma zero_le_power_abs [simp]: "0 \<le> \<bar>a\<bar> ^ n"
   5.769    by (rule zero_le_power [OF abs_ge_zero])
   5.770  
   5.771 -lemma zero_le_power2 [simp]:
   5.772 -  "0 \<le> a\<^sup>2"
   5.773 +lemma zero_le_power2 [simp]: "0 \<le> a\<^sup>2"
   5.774    by (simp add: power2_eq_square)
   5.775  
   5.776 -lemma zero_less_power2 [simp]:
   5.777 -  "0 < a\<^sup>2 \<longleftrightarrow> a \<noteq> 0"
   5.778 +lemma zero_less_power2 [simp]: "0 < a\<^sup>2 \<longleftrightarrow> a \<noteq> 0"
   5.779    by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
   5.780  
   5.781 -lemma power2_less_0 [simp]:
   5.782 -  "\<not> a\<^sup>2 < 0"
   5.783 +lemma power2_less_0 [simp]: "\<not> a\<^sup>2 < 0"
   5.784    by (force simp add: power2_eq_square mult_less_0_iff)
   5.785  
   5.786 -lemma power2_less_eq_zero_iff [simp]:
   5.787 -  "a\<^sup>2 \<le> 0 \<longleftrightarrow> a = 0"
   5.788 +lemma power2_less_eq_zero_iff [simp]: "a\<^sup>2 \<le> 0 \<longleftrightarrow> a = 0"
   5.789    by (simp add: le_less)
   5.790  
   5.791  lemma abs_power2 [simp]: "\<bar>a\<^sup>2\<bar> = a\<^sup>2"
   5.792 @@ -650,8 +599,7 @@
   5.793  lemma power2_abs [simp]: "\<bar>a\<bar>\<^sup>2 = a\<^sup>2"
   5.794    by (simp add: power2_eq_square)
   5.795  
   5.796 -lemma odd_power_less_zero:
   5.797 -  "a < 0 \<Longrightarrow> a ^ Suc (2*n) < 0"
   5.798 +lemma odd_power_less_zero: "a < 0 \<Longrightarrow> a ^ Suc (2*n) < 0"
   5.799  proof (induct n)
   5.800    case 0
   5.801    then show ?case by simp
   5.802 @@ -659,160 +607,152 @@
   5.803    case (Suc n)
   5.804    have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)"
   5.805      by (simp add: ac_simps power_add power2_eq_square)
   5.806 -  thus ?case
   5.807 +  then show ?case
   5.808      by (simp del: power_Suc add: Suc mult_less_0_iff mult_neg_neg)
   5.809  qed
   5.810  
   5.811 -lemma odd_0_le_power_imp_0_le:
   5.812 -  "0 \<le> a ^ Suc (2*n) \<Longrightarrow> 0 \<le> a"
   5.813 +lemma odd_0_le_power_imp_0_le: "0 \<le> a ^ Suc (2*n) \<Longrightarrow> 0 \<le> a"
   5.814    using odd_power_less_zero [of a n]
   5.815 -    by (force simp add: linorder_not_less [symmetric])
   5.816 +  by (force simp add: linorder_not_less [symmetric])
   5.817  
   5.818 -lemma zero_le_even_power'[simp]:
   5.819 -  "0 \<le> a ^ (2*n)"
   5.820 +lemma zero_le_even_power'[simp]: "0 \<le> a ^ (2*n)"
   5.821  proof (induct n)
   5.822    case 0
   5.823 -    show ?case by simp
   5.824 +  show ?case by simp
   5.825  next
   5.826    case (Suc n)
   5.827 -    have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)"
   5.828 -      by (simp add: ac_simps power_add power2_eq_square)
   5.829 -    thus ?case
   5.830 -      by (simp add: Suc zero_le_mult_iff)
   5.831 +  have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)"
   5.832 +    by (simp add: ac_simps power_add power2_eq_square)
   5.833 +  then show ?case
   5.834 +    by (simp add: Suc zero_le_mult_iff)
   5.835  qed
   5.836  
   5.837 -lemma sum_power2_ge_zero:
   5.838 -  "0 \<le> x\<^sup>2 + y\<^sup>2"
   5.839 +lemma sum_power2_ge_zero: "0 \<le> x\<^sup>2 + y\<^sup>2"
   5.840    by (intro add_nonneg_nonneg zero_le_power2)
   5.841  
   5.842 -lemma not_sum_power2_lt_zero:
   5.843 -  "\<not> x\<^sup>2 + y\<^sup>2 < 0"
   5.844 +lemma not_sum_power2_lt_zero: "\<not> x\<^sup>2 + y\<^sup>2 < 0"
   5.845    unfolding not_less by (rule sum_power2_ge_zero)
   5.846  
   5.847 -lemma sum_power2_eq_zero_iff:
   5.848 -  "x\<^sup>2 + y\<^sup>2 = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   5.849 +lemma sum_power2_eq_zero_iff: "x\<^sup>2 + y\<^sup>2 = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   5.850    unfolding power2_eq_square by (simp add: add_nonneg_eq_0_iff)
   5.851  
   5.852 -lemma sum_power2_le_zero_iff:
   5.853 -  "x\<^sup>2 + y\<^sup>2 \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   5.854 +lemma sum_power2_le_zero_iff: "x\<^sup>2 + y\<^sup>2 \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   5.855    by (simp add: le_less sum_power2_eq_zero_iff not_sum_power2_lt_zero)
   5.856  
   5.857 -lemma sum_power2_gt_zero_iff:
   5.858 -  "0 < x\<^sup>2 + y\<^sup>2 \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
   5.859 +lemma sum_power2_gt_zero_iff: "0 < x\<^sup>2 + y\<^sup>2 \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
   5.860    unfolding not_le [symmetric] by (simp add: sum_power2_le_zero_iff)
   5.861  
   5.862 -lemma abs_le_square_iff:
   5.863 -   "\<bar>x\<bar> \<le> \<bar>y\<bar> \<longleftrightarrow> x\<^sup>2 \<le> y\<^sup>2"
   5.864 +lemma abs_le_square_iff: "\<bar>x\<bar> \<le> \<bar>y\<bar> \<longleftrightarrow> x\<^sup>2 \<le> y\<^sup>2"
   5.865 +  (is "?lhs \<longleftrightarrow> ?rhs")
   5.866  proof
   5.867 -  assume "\<bar>x\<bar> \<le> \<bar>y\<bar>"
   5.868 -  then have "\<bar>x\<bar>\<^sup>2 \<le> \<bar>y\<bar>\<^sup>2" by (rule power_mono, simp)
   5.869 -  then show "x\<^sup>2 \<le> y\<^sup>2" by simp
   5.870 +  assume ?lhs
   5.871 +  then have "\<bar>x\<bar>\<^sup>2 \<le> \<bar>y\<bar>\<^sup>2" by (rule power_mono) simp
   5.872 +  then show ?rhs by simp
   5.873  next
   5.874 -  assume "x\<^sup>2 \<le> y\<^sup>2"
   5.875 -  then show "\<bar>x\<bar> \<le> \<bar>y\<bar>"
   5.876 +  assume ?rhs
   5.877 +  then show ?lhs
   5.878      by (auto intro!: power2_le_imp_le [OF _ abs_ge_zero])
   5.879  qed
   5.880  
   5.881  lemma abs_square_le_1:"x\<^sup>2 \<le> 1 \<longleftrightarrow> \<bar>x\<bar> \<le> 1"
   5.882 -  using abs_le_square_iff [of x 1]
   5.883 -  by simp
   5.884 +  using abs_le_square_iff [of x 1] by simp
   5.885  
   5.886  lemma abs_square_eq_1: "x\<^sup>2 = 1 \<longleftrightarrow> \<bar>x\<bar> = 1"
   5.887    by (auto simp add: abs_if power2_eq_1_iff)
   5.888  
   5.889  lemma abs_square_less_1: "x\<^sup>2 < 1 \<longleftrightarrow> \<bar>x\<bar> < 1"
   5.890 -  using  abs_square_eq_1 [of x] abs_square_le_1 [of x]
   5.891 -  by (auto simp add: le_less)
   5.892 +  using  abs_square_eq_1 [of x] abs_square_le_1 [of x] by (auto simp add: le_less)
   5.893  
   5.894  end
   5.895  
   5.896  
   5.897  subsection \<open>Miscellaneous rules\<close>
   5.898  
   5.899 -lemma (in linordered_semidom) self_le_power:
   5.900 -  "1 \<le> a \<Longrightarrow> 0 < n \<Longrightarrow> a \<le> a ^ n"
   5.901 +lemma (in linordered_semidom) self_le_power: "1 \<le> a \<Longrightarrow> 0 < n \<Longrightarrow> a \<le> a ^ n"
   5.902    using power_increasing [of 1 n a] power_one_right [of a] by auto
   5.903  
   5.904 -lemma (in power) power_eq_if:
   5.905 -  "p ^ m = (if m=0 then 1 else p * (p ^ (m - 1)))"
   5.906 +lemma (in power) power_eq_if: "p ^ m = (if m=0 then 1 else p * (p ^ (m - 1)))"
   5.907    unfolding One_nat_def by (cases m) simp_all
   5.908  
   5.909 -lemma (in comm_semiring_1) power2_sum:
   5.910 -  "(x + y)\<^sup>2 = x\<^sup>2 + y\<^sup>2 + 2 * x * y"
   5.911 +lemma (in comm_semiring_1) power2_sum: "(x + y)\<^sup>2 = x\<^sup>2 + y\<^sup>2 + 2 * x * y"
   5.912    by (simp add: algebra_simps power2_eq_square mult_2_right)
   5.913  
   5.914 -lemma (in comm_ring_1) power2_diff:
   5.915 -  "(x - y)\<^sup>2 = x\<^sup>2 + y\<^sup>2 - 2 * x * y"
   5.916 +context comm_ring_1
   5.917 +begin
   5.918 +
   5.919 +lemma power2_diff: "(x - y)\<^sup>2 = x\<^sup>2 + y\<^sup>2 - 2 * x * y"
   5.920    by (simp add: algebra_simps power2_eq_square mult_2_right)
   5.921  
   5.922 -lemma (in comm_ring_1) power2_commute:
   5.923 -  "(x - y)\<^sup>2 = (y - x)\<^sup>2"
   5.924 +lemma power2_commute: "(x - y)\<^sup>2 = (y - x)\<^sup>2"
   5.925    by (simp add: algebra_simps power2_eq_square)
   5.926  
   5.927 -lemma (in comm_ring_1) minus_power_mult_self:
   5.928 -  "(- a) ^ n * (- a) ^ n = a ^ (2 * n)"
   5.929 -  by (simp add: power_mult_distrib [symmetric]) (simp add: power2_eq_square [symmetric] power_mult [symmetric])
   5.930 -  
   5.931 -lemma (in comm_ring_1) minus_one_mult_self [simp]:
   5.932 -  "(- 1) ^ n * (- 1) ^ n = 1"
   5.933 +lemma minus_power_mult_self: "(- a) ^ n * (- a) ^ n = a ^ (2 * n)"
   5.934 +  by (simp add: power_mult_distrib [symmetric])
   5.935 +    (simp add: power2_eq_square [symmetric] power_mult [symmetric])
   5.936 +
   5.937 +lemma minus_one_mult_self [simp]: "(- 1) ^ n * (- 1) ^ n = 1"
   5.938    using minus_power_mult_self [of 1 n] by simp
   5.939  
   5.940 -lemma (in comm_ring_1) left_minus_one_mult_self [simp]:
   5.941 -  "(- 1) ^ n * ((- 1) ^ n * a) = a"
   5.942 +lemma left_minus_one_mult_self [simp]: "(- 1) ^ n * ((- 1) ^ n * a) = a"
   5.943    by (simp add: mult.assoc [symmetric])
   5.944  
   5.945 +end
   5.946 +
   5.947  text \<open>Simprules for comparisons where common factors can be cancelled.\<close>
   5.948  
   5.949  lemmas zero_compare_simps =
   5.950 -    add_strict_increasing add_strict_increasing2 add_increasing
   5.951 -    zero_le_mult_iff zero_le_divide_iff
   5.952 -    zero_less_mult_iff zero_less_divide_iff
   5.953 -    mult_le_0_iff divide_le_0_iff
   5.954 -    mult_less_0_iff divide_less_0_iff
   5.955 -    zero_le_power2 power2_less_0
   5.956 +  add_strict_increasing add_strict_increasing2 add_increasing
   5.957 +  zero_le_mult_iff zero_le_divide_iff
   5.958 +  zero_less_mult_iff zero_less_divide_iff
   5.959 +  mult_le_0_iff divide_le_0_iff
   5.960 +  mult_less_0_iff divide_less_0_iff
   5.961 +  zero_le_power2 power2_less_0
   5.962  
   5.963  
   5.964  subsection \<open>Exponentiation for the Natural Numbers\<close>
   5.965  
   5.966 -lemma nat_one_le_power [simp]:
   5.967 -  "Suc 0 \<le> i \<Longrightarrow> Suc 0 \<le> i ^ n"
   5.968 +lemma nat_one_le_power [simp]: "Suc 0 \<le> i \<Longrightarrow> Suc 0 \<le> i ^ n"
   5.969    by (rule one_le_power [of i n, unfolded One_nat_def])
   5.970  
   5.971 -lemma nat_zero_less_power_iff [simp]:
   5.972 -  "x ^ n > 0 \<longleftrightarrow> x > (0::nat) \<or> n = 0"
   5.973 +lemma nat_zero_less_power_iff [simp]: "x ^ n > 0 \<longleftrightarrow> x > 0 \<or> n = 0"
   5.974 +  for x :: nat
   5.975    by (induct n) auto
   5.976  
   5.977 -lemma nat_power_eq_Suc_0_iff [simp]:
   5.978 -  "x ^ m = Suc 0 \<longleftrightarrow> m = 0 \<or> x = Suc 0"
   5.979 +lemma nat_power_eq_Suc_0_iff [simp]: "x ^ m = Suc 0 \<longleftrightarrow> m = 0 \<or> x = Suc 0"
   5.980    by (induct m) auto
   5.981  
   5.982 -lemma power_Suc_0 [simp]:
   5.983 -  "Suc 0 ^ n = Suc 0"
   5.984 +lemma power_Suc_0 [simp]: "Suc 0 ^ n = Suc 0"
   5.985    by simp
   5.986  
   5.987 -text\<open>Valid for the naturals, but what if \<open>0<i<1\<close>?
   5.988 -Premises cannot be weakened: consider the case where @{term "i=0"},
   5.989 -@{term "m=1"} and @{term "n=0"}.\<close>
   5.990 +text \<open>
   5.991 +  Valid for the naturals, but what if \<open>0 < i < 1\<close>? Premises cannot be
   5.992 +  weakened: consider the case where \<open>i = 0\<close>, \<open>m = 1\<close> and \<open>n = 0\<close>.
   5.993 +\<close>
   5.994 +
   5.995  lemma nat_power_less_imp_less:
   5.996 -  assumes nonneg: "0 < (i::nat)"
   5.997 +  fixes i :: nat
   5.998 +  assumes nonneg: "0 < i"
   5.999    assumes less: "i ^ m < i ^ n"
  5.1000    shows "m < n"
  5.1001  proof (cases "i = 1")
  5.1002 -  case True with less power_one [where 'a = nat] show ?thesis by simp
  5.1003 +  case True
  5.1004 +  with less power_one [where 'a = nat] show ?thesis by simp
  5.1005  next
  5.1006 -  case False with nonneg have "1 < i" by auto
  5.1007 +  case False
  5.1008 +  with nonneg have "1 < i" by auto
  5.1009    from power_strict_increasing_iff [OF this] less show ?thesis ..
  5.1010  qed
  5.1011  
  5.1012 -lemma power_dvd_imp_le:
  5.1013 -  "i ^ m dvd i ^ n \<Longrightarrow> (1::nat) < i \<Longrightarrow> m \<le> n"
  5.1014 -  apply (rule power_le_imp_le_exp, assumption)
  5.1015 -  apply (erule dvd_imp_le, simp)
  5.1016 +lemma power_dvd_imp_le: "i ^ m dvd i ^ n \<Longrightarrow> 1 < i \<Longrightarrow> m \<le> n"
  5.1017 +  for i m n :: nat
  5.1018 +  apply (rule power_le_imp_le_exp)
  5.1019 +   apply assumption
  5.1020 +  apply (erule dvd_imp_le)
  5.1021 +  apply simp
  5.1022    done
  5.1023  
  5.1024 -lemma power2_nat_le_eq_le:
  5.1025 -  fixes m n :: nat
  5.1026 -  shows "m\<^sup>2 \<le> n\<^sup>2 \<longleftrightarrow> m \<le> n"
  5.1027 +lemma power2_nat_le_eq_le: "m\<^sup>2 \<le> n\<^sup>2 \<longleftrightarrow> m \<le> n"
  5.1028 +  for m n :: nat
  5.1029    by (auto intro: power2_le_imp_le power_mono)
  5.1030  
  5.1031  lemma power2_nat_le_imp_le:
  5.1032 @@ -820,18 +760,20 @@
  5.1033    assumes "m\<^sup>2 \<le> n"
  5.1034    shows "m \<le> n"
  5.1035  proof (cases m)
  5.1036 -  case 0 then show ?thesis by simp
  5.1037 +  case 0
  5.1038 +  then show ?thesis by simp
  5.1039  next
  5.1040    case (Suc k)
  5.1041    show ?thesis
  5.1042    proof (rule ccontr)
  5.1043 -    assume "\<not> m \<le> n"
  5.1044 +    assume "\<not> ?thesis"
  5.1045      then have "n < m" by simp
  5.1046      with assms Suc show False
  5.1047        by (simp add: power2_eq_square)
  5.1048    qed
  5.1049  qed
  5.1050  
  5.1051 +
  5.1052  subsubsection \<open>Cardinality of the Powerset\<close>
  5.1053  
  5.1054  lemma card_UNIV_bool [simp]: "card (UNIV :: bool set) = 2"
  5.1055 @@ -840,16 +782,17 @@
  5.1056  lemma card_Pow: "finite A \<Longrightarrow> card (Pow A) = 2 ^ card A"
  5.1057  proof (induct rule: finite_induct)
  5.1058    case empty
  5.1059 -    show ?case by auto
  5.1060 +  show ?case by auto
  5.1061  next
  5.1062    case (insert x A)
  5.1063    then have "inj_on (insert x) (Pow A)"
  5.1064      unfolding inj_on_def by (blast elim!: equalityE)
  5.1065    then have "card (Pow A) + card (insert x ` Pow A) = 2 * 2 ^ card A"
  5.1066      by (simp add: mult_2 card_image Pow_insert insert.hyps)
  5.1067 -  then show ?case using insert
  5.1068 +  with insert show ?case
  5.1069      apply (simp add: Pow_insert)
  5.1070 -    apply (subst card_Un_disjoint, auto)
  5.1071 +    apply (subst card_Un_disjoint)
  5.1072 +       apply auto
  5.1073      done
  5.1074  qed
  5.1075