[STATEMENT]
theorem fw_word': "\<not> p dvd q \<Longrightarrow> \<not> q dvd p \<Longrightarrow>
 \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<lbrakk>\<not> p dvd q; \<not> q dvd p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
proof (induction "p + p + q" arbitrary: p q rule: less_induct)
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. \<And>p q. \<lbrakk>\<And>pa qa. \<lbrakk>pa + pa + qa < p + p + q; \<not> pa dvd qa; \<not> qa dvd pa\<rbrakk> \<Longrightarrow> \<^bold>|FW_word pa qa\<^bold>| = pa + qa - gcd pa qa - 1 \<and> period (FW_word pa qa) pa \<and> period (FW_word pa qa) qa \<and> \<not> period (FW_word pa qa) (gcd pa qa); \<not> p dvd q; \<not> q dvd p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
case less
[PROOF STATE]
proof (state)
this:
\<lbrakk>?p + ?p + ?q < p + p + q; \<not> ?p dvd ?q; \<not> ?q dvd ?p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word ?p ?q\<^bold>| = ?p + ?q - gcd ?p ?q - 1 \<and> period (FW_word ?p ?q) ?p \<and> period (FW_word ?p ?q) ?q \<and> \<not> period (FW_word ?p ?q) (gcd ?p ?q)
\<not> p dvd q
\<not> q dvd p

goal (1 subgoal):
 1. \<And>p q. \<lbrakk>\<And>pa qa. \<lbrakk>pa + pa + qa < p + p + q; \<not> pa dvd qa; \<not> qa dvd pa\<rbrakk> \<Longrightarrow> \<^bold>|FW_word pa qa\<^bold>| = pa + qa - gcd pa qa - 1 \<and> period (FW_word pa qa) pa \<and> period (FW_word pa qa) qa \<and> \<not> period (FW_word pa qa) (gcd pa qa); \<not> p dvd q; \<not> q dvd p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "p \<noteq> 0"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p \<noteq> 0
[PROOF STEP]
using  \<open>\<not> q dvd p\<close> dvd_0_right[of q]
[PROOF STATE]
proof (prove)
using this:
\<not> q dvd p
q dvd 0

goal (1 subgoal):
 1. p \<noteq> 0
[PROOF STEP]
by meson
[PROOF STATE]
proof (state)
this:
p \<noteq> 0

goal (1 subgoal):
 1. \<And>p q. \<lbrakk>\<And>pa qa. \<lbrakk>pa + pa + qa < p + p + q; \<not> pa dvd qa; \<not> qa dvd pa\<rbrakk> \<Longrightarrow> \<^bold>|FW_word pa qa\<^bold>| = pa + qa - gcd pa qa - 1 \<and> period (FW_word pa qa) pa \<and> period (FW_word pa qa) qa \<and> \<not> period (FW_word pa qa) (gcd pa qa); \<not> p dvd q; \<not> q dvd p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "p \<noteq> q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p \<noteq> q
[PROOF STEP]
using \<open>\<not> p dvd q\<close>
[PROOF STATE]
proof (prove)
using this:
\<not> p dvd q

goal (1 subgoal):
 1. p \<noteq> q
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
p \<noteq> q

goal (1 subgoal):
 1. \<And>p q. \<lbrakk>\<And>pa qa. \<lbrakk>pa + pa + qa < p + p + q; \<not> pa dvd qa; \<not> qa dvd pa\<rbrakk> \<Longrightarrow> \<^bold>|FW_word pa qa\<^bold>| = pa + qa - gcd pa qa - 1 \<and> period (FW_word pa qa) pa \<and> period (FW_word pa qa) qa \<and> \<not> period (FW_word pa qa) (gcd pa qa); \<not> p dvd q; \<not> q dvd p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
then
[PROOF STATE]
proof (chain)
picking this:
p \<noteq> q
[PROOF STEP]
consider "q < p" | "p < q"
[PROOF STATE]
proof (prove)
using this:
p \<noteq> q

goal (1 subgoal):
 1. \<lbrakk>q < p \<Longrightarrow> thesis; p < q \<Longrightarrow> thesis\<rbrakk> \<Longrightarrow> thesis
[PROOF STEP]
by linarith
[PROOF STATE]
proof (state)
this:
\<lbrakk>q < p \<Longrightarrow> ?thesis; p < q \<Longrightarrow> ?thesis\<rbrakk> \<Longrightarrow> ?thesis

goal (1 subgoal):
 1. \<And>p q. \<lbrakk>\<And>pa qa. \<lbrakk>pa + pa + qa < p + p + q; \<not> pa dvd qa; \<not> qa dvd pa\<rbrakk> \<Longrightarrow> \<^bold>|FW_word pa qa\<^bold>| = pa + qa - gcd pa qa - 1 \<and> period (FW_word pa qa) pa \<and> period (FW_word pa qa) qa \<and> \<not> period (FW_word pa qa) (gcd pa qa); \<not> p dvd q; \<not> q dvd p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
then
[PROOF STATE]
proof (chain)
picking this:
\<lbrakk>q < p \<Longrightarrow> ?thesis; p < q \<Longrightarrow> ?thesis\<rbrakk> \<Longrightarrow> ?thesis
[PROOF STEP]
show ?case
[PROOF STATE]
proof (prove)
using this:
\<lbrakk>q < p \<Longrightarrow> ?thesis; p < q \<Longrightarrow> ?thesis\<rbrakk> \<Longrightarrow> ?thesis

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
proof (cases)
[PROOF STATE]
proof (state)
goal (2 subgoals):
 1. q < p \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. p < q \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
assume "q < p"
[PROOF STATE]
proof (state)
this:
q < p

goal (2 subgoals):
 1. q < p \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. p < q \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have less: "q + q + p < p + p + q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. q + q + p < p + p + q
[PROOF STEP]
by (simp add: \<open>q < p\<close>)
[PROOF STATE]
proof (state)
this:
q + q + p < p + p + q

goal (2 subgoals):
 1. q < p \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. p < q \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
thus ?case
[PROOF STATE]
proof (prove)
using this:
q + q + p < p + p + q

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
using "less.hyps"[OF _ \<open>\<not> q dvd p\<close> \<open>\<not> p dvd q\<close>]
[PROOF STATE]
proof (prove)
using this:
q + q + p < p + p + q
q + q + p < p + p + q \<Longrightarrow> \<^bold>|FW_word q p\<^bold>| = q + p - gcd q p - 1 \<and> period (FW_word q p) q \<and> period (FW_word q p) p \<and> \<not> period (FW_word q p) (gcd q p)

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
unfolding FW_sym[of p q] gcd.commute[of p q] add.commute[of p q]
[PROOF STATE]
proof (prove)
using this:
q + q + p < p + p + q
q + q + p < p + p + q \<Longrightarrow> \<^bold>|FW_word q p\<^bold>| = q + p - gcd q p - 1 \<and> period (FW_word q p) q \<and> period (FW_word q p) p \<and> \<not> period (FW_word q p) (gcd q p)

goal (1 subgoal):
 1. \<^bold>|FW_word q p\<^bold>| = q + p - gcd q p - 1 \<and> period (FW_word q p) p \<and> period (FW_word q p) q \<and> \<not> period (FW_word q p) (gcd q p)
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)

goal (1 subgoal):
 1. p < q \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
next
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. p < q \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
let ?d = "gcd p q"
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. p < q \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
let ?dw = "[0..<(gcd p q)]"
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. p < q \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
let ?pd = "p div (gcd p q)"
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. p < q \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
assume "p < q"
[PROOF STATE]
proof (state)
this:
p < q

goal (1 subgoal):
 1. p < q \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
thus ?thesis
[PROOF STATE]
proof (prove)
using this:
p < q

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
proof (cases "?d = q - p")
[PROOF STATE]
proof (state)
goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
assume "?d = q - p"
[PROOF STATE]
proof (state)
this:
gcd p q = q - p

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
hence "p + ?d = q"
[PROOF STATE]
proof (prove)
using this:
gcd p q = q - p

goal (1 subgoal):
 1. p + gcd p q = q
[PROOF STEP]
using \<open>p < q\<close>
[PROOF STATE]
proof (prove)
using this:
gcd p q = q - p
p < q

goal (1 subgoal):
 1. p + gcd p q = q
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
p + gcd p q = q

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
hence "p \<noteq> q" and "\<not> q < p"
[PROOF STATE]
proof (prove)
using this:
p + gcd p q = q

goal (1 subgoal):
 1. p \<noteq> q &&& \<not> q < p
[PROOF STEP]
using \<open>p \<noteq> 0\<close> \<open>p < q\<close>
[PROOF STATE]
proof (prove)
using this:
p + gcd p q = q
p \<noteq> 0
p < q

goal (1 subgoal):
 1. p \<noteq> q &&& \<not> q < p
[PROOF STEP]
by fastforce+
[PROOF STATE]
proof (state)
this:
p \<noteq> q
\<not> q < p

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
hence fw: "FW_word p q = fw_base p q"
[PROOF STATE]
proof (prove)
using this:
p \<noteq> q
\<not> q < p

goal (1 subgoal):
 1. FW_word p q = fw_base p q
[PROOF STEP]
unfolding FW_word_def[of p q]
[PROOF STATE]
proof (prove)
using this:
p \<noteq> q
\<not> q < p

goal (1 subgoal):
 1. (if q < p then FW_word q p else if p = 0 then \<epsilon> else if p = q then \<epsilon> else if gcd p q = q - p then fw_base p q else take p (FW_word p (q - p)) \<cdot> FW_word p (q - p)) = fw_base p q
[PROOF STEP]
using \<open>p \<noteq> 0\<close>  \<open>gcd p q = q - p\<close>
[PROOF STATE]
proof (prove)
using this:
p \<noteq> q
\<not> q < p
p \<noteq> 0
gcd p q = q - p

goal (1 subgoal):
 1. (if q < p then FW_word q p else if p = 0 then \<epsilon> else if p = q then \<epsilon> else if gcd p q = q - p then fw_base p q else take p (FW_word p (q - p)) \<cdot> FW_word p (q - p)) = fw_base p q
[PROOF STEP]
by presburger
[PROOF STATE]
proof (state)
this:
FW_word p q = fw_base p q

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have ppref: "\<^bold>|butlast (?dw\<^sup>@?pd)\<cdot>[?d]\<^bold>| = p"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q]\<^bold>| = p
[PROOF STEP]
using  \<open>p \<noteq> 0\<close> pow_len[of "?dw" "?pd"]
[PROOF STATE]
proof (prove)
using this:
p \<noteq> 0
\<^bold>|[0..<gcd p q] \<^sup>@ (p div gcd p q)\<^bold>| = p div gcd p q * \<^bold>|[0..<gcd p q]\<^bold>|

