(*  Title:      HOL/Induct/ABexp.thy
    Author:     Stefan Berghofer, TU Muenchen
*)

section \<open>Arithmetic and boolean expressions\<close>

theory ABexp
imports Main
begin

datatype 'a aexp =
    IF "'a bexp"  "'a aexp"  "'a aexp"
  | Sum "'a aexp"  "'a aexp"
  | Diff "'a aexp"  "'a aexp"
  | Var 'a
  | Num nat
and 'a bexp =
    Less "'a aexp"  "'a aexp"
  | And "'a bexp"  "'a bexp"
  | Neg "'a bexp"

text \<open>\medskip Evaluation of arithmetic and boolean expressions\<close>

primrec evala :: "('a \<Rightarrow> nat) \<Rightarrow> 'a aexp \<Rightarrow> nat"
  and evalb :: "('a \<Rightarrow> nat) \<Rightarrow> 'a bexp \<Rightarrow> bool"
where
  "evala env (IF b a1 a2) = (if evalb env b then evala env a1 else evala env a2)"
| "evala env (Sum a1 a2) = evala env a1 + evala env a2"
| "evala env (Diff a1 a2) = evala env a1 - evala env a2"
| "evala env (Var v) = env v"
| "evala env (Num n) = n"

| "evalb env (Less a1 a2) = (evala env a1 < evala env a2)"
| "evalb env (And b1 b2) = (evalb env b1 \<and> evalb env b2)"
| "evalb env (Neg b) = (\<not> evalb env b)"

text \<open>\medskip Substitution on arithmetic and boolean expressions\<close>

primrec substa :: "('a \<Rightarrow> 'b aexp) \<Rightarrow> 'a aexp \<Rightarrow> 'b aexp"
  and substb :: "('a \<Rightarrow> 'b aexp) \<Rightarrow> 'a bexp \<Rightarrow> 'b bexp"
where
  "substa f (IF b a1 a2) = IF (substb f b) (substa f a1) (substa f a2)"
| "substa f (Sum a1 a2) = Sum (substa f a1) (substa f a2)"
| "substa f (Diff a1 a2) = Diff (substa f a1) (substa f a2)"
| "substa f (Var v) = f v"
| "substa f (Num n) = Num n"

| "substb f (Less a1 a2) = Less (substa f a1) (substa f a2)"
| "substb f (And b1 b2) = And (substb f b1) (substb f b2)"
| "substb f (Neg b) = Neg (substb f b)"

lemma subst1_aexp:
  "evala env (substa (Var (v := a')) a) = evala (env (v := evala env a')) a"
and subst1_bexp:
  "evalb env (substb (Var (v := a')) b) = evalb (env (v := evala env a')) b"
    \<comment> \<open>one variable\<close>
  by (induct a and b) simp_all

lemma subst_all_aexp:
  "evala env (substa s a) = evala (\<lambda>x. evala env (s x)) a"
and subst_all_bexp:
  "evalb env (substb s b) = evalb (\<lambda>x. evala env (s x)) b"
  by (induct a and b) auto

end


(*  Title:      HOL/Induct/Comb.thy
    Author:     Lawrence C Paulson
    Copyright   1996  University of Cambridge
*)

section {* Combinatory Logic example: the Church-Rosser Theorem *}

theory Comb imports Main begin

text {*
  Curiously, combinators do not include free variables.

  Example taken from @{cite camilleri92}.

HOL system proofs may be found in the HOL distribution at
   .../contrib/rule-induction/cl.ml
*}

subsection {* Definitions *}

text {* Datatype definition of combinators @{text S} and @{text K}. *}

datatype comb = K
              | S
              | Ap comb comb (infixl "##" 90)

notation (xsymbols)
  Ap  (infixl "\<bullet>" 90)

text {*
  Inductive definition of contractions, @{text "-1->"} and
  (multi-step) reductions, @{text "--->"}.
*}

inductive_set
  contract :: "(comb*comb) set"
  and contract_rel1 :: "[comb,comb] => bool"  (infixl "-1->" 50)
  where
    "x -1-> y == (x,y) \<in> contract"
   | K:     "K##x##y -1-> x"
   | S:     "S##x##y##z -1-> (x##z)##(y##z)"
   | Ap1:   "x-1->y ==> x##z -1-> y##z"
   | Ap2:   "x-1->y ==> z##x -1-> z##y"

abbreviation
  contract_rel :: "[comb,comb] => bool"   (infixl "--->" 50) where
  "x ---> y == (x,y) \<in> contract^*"

text {*
  Inductive definition of parallel contractions, @{text "=1=>"} and
  (multi-step) parallel reductions, @{text "===>"}.
*}

inductive_set
  parcontract :: "(comb*comb) set"
  and parcontract_rel1 :: "[comb,comb] => bool"  (infixl "=1=>" 50)
  where
    "x =1=> y == (x,y) \<in> parcontract"
  | refl:  "x =1=> x"
  | K:     "K##x##y =1=> x"
  | S:     "S##x##y##z =1=> (x##z)##(y##z)"
  | Ap:    "[| x=1=>y;  z=1=>w |] ==> x##z =1=> y##w"

abbreviation
  parcontract_rel :: "[comb,comb] => bool"   (infixl "===>" 50) where
  "x ===> y == (x,y) \<in> parcontract^*"

text {*
  Misc definitions.
*}

definition
  I :: comb where
  "I = S##K##K"

