theory Example
imports Link_Algebra
begin

text{*We prove that a link diagram with a single crossing is equivalent to the
unknot*}

lemma prelim_cup_compress:
 " ((basic (cup#[])) \<circ> (basic (vert # vert # []))) ~
      ((basic [])\<circ>(basic (cup#[])))"
 proof-
 have "domain_wall (basic (cup # [])) = 0"
           by auto
 moreover have "codomain_wall (basic (cup # [])) = 2"
          by auto
 moreover
     have "make_vert_block (nat (codomain_wall (basic (cup # []))))
                                    = (vert # vert # [])"
          unfolding make_vert_block_def
           by (metis (mono_tags) calculation(2) nat_2 nat_rec_0 nat_rec_Suc)
 moreover have "is_tangle_diagram   ((basic (cup#[])) \<circ> (basic (vert # vert # [])))"
          using is_tangle_diagram.simps by auto
 ultimately
  have "compress_bottom
          ((basic (cup#[])) \<circ> (basic (vert # vert # [])))
          ((basic []) \<circ>(basic (cup#[])))"
           using compress_bottom_def by (metis is_tangle_diagram.simps(1))
 then have "compress  ((basic (cup#[])) \<circ> (basic (vert # vert # [])))
      ((basic [])\<circ>(basic (cup#[])))"
          using compress_def by auto
 then have "linkrel ((basic (cup#[])) \<circ> (basic (vert # vert # [])))
      ((basic [])\<circ>(basic (cup#[])))"
          unfolding linkrel_def by auto
 then show ?thesis
      using Tangle_Equivalence.equality compress_bottom_def
      Tangle_Moves.compress_bottom_def Tangle_Moves.compress_def
      Tangle_Moves.linkrel_def
      by auto
 qed

lemma cup_compress:
 "(basic (cup#[])) \<circ> (basic (vert # vert # [])) ~ (basic (cup#[]))"
 proof-
 have " ((basic (cup#[])) \<circ> (basic (vert # vert # []))) ~
      ((basic [])\<circ>(basic (cup#[])))"
         using prelim_cup_compress  by auto
 moreover have "((basic [])\<circ>(basic (cup#[]))) ~  (basic (cup#[]))"
         using domain_compose refl sym Tangle_Equivalence.domain_compose
         Tangle_Equivalence.sym domain.simps(2) domain_block.simps
         domain_wall.simps(1)
         is_tangle_diagram.simps(1) monoid_add_class.add.right_neutral
         by auto
 ultimately show ?thesis using trans by (metis Example.transitive)
 qed

abbreviation x::"wall"
where
"x \<equiv>   (basic [cup,cup])\<circ>(basic [vert,over,vert]) \<circ> (basic [cap,cap])"

abbreviation y::"wall"
where
"y \<equiv>    (basic [cup]) \<circ> (basic [cap])"

lemma uncross_straighten_left_over:"left_over ~ straight_line"
proof-
 have "uncross right_over left_over"
        using uncross_positive_flip_def uncross_def by auto
 then have "linkrel right_over left_over"
    using linkrel_def by auto
 then have "right_over ~ left_over"
    using Tangle_Equivalence.equality by auto
 then have 1:"left_over ~ right_over"
    using Tangle_Equivalence.sym by auto
  have "uncross right_over straight_line"
        using uncross_positive_straighten_def uncross_def by auto
 then have "linkrel right_over straight_line"
    using linkrel_def by auto
 then have 2:"right_over ~ straight_line"
    using Tangle_Equivalence.equality by auto
  have "(left_over ~  straight_line) \<and> (right_over ~ straight_line)
         \<Longrightarrow> ?thesis"
            using transitive by auto
 then show ?thesis using 1 2 transitive  by blast
 qed

theorem Example:
  "x ~ y"
proof-
 have 1:"left_over ~ straight_line"
    using Tangle_Equivalence.equality uncross_straighten_left_over by auto
moreover have 2:"straight_line ~ straight_line"
   using refl by auto
have 3:"(left_over \<otimes> straight_line) ~ (straight_line \<otimes> straight_line)"
 proof-
  have "is_tangle_diagram (left_over)"
    unfolding is_tangle_diagram_def by auto
  moreover have "is_tangle_diagram (straight_line)"
    unfolding is_tangle_diagram_def by auto
  ultimately show ?thesis using 1 2 by (metis Tangle_Equivalence.tensor_eq)
 qed
then have 4:
  "((basic (cup#[])) \<circ> (left_over \<otimes> straight_line))
           ~   ((basic (cup#[])) \<circ> (straight_line \<otimes> straight_line))"
  proof-
  have "is_tangle_diagram (left_over \<otimes> straight_line)"
        by auto
  moreover have "is_tangle_diagram (straight_line \<otimes> straight_line)"
        by auto
  moreover have "is_tangle_diagram (basic (cup#[]))"
         by auto
  moreover have "domain_wall (left_over \<otimes> straight_line) = (codomain_wall (basic (cup#[])))"
        unfolding domain_wall_def by auto
  moreover have "domain_wall (straight_line \<otimes> straight_line) = (codomain_wall (basic (cup#[])))"
        unfolding domain_wall_def by auto
  moreover have "(basic (cup#[])) ~ (basic (cup#[]))"
        using refl by auto
  ultimately show ?thesis
        using compose_eq 3  by (metis Tangle_Equivalence.compose_eq)
  qed

moreover have 5:"  (basic [cup])\<circ> (straight_line \<otimes> straight_line)
                 ~ (basic [cup])"
  proof-
   have 0:
   "(basic ([cup])) \<circ> (straight_line \<otimes> straight_line) = (basic [cup]) \<circ>(basic [vert,vert])
                                                         \<circ> (basic [vert,vert])\<circ>(basic [vert,vert])"
           by auto
   let ?x ="(basic (cup#[]))
   \<circ>(basic (vert#vert#[])) \<circ> (basic (vert#vert#[]))
   \<circ> (basic (vert#vert#[]))"
   let ?x1 = " (basic (vert#vert#[]))\<circ> (basic (vert#vert#[]))"
   have 1:"?x ~ ((basic (cup#[])) \<circ> ?x1)"
   proof-
    have "(basic (cup#[]))\<circ>(basic (vert # vert # [])) ~ (basic (cup#[]))"
         using cup_compress by auto
    moreover have "is_tangle_diagram  (basic (cup#[]))"
         using is_tangle_diagram_def by auto
    moreover have "is_tangle_diagram ((basic (cup#[]))\<circ>(basic (vert # vert # [])))"
         using is_tangle_diagram_def by auto
    moreover have "is_tangle_diagram (?x1)"
        by auto
    moreover have "?x1 ~ ?x1"
        using refl by auto
    moreover have
     "codomain_wall (basic (cup#[])) = domain_wall  (basic (vert#vert#[]))"
           by auto
    moreover have "(basic (cup#[])) ~ (basic (cup#[]))"
         using refl by auto
    ultimately show ?thesis
                 using compose_eq codomain_wall_compose compose_leftassociativity
                 converse_composition_of_tangle_diagrams domain_wall_compose
                 by (metis Tangle_Equivalence.compose_eq
                 is_tangle_diagram.simps(1))
    qed
   have 2: " ((basic (cup#[])) \<circ> ?x1) ~ (basic (cup#[]))"
    proof-
     have "
     ((basic (cup # []))\<circ>(basic (vert # vert # [])))\<circ>(basic (vert # vert # []))
          ~ ((basic(cup#[]))\<circ>(basic(vert#vert#[])))"
     proof-
      have "(basic (cup#[]))\<circ>(basic (vert # vert # [])) ~ (basic (cup#[]))"
         using cup_compress by auto
      moreover have "(basic(vert#vert#[])) ~ (basic(vert#vert#[]))"
         using refl by auto
      moreover have "is_tangle_diagram  (basic (cup#[]))"
         using is_tangle_diagram_def by auto
      moreover have "is_tangle_diagram ((basic (cup#[]))\<circ>(basic (vert # vert # [])))"
         using is_tangle_diagram_def by auto
      moreover have "is_tangle_diagram ((basic(vert#vert#[])))"
         by auto
      moreover have
         "codomain_wall ((basic (cup#[]))\<circ>  (basic(vert#vert#[])))
                       = domain_wall  (basic(vert#vert#[]))  "
         by auto
      moreover
         have "codomain_wall (basic (cup#[])) = domain_wall (basic(vert#vert#[]))"
         by auto
      ultimately show ?thesis
                 using compose_eq
                 by (metis Tangle_Equivalence.compose_eq)
      qed
    then have "((basic (cup#[])) \<circ> ?x1) ~
           ((basic(cup#[]))\<circ>(basic(vert#vert#[])))"
      by auto
    then show ?thesis using cup_compress trans
         by (metis (full_types) Example.transitive)
   qed
   from 0 1 2 show ?thesis using trans transp_def trans compose_Nil
          by (metis (hide_lams, no_types) Example.transitive)
   qed
 let ?y = "((basic ([])) \<circ> (basic (cup#[])))  "
 let ?temp = "(basic (vert#over#vert#[]))\<circ>(basic (cap#vert#vert#[])) "
 have 45:"(left_over \<otimes> straight_line) =
          ((basic (cup#vert#vert#[])) \<circ> ?temp)"
          using tensor.simps by (metis compose_Nil concatenates_Cons concatenates_Nil)
 then have 55:"(basic (cup#[])) \<circ> (left_over \<otimes> straight_line)
             =  (basic (cup#[])) \<circ>  (basic (cup#vert#vert#[])) \<circ> ?temp"
                 by auto
 then have
"(basic (cup#[])) \<circ> (basic (cup#vert#vert#[]))
   =  (basic (([]) \<otimes>(cup#[])))\<circ>(basic ((cup#[])\<otimes>(vert#vert#[])))"
         using concatenate.simps  by auto
 then have 6:
 "(basic (cup#[])) \<circ> (basic (cup#vert#vert#[]))
          = ((basic ([]))\<circ>(basic (cup#[])))
            \<otimes>((basic (cup#[])) \<circ>(basic (vert#vert#[])))"
         using tensor.simps by auto
 then have "((basic (cup#[])) \<circ>(basic (vert#vert#[])))
                   ~ (basic ([]))\<circ>(basic (cup#[]))"
         using prelim_cup_compress by auto
 moreover have "((basic ([]))\<circ>(basic (cup#[])))
                       ~ ((basic ([]))\<circ>(basic (cup#[])))"
         using refl by auto
 moreover have "is_tangle_diagram ((basic (cup#[])) \<circ>(basic (vert#vert#[])))"
                by auto
 moreover have "is_tangle_diagram ((basic ([]))\<circ>(basic (cup#[]))) "
                by auto
 ultimately have 7:"?y \<otimes> ((basic (cup#[])) \<circ>(basic (vert#vert#[])))~ ((?y) \<otimes> (?y))"
        using tensor_eq cup_compress Nil_right_tensor is_tangle_diagram.simps(1) refl
        by (metis Tangle_Equivalence.tensor_eq)
 then have " ((?y) \<otimes> (?y)) = (basic (([]) \<otimes> ([])))
                   \<circ> ((basic (cup#[])) \<otimes> (basic (cup#[])))"
                using tensor.simps(4)  by (metis compose_Nil)
 then have "  ((?y) \<otimes> (?y)) = (basic ([])) \<circ>((basic (cup#cup#[])))"
                  using tensor.simps(1) concatenate_def by auto
 then have "(?y) \<otimes> ((basic (cup#[])) \<circ>(basic (vert#vert#[])))
             ~ (basic ([])) \<circ>(basic (cup#cup#[]))"
              using 7 by auto
  moreover have "(basic ([]))\<circ>(basic (cup#cup#[]))~(basic (cup#cup#[]))"
               proof-
               have "domain_wall (basic (cup#cup#[])) = 0"
                    by auto
                then show ?thesis using domain_compose sym
                by (metis Tangle_Equivalence.domain_compose Tangle_Equivalence.sym is_tangle_diagram.simps(1))
                qed
  ultimately have "(?y) \<otimes> ((basic (cup#[])) \<circ>(basic (vert#vert#[])))
             ~  (basic (cup#cup#[]))"
                using trans  by (metis (full_types) Example.transitive)
  then have " (basic(cup#[]))\<circ>(basic(cup#vert#vert#[]))~(basic(cup#cup#[]))"
           by auto
  moreover have "?temp ~ ?temp"
           using refl by auto
  moreover  have "is_tangle_diagram ((basic(cup#[]))\<circ>(basic(cup#vert#vert#[])))"
           by auto
  moreover have "is_tangle_diagram (basic(cup#cup#[]))"
           by auto
  moreover have "is_tangle_diagram  (?temp)"
           by auto
  moreover have "codomain_wall  ((basic(cup#[]))\<circ>(basic(cup#vert#vert#[])))
                    = domain_wall ?temp"
             by auto
   moreover have "codomain_wall (basic(cup#cup#[])) = domain_wall ?temp"
             by auto
   ultimately have 8:" ((basic(cup#[]))\<circ>(basic(cup#vert#vert#[]))) \<circ>(?temp)
                       ~ (basic(cup#cup#[])) \<circ> (?temp)"
               using compose_eq by (metis Tangle_Equivalence.compose_eq)
    then have "((basic [cup,cup]) \<circ> (?temp))
                 ~ (basic [cup] \<circ> (left_over \<otimes> straight_line))"
              using 55 compose_leftassociativity sym wall.simps
              by (metis Tangle_Equivalence.sym compose_Nil)
   moreover have "(basic [cup]) \<circ> (left_over \<otimes> straight_line)
                    ~ (basic [cup]) \<circ> (straight_line \<otimes> straight_line)"
                  using 4 by auto
   ultimately have "((basic [cup,cup]) \<circ> (?temp))
                  ~ (basic [cup]) \<circ> (straight_line \<otimes> straight_line)"
    proof-
    have "((basic [cup,cup]) \<circ> (?temp))
                 ~ (basic [cup] \<circ> (left_over \<otimes> straight_line))"
              using 8 55 compose_leftassociativity sym wall.simps
              Tangle_Equivalence.sym compose_Nil
              by (metis)
    moreover have "(basic [cup]) \<circ> (left_over \<otimes> straight_line)
                    ~ (basic [cup]) \<circ> (straight_line \<otimes> straight_line)"
                  using 4 by auto
    moreover have "(((basic [cup,cup]) \<circ> (?temp))
                 ~ (basic [cup] \<circ> (left_over \<otimes> straight_line)))
  \<and> ((basic [cup]) \<circ> (left_over \<otimes> straight_line)
                    ~ (basic [cup]) \<circ> (straight_line \<otimes> straight_line))
 \<Longrightarrow> ?thesis"
         using Example.transitive by auto
   ultimately show ?thesis by auto
  qed
  then have "(basic ([cup,cup])) \<circ> (?temp)  ~ (basic (cup # []))"
            using trans transp_def 5 by (metis Example.transitive)
  moreover have "(basic (cap#[])) ~ (basic (cap#[]))"
            using refl by auto
  moreover have "is_tangle_diagram ((basic(cup#cup#[])) \<circ> (?temp))"
            by auto
  moreover have "is_tangle_diagram (basic (cup # []))"
           by auto
  moreover have "is_tangle_diagram (basic (cap # []))"
           by auto
  moreover have "codomain_wall ((basic(cup#cup#[])) \<circ> (?temp))
                   = domain_wall (basic (cap # []))"
           by auto
  moreover have "codomain_wall (basic(cup#[])) = domain_wall (basic (cap # []))"
            by auto
 ultimately have 9:"((basic(cup#cup#[])) \<circ> (?temp)) \<circ> (basic (cap#[]))
                     ~ (basic (cup#[])) \<circ> (basic (cap#[]))"
              using Tangle_Equivalence.compose_eq by metis
  let ?z = "((basic(cup#cup#[])) \<circ> (basic(vert#over#vert#[])))"
  have 10:"((basic(cup#cup#[])) \<circ> (?temp)) \<circ> (basic (cap#[]))
              = ?z \<circ> ((basic(cap#vert#vert#[])) \<circ> (basic (cap#[])))"
      by auto
  then have 11:"((basic(cap#vert#vert#[])) \<circ> (basic (cap#[])))
 = ((basic ((cap#[])\<otimes>(vert#vert#[])))\<circ>(basic (([]) \<otimes>(cap#[]))))"
      unfolding concatenate_def by auto
  then have 12:" ((basic(cap#vert#vert#[])) \<circ> (basic (cap#[])))
 = ((basic (cap#[]))\<circ>(basic ([])))
\<otimes>((basic (vert#vert#[]))\<circ>(basic (cap#[])))"
  using tensor.simps by auto
 let ?w = "((basic (cap#[]))\<circ>(basic ([])))"
 have 13:"((basic (vert#vert#[]))\<circ>(basic (cap#[]))) ~ ?w"
      proof-
      have "codomain_wall (basic (cap#[])) = 0"
       by auto
      moreover have "domain_wall (basic (cap#[])) = 2" by auto
      moreover then have "(vert#vert#[])
                          = make_vert_block (nat (domain_wall (basic (cap#[]))))"
               using make_vert_block_def domain_wall_def by (metis Suc_eq_plus1_left
               make_vert_block.simps(1) make_vert_block.simps(2) nat_1 nat_numeral one_add_one
               transfer_nat_int_numerals(2))
     ultimately have "compress_top  ((basic (vert#vert#[]))\<circ>(basic (cap#[]))) ?w"
               using compress_top_def by auto
     then have "compress ((basic (vert#vert#[]))\<circ>(basic (cap#[]))) ?w"
               using compress_def by auto
     then have "linkrel  ((basic (vert#vert#[]))\<circ>(basic (cap#[]))) ?w"
               using linkrel_def by auto
     then have " ((basic (vert#vert#[]))\<circ>(basic (cap#[]))) ~ ?w"
               using Tangle_Equivalence.equality by auto
     then show ?thesis by simp
     qed
 moreover have "is_tangle_diagram ((basic (vert#vert#[]))\<circ>(basic (cap#[])))"
       by auto
 moreover have "is_tangle_diagram ?w"
      by auto
 moreover have "?w ~ ?w"
      using refl by auto
 ultimately have 14:"(?w) \<otimes> ((basic (vert#vert#[]))\<circ>(basic (cap#[]))) ~ ((?w)\<otimes> (?w))"
     using Tangle_Equivalence.tensor_eq by metis
 then have "((basic(cap#vert#vert#[])) \<circ> (basic (cap#[]))) ~ ((?w)\<otimes> (?w))"
     using 13 by auto
 moreover have " ((?w)\<otimes> (?w)) = (basic (cap#cap#[])) \<circ> (basic ([]))"
          using tensor.simps by auto
 ultimately have "((basic(cap#vert#vert#[])) \<circ> (basic (cap#[]))) ~
                           (basic (cap#cap#[])) \<circ> (basic ([]))"
               by auto
 moreover have "?z ~ ?z"
           using refl by auto
 moreover have "domain_wall ((basic(cap#cap#[])) \<circ> (basic ([])))
                                = codomain_wall (?z)"
               by auto
 moreover have "domain_wall (((basic(cap#vert#vert#[])) \<circ> (basic (cap#[]))))
                                = codomain_wall (?z)"
              by auto
 moreover have "is_tangle_diagram ((basic(cap#vert#vert#[])) \<circ> (basic (cap#[])))"
              by auto
 moreover have "is_tangle_diagram (?z)"
              by auto
 moreover have "is_tangle_diagram  ((basic(cap#cap#[])) \<circ> (basic ([])))"
             by auto
 ultimately have 14:" (?z) \<circ>  ((basic(cap#vert#vert#[])) \<circ> (basic (cap#[])))
                      ~ (?z) \<circ> ((basic(cap#cap#[])) \<circ> (basic ([])))" (is "?aa ~ ?bb")
               using Tangle_Equivalence.compose_eq by metis
 moreover  have 15: "((?z) \<circ> ((basic(cap#cap#[]))) \<circ> (basic ([])))
                ~ ((?z) \<circ> (basic(cap#cap#[])))" (is "?bb ~ ?cc")
               using Tangle_Equivalence.codomain_compose  Tangle_Equivalence.sym
               `is_tangle_diagram (basic [cap, cap] \<circ> basic [])` codomain_wall_compose
               compose_leftassociativity converse_composition_of_tangle_diagrams
               domain_block.simps(1) domain_wall.simps(1)
               by (metis (hide_lams, mono_tags) Tangle_Equivalence.compose_eq Tangle_Equivalence.refl `codomain_wall (basic [cup, cup]) = domain_wall (basic [vert, over, vert] \<circ> basic [cap, vert, vert])` `domain_wall (basic [cap, cap] \<circ> basic []) = codomain_wall (basic [cup, cup] \<circ> basic [vert, over, vert])` comp_of_tangle_dgms domain_wall_compose is_tangle_diagram.simps(1))
 ultimately have "(?aa ~ ?bb)\<and> (?bb ~ ?cc) \<Longrightarrow>?aa ~ ?cc"
                  using transitive by auto
 then have 16:"?aa ~ ?cc"
            using 14 15 by auto
 then have 17:" ((basic (cup#[]))\<circ>(basic (cap#[])))~ ?aa"
        using 9 10 Tangle_Equivalence.trans  Tangle_Equivalence.sym
            by (metis (hide_lams, no_types))
   have "(((basic (cup#[]))\<circ>(basic (cap#[])))~ ?aa)\<and>(?aa ~ ?cc)
            \<Longrightarrow> ((basic (cup#[]))\<circ>(basic (cap#[])))~ ?cc"
          using transitive by auto
 then have "((basic (cup#[]))\<circ>(basic (cap#[])))~ ?cc"
            using 17 16 by auto
 then show ?thesis using Tangle_Equivalence.sym by auto
qed

end


theory CycleCutOff
imports Main
begin

inductive P :: "('a \<rightharpoonup> 'a) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> bool"
  for f :: "'a \<rightharpoonup> 'a"
  where PBase: "f x = None \<Longrightarrow> P f x xs"
     |  PStep: "f x = Some y \<Longrightarrow> P f y (x#xs) \<Longrightarrow> y \<notin> set (x#xs) \<Longrightarrow> P f x xs"

inductive P' :: "('a \<rightharpoonup> 'a) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> bool"
  for f :: "'a \<rightharpoonup> 'a"
  where P'Base: "f x = None \<Longrightarrow> P' f x xs"
     |  P'Step: "f x = Some y \<Longrightarrow> P' f y (x#xs) \<Longrightarrow> P' f x xs"

lemma "P f x xs \<Longrightarrow> P' f x xs"
  by (induction x xs rule: P.induct)
     (elim P'.intros)+

fun valid_stack where
   "valid_stack f y (x#xs) \<longleftrightarrow> f x = Some y \<and> valid_stack f x xs"
 | "valid_stack f x [] \<longleftrightarrow> True"

fun map_pow where
    "map_pow f 0 x = Some x"
  | "map_pow f (Suc n) x = Option.bind (f x) (map_pow f n) "

definition cycle where "cycle f x \<longleftrightarrow> (\<exists>n. map_pow f (Suc n) x = Some x)"

lemma map_pow_0[simp]: "map_pow f 0 = Some" by auto

lemma map_pow_Suc': "map_pow f (Suc n) x = Option.bind (map_pow f n x) f"
 by (induction f n x rule:map_pow.induct)
    (simp_all cong: Option.bind_cong )

(* Better with relations *)
definition fun_graph where "fun_graph f x y \<longleftrightarrow> f x = Some y"

fun valid_stack' where
   "valid_stack' f y (x#xs) \<longleftrightarrow> (fun_graph f)\<^sup>+\<^sup>+ x y \<and> valid_stack' f x xs"
 | "valid_stack' f x [] \<longleftrightarrow> True"

lemma bind_some: "Option.bind x f = Some y \<longleftrightarrow> (\<exists> y'. x = Some y' \<and> f y' = Some y)"
  by (cases x, auto)

lemma rel_to_map_pow: "(fun_graph f)\<^sup>+\<^sup>+ x y \<Longrightarrow> \<exists> n. map_pow f (Suc n) x = Some y"
proof (induction rule:converse_tranclp_induct)
case (base x)
  hence "f x = Some y" unfolding fun_graph_def.
  hence "map_pow f (Suc 0) x = Some y" by simp
  thus ?case..
next
case (step x y')
  from this(3)
  obtain n where "map_pow f (Suc n) y' = Some y" by blast
  moreover
  from `fun_graph f x y'`
  have "f x = Some y'" unfolding fun_graph_def.
  ultimately
  have "map_pow f (Suc (Suc n)) x = Some y" by simp
  thus ?case..
qed

lemma map_pow_to_rel: "map_pow f (Suc n) x = Some y \<Longrightarrow> (fun_graph f)\<^sup>+\<^sup>+ x y"
proof (induction f "Suc n" x arbitrary: n rule:map_pow.induct)
case (2 f n x)

  from this(2)
  have "f x \<noteq> None" by (clarsimp simp add: bind_some)
  then obtain y' where "f x = Some y'" by auto
  hence "(fun_graph f) x y'" by (simp add: fun_graph_def)
  hence "(fun_graph f)\<^sup>+\<^sup>+ x y'" ..

  show ?case
  proof (cases "n")
  case 0
    with 2(2) `f x = Some y'`
    have "y' = y" by (auto)
    thus ?thesis using `(fun_graph f)\<^sup>+\<^sup>+ x y'` by simp
  next
  case (Suc n')
    note `(fun_graph f)\<^sup>+\<^sup>+ x y'`
    also
    from `f x = Some y'` 2(2) Suc
    have "map_pow f (Suc n') y' = Some y" by simp
    with `f x = Some y'` Suc
    have "(fun_graph f)\<^sup>+\<^sup>+ y' y"
      by (rule "2.hyps")
    finally
    show ?thesis.
  qed
qed

lemma cycle_rel_def: "cycle f x \<longleftrightarrow> (fun_graph f)\<^sup>+\<^sup>+ x x"
  unfolding cycle_def
  by (auto intro: map_pow_to_rel dest!: rel_to_map_pow)

(* Hard to prove with the relation. *)
lemma image_also_cycle:
  assumes "f x = Some y"
  assumes "cycle f x"
  shows "cycle f y"
proof-
  from `cycle f x` obtain n where "map_pow f (Suc n) x = Some x" unfolding cycle_def ..
  hence "map_pow f n y = Some x" using `f x = Some y` by simp
  hence "map_pow f (Suc n) y = Some y"  using `f x = Some y` by (simp add: map_pow_Suc' del:map_pow.simps)
  thus "cycle f y" unfolding cycle_def ..
qed

lemma rel_also_cycle:
  assumes "(fun_graph f)\<^sup>+\<^sup>+ x y"
  assumes "cycle f x"
  shows "cycle f y"
using assms
by (induction)  (auto intro: image_also_cycle simp add: fun_graph_def)

lemma fun_graphI[intro!]: "f x = Some y \<Longrightarrow> fun_graph f x y"
  unfolding fun_graph_def.

lemma valid_stack_rel:
  "valid_stack f y xs \<Longrightarrow> x \<in> set xs \<Longrightarrow> (fun_graph f)\<^sup>+\<^sup>+ x y"
  by (induction f y xs rule:valid_stack.induct)
     (auto 0 3 elim: tranclp_trans)

lemma valid_stack'_rel:
  "valid_stack' f y xs \<Longrightarrow> x \<in> set xs \<Longrightarrow> (fun_graph f)\<^sup>+\<^sup>+ x y"
  by (induction f y xs rule:valid_stack'.induct)
     (auto 0 3 elim: tranclp_trans)

lemma
  assumes "P' f x xs"
  assumes "valid_stack' f x xs"
  shows "\<not> cycle f x" and "P f x xs"
using assms
proof (induction rule: P'.induct)
  case P'Base
    case 1 from P'Base show ?case by simp
    case 2 from P'Base show ?case by (rule PBase)
next
  case (P'Step x y xs)
    from `f x = Some y`
    have "fun_graph f x y" by rule
    hence "(fun_graph f)\<^sup>+\<^sup>+ x y" by rule
    moreover
    assume "valid_stack' f x xs"
    ultimately
    have "valid_stack' f y (x # xs)" by simp
    note P'Step.IH[OF this]

    from `f x = Some y ` `\<not> cycle f y`
    show "\<not> cycle f x" by (auto simp add: image_also_cycle)

    have "y \<notin> set (x # xs)"
    proof
      from `f x = Some y`
      have "fun_graph f x y" by rule
      also
      assume "y \<in> set (x#xs)"
      with `valid_stack' f y _`
      have "(fun_graph f)\<^sup>+\<^sup>+ y y"  by (rule valid_stack'_rel)
      hence "cycle f y" unfolding cycle_rel_def.
      with `\<not> cycle f y`
      show False..
    qed
    with `f x = Some y` `P f y (x # xs)`
    show "P f x xs" by (rule PStep)
qed


section \<open>Example usage of the ``sturm'' method\<close>
(* Author: Manuel Eberl <manuel@pruvisto.org> *)
theory Sturm_Ex
imports "../Sturm"
begin

text \<open>
  In this section, we give a variety of statements about real polynomials that can b
  proven by the \emph{sturm} method.
\<close>

lemma
 "\<forall>x::real. x^2 + 1 \<noteq> 0"
by sturm

lemma
  fixes x :: real
  shows "x^2 + 1 \<noteq> 0" by sturm

lemma "(x::real) > 1 \<Longrightarrow> x^3 > 1" by sturm

lemma "\<forall>x::real. x*x \<noteq> -1" by sturm

schematic_goal A:
"card {x::real. -0.010831 < x \<and> x < 0.010831 \<and>
    1/120*x^5 + 1/24*x^4 +1/6*x^3 - 49/16777216*x^2 - 17/2097152*x = 0}
  = ?n"
  by sturm

lemma "card {x::real. x^3 + x = 2*x^2 \<and> x^3 - 6*x^2 + 11*x = 6} = 1"
by sturm

schematic_goal "card {x::real. x^3 + x = 2*x^2 \<or> x^3 - 6*x^2 + 11*x = 6} = ?n" by sturm

lemma
  "card {x::real. -0.010831 < x \<and> x < 0.010831 \<and>
     poly [:0, -17/2097152, -49/16777216, 1/6, 1/24, 1/120:] x = 0} = 3"
  by sturm

lemma "\<forall>x::real. x*x \<noteq> 0 \<or> x*x - 1 \<noteq> 2*x" by sturm

lemma "(x::real)*x+1 \<noteq> 0 \<and> (x^2+1)*(x^2+2) \<noteq> 0" by sturm

text\<open>3 examples related to continued fraction approximants to exp: LCP\<close>
lemma fixes x::real
  shows "-7.29347719 \<le> x \<Longrightarrow> 0 < x^5 + 30*x^4 + 420*x^3 + 3360*x^2 + 15120*x + 30240"
by sturm

lemma fixes x::real
  shows "0 < x^6 + 42*x^5 + 840*x^4 + 10080*x^3 + 75600*x\<^sup>2 + 332640*x + 665280"
by sturm

schematic_goal "card {x::real. x^7 + 56*x^6 + 1512*x^5 + 25200*x^4 + 277200*x^3 + 1995840*x^2 + 8648640*x = -17297280} = ?n"
by sturm

end


(* Title:      Cross_Product_7.thy
   Author:     Angeliki Koutsoukou-Argyraki, University of Cambridge
   Date:       September 2018
*)
section\<open>Vector Cross Product in 7 Dimensions\<close>
theory Cross_Product_7
  imports "HOL-Analysis.Multivariate_Analysis"
begin

subsection\<open>Elementary auxiliary lemmas.\<close>

lemma exhaust_7:
  fixes x :: 7
  shows "x = 1 \<or> x = 2 \<or> x = 3 \<or> x = 4 \<or> x = 5 \<or> x = 6  \<or> x = 7 "
proof (induct x)
  case (of_int z)
  then have "0 \<le> z" and "z < 7" by simp_all
  then have "z = 0 \<or> z = 1 \<or> z = 2 \<or> z = 3 \<or> z = 4 \<or> z =5\<or> z = 6 \<or> z =7   " by arith
  then show ?case by auto
qed

lemma forall_7: "(\<forall>i::7. P i) \<longleftrightarrow> P 1 \<and> P 2 \<and> P 3\<and> P 4 \<and> P 5 \<and> P 6\<and> P 7  "
  by (metis exhaust_7)

lemma vector_7 [simp]:
 "(vector [x1,x2,x3,x4,x5,x6,x7] ::('a::zero)^7)$1 = x1"
 "(vector [x1,x2,x3,x4,x5,x6,x7] ::('a::zero)^7)$2 = x2"
 "(vector [x1,x2,x3,x4,x5,x6,x7] ::('a::zero)^7)$3 = x3"
 "(vector [x1,x2,x3,x4,x5,x6,x7] ::('a::zero)^7)$4 = x4"
 "(vector [x1,x2,x3,x4,x5,x6,x7] ::('a::zero)^7)$5 = x5"
 "(vector [x1,x2,x3,x4,x5,x6,x7] ::('a::zero)^7)$6 = x6"
 "(vector [x1,x2,x3,x4,x5,x6,x7] ::('a::zero)^7)$7 = x7"
  unfolding vector_def by auto

lemma forall_vector_7:
 "(\<forall>v::'a::zero^7. P v) \<longleftrightarrow> (\<forall>x1 x2 x3 x4 x5 x6 x7. P(vector[x1, x2, x3, x4, x5, x6, x7]))"
  apply auto
  apply (erule_tac x="v$1" in allE)
  apply (erule_tac x="v$2" in allE)
  apply (erule_tac x="v$3" in allE)
  apply (erule_tac x="v$4" in allE)
  apply (erule_tac x="v$5" in allE)
  apply (erule_tac x="v$6" in allE)
  apply (erule_tac x="v$7" in allE)
  apply (subgoal_tac "vector [v$1, v$2, v$3, v$4, v$5, v$6, v$7] = v")
  apply simp
  apply (vector vector_def)
  apply (simp add: forall_7)
  done

lemma UNIV_7: "UNIV = {1::7, 2::7, 3::7, 4::7, 5::7, 6::7, 7::7}"
  using exhaust_7 by auto

lemma sum_7: "sum f (UNIV::7 set) = f 1 + f 2 + f 3 + f 4 + f 5 + f 6 + f 7"
  unfolding UNIV_7 by (simp add: ac_simps)

lemma not_equal_vector7 :
  fixes x::"real^7" and  y::"real^7"
  assumes "x = vector[x1,x2,x3,x4,x5,x6,x7] " and "y= vector [y1,y2,y3,y4,y5,y6,y7]"
and  "x$1 \<noteq> y$1 \<or> x$2 \<noteq> y$2 \<or> x$3 \<noteq> y$3 \<or> x$4 \<noteq> y$4 \<or> x$5 \<noteq> y$5 \<or> x$6 \<noteq> y$6 \<or>  x$7 \<noteq> y$7    "
shows "x \<noteq> y"  using assms by blast

lemma equal_vector7:
  fixes  x::"real^7" and  y::"real^7"
 assumes "x = vector[x1,x2,x3,x4,x5,x6,x7] " and "y= vector [y1,y2,y3,y4,y5,y6,y7]"
and "x = y"
shows "x$1 = y$1 \<and> x$2 = y$2 \<and> x$3 = y$3 \<and> x$4 = y$4 \<and> x$5 = y$5 \<and> x$6 = y$6 \<and>  x$7 = y$7    "
  using assms by blast

lemma numeral_4_eq_4: "4 = Suc( Suc (Suc (Suc 0)))"
  by (simp add: eval_nat_numeral)
lemma numeral_5_eq_5: "5 = Suc(Suc( Suc (Suc (Suc 0))))"
  by (simp add: eval_nat_numeral)
lemma numeral_6_eq_6: "6 = Suc( Suc(Suc( Suc (Suc (Suc 0)))))"
  by (simp add: eval_nat_numeral)
lemma numeral_7_eq_7: "7 = Suc(Suc( Suc(Suc( Suc (Suc (Suc 0))))))"
  by (simp add: eval_nat_numeral)

subsection\<open>The definition of the 7D cross product and related lemmas\<close>

context includes no_Set_Product_syntax
begin (* locally disable syntax for set product, to avoid warnings *)

text \<open>Note: in total there exist 480 equivalent  multiplication tables for the definition,
 the following is based on the one most widely used: \<close>

definition cross7 :: "[real^7, real^7] \<Rightarrow> real^7"  (infixr "\<times>\<^sub>7" 80)
  where "a \<times>\<^sub>7 b \<equiv>
    vector [a$2 * b$4 - a$4 * b$2 +  a$3 * b$7 - a$7 * b$3  +  a$5 * b$6 - a$6 * b$5 ,
            a$3 * b$5 - a$5 * b$3 +  a$4 * b$1 - a$1 * b$4  +  a$6 * b$7 - a$7 * b$6 ,
            a$4 * b$6 - a$6 * b$4 +  a$5 * b$2 - a$2 * b$5  +  a$7 * b$1 - a$1 * b$7 ,
            a$5 * b$7 - a$7 * b$5 +  a$6 * b$3 - a$3 * b$6  +  a$1 * b$2 - a$2 * b$1 ,
            a$6 * b$1 - a$1 * b$6 +  a$7 * b$4 - a$4 * b$7  +  a$2 * b$3 - a$3 * b$2 ,
            a$7 * b$2 - a$2 * b$7 +  a$1 * b$5 - a$5 * b$1  +  a$3 * b$4 - a$4 * b$3 ,
            a$1 * b$3 - a$3 * b$1 +  a$2 * b$6 - a$6 * b$2  +  a$4 * b$5 - a$5 * b$4 ]"

end

bundle cross7_syntax begin
notation cross7 (infixr "\<times>\<^sub>7" 80)
no_notation Product_Type.Times (infixr "\<times>\<^sub>7" 80)
end

bundle no_cross7_syntax begin
no_notation cross7 (infixr "\<times>\<^sub>7" 80)
notation Product_Type.Times (infixr "\<times>\<^sub>7" 80)
end

unbundle cross7_syntax

lemmas cross7_simps = cross7_def inner_vec_def sum_7 det_def vec_eq_iff vector_def algebra_simps

lemma dot_cross7_self: "x \<bullet> (x \<times>\<^sub>7 y) = 0" "x \<bullet> (y \<times>\<^sub>7 x) = 0" "(x \<times>\<^sub>7 y) \<bullet> y = 0" "(y \<times>\<^sub>7 x) \<bullet> y = 0"
  by (simp_all add: orthogonal_def cross7_simps)

lemma orthogonal_cross7: "orthogonal (x \<times>\<^sub>7 y) x" "orthogonal (x \<times>\<^sub>7 y) y"
                         "orthogonal y (x\<times>\<^sub>7  y)" "orthogonal (x \<times>\<^sub>7 y) x"
  by (simp_all add: orthogonal_def dot_cross7_self)

lemma cross7_zero_left [simp]:  "0 \<times>\<^sub>7 x = 0"
  and cross7_zero_right [simp]: "x \<times>\<^sub>7 0 = 0"
  by (simp_all add: cross7_simps)

lemma cross7_skew: "(x \<times>\<^sub>7 y) = -(y \<times>\<^sub>7 x)"
  by (simp add: cross7_simps)

lemma cross7_refl [simp]: "x \<times>\<^sub>7 x = 0"
  by (simp add: cross7_simps)

lemma cross7_add_left: "(x + y) \<times>\<^sub>7 z = (x \<times>\<^sub>7 z) + (y \<times>\<^sub>7 z)"
  and   cross7_add_right: "x \<times>\<^sub>7 (y + z) = (x \<times>\<^sub>7 y) + (x \<times>\<^sub>7 z)"
  by (simp_all add: cross7_simps)

lemma cross7_mult_left: "(c *\<^sub>R x) \<times>\<^sub>7 y = c *\<^sub>R (x \<times>\<^sub>7 y)"
  and  cross7_mult_right: "x \<times>\<^sub>7 (c *\<^sub>R y) = c *\<^sub>R (x \<times>\<^sub>7 y)"
  by (simp_all add: cross7_simps)

lemma cross7_minus_left [simp]: "(-x) \<times>\<^sub>7 y = - (x \<times>\<^sub>7 y)"
and cross7_minus_right [simp]: "x \<times>\<^sub>7 -y = - (x \<times>\<^sub>7 y)"
  by (simp_all add: cross7_simps)

lemma left_diff_distrib: "(x - y) \<times>\<^sub>7 z = x \<times>\<^sub>7 z - y \<times>\<^sub>7 z"
  and  right_diff_distrib: "x \<times>\<^sub>7 (y - z) = x \<times>\<^sub>7 y - x \<times>\<^sub>7 z"
  by (simp_all add: cross7_simps)

hide_fact (open) left_diff_distrib right_diff_distrib

lemma cross7_triple1: "(x \<times>\<^sub>7 y) \<bullet> z = (y \<times>\<^sub>7 z) \<bullet> x"
and  cross7_triple2: "(x \<times>\<^sub>7 y) \<bullet> z = x \<bullet> (y \<times>\<^sub>7 z)  "
  by (simp_all add: cross7_def inner_vec_def sum_7 vec_eq_iff algebra_simps)

lemma cross7_components:
  "(x \<times>\<^sub>7 y)$1 = x$2 * y$4 - x$4 * y$2 + x$3 * y$7 -  x$7 * y$3 +  x$5 * y$6 -  x$6 * y$5  "
  "(x \<times>\<^sub>7 y)$2 = x$4 * y$1 - x$1 * y$4 + x$3 * y$5 -  x$5 * y$3 +  x$6 * y$7 -  x$7 * y$6  "
  "(x \<times>\<^sub>7 y)$3 = x$5 * y$2 - x$2 * y$5 + x$4 * y$6 -  x$6 * y$4 +  x$7 * y$1 -  x$1 * y$7  "
  "(x \<times>\<^sub>7 y)$4 = x$1 * y$2 - x$2 * y$1 + x$6 * y$3 -  x$3 * y$6 +  x$5 * y$7 -  x$7 * y$5  "
  "(x \<times>\<^sub>7 y)$5 = x$6 * y$1 - x$1 * y$6 + x$2 * y$3 -  x$3 * y$2 +  x$7 * y$4 -  x$4 * y$7  "
  "(x \<times>\<^sub>7 y)$6 = x$1 * y$5 - x$5 * y$1 + x$7 * y$2 -  x$2 * y$7 +  x$3 * y$4 -  x$4 * y$3  "
  "(x \<times>\<^sub>7 y)$7 = x$1 * y$3 - x$3 * y$1 + x$4 * y$5 -  x$5 * y$4 +  x$2 * y$6 -  x$6 * y$2  "
  by (simp_all add: cross7_def inner_vec_def sum_7 vec_eq_iff algebra_simps)

text \<open>Nonassociativity of the 7D cross product showed using a counterexample\<close>

lemma cross7_nonassociative:
   " \<not> (\<forall> (c::real^7) (a::real^7) ( b::real^7) .  c \<times>\<^sub>7  (a \<times>\<^sub>7 b) = (c \<times>\<^sub>7 a ) \<times>\<^sub>7 b) "
proof-
  have *: " \<exists> (c::real^7) (a::real^7) ( b::real^7) .  c \<times>\<^sub>7  (a \<times>\<^sub>7 b) \<noteq> (c \<times>\<^sub>7 a ) \<times>\<^sub>7 b "
proof-
  define f::"real^7" where "f = vector[ 0, 0, 0, 0, 0, 1, 1 ]"
  define g::"real^7" where "g = vector[ 0, 0, 0, 1, 0, 0, 0 ]"
  define h::"real^7" where "h = vector[ 1, 0, 1, 0, 0, 0, 0 ]"
  define u where " u= (g \<times>\<^sub>7 h) "
  define v where " v= (f \<times>\<^sub>7 g) "

  have 1:" u  = vector[0, 1, 0, 0, 0, -1, 0]"
    unfolding cross7_def g_def h_def f_def u_def by simp
  have 3:"  f \<times>\<^sub>7 u = vector[0, 1, 0, 0, 0, 1, -1 ] "
    unfolding cross7_def f_def  using 1 by simp

  have 2:" v  = vector[0, 0, -1, 0, 1, 0, 0]"
    unfolding cross7_def g_def h_def f_def v_def by simp
  have  4:"  v \<times>\<^sub>7 h = vector[0, -1, 0, 0, 0, -1, 1 ] "
    unfolding cross7_def h_def  using 2 by simp

  define x::"real^7" where "x= vector[0, 1, 0, 0, 0, 1, -1]  "
  define y::"real^7" where "y= vector[0, -1, 0, 0, 0,-1, 1]  "

    have *:  "x$2 =  1" unfolding x_def by simp
    have **: "y$2 = -1" unfolding y_def by simp

    have ***: "x \<noteq> y" using * ** by auto
    have 5: " f \<times>\<^sub>7 u \<noteq>  v \<times>\<^sub>7 h"
      unfolding x_def y_def
      using ***
      by (simp add: "3" "4" x_def y_def)

have  6:"  f \<times>\<^sub>7  (g \<times>\<^sub>7 h) \<noteq> (f \<times>\<^sub>7 g ) \<times>\<^sub>7 h " using 5  by (simp add: u_def v_def)

 show ?thesis unfolding f_def g_def h_def using 6 by blast
qed
show ?thesis using * by blast
qed

text \<open>The 7D cross product does not satisfy the Jacobi Identity(shown using a counterexample) \<close>

lemma cross7_not_Jacobi:
   " \<not> (\<forall> (c::real^7) (a::real^7) ( b::real^7) . (c \<times>\<^sub>7 a ) \<times>\<^sub>7 b +   (b \<times>\<^sub>7 c) \<times>\<^sub>7 a
+  (a \<times>\<^sub>7 b ) \<times>\<^sub>7 c =0     ) "

proof-
  have *: " \<exists> (c::real^7) (a::real^7) ( b::real^7) . (c \<times>\<^sub>7 a ) \<times>\<^sub>7 b +   (b \<times>\<^sub>7 c) \<times>\<^sub>7 a
+  (a \<times>\<^sub>7 b ) \<times>\<^sub>7 c \<noteq>0      "
  proof-

  define  f::"real^7"  where " f= vector[ 0, 0, 0, 0, 0, 1, 1 ] "
  define  g::"real^7"  where " g= vector[ 0, 0, 0, 1, 0, 0, 0 ] "
  define  h::"real^7" where " h= vector[ 1, 0, 1, 0, 0, 0, 0 ] "
  define u where " u= (g \<times>\<^sub>7 h) "
  define v where " v= (f \<times>\<^sub>7 g) "
  define w where " w = (h \<times>\<^sub>7 f)  "

  have 1:" u  = vector[0, 1, 0, 0, 0, -1, 0]"
    unfolding cross7_def g_def h_def f_def u_def by simp
  have 3:" u \<times>\<^sub>7 f = vector[0,- 1, 0, 0, 0,- 1, 1 ] "
    unfolding cross7_def f_def  using 1 by simp

  have 2:" v  = vector[0, 0, -1, 0, 1, 0, 0]"
    unfolding cross7_def g_def h_def f_def v_def by simp
  have  4:"  v \<times>\<^sub>7 h = vector[0, -1, 0, 0, 0, -1, 1 ] "
    unfolding cross7_def h_def  using 2 by simp
  have 8: " w  = vector[1, 0, -1, -1, -1, 0, 0]"
    unfolding cross7_def  h_def f_def w_def by simp
  have 9: "  w \<times>\<^sub>7 g  = vector[0, -1, 0, 0, 0, -1, 1]"
    unfolding cross7_def  h_def f_def w_def g_def apply simp done
  have &: "(u \<times>\<^sub>7 f)$2+( v \<times>\<^sub>7 h) $2+( w \<times>\<^sub>7 g) $2 =-3" using 3 4 9 by simp
  have &&: " u \<times>\<^sub>7 f + v \<times>\<^sub>7 h +  w \<times>\<^sub>7 g \<noteq> 0 " using &
    by (metis vector_add_component zero_index zero_neq_neg_numeral)

  show ?thesis  using && u_def v_def w_def by blast
  qed

  show ?thesis using * by blast
qed

text\<open>The vector triple product property fulfilled for the 3D cross product does not hold
for the 7D cross product. Shown below with a counterexample.  \<close>

lemma cross7_not_vectortriple:
  "\<not>(\<forall> (c::real^7) (a::real^7) ( b::real^7).( c \<times>\<^sub>7 a ) \<times>\<^sub>7 b = (c  \<bullet> b ) *\<^sub>R a -  (c  \<bullet> a )*\<^sub>R b)"
proof-
  have *: "\<exists>(c::real^7) (a::real^7) ( b::real^7). (c \<times>\<^sub>7 a) \<times>\<^sub>7 b \<noteq> (c \<bullet> b ) *\<^sub>R a -  (c \<bullet> a )*\<^sub>R b"
  proof-
    define g:: "real ^ 7" where "g = vector[0, 0, 0, 1, 0, 0, 0]"
    define h:: "real ^ 7" where "h = vector[1, 0, 1, 0, 0, 0, 0]"
    define f:: "real ^ 7" where "f = vector[0, 0, 0, 0, 0, 1, 1]"
    define u where "u = (g \<times>\<^sub>7 h)"
    have 1: "u = vector[0, 1, 0, 0, 0, -1, 0]"
      unfolding cross7_def g_def h_def f_def u_def by simp
    have 2:" u \<times>\<^sub>7 f = vector[0,- 1, 0, 0, 0,- 1, 1 ]"
      unfolding cross7_def f_def using 1 by simp
    have 3: "(g \<bullet> f) *\<^sub>R h = 0" unfolding g_def f_def  inner_vec_def
      by (simp add: sum_7)
    have 4: "(g \<bullet> h) *\<^sub>R f = 0" unfolding g_def h_def  inner_vec_def
      by (simp add: sum_7)
    have 5: "(g \<bullet> f) *\<^sub>R h -(g \<bullet> h) *\<^sub>R f = 0"
      using 3 4 by auto
    have 6: "u \<times>\<^sub>7 f \<noteq> 0" using 2
      by (metis one_neq_zero vector_7(7) zero_index)

    have 7: "(g \<times>\<^sub>7 h) \<times>\<^sub>7 f \<noteq> 0" using 2 6 u_def by blast
    have 8: "(g \<times>\<^sub>7 h) \<times>\<^sub>7 f \<noteq> (g \<bullet> f) *\<^sub>R h - (g \<bullet> h ) *\<^sub>R f"
      using 5 7 by simp
    show ?thesis using 8 by auto
  qed
  show ?thesis using * by auto
qed

lemma axis_nth_neq [simp]: "i \<noteq> j \<Longrightarrow> axis i x $ j = 0"
  by (simp add: axis_def)

lemma cross7_basis:
  "(axis 1 1) \<times>\<^sub>7 (axis 2 1) = axis 4 1" "(axis 2 1) \<times>\<^sub>7 (axis 1 1) = -(axis 4 1)"
  "(axis 2 1) \<times>\<^sub>7 (axis 3 1) = axis 5 1" "(axis 3 1) \<times>\<^sub>7 (axis 2 1) = -(axis 5 1)"
  "(axis 3 1) \<times>\<^sub>7 (axis 4 1) = axis 6 1" "(axis 4 1) \<times>\<^sub>7 (axis 3 1) = -(axis 6 1)"
  "(axis 2 1) \<times>\<^sub>7 (axis 4 1) = axis 1 1" "(axis 4 1) \<times>\<^sub>7 (axis 2 1) = -(axis 1 1)"
  "(axis 4 1) \<times>\<^sub>7 (axis 5 1) = axis 7 1" "(axis 5 1) \<times>\<^sub>7 (axis 4 1) = -(axis 7 1)"
  "(axis 3 1) \<times>\<^sub>7 (axis 5 1) = axis 2 1" "(axis 5 1) \<times>\<^sub>7 (axis 3 1) = -(axis 2 1)"
  "(axis 4 1) \<times>\<^sub>7 (axis 6 1) = axis 3 1" "(axis 6 1) \<times>\<^sub>7 (axis 4 1) = -(axis 3 1)"
  "(axis 5 1) \<times>\<^sub>7 (axis 7 1) = axis 4 1" "(axis 7 1) \<times>\<^sub>7 (axis 5 1) = -(axis 4 1)"
  "(axis 4 1) \<times>\<^sub>7 (axis 1 1) = axis 2 1" "(axis 1 1) \<times>\<^sub>7 (axis 4 1) = -(axis 2 1)"
  "(axis 5 1) \<times>\<^sub>7 (axis 2 1) = axis 3 1" "(axis 2 1) \<times>\<^sub>7 (axis 5 1) = -(axis 3 1)"
  "(axis 6 1) \<times>\<^sub>7 (axis 3 1) = axis 4 1" "(axis 3 1) \<times>\<^sub>7 (axis 6 1) = -(axis 4 1)"
  "(axis 7 1) \<times>\<^sub>7 (axis 4 1) = axis 5 1" "(axis 4 1) \<times>\<^sub>7 (axis 7 1) = -(axis 5 1)"
  "(axis 5 1) \<times>\<^sub>7 (axis 6 1) = axis 1 1" "(axis 6 1) \<times>\<^sub>7 (axis 5 1) = -(axis 1 1)"
  "(axis 6 1) \<times>\<^sub>7 (axis 7 1) = axis 2 1" "(axis 7 1) \<times>\<^sub>7 (axis 6 1) = -(axis 2 1)"
  "(axis 7 1) \<times>\<^sub>7 (axis 1 1) = axis 3 1" "(axis 1 1) \<times>\<^sub>7 (axis 7 1) = -(axis 3 1)"
  "(axis 6 1) \<times>\<^sub>7 (axis 1 1) = axis 5 1" "(axis 1 1) \<times>\<^sub>7 (axis 6 1) = -(axis 5 1)"
  "(axis 7 1) \<times>\<^sub>7 (axis 2 1) = axis 6 1" "(axis 2 1) \<times>\<^sub>7 (axis 7 1) = -(axis 6 1)"
  "(axis 1 1) \<times>\<^sub>7 (axis 3 1) = axis 7 1" "(axis 3 1) \<times>\<^sub>7 (axis 1 1) = -(axis 7 1)"
  "(axis 1 1) \<times>\<^sub>7 (axis 5 1) = axis 6 1" "(axis 5 1) \<times>\<^sub>7 (axis 1 1) = -(axis 6 1)"
  "(axis 2 1) \<times>\<^sub>7 (axis 6 1) = axis 7 1" "(axis 6 1) \<times>\<^sub>7 (axis 2 1) = -(axis 7 1)"
  "(axis 3 1) \<times>\<^sub>7 (axis 7 1) = axis 1 1" "(axis 7 1) \<times>\<^sub>7 (axis 3 1) = -(axis 1 1)"
  by (simp_all add: vec_eq_iff forall_7 cross7_components)

lemma cross7_basis_zero:
 "  u=0  \<Longrightarrow> (u \<times>\<^sub>7 axis 1 1 = 0) \<and> (u \<times>\<^sub>7 axis 2 1 = 0) \<and> (u \<times>\<^sub>7 axis 3 1 = 0)
 \<and> (u \<times>\<^sub>7 axis 4 1 = 0) \<and> (u \<times>\<^sub>7 axis 5 1 = 0 ) \<and> (u \<times>\<^sub>7 axis 6 1 = 0 )
 \<and> (u \<times>\<^sub>7 axis 7 1 = 0)  "
  by auto

lemma cross7_basis_nonzero:
"\<not> (u \<times>\<^sub>7 axis 1 1 = 0) \<or> \<not> (u \<times>\<^sub>7 axis 2 1 = 0) \<or> \<not> (u \<times>\<^sub>7 axis 3 1 = 0)
 \<or> \<not>   (u \<times>\<^sub>7 axis 4 1 = 0)  \<or> \<not>  (u \<times>\<^sub>7 axis 5 1 = 0 ) \<or> \<not>   (u \<times>\<^sub>7 axis 6 1 = 0 )
 \<or> \<not>   (u \<times>\<^sub>7 axis 7 1 = 0)  \<Longrightarrow> u \<noteq> 0"
  by auto

text\<open>Pythagorean theorem/magnitude\<close>

lemma norm_square_vec_eq: "norm x ^ 2 = (\<Sum>i\<in>UNIV. x $ i ^ 2)"
  by (auto simp: norm_vec_def L2_set_def intro!: sum_nonneg)

lemma norm_cross7_dot_magnitude: "(norm (x \<times>\<^sub>7 y))\<^sup>2 = (norm x)\<^sup>2 * (norm y)\<^sup>2 - (x \<bullet> y)\<^sup>2"
  unfolding norm_square_vec_eq sum_7 cross7_components inner_vec_def real_norm_def inner_real_def
  by algebra

lemma cross7_eq_0: "x \<times>\<^sub>7 y = 0 \<longleftrightarrow> collinear {0, x, y}"
proof -
  have "x \<times>\<^sub>7 y = 0 \<longleftrightarrow> norm (x \<times>\<^sub>7 y) = 0"
    by simp
  also have "... \<longleftrightarrow> (norm x * norm y)\<^sup>2 = (x \<bullet> y)\<^sup>2"
    using norm_cross7_dot_magnitude [of x y] by (auto simp: power_mult_distrib)
  also have "... \<longleftrightarrow> \<bar>x \<bullet> y\<bar> = norm x * norm y"
    using power2_eq_iff
    by (metis (mono_tags, hide_lams) abs_minus abs_norm_cancel
              abs_power2 norm_mult power_abs real_norm_def)
  also have "... \<longleftrightarrow> collinear {0, x, y}"
    by (rule norm_cauchy_schwarz_equal)
  finally show ?thesis .
qed

lemma cross7_eq_self: "x \<times>\<^sub>7 y = x \<longleftrightarrow> x = 0" "x \<times>\<^sub>7 y = y \<longleftrightarrow> y = 0"
  apply (metis cross7_zero_left dot_cross7_self(1) inner_eq_zero_iff)
  apply (metis cross7_zero_right dot_cross7_self(2) inner_eq_zero_iff)
  done

lemma norm_and_cross7_eq_0:
   "x \<bullet> y = 0 \<and> x \<times>\<^sub>7 y = 0 \<longleftrightarrow> x = 0 \<or> y = 0" (is "?lhs = ?rhs")
proof
  assume ?lhs
  then show ?rhs
    using cross7_eq_0 norm_cauchy_schwarz_equal by force
next
assume ?rhs
  then show ?lhs
    by auto
qed

lemma bilinear_cross7: "bilinear (\<times>\<^sub>7)"
  apply (auto simp add: bilinear_def linear_def)
  apply unfold_locales
  apply (simp_all add: cross7_add_right cross7_mult_right cross7_add_left cross7_mult_left)
  done

subsection\<open>Continuity\<close>

lemma continuous_cross7: "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x \<times>\<^sub>7 g x)"
  by (subst continuous_componentwise)(auto intro!: continuous_intros simp: cross7_simps)

lemma continuous_on_cross:
  fixes f :: "'a::t2_space \<Rightarrow> real^7"
  shows "\<lbrakk>continuous_on S f; continuous_on S g\<rbrakk> \<Longrightarrow> continuous_on S (\<lambda>x. f x \<times>\<^sub>7 g x)"
  by (simp add: continuous_on_eq_continuous_within continuous_cross7)

end


(* Author: Tobias Nipkow *)

theory Abs_Int1_parity_ITP
imports Abs_Int1_ITP
begin

subsection "Parity Analysis"

datatype parity = Even | Odd | Either

text{* Instantiation of class @{class preord} with type @{typ parity}: *}

instantiation parity :: preord
begin

text{* First the definition of the interface function @{text"\<sqsubseteq>"}. Note that
the header of the definition must refer to the ascii name @{const le} of the
constants as @{text le_parity} and the definition is named @{text
le_parity_def}.  Inside the definition the symbolic names can be used. *}

definition le_parity where
"x \<sqsubseteq> y = (y = Either \<or> x=y)"

text{* Now the instance proof, i.e.\ the proof that the definition fulfills
the axioms (assumptions) of the class. The initial proof-step generates the
necessary proof obligations. *}

instance
proof
  fix x::parity show "x \<sqsubseteq> x" by(auto simp: le_parity_def)
next
  fix x y z :: parity assume "x \<sqsubseteq> y" "y \<sqsubseteq> z" thus "x \<sqsubseteq> z"
    by(auto simp: le_parity_def)
qed

end

text{* Instantiation of class @{class SL_top} with type @{typ parity}: *}

instantiation parity :: SL_top
begin

definition join_parity where
"x \<squnion> y = (if x \<sqsubseteq> y then y else if y \<sqsubseteq> x then x else Either)"

definition Top_parity where
"\<top> = Either"

text{* Now the instance proof. This time we take a lazy shortcut: we do not
write out the proof obligations but use the @{text goali} primitive to refer
to the assumptions of subgoal i and @{text "case?"} to refer to the
conclusion of subgoal i. The class axioms are presented in the same order as
in the class definition. *}

instance
proof
  case goal1 (*join1*) show ?case by(auto simp: le_parity_def join_parity_def)
next
  case goal2 (*join2*) show ?case by(auto simp: le_parity_def join_parity_def)
next
  case goal3 (*join least*) thus ?case by(auto simp: le_parity_def join_parity_def)
next
  case goal4 (*Top*) show ?case by(auto simp: le_parity_def Top_parity_def)
qed

end

text{* Now we define the functions used for instantiating the abstract
interpretation locales. Note that the Isabelle terminology is
\emph{interpretation}, not \emph{instantiation} of locales, but we use
instantiation to avoid confusion with abstract interpretation.  *}

fun \<gamma>_parity :: "parity \<Rightarrow> val set" where
"\<gamma>_parity Even = {i. i mod 2 = 0}" |
"\<gamma>_parity Odd  = {i. i mod 2 = 1}" |
"\<gamma>_parity Either = UNIV"

fun num_parity :: "val \<Rightarrow> parity" where
"num_parity i = (if i mod 2 = 0 then Even else Odd)"

fun plus_parity :: "parity \<Rightarrow> parity \<Rightarrow> parity" where
"plus_parity Even Even = Even" |
"plus_parity Odd  Odd  = Even" |
"plus_parity Even Odd  = Odd" |
"plus_parity Odd  Even = Odd" |
"plus_parity Either y  = Either" |
"plus_parity x Either  = Either"

text{* First we instantiate the abstract value interface and prove that the
functions on type @{typ parity} have all the necessary properties: *}

interpretation Val_abs
where \<gamma> = \<gamma>_parity and num' = num_parity and plus' = plus_parity
proof txt{* of the locale axioms *}
  fix a b :: parity
  assume "a \<sqsubseteq> b" thus "\<gamma>_parity a \<subseteq> \<gamma>_parity b"
    by(auto simp: le_parity_def)
next txt{* The rest in the lazy, implicit way *}
  case goal2 show ?case by(auto simp: Top_parity_def)
next
  case goal3 show ?case by auto
next
  txt{* Warning: this subproof refers to the names @{text a1} and @{text a2}
  from the statement of the axiom. *}
  case goal4 thus ?case
  proof(cases a1 a2 rule: parity.exhaust[case_product parity.exhaust])
  qed (auto simp add:mod_add_eq)
qed

text{* Instantiating the abstract interpretation locale requires no more
proofs (they happened in the instatiation above) but delivers the
instantiated abstract interpreter which we call AI: *}

global_interpretation Abs_Int
where \<gamma> = \<gamma>_parity and num' = num_parity and plus' = plus_parity
defines aval_parity = aval' and step_parity = step' and AI_parity = AI
..

subsubsection "Tests"

definition "test1_parity =
  ''x'' ::= N 1;;
  WHILE Less (V ''x'') (N 100) DO ''x'' ::= Plus (V ''x'') (N 2)"

value "show_acom_opt (AI_parity test1_parity)"

definition "test2_parity =
  ''x'' ::= N 1;;
  WHILE Less (V ''x'') (N 100) DO ''x'' ::= Plus (V ''x'') (N 3)"

value "show_acom ((step_parity \<top> ^^1) (anno None test2_parity))"
value "show_acom ((step_parity \<top> ^^2) (anno None test2_parity))"
value "show_acom ((step_parity \<top> ^^3) (anno None test2_parity))"
value "show_acom ((step_parity \<top> ^^4) (anno None test2_parity))"
value "show_acom ((step_parity \<top> ^^5) (anno None test2_parity))"
value "show_acom_opt (AI_parity test2_parity)"

subsubsection "Termination"

global_interpretation Abs_Int_mono
where \<gamma> = \<gamma>_parity and num' = num_parity and plus' = plus_parity
proof
  case goal1 thus ?case
  proof(cases a1 a2 b1 b2
   rule: parity.exhaust[case_product parity.exhaust[case_product parity.exhaust[case_product parity.exhaust]]]) (* FIXME - UGLY! *)
  qed (auto simp add:le_parity_def)
qed

definition m_parity :: "parity \<Rightarrow> nat" where
"m_parity x = (if x=Either then 0 else 1)"

lemma measure_parity:
  "(strict{(x::parity,y). x \<sqsubseteq> y})^-1 \<subseteq> measure m_parity"
by(auto simp add: m_parity_def le_parity_def)

lemma measure_parity_eq:
  "\<forall>x y::parity. x \<sqsubseteq> y \<and> y \<sqsubseteq> x \<longrightarrow> m_parity x = m_parity y"
by(auto simp add: m_parity_def le_parity_def)

lemma AI_parity_Some: "\<exists>c'. AI_parity c = Some c'"
by(rule AI_Some_measure[OF measure_parity measure_parity_eq])

end


(*  Title:      HOL/Fields.thy
    Author:     Gertrud Bauer
    Author:     Steven Obua
    Author:     Tobias Nipkow
    Author:     Lawrence C Paulson
    Author:     Markus Wenzel
    Author:     Jeremy Avigad
*)

section {* Fields *}

theory Fields
imports Rings
begin

subsection {* Division rings *}

text {*
  A division ring is like a field, but without the commutativity requirement.
*}

class inverse =
  fixes inverse :: "'a \<Rightarrow> 'a"
    and divide :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "'/" 70)

setup {* Sign.add_const_constraint (@{const_name "divide"}, SOME @{typ "'a \<Rightarrow> 'a \<Rightarrow> 'a"}) *}

context semiring
begin

lemma [field_simps]:
  shows distrib_left_NO_MATCH: "NO_MATCH a (x / y) \<Longrightarrow> a * (b + c) = a * b + a * c"
    and distrib_right_NO_MATCH: "NO_MATCH c (x / y) \<Longrightarrow> (a + b) * c = a * c + b * c"
  by (rule distrib_left distrib_right)+

end

context ring
begin

lemma [field_simps]:
  shows left_diff_distrib_NO_MATCH: "NO_MATCH c (x / y) \<Longrightarrow> (a - b) * c = a * c - b * c"
    and right_diff_distrib_NO_MATCH: "NO_MATCH a (x / y) \<Longrightarrow> a * (b - c) = a * b - a * c"
  by (rule left_diff_distrib right_diff_distrib)+

end

setup {* Sign.add_const_constraint (@{const_name "divide"}, SOME @{typ "'a::inverse \<Rightarrow> 'a \<Rightarrow> 'a"}) *}

text{* Lemmas @{text divide_simps} move division to the outside and eliminates them on (in)equalities. *}

named_theorems divide_simps "rewrite rules to eliminate divisions"

class division_ring = ring_1 + inverse +
  assumes left_inverse [simp]:  "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
  assumes right_inverse [simp]: "a \<noteq> 0 \<Longrightarrow> a * inverse a = 1"
  assumes divide_inverse: "a / b = a * inverse b"
begin

subclass ring_1_no_zero_divisors
proof
  fix a b :: 'a
  assume a: "a \<noteq> 0" and b: "b \<noteq> 0"
  show "a * b \<noteq> 0"
  proof
    assume ab: "a * b = 0"
    hence "0 = inverse a * (a * b) * inverse b" by simp
    also have "\<dots> = (inverse a * a) * (b * inverse b)"
      by (simp only: mult.assoc)
    also have "\<dots> = 1" using a b by simp
    finally show False by simp
  qed
qed

lemma nonzero_imp_inverse_nonzero:
  "a \<noteq> 0 \<Longrightarrow> inverse a \<noteq> 0"
proof
  assume ianz: "inverse a = 0"
  assume "a \<noteq> 0"
  hence "1 = a * inverse a" by simp
  also have "... = 0" by (simp add: ianz)
  finally have "1 = 0" .
  thus False by (simp add: eq_commute)
qed

lemma inverse_zero_imp_zero:
  "inverse a = 0 \<Longrightarrow> a = 0"
apply (rule classical)
apply (drule nonzero_imp_inverse_nonzero)
apply auto
done

lemma inverse_unique:
  assumes ab: "a * b = 1"
  shows "inverse a = b"
proof -
  have "a \<noteq> 0" using ab by (cases "a = 0") simp_all
  moreover have "inverse a * (a * b) = inverse a" by (simp add: ab)
  ultimately show ?thesis by (simp add: mult.assoc [symmetric])
qed

lemma nonzero_inverse_minus_eq:
  "a \<noteq> 0 \<Longrightarrow> inverse (- a) = - inverse a"
by (rule inverse_unique) simp

lemma nonzero_inverse_inverse_eq:
  "a \<noteq> 0 \<Longrightarrow> inverse (inverse a) = a"
by (rule inverse_unique) simp

lemma nonzero_inverse_eq_imp_eq:
  assumes "inverse a = inverse b" and "a \<noteq> 0" and "b \<noteq> 0"
  shows "a = b"
proof -
  from `inverse a = inverse b`
  have "inverse (inverse a) = inverse (inverse b)" by (rule arg_cong)
  with `a \<noteq> 0` and `b \<noteq> 0` show "a = b"
    by (simp add: nonzero_inverse_inverse_eq)
qed

lemma inverse_1 [simp]: "inverse 1 = 1"
by (rule inverse_unique) simp

lemma nonzero_inverse_mult_distrib:
  assumes "a \<noteq> 0" and "b \<noteq> 0"
  shows "inverse (a * b) = inverse b * inverse a"
proof -
  have "a * (b * inverse b) * inverse a = 1" using assms by simp
  hence "a * b * (inverse b * inverse a) = 1" by (simp only: mult.assoc)
  thus ?thesis by (rule inverse_unique)
qed

lemma division_ring_inverse_add:
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> inverse a + inverse b = inverse a * (a + b) * inverse b"
by (simp add: algebra_simps)

lemma division_ring_inverse_diff:
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> inverse a - inverse b = inverse a * (b - a) * inverse b"
by (simp add: algebra_simps)

lemma right_inverse_eq: "b \<noteq> 0 \<Longrightarrow> a / b = 1 \<longleftrightarrow> a = b"
proof
  assume neq: "b \<noteq> 0"
  {
    hence "a = (a / b) * b" by (simp add: divide_inverse mult.assoc)
    also assume "a / b = 1"
    finally show "a = b" by simp
  next
    assume "a = b"
    with neq show "a / b = 1" by (simp add: divide_inverse)
  }
qed

lemma nonzero_inverse_eq_divide: "a \<noteq> 0 \<Longrightarrow> inverse a = 1 / a"
by (simp add: divide_inverse)

lemma divide_self [simp]: "a \<noteq> 0 \<Longrightarrow> a / a = 1"
by (simp add: divide_inverse)

lemma divide_zero_left [simp]: "0 / a = 0"
by (simp add: divide_inverse)

lemma inverse_eq_divide [field_simps, divide_simps]: "inverse a = 1 / a"
by (simp add: divide_inverse)

lemma add_divide_distrib: "(a+b) / c = a/c + b/c"
by (simp add: divide_inverse algebra_simps)

lemma divide_1 [simp]: "a / 1 = a"
  by (simp add: divide_inverse)

lemma times_divide_eq_right [simp]: "a * (b / c) = (a * b) / c"
  by (simp add: divide_inverse mult.assoc)

lemma minus_divide_left: "- (a / b) = (-a) / b"
  by (simp add: divide_inverse)

lemma nonzero_minus_divide_right: "b \<noteq> 0 ==> - (a / b) = a / (- b)"
  by (simp add: divide_inverse nonzero_inverse_minus_eq)

lemma nonzero_minus_divide_divide: "b \<noteq> 0 ==> (-a) / (-b) = a / b"
  by (simp add: divide_inverse nonzero_inverse_minus_eq)

lemma divide_minus_left [simp]: "(-a) / b = - (a / b)"
  by (simp add: divide_inverse)

lemma diff_divide_distrib: "(a - b) / c = a / c - b / c"
  using add_divide_distrib [of a "- b" c] by simp

lemma nonzero_eq_divide_eq [field_simps]: "c \<noteq> 0 \<Longrightarrow> a = b / c \<longleftrightarrow> a * c = b"
proof -
  assume [simp]: "c \<noteq> 0"
  have "a = b / c \<longleftrightarrow> a * c = (b / c) * c" by simp
  also have "... \<longleftrightarrow> a * c = b" by (simp add: divide_inverse mult.assoc)
  finally show ?thesis .
qed

lemma nonzero_divide_eq_eq [field_simps]: "c \<noteq> 0 \<Longrightarrow> b / c = a \<longleftrightarrow> b = a * c"
proof -
  assume [simp]: "c \<noteq> 0"
  have "b / c = a \<longleftrightarrow> (b / c) * c = a * c" by simp
  also have "... \<longleftrightarrow> b = a * c" by (simp add: divide_inverse mult.assoc)
  finally show ?thesis .
qed

lemma nonzero_neg_divide_eq_eq [field_simps]: "b \<noteq> 0 \<Longrightarrow> - (a / b) = c \<longleftrightarrow> - a = c * b"
  using nonzero_divide_eq_eq[of b "-a" c] by (simp add: divide_minus_left)

lemma nonzero_neg_divide_eq_eq2 [field_simps]: "b \<noteq> 0 \<Longrightarrow> c = - (a / b) \<longleftrightarrow> c * b = - a"
  using nonzero_neg_divide_eq_eq[of b a c] by auto

lemma divide_eq_imp: "c \<noteq> 0 \<Longrightarrow> b = a * c \<Longrightarrow> b / c = a"
  by (simp add: divide_inverse mult.assoc)

lemma eq_divide_imp: "c \<noteq> 0 \<Longrightarrow> a * c = b \<Longrightarrow> a = b / c"
  by (drule sym) (simp add: divide_inverse mult.assoc)

lemma add_divide_eq_iff [field_simps]:
  "z \<noteq> 0 \<Longrightarrow> x + y / z = (x * z + y) / z"
  by (simp add: add_divide_distrib nonzero_eq_divide_eq)

lemma divide_add_eq_iff [field_simps]:
  "z \<noteq> 0 \<Longrightarrow> x / z + y = (x + y * z) / z"
  by (simp add: add_divide_distrib nonzero_eq_divide_eq)

lemma diff_divide_eq_iff [field_simps]:
  "z \<noteq> 0 \<Longrightarrow> x - y / z = (x * z - y) / z"
  by (simp add: diff_divide_distrib nonzero_eq_divide_eq eq_diff_eq)

lemma minus_divide_add_eq_iff [field_simps]:
  "z \<noteq> 0 \<Longrightarrow> - (x / z) + y = (- x + y * z) / z"
  by (simp add: add_divide_distrib diff_divide_eq_iff divide_minus_left)

lemma divide_diff_eq_iff [field_simps]:
  "z \<noteq> 0 \<Longrightarrow> x / z - y = (x - y * z) / z"
  by (simp add: field_simps)

lemma minus_divide_diff_eq_iff [field_simps]:
  "z \<noteq> 0 \<Longrightarrow> - (x / z) - y = (- x - y * z) / z"
  by (simp add: divide_diff_eq_iff[symmetric] divide_minus_left)

end

class division_ring_inverse_zero = division_ring +
  assumes inverse_zero [simp]: "inverse 0 = 0"
begin

lemma divide_zero [simp]:
  "a / 0 = 0"
  by (simp add: divide_inverse)

lemma divide_self_if [simp]:
  "a / a = (if a = 0 then 0 else 1)"
  by simp

lemma inverse_nonzero_iff_nonzero [simp]:
  "inverse a = 0 \<longleftrightarrow> a = 0"
  by rule (fact inverse_zero_imp_zero, simp)

lemma inverse_minus_eq [simp]:
  "inverse (- a) = - inverse a"
proof cases
  assume "a=0" thus ?thesis by simp
next
  assume "a\<noteq>0"
  thus ?thesis by (simp add: nonzero_inverse_minus_eq)
qed

lemma inverse_inverse_eq [simp]:
  "inverse (inverse a) = a"
proof cases
  assume "a=0" thus ?thesis by simp
next
  assume "a\<noteq>0"
  thus ?thesis by (simp add: nonzero_inverse_inverse_eq)
qed

lemma inverse_eq_imp_eq:
  "inverse a = inverse b \<Longrightarrow> a = b"
  by (drule arg_cong [where f="inverse"], simp)

lemma inverse_eq_iff_eq [simp]:
  "inverse a = inverse b \<longleftrightarrow> a = b"
  by (force dest!: inverse_eq_imp_eq)

lemma add_divide_eq_if_simps [divide_simps]:
    "a + b / z = (if z = 0 then a else (a * z + b) / z)"
    "a / z + b = (if z = 0 then b else (a + b * z) / z)"
    "- (a / z) + b = (if z = 0 then b else (-a + b * z) / z)"
    "a - b / z = (if z = 0 then a else (a * z - b) / z)"
    "a / z - b = (if z = 0 then -b else (a - b * z) / z)"
    "- (a / z) - b = (if z = 0 then -b else (- a - b * z) / z)"
  by (simp_all add: add_divide_eq_iff divide_add_eq_iff diff_divide_eq_iff divide_diff_eq_iff
      minus_divide_diff_eq_iff)

lemma [divide_simps]:
  shows divide_eq_eq: "b / c = a \<longleftrightarrow> (if c \<noteq> 0 then b = a * c else a = 0)"
    and eq_divide_eq: "a = b / c \<longleftrightarrow> (if c \<noteq> 0 then a * c = b else a = 0)"
    and minus_divide_eq_eq: "- (b / c) = a \<longleftrightarrow> (if c \<noteq> 0 then - b = a * c else a = 0)"
    and eq_minus_divide_eq: "a = - (b / c) \<longleftrightarrow> (if c \<noteq> 0 then a * c = - b else a = 0)"
  by (auto simp add:  field_simps)

end

subsection {* Fields *}

class field = comm_ring_1 + inverse +
  assumes field_inverse: "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
  assumes field_divide_inverse: "a / b = a * inverse b"
begin

subclass division_ring
proof
  fix a :: 'a
  assume "a \<noteq> 0"
  thus "inverse a * a = 1" by (rule field_inverse)
  thus "a * inverse a = 1" by (simp only: mult.commute)
next
  fix a b :: 'a
  show "a / b = a * inverse b" by (rule field_divide_inverse)
qed

subclass idom ..

text{*There is no slick version using division by zero.*}
lemma inverse_add:
  "[| a \<noteq> 0;  b \<noteq> 0 |]
   ==> inverse a + inverse b = (a + b) * inverse a * inverse b"
by (simp add: division_ring_inverse_add ac_simps)

lemma nonzero_mult_divide_mult_cancel_left [simp]:
assumes [simp]: "b\<noteq>0" and [simp]: "c\<noteq>0" shows "(c*a)/(c*b) = a/b"
proof -
  have "(c*a)/(c*b) = c * a * (inverse b * inverse c)"
    by (simp add: divide_inverse nonzero_inverse_mult_distrib)
  also have "... =  a * inverse b * (inverse c * c)"
    by (simp only: ac_simps)
  also have "... =  a * inverse b" by simp
    finally show ?thesis by (simp add: divide_inverse)
qed

lemma nonzero_mult_divide_mult_cancel_right [simp]:
  "\<lbrakk>b \<noteq> 0; c \<noteq> 0\<rbrakk> \<Longrightarrow> (a * c) / (b * c) = a / b"
by (simp add: mult.commute [of _ c])

lemma times_divide_eq_left [simp]: "(b / c) * a = (b * a) / c"
  by (simp add: divide_inverse ac_simps)

text{*It's not obvious whether @{text times_divide_eq} should be
  simprules or not. Their effect is to gather terms into one big
  fraction, like a*b*c / x*y*z. The rationale for that is unclear, but
  many proofs seem to need them.*}

lemmas times_divide_eq = times_divide_eq_right times_divide_eq_left

lemma add_frac_eq:
  assumes "y \<noteq> 0" and "z \<noteq> 0"
  shows "x / y + w / z = (x * z + w * y) / (y * z)"
proof -
  have "x / y + w / z = (x * z) / (y * z) + (y * w) / (y * z)"
    using assms by simp
  also have "\<dots> = (x * z + y * w) / (y * z)"
    by (simp only: add_divide_distrib)
  finally show ?thesis
    by (simp only: mult.commute)
qed

text{*Special Cancellation Simprules for Division*}

lemma nonzero_mult_divide_cancel_right [simp]:
  "b \<noteq> 0 \<Longrightarrow> a * b / b = a"
  using nonzero_mult_divide_mult_cancel_right [of 1 b a] by simp

lemma nonzero_mult_divide_cancel_left [simp]:
  "a \<noteq> 0 \<Longrightarrow> a * b / a = b"
using nonzero_mult_divide_mult_cancel_left [of 1 a b] by simp

lemma nonzero_divide_mult_cancel_right [simp]:
  "\<lbrakk>a \<noteq> 0; b \<noteq> 0\<rbrakk> \<Longrightarrow> b / (a * b) = 1 / a"
using nonzero_mult_divide_mult_cancel_right [of a b 1] by simp

lemma nonzero_divide_mult_cancel_left [simp]:
  "\<lbrakk>a \<noteq> 0; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / (a * b) = 1 / b"
using nonzero_mult_divide_mult_cancel_left [of b a 1] by simp

lemma nonzero_mult_divide_mult_cancel_left2 [simp]:
  "\<lbrakk>b \<noteq> 0; c \<noteq> 0\<rbrakk> \<Longrightarrow> (c * a) / (b * c) = a / b"
using nonzero_mult_divide_mult_cancel_left [of b c a] by (simp add: ac_simps)

lemma nonzero_mult_divide_mult_cancel_right2 [simp]:
  "\<lbrakk>b \<noteq> 0; c \<noteq> 0\<rbrakk> \<Longrightarrow> (a * c) / (c * b) = a / b"
using nonzero_mult_divide_mult_cancel_right [of b c a] by (simp add: ac_simps)

lemma diff_frac_eq:
  "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> x / y - w / z = (x * z - w * y) / (y * z)"
  by (simp add: field_simps)

lemma frac_eq_eq:
  "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> (x / y = w / z) = (x * z = w * y)"
  by (simp add: field_simps)

lemma divide_minus1 [simp]: "x / - 1 = - x"
  using nonzero_minus_divide_right [of "1" x] by simp

end

class field_inverse_zero = field +
  assumes field_inverse_zero: "inverse 0 = 0"
begin

subclass division_ring_inverse_zero proof
qed (fact field_inverse_zero)

text{*This version builds in division by zero while also re-orienting
      the right-hand side.*}
lemma inverse_mult_distrib [simp]:
  "inverse (a * b) = inverse a * inverse b"
proof cases
  assume "a \<noteq> 0 & b \<noteq> 0"
  thus ?thesis by (simp add: nonzero_inverse_mult_distrib ac_simps)
next
  assume "~ (a \<noteq> 0 & b \<noteq> 0)"
  thus ?thesis by force
qed

lemma inverse_divide [simp]:
  "inverse (a / b) = b / a"
  by (simp add: divide_inverse mult.commute)

text {* Calculations with fractions *}

text{* There is a whole bunch of simp-rules just for class @{text
field} but none for class @{text field} and @{text nonzero_divides}
because the latter are covered by a simproc. *}

lemma mult_divide_mult_cancel_left:
  "c \<noteq> 0 \<Longrightarrow> (c * a) / (c * b) = a / b"
apply (cases "b = 0")
apply simp_all
done

lemma mult_divide_mult_cancel_right:
  "c \<noteq> 0 \<Longrightarrow> (a * c) / (b * c) = a / b"
apply (cases "b = 0")
apply simp_all
done

lemma divide_divide_eq_right [simp]:
  "a / (b / c) = (a * c) / b"
  by (simp add: divide_inverse ac_simps)

lemma divide_divide_eq_left [simp]:
  "(a / b) / c = a / (b * c)"
  by (simp add: divide_inverse mult.assoc)

lemma divide_divide_times_eq:
  "(x / y) / (z / w) = (x * w) / (y * z)"
  by simp

text {*Special Cancellation Simprules for Division*}

lemma mult_divide_mult_cancel_left_if [simp]:
  shows "(c * a) / (c * b) = (if c = 0 then 0 else a / b)"
  by (simp add: mult_divide_mult_cancel_left)

text {* Division and Unary Minus *}

lemma minus_divide_right:
  "- (a / b) = a / - b"
  by (simp add: divide_inverse)

lemma divide_minus_right [simp]:
  "a / - b = - (a / b)"
  by (simp add: divide_inverse)

lemma minus_divide_divide:
  "(- a) / (- b) = a / b"
apply (cases "b=0", simp)
apply (simp add: nonzero_minus_divide_divide)
done

lemma inverse_eq_1_iff [simp]:
  "inverse x = 1 \<longleftrightarrow> x = 1"
  by (insert inverse_eq_iff_eq [of x 1], simp)

lemma divide_eq_0_iff [simp]:
  "a / b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
  by (simp add: divide_inverse)

lemma divide_cancel_right [simp]:
  "a / c = b / c \<longleftrightarrow> c = 0 \<or> a = b"
  apply (cases "c=0", simp)
  apply (simp add: divide_inverse)
  done

lemma divide_cancel_left [simp]:
  "c / a = c / b \<longleftrightarrow> c = 0 \<or> a = b"
  apply (cases "c=0", simp)
  apply (simp add: divide_inverse)
  done

lemma divide_eq_1_iff [simp]:
  "a / b = 1 \<longleftrightarrow> b \<noteq> 0 \<and> a = b"
  apply (cases "b=0", simp)
  apply (simp add: right_inverse_eq)
  done

lemma one_eq_divide_iff [simp]:
  "1 = a / b \<longleftrightarrow> b \<noteq> 0 \<and> a = b"
  by (simp add: eq_commute [of 1])

lemma times_divide_times_eq:
  "(x / y) * (z / w) = (x * z) / (y * w)"
  by simp

lemma add_frac_num:
  "y \<noteq> 0 \<Longrightarrow> x / y + z = (x + z * y) / y"
  by (simp add: add_divide_distrib)

lemma add_num_frac:
  "y \<noteq> 0 \<Longrightarrow> z + x / y = (x + z * y) / y"
  by (simp add: add_divide_distrib add.commute)

end

subsection {* Ordered fields *}

class linordered_field = field + linordered_idom
begin

lemma positive_imp_inverse_positive:
  assumes a_gt_0: "0 < a"
  shows "0 < inverse a"
proof -
  have "0 < a * inverse a"
    by (simp add: a_gt_0 [THEN less_imp_not_eq2])
  thus "0 < inverse a"
    by (simp add: a_gt_0 [THEN less_not_sym] zero_less_mult_iff)
qed

lemma negative_imp_inverse_negative:
  "a < 0 \<Longrightarrow> inverse a < 0"
  by (insert positive_imp_inverse_positive [of "-a"],
    simp add: nonzero_inverse_minus_eq less_imp_not_eq)

lemma inverse_le_imp_le:
  assumes invle: "inverse a \<le> inverse b" and apos: "0 < a"
  shows "b \<le> a"
proof (rule classical)
  assume "~ b \<le> a"
  hence "a < b"  by (simp add: linorder_not_le)
  hence bpos: "0 < b"  by (blast intro: apos less_trans)
  hence "a * inverse a \<le> a * inverse b"
    by (simp add: apos invle less_imp_le mult_left_mono)
  hence "(a * inverse a) * b \<le> (a * inverse b) * b"
    by (simp add: bpos less_imp_le mult_right_mono)
  thus "b \<le> a"  by (simp add: mult.assoc apos bpos less_imp_not_eq2)
qed

lemma inverse_positive_imp_positive:
  assumes inv_gt_0: "0 < inverse a" and nz: "a \<noteq> 0"
  shows "0 < a"
proof -
  have "0 < inverse (inverse a)"
    using inv_gt_0 by (rule positive_imp_inverse_positive)
  thus "0 < a"
    using nz by (simp add: nonzero_inverse_inverse_eq)
qed

lemma inverse_negative_imp_negative:
  assumes inv_less_0: "inverse a < 0" and nz: "a \<noteq> 0"
  shows "a < 0"
proof -
  have "inverse (inverse a) < 0"
    using inv_less_0 by (rule negative_imp_inverse_negative)
  thus "a < 0" using nz by (simp add: nonzero_inverse_inverse_eq)
qed

lemma linordered_field_no_lb:
  "\<forall>x. \<exists>y. y < x"
proof
  fix x::'a
  have m1: "- (1::'a) < 0" by simp
  from add_strict_right_mono[OF m1, where c=x]
  have "(- 1) + x < x" by simp
  thus "\<exists>y. y < x" by blast
qed

lemma linordered_field_no_ub:
  "\<forall> x. \<exists>y. y > x"
proof
  fix x::'a
  have m1: " (1::'a) > 0" by simp
  from add_strict_right_mono[OF m1, where c=x]
  have "1 + x > x" by simp
  thus "\<exists>y. y > x" by blast
qed

lemma less_imp_inverse_less:
  assumes less: "a < b" and apos:  "0 < a"
  shows "inverse b < inverse a"
proof (rule ccontr)
  assume "~ inverse b < inverse a"
  hence "inverse a \<le> inverse b" by simp
  hence "~ (a < b)"
    by (simp add: not_less inverse_le_imp_le [OF _ apos])
  thus False by (rule notE [OF _ less])
qed

lemma inverse_less_imp_less:
  "inverse a < inverse b \<Longrightarrow> 0 < a \<Longrightarrow> b < a"
apply (simp add: less_le [of "inverse a"] less_le [of "b"])
apply (force dest!: inverse_le_imp_le nonzero_inverse_eq_imp_eq)
done

text{*Both premises are essential. Consider -1 and 1.*}
lemma inverse_less_iff_less [simp]:
  "0 < a \<Longrightarrow> 0 < b \<Longrightarrow> inverse a < inverse b \<longleftrightarrow> b < a"
  by (blast intro: less_imp_inverse_less dest: inverse_less_imp_less)

lemma le_imp_inverse_le:
  "a \<le> b \<Longrightarrow> 0 < a \<Longrightarrow> inverse b \<le> inverse a"
  by (force simp add: le_less less_imp_inverse_less)

lemma inverse_le_iff_le [simp]:
  "0 < a \<Longrightarrow> 0 < b \<Longrightarrow> inverse a \<le> inverse b \<longleftrightarrow> b \<le> a"
  by (blast intro: le_imp_inverse_le dest: inverse_le_imp_le)

text{*These results refer to both operands being negative.  The opposite-sign
case is trivial, since inverse preserves signs.*}
lemma inverse_le_imp_le_neg:
  "inverse a \<le> inverse b \<Longrightarrow> b < 0 \<Longrightarrow> b \<le> a"
apply (rule classical)
apply (subgoal_tac "a < 0")
 prefer 2 apply force
apply (insert inverse_le_imp_le [of "-b" "-a"])
apply (simp add: nonzero_inverse_minus_eq)
done

lemma less_imp_inverse_less_neg:
   "a < b \<Longrightarrow> b < 0 \<Longrightarrow> inverse b < inverse a"
apply (subgoal_tac "a < 0")
 prefer 2 apply (blast intro: less_trans)
apply (insert less_imp_inverse_less [of "-b" "-a"])
apply (simp add: nonzero_inverse_minus_eq)
done

lemma inverse_less_imp_less_neg:
   "inverse a < inverse b \<Longrightarrow> b < 0 \<Longrightarrow> b < a"
apply (rule classical)
apply (subgoal_tac "a < 0")
 prefer 2
 apply force
apply (insert inverse_less_imp_less [of "-b" "-a"])
apply (simp add: nonzero_inverse_minus_eq)
done

lemma inverse_less_iff_less_neg [simp]:
  "a < 0 \<Longrightarrow> b < 0 \<Longrightarrow> inverse a < inverse b \<longleftrightarrow> b < a"
apply (insert inverse_less_iff_less [of "-b" "-a"])
apply (simp del: inverse_less_iff_less
            add: nonzero_inverse_minus_eq)
done

lemma le_imp_inverse_le_neg:
  "a \<le> b \<Longrightarrow> b < 0 ==> inverse b \<le> inverse a"
  by (force simp add: le_less less_imp_inverse_less_neg)

lemma inverse_le_iff_le_neg [simp]:
  "a < 0 \<Longrightarrow> b < 0 \<Longrightarrow> inverse a \<le> inverse b \<longleftrightarrow> b \<le> a"
  by (blast intro: le_imp_inverse_le_neg dest: inverse_le_imp_le_neg)

lemma one_less_inverse:
  "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> 1 < inverse a"
  using less_imp_inverse_less [of a 1, unfolded inverse_1] .

lemma one_le_inverse:
  "0 < a \<Longrightarrow> a \<le> 1 \<Longrightarrow> 1 \<le> inverse a"
  using le_imp_inverse_le [of a 1, unfolded inverse_1] .

lemma pos_le_divide_eq [field_simps]: "0 < c \<Longrightarrow> a \<le> b / c \<longleftrightarrow> a * c \<le> b"
proof -
  assume less: "0<c"
  hence "(a \<le> b/c) = (a*c \<le> (b/c)*c)"
    by (simp add: mult_le_cancel_right less_not_sym [OF less] del: times_divide_eq)
  also have "... = (a*c \<le> b)"
    by (simp add: less_imp_not_eq2 [OF less] divide_inverse mult.assoc)
  finally show ?thesis .
qed

lemma neg_le_divide_eq [field_simps]: "c < 0 \<Longrightarrow> a \<le> b / c \<longleftrightarrow> b \<le> a * c"
proof -
  assume less: "c<0"
  hence "(a \<le> b/c) = ((b/c)*c \<le> a*c)"
    by (simp add: mult_le_cancel_right less_not_sym [OF less] del: times_divide_eq)
  also have "... = (b \<le> a*c)"
    by (simp add: less_imp_not_eq [OF less] divide_inverse mult.assoc)
  finally show ?thesis .
qed

lemma pos_less_divide_eq [field_simps]: "0 < c \<Longrightarrow> a < b / c \<longleftrightarrow> a * c < b"
proof -
  assume less: "0<c"
  hence "(a < b/c) = (a*c < (b/c)*c)"
    by (simp add: mult_less_cancel_right_disj less_not_sym [OF less] del: times_divide_eq)
  also have "... = (a*c < b)"
    by (simp add: less_imp_not_eq2 [OF less] divide_inverse mult.assoc)
  finally show ?thesis .
qed

lemma neg_less_divide_eq [field_simps]: "c < 0 \<Longrightarrow> a < b / c \<longleftrightarrow> b < a * c"
proof -
  assume less: "c<0"
  hence "(a < b/c) = ((b/c)*c < a*c)"
    by (simp add: mult_less_cancel_right_disj less_not_sym [OF less] del: times_divide_eq)
  also have "... = (b < a*c)"
    by (simp add: less_imp_not_eq [OF less] divide_inverse mult.assoc)
  finally show ?thesis .
qed

lemma pos_divide_less_eq [field_simps]: "0 < c \<Longrightarrow> b / c < a \<longleftrightarrow> b < a * c"
proof -
  assume less: "0<c"
  hence "(b/c < a) = ((b/c)*c < a*c)"
    by (simp add: mult_less_cancel_right_disj less_not_sym [OF less] del: times_divide_eq)
  also have "... = (b < a*c)"
    by (simp add: less_imp_not_eq2 [OF less] divide_inverse mult.assoc)
  finally show ?thesis .
qed

lemma neg_divide_less_eq [field_simps]: "c < 0 \<Longrightarrow> b / c < a \<longleftrightarrow> a * c < b"
proof -
  assume less: "c<0"
  hence "(b/c < a) = (a*c < (b/c)*c)"
    by (simp add: mult_less_cancel_right_disj less_not_sym [OF less] del: times_divide_eq)
  also have "... = (a*c < b)"
    by (simp add: less_imp_not_eq [OF less] divide_inverse mult.assoc)
  finally show ?thesis .
qed

lemma pos_divide_le_eq [field_simps]: "0 < c \<Longrightarrow> b / c \<le> a \<longleftrightarrow> b \<le> a * c"
proof -
  assume less: "0<c"
  hence "(b/c \<le> a) = ((b/c)*c \<le> a*c)"
    by (simp add: mult_le_cancel_right less_not_sym [OF less] del: times_divide_eq)
  also have "... = (b \<le> a*c)"
    by (simp add: less_imp_not_eq2 [OF less] divide_inverse mult.assoc)
  finally show ?thesis .
qed

lemma neg_divide_le_eq [field_simps]: "c < 0 \<Longrightarrow> b / c \<le> a \<longleftrightarrow> a * c \<le> b"
proof -
  assume less: "c<0"
  hence "(b/c \<le> a) = (a*c \<le> (b/c)*c)"
    by (simp add: mult_le_cancel_right less_not_sym [OF less] del: times_divide_eq)
  also have "... = (a*c \<le> b)"
    by (simp add: less_imp_not_eq [OF less] divide_inverse mult.assoc)
  finally show ?thesis .
qed

text{* The following @{text field_simps} rules are necessary, as minus is always moved atop of
division but we want to get rid of division. *}

lemma pos_le_minus_divide_eq [field_simps]: "0 < c \<Longrightarrow> a \<le> - (b / c) \<longleftrightarrow> a * c \<le> - b"
  unfolding minus_divide_left by (rule pos_le_divide_eq)

lemma neg_le_minus_divide_eq [field_simps]: "c < 0 \<Longrightarrow> a \<le> - (b / c) \<longleftrightarrow> - b \<le> a * c"
  unfolding minus_divide_left by (rule neg_le_divide_eq)

lemma pos_less_minus_divide_eq [field_simps]: "0 < c \<Longrightarrow> a < - (b / c) \<longleftrightarrow> a * c < - b"
  unfolding minus_divide_left by (rule pos_less_divide_eq)

lemma neg_less_minus_divide_eq [field_simps]: "c < 0 \<Longrightarrow> a < - (b / c) \<longleftrightarrow> - b < a * c"
  unfolding minus_divide_left by (rule neg_less_divide_eq)

lemma pos_minus_divide_less_eq [field_simps]: "0 < c \<Longrightarrow> - (b / c) < a \<longleftrightarrow> - b < a * c"
  unfolding minus_divide_left by (rule pos_divide_less_eq)

lemma neg_minus_divide_less_eq [field_simps]: "c < 0 \<Longrightarrow> - (b / c) < a \<longleftrightarrow> a * c < - b"
  unfolding minus_divide_left by (rule neg_divide_less_eq)

lemma pos_minus_divide_le_eq [field_simps]: "0 < c \<Longrightarrow> - (b / c) \<le> a \<longleftrightarrow> - b \<le> a * c"
  unfolding minus_divide_left by (rule pos_divide_le_eq)

lemma neg_minus_divide_le_eq [field_simps]: "c < 0 \<Longrightarrow> - (b / c) \<le> a \<longleftrightarrow> a * c \<le> - b"
  unfolding minus_divide_left by (rule neg_divide_le_eq)

lemma frac_less_eq:
  "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> x / y < w / z \<longleftrightarrow> (x * z - w * y) / (y * z) < 0"
  by (subst less_iff_diff_less_0) (simp add: diff_frac_eq )

lemma frac_le_eq:
  "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> x / y \<le> w / z \<longleftrightarrow> (x * z - w * y) / (y * z) \<le> 0"
  by (subst le_iff_diff_le_0) (simp add: diff_frac_eq )

text{* Lemmas @{text sign_simps} is a first attempt to automate proofs
of positivity/negativity needed for @{text field_simps}. Have not added @{text
sign_simps} to @{text field_simps} because the former can lead to case
explosions. *}

lemmas sign_simps = algebra_simps zero_less_mult_iff mult_less_0_iff

lemmas (in -) sign_simps = algebra_simps zero_less_mult_iff mult_less_0_iff

(* Only works once linear arithmetic is installed:
text{*An example:*}
lemma fixes a b c d e f :: "'a::linordered_field"
shows "\<lbrakk>a>b; c<d; e<f; 0 < u \<rbrakk> \<Longrightarrow>
 ((a-b)*(c-d)*(e-f))/((c-d)*(e-f)*(a-b)) <
 ((e-f)*(a-b)*(c-d))/((e-f)*(a-b)*(c-d)) + u"
apply(subgoal_tac "(c-d)*(e-f)*(a-b) > 0")
 prefer 2 apply(simp add:sign_simps)
apply(subgoal_tac "(c-d)*(e-f)*(a-b)*u > 0")
 prefer 2 apply(simp add:sign_simps)
apply(simp add:field_simps)
done
*)

lemma divide_pos_pos[simp]:
  "0 < x ==> 0 < y ==> 0 < x / y"
by(simp add:field_simps)

lemma divide_nonneg_pos:
  "0 <= x ==> 0 < y ==> 0 <= x / y"
by(simp add:field_simps)

lemma divide_neg_pos:
  "x < 0 ==> 0 < y ==> x / y < 0"
by(simp add:field_simps)

lemma divide_nonpos_pos:
  "x <= 0 ==> 0 < y ==> x / y <= 0"
by(simp add:field_simps)

lemma divide_pos_neg:
  "0 < x ==> y < 0 ==> x / y < 0"
by(simp add:field_simps)

lemma divide_nonneg_neg:
  "0 <= x ==> y < 0 ==> x / y <= 0"
by(simp add:field_simps)

lemma divide_neg_neg:
  "x < 0 ==> y < 0 ==> 0 < x / y"
by(simp add:field_simps)

lemma divide_nonpos_neg:
  "x <= 0 ==> y < 0 ==> 0 <= x / y"
by(simp add:field_simps)

lemma divide_strict_right_mono:
     "[|a < b; 0 < c|] ==> a / c < b / c"
by (simp add: less_imp_not_eq2 divide_inverse mult_strict_right_mono
              positive_imp_inverse_positive)

lemma divide_strict_right_mono_neg:
     "[|b < a; c < 0|] ==> a / c < b / c"
apply (drule divide_strict_right_mono [of _ _ "-c"], simp)
apply (simp add: less_imp_not_eq nonzero_minus_divide_right [symmetric])
done

text{*The last premise ensures that @{term a} and @{term b}
      have the same sign*}
lemma divide_strict_left_mono:
  "[|b < a; 0 < c; 0 < a*b|] ==> c / a < c / b"
  by (auto simp: field_simps zero_less_mult_iff mult_strict_right_mono)

lemma divide_left_mono:
  "[|b \<le> a; 0 \<le> c; 0 < a*b|] ==> c / a \<le> c / b"
  by (auto simp: field_simps zero_less_mult_iff mult_right_mono)

lemma divide_strict_left_mono_neg:
  "[|a < b; c < 0; 0 < a*b|] ==> c / a < c / b"
  by (auto simp: field_simps zero_less_mult_iff mult_strict_right_mono_neg)

lemma mult_imp_div_pos_le: "0 < y ==> x <= z * y ==>
    x / y <= z"
by (subst pos_divide_le_eq, assumption+)

lemma mult_imp_le_div_pos: "0 < y ==> z * y <= x ==>
    z <= x / y"
by(simp add:field_simps)

lemma mult_imp_div_pos_less: "0 < y ==> x < z * y ==>
    x / y < z"
by(simp add:field_simps)

lemma mult_imp_less_div_pos: "0 < y ==> z * y < x ==>
    z < x / y"
by(simp add:field_simps)

lemma frac_le: "0 <= x ==>
    x <= y ==> 0 < w ==> w <= z  ==> x / z <= y / w"
  apply (rule mult_imp_div_pos_le)
  apply simp
  apply (subst times_divide_eq_left)
  apply (rule mult_imp_le_div_pos, assumption)
  apply (rule mult_mono)
  apply simp_all
done

lemma frac_less: "0 <= x ==>
    x < y ==> 0 < w ==> w <= z  ==> x / z < y / w"
  apply (rule mult_imp_div_pos_less)
  apply simp
  apply (subst times_divide_eq_left)
  apply (rule mult_imp_less_div_pos, assumption)
  apply (erule mult_less_le_imp_less)
  apply simp_all
done

lemma frac_less2: "0 < x ==>
    x <= y ==> 0 < w ==> w < z  ==> x / z < y / w"
  apply (rule mult_imp_div_pos_less)
  apply simp_all
  apply (rule mult_imp_less_div_pos, assumption)
  apply (erule mult_le_less_imp_less)
  apply simp_all
done

lemma less_half_sum: "a < b ==> a < (a+b) / (1+1)"
by (simp add: field_simps zero_less_two)

lemma gt_half_sum: "a < b ==> (a+b)/(1+1) < b"
by (simp add: field_simps zero_less_two)

subclass unbounded_dense_linorder
proof
  fix x y :: 'a
  from less_add_one show "\<exists>y. x < y" ..
  from less_add_one have "x + (- 1) < (x + 1) + (- 1)" by (rule add_strict_right_mono)
  then have "x - 1 < x + 1 - 1" by simp
  then have "x - 1 < x" by (simp add: algebra_simps)
  then show "\<exists>y. y < x" ..
  show "x < y \<Longrightarrow> \<exists>z>x. z < y" by (blast intro!: less_half_sum gt_half_sum)
qed

lemma nonzero_abs_inverse:
     "a \<noteq> 0 ==> \<bar>inverse a\<bar> = inverse \<bar>a\<bar>"
apply (auto simp add: neq_iff abs_if nonzero_inverse_minus_eq
                      negative_imp_inverse_negative)
apply (blast intro: positive_imp_inverse_positive elim: less_asym)
done

lemma nonzero_abs_divide:
     "b \<noteq> 0 ==> \<bar>a / b\<bar> = \<bar>a\<bar> / \<bar>b\<bar>"
  by (simp add: divide_inverse abs_mult nonzero_abs_inverse)

lemma field_le_epsilon:
  assumes e: "\<And>e. 0 < e \<Longrightarrow> x \<le> y + e"
  shows "x \<le> y"
proof (rule dense_le)
  fix t assume "t < x"
  hence "0 < x - t" by (simp add: less_diff_eq)
  from e [OF this] have "x + 0 \<le> x + (y - t)" by (simp add: algebra_simps)
  then have "0 \<le> y - t" by (simp only: add_le_cancel_left)
  then show "t \<le> y" by (simp add: algebra_simps)
qed

end

class linordered_field_inverse_zero = linordered_field + field_inverse_zero
begin

lemma inverse_positive_iff_positive [simp]:
  "(0 < inverse a) = (0 < a)"
apply (cases "a = 0", simp)
apply (blast intro: inverse_positive_imp_positive positive_imp_inverse_positive)
done

lemma inverse_negative_iff_negative [simp]:
  "(inverse a < 0) = (a < 0)"
apply (cases "a = 0", simp)
apply (blast intro: inverse_negative_imp_negative negative_imp_inverse_negative)
done

lemma inverse_nonnegative_iff_nonnegative [simp]:
  "0 \<le> inverse a \<longleftrightarrow> 0 \<le> a"
  by (simp add: not_less [symmetric])

lemma inverse_nonpositive_iff_nonpositive [simp]:
  "inverse a \<le> 0 \<longleftrightarrow> a \<le> 0"
  by (simp add: not_less [symmetric])

lemma one_less_inverse_iff: "1 < inverse x \<longleftrightarrow> 0 < x \<and> x < 1"
  using less_trans[of 1 x 0 for x]
  by (cases x 0 rule: linorder_cases) (auto simp add: field_simps)

lemma one_le_inverse_iff: "1 \<le> inverse x \<longleftrightarrow> 0 < x \<and> x \<le> 1"
proof (cases "x = 1")
  case True then show ?thesis by simp
next
  case False then have "inverse x \<noteq> 1" by simp
  then have "1 \<noteq> inverse x" by blast
  then have "1 \<le> inverse x \<longleftrightarrow> 1 < inverse x" by (simp add: le_less)
  with False show ?thesis by (auto simp add: one_less_inverse_iff)
qed

lemma inverse_less_1_iff: "inverse x < 1 \<longleftrightarrow> x \<le> 0 \<or> 1 < x"
  by (simp add: not_le [symmetric] one_le_inverse_iff)

lemma inverse_le_1_iff: "inverse x \<le> 1 \<longleftrightarrow> x \<le> 0 \<or> 1 \<le> x"
  by (simp add: not_less [symmetric] one_less_inverse_iff)

lemma [divide_simps]:
  shows le_divide_eq: "a \<le> b / c \<longleftrightarrow> (if 0 < c then a * c \<le> b else if c < 0 then b \<le> a * c else a \<le> 0)"
    and divide_le_eq: "b / c \<le> a \<longleftrightarrow> (if 0 < c then b \<le> a * c else if c < 0 then a * c \<le> b else 0 \<le> a)"
    and less_divide_eq: "a < b / c \<longleftrightarrow> (if 0 < c then a * c < b else if c < 0 then b < a * c else a < 0)"
    and divide_less_eq: "b / c < a \<longleftrightarrow> (if 0 < c then b < a * c else if c < 0 then a * c < b else 0 < a)"
    and le_minus_divide_eq: "a \<le> - (b / c) \<longleftrightarrow> (if 0 < c then a * c \<le> - b else if c < 0 then - b \<le> a * c else a \<le> 0)"
    and minus_divide_le_eq: "- (b / c) \<le> a \<longleftrightarrow> (if 0 < c then - b \<le> a * c else if c < 0 then a * c \<le> - b else 0 \<le> a)"
    and less_minus_divide_eq: "a < - (b / c) \<longleftrightarrow> (if 0 < c then a * c < - b else if c < 0 then - b < a * c else  a < 0)"
    and minus_divide_less_eq: "- (b / c) < a \<longleftrightarrow> (if 0 < c then - b < a * c else if c < 0 then a * c < - b else 0 < a)"
  by (auto simp: field_simps not_less dest: antisym)

text {*Division and Signs*}

lemma
  shows zero_less_divide_iff: "0 < a / b \<longleftrightarrow> 0 < a \<and> 0 < b \<or> a < 0 \<and> b < 0"
    and divide_less_0_iff: "a / b < 0 \<longleftrightarrow> 0 < a \<and> b < 0 \<or> a < 0 \<and> 0 < b"
    and zero_le_divide_iff: "0 \<le> a / b \<longleftrightarrow> 0 \<le> a \<and> 0 \<le> b \<or> a \<le> 0 \<and> b \<le> 0"
    and divide_le_0_iff: "a / b \<le> 0 \<longleftrightarrow> 0 \<le> a \<and> b \<le> 0 \<or> a \<le> 0 \<and> 0 \<le> b"
  by (auto simp add: divide_simps)

text {* Division and the Number One *}

text{*Simplify expressions equated with 1*}

lemma zero_eq_1_divide_iff [simp]: "0 = 1 / a \<longleftrightarrow> a = 0"
  by (cases "a = 0") (auto simp: field_simps)

lemma one_divide_eq_0_iff [simp]: "1 / a = 0 \<longleftrightarrow> a = 0"
  using zero_eq_1_divide_iff[of a] by simp

text{*Simplify expressions such as @{text "0 < 1/x"} to @{text "0 < x"}*}

lemma zero_le_divide_1_iff [simp]:
  "0 \<le> 1 / a \<longleftrightarrow> 0 \<le> a"
  by (simp add: zero_le_divide_iff)

lemma zero_less_divide_1_iff [simp]:
  "0 < 1 / a \<longleftrightarrow> 0 < a"
  by (simp add: zero_less_divide_iff)

lemma divide_le_0_1_iff [simp]:
  "1 / a \<le> 0 \<longleftrightarrow> a \<le> 0"
  by (simp add: divide_le_0_iff)

lemma divide_less_0_1_iff [simp]:
  "1 / a < 0 \<longleftrightarrow> a < 0"
  by (simp add: divide_less_0_iff)

lemma divide_right_mono:
     "[|a \<le> b; 0 \<le> c|] ==> a/c \<le> b/c"
by (force simp add: divide_strict_right_mono le_less)

lemma divide_right_mono_neg: "a <= b
    ==> c <= 0 ==> b / c <= a / c"
apply (drule divide_right_mono [of _ _ "- c"])
apply auto
done

lemma divide_left_mono_neg: "a <= b
    ==> c <= 0 ==> 0 < a * b ==> c / a <= c / b"
  apply (drule divide_left_mono [of _ _ "- c"])
  apply (auto simp add: mult.commute)
done

lemma inverse_le_iff: "inverse a \<le> inverse b \<longleftrightarrow> (0 < a * b \<longrightarrow> b \<le> a) \<and> (a * b \<le> 0 \<longrightarrow> a \<le> b)"
  by (cases a 0 b 0 rule: linorder_cases[case_product linorder_cases])
     (auto simp add: field_simps zero_less_mult_iff mult_le_0_iff)

lemma inverse_less_iff: "inverse a < inverse b \<longleftrightarrow> (0 < a * b \<longrightarrow> b < a) \<and> (a * b \<le> 0 \<longrightarrow> a < b)"
  by (subst less_le) (auto simp: inverse_le_iff)

lemma divide_le_cancel: "a / c \<le> b / c \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
  by (simp add: divide_inverse mult_le_cancel_right)

lemma divide_less_cancel: "a / c < b / c \<longleftrightarrow> (0 < c \<longrightarrow> a < b) \<and> (c < 0 \<longrightarrow> b < a) \<and> c \<noteq> 0"
  by (auto simp add: divide_inverse mult_less_cancel_right)

text{*Simplify quotients that are compared with the value 1.*}

lemma le_divide_eq_1:
  "(1 \<le> b / a) = ((0 < a & a \<le> b) | (a < 0 & b \<le> a))"
by (auto simp add: le_divide_eq)

lemma divide_le_eq_1:
  "(b / a \<le> 1) = ((0 < a & b \<le> a) | (a < 0 & a \<le> b) | a=0)"
by (auto simp add: divide_le_eq)

lemma less_divide_eq_1:
  "(1 < b / a) = ((0 < a & a < b) | (a < 0 & b < a))"
by (auto simp add: less_divide_eq)

lemma divide_less_eq_1:
  "(b / a < 1) = ((0 < a & b < a) | (a < 0 & a < b) | a=0)"
by (auto simp add: divide_less_eq)

lemma divide_nonneg_nonneg [simp]:
  "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x / y"
  by (auto simp add: divide_simps)

lemma divide_nonpos_nonpos:
  "x \<le> 0 \<Longrightarrow> y \<le> 0 \<Longrightarrow> 0 \<le> x / y"
  by (auto simp add: divide_simps)

lemma divide_nonneg_nonpos:
  "0 \<le> x \<Longrightarrow> y \<le> 0 \<Longrightarrow> x / y \<le> 0"
  by (auto simp add: divide_simps)

lemma divide_nonpos_nonneg:
  "x \<le> 0 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x / y \<le> 0"
  by (auto simp add: divide_simps)

text {*Conditional Simplification Rules: No Case Splits*}

lemma le_divide_eq_1_pos [simp]:
  "0 < a \<Longrightarrow> (1 \<le> b/a) = (a \<le> b)"
by (auto simp add: le_divide_eq)

lemma le_divide_eq_1_neg [simp]:
  "a < 0 \<Longrightarrow> (1 \<le> b/a) = (b \<le> a)"
by (auto simp add: le_divide_eq)

lemma divide_le_eq_1_pos [simp]:
  "0 < a \<Longrightarrow> (b/a \<le> 1) = (b \<le> a)"
by (auto simp add: divide_le_eq)

lemma divide_le_eq_1_neg [simp]:
  "a < 0 \<Longrightarrow> (b/a \<le> 1) = (a \<le> b)"
by (auto simp add: divide_le_eq)

lemma less_divide_eq_1_pos [simp]:
  "0 < a \<Longrightarrow> (1 < b/a) = (a < b)"
by (auto simp add: less_divide_eq)

lemma less_divide_eq_1_neg [simp]:
  "a < 0 \<Longrightarrow> (1 < b/a) = (b < a)"
by (auto simp add: less_divide_eq)

lemma divide_less_eq_1_pos [simp]:
  "0 < a \<Longrightarrow> (b/a < 1) = (b < a)"
by (auto simp add: divide_less_eq)

lemma divide_less_eq_1_neg [simp]:
  "a < 0 \<Longrightarrow> b/a < 1 <-> a < b"
by (auto simp add: divide_less_eq)

lemma eq_divide_eq_1 [simp]:
  "(1 = b/a) = ((a \<noteq> 0 & a = b))"
by (auto simp add: eq_divide_eq)

lemma divide_eq_eq_1 [simp]:
  "(b/a = 1) = ((a \<noteq> 0 & a = b))"
by (auto simp add: divide_eq_eq)

lemma abs_inverse [simp]:
     "\<bar>inverse a\<bar> =
      inverse \<bar>a\<bar>"
apply (cases "a=0", simp)
apply (simp add: nonzero_abs_inverse)
done

lemma abs_divide [simp]:
     "\<bar>a / b\<bar> = \<bar>a\<bar> / \<bar>b\<bar>"
apply (cases "b=0", simp)
apply (simp add: nonzero_abs_divide)
done

lemma abs_div_pos: "0 < y ==>
    \<bar>x\<bar> / y = \<bar>x / y\<bar>"
  apply (subst abs_divide)
  apply (simp add: order_less_imp_le)
done

lemma zero_le_divide_abs_iff [simp]: "(0 \<le> a / abs b) = (0 \<le> a | b = 0)"
by (auto simp: zero_le_divide_iff)

lemma divide_le_0_abs_iff [simp]: "(a / abs b \<le> 0) = (a \<le> 0 | b = 0)"
by (auto simp: divide_le_0_iff)

lemma field_le_mult_one_interval:
  assumes *: "\<And>z. \<lbrakk> 0 < z ; z < 1 \<rbrakk> \<Longrightarrow> z * x \<le> y"
  shows "x \<le> y"
proof (cases "0 < x")
  assume "0 < x"
  thus ?thesis
    using dense_le_bounded[of 0 1 "y/x"] *
    unfolding le_divide_eq if_P[OF `0 < x`] by simp
next
  assume "\<not>0 < x" hence "x \<le> 0" by simp
  obtain s::'a where s: "0 < s" "s < 1" using dense[of 0 "1\<Colon>'a"] by auto
  hence "x \<le> s * x" using mult_le_cancel_right[of 1 x s] `x \<le> 0` by auto
  also note *[OF s]
  finally show ?thesis .
qed

end

code_identifier
  code_module Fields \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith

end