goal (1 subgoal):
 1. \<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q]\<^bold>| = p
[PROOF STEP]
unfolding lenmorph sing_len
[PROOF STATE]
proof (prove)
using this:
p \<noteq> 0
\<^bold>|[0..<gcd p q] \<^sup>@ (p div gcd p q)\<^bold>| = p div gcd p q * \<^bold>|[0..<gcd p q]\<^bold>|

goal (1 subgoal):
 1. \<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))\<^bold>| + 1 = p
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
\<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q]\<^bold>| = p

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
note ppref' = this[unfolded lenmorph]
[PROOF STATE]
proof (state)
this:
\<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))\<^bold>| + \<^bold>|[gcd p q]\<^bold>| = p

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have qpref: "\<^bold>|butlast (?dw\<^sup>@?pd)\<cdot>[?d]\<cdot>?dw\<^bold>| = q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> [0..<gcd p q]\<^bold>| = q
[PROOF STEP]
unfolding lassoc lenmorph ppref'
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p + \<^bold>|[0..<gcd p q]\<^bold>| = q
[PROOF STEP]
using  \<open>p + gcd p q = q\<close>
[PROOF STATE]
proof (prove)
using this:
p + gcd p q = q

goal (1 subgoal):
 1. p + \<^bold>|[0..<gcd p q]\<^bold>| = q
[PROOF STEP]
by simp
[PROOF STATE]
proof (state)
this:
\<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> [0..<gcd p q]\<^bold>| = q

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "butlast (?dw\<^sup>@?pd)\<cdot>[?d] \<le>p FW_word p q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<le>p FW_word p q
[PROOF STEP]
unfolding fw fw_base_def fw_p_per_def
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<le>p butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))
[PROOF STEP]
by force
[PROOF STATE]
proof (state)
this:
butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<le>p FW_word p q

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
from pref_take[OF this]
[PROOF STATE]
proof (chain)
picking this:
take \<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q]\<^bold>| (FW_word p q) = butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q]
[PROOF STEP]
have takep: "take p (FW_word p q) = butlast (?dw\<^sup>@?pd)\<cdot>[?d]"
[PROOF STATE]
proof (prove)
using this:
take \<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q]\<^bold>| (FW_word p q) = butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q]

goal (1 subgoal):
 1. take p (FW_word p q) = butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q]
[PROOF STEP]
unfolding ppref
[PROOF STATE]
proof (prove)
using this:
take p (FW_word p q) = butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q]

goal (1 subgoal):
 1. take p (FW_word p q) = butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q]
[PROOF STEP]
.
[PROOF STATE]
proof (state)
this:
take p (FW_word p q) = butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q]

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "?dw \<noteq> \<epsilon>" and "\<^bold>|?dw\<^bold>| = ?d"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. [0..<gcd p q] \<noteq> \<epsilon> &&& \<^bold>|[0..<gcd p q]\<^bold>| = gcd p q
[PROOF STEP]
using \<open>p \<noteq> 0\<close>
[PROOF STATE]
proof (prove)
using this:
p \<noteq> 0

goal (1 subgoal):
 1. [0..<gcd p q] \<noteq> \<epsilon> &&& \<^bold>|[0..<gcd p q]\<^bold>| = gcd p q
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
[0..<gcd p q] \<noteq> \<epsilon>
\<^bold>|[0..<gcd p q]\<^bold>| = gcd p q

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "?pd \<noteq> 0"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p div gcd p q \<noteq> 0
[PROOF STEP]
by (simp add: \<open>p \<noteq> 0\<close> dvd_div_eq_0_iff)
[PROOF STATE]
proof (state)
this:
p div gcd p q \<noteq> 0

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
from not0_implies_Suc[OF this]
[PROOF STATE]
proof (chain)
picking this:
\<exists>m. p div gcd p q = Suc m
[PROOF STEP]
obtain e where "?pd = Suc e"
[PROOF STATE]
proof (prove)
using this:
\<exists>m. p div gcd p q = Suc m

goal (1 subgoal):
 1. (\<And>e. p div gcd p q = Suc e \<Longrightarrow> thesis) \<Longrightarrow> thesis
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
p div gcd p q = Suc e

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "gcd p q \<noteq> p"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. gcd p q \<noteq> p
[PROOF STEP]
using \<open>\<not> p dvd q\<close> gcd_dvd2[of p q]
[PROOF STATE]
proof (prove)
using this:
\<not> p dvd q
gcd p q dvd q

goal (1 subgoal):
 1. gcd p q \<noteq> p
[PROOF STEP]
by force
[PROOF STATE]
proof (state)
this:
gcd p q \<noteq> p

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
hence "Suc e \<noteq> 1"
[PROOF STATE]
proof (prove)
using this:
gcd p q \<noteq> p

goal (1 subgoal):
 1. Suc e \<noteq> 1
[PROOF STEP]
using dvd_mult_div_cancel[OF gcd_dvd1[of p q], unfolded \<open>?pd = Suc e\<close>]
[PROOF STATE]
proof (prove)
using this:
gcd p q \<noteq> p
gcd p q * Suc e = p

goal (1 subgoal):
 1. Suc e \<noteq> 1
[PROOF STEP]
by force
[PROOF STATE]
proof (state)
this:
Suc e \<noteq> 1

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
hence "e \<noteq> 0"
[PROOF STATE]
proof (prove)
using this:
Suc e \<noteq> 1

goal (1 subgoal):
 1. e \<noteq> 0
[PROOF STEP]
by simp
[PROOF STATE]
proof (state)
this:
e \<noteq> 0

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "[0..<gcd p q] \<^sup>@ e \<noteq> \<epsilon>"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. [0..<gcd p q] \<^sup>@ e \<noteq> \<epsilon>
[PROOF STEP]
using \<open>[0..<gcd p q] \<noteq> \<epsilon>\<close> \<open>e \<noteq> 0\<close> nonzero_pow_emp
[PROOF STATE]
proof (prove)
using this:
[0..<gcd p q] \<noteq> \<epsilon>
e \<noteq> 0
?m \<noteq> 0 \<Longrightarrow> (?u \<^sup>@ ?m = \<epsilon>) = (?u = \<epsilon>)

goal (1 subgoal):
 1. [0..<gcd p q] \<^sup>@ e \<noteq> \<epsilon>
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
[0..<gcd p q] \<^sup>@ e \<noteq> \<epsilon>

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
hence but_dec: "butlast (?dw\<^sup>@?pd) = ?dw \<cdot> butlast (?dw\<^sup>@e)"
[PROOF STATE]
proof (prove)
using this:
[0..<gcd p q] \<^sup>@ e \<noteq> \<epsilon>

goal (1 subgoal):
 1. butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) = [0..<gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ e)
[PROOF STEP]
unfolding \<open>?pd = Suc e\<close> pow_Suc butlast_append  if_not_P[OF \<open>[0..<gcd p q] \<^sup>@ e \<noteq> \<epsilon>\<close>]
[PROOF STATE]
proof (prove)
using this:
[0..<gcd p q] \<^sup>@ e \<noteq> \<epsilon>

goal (1 subgoal):
 1. [0..<gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ e) = [0..<gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ e)
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) = [0..<gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ e)

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have but_dec_b: "butlast (?dw\<^sup>@?pd) = ?dw\<^sup>@e \<cdot> butlast ?dw"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) = [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q]
[PROOF STEP]
unfolding \<open>?pd = Suc e\<close> pow_Suc2 butlast_append if_not_P[OF \<open>?dw \<noteq> \<epsilon>\<close>]
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q] = [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q]
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) = [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q]

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "butlast (?dw\<^sup>@?pd)\<cdot>[?d]\<cdot>?dw \<le>p FW_word p q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> [0..<gcd p q] \<le>p FW_word p q
[PROOF STEP]
unfolding fw but_dec lassoc fw_base_def fw_p_per_def
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. (([0..<gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ e)) \<cdot> [gcd p q]) \<cdot> [0..<gcd p q] \<le>p ((([0..<gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ e)) \<cdot> [gcd p q]) \<cdot> [0..<gcd p q]) \<cdot> butlast ([0..<gcd p q] \<^sup>@ e)
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> [0..<gcd p q] \<le>p FW_word p q

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
note takeq = pref_take[OF this, unfolded qpref]
[PROOF STATE]
proof (state)
this:
take q (FW_word p q) = butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> [0..<gcd p q]

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
proof-
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
have "p + q - (q - p) = p + p"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p + q - (q - p) = p + p
[PROOF STEP]
using \<open>p + gcd p q = q\<close>
[PROOF STATE]
proof (prove)
using this:
p + gcd p q = q

goal (1 subgoal):
 1. p + q - (q - p) = p + p
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
p + q - (q - p) = p + p

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
have "\<^bold>|?dw\<^bold>| = ?d"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<^bold>|[0..<gcd p q]\<^bold>| = gcd p q
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
\<^bold>|[0..<gcd p q]\<^bold>| = gcd p q

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
have "gcd p q dvd p"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. gcd p q dvd p
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
gcd p q dvd p

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
hence "\<^bold>|?dw\<^sup>@?pd\<^bold>| = p"
[PROOF STATE]
proof (prove)
using this:
gcd p q dvd p

goal (1 subgoal):
 1. \<^bold>|[0..<gcd p q] \<^sup>@ (p div gcd p q)\<^bold>| = p
[PROOF STEP]
using pow_len[of "?dw" "?pd"]
[PROOF STATE]
proof (prove)
using this:
gcd p q dvd p
\<^bold>|[0..<gcd p q] \<^sup>@ (p div gcd p q)\<^bold>| = p div gcd p q * \<^bold>|[0..<gcd p q]\<^bold>|

goal (1 subgoal):
 1. \<^bold>|[0..<gcd p q] \<^sup>@ (p div gcd p q)\<^bold>| = p
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
\<^bold>|[0..<gcd p q] \<^sup>@ (p div gcd p q)\<^bold>| = p

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
hence "\<^bold>|butlast (?dw\<^sup>@?pd)\<^bold>| = p - 1"
[PROOF STATE]
proof (prove)
using this:
\<^bold>|[0..<gcd p q] \<^sup>@ (p div gcd p q)\<^bold>| = p

goal (1 subgoal):
 1. \<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))\<^bold>| = p - 1
[PROOF STEP]
by simp
[PROOF STATE]
proof (state)
this:
\<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))\<^bold>| = p - 1

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
hence "\<^bold>|FW_word p q\<^bold>| = (p - 1) + 1 +  (p - 1)"
[PROOF STATE]
proof (prove)
using this:
\<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))\<^bold>| = p - 1

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p - 1 + 1 + (p - 1)
[PROOF STEP]
unfolding fw lenmorph sing_len fw_base_def fw_p_per_def
[PROOF STATE]
proof (prove)
using this:
\<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))\<^bold>| = p - 1