definition
  diamond   :: "('a * 'a)set => bool" where
    --{*confluence; Lambda/Commutation treats this more abstractly*}
  "diamond(r) = (\<forall>x y. (x,y) \<in> r -->
                  (\<forall>y'. (x,y') \<in> r -->
                    (\<exists>z. (y,z) \<in> r & (y',z) \<in> r)))"

subsection {*Reflexive/Transitive closure preserves Church-Rosser property*}

text{*So does the Transitive closure, with a similar proof*}

text{*Strip lemma.
   The induction hypothesis covers all but the last diamond of the strip.*}
lemma diamond_strip_lemmaE [rule_format]:
    "[| diamond(r);  (x,y) \<in> r^* |] ==>
          \<forall>y'. (x,y') \<in> r --> (\<exists>z. (y',z) \<in> r^* & (y,z) \<in> r)"
apply (unfold diamond_def)
apply (erule rtrancl_induct)
apply (meson rtrancl_refl)
apply (meson rtrancl_trans r_into_rtrancl)
done

lemma diamond_rtrancl: "diamond(r) ==> diamond(r^*)"
apply (simp (no_asm_simp) add: diamond_def)
apply (rule impI [THEN allI, THEN allI])
apply (erule rtrancl_induct, blast)
apply (meson rtrancl_trans r_into_rtrancl diamond_strip_lemmaE)
done

subsection {* Non-contraction results *}

text {* Derive a case for each combinator constructor. *}

inductive_cases
      K_contractE [elim!]: "K -1-> r"
  and S_contractE [elim!]: "S -1-> r"
  and Ap_contractE [elim!]: "p##q -1-> r"

declare contract.K [intro!] contract.S [intro!]
declare contract.Ap1 [intro] contract.Ap2 [intro]

lemma I_contract_E [elim!]: "I -1-> z ==> P"
by (unfold I_def, blast)

lemma K1_contractD [elim!]: "K##x -1-> z ==> (\<exists>x'. z = K##x' & x -1-> x')"
by blast

lemma Ap_reduce1 [intro]: "x ---> y ==> x##z ---> y##z"
apply (erule rtrancl_induct)
apply (blast intro: rtrancl_trans)+
done

lemma Ap_reduce2 [intro]: "x ---> y ==> z##x ---> z##y"
apply (erule rtrancl_induct)
apply (blast intro: rtrancl_trans)+
done

text {*Counterexample to the diamond property for @{term "x -1-> y"}*}

lemma not_diamond_contract: "~ diamond(contract)"
by (unfold diamond_def, metis S_contractE contract.K)

subsection {* Results about Parallel Contraction *}

text {* Derive a case for each combinator constructor. *}

inductive_cases
      K_parcontractE [elim!]: "K =1=> r"
  and S_parcontractE [elim!]: "S =1=> r"
  and Ap_parcontractE [elim!]: "p##q =1=> r"

declare parcontract.intros [intro]

(*** Basic properties of parallel contraction ***)

subsection {* Basic properties of parallel contraction *}

lemma K1_parcontractD [dest!]: "K##x =1=> z ==> (\<exists>x'. z = K##x' & x =1=> x')"
by blast

lemma S1_parcontractD [dest!]: "S##x =1=> z ==> (\<exists>x'. z = S##x' & x =1=> x')"
by blast

lemma S2_parcontractD [dest!]:
     "S##x##y =1=> z ==> (\<exists>x' y'. z = S##x'##y' & x =1=> x' & y =1=> y')"
by blast

text{*The rules above are not essential but make proofs much faster*}

text{*Church-Rosser property for parallel contraction*}
lemma diamond_parcontract: "diamond parcontract"
apply (unfold diamond_def)
apply (rule impI [THEN allI, THEN allI])
apply (erule parcontract.induct, fast+)
done

text {*
  \medskip Equivalence of @{prop "p ---> q"} and @{prop "p ===> q"}.
*}

lemma contract_subset_parcontract: "contract <= parcontract"
by (auto, erule contract.induct, blast+)

text{*Reductions: simply throw together reflexivity, transitivity and
  the one-step reductions*}

declare r_into_rtrancl [intro]  rtrancl_trans [intro]

(*Example only: not used*)
lemma reduce_I: "I##x ---> x"
by (unfold I_def, blast)

lemma parcontract_subset_reduce: "parcontract <= contract^*"
by (auto, erule parcontract.induct, blast+)

lemma reduce_eq_parreduce: "contract^* = parcontract^*"
by (metis contract_subset_parcontract parcontract_subset_reduce rtrancl_subset)

theorem diamond_reduce: "diamond(contract^*)"
by (simp add: reduce_eq_parreduce diamond_rtrancl diamond_parcontract)

end


(*  Title:       HOL/Complex.thy
    Author:      Jacques D. Fleuriot, 2001 University of Edinburgh
    Author:      Lawrence C Paulson, 2003/4
*)

section \<open>Complex Numbers: Rectangular and Polar Representations\<close>

theory Complex
imports Transcendental
begin

text \<open>
  We use the \<^theory_text>\<open>codatatype\<close> command to define the type of complex numbers. This
  allows us to use \<^theory_text>\<open>primcorec\<close> to define complex functions by defining their
  real and imaginary result separately.
\<close>

codatatype complex = Complex (Re: real) (Im: real)

lemma complex_surj: "Complex (Re z) (Im z) = z"
  by (rule complex.collapse)

lemma complex_eqI [intro?]: "Re x = Re y \<Longrightarrow> Im x = Im y \<Longrightarrow> x = y"
  by (rule complex.expand) simp

lemma complex_eq_iff: "x = y \<longleftrightarrow> Re x = Re y \<and> Im x = Im y"
  by (auto intro: complex.expand)

subsection \<open>Addition and Subtraction\<close>

instantiation complex :: ab_group_add
begin

primcorec zero_complex
  where
    "Re 0 = 0"
  | "Im 0 = 0"

primcorec plus_complex
  where
    "Re (x + y) = Re x + Re y"
  | "Im (x + y) = Im x + Im y"

primcorec uminus_complex
  where
    "Re (- x) = - Re x"
  | "Im (- x) = - Im x"

primcorec minus_complex
  where
    "Re (x - y) = Re x - Re y"
  | "Im (x - y) = Im x - Im y"

instance
  by standard (simp_all add: complex_eq_iff)

end

subsection \<open>Multiplication and Division\<close>

instantiation complex :: field
begin

primcorec one_complex
  where
    "Re 1 = 1"
  | "Im 1 = 0"

primcorec times_complex
  where
    "Re (x * y) = Re x * Re y - Im x * Im y"
  | "Im (x * y) = Re x * Im y + Im x * Re y"

primcorec inverse_complex
  where
    "Re (inverse x) = Re x / ((Re x)\<^sup>2 + (Im x)\<^sup>2)"
  | "Im (inverse x) = - Im x / ((Re x)\<^sup>2 + (Im x)\<^sup>2)"

definition "x div y = x * inverse y" for x y :: complex

instance
  by standard
     (simp_all add: complex_eq_iff divide_complex_def
      distrib_left distrib_right right_diff_distrib left_diff_distrib
      power2_eq_square add_divide_distrib [symmetric])

end

lemma Re_divide: "Re (x / y) = (Re x * Re y + Im x * Im y) / ((Re y)\<^sup>2 + (Im y)\<^sup>2)"
  by (simp add: divide_complex_def add_divide_distrib)

lemma Im_divide: "Im (x / y) = (Im x * Re y - Re x * Im y) / ((Re y)\<^sup>2 + (Im y)\<^sup>2)"
  unfolding divide_complex_def times_complex.sel inverse_complex.sel
  by (simp add: divide_simps)

lemma Re_power2: "Re (x ^ 2) = (Re x)^2 - (Im x)^2"
  by (simp add: power2_eq_square)

lemma Im_power2: "Im (x ^ 2) = 2 * Re x * Im x"
  by (simp add: power2_eq_square)

lemma Re_power_real [simp]: "Im x = 0 \<Longrightarrow> Re (x ^ n) = Re x ^ n "
  by (induct n) simp_all

lemma Im_power_real [simp]: "Im x = 0 \<Longrightarrow> Im (x ^ n) = 0"
  by (induct n) simp_all

subsection \<open>Scalar Multiplication\<close>

instantiation complex :: real_field
begin

primcorec scaleR_complex
  where
    "Re (scaleR r x) = r * Re x"
  | "Im (scaleR r x) = r * Im x"

instance
proof
  fix a b :: real and x y :: complex
  show "scaleR a (x + y) = scaleR a x + scaleR a y"
    by (simp add: complex_eq_iff distrib_left)
  show "scaleR (a + b) x = scaleR a x + scaleR b x"
    by (simp add: complex_eq_iff distrib_right)
  show "scaleR a (scaleR b x) = scaleR (a * b) x"
    by (simp add: complex_eq_iff mult.assoc)
  show "scaleR 1 x = x"
    by (simp add: complex_eq_iff)
  show "scaleR a x * y = scaleR a (x * y)"
    by (simp add: complex_eq_iff algebra_simps)
  show "x * scaleR a y = scaleR a (x * y)"
    by (simp add: complex_eq_iff algebra_simps)
qed

end

subsection \<open>Numerals, Arithmetic, and Embedding from Reals\<close>

abbreviation complex_of_real :: "real \<Rightarrow> complex"
  where "complex_of_real \<equiv> of_real"

declare [[coercion "of_real :: real \<Rightarrow> complex"]]
declare [[coercion "of_rat :: rat \<Rightarrow> complex"]]
declare [[coercion "of_int :: int \<Rightarrow> complex"]]
declare [[coercion "of_nat :: nat \<Rightarrow> complex"]]

lemma complex_Re_of_nat [simp]: "Re (of_nat n) = of_nat n"
  by (induct n) simp_all

lemma complex_Im_of_nat [simp]: "Im (of_nat n) = 0"
  by (induct n) simp_all

lemma complex_Re_of_int [simp]: "Re (of_int z) = of_int z"
  by (cases z rule: int_diff_cases) simp

lemma complex_Im_of_int [simp]: "Im (of_int z) = 0"
  by (cases z rule: int_diff_cases) simp

lemma complex_Re_numeral [simp]: "Re (numeral v) = numeral v"
  using complex_Re_of_int [of "numeral v"] by simp

lemma complex_Im_numeral [simp]: "Im (numeral v) = 0"
  using complex_Im_of_int [of "numeral v"] by simp

lemma Re_complex_of_real [simp]: "Re (complex_of_real z) = z"
  by (simp add: of_real_def)

lemma Im_complex_of_real [simp]: "Im (complex_of_real z) = 0"
  by (simp add: of_real_def)

lemma Re_divide_numeral [simp]: "Re (z / numeral w) = Re z / numeral w"
  by (simp add: Re_divide sqr_conv_mult)

lemma Im_divide_numeral [simp]: "Im (z / numeral w) = Im z / numeral w"
  by (simp add: Im_divide sqr_conv_mult)

lemma Re_divide_of_nat [simp]: "Re (z / of_nat n) = Re z / of_nat n"
  by (cases n) (simp_all add: Re_divide divide_simps power2_eq_square del: of_nat_Suc)

lemma Im_divide_of_nat [simp]: "Im (z / of_nat n) = Im z / of_nat n"
  by (cases n) (simp_all add: Im_divide divide_simps power2_eq_square del: of_nat_Suc)

lemma of_real_Re [simp]: "z \<in> \<real> \<Longrightarrow> of_real (Re z) = z"
  by (auto simp: Reals_def)

lemma complex_Re_fact [simp]: "Re (fact n) = fact n"
proof -
  have "(fact n :: complex) = of_real (fact n)"
    by simp
  also have "Re \<dots> = fact n"
    by (subst Re_complex_of_real) simp_all
  finally show ?thesis .
qed

lemma complex_Im_fact [simp]: "Im (fact n) = 0"
  by (subst of_nat_fact [symmetric]) (simp only: complex_Im_of_nat)

subsection \<open>The Complex Number $i$\<close>

primcorec "ii" :: complex  ("\<i>")
  where
    "Re \<i> = 0"
  | "Im \<i> = 1"

lemma Complex_eq[simp]: "Complex a b = a + \<i> * b"
  by (simp add: complex_eq_iff)

lemma complex_eq: "a = Re a + \<i> * Im a"
  by (simp add: complex_eq_iff)

lemma fun_complex_eq: "f = (\<lambda>x. Re (f x) + \<i> * Im (f x))"
  by (simp add: fun_eq_iff complex_eq)

lemma i_squared [simp]: "\<i> * \<i> = -1"
  by (simp add: complex_eq_iff)

lemma power2_i [simp]: "\<i>\<^sup>2 = -1"
  by (simp add: power2_eq_square)

lemma inverse_i [simp]: "inverse \<i> = - \<i>"
  by (rule inverse_unique) simp

lemma divide_i [simp]: "x / \<i> = - \<i> * x"
  by (simp add: divide_complex_def)

lemma complex_i_mult_minus [simp]: "\<i> * (\<i> * x) = - x"
  by (simp add: mult.assoc [symmetric])

lemma complex_i_not_zero [simp]: "\<i> \<noteq> 0"
  by (simp add: complex_eq_iff)

lemma complex_i_not_one [simp]: "\<i> \<noteq> 1"
  by (simp add: complex_eq_iff)

lemma complex_i_not_numeral [simp]: "\<i> \<noteq> numeral w"
  by (simp add: complex_eq_iff)

lemma complex_i_not_neg_numeral [simp]: "\<i> \<noteq> - numeral w"
  by (simp add: complex_eq_iff)

lemma complex_split_polar: "\<exists>r a. z = complex_of_real r * (cos a + \<i> * sin a)"
  by (simp add: complex_eq_iff polar_Ex)

lemma i_even_power [simp]: "\<i> ^ (n * 2) = (-1) ^ n"
  by (metis mult.commute power2_i power_mult)

lemma Re_ii_times [simp]: "Re (\<i> * z) = - Im z"
  by simp

lemma Im_ii_times [simp]: "Im (\<i> * z) = Re z"
  by simp

lemma ii_times_eq_iff: "\<i> * w = z \<longleftrightarrow> w = - (\<i> * z)"
  by auto

lemma divide_numeral_i [simp]: "z / (numeral n * \<i>) = - (\<i> * z) / numeral n"
  by (metis divide_divide_eq_left divide_i mult.commute mult_minus_right)

subsection \<open>Vector Norm\<close>

instantiation complex :: real_normed_field
begin

definition "norm z = sqrt ((Re z)\<^sup>2 + (Im z)\<^sup>2)"

abbreviation cmod :: "complex \<Rightarrow> real"
  where "cmod \<equiv> norm"

definition complex_sgn_def: "sgn x = x /\<^sub>R cmod x"

definition dist_complex_def: "dist x y = cmod (x - y)"

definition uniformity_complex_def [code del]:
  "(uniformity :: (complex \<times> complex) filter) = (INF e:{0 <..}. principal {(x, y). dist x y < e})"

definition open_complex_def [code del]:
  "open (U :: complex set) \<longleftrightarrow> (\<forall>x\<in>U. eventually (\<lambda>(x', y). x' = x \<longrightarrow> y \<in> U) uniformity)"

instance
proof
  fix r :: real and x y :: complex and S :: "complex set"
  show "(norm x = 0) = (x = 0)"
    by (simp add: norm_complex_def complex_eq_iff)
  show "norm (x + y) \<le> norm x + norm y"
    by (simp add: norm_complex_def complex_eq_iff real_sqrt_sum_squares_triangle_ineq)
  show "norm (scaleR r x) = \<bar>r\<bar> * norm x"
    by (simp add: norm_complex_def complex_eq_iff power_mult_distrib distrib_left [symmetric]
        real_sqrt_mult)
  show "norm (x * y) = norm x * norm y"
    by (simp add: norm_complex_def complex_eq_iff real_sqrt_mult [symmetric]
        power2_eq_square algebra_simps)
qed (rule complex_sgn_def dist_complex_def open_complex_def uniformity_complex_def)+

end

declare uniformity_Abort[where 'a = complex, code]

lemma norm_ii [simp]: "norm \<i> = 1"
  by (simp add: norm_complex_def)

lemma cmod_unit_one: "cmod (cos a + \<i> * sin a) = 1"
  by (simp add: norm_complex_def)

lemma cmod_complex_polar: "cmod (r * (cos a + \<i> * sin a)) = \<bar>r\<bar>"
  by (simp add: norm_mult cmod_unit_one)

lemma complex_Re_le_cmod: "Re x \<le> cmod x"
  unfolding norm_complex_def by (rule real_sqrt_sum_squares_ge1)

lemma complex_mod_minus_le_complex_mod: "- cmod x \<le> cmod x"
  by (rule order_trans [OF _ norm_ge_zero]) simp

lemma complex_mod_triangle_ineq2: "cmod (b + a) - cmod b \<le> cmod a"
  by (rule ord_le_eq_trans [OF norm_triangle_ineq2]) simp

lemma abs_Re_le_cmod: "\<bar>Re x\<bar> \<le> cmod x"
  by (simp add: norm_complex_def)

lemma abs_Im_le_cmod: "\<bar>Im x\<bar> \<le> cmod x"
  by (simp add: norm_complex_def)

lemma cmod_le: "cmod z \<le> \<bar>Re z\<bar> + \<bar>Im z\<bar>"
  apply (subst complex_eq)
  apply (rule order_trans)
   apply (rule norm_triangle_ineq)
  apply (simp add: norm_mult)
  done

lemma cmod_eq_Re: "Im z = 0 \<Longrightarrow> cmod z = \<bar>Re z\<bar>"
  by (simp add: norm_complex_def)

lemma cmod_eq_Im: "Re z = 0 \<Longrightarrow> cmod z = \<bar>Im z\<bar>"
  by (simp add: norm_complex_def)

lemma cmod_power2: "(cmod z)\<^sup>2 = (Re z)\<^sup>2 + (Im z)\<^sup>2"
  by (simp add: norm_complex_def)

lemma cmod_plus_Re_le_0_iff: "cmod z + Re z \<le> 0 \<longleftrightarrow> Re z = - cmod z"
  using abs_Re_le_cmod[of z] by auto

lemma cmod_Re_le_iff: "Im x = Im y \<Longrightarrow> cmod x \<le> cmod y \<longleftrightarrow> \<bar>Re x\<bar> \<le> \<bar>Re y\<bar>"
  by (metis add.commute add_le_cancel_left norm_complex_def real_sqrt_abs real_sqrt_le_iff)

lemma cmod_Im_le_iff: "Re x = Re y \<Longrightarrow> cmod x \<le> cmod y \<longleftrightarrow> \<bar>Im x\<bar> \<le> \<bar>Im y\<bar>"
  by (metis add_le_cancel_left norm_complex_def real_sqrt_abs real_sqrt_le_iff)

lemma Im_eq_0: "\<bar>Re z\<bar> = cmod z \<Longrightarrow> Im z = 0"
  by (subst (asm) power_eq_iff_eq_base[symmetric, where n=2]) (auto simp add: norm_complex_def)

lemma abs_sqrt_wlog: "(\<And>x. x \<ge> 0 \<Longrightarrow> P x (x\<^sup>2)) \<Longrightarrow> P \<bar>x\<bar> (x\<^sup>2)"
  for x::"'a::linordered_idom"
  by (metis abs_ge_zero power2_abs)

lemma complex_abs_le_norm: "\<bar>Re z\<bar> + \<bar>Im z\<bar> \<le> sqrt 2 * norm z"
  unfolding norm_complex_def
  apply (rule abs_sqrt_wlog [where x="Re z"])
  apply (rule abs_sqrt_wlog [where x="Im z"])
  apply (rule power2_le_imp_le)
   apply (simp_all add: power2_sum add.commute sum_squares_bound real_sqrt_mult [symmetric])
  done

lemma complex_unit_circle: "z \<noteq> 0 \<Longrightarrow> (Re z / cmod z)\<^sup>2 + (Im z / cmod z)\<^sup>2 = 1"
  by (simp add: norm_complex_def divide_simps complex_eq_iff)

text \<open>Properties of complex signum.\<close>

lemma sgn_eq: "sgn z = z / complex_of_real (cmod z)"
  by (simp add: sgn_div_norm divide_inverse scaleR_conv_of_real mult.commute)

lemma Re_sgn [simp]: "Re(sgn z) = Re(z)/cmod z"
  by (simp add: complex_sgn_def divide_inverse)

lemma Im_sgn [simp]: "Im(sgn z) = Im(z)/cmod z"
  by (simp add: complex_sgn_def divide_inverse)

subsection \<open>Absolute value\<close>

instantiation complex :: field_abs_sgn
begin

definition abs_complex :: "complex \<Rightarrow> complex"
  where "abs_complex = of_real \<circ> norm"

instance
  apply standard
         apply (auto simp add: abs_complex_def complex_sgn_def norm_mult)
  apply (auto simp add: scaleR_conv_of_real field_simps)
  done

end

subsection \<open>Completeness of the Complexes\<close>

lemma bounded_linear_Re: "bounded_linear Re"
  by (rule bounded_linear_intro [where K=1]) (simp_all add: norm_complex_def)

lemma bounded_linear_Im: "bounded_linear Im"
  by (rule bounded_linear_intro [where K=1]) (simp_all add: norm_complex_def)

lemmas Cauchy_Re = bounded_linear.Cauchy [OF bounded_linear_Re]
lemmas Cauchy_Im = bounded_linear.Cauchy [OF bounded_linear_Im]
lemmas tendsto_Re [tendsto_intros] = bounded_linear.tendsto [OF bounded_linear_Re]
lemmas tendsto_Im [tendsto_intros] = bounded_linear.tendsto [OF bounded_linear_Im]
lemmas isCont_Re [simp] = bounded_linear.isCont [OF bounded_linear_Re]
lemmas isCont_Im [simp] = bounded_linear.isCont [OF bounded_linear_Im]
lemmas continuous_Re [simp] = bounded_linear.continuous [OF bounded_linear_Re]
lemmas continuous_Im [simp] = bounded_linear.continuous [OF bounded_linear_Im]
lemmas continuous_on_Re [continuous_intros] = bounded_linear.continuous_on[OF bounded_linear_Re]
lemmas continuous_on_Im [continuous_intros] = bounded_linear.continuous_on[OF bounded_linear_Im]
lemmas has_derivative_Re [derivative_intros] = bounded_linear.has_derivative[OF bounded_linear_Re]
lemmas has_derivative_Im [derivative_intros] = bounded_linear.has_derivative[OF bounded_linear_Im]
lemmas sums_Re = bounded_linear.sums [OF bounded_linear_Re]
lemmas sums_Im = bounded_linear.sums [OF bounded_linear_Im]

lemma tendsto_Complex [tendsto_intros]:
  "(f \<longlongrightarrow> a) F \<Longrightarrow> (g \<longlongrightarrow> b) F \<Longrightarrow> ((\<lambda>x. Complex (f x) (g x)) \<longlongrightarrow> Complex a b) F"
  by (auto intro!: tendsto_intros)

lemma tendsto_complex_iff:
  "(f \<longlongrightarrow> x) F \<longleftrightarrow> (((\<lambda>x. Re (f x)) \<longlongrightarrow> Re x) F \<and> ((\<lambda>x. Im (f x)) \<longlongrightarrow> Im x) F)"
proof safe
  assume "((\<lambda>x. Re (f x)) \<longlongrightarrow> Re x) F" "((\<lambda>x. Im (f x)) \<longlongrightarrow> Im x) F"
  from tendsto_Complex[OF this] show "(f \<longlongrightarrow> x) F"
    unfolding complex.collapse .
qed (auto intro: tendsto_intros)

lemma continuous_complex_iff:
  "continuous F f \<longleftrightarrow> continuous F (\<lambda>x. Re (f x)) \<and> continuous F (\<lambda>x. Im (f x))"
  by (simp only: continuous_def tendsto_complex_iff)

lemma has_vector_derivative_complex_iff: "(f has_vector_derivative x) F \<longleftrightarrow>
    ((\<lambda>x. Re (f x)) has_field_derivative (Re x)) F \<and>
    ((\<lambda>x. Im (f x)) has_field_derivative (Im x)) F"
  by (simp add: has_vector_derivative_def has_field_derivative_def has_derivative_def
      tendsto_complex_iff field_simps bounded_linear_scaleR_left bounded_linear_mult_right)

lemma has_field_derivative_Re[derivative_intros]:
  "(f has_vector_derivative D) F \<Longrightarrow> ((\<lambda>x. Re (f x)) has_field_derivative (Re D)) F"
  unfolding has_vector_derivative_complex_iff by safe

lemma has_field_derivative_Im[derivative_intros]:
  "(f has_vector_derivative D) F \<Longrightarrow> ((\<lambda>x. Im (f x)) has_field_derivative (Im D)) F"
  unfolding has_vector_derivative_complex_iff by safe

instance complex :: banach
proof
  fix X :: "nat \<Rightarrow> complex"
  assume X: "Cauchy X"
  then have "(\<lambda>n. Complex (Re (X n)) (Im (X n))) \<longlonglongrightarrow>
    Complex (lim (\<lambda>n. Re (X n))) (lim (\<lambda>n. Im (X n)))"
    by (intro tendsto_Complex convergent_LIMSEQ_iff[THEN iffD1]
        Cauchy_convergent_iff[THEN iffD1] Cauchy_Re Cauchy_Im)
  then show "convergent X"
    unfolding complex.collapse by (rule convergentI)
qed

declare DERIV_power[where 'a=complex, unfolded of_nat_def[symmetric], derivative_intros]

subsection \<open>Complex Conjugation\<close>

primcorec cnj :: "complex \<Rightarrow> complex"
  where
    "Re (cnj z) = Re z"
  | "Im (cnj z) = - Im z"

lemma complex_cnj_cancel_iff [simp]: "cnj x = cnj y \<longleftrightarrow> x = y"
  by (simp add: complex_eq_iff)

lemma complex_cnj_cnj [simp]: "cnj (cnj z) = z"
  by (simp add: complex_eq_iff)

lemma complex_cnj_zero [simp]: "cnj 0 = 0"
  by (simp add: complex_eq_iff)

lemma complex_cnj_zero_iff [iff]: "cnj z = 0 \<longleftrightarrow> z = 0"
  by (simp add: complex_eq_iff)

lemma complex_cnj_add [simp]: "cnj (x + y) = cnj x + cnj y"
  by (simp add: complex_eq_iff)

lemma cnj_sum [simp]: "cnj (sum f s) = (\<Sum>x\<in>s. cnj (f x))"
  by (induct s rule: infinite_finite_induct) auto

lemma complex_cnj_diff [simp]: "cnj (x - y) = cnj x - cnj y"
  by (simp add: complex_eq_iff)

lemma complex_cnj_minus [simp]: "cnj (- x) = - cnj x"
  by (simp add: complex_eq_iff)

lemma complex_cnj_one [simp]: "cnj 1 = 1"
  by (simp add: complex_eq_iff)

lemma complex_cnj_mult [simp]: "cnj (x * y) = cnj x * cnj y"
  by (simp add: complex_eq_iff)

lemma cnj_prod [simp]: "cnj (prod f s) = (\<Prod>x\<in>s. cnj (f x))"
  by (induct s rule: infinite_finite_induct) auto

lemma complex_cnj_inverse [simp]: "cnj (inverse x) = inverse (cnj x)"
  by (simp add: complex_eq_iff)

lemma complex_cnj_divide [simp]: "cnj (x / y) = cnj x / cnj y"
  by (simp add: divide_complex_def)

lemma complex_cnj_power [simp]: "cnj (x ^ n) = cnj x ^ n"
  by (induct n) simp_all

lemma complex_cnj_of_nat [simp]: "cnj (of_nat n) = of_nat n"
  by (simp add: complex_eq_iff)

lemma complex_cnj_of_int [simp]: "cnj (of_int z) = of_int z"
  by (simp add: complex_eq_iff)

lemma complex_cnj_numeral [simp]: "cnj (numeral w) = numeral w"
  by (simp add: complex_eq_iff)

lemma complex_cnj_neg_numeral [simp]: "cnj (- numeral w) = - numeral w"
  by (simp add: complex_eq_iff)

lemma complex_cnj_scaleR [simp]: "cnj (scaleR r x) = scaleR r (cnj x)"
  by (simp add: complex_eq_iff)

lemma complex_mod_cnj [simp]: "cmod (cnj z) = cmod z"
  by (simp add: norm_complex_def)

lemma complex_cnj_complex_of_real [simp]: "cnj (of_real x) = of_real x"
  by (simp add: complex_eq_iff)

lemma complex_cnj_i [simp]: "cnj \<i> = - \<i>"
  by (simp add: complex_eq_iff)

lemma complex_add_cnj: "z + cnj z = complex_of_real (2 * Re z)"
  by (simp add: complex_eq_iff)

lemma complex_diff_cnj: "z - cnj z = complex_of_real (2 * Im z) * \<i>"
  by (simp add: complex_eq_iff)

lemma complex_mult_cnj: "z * cnj z = complex_of_real ((Re z)\<^sup>2 + (Im z)\<^sup>2)"
  by (simp add: complex_eq_iff power2_eq_square)

lemma complex_mod_mult_cnj: "cmod (z * cnj z) = (cmod z)\<^sup>2"
  by (simp add: norm_mult power2_eq_square)

lemma complex_mod_sqrt_Re_mult_cnj: "cmod z = sqrt (Re (z * cnj z))"
  by (simp add: norm_complex_def power2_eq_square)

lemma complex_In_mult_cnj_zero [simp]: "Im (z * cnj z) = 0"
  by simp

lemma complex_cnj_fact [simp]: "cnj (fact n) = fact n"
  by (subst of_nat_fact [symmetric], subst complex_cnj_of_nat) simp

lemma complex_cnj_pochhammer [simp]: "cnj (pochhammer z n) = pochhammer (cnj z) n"
  by (induct n arbitrary: z) (simp_all add: pochhammer_rec)

lemma bounded_linear_cnj: "bounded_linear cnj"
  using complex_cnj_add complex_cnj_scaleR by (rule bounded_linear_intro [where K=1]) simp

lemmas tendsto_cnj [tendsto_intros] = bounded_linear.tendsto [OF bounded_linear_cnj]
  and isCont_cnj [simp] = bounded_linear.isCont [OF bounded_linear_cnj]
  and continuous_cnj [simp, continuous_intros] = bounded_linear.continuous [OF bounded_linear_cnj]
  and continuous_on_cnj [simp, continuous_intros] = bounded_linear.continuous_on [OF bounded_linear_cnj]
  and has_derivative_cnj [simp, derivative_intros] = bounded_linear.has_derivative [OF bounded_linear_cnj]

lemma lim_cnj: "((\<lambda>x. cnj(f x)) \<longlongrightarrow> cnj l) F \<longleftrightarrow> (f \<longlongrightarrow> l) F"
  by (simp add: tendsto_iff dist_complex_def complex_cnj_diff [symmetric] del: complex_cnj_diff)

lemma sums_cnj: "((\<lambda>x. cnj(f x)) sums cnj l) \<longleftrightarrow> (f sums l)"
  by (simp add: sums_def lim_cnj cnj_sum [symmetric] del: cnj_sum)

subsection \<open>Basic Lemmas\<close>

lemma complex_eq_0: "z=0 \<longleftrightarrow> (Re z)\<^sup>2 + (Im z)\<^sup>2 = 0"
  by (metis zero_complex.sel complex_eqI sum_power2_eq_zero_iff)

lemma complex_neq_0: "z\<noteq>0 \<longleftrightarrow> (Re z)\<^sup>2 + (Im z)\<^sup>2 > 0"
  by (metis complex_eq_0 less_numeral_extra(3) sum_power2_gt_zero_iff)

lemma complex_norm_square: "of_real ((norm z)\<^sup>2) = z * cnj z"
  by (cases z)
    (auto simp: complex_eq_iff norm_complex_def power2_eq_square[symmetric] of_real_power[symmetric]
      simp del: of_real_power)

lemma complex_div_cnj: "a / b = (a * cnj b) / (norm b)\<^sup>2"
  using complex_norm_square by auto

lemma Re_complex_div_eq_0: "Re (a / b) = 0 \<longleftrightarrow> Re (a * cnj b) = 0"
  by (auto simp add: Re_divide)

lemma Im_complex_div_eq_0: "Im (a / b) = 0 \<longleftrightarrow> Im (a * cnj b) = 0"
  by (auto simp add: Im_divide)

lemma complex_div_gt_0: "(Re (a / b) > 0 \<longleftrightarrow> Re (a * cnj b) > 0) \<and> (Im (a / b) > 0 \<longleftrightarrow> Im (a * cnj b) > 0)"
proof (cases "b = 0")
  case True
  then show ?thesis by auto
next
  case False
  then have "0 < (Re b)\<^sup>2 + (Im b)\<^sup>2"
    by (simp add: complex_eq_iff sum_power2_gt_zero_iff)
  then show ?thesis
    by (simp add: Re_divide Im_divide zero_less_divide_iff)
qed

lemma Re_complex_div_gt_0: "Re (a / b) > 0 \<longleftrightarrow> Re (a * cnj b) > 0"
  and Im_complex_div_gt_0: "Im (a / b) > 0 \<longleftrightarrow> Im (a * cnj b) > 0"
  using complex_div_gt_0 by auto

lemma Re_complex_div_ge_0: "Re (a / b) \<ge> 0 \<longleftrightarrow> Re (a * cnj b) \<ge> 0"
  by (metis le_less Re_complex_div_eq_0 Re_complex_div_gt_0)

lemma Im_complex_div_ge_0: "Im (a / b) \<ge> 0 \<longleftrightarrow> Im (a * cnj b) \<ge> 0"
  by (metis Im_complex_div_eq_0 Im_complex_div_gt_0 le_less)

lemma Re_complex_div_lt_0: "Re (a / b) < 0 \<longleftrightarrow> Re (a * cnj b) < 0"
  by (metis less_asym neq_iff Re_complex_div_eq_0 Re_complex_div_gt_0)

lemma Im_complex_div_lt_0: "Im (a / b) < 0 \<longleftrightarrow> Im (a * cnj b) < 0"
  by (metis Im_complex_div_eq_0 Im_complex_div_gt_0 less_asym neq_iff)

lemma Re_complex_div_le_0: "Re (a / b) \<le> 0 \<longleftrightarrow> Re (a * cnj b) \<le> 0"
  by (metis not_le Re_complex_div_gt_0)

lemma Im_complex_div_le_0: "Im (a / b) \<le> 0 \<longleftrightarrow> Im (a * cnj b) \<le> 0"
  by (metis Im_complex_div_gt_0 not_le)

lemma Re_divide_of_real [simp]: "Re (z / of_real r) = Re z / r"
  by (simp add: Re_divide power2_eq_square)

lemma Im_divide_of_real [simp]: "Im (z / of_real r) = Im z / r"
  by (simp add: Im_divide power2_eq_square)

lemma Re_divide_Reals: "r \<in> Reals \<Longrightarrow> Re (z / r) = Re z / Re r"
  by (metis Re_divide_of_real of_real_Re)

lemma Im_divide_Reals: "r \<in> Reals \<Longrightarrow> Im (z / r) = Im z / Re r"
  by (metis Im_divide_of_real of_real_Re)

lemma Re_sum[simp]: "Re (sum f s) = (\<Sum>x\<in>s. Re (f x))"
  by (induct s rule: infinite_finite_induct) auto

lemma Im_sum[simp]: "Im (sum f s) = (\<Sum>x\<in>s. Im(f x))"
  by (induct s rule: infinite_finite_induct) auto

lemma sums_complex_iff: "f sums x \<longleftrightarrow> ((\<lambda>x. Re (f x)) sums Re x) \<and> ((\<lambda>x. Im (f x)) sums Im x)"
  unfolding sums_def tendsto_complex_iff Im_sum Re_sum ..

lemma summable_complex_iff: "summable f \<longleftrightarrow> summable (\<lambda>x. Re (f x)) \<and>  summable (\<lambda>x. Im (f x))"
  unfolding summable_def sums_complex_iff[abs_def] by (metis complex.sel)

lemma summable_complex_of_real [simp]: "summable (\<lambda>n. complex_of_real (f n)) \<longleftrightarrow> summable f"
  unfolding summable_complex_iff by simp

lemma summable_Re: "summable f \<Longrightarrow> summable (\<lambda>x. Re (f x))"
  unfolding summable_complex_iff by blast

lemma summable_Im: "summable f \<Longrightarrow> summable (\<lambda>x. Im (f x))"
  unfolding summable_complex_iff by blast

lemma complex_is_Nat_iff: "z \<in> \<nat> \<longleftrightarrow> Im z = 0 \<and> (\<exists>i. Re z = of_nat i)"
  by (auto simp: Nats_def complex_eq_iff)

lemma complex_is_Int_iff: "z \<in> \<int> \<longleftrightarrow> Im z = 0 \<and> (\<exists>i. Re z = of_int i)"
  by (auto simp: Ints_def complex_eq_iff)

lemma complex_is_Real_iff: "z \<in> \<real> \<longleftrightarrow> Im z = 0"
  by (auto simp: Reals_def complex_eq_iff)

lemma Reals_cnj_iff: "z \<in> \<real> \<longleftrightarrow> cnj z = z"
  by (auto simp: complex_is_Real_iff complex_eq_iff)

lemma in_Reals_norm: "z \<in> \<real> \<Longrightarrow> norm z = \<bar>Re z\<bar>"
  by (simp add: complex_is_Real_iff norm_complex_def)

lemma series_comparison_complex:
  fixes f:: "nat \<Rightarrow> 'a::banach"
  assumes sg: "summable g"
    and "\<And>n. g n \<in> \<real>" "\<And>n. Re (g n) \<ge> 0"
    and fg: "\<And>n. n \<ge> N \<Longrightarrow> norm(f n) \<le> norm(g n)"
  shows "summable f"
proof -
  have g: "\<And>n. cmod (g n) = Re (g n)"
    using assms by (metis abs_of_nonneg in_Reals_norm)
  show ?thesis
    apply (rule summable_comparison_test' [where g = "\<lambda>n. norm (g n)" and N=N])
    using sg
     apply (auto simp: summable_def)
     apply (rule_tac x = "Re s" in exI)
     apply (auto simp: g sums_Re)
    apply (metis fg g)
    done
qed

subsection \<open>Polar Form for Complex Numbers\<close>

lemma complex_unimodular_polar:
  assumes "norm z = 1"
  obtains t where "0 \<le> t" "t < 2 * pi" "z = Complex (cos t) (sin t)"
  by (metis cmod_power2 one_power2 complex_surj sincos_total_2pi [of "Re z" "Im z"] assms)

subsubsection \<open>$\cos \theta + i \sin \theta$\<close>

primcorec cis :: "real \<Rightarrow> complex"
  where
    "Re (cis a) = cos a"
  | "Im (cis a) = sin a"

lemma cis_zero [simp]: "cis 0 = 1"
  by (simp add: complex_eq_iff)

lemma norm_cis [simp]: "norm (cis a) = 1"
  by (simp add: norm_complex_def)

lemma sgn_cis [simp]: "sgn (cis a) = cis a"
  by (simp add: sgn_div_norm)

lemma cis_neq_zero [simp]: "cis a \<noteq> 0"
  by (metis norm_cis norm_zero zero_neq_one)

lemma cis_mult: "cis a * cis b = cis (a + b)"
  by (simp add: complex_eq_iff cos_add sin_add)

lemma DeMoivre: "(cis a) ^ n = cis (real n * a)"
  by (induct n) (simp_all add: algebra_simps cis_mult)

lemma cis_inverse [simp]: "inverse (cis a) = cis (- a)"
  by (simp add: complex_eq_iff)

lemma cis_divide: "cis a / cis b = cis (a - b)"
  by (simp add: divide_complex_def cis_mult)

lemma cos_n_Re_cis_pow_n: "cos (real n * a) = Re (cis a ^ n)"
  by (auto simp add: DeMoivre)

lemma sin_n_Im_cis_pow_n: "sin (real n * a) = Im (cis a ^ n)"
  by (auto simp add: DeMoivre)

lemma cis_pi: "cis pi = -1"
  by (simp add: complex_eq_iff)

subsubsection \<open>$r(\cos \theta + i \sin \theta)$\<close>

definition rcis :: "real \<Rightarrow> real \<Rightarrow> complex"
  where "rcis r a = complex_of_real r * cis a"

lemma Re_rcis [simp]: "Re(rcis r a) = r * cos a"
  by (simp add: rcis_def)

lemma Im_rcis [simp]: "Im(rcis r a) = r * sin a"
  by (simp add: rcis_def)

lemma rcis_Ex: "\<exists>r a. z = rcis r a"
  by (simp add: complex_eq_iff polar_Ex)

lemma complex_mod_rcis [simp]: "cmod (rcis r a) = \<bar>r\<bar>"
  by (simp add: rcis_def norm_mult)

lemma cis_rcis_eq: "cis a = rcis 1 a"
  by (simp add: rcis_def)

lemma rcis_mult: "rcis r1 a * rcis r2 b = rcis (r1 * r2) (a + b)"
  by (simp add: rcis_def cis_mult)

lemma rcis_zero_mod [simp]: "rcis 0 a = 0"
  by (simp add: rcis_def)

lemma rcis_zero_arg [simp]: "rcis r 0 = complex_of_real r"
  by (simp add: rcis_def)

lemma rcis_eq_zero_iff [simp]: "rcis r a = 0 \<longleftrightarrow> r = 0"
  by (simp add: rcis_def)

lemma DeMoivre2: "(rcis r a) ^ n = rcis (r ^ n) (real n * a)"
  by (simp add: rcis_def power_mult_distrib DeMoivre)

lemma rcis_inverse: "inverse(rcis r a) = rcis (1 / r) (- a)"
  by (simp add: divide_inverse rcis_def)

lemma rcis_divide: "rcis r1 a / rcis r2 b = rcis (r1 / r2) (a - b)"
  by (simp add: rcis_def cis_divide [symmetric])

subsubsection \<open>Complex exponential\<close>

lemma cis_conv_exp: "cis b = exp (\<i> * b)"
proof -
  have "(\<i> * complex_of_real b) ^ n /\<^sub>R fact n =
      of_real (cos_coeff n * b^n) + \<i> * of_real (sin_coeff n * b^n)"
    for n :: nat
  proof -
    have "\<i> ^ n = fact n *\<^sub>R (cos_coeff n + \<i> * sin_coeff n)"
      by (induct n)
        (simp_all add: sin_coeff_Suc cos_coeff_Suc complex_eq_iff Re_divide Im_divide field_simps
          power2_eq_square add_nonneg_eq_0_iff)
    then show ?thesis
      by (simp add: field_simps)
  qed
  then show ?thesis
    using sin_converges [of b] cos_converges [of b]
    by (auto simp add: cis.ctr exp_def simp del: of_real_mult
        intro!: sums_unique sums_add sums_mult sums_of_real)
qed

lemma exp_eq_polar: "exp z = exp (Re z) * cis (Im z)"
  unfolding cis_conv_exp exp_of_real [symmetric] mult_exp_exp
  by (cases z) simp

lemma Re_exp: "Re (exp z) = exp (Re z) * cos (Im z)"
  unfolding exp_eq_polar by simp

lemma Im_exp: "Im (exp z) = exp (Re z) * sin (Im z)"
  unfolding exp_eq_polar by simp

lemma norm_cos_sin [simp]: "norm (Complex (cos t) (sin t)) = 1"
  by (simp add: norm_complex_def)

lemma norm_exp_eq_Re [simp]: "norm (exp z) = exp (Re z)"
  by (simp add: cis.code cmod_complex_polar exp_eq_polar)

lemma complex_exp_exists: "\<exists>a r. z = complex_of_real r * exp a"
  apply (insert rcis_Ex [of z])
  apply (auto simp add: exp_eq_polar rcis_def mult.assoc [symmetric])
  apply (rule_tac x = "\<i> * complex_of_real a" in exI)
  apply auto
  done

lemma exp_pi_i [simp]: "exp (of_real pi * \<i>) = -1"
  by (metis cis_conv_exp cis_pi mult.commute)

lemma exp_pi_i' [simp]: "exp (\<i> * of_real pi) = -1"
  using cis_conv_exp cis_pi by auto

lemma exp_two_pi_i [simp]: "exp (2 * of_real pi * \<i>) = 1"
  by (simp add: exp_eq_polar complex_eq_iff)

lemma exp_two_pi_i' [simp]: "exp (\<i> * (of_real pi * 2)) = 1"
  by (metis exp_two_pi_i mult.commute)

subsubsection \<open>Complex argument\<close>

definition arg :: "complex \<Rightarrow> real"
  where "arg z = (if z = 0 then 0 else (SOME a. sgn z = cis a \<and> - pi < a \<and> a \<le> pi))"

lemma arg_zero: "arg 0 = 0"
  by (simp add: arg_def)

lemma arg_unique:
  assumes "sgn z = cis x" and "-pi < x" and "x \<le> pi"
  shows "arg z = x"
proof -
  from assms have "z \<noteq> 0" by auto
  have "(SOME a. sgn z = cis a \<and> -pi < a \<and> a \<le> pi) = x"
  proof
    fix a
    define d where "d = a - x"
    assume a: "sgn z = cis a \<and> - pi < a \<and> a \<le> pi"
    from a assms have "- (2*pi) < d \<and> d < 2*pi"
      unfolding d_def by simp
    moreover
    from a assms have "cos a = cos x" and "sin a = sin x"
      by (simp_all add: complex_eq_iff)
    then have cos: "cos d = 1"
      by (simp add: d_def cos_diff)
    moreover from cos have "sin d = 0"
      by (rule cos_one_sin_zero)
    ultimately have "d = 0"
      by (auto simp: sin_zero_iff elim!: evenE dest!: less_2_cases)
    then show "a = x"
      by (simp add: d_def)
  qed (simp add: assms del: Re_sgn Im_sgn)
  with \<open>z \<noteq> 0\<close> show "arg z = x"
    by (simp add: arg_def)
qed

lemma arg_correct:
  assumes "z \<noteq> 0"
  shows "sgn z = cis (arg z) \<and> -pi < arg z \<and> arg z \<le> pi"
proof (simp add: arg_def assms, rule someI_ex)
  obtain r a where z: "z = rcis r a"
    using rcis_Ex by fast
  with assms have "r \<noteq> 0" by auto
  define b where "b = (if 0 < r then a else a + pi)"
  have b: "sgn z = cis b"
    using \<open>r \<noteq> 0\<close> by (simp add: z b_def rcis_def of_real_def sgn_scaleR sgn_if complex_eq_iff)
  have cis_2pi_nat: "cis (2 * pi * real_of_nat n) = 1" for n
    by (induct n) (simp_all add: distrib_left cis_mult [symmetric] complex_eq_iff)
  have cis_2pi_int: "cis (2 * pi * real_of_int x) = 1" for x
    by (cases x rule: int_diff_cases)
      (simp add: right_diff_distrib cis_divide [symmetric] cis_2pi_nat)
  define c where "c = b - 2 * pi * of_int \<lceil>(b - pi) / (2 * pi)\<rceil>"
  have "sgn z = cis c"
    by (simp add: b c_def cis_divide [symmetric] cis_2pi_int)
  moreover have "- pi < c \<and> c \<le> pi"
    using ceiling_correct [of "(b - pi) / (2*pi)"]
    by (simp add: c_def less_divide_eq divide_le_eq algebra_simps del: le_of_int_ceiling)
  ultimately show "\<exists>a. sgn z = cis a \<and> -pi < a \<and> a \<le> pi"
    by fast
qed

lemma arg_bounded: "- pi < arg z \<and> arg z \<le> pi"
  by (cases "z = 0") (simp_all add: arg_zero arg_correct)

lemma cis_arg: "z \<noteq> 0 \<Longrightarrow> cis (arg z) = sgn z"
  by (simp add: arg_correct)

lemma rcis_cmod_arg: "rcis (cmod z) (arg z) = z"
  by (cases "z = 0") (simp_all add: rcis_def cis_arg sgn_div_norm of_real_def)

lemma cos_arg_i_mult_zero [simp]: "y \<noteq> 0 \<Longrightarrow> Re y = 0 \<Longrightarrow> cos (arg y) = 0"
  using cis_arg [of y] by (simp add: complex_eq_iff)

subsection \<open>Square root of complex numbers\<close>

primcorec csqrt :: "complex \<Rightarrow> complex"
  where
    "Re (csqrt z) = sqrt ((cmod z + Re z) / 2)"
  | "Im (csqrt z) = (if Im z = 0 then 1 else sgn (Im z)) * sqrt ((cmod z - Re z) / 2)"

lemma csqrt_of_real_nonneg [simp]: "Im x = 0 \<Longrightarrow> Re x \<ge> 0 \<Longrightarrow> csqrt x = sqrt (Re x)"
  by (simp add: complex_eq_iff norm_complex_def)

lemma csqrt_of_real_nonpos [simp]: "Im x = 0 \<Longrightarrow> Re x \<le> 0 \<Longrightarrow> csqrt x = \<i> * sqrt \<bar>Re x\<bar>"
  by (simp add: complex_eq_iff norm_complex_def)

lemma of_real_sqrt: "x \<ge> 0 \<Longrightarrow> of_real (sqrt x) = csqrt (of_real x)"
  by (simp add: complex_eq_iff norm_complex_def)

lemma csqrt_0 [simp]: "csqrt 0 = 0"
  by simp

lemma csqrt_1 [simp]: "csqrt 1 = 1"
  by simp

lemma csqrt_ii [simp]: "csqrt \<i> = (1 + \<i>) / sqrt 2"
  by (simp add: complex_eq_iff Re_divide Im_divide real_sqrt_divide real_div_sqrt)

lemma power2_csqrt[simp,algebra]: "(csqrt z)\<^sup>2 = z"
proof (cases "Im z = 0")
  case True
  then show ?thesis
    using real_sqrt_pow2[of "Re z"] real_sqrt_pow2[of "- Re z"]
    by (cases "0::real" "Re z" rule: linorder_cases)
      (simp_all add: complex_eq_iff Re_power2 Im_power2 power2_eq_square cmod_eq_Re)
next
  case False
  moreover have "cmod z * cmod z - Re z * Re z = Im z * Im z"
    by (simp add: norm_complex_def power2_eq_square)
  moreover have "\<bar>Re z\<bar> \<le> cmod z"
    by (simp add: norm_complex_def)
  ultimately show ?thesis
    by (simp add: Re_power2 Im_power2 complex_eq_iff real_sgn_eq
        field_simps real_sqrt_mult[symmetric] real_sqrt_divide)
qed

lemma csqrt_eq_0 [simp]: "csqrt z = 0 \<longleftrightarrow> z = 0"
  by auto (metis power2_csqrt power_eq_0_iff)

lemma csqrt_eq_1 [simp]: "csqrt z = 1 \<longleftrightarrow> z = 1"
  by auto (metis power2_csqrt power2_eq_1_iff)

lemma csqrt_principal: "0 < Re (csqrt z) \<or> Re (csqrt z) = 0 \<and> 0 \<le> Im (csqrt z)"
  by (auto simp add: not_less cmod_plus_Re_le_0_iff Im_eq_0)

lemma Re_csqrt: "0 \<le> Re (csqrt z)"
  by (metis csqrt_principal le_less)

lemma csqrt_square:
  assumes "0 < Re b \<or> (Re b = 0 \<and> 0 \<le> Im b)"
  shows "csqrt (b^2) = b"
proof -
  have "csqrt (b^2) = b \<or> csqrt (b^2) = - b"
    by (simp add: power2_eq_iff[symmetric])
  moreover have "csqrt (b^2) \<noteq> -b \<or> b = 0"
    using csqrt_principal[of "b ^ 2"] assms
    by (intro disjCI notI) (auto simp: complex_eq_iff)
  ultimately show ?thesis
    by auto
qed

lemma csqrt_unique: "w\<^sup>2 = z \<Longrightarrow> 0 < Re w \<or> Re w = 0 \<and> 0 \<le> Im w \<Longrightarrow> csqrt z = w"
  by (auto simp: csqrt_square)

lemma csqrt_minus [simp]:
  assumes "Im x < 0 \<or> (Im x = 0 \<and> 0 \<le> Re x)"
  shows "csqrt (- x) = \<i> * csqrt x"
proof -
  have "csqrt ((\<i> * csqrt x)^2) = \<i> * csqrt x"
  proof (rule csqrt_square)
    have "Im (csqrt x) \<le> 0"
      using assms by (auto simp add: cmod_eq_Re mult_le_0_iff field_simps complex_Re_le_cmod)
    then show "0 < Re (\<i> * csqrt x) \<or> Re (\<i> * csqrt x) = 0 \<and> 0 \<le> Im (\<i> * csqrt x)"
      by (auto simp add: Re_csqrt simp del: csqrt.simps)
  qed
  also have "(\<i> * csqrt x)^2 = - x"
    by (simp add: power_mult_distrib)
  finally show ?thesis .
qed

text \<open>Legacy theorem names\<close>

lemmas expand_complex_eq = complex_eq_iff
lemmas complex_Re_Im_cancel_iff = complex_eq_iff
lemmas complex_equality = complex_eqI
lemmas cmod_def = norm_complex_def
lemmas complex_norm_def = norm_complex_def
lemmas complex_divide_def = divide_complex_def

lemma legacy_Complex_simps:
  shows Complex_eq_0: "Complex a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
    and complex_add: "Complex a b + Complex c d = Complex (a + c) (b + d)"
    and complex_minus: "- (Complex a b) = Complex (- a) (- b)"
    and complex_diff: "Complex a b - Complex c d = Complex (a - c) (b - d)"
    and Complex_eq_1: "Complex a b = 1 \<longleftrightarrow> a = 1 \<and> b = 0"
    and Complex_eq_neg_1: "Complex a b = - 1 \<longleftrightarrow> a = - 1 \<and> b = 0"
    and complex_mult: "Complex a b * Complex c d = Complex (a * c - b * d) (a * d + b * c)"
    and complex_inverse: "inverse (Complex a b) = Complex (a / (a\<^sup>2 + b\<^sup>2)) (- b / (a\<^sup>2 + b\<^sup>2))"
    and Complex_eq_numeral: "Complex a b = numeral w \<longleftrightarrow> a = numeral w \<and> b = 0"
    and Complex_eq_neg_numeral: "Complex a b = - numeral w \<longleftrightarrow> a = - numeral w \<and> b = 0"
    and complex_scaleR: "scaleR r (Complex a b) = Complex (r * a) (r * b)"
    and Complex_eq_i: "Complex x y = \<i> \<longleftrightarrow> x = 0 \<and> y = 1"
    and i_mult_Complex: "\<i> * Complex a b = Complex (- b) a"
    and Complex_mult_i: "Complex a b * \<i> = Complex (- b) a"
    and i_complex_of_real: "\<i> * complex_of_real r = Complex 0 r"
    and complex_of_real_i: "complex_of_real r * \<i> = Complex 0 r"
    and Complex_add_complex_of_real: "Complex x y + complex_of_real r = Complex (x+r) y"
    and complex_of_real_add_Complex: "complex_of_real r + Complex x y = Complex (r+x) y"
    and Complex_mult_complex_of_real: "Complex x y * complex_of_real r = Complex (x*r) (y*r)"
    and complex_of_real_mult_Complex: "complex_of_real r * Complex x y = Complex (r*x) (r*y)"
    and complex_eq_cancel_iff2: "(Complex x y = complex_of_real xa) = (x = xa \<and> y = 0)"
    and complex_cn: "cnj (Complex a b) = Complex a (- b)"
    and Complex_sum': "sum (\<lambda>x. Complex (f x) 0) s = Complex (sum f s) 0"
    and Complex_sum: "Complex (sum f s) 0 = sum (\<lambda>x. Complex (f x) 0) s"
    and complex_of_real_def: "complex_of_real r = Complex r 0"
    and complex_norm: "cmod (Complex x y) = sqrt (x\<^sup>2 + y\<^sup>2)"
  by (simp_all add: norm_complex_def field_simps complex_eq_iff Re_divide Im_divide del: Complex_eq)

lemma Complex_in_Reals: "Complex x 0 \<in> \<real>"
  by (metis Reals_of_real complex_of_real_def)

end


theory Interest
  imports Preliminaries
begin

section \<open>List of Actuarial Notations (Global Scope)\<close>

definition i_nom :: "real \<Rightarrow> nat \<Rightarrow> real" ("$i[_]^{_}" [0,0] 200)
  where "$i[i]^{m} \<equiv> m * ((1+i).^(1/m) - 1)"  \<comment> \<open>nominal interest rate\<close>
definition i_force :: "real \<Rightarrow> real" ("$\<delta>[_]" [0] 200)
  where "$\<delta>[i] \<equiv> ln (1+i)" \<comment> \<open>force of interest\<close>
definition d_nom :: "real \<Rightarrow> nat \<Rightarrow> real" ("$d[_]^{_}" [0,0] 200)
  where "$d[i]^{m} \<equiv> $i[i]^{m} / (1 + $i[i]^{m}/m)"  \<comment> \<open>discount rate\<close>
abbreviation d_nom_yr :: "real \<Rightarrow> real" ("$d[_]" [0] 200)
  where "$d[i] \<equiv> $d[i]^{1}"  \<comment> \<open>Post-fix "yr" stands for "year".\<close>
definition v_pres :: "real \<Rightarrow> real" ("$v[_]" [0] 200)
  where "$v[i] \<equiv> 1 / (1+i)"  \<comment> \<open>present value factor\<close>
definition ann :: "real \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real" ("$a[_]^{_}'__" [0,0,101] 200)
  where "$a[i]^{m}_n \<equiv> \<Sum>k<n*m. $v[i].^((k+1::nat)/m) / m"
    \<comment> \<open>present value of an immediate annuity\<close>
abbreviation ann_yr :: "real \<Rightarrow> nat \<Rightarrow> real" ("$a[_]'__" [0,101] 200)
  where "$a[i]_n \<equiv> $a[i]^{1}_n"
definition acc :: "real \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real" ("$s[_]^{_}'__" [0,0,101] 200)
  where "$s[i]^{m}_n \<equiv> \<Sum>k<n*m. (1+i).^((k::nat)/m) / m"
    \<comment> \<open>future value of an immediate annuity\<close>
    \<comment> \<open>The name "acc" stands for "accumulation".\<close>
abbreviation acc_yr :: "real \<Rightarrow> nat \<Rightarrow> real" ("$s[_]'__" [0] 200)
  where "$s[i]_n \<equiv> $s[i]^{1}_n"
definition ann_due :: "real \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real" ("$a''''[_]^{_}'__" [0,0,101] 200)
  where "$a''[i]^{m}_n \<equiv> \<Sum>k<n*m. $v[i].^((k::nat)/m) / m"
    \<comment> \<open>present value of an annuity-due\<close>
abbreviation ann_due_yr :: "real \<Rightarrow> nat \<Rightarrow> real" ("$a''''[_]'__" [0,101] 200)
  where "$a''[i]_n \<equiv> $a''[i]^{1}_n"
definition acc_due :: "real \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real" ("$s''''[_]^{_}'__" [0,0,101] 200)
  where "$s''[i]^{m}_n \<equiv> \<Sum>k<n*m. (1+i).^((k+1::nat)/m) / m"
    \<comment> \<open>future value of an annuity-due\<close>
abbreviation acc_due_yr :: "real \<Rightarrow> nat \<Rightarrow> real" ("$s''''[_]'__" [0,101] 200)
  where "$s''[i]_n \<equiv> $s''[i]^{1}_n"
definition ann_cont :: "real \<Rightarrow> real \<Rightarrow> real" ("$a''[_]'__" [0,101] 200)
  where "$a'[i]_n \<equiv> integral {0..n} (\<lambda>t::real. $v[i].^t)"
    \<comment> \<open>present value of a continuous annuity\<close>
definition acc_cont :: "real \<Rightarrow> real \<Rightarrow> real" ("$s''[_]'__" [0,101] 200)
  where "$s'[i]_n \<equiv> integral {0..n} (\<lambda>t::real. (1+i).^t)"
    \<comment> \<open>future value of a continuous annuity\<close>
definition perp :: "real \<Rightarrow> nat \<Rightarrow> real" ("$a[_]^{_}'_\<infinity>" [0,0] 200)
  where "$a[i]^{m}_\<infinity> \<equiv> 1 / $i[i]^{m}"
    \<comment> \<open>present value of a perpetual annuity\<close>
abbreviation perp_yr :: "real \<Rightarrow> real" ("$a[_]'_\<infinity>" [0] 200)
  where "$a[i]_\<infinity> \<equiv> $a[i]^{1}_\<infinity>"
definition perp_due :: "real \<Rightarrow> nat \<Rightarrow> real" ("$a''''[_]^{_}'_\<infinity>" [0,0] 200)
  where "$a''[i]^{m}_\<infinity> \<equiv> 1 / $d[i]^{m}"
    \<comment> \<open>present value of a perpetual annuity-due\<close>
abbreviation perp_due_yr :: "real \<Rightarrow> real" ("$a''''[_]'_\<infinity>" [0] 200)
  where "$a''[i]_\<infinity> \<equiv> $a''[i]^{1}_\<infinity>"
definition ann_incr :: "nat \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real"
  ("$'(I^{_}a')[_]^{_}'__" [0,0,0,101] 200)
  where "$(I^{l}a)[i]^{m}_n \<equiv> \<Sum>k<n*m. $v[i].^((k+1::nat)/m) * \<lceil>l*(k+1::nat)/m\<rceil> / (l*m)"
    \<comment> \<open>present value of an increasing annuity\<close>
    \<comment> \<open>This is my original definition.\<close>
    \<comment> \<open>Here, "l" represents the number of increments per unit time.\<close>
abbreviation ann_incr_lvl :: "real \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real"
  ("$'(Ia')[_]^{_}'__" [0,0,101] 200)
  where "$(Ia)[i]^{m}_n \<equiv> $(I^{1}a)[i]^{m}_n"
    \<comment> \<open>The post-fix "lvl" stands for "level".\<close>
abbreviation ann_incr_yr :: "real \<Rightarrow> nat \<Rightarrow> real" ("$'(Ia')[_]'__" [0,101] 200)
  where "$(Ia)[i]_n \<equiv> $(Ia)[i]^{1}_n"
definition acc_incr :: "nat \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real"
  ("$'(I^{_}s')[_]^{_}'__" [0,0,0,101] 200)
  where "$(I^{l}s)[i]^{m}_n \<equiv> \<Sum>k<n*m. (1+i).^(n-(k+1::nat)/m) * \<lceil>l*(k+1::nat)/m\<rceil> / (l*m)"
    \<comment> \<open>future value of an increasing annuity\<close>
abbreviation acc_incr_lvl :: "real \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real"
  ("$'(Is')[_]^{_}'__" [0,0,101] 200)
  where "$(Is)[i]^{m}_n \<equiv> $(I^{1}s)[i]^{m}_n"
abbreviation acc_incr_yr :: "real \<Rightarrow> nat \<Rightarrow> real" ("$'(Is')[_]'__" [0,101] 200)
  where "$(Is)[i]_n \<equiv> $(Is)[i]^{1}_n"
definition ann_due_incr :: "nat \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real"
  ("$'(I^{_}a''''')[_]^{_}'__" [0,0,0,101] 200)
  where "$(I^{l}a'')[i]^{m}_n \<equiv> \<Sum>k<n*m. $v[i].^((k::nat)/m) * \<lceil>l*(k+1::nat)/m\<rceil> / (l*m)"
abbreviation ann_due_incr_lvl :: "real \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real"
  ("$'(Ia''''')[_]^{_}'__" [0,0,101] 200)
  where "$(Ia'')[i]^{m}_n \<equiv> $(I^{1}a'')[i]^{m}_n"
abbreviation ann_due_incr_yr :: "real \<Rightarrow> nat \<Rightarrow> real" ("$'(Ia''''')[_]'__" [0,101] 200)
  where "$(Ia'')[i]_n \<equiv> $(Ia'')[i]^{1}_n"
definition acc_due_incr :: "nat \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real"
  ("$'(I^{_}s''''')[_]^{_}'__" [0,0,0,101] 200)
  where "$(I^{l}s'')[i]^{m}_n \<equiv> \<Sum>k<n*m. (1+i).^(n-(k::nat)/m) * \<lceil>l*(k+1::nat)/m\<rceil> / (l*m)"
abbreviation acc_due_incr_lvl :: "real \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real"
  ("$'(Is''''')[_]^{_}'__" [0,0,101] 200)
  where "$(Is'')[i]^{m}_n \<equiv> $(I^{1}s'')[i]^{m}_n"
abbreviation acc_due_incr_yr :: "real \<Rightarrow> nat \<Rightarrow> real" ("$'(Is''''')[_]'__" [0,101] 200)
  where "$(Is'')[i]_n \<equiv> $(Is'')[i]^{1}_n"
definition perp_incr :: "nat \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> real" ("$'(I^{_}a')[_]^{_}'_\<infinity>" [0,0,0] 200)
  where "$(I^{l}a)[i]^{m}_\<infinity> \<equiv> lim (\<lambda>n. $(I^{l}a)[i]^{m}_n)"
abbreviation perp_incr_lvl :: "real \<Rightarrow> nat \<Rightarrow> real" ("$'(Ia')[_]^{_}'_\<infinity>" [0,0] 200)
  where "$(Ia)[i]^{m}_\<infinity> \<equiv> $(I^{1}a)[i]^{m}_\<infinity>"
abbreviation perp_incr_yr :: "real \<Rightarrow> real" ("$'(Ia')[_]'_\<infinity>" [0] 200)
  where "$(Ia)[i]_\<infinity> \<equiv> $(Ia)[i]^{1}_\<infinity>"
definition perp_due_incr :: "nat \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> real" ("$'(I^{_}a''''')[_]^{_}'_\<infinity>" [0,0,0] 200)
  where "$(I^{l}a'')[i]^{m}_\<infinity> \<equiv> lim (\<lambda>n. $(I^{l}a'')[i]^{m}_n)"
abbreviation perp_due_incr_lvl :: "real \<Rightarrow> nat \<Rightarrow> real" ("$'(Ia''''')[_]^{_}'_\<infinity>" [0,0] 200)
  where "$(Ia'')[i]^{m}_\<infinity> \<equiv> $(I^{1}a'')[i]^{m}_\<infinity>"
abbreviation perp_due_incr_yr :: "real \<Rightarrow> real" ("$'(Ia''''')[_]'_\<infinity>" [0] 200)
  where "$(Ia'')[i]_\<infinity> \<equiv> $(Ia'')[i]^{1}_\<infinity>"

section \<open>Theory of Interest\<close>

locale interest =
  fixes i :: real  \<comment> \<open>i stands for an interest rate.\<close>
  assumes v_futr_pos: "1 + i > 0"  \<comment> \<open>Assume that the future value is positive.\<close>

context interest
begin

abbreviation i_nom' :: "nat \<Rightarrow> real" ("$i^{_}" [0] 200)
  where "$i^{m} \<equiv> $i[i]^{m}"
abbreviation i_force' :: real ("$\<delta>")
  where "$\<delta> \<equiv> $\<delta>[i]"
abbreviation d_nom' :: "nat \<Rightarrow> real" ("$d^{_}" [0] 200)
  where "$d^{m} \<equiv> $d[i]^{m}"
abbreviation d_nom_yr' :: real ("$d")
  where "$d \<equiv> $d[i]"
abbreviation v_pres' :: real ("$v")
  where "$v \<equiv> $v[i]"
abbreviation ann' :: "nat \<Rightarrow> nat \<Rightarrow> real" ("$a^{_}'__" [0,101] 200)
  where "$a^{m}_n \<equiv> $a[i]^{m}_n"
abbreviation ann_yr' :: "nat \<Rightarrow> real" ("$a'__" [101] 200)
  where "$a_n \<equiv> $a[i]_n"
abbreviation acc' :: "nat \<Rightarrow> nat \<Rightarrow> real" ("$s^{_}'__" [0,101] 200)
  where "$s^{m}_n \<equiv> $s[i]^{m}_n"
abbreviation acc_yr' :: "nat \<Rightarrow> real" ("$s'__" [101] 200)
  where "$s_n \<equiv> $s[i]_n"
abbreviation ann_due' :: "nat \<Rightarrow> nat \<Rightarrow> real" ("$a''''^{_}'__" [0,101] 200)
  where "$a''^{m}_n \<equiv> $a''[i]^{m}_n"
abbreviation ann_due_yr' :: "nat \<Rightarrow> real" ("$a'''''__" [101] 200)
  where "$a''_n \<equiv> $a''[i]_n"
abbreviation acc_due' :: "nat \<Rightarrow> nat \<Rightarrow> real" ("$s''''^{_}'__" [0,101] 200)
  where "$s''^{m}_n \<equiv> $s''[i]^{m}_n"
abbreviation acc_due_yr' :: "nat \<Rightarrow> real" ("$s'''''__" [101] 200)
  where "$s''_n \<equiv> $s''[i]_n"
abbreviation ann_cont' :: "real \<Rightarrow> real" ("$a'''__" [101] 200)
  where "$a'_n \<equiv> $a'[i]_n"
abbreviation acc_cont' :: "real \<Rightarrow> real" ("$s'''__" [101] 200)
  where "$s'_n \<equiv> $s'[i]_n"
abbreviation perp' :: "nat \<Rightarrow> real" ("$a^{_}'_\<infinity>" [0] 200)
  where "$a^{m}_\<infinity> \<equiv> $a[i]^{m}_\<infinity>"
abbreviation perp_yr' :: real ("$a'_\<infinity>")
  where "$a_\<infinity> \<equiv> $a[i]_\<infinity>"
abbreviation perp_due' :: "nat \<Rightarrow> real" ("$a''''^{_}'_\<infinity>" [0] 200)
  where "$a''^{m}_\<infinity> \<equiv> $a''[i]^{m}_\<infinity>"
abbreviation perp_due_yr' :: real ("$a'''''_\<infinity>")
  where "$a''_\<infinity> \<equiv> $a''[i]_\<infinity>"
abbreviation ann_incr' :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real" ("$'(I^{_}a')^{_}'__" [0,0,101] 200)
  where "$(I^{l}a)^{m}_n \<equiv> $(I^{l}a)[i]^{m}_n"
abbreviation ann_incr_lvl' :: "nat \<Rightarrow> nat \<Rightarrow> real" ("$'(Ia')^{_}'__" [0,101] 200)
  where "$(Ia)^{m}_n \<equiv> $(Ia)[i]^{m}_n"
abbreviation ann_incr_yr' :: "nat \<Rightarrow> real" ("$'(Ia')'__" [101] 200)
  where "$(Ia)_n \<equiv> $(Ia)[i]_n"
abbreviation acc_incr' :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real" ("$'(I^{_}s')^{_}'__" [0,0,101] 200)
  where "$(I^{l}s)^{m}_n \<equiv> $(I^{l}s)[i]^{m}_n"
abbreviation acc_incr_lvl' :: "nat \<Rightarrow> nat \<Rightarrow> real" ("$'(Is')^{_}'__" [0,101] 200)
  where "$(Is)^{m}_n \<equiv> $(Is)[i]^{m}_n"
abbreviation acc_incr_yr' :: "nat \<Rightarrow> real" ("$'(Is')'__" [101] 200)
  where "$(Is)_n \<equiv> $(Is)[i]_n"
abbreviation ann_due_incr' :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real" ("$'(I^{_}a''''')^{_}'__" [0,0,101] 200)
  where "$(I^{l}a'')^{m}_n \<equiv> $(I^{l}a'')[i]^{m}_n"
abbreviation ann_due_incr_lvl' :: "nat \<Rightarrow> nat \<Rightarrow> real" ("$'(Ia''''')^{_}'__" [0,101] 200)
  where "$(Ia'')^{m}_n \<equiv> $(Ia'')[i]^{m}_n"
abbreviation ann_due_incr_yr' :: "nat \<Rightarrow> real" ("$'(Ia''''')'__" [101] 200)
  where "$(Ia'')_n \<equiv> $(Ia'')[i]_n"
abbreviation acc_due_incr' :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real" ("$'(I^{_}s''''')^{_}'__" [0,0,101] 200)
  where "$(I^{l}s'')^{m}_n \<equiv> $(I^{l}s'')[i]^{m}_n"
abbreviation acc_due_incr_lvl' :: "nat \<Rightarrow> nat \<Rightarrow> real" ("$'(Is''''')^{_}'__" [0,101] 200)
  where "$(Is'')^{m}_n \<equiv> $(Is'')[i]^{m}_n"
abbreviation acc_due_incr_yr' :: "nat \<Rightarrow> real" ("$'(Is''''')'__" [101] 200)
  where "$(Is'')_n \<equiv> $(Is'')[i]_n"
abbreviation perp_incr' :: "nat \<Rightarrow> nat \<Rightarrow> real" ("$'(I^{_}a')^{_}'_\<infinity>" [0,0] 200)
  where "$(I^{l}a)^{m}_\<infinity> \<equiv> $(I^{l}a)[i]^{m}_\<infinity>"
abbreviation perp_incr_lvl' :: "nat \<Rightarrow> real" ("$'(Ia')^{_}'_\<infinity>" [0] 200)
  where "$(Ia)^{m}_\<infinity> \<equiv> $(Ia)[i]^{m}_\<infinity>"
abbreviation perp_incr_yr' :: real ("$'(Ia')'_\<infinity>")
  where "$(Ia)_\<infinity> \<equiv> $(Ia)[i]_\<infinity>"
abbreviation perp_due_incr' :: "nat \<Rightarrow> nat \<Rightarrow> real" ("$'(I^{_}a''''')^{_}'_\<infinity>" [0,0] 200)
  where "$(I^{l}a'')^{m}_\<infinity> \<equiv> $(I^{l}a'')[i]^{m}_\<infinity>"
abbreviation perp_due_incr_lvl' :: "nat \<Rightarrow> real" ("$'(Ia''''')^{_}'_\<infinity>" [0] 200)
  where "$(Ia'')^{m}_\<infinity> \<equiv> $(Ia'')[i]^{m}_\<infinity>"
abbreviation perp_due_incr_yr' :: real ("$'(Ia''''')'_\<infinity>")
  where "$(Ia'')_\<infinity> \<equiv> $(Ia'')[i]_\<infinity>"

lemma v_futr_m_pos: "1 + $i^{m}/m > 0" if "m \<noteq> 0" for m::nat
  using v_futr_pos i_nom_def by force

lemma i_nom_1[simp]: "$i^{1} = i"
  using v_futr_pos i_nom_def by force

lemma i_nom_eff: "(1 + $i^{m}/m)^m = 1 + i" if "m \<noteq> 0" for m::nat
  unfolding i_nom_def using less_imp_neq v_futr_pos that
  apply (simp, subst powr_realpow[THEN sym], simp)
  by (subst powr_powr, simp)

lemma i_nom_i: "1 + $i^{m}/m = (1+i).^(1/m)" if "m \<noteq> 0" for m::nat
  unfolding i_nom_def by (simp add: that)

lemma i_nom_0_iff_i_0: "$i^{m} = 0 \<longleftrightarrow> i = 0" if "m \<noteq> 0" for m::nat
proof
  assume "$i^{m} = 0"
  hence \<star>: "(1+i).^(1/m) = (1+i).^0"
    unfolding i_nom_def using v_futr_pos that by simp
  show "i = 0"
  proof (rule ccontr)
    assume "i \<noteq> 0"
    hence "1/m = 0" using powr_inj \<star> v_futr_pos by smt
    thus False using that by simp
  qed
next
  assume "i = 0"
  thus "$i^{m} = 0"
    unfolding i_nom_def by simp
qed

lemma i_nom_pos_iff_i_pos: "$i^{m} > 0 \<longleftrightarrow> i > 0" if "m \<noteq> 0" for m::nat
proof
  assume "$i^{m} > 0"
  hence \<star>: "(1+i).^(1/m) > 1.^(1/m)"
    unfolding i_nom_def using v_futr_pos that by (simp add: zero_less_mult_iff)
  thus "i > 0"
    using powr_less_cancel2[of "1/m" 1 "1+i"] v_futr_pos that by simp
next
  assume "i > 0"
  hence "(1+i).^(1/m) > 1.^(1/m)"
    using powr_less_mono2 v_futr_pos that by simp
  thus "$i^{m} > 0"
    unfolding i_nom_def using that by (simp add: zero_less_mult_iff)
qed

lemma e_delta: "exp $\<delta> = 1 + i"
  unfolding i_force_def by (simp add: v_futr_pos)

lemma delta_0_iff_i_0: "$\<delta> = 0 \<longleftrightarrow> i = 0"
proof
  assume "$\<delta> = 0"
  thus "i = 0"
    using e_delta by auto
next
  assume "i = 0"
  thus "$\<delta> = 0"
    unfolding i_force_def by simp
qed

lemma lim_i_nom: "(\<lambda>m. $i^{m}) \<longlonglongrightarrow> $\<delta>"
proof -
  let ?f = "\<lambda>h. ((1+i).^h - 1) / h"
  have D1ipwr: "DERIV (\<lambda>h. (1+i).^h) 0 :> $\<delta>"
    unfolding i_force_def
    using has_real_derivative_powr2[OF v_futr_pos, where x=0] v_futr_pos by simp
  hence limf: "(?f \<longlongrightarrow> $\<delta>) (at 0)"
    unfolding DERIV_def using v_futr_pos by auto
  hence "(\<lambda>m. $i^{Suc m}) \<longlonglongrightarrow> $\<delta>"
    unfolding i_nom_def using tendsto_at_iff_sequentially[of ?f "$\<delta>" 0 \<real>, THEN iffD1]
    apply simp
    apply (drule_tac x="\<lambda>m. 1 / Suc m" in spec, simp, drule mp)
    subgoal using lim_1_over_n LIMSEQ_Suc by force
    by (simp add: o_def mult.commute)
  thus ?thesis
    by (simp add: LIMSEQ_imp_Suc)
qed

lemma d_nom_0_iff_i_0: "$d^{m} = 0 \<longleftrightarrow> i = 0" if "m \<noteq> 0" for m::nat
proof -
  have "$d^{m} = 0 \<longleftrightarrow> $i^{m} = 0"
    unfolding d_nom_def using v_futr_m_pos by (smt (verit) divide_eq_0_iff of_nat_0)
  thus ?thesis
    using i_nom_0_iff_i_0 that by auto
qed

lemma d_nom_pos_iff_i_pos: "$d^{m} > 0 \<longleftrightarrow> i > 0" if "m \<noteq> 0" for m::nat
proof -
  have "$d^{m} > 0 \<longleftrightarrow> $i^{m} > 0"
    unfolding d_nom_def using zero_less_divide_iff i_nom_pos_iff_i_pos v_futr_m_pos that by smt
  thus ?thesis
    using i_nom_pos_iff_i_pos that by auto
qed

lemma d_nom_i_nom: "1 - $d^{m}/m = 1 / (1 + $i^{m}/m)" if "m \<noteq> 0" for m::nat
proof -
  have "1 - $d^{m}/m = 1 - ($i^{m}/m) / (1 + $i^{m}/m)"
    by (simp add: d_nom_def)
  also have "\<dots> = 1 / (1 + $i^{m}/m)"
    using v_futr_m_pos
    by (smt (verit, ccfv_SIG) add_divide_distrib that div_self)
  finally show ?thesis .
qed

lemma lim_d_nom: "(\<lambda>m. $d^{m}) \<longlonglongrightarrow> $\<delta>"
proof -
  have "(\<lambda>m. $i^{m}/m) \<longlonglongrightarrow> 0"
    using lim_i_nom tendsto_divide_0 tendsto_of_nat by blast
  hence "(\<lambda>m. 1 + $i^{m}/m) \<longlonglongrightarrow> 1"
    by (metis add.right_neutral tendsto_add_const_iff)
  thus ?thesis
    unfolding d_nom_def using lim_i_nom tendsto_divide div_by_1 by fastforce
qed

lemma v_pos: "$v > 0"
  unfolding v_pres_def using v_futr_pos by auto

lemma v_1_iff_i_0: "$v = 1 \<longleftrightarrow> i = 0"
proof
  assume "$v = 1"
  thus "i = 0"
    unfolding v_pres_def by simp
next
  assume "i = 0"
  thus "$v = 1"
    unfolding v_pres_def by simp
qed

lemma v_lt_1_iff_i_pos: "$v < 1 \<longleftrightarrow> i > 0"
proof
  assume "$v < 1"
  thus "i > 0"
    unfolding v_pres_def by (simp add: v_futr_pos)
next
  assume "i > 0"
  thus "$v < 1"
    unfolding v_pres_def by (simp add: v_futr_pos)
qed

lemma v_i_nom: "$v = (1 + $i^{m}/m).^-m" if "m \<noteq> 0" for m::nat
proof -
  have "$v = (1 + i).^-1"
    unfolding v_pres_def using v_futr_pos powr_real_def that by (simp add: powr_neg_one)
  also have "\<dots> = ((1 + $i^{m}/m)^m).^-1"
    using i_nom_eff that by presburger
  also have "\<dots> = (1 + $i^{m}/m).^-m"
    using powr_powr powr_realpow[THEN sym] v_futr_m_pos that by simp
  finally show ?thesis .
qed

lemma i_v: "1 + i = $v.^-1"
  unfolding v_pres_def powr_real_def using v_futr_pos powr_neg_one by simp

lemma i_v_powr: "(1 + i).^a = $v.^-a" for a::real
  by (subst i_v, subst powr_powr, simp)

lemma v_delta: "ln $v = - $\<delta>"
  unfolding i_force_def v_pres_def using v_futr_pos by (simp add: ln_div)

lemma is_derive_vpow: "DERIV (\<lambda>t. $v.^t) t :> - $\<delta> * $v.^t"
  using v_delta has_real_derivative_powr2 v_pos by (metis mult.commute)

lemma d_nom_v: "$d^{m} = m * (1 - $v.^(1/m))" if "m \<noteq> 0" for m::nat
proof -
  have "$d^{m} = m * (1 - 1 / (1 + $i^{m}/m))"
    using d_nom_i_nom[THEN sym] that by force
  also have "\<dots> = m * (1 - 1 / (1 + i).^(1/m))"
    using i_nom_i that powr_minus_divide by simp
  also have "\<dots> = m * (1 - $v.^(1/m))"
    using v_pres_def v_futr_pos powr_divide by simp
  finally show ?thesis .
qed

lemma d_nom_i_nom_v: "$d^{m} = $i^{m} * $v.^(1/m)" if "m \<noteq>0" for m::nat
  unfolding d_nom_def v_pres_def using i_nom_i powr_divide v_futr_pos that by auto

lemma a_calc: "$a^{m}_n = (1 - $v^n) / $i^{m}" if "m \<noteq> 0" "i \<noteq> 0" for n m ::nat
proof -
  have "\<And>l::nat. l/m = (1/m) * l" by simp
  hence \<star>: "\<And>l::nat. $v.^(l/m) = ($v.^(1/m))^l"
    using powr_powr powr_realpow v_pos by (metis powr_gt_zero)
  hence "$a^{m}_n = (\<Sum>k<n*m. ($v.^(1/m))^(k+1::nat) / m)"
    unfolding ann_def by presburger
  also have "\<dots> = $v.^(1/m) * (\<Sum>k<n*m. ($v.^(1/m))^k) / m"
    by (simp, subst sum_divide_distrib[THEN sym], subst sum_distrib_left[THEN sym], simp)
  also have "\<dots> = $v.^(1/m) * ((($v.^(1/m))^(n*m) - 1) / ($v.^(1/m) - 1)) / m"
    apply (subst geometric_sum[of "$v.^(1/m)" "n*m"]; simp?)
    using powr_zero_eq_one[of "$v"] v_pos v_1_iff_i_0 powr_inj that
    by (smt (verit, del_insts) divide_eq_0_iff of_nat_eq_0_iff)
  also have "\<dots> = (($v.^(1/m))^(n*m) - 1) / (m * ($v.^(1/m) - 1) / $v.^(1/m))"
    by (simp add: field_simps)
  also have "\<dots> = ($v^n - 1) / (m * (1 - 1 / $v.^(1/m)))"
    apply (subst \<star>[of "n*m::nat", THEN sym], simp only: of_nat_simps)
    apply (subst nonzero_mult_div_cancel_right[where 'a=real, of m n], simp add: that)
    apply (subst powr_realpow[OF v_pos])
    apply (subst times_divide_eq_right[of _ _ "$v.^(1/m)", THEN sym])
    using v_pos by (subst diff_divide_distrib[of _ _ "$v.^(1/m)"], simp)
  also have "\<dots> = (1 - $v^n) / (m * (1 / $v.^(1/m) - 1))"
    using minus_divide_divide by (smt mult_minus_right)
  also have "\<dots> = (1 - $v^n) / $i^{m}"
    unfolding i_nom_def v_pres_def using v_futr_pos powr_divide by auto
  finally show ?thesis .
qed

lemma a_calc_i_0: "$a^{m}_n = n" if "m \<noteq> 0" "i = 0" for n m :: nat
  unfolding ann_def v_pres_def using that by simp

lemma s_calc_i_0: "$s^{m}_n = n" if "m \<noteq> 0" "i = 0" for n m :: nat
  unfolding acc_def using that by simp

lemma s_a: "$s^{m}_n = (1+i)^n * $a^{m}_n" if "m \<noteq> 0" for n m :: nat
proof -
  have "(1+i)^n * $a^{m}_n = (\<Sum>k<n*m. (1+i)^n * ($v.^((k+1::nat)/m) / m))"
    unfolding ann_def using sum_distrib_left by blast
  also have "\<dots> = (\<Sum>k<n*m. (1+i).^((n*m - Suc k)/m) / m)"
  proof -
    have "\<And>k::nat. k < n*m \<Longrightarrow> (1+i)^n * ($v.^((k+1::nat)/m) / m) = (1+i).^((n*m - Suc k)/m) / m"
      unfolding v_pres_def
      apply (subst powr_realpow[THEN sym], simp add: v_futr_pos)
      apply (subst inverse_powr, simp add: v_futr_pos)
      apply (subst times_divide_eq_right, subst powr_add[THEN sym], simp add: that)
      by (subst of_nat_diff, simp add: Suc_le_eq, simp add: diff_divide_distrib that)
    thus ?thesis by (meson lessThan_iff sum.cong)
  qed
  also have "\<dots> = (\<Sum>k<n*m. (1+i).^(k/m) / m)"
    apply (subst atLeast0LessThan[THEN sym])+
    by (subst sum.atLeastLessThan_rev[THEN sym, of _ "n*m" 0, simplified add_0_right], simp)
  also have "\<dots> = $s^{m}_n"
    unfolding acc_def by simp
  finally show ?thesis ..
qed

lemma s_calc: "$s^{m}_n = ((1+i)^n - 1) / $i^{m}" if "m \<noteq> 0" "i \<noteq> 0" for n m :: nat
  using that v_futr_pos
  apply (subst s_a, simp, subst a_calc; simp?)
  apply (rule disjI2)
  apply (subst right_diff_distrib, simp)
  apply (rule left_right_inverse_power)
  unfolding v_pres_def by auto

lemma a''_a: "$a''^{m}_n = (1+i).^(1/m) * $a^{m}_n" if "m \<noteq> 0" for m::nat
  unfolding ann_def ann_due_def
  apply (subst sum_distrib_left, subst times_divide_eq_right, simp)
  by (subst i_v, subst powr_powr, subst powr_add[THEN sym], simp, subst add_divide_distrib, simp)

lemma a_a'': "$a^{m}_n = $v.^(1/m) * $a''^{m}_n" if "m \<noteq> 0" for m::nat
  unfolding ann_def ann_due_def
  apply (subst sum_distrib_left, subst times_divide_eq_right, simp)
  by (subst powr_add[THEN sym], subst add_divide_distrib, simp)

lemma a''_calc_i_0: "$a''^{m}_n = n" if "m \<noteq> 0" "i = 0" for n m :: nat
  unfolding ann_due_def v_pres_def using that by simp

lemma s''_calc_i_0: "$s''^{m}_n = n" if "m \<noteq> 0" "i = 0" for n m :: nat
  unfolding acc_due_def using that by simp

lemma a''_calc: "$a''^{m}_n = (1 - $v^n) / $d^{m}" if "m \<noteq> 0" "i \<noteq> 0" for n m :: nat
proof -
  have "$a''^{m}_n = (1+i).^(1/m) * ((1 - $v^n) / $i^{m})"
    using a''_a a_calc times_divide_eq_right that by simp
  also have "\<dots> = (1 - $v^n) / ($v.^(1/m) * $i^{m})"
    by (subst i_v, subst powr_powr, simp, subst powr_minus_divide, simp)
  also have "\<dots> = (1 - $v^n) / $d^{m}"
    using d_nom_i_nom_v that by simp
  finally show ?thesis .
qed

lemma s''_s: "$s''^{m}_n = (1+i).^(1/m) * $s^{m}_n" if "m \<noteq> 0" for m::nat
  unfolding acc_def acc_due_def
  by (simp add: sum_distrib_left add_divide_distrib powr_add)

lemma s_s'': "$s^{m}_n = $v.^(1/m) * $s''^{m}_n" if "m \<noteq> 0" for m::nat
  unfolding acc_def acc_due_def v_pres_def using v_futr_pos
  apply (simp add: sum_distrib_left inverse_powr add_divide_distrib)
  by (metis (no_types) add_diff_cancel_left' powr_add uminus_add_conv_diff)

lemma s''_calc: "$s''^{m}_n = ((1+i)^n - 1) / $d^{m}" if "m \<noteq> 0" "i \<noteq> 0" for n m :: nat
proof -
  have "$s''^{m}_n = (1+i).^(1/m) * ((1+i)^n - 1) / $i^{m}"
    using s''_s s_calc times_divide_eq_right that by simp
  also have "\<dots> = ((1+i)^n - 1) / ($v.^(1/m) * $i^{m})"
    by (subst i_v, subst powr_powr, simp, subst powr_minus_divide, simp)
  also have "\<dots> = ((1+i)^n - 1) / $d^{m}"
    using d_nom_i_nom_v that by simp
  finally show ?thesis .
qed

lemma s''_a'': "$s''^{m}_n = (1+i)^n * $a''^{m}_n" if "m \<noteq> 0" for m::nat
  using that s''_s a''_a s_a by simp

lemma a'_calc: "$a'_n = (1 - $v.^n) / $\<delta>" if "i \<noteq> 0" "n \<ge> 0" for n::real
  unfolding ann_cont_def
  apply (rule integral_unique)
  using has_integral_powr2_from_0[OF v_pos _ that(2)] v_delta v_1_iff_i_0 that
  by (smt minus_divide_divide)

lemma a'_calc_i_0: "$a'_n = n" if "i = 0" "n \<ge> 0" for n::real
  unfolding ann_cont_def
  apply (subst iffD2[OF v_1_iff_i_0], simp add: that)
  by (simp add: integral_cong that)

lemma s'_calc: "$s'_n = ((1+i).^n - 1) / $\<delta>" if "i \<noteq> 0" "n \<ge> 0" for n::real
  unfolding acc_cont_def
  apply (rule integral_unique)
  using has_integral_powr2_from_0[OF v_futr_pos _ that(2)] i_force_def that
  by simp

lemma s'_calc_i_0: "$s'_n = n" if "i = 0" "n \<ge> 0" for n::real
  unfolding acc_cont_def
  apply (subst \<open>i = 0\<close>, simp)
  by (simp add: integral_cong that)

lemma s'_a': "$s'_n = (1+i).^n * $a'_n" if "n \<ge> 0" for n::real
proof -
  have "(1+i).^n * $a'_n = integral {0..n} (\<lambda>t. (1+i).^(n-t))"
    unfolding ann_cont_def
    using integrable_on_powr2_from_0_general[of "$v" n] v_pos v_futr_pos that
    apply (subst integral_mult, simp)
    apply (rule integral_cong)
    unfolding v_pres_def using inverse_powr powr_add[THEN sym] by smt
  also have "\<dots> = $s'_n"
    unfolding acc_cont_def using v_futr_pos that
    apply (subst has_integral_interval_reverse[of 0 n, simplified, THEN integral_unique]; simp?)
    by (rule continuous_on_powr; auto)
  finally show ?thesis ..
qed

lemma lim_m_a: "(\<lambda>m. $a^{m}_n) \<longlonglongrightarrow> $a'_n" for n::nat
proof (rule LIMSEQ_imp_Suc)
  show "(\<lambda>m. $a^{Suc m}_n) \<longlonglongrightarrow> $a'_n"
  proof (cases "i = 0")
    case True
    show ?thesis
      using a_calc_i_0 a'_calc_i_0 True by simp
  next
    case False
    show ?thesis
      using False v_pos delta_0_iff_i_0
      apply (subst a_calc; simp?)
      apply (subst a'_calc; simp?)
      apply (subst powr_realpow, simp)
      apply (rule tendsto_divide; simp?)
      by (rule LIMSEQ_Suc[OF lim_i_nom])
  qed
qed

lemma lim_m_a'': "(\<lambda>m. $a''^{m}_n) \<longlonglongrightarrow> $a'_n" for n::nat
proof (rule LIMSEQ_imp_Suc)
  show "(\<lambda>m. $a''^{Suc m}_n) \<longlonglongrightarrow> $a'_n"
  proof (cases "i = 0")
    case True
    show ?thesis
      using a''_calc_i_0 a'_calc_i_0 True by simp
  next
    case False
    show ?thesis
      using False v_pos delta_0_iff_i_0
      apply (subst a''_calc; simp?)
      apply (subst a'_calc; simp?)
      apply (subst powr_realpow, simp)
      apply (rule tendsto_divide; simp?)
      by (rule LIMSEQ_Suc[OF lim_d_nom])
  qed
qed

lemma lim_m_s: "(\<lambda>m. $s^{m}_n) \<longlonglongrightarrow> $s'_n" for n::nat
proof (rule LIMSEQ_imp_Suc)
  show "(\<lambda>m. $s^{Suc m}_n) \<longlonglongrightarrow> $s'_n"
  proof (cases "i = 0")
    case True
    show ?thesis
      using s_calc_i_0 s'_calc_i_0 True by simp
  next
    case False
    show ?thesis
      using False v_futr_pos delta_0_iff_i_0
      apply (subst s_calc; simp?)
      apply (subst s'_calc; simp?)
      apply (subst powr_realpow, simp)
      apply (rule tendsto_divide; simp?)
      by (rule LIMSEQ_Suc[OF lim_i_nom])
  qed
qed

lemma lim_m_s'': "(\<lambda>m. $s''^{m}_n) \<longlonglongrightarrow> $s'_n" for n::nat
proof (rule LIMSEQ_imp_Suc)
  show "(\<lambda>m. $s''^{Suc m}_n) \<longlonglongrightarrow> $s'_n"
  proof (cases "i = 0")
    case True
    show ?thesis
      using s''_calc_i_0 s'_calc_i_0 True by simp
  next
    case False
    show ?thesis
      using False v_futr_pos delta_0_iff_i_0
      apply (subst s''_calc; simp?)
      apply (subst s'_calc; simp?)
      apply (subst powr_realpow, simp)
      apply (rule tendsto_divide; simp?)
      by (rule LIMSEQ_Suc[OF lim_d_nom])
  qed
qed

lemma lim_n_a: "(\<lambda>n. $a^{m}_n) \<longlonglongrightarrow> $a^{m}_\<infinity>" if "m \<noteq> 0" "i > 0" for m::nat
proof -
  have "$i^{m} \<noteq> 0" using i_nom_pos_iff_i_pos that by smt
  moreover have "(\<lambda>n. $v^n) \<longlonglongrightarrow> 0"
    using LIMSEQ_realpow_zero[of "$v"] v_pos v_lt_1_iff_i_pos that by simp
  ultimately show ?thesis
    using that apply (subst a_calc; simp?)
    unfolding perp_def apply (rule tendsto_divide; simp?)
    using tendsto_diff[where a=1 and b=0] by auto
qed

lemma lim_n_a'': "(\<lambda>n. $a''^{m}_n) \<longlonglongrightarrow> $a''^{m}_\<infinity>" if "m \<noteq> 0" "i > 0" for m::nat
proof -
  have "$d^{m} \<noteq> 0" using d_nom_pos_iff_i_pos that by smt
  moreover have "(\<lambda>n. $v^n) \<longlonglongrightarrow> 0"
    using LIMSEQ_realpow_zero[of "$v"] v_pos v_lt_1_iff_i_pos that by simp
  ultimately show ?thesis
    using that apply (subst a''_calc; simp?)
    unfolding perp_due_def apply (rule tendsto_divide; simp?)
    using tendsto_diff[where a=1 and b=0] by auto
qed

lemma Ilsm_Ilam: "$(I^{l}s)^{m}_n = (1+i)^n * $(I^{l}a)^{m}_n"
  if "l \<noteq> 0" "m \<noteq> 0" for l n m :: nat
  unfolding acc_incr_def ann_incr_def v_pres_def using v_futr_pos powr_realpow
  apply (subst inverse_powr, simp)
  apply (subst sum_distrib_left)
  by (subst minus_real_def, subst powr_add, subst times_divide_eq_right, subst mult.assoc, simp)

lemma Iam_calc: "$(Ia)^{m}_n = (\<Sum>j<n. (j+1)/m * (\<Sum>k=j*m..<(j+1)*m. $v.^((k+1)/m)))"
  if "m \<noteq> 0" for n m :: nat
proof -
  let ?I = "{..<n}"
  let ?A = "\<lambda>j. {j*m..<(j+1)*m}"
  let ?g = "\<lambda>k. $v.^((k+1::nat)/m) * \<lceil>(k+1::nat)/m\<rceil> / m"
  have "$(Ia)^{m}_n = (\<Sum>j<n. \<Sum>k=j*m..<(j+1)*m. $v.^((k+1)/m) * \<lceil>(k+1)/m\<rceil> / m)"
    unfolding ann_incr_def using seq_part_multiple that
    apply (simp only: mult_1)
    by (subst sum.UNION_disjoint[of ?I ?A ?g, THEN sym]; simp)
  also have "\<dots> = (\<Sum>j<n. (j+1)/m * (\<Sum>k=j*m..<(j+1)*m. $v.^((k+1)/m)))"
  proof -
    { fix j k
      assume "j*m \<le> k \<and> k < (j+1)*m"
      hence "j*m < k+1 \<and> k+1 \<le> (j+1)*m" by force
      hence "j < (k+1)/m \<and> (k+1)/m \<le> j+1"
        using pos_less_divide_eq pos_divide_le_eq of_nat_less_iff of_nat_le_iff that
        by (smt (verit) of_nat_le_0_iff of_nat_mult)
      hence "\<lceil>(k+1)/m\<rceil> = j+1"
        by (simp add: ceiling_unique) }
    hence "\<And>j k. j*m \<le> k \<and> k < (j+1)*m \<Longrightarrow> \<lceil>(k+1)/m\<rceil> = j+1"
      by (metis (no_types) of_nat_1 of_nat_add)
    with v_pos show ?thesis
      apply (intro sum.cong, simp)
      apply (subst sum_distrib_left, rule sum.cong; simp)
      by (smt (verit, ccfv_SIG) of_int_1 of_int_diff of_int_of_nat_eq)
  qed
  finally show ?thesis .
qed

lemma Ism_calc: "$(Is)^{m}_n = (\<Sum>j<n. (j+1)/m * (\<Sum>k=j*m..<(j+1)*m. (1+i).^(n-(k+1)/m)))"
  if "m \<noteq> 0" for n m :: nat
  using v_pos that
  apply (subst Ilsm_Ilam; simp)
  apply (subst Iam_calc[simplified]; simp?)
  apply ((subst sum_distrib_left, rule sum.cong; simp))+
  unfolding v_pres_def using v_futr_pos
  apply (subst inverse_powr; simp)
  apply (subst powr_realpow[THEN sym], simp)
  by (subst powr_add[THEN sym]; simp)

lemma Imam_calc_aux: "$(I^{m}a)^{m}_n = (\<Sum>k<n*m. $v.^((k+1)/m) * (k+1) / m^2)"
  if "m \<noteq> 0" for m::nat
  unfolding ann_incr_def power_def
  apply (rule sum.cong, simp)
  apply (subst of_nat_mult)
  using v_pos that
  apply (subst nonzero_mult_div_cancel_left, simp)
  by (subst ceiling_of_nat; simp)

lemma Imam_calc:
  "$(I^{m}a)^{m}_n = ($v.^(1/m) * (1 - (n*m+1)*$v^n + n*m*$v.^(n+1/m))) / (m*(1-$v.^(1/m)))^2"
  if "i \<noteq> 0" "m \<noteq> 0" for n m :: nat
proof -
  have \<star>: "$v.^(1/m) > 0" using v_pos by force
  hence "$(I^{m}a)^{m}_n = (\<Sum>k<n*m. (k+1)*($v.^(1/m))^(k+1)) / m^2"
    using that
    apply (subst Imam_calc_aux, simp)
    apply (subst sum_divide_distrib[THEN sym], simp)
    apply (rule sum.cong; simp)
    using powr_realpow[THEN sym] powr_powr by (simp add: add_divide_distrib powr_add)
  also have "\<dots> = $v.^(1/m) * (\<Sum>k<n*m. (k+1)*($v.^(1/m))^k) / m^2"
    by (subst sum_distrib_left, simp add: that, rule sum.cong; simp)
  also have "\<dots> = $v.^(1/m) *
    ((1 - (n*m+1)*($v.^(1/m))^(n*m) + n*m*($v.^(1/m))^(n*m+1)) / (1 - $v.^(1/m))^2) / m^2"
    using v_pos v_1_iff_i_0 that by (subst geometric_increasing_sum; simp?)
  also have "\<dots> = ($v.^(1/m) * (1 - (n*m+1)*$v^n + n*m*$v.^(n+1/m))) / (m*(1-$v.^(1/m)))^2"
    using \<star>
    apply (subst powr_realpow[of "$v.^(1/m)", THEN sym], simp)+
    apply (subst powr_powr)+
    apply (subst times_divide_eq_right[THEN sym], subst divide_divide_eq_left)
    apply (subst power_mult_distrib)
    using powr_eq_one_iff_gen v_pos v_1_iff_i_0 apply (simp add: field_simps)
    by ((subst powr_realpow, simp)+, simp)
  finally show ?thesis .
qed

lemma Imam_calc_i_0: "$(I^{m}a)^{m}_n = (n*m+1)*n / (2*m)" if "i = 0" "m \<noteq> 0" for n m :: nat
proof -
  have "$(I^{m}a)^{m}_n = (\<Sum>k<n*m. $v.^((k+1)/m) * (k+1) / m^2)"
    by (subst Imam_calc_aux, simp_all add: that)
  also have "\<dots> = (\<Sum>k<n*m. k+1) / m^2"
    apply (subst v_1_iff_i_0[THEN iffD2], simp_all add: that)
    by (subst sum_divide_distrib[THEN sym], simp)
  also have "\<dots> = (n*m*(n*m+1) div 2) / m^2"
    apply (subst Suc_eq_plus1[THEN sym], subst sum_bounds_lt_plus1[of id, simplified])
    by (subst Sum_Icc_nat, simp)
  also have "\<dots> = (n*m+1)*n / (2*m)"
    apply (subst real_of_nat_div, simp)
    using that by (subst power2_eq_square, simp add: field_simps)
  finally show ?thesis .
qed

lemma Imsm_calc:
  "$(I^{m}s)^{m}_n = ((1+i).^(n+1/m) - (n*m+1)*(1+i).^(1/m) + n*m) / (m*((1+i).^(1/m)-1))^2"
  if "i \<noteq> 0" "m \<noteq> 0" for n m :: nat
proof -
  have "$(I^{m}a)^{m}_n =
    ($v^n * ((1+i).^(n+1/m) - (n*m+1)*(1+i).^(1/m) + n*m)) / (m*((1+i).^(1/m)-1))^2"
  proof -
    have "$(I^{m}a)^{m}_n =
      ($v.^(1/m) * (1 - (n*m+1)*$v^n + n*m*$v.^(n+1/m))) / (m*(1-$v.^(1/m)))^2"
      using that by (subst Imam_calc; simp)
    also have "\<dots> = (1 - (n*m+1)*$v^n + n*m*$v.^(n+1/m)) / ($v.^(1/m)*(m*($v.^(-1/m)-1))^2)"
      apply (subgoal_tac "$v.^(-1/m) = 1 / $v.^(1/m)", erule ssubst)
       apply ((subst power2_eq_square)+, simp add: field_simps that)
      by (simp add: powr_minus_divide)
    also have "\<dots> =
      ($v.^(n+1/m) * ($v.^(-n-1/m) - (n*m+1)*$v.^(-1/m) + n*m)) / ($v.^(1/m)*(m*($v.^(-1/m)-1))^2)"
      apply (subgoal_tac "$v.^(-n-1/m) = 1 / $v.^(n+1/m)" "$v.^(-1/m) = $v^n / $v.^(n+1/m)")
        apply ((erule ssubst)+, simp_all add: field_simps)
      using v_pos
       apply (simp add: powr_diff[THEN sym] powr_realpow[THEN sym])
      by (smt powr_minus_divide)
    also have "\<dots> =
      ($v^n * ($v.^(-n-1/m) - (n*m+1)*$v.^(-1/m) + n*m)) / ((m*($v.^(-1/m)-1))^2)"
      apply (subst powr_add[of _ n "1/m"])
      using v_pos powr_realpow by simp
    also have "\<dots> =
      ($v^n * ((1+i).^(n+1/m) - (n*m+1)*(1+i).^(1/m) + n*m)) / ((m*((1+i).^(1/m)-1))^2)"
      apply (subgoal_tac "-n-1/m = -(n+1/m)" "-1/m = -(1/m)", (erule ssubst)+)
        apply (subst i_v_powr[THEN sym])+
      by simp_all
    finally show ?thesis .
  qed
  thus ?thesis
    apply -
    using that v_futr_pos
    apply (subst Ilsm_Ilam, simp)
    apply (erule ssubst, simp)
    apply (rule disjI2)
    by (subst power_mult_distrib[THEN sym], simp add: v_pres_def)
qed

lemma Imsm_calc_i_0: "$(I^{m}s)^{m}_n = (n*m+1)*n / (2*m)" if "i = 0" "m \<noteq> 0" for n m :: nat
  using that
  apply (subst Ilsm_Ilam, simp)
  by (subst Imam_calc_i_0; simp)

lemma Ila''m_Ilam: "$(I^{l}a'')^{m}_n = (1+i).^(1/m) * $(I^{l}a)^{m}_n"
  if "l \<noteq> 0" "m \<noteq> 0" for l m n :: nat
  unfolding ann_incr_def ann_due_incr_def using that
  apply (subst i_v, subst powr_powr, simp)
  apply (subst sum_distrib_left)
  apply (rule sum.cong; simp)
  apply (rule disjI2)
  by (smt (verit) add_divide_distrib powr_add)

lemma Ia''m_calc: "$(Ia'')^{m}_n = (\<Sum>j<n. (j+1)/m * (\<Sum>k=j*m..<(j+1)*m. $v.^(k/m)))"
  if "m \<noteq> 0" for n m :: nat
  using that
  apply (subst Ila''m_Ilam; simp del: One_nat_def)
  apply (subst Iam_calc; simp)
  apply (subst sum_distrib_left)
  apply (rule sum.cong; simp)
  apply (subst sum_distrib_left)+
  apply (rule sum.cong; simp)
  apply (subst i_v_powr)
  using powr_add[of "$v", THEN sym] by (simp add: field_simps)

lemma Ima''m_calc_aux: "$(I^{m}a'')^{m}_n = (\<Sum>k<n*m. $v.^(k/m) * (k+1) / m^2)"
  if "m \<noteq> 0" for m::nat
  using that
  apply (subst Ila''m_Ilam, simp)
  apply (subst Imam_calc_aux, simp)
  apply (subst sum_distrib_left)
  apply (rule sum.cong; simp)
  using powr_add[of "$v", THEN sym] i_v_powr by (simp add: field_simps)

lemma Ima''m_calc: "$(I^{m}a'')^{m}_n = (1 - (n*m+1)*$v^n + n*m*$v.^(n+1/m)) / (m*(1-$v.^(1/m)))^2"
  if "i \<noteq> 0" "m \<noteq> 0" for n m :: nat
  using that v_pos
  apply (subst Ila''m_Ilam, simp)
  apply (subst Imam_calc; simp)
  by (smt (verit, del_insts) i_v_powr powr_add powr_zero_eq_one)

lemma Ils''m_Ilsm: "$(I^{l}s'')^{m}_n = (1+i).^(1/m) * $(I^{l}s)^{m}_n"
  if "l \<noteq> 0" "m \<noteq> 0" for l m n :: nat
  unfolding acc_incr_def acc_due_incr_def sum_distrib_left using that
  apply (intro sum.cong; simp)
  by (smt (verit, ccfv_SIG) add_divide_distrib powr_add)

lemma Ims''m_calc:
  "$(I^{m}s'')^{m}_n =
    (1+i).^(1/m) * ((1+i).^(n+1/m) - (n*m+1)*(1+i).^(1/m) + n*m) / (m*((1+i).^(1/m)-1))^2"
  if "i \<noteq> 0" "m \<noteq> 0" for n m :: nat
  using that by (simp add: Ils''m_Ilsm Imsm_calc)

lemma lim_Imam: "(\<lambda>n. $(I^{m}a)^{m}_n) \<longlonglongrightarrow> 1 / ($i^{m}*$d^{m})" if "m \<noteq> 0" "i > 0" for m::nat
proof -
  have "(\<lambda>n. $(I^{m}a)^{m}_n) =
    (\<lambda>n. $v.^(1/m) * (1 - (n*m+1)*$v^n + n*m*$v.^(n+1/m)) / (m*(1-$v.^(1/m)))^2)"
    using that by (subst Imam_calc; simp)
  moreover have "(\<lambda>n. $v.^(1/m) * (1 - (n*m+1)*$v^n + n*m*$v.^(n+1/m)) / (m*(1-$v.^(1/m)))^2)
    \<longlonglongrightarrow> 1 / ($i^{m}*$d^{m})"
  proof -
    have \<star>: "\<bar>$v\<bar> < 1"
      using v_lt_1_iff_i_pos v_pos that by force
    hence "(\<lambda>n. (n*m+1)*$v^n) \<longlonglongrightarrow> 0"
      apply (subst tendsto_cong[of _ "(\<lambda>n. n*m*$v^n + $v^n)"])
       apply (rule always_eventually, rule allI)
       apply (simp add: distrib_right)
      apply (subgoal_tac "0 = 0 + 0", erule ssubst, intro tendsto_intros; simp)
      apply (subst mult.commute, subst mult.assoc)
      apply (subgoal_tac "0 = real m * 0", erule ssubst, intro tendsto_intros; simp?)
      by (rule powser_times_n_limit_0; simp)
    moreover have "(\<lambda>n. n*m*$v.^(n+1/m)) \<longlonglongrightarrow> 0"
      apply (subst tendsto_cong[of _ "(\<lambda>n. (m*$v.^(1/m))*(n*$v^n))"])
       apply (rule always_eventually, rule allI)
      apply (simp add: powr_add powr_realpow v_pos)
      apply (subgoal_tac "0 = m*$v.^(1/m) * 0", erule ssubst, intro tendsto_intros; simp?)
      by (rule powser_times_n_limit_0, simp add: \<star>)
    ultimately have "(\<lambda>n. $v.^(1/m) * (1 - (n*m+1)*$v^n + n*m*$v.^(n+1/m)) / (m*(1-$v.^(1/m)))^2)
      \<longlonglongrightarrow> $v.^(1/m) * (1 - 0 + 0)/ (m*(1-$v.^(1/m)))^2"
      using v_lt_1_iff_i_pos v_pos that by (intro tendsto_intros; simp)
    thus ?thesis
      unfolding i_nom_def using v_pos that
      apply (subst i_v_powr, subst powr_minus_divide, subst d_nom_v; simp)
      by (subst(asm)(2) power2_eq_square, simp add: field_simps)
  qed
  ultimately show ?thesis by simp
qed

lemma perp_incr_calc: "$(I^{m}a)^{m}_\<infinity> = 1 / ($i^{m}*$d^{m})" if "m \<noteq> 0" "i > 0" for m::nat
  unfolding perp_incr_def by (rule limI, rule lim_Imam; simp add: that)

lemma lim_Ima''m: "(\<lambda>n. $(I^{m}a'')^{m}_n) \<longlonglongrightarrow> 1 / ($d^{m})^2" if "m \<noteq> 0" "i > 0" for m::nat
  unfolding perp_due_incr_def using that
  apply (subst Ila''m_Ilam, simp, subst mult.commute, subst i_v_powr, subst powr_minus_divide)
  apply (subgoal_tac "1/($d^{m})^2 = (1/($i^{m}*$d^{m}))*(1/$v.^(1/m))", erule ssubst)
   apply (intro tendsto_intros, simp add: lim_Imam)
  by (simp add: d_nom_i_nom_v power2_eq_square)

lemma perp_due_incr_calc: "$(I^{m}a'')^{m}_\<infinity> = 1 / ($d^{m})^2" if "m \<noteq> 0" "i > 0" for m::nat
  unfolding perp_due_incr_def by (rule limI, rule lim_Ima''m; simp add: that)

end

end