diff --git a/src/HOL/Library/Char_ord.thy b/src/HOL/Library/Char_ord.thy --- a/src/HOL/Library/Char_ord.thy +++ b/src/HOL/Library/Char_ord.thy @@ -1,49 +1,126 @@ (* Title: HOL/Library/Char_ord.thy Author: Norbert Voelker, Florian Haftmann *) section \Order on characters\ theory Char_ord imports Main begin +context linordered_semidom +begin + +lemma horner_sum_nonnegative: + \0 \ horner_sum of_bool 2 bs\ + by (induction bs) simp_all + +end + +context unique_euclidean_semiring_numeral +begin + +lemma horner_sum_bound: + \horner_sum of_bool 2 bs < 2 ^ length bs\ +proof (induction bs) + case Nil + then show ?case + by simp +next + case (Cons b bs) + moreover define a where \a = 2 ^ length bs - horner_sum of_bool 2 bs\ + ultimately have *: \2 ^ length bs = horner_sum of_bool 2 bs + a\ + by simp + have \1 < a * 2\ if \0 < a\ + using that add_mono [of 1 a 1 a] + by (simp add: mult_2_right discrete) + with Cons show ?case + by (simp add: algebra_simps *) +qed + +end + +context unique_euclidean_semiring_numeral +begin + +lemma horner_sum_less_eq_iff_lexordp_eq: + \horner_sum of_bool 2 bs \ horner_sum of_bool 2 cs \ lexordp_eq (rev bs) (rev cs)\ + if \length bs = length cs\ +proof - + have \horner_sum of_bool 2 (rev bs) \ horner_sum of_bool 2 (rev cs) \ lexordp_eq bs cs\ + if \length bs = length cs\ for bs cs + using that proof (induction bs cs rule: list_induct2) + case Nil + then show ?case + by simp + next + case (Cons b bs c cs) + with horner_sum_nonnegative [of \rev bs\] horner_sum_nonnegative [of \rev cs\] + horner_sum_bound [of \rev bs\] horner_sum_bound [of \rev cs\] + show ?case + by (auto simp add: horner_sum_append not_le Cons intro: add_strict_increasing2 add_increasing) + qed + from that this [of \rev bs\ \rev cs\] show ?thesis + by simp +qed + +lemma horner_sum_less_iff_lexordp: + \horner_sum of_bool 2 bs < horner_sum of_bool 2 cs \ ord_class.lexordp (rev bs) (rev cs)\ + if \length bs = length cs\ +proof - + have \horner_sum of_bool 2 (rev bs) < horner_sum of_bool 2 (rev cs) \ ord_class.lexordp bs cs\ + if \length bs = length cs\ for bs cs + using that proof (induction bs cs rule: list_induct2) + case Nil + then show ?case + by simp + next + case (Cons b bs c cs) + with horner_sum_nonnegative [of \rev bs\] horner_sum_nonnegative [of \rev cs\] + horner_sum_bound [of \rev bs\] horner_sum_bound [of \rev cs\] + show ?case + by (auto simp add: horner_sum_append not_less Cons intro: add_strict_increasing2 add_increasing) + qed + from that this [of \rev bs\ \rev cs\] show ?thesis + by simp +qed + +end + instantiation char :: linorder begin -definition less_eq_char :: "char \ char \ bool" - where "c1 \ c2 \ of_char c1 \ (of_char c2 :: nat)" +definition less_eq_char :: \char \ char \ bool\ + where \c1 \ c2 \ of_char c1 \ (of_char c2 :: nat)\ -definition less_char :: "char \ char \ bool" - where "c1 < c2 \ of_char c1 < (of_char c2 :: nat)" +definition less_char :: \char \ char \ bool\ + where \c1 < c2 \ of_char c1 < (of_char c2 :: nat)\ instance by standard (auto simp add: less_eq_char_def less_char_def) end -lemma less_eq_char_simp [simp]: - "Char b0 b1 b2 b3 b4 b5 b6 b7 \ Char c0 c1 c2 c3 c4 c5 c6 c7 - \ horner_sum of_bool (2 :: nat) [b0, b1, b2, b3, b4, b5, b6, b7] - \ horner_sum of_bool (2 :: nat) [c0, c1, c2, c3, c4, c5, c6, c7]" - by (simp add: less_eq_char_def) +lemma less_eq_char_simp [simp, code]: + \Char b0 b1 b2 b3 b4 b5 b6 b7 \ Char c0 c1 c2 c3 c4 c5 c6 c7 + \ lexordp_eq [b7, b6, b5, b4, b3, b2, b1, b0] [c7, c6, c5, c4, c3, c2, c1, c0]\ + by (simp only: less_eq_char_def of_char_def char.sel horner_sum_less_eq_iff_lexordp_eq list.size) simp -lemma less_char_simp [simp]: - "Char b0 b1 b2 b3 b4 b5 b6 b7 < Char c0 c1 c2 c3 c4 c5 c6 c7 - \ horner_sum of_bool (2 :: nat) [b0, b1, b2, b3, b4, b5, b6, b7] - < horner_sum of_bool (2 :: nat) [c0, c1, c2, c3, c4, c5, c6, c7]" - by (simp add: less_char_def) +lemma less_char_simp [simp, code]: + \Char b0 b1 b2 b3 b4 b5 b6 b7 < Char c0 c1 c2 c3 c4 c5 c6 c7 + \ ord_class.lexordp [b7, b6, b5, b4, b3, b2, b1, b0] [c7, c6, c5, c4, c3, c2, c1, c0]\ + by (simp only: less_char_def of_char_def char.sel horner_sum_less_iff_lexordp list.size) simp instantiation char :: distrib_lattice begin -definition "(inf :: char \ _) = min" -definition "(sup :: char \ _) = max" +definition \(inf :: char \ _) = min\ +definition \(sup :: char \ _) = max\ instance by standard (auto simp add: inf_char_def sup_char_def max_min_distrib2) end end