goal (1 subgoal):
 1. \<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))\<^bold>| + (1 + \<^bold>|butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))\<^bold>|) = p - 1 + 1 + (p - 1)
[PROOF STEP]
by presburger
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p q\<^bold>| = p - 1 + 1 + (p - 1)

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
thus "\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1"
[PROOF STATE]
proof (prove)
using this:
\<^bold>|FW_word p q\<^bold>| = p - 1 + 1 + (p - 1)

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
unfolding \<open>gcd p q = q - p\<close> \<open>p + q - (q - p) = p + p\<close>
[PROOF STATE]
proof (prove)
using this:
\<^bold>|FW_word p q\<^bold>| = p - 1 + 1 + (p - 1)

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + p - 1
[PROOF STEP]
using \<open>p \<noteq> 0\<close>
[PROOF STATE]
proof (prove)
using this:
\<^bold>|FW_word p q\<^bold>| = p - 1 + 1 + (p - 1)
p \<noteq> 0

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + p - 1
[PROOF STEP]
by fastforce
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1

goal:
No subgoals!
[PROOF STEP]
qed
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "period (FW_word p q) p"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. period (FW_word p q) p
[PROOF STEP]
proof-
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. period (FW_word p q) p
[PROOF STEP]
have "take p (FW_word p q) \<noteq> \<epsilon>"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. take p (FW_word p q) \<noteq> \<epsilon>
[PROOF STEP]
using \<open>p \<noteq> 0\<close>
[PROOF STATE]
proof (prove)
using this:
p \<noteq> 0

goal (1 subgoal):
 1. take p (FW_word p q) \<noteq> \<epsilon>
[PROOF STEP]
unfolding length_0_conv[symmetric] ppref[folded takep]
[PROOF STATE]
proof (prove)
using this:
p \<noteq> 0

goal (1 subgoal):
 1. p \<noteq> 0
[PROOF STEP]
.
[PROOF STATE]
proof (state)
this:
take p (FW_word p q) \<noteq> \<epsilon>

goal (1 subgoal):
 1. period (FW_word p q) p
[PROOF STEP]
moreover
[PROOF STATE]
proof (state)
this:
take p (FW_word p q) \<noteq> \<epsilon>

goal (1 subgoal):
 1. period (FW_word p q) p
[PROOF STEP]
have "fw_base p q \<le>p fw_p_per p q \<cdot> [gcd p q] \<cdot> fw_base p q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. fw_base p q \<le>p fw_p_per p q \<cdot> [gcd p q] \<cdot> fw_base p q
[PROOF STEP]
unfolding rassoc pref_cancel_conv fw_base_def fw_p_per_def
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<le>p butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))
[PROOF STEP]
by simp
[PROOF STATE]
proof (state)
this:
fw_base p q \<le>p fw_p_per p q \<cdot> [gcd p q] \<cdot> fw_base p q

goal (1 subgoal):
 1. period (FW_word p q) p
[PROOF STEP]
ultimately
[PROOF STATE]
proof (chain)
picking this:
take p (FW_word p q) \<noteq> \<epsilon>
fw_base p q \<le>p fw_p_per p q \<cdot> [gcd p q] \<cdot> fw_base p q
[PROOF STEP]
show "period (FW_word p q) p"
[PROOF STATE]
proof (prove)
using this:
take p (FW_word p q) \<noteq> \<epsilon>
fw_base p q \<le>p fw_p_per p q \<cdot> [gcd p q] \<cdot> fw_base p q

goal (1 subgoal):
 1. period (FW_word p q) p
[PROOF STEP]
unfolding period_def period_root_def takep
[PROOF STATE]
proof (prove)
using this:
butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<noteq> \<epsilon>
fw_base p q \<le>p fw_p_per p q \<cdot> [gcd p q] \<cdot> fw_base p q

goal (1 subgoal):
 1. FW_word p q \<le>p (butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q]) \<cdot> FW_word p q \<and> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<noteq> \<epsilon>
[PROOF STEP]
unfolding fw rassoc fw_base_def fw_p_per_def
[PROOF STATE]
proof (prove)
using this:
butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<noteq> \<epsilon>
butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<le>p butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))

goal (1 subgoal):
 1. butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<le>p butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<and> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<noteq> \<epsilon>
[PROOF STEP]
by fast
[PROOF STATE]
proof (state)
this:
period (FW_word p q) p

goal:
No subgoals!
[PROOF STEP]
qed
[PROOF STATE]
proof (state)
this:
period (FW_word p q) p

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "period (FW_word p q) q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. period (FW_word p q) q
[PROOF STEP]
unfolding period_def period_root_def
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. FW_word p q \<le>p take q (FW_word p q) \<cdot> FW_word p q \<and> take q (FW_word p q) \<noteq> \<epsilon>
[PROOF STEP]
proof
[PROOF STATE]
proof (state)
goal (2 subgoals):
 1. FW_word p q \<le>p take q (FW_word p q) \<cdot> FW_word p q
 2. take q (FW_word p q) \<noteq> \<epsilon>
[PROOF STEP]
show "take q (FW_word p q) \<noteq> \<epsilon>"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. take q (FW_word p q) \<noteq> \<epsilon>
[PROOF STEP]
unfolding length_0_conv[symmetric] qpref[folded takeq]
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. q \<noteq> 0
[PROOF STEP]
using \<open>p \<noteq> 0\<close> \<open>p < q\<close>
[PROOF STATE]
proof (prove)
using this:
p \<noteq> 0
p < q

goal (1 subgoal):
 1. q \<noteq> 0
[PROOF STEP]
by linarith
[PROOF STATE]
proof (state)
this:
take q (FW_word p q) \<noteq> \<epsilon>

goal (1 subgoal):
 1. FW_word p q \<le>p take q (FW_word p q) \<cdot> FW_word p q
[PROOF STEP]
next
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. FW_word p q \<le>p take q (FW_word p q) \<cdot> FW_word p q
[PROOF STEP]
show "FW_word p q \<le>p take q (FW_word p q) \<cdot> FW_word p q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. FW_word p q \<le>p take q (FW_word p q) \<cdot> FW_word p q
[PROOF STEP]
unfolding takeq
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. FW_word p q \<le>p (butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q)) \<cdot> [gcd p q] \<cdot> [0..<gcd p q]) \<cdot> FW_word p q
[PROOF STEP]
unfolding fw fw_base_def fw_p_per_def rassoc pref_cancel_conv but_dec but_dec_b \<open>?pd = Suc e\<close> pow_Suc2 butlast_append pow_Suc if_not_P[OF \<open>?dw \<noteq> \<epsilon>\<close>]
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q] \<le>p [0..<gcd p q] \<cdot> [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q] \<cdot> [gcd p q] \<cdot> [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q]
[PROOF STEP]
unfolding lassoc power_commutes[symmetric]
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q] \<le>p (((([0..<gcd p q] \<^sup>@ e \<cdot> [0..<gcd p q]) \<cdot> butlast [0..<gcd p q]) \<cdot> [gcd p q]) \<cdot> [0..<gcd p q] \<^sup>@ e) \<cdot> butlast [0..<gcd p q]
[PROOF STEP]
unfolding rassoc pref_cancel_conv
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. butlast [0..<gcd p q] \<le>p [0..<gcd p q] \<cdot> butlast [0..<gcd p q] \<cdot> [gcd p q] \<cdot> [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q]
[PROOF STEP]
using pref_ext[OF prefixeq_butlast, of "?dw"]
[PROOF STATE]
proof (prove)
using this:
butlast [0..<gcd p q] \<le>p [0..<gcd p q] \<cdot> ?zs

goal (1 subgoal):
 1. butlast [0..<gcd p q] \<le>p [0..<gcd p q] \<cdot> butlast [0..<gcd p q] \<cdot> [gcd p q] \<cdot> [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q]
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
FW_word p q \<le>p take q (FW_word p q) \<cdot> FW_word p q

goal:
No subgoals!
[PROOF STEP]
qed
[PROOF STATE]
proof (state)
this:
period (FW_word p q) q

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "\<not> period (FW_word p q) ?d"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
proof-
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have last_a: "last (take p (FW_word p q)) = ?d"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. last (take p (FW_word p q)) = gcd p q
[PROOF STEP]
unfolding takep nth_append_length[of "butlast (?dw \<^sup>@ ?pd)" "?d" \<epsilon>]
            last_snoc
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. gcd p q = gcd p q
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
last (take p (FW_word p q)) = gcd p q

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "?dw \<le>p FW_word p q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. [0..<gcd p q] \<le>p FW_word p q
[PROOF STEP]
unfolding fw but_dec rassoc fw_base_def fw_p_per_def
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. [0..<gcd p q] \<le>p [0..<gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ e) \<cdot> [gcd p q] \<cdot> [0..<gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ e)
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
[0..<gcd p q] \<le>p FW_word p q

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
from pref_take[OF this, unfolded \<open>\<^bold>|?dw\<^bold>| = ?d\<close>]
[PROOF STATE]
proof (chain)
picking this:
take (gcd p q) (FW_word p q) = [0..<gcd p q]
[PROOF STEP]
have takegcd:  "take (gcd p q) (FW_word p q) = [0..<gcd p q]"
[PROOF STATE]
proof (prove)
using this:
take (gcd p q) (FW_word p q) = [0..<gcd p q]

goal (1 subgoal):
 1. take (gcd p q) (FW_word p q) = [0..<gcd p q]
