(*<*)
theory natsum imports Main begin
(*>*)
text{*\noindent
In particular, there are @{text"case"}-expressions, for example
@{term[display]"case n of 0 => 0 | Suc m => m"}
primitive recursion, for example
*}

primrec sum :: "nat \<Rightarrow> nat" where
"sum 0 = 0" |
"sum (Suc n) = Suc n + sum n"

text{*\noindent
and induction, for example
*}

lemma "sum n + sum n = n*(Suc n)"
apply(induct_tac n)
apply(auto)
done

text{*\newcommand{\mystar}{*%
}
\index{arithmetic operations!for \protect\isa{nat}}%
The arithmetic operations \isadxboldpos{+}{$HOL2arithfun},
\isadxboldpos{-}{$HOL2arithfun}, \isadxboldpos{\mystar}{$HOL2arithfun},
\sdx{div}, \sdx{mod}, \cdx{min} and
\cdx{max} are predefined, as are the relations
\isadxboldpos{\isasymle}{$HOL2arithrel} and
\isadxboldpos{<}{$HOL2arithrel}. As usual, @{prop"m-n = (0::nat)"} if
@{prop"m<n"}. There is even a least number operation
\sdx{LEAST}\@.  For example, @{prop"(LEAST n. 0 < n) = Suc 0"}.
\begin{warn}\index{overloading}
  The constants \cdx{0} and \cdx{1} and the operations
  \isadxboldpos{+}{$HOL2arithfun}, \isadxboldpos{-}{$HOL2arithfun},
  \isadxboldpos{\mystar}{$HOL2arithfun}, \cdx{min},
  \cdx{max}, \isadxboldpos{\isasymle}{$HOL2arithrel} and
  \isadxboldpos{<}{$HOL2arithrel} are overloaded: they are available
  not just for natural numbers but for other types as well.
  For example, given the goal @{text"x + 0 = x"}, there is nothing to indicate
  that you are talking about natural numbers. Hence Isabelle can only infer
  that @{term x} is of some arbitrary type where @{text 0} and @{text"+"} are
  declared. As a consequence, you will be unable to prove the
  goal. To alert you to such pitfalls, Isabelle flags numerals without a
  fixed type in its output: @{prop"x+0 = x"}. (In the absence of a numeral,
  it may take you some time to realize what has happened if \pgmenu{Show
  Types} is not set).  In this particular example, you need to include
  an explicit type constraint, for example @{text"x+0 = (x::nat)"}. If there
  is enough contextual information this may not be necessary: @{prop"Suc x =
  x"} automatically implies @{text"x::nat"} because @{term Suc} is not
  overloaded.

  For details on overloading see \S\ref{sec:overloading}.
  Table~\ref{tab:overloading} in the appendix shows the most important
  overloaded operations.
\end{warn}
\begin{warn}
  The symbols \isadxboldpos{>}{$HOL2arithrel} and
  \isadxboldpos{\isasymge}{$HOL2arithrel} are merely syntax: @{text"x > y"}
  stands for @{prop"y < x"} and similary for @{text"\<ge>"} and
  @{text"\<le>"}.
\end{warn}
\begin{warn}
  Constant @{text"1::nat"} is defined to equal @{term"Suc 0"}. This definition
  (see \S\ref{sec:ConstDefinitions}) is unfolded automatically by some
  tactics (like @{text auto}, @{text simp} and @{text arith}) but not by
  others (especially the single step tactics in Chapter~\ref{chap:rules}).
  If you need the full set of numerals, see~\S\ref{sec:numerals}.
  \emph{Novices are advised to stick to @{term"0::nat"} and @{term Suc}.}
\end{warn}

Both @{text auto} and @{text simp}
(a method introduced below, \S\ref{sec:Simplification}) prove
simple arithmetic goals automatically:
*}

lemma "\<lbrakk> \<not> m < n; m < n + (1::nat) \<rbrakk> \<Longrightarrow> m = n"
(*<*)by(auto)(*>*)

text{*\noindent
For efficiency's sake, this built-in prover ignores quantified formulae,
many logical connectives, and all arithmetic operations apart from addition.
In consequence, @{text auto} and @{text simp} cannot prove this slightly more complex goal:
*}

lemma "m \<noteq> (n::nat) \<Longrightarrow> m < n \<or> n < m"
(*<*)by(arith)(*>*)

text{*\noindent The method \methdx{arith} is more general.  It attempts to
prove the first subgoal provided it is a \textbf{linear arithmetic} formula.
Such formulas may involve the usual logical connectives (@{text"\<not>"},
@{text"\<and>"}, @{text"\<or>"}, @{text"\<longrightarrow>"}, @{text"="},
@{text"\<forall>"}, @{text"\<exists>"}), the relations @{text"="},
@{text"\<le>"} and @{text"<"}, and the operations @{text"+"}, @{text"-"},
@{term min} and @{term max}.  For example, *}

lemma "min i (max j (k*k)) = max (min (k*k) i) (min i (j::nat))"
apply(arith)
(*<*)done(*>*)

text{*\noindent
succeeds because @{term"k*k"} can be treated as atomic. In contrast,
*}

lemma "n*n = n+1 \<Longrightarrow> n=0"
(*<*)oops(*>*)

text{*\noindent
is not proved by @{text arith} because the proof relies
on properties of multiplication. Only multiplication by numerals (which is
the same as iterated addition) is taken into account.

\begin{warn} The running time of @{text arith} is exponential in the number
  of occurrences of \ttindexboldpos{-}{$HOL2arithfun}, \cdx{min} and
  \cdx{max} because they are first eliminated by case distinctions.

If @{text k} is a numeral, \sdx{div}~@{text k}, \sdx{mod}~@{text k} and
@{text k}~\sdx{dvd} are also supported, where the former two are eliminated
by case distinctions, again blowing up the running time.

If the formula involves quantifiers, @{text arith} may take
super-exponential time and space.
\end{warn}
*}

(*<*)
end
(*>*)


(* Author: Nan Jiang *)

section \<open>More auxiliary lemmas for Lists Sorted wrt $<$\<close>

theory Sorted_Less2
  imports Main "HOL-Data_Structures.Cmp" "HOL-Data_Structures.Sorted_Less"
begin

lemma Cons_sorted_less: "sorted (rev xs) \<Longrightarrow> \<forall>x\<in>set xs. x < p \<Longrightarrow>   sorted (rev (p # xs))"
  by (induct xs) (auto simp add:sorted_wrt_append)

lemma Cons_sorted_less_nth:  "\<forall>x<length xs. xs ! x < p \<Longrightarrow> sorted (rev xs) \<Longrightarrow> sorted (rev (p # xs))"
  apply(subgoal_tac "\<forall>x\<in>set xs. x < p")
  apply(fastforce dest:Cons_sorted_less)
  apply(auto simp add: set_conv_nth)
  done

lemma distinct_sorted_rev: "sorted (rev xs) \<Longrightarrow> distinct xs"
  by (induct xs) (auto simp add:sorted_wrt_append)

lemma sorted_le2lt:
  assumes "List.sorted xs"
      and "distinct xs"
    shows "sorted xs"
  using assms
proof (induction xs)
  case Nil then show ?case by auto
next
  case (Cons x xs)
  note ind_hyp_xs = Cons(1)
  note sorted_le_x_xs = Cons(2)
  note dist_x_xs = Cons(3)
  from dist_x_xs have x_neq_xs: "\<forall>v \<in> set xs. x \<noteq> v"
                  and     dist: "distinct xs" by auto
  from sorted_le_x_xs have sorted_le_xs: "List.sorted xs"
                       and      x_le_xs: "\<forall>v \<in> set xs. v \<ge> x" by auto
  from x_neq_xs x_le_xs have x_lt_xs: "\<forall>v \<in> set xs. v > x" by fastforce
  from ind_hyp_xs[OF sorted_le_xs dist] have "sorted xs" by auto
  with x_lt_xs show ?case by auto
qed

lemma sorted_less_sorted_list_of_set: "sorted (sorted_list_of_set S)"
  by (auto intro:sorted_le2lt)

lemma distinct_sorted: "sorted xs \<Longrightarrow> distinct xs"
  by (induct xs) (auto simp add: sorted_wrt_append)

lemma sorted_less_set_unique:
  assumes "sorted xs"
      and "sorted ys"
      and "set xs = set ys"
    shows "xs = ys"
  using assms
proof -
  from assms(1) have "distinct xs" and "List.sorted xs" by (induct xs) auto
  also from assms(2) have "distinct ys" and "List.sorted ys" by (induct ys) auto
  ultimately show "xs = ys" using assms(3) by (auto intro: sorted_distinct_set_unique)
qed

lemma sorted_less_rev_set_unique:
  assumes "sorted (rev xs)"
      and "sorted (rev ys)"
      and "set xs = set ys"
    shows "xs = ys"
  using assms sorted_less_set_unique[of "rev xs" "rev ys"] by auto

lemma sorted_less_set_eq:
  assumes "sorted xs "
    shows "xs = sorted_list_of_set (set xs)"
  using assms
  apply(subgoal_tac "sorted (sorted_list_of_set (set xs))")
   apply(auto intro: sorted_less_set_unique sorted_le2lt)
  done

lemma sorted_less_rev_set_eq:
  assumes "sorted (rev xs) "
    shows "sorted_list_of_set (set xs) = rev xs"
  using assms sorted_less_set_eq[of "rev xs"] by auto

lemma sorted_insort_remove1: "sorted w \<Longrightarrow> (insort a (remove1 a w)) = sorted_list_of_set (insert a (set w)) "
proof-
  assume "sorted w"
  then have "(sorted_list_of_set (set w - {a})) = remove1 a w" using sorted_less_set_eq
    by (fastforce simp add:sorted_list_of_set_remove)
  hence "insort a (remove1 a w) = insort a (sorted_list_of_set (set w - {a}))" by simp
  then show ?thesis by (auto simp add:sorted_list_of_set_insert)
qed

end