[PROOF STEP]
.
[PROOF STATE]
proof (state)
this:
take (gcd p q) (FW_word p q) = [0..<gcd p q]

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have fw_dec_b: "FW_word p q = [0..<gcd p q]\<^sup>@e \<cdot> butlast ([0..<gcd p q]) \<cdot> [?d] \<cdot> (butlast ([0..<gcd p q]\<^sup>@(p div gcd p q)))"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. FW_word p q = [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q] \<cdot> [gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))
[PROOF STEP]
unfolding fw but_dec_b rassoc fw_base_def fw_p_per_def
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q] \<cdot> [gcd p q] \<cdot> [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q] = [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q] \<cdot> [gcd p q] \<cdot> [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q]
[PROOF STEP]
..
[PROOF STATE]
proof (state)
this:
FW_word p q = [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q] \<cdot> [gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have gcdepref:  "[0..<gcd p q]\<^sup>@ Suc e \<le>p take (gcd p q) (FW_word p q) \<cdot> FW_word p q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. [0..<gcd p q] \<^sup>@ Suc e \<le>p take (gcd p q) (FW_word p q) \<cdot> FW_word p q
[PROOF STEP]
unfolding takegcd pow_Suc pref_cancel_conv
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. [0..<gcd p q] \<^sup>@ e \<le>p FW_word p q
[PROOF STEP]
unfolding fw_dec_b
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. [0..<gcd p q] \<^sup>@ e \<le>p [0..<gcd p q] \<^sup>@ e \<cdot> butlast [0..<gcd p q] \<cdot> [gcd p q] \<cdot> butlast ([0..<gcd p q] \<^sup>@ (p div gcd p q))
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
[0..<gcd p q] \<^sup>@ Suc e \<le>p take (gcd p q) (FW_word p q) \<cdot> FW_word p q

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "\<^bold>|[0..<gcd p q]\<^sup>@ Suc e\<^bold>| = p"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<^bold>|[0..<gcd p q] \<^sup>@ Suc e\<^bold>| = p
[PROOF STEP]
unfolding pow_len \<open>\<^bold>|?dw\<^bold>| = ?d\<close> \<open>?pd = Suc e\<close>[symmetric]
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p div gcd p q * gcd p q = p
[PROOF STEP]
using
            dvd_div_mult_self[OF gcd_dvd1]
[PROOF STATE]
proof (prove)
using this:
?b div gcd ?b ?b1 * gcd ?b ?b1 = ?b

goal (1 subgoal):
 1. p div gcd p q * gcd p q = p
[PROOF STEP]
.
[PROOF STATE]
proof (state)
this:
\<^bold>|[0..<gcd p q] \<^sup>@ Suc e\<^bold>| = p

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
from pref_take[OF gcdepref, unfolded this]
[PROOF STATE]
proof (chain)
picking this:
take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q) = [0..<gcd p q] \<^sup>@ Suc e
[PROOF STEP]
have takegcdp:  "take p (take (gcd p q) (FW_word p q) \<cdot> (FW_word p q)) = [0..<gcd p q]\<^sup>@e \<cdot> [0..<gcd p q]"
[PROOF STATE]
proof (prove)
using this:
take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q) = [0..<gcd p q] \<^sup>@ Suc e

goal (1 subgoal):
 1. take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q) = [0..<gcd p q] \<^sup>@ e \<cdot> [0..<gcd p q]
[PROOF STEP]
unfolding pow_Suc2
[PROOF STATE]
proof (prove)
using this:
take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q) = [0..<gcd p q] \<^sup>@ e \<cdot> [0..<gcd p q]

goal (1 subgoal):
 1. take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q) = [0..<gcd p q] \<^sup>@ e \<cdot> [0..<gcd p q]
[PROOF STEP]
.
[PROOF STATE]
proof (state)
this:
take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q) = [0..<gcd p q] \<^sup>@ e \<cdot> [0..<gcd p q]

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "0 < gcd p q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. 0 < gcd p q
[PROOF STEP]
by (simp add: \<open>p \<noteq> 0\<close>)
[PROOF STATE]
proof (state)
this:
0 < gcd p q

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
from last_upt[OF this]
[PROOF STATE]
proof (chain)
picking this:
last [0..<gcd p q] = gcd p q - 1
[PROOF STEP]
have last_b: "last (take p (take (gcd p q) (FW_word p q) \<cdot> (FW_word p q))) = gcd p q - 1"
[PROOF STATE]
proof (prove)
using this:
last [0..<gcd p q] = gcd p q - 1

goal (1 subgoal):
 1. last (take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q)) = gcd p q - 1
[PROOF STEP]
unfolding takegcdp  last_appendR[of "[0..<gcd p q]" "[0..<gcd p q]\<^sup>@e", OF \<open>[0..<gcd p q] \<noteq> \<epsilon>\<close>]
[PROOF STATE]
proof (prove)
using this:
last [0..<gcd p q] = gcd p q - 1

goal (1 subgoal):
 1. last [0..<gcd p q] = gcd p q - 1
[PROOF STEP]
.
[PROOF STATE]
proof (state)
this:
last (take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q)) = gcd p q - 1

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "p \<le> \<^bold>|FW_word p q\<^bold>|"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p \<le> \<^bold>|FW_word p q\<^bold>|
[PROOF STEP]
unfolding \<open>\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1\<close> \<open>gcd p q = q - p\<close>
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p \<le> p + q - (q - p) - 1
[PROOF STEP]
using  \<open>p < q\<close>
[PROOF STATE]
proof (prove)
using this:
p < q

goal (1 subgoal):
 1. p \<le> p + q - (q - p) - 1
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
p \<le> \<^bold>|FW_word p q\<^bold>|

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "gcd p q \<noteq> gcd p q - 1"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. gcd p q \<noteq> gcd p q - 1
[PROOF STEP]
using \<open>gcd p q = q - p\<close> \<open>p < q\<close>
[PROOF STATE]
proof (prove)
using this:
gcd p q = q - p
p < q

goal (1 subgoal):
 1. gcd p q \<noteq> gcd p q - 1
[PROOF STEP]
by linarith
[PROOF STATE]
proof (state)
this:
gcd p q \<noteq> gcd p q - 1

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
hence "take p (FW_word p q) \<noteq> take p (take (gcd p q) (FW_word p q) \<cdot> (FW_word p q))"
[PROOF STATE]
proof (prove)
using this:
gcd p q \<noteq> gcd p q - 1

goal (1 subgoal):
 1. take p (FW_word p q) \<noteq> take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q)
[PROOF STEP]
unfolding last_b[symmetric]
[PROOF STATE]
proof (prove)
using this:
gcd p q \<noteq> last (take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q))

goal (1 subgoal):
 1. take p (FW_word p q) \<noteq> take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q)
[PROOF STEP]
unfolding last_a[symmetric]
[PROOF STATE]
proof (prove)
using this:
last (take p (FW_word p q)) \<noteq> last (take p (take (last (take p (FW_word p q))) (FW_word p q) \<cdot> FW_word p q))

goal (1 subgoal):
 1. take p (FW_word p q) \<noteq> take p (take (last (take p (FW_word p q))) (FW_word p q) \<cdot> FW_word p q)
[PROOF STEP]
using arg_cong[of _ _ last]
[PROOF STATE]
proof (prove)
using this:
last (take p (FW_word p q)) \<noteq> last (take p (take (last (take p (FW_word p q))) (FW_word p q) \<cdot> FW_word p q))
?x = ?y \<Longrightarrow> last ?x = last ?y

goal (1 subgoal):
 1. take p (FW_word p q) \<noteq> take p (take (last (take p (FW_word p q))) (FW_word p q) \<cdot> FW_word p q)
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
take p (FW_word p q) \<noteq> take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q)

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
hence "\<not> FW_word p q \<le>p take (gcd p q) (FW_word p q) \<cdot> FW_word p q "
[PROOF STATE]
proof (prove)
using this:
take p (FW_word p q) \<noteq> take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q)

goal (1 subgoal):
 1. \<not> FW_word p q \<le>p take (gcd p q) (FW_word p q) \<cdot> FW_word p q
[PROOF STEP]
using pref_share_take[OF _ \<open>p \<le> \<^bold>|FW_word p q\<^bold>|\<close>, of "take (gcd p q) (FW_word p q) \<cdot> FW_word p q"]
[PROOF STATE]
proof (prove)
using this:
take p (FW_word p q) \<noteq> take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q)
FW_word p q \<le>p take (gcd p q) (FW_word p q) \<cdot> FW_word p q \<Longrightarrow> take p (FW_word p q) = take p (take (gcd p q) (FW_word p q) \<cdot> FW_word p q)

goal (1 subgoal):
 1. \<not> FW_word p q \<le>p take (gcd p q) (FW_word p q) \<cdot> FW_word p q
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
\<not> FW_word p q \<le>p take (gcd p q) (FW_word p q) \<cdot> FW_word p q

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
thus "\<not> period (FW_word p q) (gcd p q)"
[PROOF STATE]
proof (prove)
using this:
\<not> FW_word p q \<le>p take (gcd p q) (FW_word p q) \<cdot> FW_word p q

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
unfolding period_def period_root_def
[PROOF STATE]
proof (prove)
using this:
\<not> FW_word p q \<le>p take (gcd p q) (FW_word p q) \<cdot> FW_word p q

goal (1 subgoal):
 1. \<not> (FW_word p q \<le>p take (gcd p q) (FW_word p q) \<cdot> FW_word p q \<and> take (gcd p q) (FW_word p q) \<noteq> \<epsilon>)
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
\<not> period (FW_word p q) (gcd p q)

goal:
No subgoals!
[PROOF STEP]
qed
[PROOF STATE]
proof (state)
this:
\<not> period (FW_word p q) (gcd p q)

goal (2 subgoals):
 1. \<lbrakk>p < q; gcd p q = q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
 2. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
show ?thesis
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
using \<open>period (FW_word p q) p\<close> \<open>period (FW_word p q) q\<close>
          \<open>\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1\<close> \<open>\<not> period (FW_word p q) (gcd p q)\<close>
[PROOF STATE]
proof (prove)
using this:
period (FW_word p q) p
period (FW_word p q) q
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
\<not> period (FW_word p q) (gcd p q)

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
next
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
let ?d' = "gcd p (q-p)"
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
assume "gcd p q \<noteq> q - p"
[PROOF STATE]
proof (state)
this:
gcd p q \<noteq> q - p

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
hence fw: "FW_word p q = (take p (FW_word p (q-p))) \<cdot> FW_word p (q-p)"
[PROOF STATE]
proof (prove)
using this:
gcd p q \<noteq> q - p

goal (1 subgoal):
 1. FW_word p q = take p (FW_word p (q - p)) \<cdot> FW_word p (q - p)
[PROOF STEP]
using FW_word_def \<open>p \<noteq> 0\<close> \<open>p \<noteq> q\<close> \<open>p < q\<close>
[PROOF STATE]
proof (prove)
using this:
gcd p q \<noteq> q - p
FW_word ?p ?q = (if ?q < ?p then FW_word ?q ?p else if ?p = 0 then \<epsilon> else if ?p = ?q then \<epsilon> else if gcd ?p ?q = ?q - ?p then fw_base ?p ?q else take ?p (FW_word ?p (?q - ?p)) \<cdot> FW_word ?p (?q - ?p))
p \<noteq> 0
p \<noteq> q
p < q

goal (1 subgoal):
 1. FW_word p q = take p (FW_word p (q - p)) \<cdot> FW_word p (q - p)
[PROOF STEP]
by (meson less_Suc_eq not_less_eq)
[PROOF STATE]
proof (state)
this:
FW_word p q = take p (FW_word p (q - p)) \<cdot> FW_word p (q - p)

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have divhyp1: "\<not> p dvd q - p"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<not> p dvd q - p
[PROOF STEP]
using \<open>\<not> p dvd q\<close> \<open>p < q\<close> dvd_minus_self
[PROOF STATE]
proof (prove)
using this:
\<not> p dvd q
p < q
(?m dvd ?n - ?m) = (?n < ?m \<or> ?m dvd ?n)

goal (1 subgoal):
 1. \<not> p dvd q - p
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
\<not> p dvd q - p

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have divhyp2: "\<not> q - p dvd p"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<not> q - p dvd p
[PROOF STEP]
proof (rule notI)
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. q - p dvd p \<Longrightarrow> False
[PROOF STEP]
assume "q - p dvd p"
[PROOF STATE]
proof (state)
this:
q - p dvd p

goal (1 subgoal):
 1. q - p dvd p \<Longrightarrow> False
[PROOF STEP]
have "q = p + (q - p)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. q = p + (q - p)
[PROOF STEP]
by (simp add: \<open>p < q\<close> less_or_eq_imp_le)
[PROOF STATE]
proof (state)
this:
q = p + (q - p)

goal (1 subgoal):
 1. q - p dvd p \<Longrightarrow> False
[PROOF STEP]
from gcd_add2[of p "q - p", folded  this, unfolded gcd_nat.absorb2[of "q - p" p, OF \<open>q - p dvd p\<close>]]
[PROOF STATE]
proof (chain)
picking this:
gcd p q = q - p
[PROOF STEP]
show "False"
[PROOF STATE]
proof (prove)
using this:
gcd p q = q - p

goal (1 subgoal):
 1. False
[PROOF STEP]
using \<open>gcd p q \<noteq> q - p\<close>
[PROOF STATE]
proof (prove)
using this:
gcd p q = q - p
gcd p q \<noteq> q - p

goal (1 subgoal):
 1. False
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
False

goal:
No subgoals!
[PROOF STEP]
qed
[PROOF STATE]
proof (state)
this:
\<not> q - p dvd p

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have lenhyp: "p + p + (q - p) < p + p + q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p + p + (q - p) < p + p + q
[PROOF STEP]
using \<open>p < q\<close> \<open>p \<noteq> 0\<close>
[PROOF STATE]
proof (prove)
using this:
p < q
p \<noteq> 0

goal (1 subgoal):
 1. p + p + (q - p) < p + p + q
[PROOF STEP]
by linarith

\<comment> \<open>induction assumption\<close>
[PROOF STATE]
proof (state)
this:
p + p + (q - p) < p + p + q

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "\<^bold>|FW_word p (q - p)\<^bold>| = p + (q - p) - ?d' - 1" and "period (FW_word p (q-p)) p" and "period (FW_word p (q-p)) (q-p)" and
        "\<not> period (FW_word p (q-p)) (gcd p (q-p))"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. (\<^bold>|FW_word p (q - p)\<^bold>| = p + (q - p) - gcd p (q - p) - 1 &&& period (FW_word p (q - p)) p) &&& period (FW_word p (q - p)) (q - p) &&& \<not> period (FW_word p (q - p)) (gcd p (q - p))
[PROOF STEP]
using "less.hyps"[OF _ divhyp1 divhyp2] lenhyp
[PROOF STATE]
proof (prove)
using this:
p + p + (q - p) < p + p + q \<Longrightarrow> \<^bold>|FW_word p (q - p)\<^bold>| = p + (q - p) - gcd p (q - p) - 1 \<and> period (FW_word p (q - p)) p \<and> period (FW_word p (q - p)) (q - p) \<and> \<not> period (FW_word p (q - p)) (gcd p (q - p))
p + p + (q - p) < p + p + q

goal (1 subgoal):
 1. (\<^bold>|FW_word p (q - p)\<^bold>| = p + (q - p) - gcd p (q - p) - 1 &&& period (FW_word p (q - p)) p) &&& period (FW_word p (q - p)) (q - p) &&& \<not> period (FW_word p (q - p)) (gcd p (q - p))
[PROOF STEP]
by blast+

\<comment> \<open>auxiliary facts\<close>
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p (q - p)\<^bold>| = p + (q - p) - gcd p (q - p) - 1
period (FW_word p (q - p)) p
period (FW_word p (q - p)) (q - p)
\<not> period (FW_word p (q - p)) (gcd p (q - p))

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "p + (q - p) = q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p + (q - p) = q
[PROOF STEP]
using divhyp1 dvd_minus_self
[PROOF STATE]
proof (prove)
using this:
\<not> p dvd q - p
(?m dvd ?n - ?m) = (?n < ?m \<or> ?m dvd ?n)

goal (1 subgoal):
 1. p + (q - p) = q
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
p + (q - p) = q

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "?d = ?d'"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. gcd p q = gcd p (q - p)
[PROOF STEP]
using  gcd_add2[of p "q-p", unfolded le_add_diff_inverse[OF less_imp_le[OF \<open>p < q\<close>]]]
[PROOF STATE]
proof (prove)
using this:
gcd p q = gcd p (q - p)

goal (1 subgoal):
 1. gcd p q = gcd p (q - p)
[PROOF STEP]
.
[PROOF STATE]
proof (state)
this:
gcd p q = gcd p (q - p)

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "?d \<noteq> q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. gcd p q \<noteq> q
[PROOF STEP]
using  \<open>\<not> q dvd p\<close>  gcd_dvd2[of q p, unfolded gcd.commute[of q]]
[PROOF STATE]
proof (prove)
using this:
\<not> q dvd p
gcd p q dvd p

goal (1 subgoal):
 1. gcd p q \<noteq> q
[PROOF STEP]
by force
[PROOF STATE]
proof (state)
this:
gcd p q \<noteq> q

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
from this[unfolded nat_neq_iff]
[PROOF STATE]
proof (chain)
picking this:
gcd p q < q \<or> q < gcd p q
[PROOF STEP]
have "?d < q"
[PROOF STATE]
proof (prove)
using this:
gcd p q < q \<or> q < gcd p q

goal (1 subgoal):
 1. gcd p q < q
[PROOF STEP]
using  gr_implies_not0 \<open>p < q\<close> nat_dvd_not_less
[PROOF STATE]
proof (prove)
using this:
gcd p q < q \<or> q < gcd p q
?m < ?n \<Longrightarrow> ?n \<noteq> 0
p < q
\<lbrakk>0 < ?m; ?m < ?n\<rbrakk> \<Longrightarrow> \<not> ?n dvd ?m

goal (1 subgoal):
 1. gcd p q < q
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
gcd p q < q

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
hence "1 \<le> q - ?d"
[PROOF STATE]
proof (prove)
using this:
gcd p q < q

goal (1 subgoal):
 1. 1 \<le> q - gcd p q
[PROOF STEP]
by linarith
[PROOF STATE]
proof (state)
this:
1 \<le> q - gcd p q

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "?d' < q - p"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. gcd p (q - p) < q - p
[PROOF STEP]
using  gcd_le2_nat[OF per_positive[OF \<open>period (FW_word p (q - p)) (q - p)\<close>], of p]  divhyp2[unfolded  gcd_nat.absorb_iff2] nat_less_le
[PROOF STATE]
proof (prove)
using this:
gcd p (q - p) \<le> q - p
gcd p (q - p) \<noteq> q - p
(?m < ?n) = (?m \<le> ?n \<and> ?m \<noteq> ?n)

goal (1 subgoal):
 1. gcd p (q - p) < q - p
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
gcd p (q - p) < q - p

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
hence "p \<le> \<^bold>|(FW_word p (q - p))\<^bold>|"
[PROOF STATE]
proof (prove)
using this:
gcd p (q - p) < q - p

goal (1 subgoal):
 1. p \<le> \<^bold>|FW_word p (q - p)\<^bold>|
[PROOF STEP]
unfolding  \<open>\<^bold>|FW_word p (q - p)\<^bold>| = p + (q - p) - ?d' - 1\<close>  diff_diff_left discrete
[PROOF STATE]
proof (prove)
using this:
gcd p (q - p) + 1 \<le> q - p

goal (1 subgoal):
 1. p \<le> p + (q - p) - (gcd p (q - p) + 1)
[PROOF STEP]
by linarith
[PROOF STATE]
proof (state)
this:
p \<le> \<^bold>|FW_word p (q - p)\<^bold>|

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "FW_word p (q - p) \<noteq> \<epsilon>"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. FW_word p (q - p) \<noteq> \<epsilon>
[PROOF STEP]
unfolding length_0_conv[symmetric]
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<^bold>|FW_word p (q - p)\<^bold>| \<noteq> 0
[PROOF STEP]
using  \<open>p \<le> \<^bold>|FW_word p (q - p)\<^bold>|\<close> \<open>p \<noteq> 0\<close>[folded le_zero_eq]
[PROOF STATE]
proof (prove)
using this:
p \<le> \<^bold>|FW_word p (q - p)\<^bold>|
\<not> p \<le> 0

goal (1 subgoal):
 1. \<^bold>|FW_word p (q - p)\<^bold>| \<noteq> 0
[PROOF STEP]
by linarith

\<comment> \<open>claim 1\<close>
[PROOF STATE]
proof (state)
this:
FW_word p (q - p) \<noteq> \<epsilon>

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "\<^bold>|FW_word p q\<^bold>| = p + q - ?d - 1"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
proof-
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
have "p + (q - p) = q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p + (q - p) = q
[PROOF STEP]
using less_imp_le[OF \<open>p < q\<close>]
[PROOF STATE]
proof (prove)
using this:
p \<le> q

goal (1 subgoal):
 1. p + (q - p) = q
[PROOF STEP]
by fastforce
[PROOF STATE]
proof (state)
this:
p + (q - p) = q

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
have "\<^bold>|FW_word p q\<^bold>| = \<^bold>|take p (FW_word p (q - p))\<^bold>| + \<^bold>|FW_word p (q - p)\<^bold>|"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = \<^bold>|take p (FW_word p (q - p))\<^bold>| + \<^bold>|FW_word p (q - p)\<^bold>|
[PROOF STEP]
using fw lenmorph[of "take p (FW_word p (q - p))" "FW_word p (q - p)"]
[PROOF STATE]
proof (prove)
using this:
FW_word p q = take p (FW_word p (q - p)) \<cdot> FW_word p (q - p)
\<^bold>|take p (FW_word p (q - p)) \<cdot> FW_word p (q - p)\<^bold>| = \<^bold>|take p (FW_word p (q - p))\<^bold>| + \<^bold>|FW_word p (q - p)\<^bold>|

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = \<^bold>|take p (FW_word p (q - p))\<^bold>| + \<^bold>|FW_word p (q - p)\<^bold>|
[PROOF STEP]
by presburger
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p q\<^bold>| = \<^bold>|take p (FW_word p (q - p))\<^bold>| + \<^bold>|FW_word p (q - p)\<^bold>|

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
also
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p q\<^bold>| = \<^bold>|take p (FW_word p (q - p))\<^bold>| + \<^bold>|FW_word p (q - p)\<^bold>|

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
have "... = p + (p + (q - p) - ?d' - 1)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<^bold>|take p (FW_word p (q - p))\<^bold>| + \<^bold>|FW_word p (q - p)\<^bold>| = p + (p + (q - p) - gcd p (q - p) - 1)
[PROOF STEP]
unfolding \<open>\<^bold>|FW_word p (q - p)\<^bold>| = p + (q - p) - ?d' - 1\<close>
            take_len[OF \<open>p \<le> \<^bold>|FW_word p (q - p)\<^bold>|\<close>]
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p + (p + (q - p) - gcd p (q - p) - 1) = p + (p + (q - p) - gcd p (q - p) - 1)
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
\<^bold>|take p (FW_word p (q - p))\<^bold>| + \<^bold>|FW_word p (q - p)\<^bold>| = p + (p + (q - p) - gcd p (q - p) - 1)

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
also
[PROOF STATE]
proof (state)
this:
\<^bold>|take p (FW_word p (q - p))\<^bold>| + \<^bold>|FW_word p (q - p)\<^bold>| = p + (p + (q - p) - gcd p (q - p) - 1)

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
have "... = p + (q - ?d  - 1)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p + (p + (q - p) - gcd p (q - p) - 1) = p + (q - gcd p q - 1)
[PROOF STEP]
unfolding \<open>?d = ?d'\<close> \<open>p + (q - p) = q\<close>
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p + (q - gcd p (q - p) - 1) = p + (q - gcd p (q - p) - 1)
[PROOF STEP]
..
[PROOF STATE]
proof (state)
this:
p + (p + (q - p) - gcd p (q - p) - 1) = p + (q - gcd p q - 1)

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
also
[PROOF STATE]
proof (state)
this:
p + (p + (q - p) - gcd p (q - p) - 1) = p + (q - gcd p q - 1)

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
have "... = p + (q - ?d) - 1"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p + (q - gcd p q - 1) = p + (q - gcd p q) - 1
[PROOF STEP]
using Nat.add_diff_assoc[OF \<open>1 \<le> q - ?d\<close>]
[PROOF STATE]
proof (prove)
using this:
?i + (q - gcd p q - 1) = ?i + (q - gcd p q) - 1

goal (1 subgoal):
 1. p + (q - gcd p q - 1) = p + (q - gcd p q) - 1
[PROOF STEP]
.
[PROOF STATE]
proof (state)
this:
p + (q - gcd p q - 1) = p + (q - gcd p q) - 1

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
also
[PROOF STATE]
proof (state)
this:
p + (q - gcd p q - 1) = p + (q - gcd p q) - 1

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
have "... = p + q - ?d - 1"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. p + (q - gcd p q) - 1 = p + q - gcd p q - 1
[PROOF STEP]
by (simp add: \<open>?d < q\<close> less_or_eq_imp_le)
[PROOF STATE]
proof (state)
this:
p + (q - gcd p q) - 1 = p + q - gcd p q - 1

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
finally
[PROOF STATE]
proof (chain)
picking this:
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
show "\<^bold>|FW_word p q\<^bold>| = p + q - ?d - 1"
[PROOF STATE]
proof (prove)
using this:
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1
[PROOF STEP]
by presburger
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1

goal:
No subgoals!
[PROOF STEP]
qed

\<comment> \<open>claim 2\<close>
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "period (FW_word p q) p"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. period (FW_word p q) p
[PROOF STEP]
using fw  ext_per_left[OF \<open>period (FW_word p (q-p)) p\<close> \<open>p \<le> \<^bold>|FW_word p (q - p)\<^bold>|\<close>]
[PROOF STATE]
proof (prove)
using this:
FW_word p q = take p (FW_word p (q - p)) \<cdot> FW_word p (q - p)
period (take p (FW_word p (q - p)) \<cdot> FW_word p (q - p)) p

goal (1 subgoal):
 1. period (FW_word p q) p
[PROOF STEP]
by presburger

\<comment> \<open>claim 3\<close>
[PROOF STATE]
proof (state)
this:
period (FW_word p q) p

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "period (FW_word p q) q"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. period (FW_word p q) q
[PROOF STEP]
using ext_per_sum[OF \<open>period (FW_word p (q - p)) p\<close> \<open>period (FW_word p (q - p)) (q - p)\<close> \<open>p \<le> \<^bold>|FW_word p (q - p)\<^bold>|\<close>, folded fw, unfolded \<open>p + (q-p) = q\<close>]
[PROOF STATE]
proof (prove)
using this:
period (FW_word p q) q

goal (1 subgoal):
 1. period (FW_word p q) q
[PROOF STEP]
.

\<comment> \<open>claim 4\<close>
[PROOF STATE]
proof (state)
this:
period (FW_word p q) q

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
have "\<not> period (FW_word p q) ?d"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
using \<open>\<not> period (FW_word p (q -p)) (gcd p (q-p))\<close>
[PROOF STATE]
proof (prove)
using this:
\<not> period (FW_word p (q - p)) (gcd p (q - p))

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
unfolding \<open>?d = ?d'\<close>[symmetric]
[PROOF STATE]
proof (prove)
using this:
\<not> period (FW_word p (q - p)) (gcd p q)

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
using period_fac[of "take p (FW_word p (q - p))" "FW_word p (q - p)" \<epsilon> "?d", unfolded append_Nil2,
                          OF _ \<open>FW_word p (q - p) \<noteq> \<epsilon>\<close>, folded fw]
[PROOF STATE]
proof (prove)
using this:
\<not> period (FW_word p (q - p)) (gcd p q)
period (FW_word p q) (gcd p q) \<Longrightarrow> period (FW_word p (q - p)) (gcd p q)

goal (1 subgoal):
 1. \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
\<not> period (FW_word p q) (gcd p q)

goal (1 subgoal):
 1. \<lbrakk>p < q; gcd p q \<noteq> q - p\<rbrakk> \<Longrightarrow> \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
thus ?thesis
[PROOF STATE]
proof (prove)
using this:
\<not> period (FW_word p q) (gcd p q)

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
using \<open>period (FW_word p q) p\<close> \<open>period (FW_word p q) q\<close> \<open>\<^bold>|FW_word p q\<^bold>| = p + q - ?d - 1\<close>
[PROOF STATE]
proof (prove)
using this:
\<not> period (FW_word p q) (gcd p q)
period (FW_word p q) p
period (FW_word p q) q
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1

goal (1 subgoal):
 1. \<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)
[PROOF STEP]
by blast
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)

goal:
No subgoals!
[PROOF STEP]
qed
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)

goal:
No subgoals!
[PROOF STEP]
qed
[PROOF STATE]
proof (state)
this:
\<^bold>|FW_word p q\<^bold>| = p + q - gcd p q - 1 \<and> period (FW_word p q) p \<and> period (FW_word p q) q \<and> \<not> period (FW_word p q) (gcd p q)

goal:
No subgoals!
[PROOF STEP]
qed


[STATEMENT]
lemma (in group) inv_solve_left':
  "\<lbrakk> a \<in> carrier G; b \<in> carrier G; c \<in> carrier G \<rbrakk> \<Longrightarrow> inv b \<otimes> c = a \<longleftrightarrow> c = b \<otimes> a"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<lbrakk>a \<in> carrier G; b \<in> carrier G; c \<in> carrier G\<rbrakk> \<Longrightarrow> (inv b \<otimes> c = a) = (c = b \<otimes> a)
[PROOF STEP]
by (metis inv_equality l_inv_ex l_one m_assoc r_inv)


[STATEMENT]
lemma row_space_eq_col_space_transpose:
  shows "row_space A = col_space A\<^sup>T"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. row_space A = col_space A\<^sup>T
[PROOF STEP]
unfolding col_space_def row_space_def cols_transpose
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. local.span (set (rows A)) = local.span (set (rows A))
[PROOF STEP]
..


[STATEMENT]
lemma minus_suc: "n > 0 \<Longrightarrow> n - Suc 0 \<noteq> n"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. 0 < n \<Longrightarrow> n - Suc 0 \<noteq> n
[PROOF STEP]
by linarith


[STATEMENT]
lemma d_delta_arctan_upper_14:
  "((\<lambda>x. arctan_upper_14 x - arctan x) has_field_derivative diff_delta_arctan_upper_14 x) (at x)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. ((\<lambda>x. arctan_upper_14 x - arctan x) has_real_derivative diff_delta_arctan_upper_14 x) (at x)
[PROOF STEP]
unfolding arctan_upper_14_def diff_delta_arctan_upper_14_def
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. ((\<lambda>x. pi / 2 - 3 * x / (1 + 3 * x\<^sup>2) - arctan x) has_real_derivative - 4 / ((1 + 3 * x\<^sup>2)\<^sup>2 * (1 + x\<^sup>2))) (at x)
[PROOF STEP]
apply (intro derivative_eq_intros | simp add: add_nonneg_eq_0_iff)+
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. - ((3 + 9 * x\<^sup>2 - 18 * (x * x)) / ((1 + 3 * x\<^sup>2) * (1 + 3 * x\<^sup>2))) - inverse (1 + x\<^sup>2) = - (4 / ((1 + 3 * x\<^sup>2)\<^sup>2 * (1 + x\<^sup>2)))
[PROOF STEP]
apply (simp add: divide_simps add_nonneg_eq_0_iff, algebra)
[PROOF STATE]
proof (prove)
goal:
No subgoals!
[PROOF STEP]
done


[STATEMENT]
lemma gcd_0_0[simp]: "gcd 0 0 = 0"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. gcd (0::'a) (0::'a) = (0::'a)
[PROOF STEP]
using gcd_greatest[OF dvd_0_right dvd_0_right, of 0]
[PROOF STATE]
proof (prove)
using this:
(0::'a) dvd gcd (0::'a) (0::'a)

goal (1 subgoal):
 1. gcd (0::'a) (0::'a) = (0::'a)
[PROOF STEP]
by auto


[STATEMENT]
lemma real_of_int_div2: "0 \<le> real_of_int n / real_of_int x - real_of_int (n div x)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. 0 \<le> real_of_int n / real_of_int x - real_of_int (n div x)
[PROOF STEP]
proof (cases "x = 0")
[PROOF STATE]
proof (state)
goal (2 subgoals):
 1. x = 0 \<Longrightarrow> 0 \<le> real_of_int n / real_of_int x - real_of_int (n div x)
 2. x \<noteq> 0 \<Longrightarrow> 0 \<le> real_of_int n / real_of_int x - real_of_int (n div x)
[PROOF STEP]
case False
[PROOF STATE]
proof (state)
this:
x \<noteq> 0

goal (2 subgoals):
 1. x = 0 \<Longrightarrow> 0 \<le> real_of_int n / real_of_int x - real_of_int (n div x)
 2. x \<noteq> 0 \<Longrightarrow> 0 \<le> real_of_int n / real_of_int x - real_of_int (n div x)
[PROOF STEP]
then
[PROOF STATE]
proof (chain)
picking this:
x \<noteq> 0
[PROOF STEP]
show ?thesis
[PROOF STATE]
proof (prove)
using this:
x \<noteq> 0

goal (1 subgoal):
 1. 0 \<le> real_of_int n / real_of_int x - real_of_int (n div x)
[PROOF STEP]
by (metis diff_ge_0_iff_ge floor_divide_of_int_eq of_int_floor_le)
[PROOF STATE]
proof (state)
this:
0 \<le> real_of_int n / real_of_int x - real_of_int (n div x)

goal (1 subgoal):
 1. x = 0 \<Longrightarrow> 0 \<le> real_of_int n / real_of_int x - real_of_int (n div x)
[PROOF STEP]
qed simp


[STATEMENT]
lemma max_reg_Times: "max_reg (Times a1 a2) = max (max_reg a1) (max_reg a2)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. max_reg (Times a1 a2) = max (max_reg a1) (max_reg a2)
[PROOF STEP]
apply (simp add: max_reg_def Let_def max_absorb2)
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. enumerate_regs a1 \<noteq> {} \<longrightarrow> enumerate_regs a2 \<noteq> {} \<longrightarrow> Some (Max (enumerate_regs a1 \<union> enumerate_regs a2)) = max (Some (Max (enumerate_regs a1))) (Some (Max (enumerate_regs a2)))
[PROOF STEP]
by (metis Max.union bot_option_def finite_enumerate_regs max_bot2 sup_Some sup_max)


[STATEMENT]
lemma "(-x = (0::real)) = (x = 0)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. (- x = 0) = (x = 0)
[PROOF STEP]
by arith


[STATEMENT]
lemma path_connected_Euclidean_space: "path_connected_space (Euclidean_space n)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. path_connected_space (Euclidean_space n)
[PROOF STEP]
by (simp add: contractible_imp_path_connected_space)


[STATEMENT]
lemma continuous_prod' [continuous_intros]:
  fixes f :: "'a \<Rightarrow> 'b::t2_space \<Rightarrow> 'c::topological_comm_monoid_mult"
  shows "(\<And>i. i \<in> I \<Longrightarrow> continuous F (f i)) \<Longrightarrow> continuous F (\<lambda>x. \<Prod>i\<in>I. f i x)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. (\<And>i. i \<in> I \<Longrightarrow> continuous F (f i)) \<Longrightarrow> continuous F (\<lambda>x. \<Prod>i\<in>I. f i x)
[PROOF STEP]
unfolding continuous_def
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. (\<And>i. i \<in> I \<Longrightarrow> (f i \<longlongrightarrow> f i (Lim F (\<lambda>x. x))) F) \<Longrightarrow> ((\<lambda>x. \<Prod>i\<in>I. f i x) \<longlongrightarrow> (\<Prod>i\<in>I. f i (Lim F (\<lambda>x. x)))) F
[PROOF STEP]
by (rule tendsto_prod')


[STATEMENT]
lemma sphere_params:
  fixes x y z :: real
  assumes "x*x + y*y + z*z = 1"
  shows "x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
proof (cases "z=1 \<or> z = -1")
[PROOF STATE]
proof (state)
goal (2 subgoals):
 1. z = 1 \<or> z = - 1 \<Longrightarrow> x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
 2. \<not> (z = 1 \<or> z = - 1) \<Longrightarrow> x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
case True
[PROOF STATE]
proof (state)
this:
z = 1 \<or> z = - 1

goal (2 subgoals):
 1. z = 1 \<or> z = - 1 \<Longrightarrow> x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
 2. \<not> (z = 1 \<or> z = - 1) \<Longrightarrow> x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
hence "x = 0 \<and> y = 0"
[PROOF STATE]
proof (prove)
using this:
z = 1 \<or> z = - 1

goal (1 subgoal):
 1. x = 0 \<and> y = 0
[PROOF STEP]
using assms
[PROOF STATE]
proof (prove)
using this:
z = 1 \<or> z = - 1
x * x + y * y + z * z = 1

goal (1 subgoal):
 1. x = 0 \<and> y = 0
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
x = 0 \<and> y = 0

goal (2 subgoals):
 1. z = 1 \<or> z = - 1 \<Longrightarrow> x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
 2. \<not> (z = 1 \<or> z = - 1) \<Longrightarrow> x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
thus ?thesis
[PROOF STATE]
proof (prove)
using this:
x = 0 \<and> y = 0

goal (1 subgoal):
 1. x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
using \<open>z = 1 \<or> z = -1\<close>
[PROOF STATE]
proof (prove)
using this:
x = 0 \<and> y = 0
z = 1 \<or> z = - 1

goal (1 subgoal):
 1. x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
by (auto simp add: cos_arcsin)
[PROOF STATE]
proof (state)
this:
x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)

goal (1 subgoal):
 1. \<not> (z = 1 \<or> z = - 1) \<Longrightarrow> x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
next
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. \<not> (z = 1 \<or> z = - 1) \<Longrightarrow> x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
case False
[PROOF STATE]
proof (state)
this:
\<not> (z = 1 \<or> z = - 1)

goal (1 subgoal):
 1. \<not> (z = 1 \<or> z = - 1) \<Longrightarrow> x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
hence "x \<noteq> 0 \<or> y \<noteq> 0"
[PROOF STATE]
proof (prove)
using this:
\<not> (z = 1 \<or> z = - 1)

goal (1 subgoal):
 1. x \<noteq> 0 \<or> y \<noteq> 0
[PROOF STEP]
using assms
[PROOF STATE]
proof (prove)
using this:
\<not> (z = 1 \<or> z = - 1)
x * x + y * y + z * z = 1

goal (1 subgoal):
 1. x \<noteq> 0 \<or> y \<noteq> 0
[PROOF STEP]
by (auto simp add: square_eq_1_iff)
[PROOF STATE]
proof (state)
this:
x \<noteq> 0 \<or> y \<noteq> 0

goal (1 subgoal):
 1. \<not> (z = 1 \<or> z = - 1) \<Longrightarrow> x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
thus ?thesis
[PROOF STATE]
proof (prove)
using this:
x \<noteq> 0 \<or> y \<noteq> 0

goal (1 subgoal):
 1. x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
using real_sqrt_unique[of y "1 - z*z"]
[PROOF STATE]
proof (prove)
using this:
x \<noteq> 0 \<or> y \<noteq> 0
\<lbrakk>y\<^sup>2 = 1 - z * z; 0 \<le> y\<rbrakk> \<Longrightarrow> sqrt (1 - z * z) = y

goal (1 subgoal):
 1. x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
using real_sqrt_unique[of "-y" "1 - z*z"]
[PROOF STATE]
proof (prove)
using this:
x \<noteq> 0 \<or> y \<noteq> 0
\<lbrakk>y\<^sup>2 = 1 - z * z; 0 \<le> y\<rbrakk> \<Longrightarrow> sqrt (1 - z * z) = y
\<lbrakk>(- y)\<^sup>2 = 1 - z * z; 0 \<le> - y\<rbrakk> \<Longrightarrow> sqrt (1 - z * z) = - y

goal (1 subgoal):
 1. x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
using sphere_bounds[OF assms] assms
[PROOF STATE]
proof (prove)
using this:
x \<noteq> 0 \<or> y \<noteq> 0
\<lbrakk>y\<^sup>2 = 1 - z * z; 0 \<le> y\<rbrakk> \<Longrightarrow> sqrt (1 - z * z) = y
\<lbrakk>(- y)\<^sup>2 = 1 - z * z; 0 \<le> - y\<rbrakk> \<Longrightarrow> sqrt (1 - z * z) = - y
- 1 \<le> x \<and> x \<le> 1
- 1 \<le> y \<and> y \<le> 1
- 1 \<le> z \<and> z \<le> 1
x * x + y * y + z * z = 1

goal (1 subgoal):
 1. x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)
[PROOF STEP]
by (auto simp add: cos_arcsin cos_arctan sin_arctan power2_eq_square field_simps real_sqrt_divide atan2_def)
[PROOF STATE]
proof (state)
this:
x = cos (arcsin z) * cos (atan2 y x) \<and> y = cos (arcsin z) * sin (atan2 y x) \<and> z = sin (arcsin z)

goal:
No subgoals!
[PROOF STEP]
qed


[STATEMENT]
lemma Kdelta_in_Zinf1:"\<lbrakk>j \<le> n; k \<le> n\<rbrakk>  \<Longrightarrow> \<delta>\<^bsub>j k\<^esub> \<in> Z\<^sub>\<infinity>"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<lbrakk>j \<le> n; k \<le> n\<rbrakk> \<Longrightarrow> \<delta>\<^bsub>j k\<^esub> \<in> Z\<^sub>\<infinity>
[PROOF STEP]
apply (simp add:Kronecker_delta_def)
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<lbrakk>j \<le> n; k \<le> n\<rbrakk> \<Longrightarrow> (j = k \<longrightarrow> 1 \<in> Z\<^sub>\<infinity>) \<and> (j \<noteq> k \<longrightarrow> 0 \<in> Z\<^sub>\<infinity>)
[PROOF STEP]
apply (simp add:z_in_aug_inf Zero_in_aug_inf)
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<lbrakk>j \<le> n; k \<le> n\<rbrakk> \<Longrightarrow> j = k \<longrightarrow> 1 \<in> Z\<^sub>\<infinity>
[PROOF STEP]
apply (rule impI)
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<lbrakk>j \<le> n; k \<le> n; j = k\<rbrakk> \<Longrightarrow> 1 \<in> Z\<^sub>\<infinity>
[PROOF STEP]
apply (simp only:ant_1[THEN sym], simp del:ant_1 add:z_in_aug_inf)
[PROOF STATE]
proof (prove)
goal:
No subgoals!
[PROOF STEP]
done


[STATEMENT]
lemma markedElementsToArePrefixOfMarkedElements:
  shows "isPrefix (markedElementsTo e M) (markedElements M)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. isPrefix (markedElementsTo e M) (markedElements M)
[PROOF STEP]
unfolding isPrefix_def
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<exists>s. markedElementsTo e M @ s = markedElements M
[PROOF STEP]
unfolding markedElementsTo_def
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<exists>s. markedElements (prefixToElement e M) @ s = markedElements M
[PROOF STEP]
by (induct M) auto


[STATEMENT]
lemma inj_on_eq_image': "\<lbrakk> inj_on f A; X\<subseteq>A; Y\<subseteq>A; f`X\<subseteq>f`Y \<rbrakk> \<Longrightarrow> X\<subseteq>Y"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<lbrakk>inj_on f A; X \<subseteq> A; Y \<subseteq> A; f ` X \<subseteq> f ` Y\<rbrakk> \<Longrightarrow> X \<subseteq> Y
[PROOF STEP]
unfolding inj_on_def
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<lbrakk>\<forall>x\<in>A. \<forall>y\<in>A. f x = f y \<longrightarrow> x = y; X \<subseteq> A; Y \<subseteq> A; f ` X \<subseteq> f ` Y\<rbrakk> \<Longrightarrow> X \<subseteq> Y
[PROOF STEP]
by fast


[STATEMENT]
lemma linf_norm_vec_greater_0 [simp]:
  fixes v :: "'a :: ordered_ab_group_add_abs vec"
  assumes "v \<in> carrier_vec n"
  shows "\<parallel>v\<parallel>\<^sub>\<infinity> > 0 \<longleftrightarrow> v \<noteq> 0\<^sub>v n"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. ((0::'a) < \<parallel>v\<parallel>\<^sub>\<infinity>) = (v \<noteq> 0\<^sub>v n)
[PROOF STEP]
by (insert assms, induct rule: carrier_vec_induct, auto simp: zero_vec_Suc max_def)


[STATEMENT]
lemma finite_multisets_of_size:
  assumes "finite A"
  shows   "finite {X. set_mset X \<subseteq> A \<and> size X = n}"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. finite {X. set_mset X \<subseteq> A \<and> size X = n}
[PROOF STEP]
proof (rule finite_subset)
[PROOF STATE]
proof (state)
goal (2 subgoals):
 1. {X. set_mset X \<subseteq> A \<and> size X = n} \<subseteq> ?B
 2. finite ?B
[PROOF STEP]
show "{X. set_mset X \<subseteq> A \<and> size X = n} \<subseteq> mset ` {xs. set xs \<subseteq> A \<and> length xs = n}"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. {X. set_mset X \<subseteq> A \<and> size X = n} \<subseteq> mset ` {xs. set xs \<subseteq> A \<and> length xs = n}
[PROOF STEP]
proof
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. \<And>x. x \<in> {X. set_mset X \<subseteq> A \<and> size X = n} \<Longrightarrow> x \<in> mset ` {xs. set xs \<subseteq> A \<and> length xs = n}
[PROOF STEP]
fix X
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. \<And>x. x \<in> {X. set_mset X \<subseteq> A \<and> size X = n} \<Longrightarrow> x \<in> mset ` {xs. set xs \<subseteq> A \<and> length xs = n}
[PROOF STEP]
assume X: "X \<in> {X. set_mset X \<subseteq> A \<and> size X = n}"
[PROOF STATE]
proof (state)
this:
X \<in> {X. set_mset X \<subseteq> A \<and> size X = n}

goal (1 subgoal):
 1. \<And>x. x \<in> {X. set_mset X \<subseteq> A \<and> size X = n} \<Longrightarrow> x \<in> mset ` {xs. set xs \<subseteq> A \<and> length xs = n}
[PROOF STEP]
obtain xs where [simp]: "X = mset xs"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. (\<And>xs. X = mset xs \<Longrightarrow> thesis) \<Longrightarrow> thesis
[PROOF STEP]
by (metis ex_mset)
[PROOF STATE]
proof (state)
this:
X = mset xs

goal (1 subgoal):
 1. \<And>x. x \<in> {X. set_mset X \<subseteq> A \<and> size X = n} \<Longrightarrow> x \<in> mset ` {xs. set xs \<subseteq> A \<and> length xs = n}
[PROOF STEP]
thus "X \<in> mset ` {xs. set xs \<subseteq> A \<and> length xs = n}"
[PROOF STATE]
proof (prove)
using this:
X = mset xs

goal (1 subgoal):
 1. X \<in> mset ` {xs. set xs \<subseteq> A \<and> length xs = n}
[PROOF STEP]
using X
[PROOF STATE]
proof (prove)
using this:
X = mset xs
X \<in> {X. set_mset X \<subseteq> A \<and> size X = n}

goal (1 subgoal):
 1. X \<in> mset ` {xs. set xs \<subseteq> A \<and> length xs = n}
[PROOF STEP]
by auto
[PROOF STATE]
proof (state)
this:
X \<in> mset ` {xs. set xs \<subseteq> A \<and> length xs = n}

goal:
No subgoals!
[PROOF STEP]
qed
[PROOF STATE]
proof (state)
this:
{X. set_mset X \<subseteq> A \<and> size X = n} \<subseteq> mset ` {xs. set xs \<subseteq> A \<and> length xs = n}

goal (1 subgoal):
 1. finite (mset ` {xs. set xs \<subseteq> A \<and> length xs = n})
[PROOF STEP]
next
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. finite (mset ` {xs. set xs \<subseteq> A \<and> length xs = n})
[PROOF STEP]
show "finite (mset ` {xs. set xs \<subseteq> A \<and> length xs = n})"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. finite (mset ` {xs. set xs \<subseteq> A \<and> length xs = n})
[PROOF STEP]
by (intro finite_imageI finite_lists_length_eq assms)
[PROOF STATE]
proof (state)
this:
finite (mset ` {xs. set xs \<subseteq> A \<and> length xs = n})

goal:
No subgoals!
[PROOF STEP]
qed


[STATEMENT]
lemma Lcm_dvd_nat [simp]:
  fixes M :: "nat set"
  assumes "\<forall>m\<in>M. m dvd n"
  shows "Lcm M dvd n"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. Lcm M dvd n
[PROOF STEP]
proof (cases "n > 0")
[PROOF STATE]
proof (state)
goal (2 subgoals):
 1. 0 < n \<Longrightarrow> Lcm M dvd n
 2. \<not> 0 < n \<Longrightarrow> Lcm M dvd n
[PROOF STEP]
case False
[PROOF STATE]
proof (state)
this:
\<not> 0 < n

goal (2 subgoals):
 1. 0 < n \<Longrightarrow> Lcm M dvd n
 2. \<not> 0 < n \<Longrightarrow> Lcm M dvd n
[PROOF STEP]
then
[PROOF STATE]
proof (chain)
picking this:
\<not> 0 < n
[PROOF STEP]
show ?thesis
[PROOF STATE]
proof (prove)
using this:
\<not> 0 < n

goal (1 subgoal):
 1. Lcm M dvd n
[PROOF STEP]
by simp
[PROOF STATE]
proof (state)
this:
Lcm M dvd n

goal (1 subgoal):
 1. 0 < n \<Longrightarrow> Lcm M dvd n
[PROOF STEP]
next
[PROOF STATE]
proof (state)
goal (1 subgoal):
 1. 0 < n \<Longrightarrow> Lcm M dvd n
[PROOF STEP]
case True
[PROOF STATE]
proof (state)
this:
0 < n

goal (1 subgoal):
 1. 0 < n \<Longrightarrow> Lcm M dvd n
[PROOF STEP]
then
[PROOF STATE]
proof (chain)
picking this:
0 < n
[PROOF STEP]
have "finite {d. d dvd n}"
[PROOF STATE]
proof (prove)
using this:
0 < n

goal (1 subgoal):
 1. finite {d. d dvd n}
[PROOF STEP]
by (rule finite_divisors_nat)
[PROOF STATE]
proof (state)
this:
finite {d. d dvd n}

goal (1 subgoal):
 1. 0 < n \<Longrightarrow> Lcm M dvd n
[PROOF STEP]
moreover
[PROOF STATE]
proof (state)
this:
finite {d. d dvd n}

goal (1 subgoal):
 1. 0 < n \<Longrightarrow> Lcm M dvd n
[PROOF STEP]
have "M \<subseteq> {d. d dvd n}"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. M \<subseteq> {d. d dvd n}
[PROOF STEP]
using assms
[PROOF STATE]
proof (prove)
using this:
\<forall>m\<in>M. m dvd n

goal (1 subgoal):
 1. M \<subseteq> {d. d dvd n}
[PROOF STEP]
by fast
[PROOF STATE]
proof (state)
this:
M \<subseteq> {d. d dvd n}

goal (1 subgoal):
 1. 0 < n \<Longrightarrow> Lcm M dvd n
[PROOF STEP]
ultimately
[PROOF STATE]
proof (chain)
picking this:
finite {d. d dvd n}
M \<subseteq> {d. d dvd n}
[PROOF STEP]
have "finite M"
[PROOF STATE]
proof (prove)
using this:
finite {d. d dvd n}
M \<subseteq> {d. d dvd n}

goal (1 subgoal):
 1. finite M
[PROOF STEP]
by (rule rev_finite_subset)
[PROOF STATE]
proof (state)
this:
finite M

goal (1 subgoal):
 1. 0 < n \<Longrightarrow> Lcm M dvd n
[PROOF STEP]
then
[PROOF STATE]
proof (chain)
picking this:
finite M
[PROOF STEP]
show ?thesis
[PROOF STATE]
proof (prove)
using this:
finite M

goal (1 subgoal):
 1. Lcm M dvd n
[PROOF STEP]
using assms
[PROOF STATE]
proof (prove)
using this:
finite M
\<forall>m\<in>M. m dvd n

goal (1 subgoal):
 1. Lcm M dvd n
[PROOF STEP]
by (induct M) (simp_all add: Lcm_nat_empty Lcm_nat_insert)
[PROOF STATE]
proof (state)
this:
Lcm M dvd n

goal:
No subgoals!
[PROOF STEP]
qed


[STATEMENT]
lemma ran_dom_the_eq : "(\<lambda>k . the (m k)) ` dom m = ran m"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. (\<lambda>k. the (m k)) ` dom m = ran m
[PROOF STEP]
unfolding ran_def dom_def
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. (\<lambda>k. the (m k)) ` {a. m a \<noteq> None} = {b. \<exists>a. m a = Some b}
[PROOF STEP]
by force


[STATEMENT]
lemma tendsto_within_open: "a \<in> S \<Longrightarrow> open S \<Longrightarrow> (f \<longlongrightarrow> l) (at a within S) \<longleftrightarrow> (f \<midarrow>a\<rightarrow> l)"
[PROOF STATE]
proof (prove)
goal (1 subgoal):
 1. \<lbrakk>a \<in> S; open S\<rbrakk> \<Longrightarrow> (f \<longlongrightarrow> l) (at a within S) = f \<midarrow>a\<rightarrow> l
[PROOF STEP]
by (simp add: tendsto_def at_within_open[where S = S])