src/HOL/Int.thy
changeset 29040 286c669d3a7a
parent 29039 8b9207f82a78
child 29046 773098b76201
equal deleted inserted replaced
29039:8b9207f82a78 29040:286c669d3a7a
  1074 
  1074 
  1075 subsubsection {* Equality of Binary Numbers *}
  1075 subsubsection {* Equality of Binary Numbers *}
  1076 
  1076 
  1077 text {* First version by Norbert Voelker *}
  1077 text {* First version by Norbert Voelker *}
  1078 
  1078 
  1079 definition
       
  1080   neg  :: "'a\<Colon>ordered_idom \<Rightarrow> bool"
       
  1081 where
       
  1082   "neg Z \<longleftrightarrow> Z < 0"
       
  1083 
       
  1084 definition (*for simplifying equalities*)
  1079 definition (*for simplifying equalities*)
  1085   iszero :: "'a\<Colon>semiring_1 \<Rightarrow> bool"
  1080   iszero :: "'a\<Colon>semiring_1 \<Rightarrow> bool"
  1086 where
  1081 where
  1087   "iszero z \<longleftrightarrow> z = 0"
  1082   "iszero z \<longleftrightarrow> z = 0"
  1088 
  1083 
  1089 lemma not_neg_int [simp]: "~ neg (of_nat n)"
       
  1090 by (simp add: neg_def)
       
  1091 
       
  1092 lemma neg_zminus_int [simp]: "neg (- (of_nat (Suc n)))"
       
  1093 by (simp add: neg_def neg_less_0_iff_less del: of_nat_Suc)
       
  1094 
       
  1095 lemmas neg_eq_less_0 = neg_def
       
  1096 
       
  1097 lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)"
       
  1098 by (simp add: neg_def linorder_not_less)
       
  1099 
       
  1100 text{*To simplify inequalities when Numeral1 can get simplified to 1*}
       
  1101 
       
  1102 lemma not_neg_0: "~ neg 0"
       
  1103 by (simp add: One_int_def neg_def)
       
  1104 
       
  1105 lemma not_neg_1: "~ neg 1"
       
  1106 by (simp add: neg_def linorder_not_less zero_le_one)
       
  1107 
       
  1108 lemma iszero_0: "iszero 0"
  1084 lemma iszero_0: "iszero 0"
  1109 by (simp add: iszero_def)
  1085 by (simp add: iszero_def)
  1110 
  1086 
  1111 lemma not_iszero_1: "~ iszero 1"
  1087 lemma not_iszero_1: "~ iszero 1"
  1112 by (simp add: iszero_def eq_commute)
  1088 by (simp add: iszero_def eq_commute)
  1113 
       
  1114 lemma neg_nat: "neg z ==> nat z = 0"
       
  1115 by (simp add: neg_def order_less_imp_le) 
       
  1116 
       
  1117 lemma not_neg_nat: "~ neg z ==> of_nat (nat z) = z"
       
  1118 by (simp add: linorder_not_less neg_def)
       
  1119 
  1089 
  1120 lemma eq_number_of_eq:
  1090 lemma eq_number_of_eq:
  1121   "((number_of x::'a::number_ring) = number_of y) =
  1091   "((number_of x::'a::number_ring) = number_of y) =
  1122    iszero (number_of (x + uminus y) :: 'a)"
  1092    iszero (number_of (x + uminus y) :: 'a)"
  1123   unfolding iszero_def number_of_add number_of_minus
  1093   unfolding iszero_def number_of_add number_of_minus
  1194 (* iszero_number_of_Pls would never normally be used
  1164 (* iszero_number_of_Pls would never normally be used
  1195    because its lhs simplifies to "iszero 0" *)
  1165    because its lhs simplifies to "iszero 0" *)
  1196 
  1166 
  1197 subsubsection {* The Less-Than Relation *}
  1167 subsubsection {* The Less-Than Relation *}
  1198 
  1168 
  1199 lemma less_number_of_eq_neg:
       
  1200   "((number_of x::'a::{ordered_idom,number_ring}) < number_of y)
       
  1201   = neg (number_of (x + uminus y) :: 'a)"
       
  1202 apply (subst less_iff_diff_less_0) 
       
  1203 apply (simp add: neg_def diff_minus number_of_add number_of_minus)
       
  1204 done
       
  1205 
       
  1206 text {*
       
  1207   If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
       
  1208   @{term Numeral0} IS @{term "number_of Pls"}
       
  1209 *}
       
  1210 
       
  1211 lemma not_neg_number_of_Pls:
       
  1212   "~ neg (number_of Pls ::'a::{ordered_idom,number_ring})"
       
  1213   by (simp add: neg_def numeral_0_eq_0)
       
  1214 
       
  1215 lemma neg_number_of_Min:
       
  1216   "neg (number_of Min ::'a::{ordered_idom,number_ring})"
       
  1217   by (simp add: neg_def zero_less_one numeral_m1_eq_minus_1)
       
  1218 
       
  1219 lemma double_less_0_iff:
  1169 lemma double_less_0_iff:
  1220   "(a + a < 0) = (a < (0::'a::ordered_idom))"
  1170   "(a + a < 0) = (a < (0::'a::ordered_idom))"
  1221 proof -
  1171 proof -
  1222   have "(a + a < 0) = ((1+1)*a < 0)" by (simp add: left_distrib)
  1172   have "(a + a < 0) = ((1+1)*a < 0)" by (simp add: left_distrib)
  1223   also have "... = (a < 0)"
  1173   also have "... = (a < 0)"
  1236   case (neg n)
  1186   case (neg n)
  1237   thus ?thesis by (simp del: of_nat_Suc of_nat_add
  1187   thus ?thesis by (simp del: of_nat_Suc of_nat_add
  1238     add: compare_rls of_nat_1 [symmetric] of_nat_add [symmetric])
  1188     add: compare_rls of_nat_1 [symmetric] of_nat_add [symmetric])
  1239 qed
  1189 qed
  1240 
  1190 
  1241 lemma neg_number_of_Bit0:
       
  1242   "neg (number_of (Bit0 w)::'a) = 
       
  1243   neg (number_of w :: 'a::{ordered_idom,number_ring})"
       
  1244 by (simp add: neg_def number_of_eq numeral_simps double_less_0_iff)
       
  1245 
       
  1246 lemma neg_number_of_Bit1:
       
  1247   "neg (number_of (Bit1 w)::'a) = 
       
  1248   neg (number_of w :: 'a::{ordered_idom,number_ring})"
       
  1249 proof -
       
  1250   have "((1::'a) + of_int w + of_int w < 0) = (of_int (1 + w + w) < (of_int 0 :: 'a))"
       
  1251     by simp
       
  1252   also have "... = (w < 0)" by (simp only: of_int_less_iff odd_less_0)
       
  1253   finally show ?thesis
       
  1254   by (simp add: neg_def number_of_eq numeral_simps)
       
  1255 qed
       
  1256 
       
  1257 lemmas neg_simps [simp] =
       
  1258   not_neg_0 not_neg_1
       
  1259   not_neg_number_of_Pls neg_number_of_Min
       
  1260   neg_number_of_Bit0 neg_number_of_Bit1
       
  1261 
       
  1262 text {* Less-Than or Equals *}
  1191 text {* Less-Than or Equals *}
  1263 
  1192 
  1264 text {* Reduces @{term "a\<le>b"} to @{term "~ (b<a)"} for ALL numerals. *}
  1193 text {* Reduces @{term "a\<le>b"} to @{term "~ (b<a)"} for ALL numerals. *}
  1265 
  1194 
  1266 lemmas le_number_of_eq_not_less =
  1195 lemmas le_number_of_eq_not_less =
  1267   linorder_not_less [of "number_of w" "number_of v", symmetric, 
  1196   linorder_not_less [of "number_of w" "number_of v", symmetric, 
  1268   standard]
  1197   standard]
  1269 
       
  1270 lemma le_number_of_eq:
       
  1271     "((number_of x::'a::{ordered_idom,number_ring}) \<le> number_of y)
       
  1272      = (~ (neg (number_of (y + uminus x) :: 'a)))"
       
  1273 by (simp add: le_number_of_eq_not_less less_number_of_eq_neg)
       
  1274 
  1198 
  1275 
  1199 
  1276 text {* Absolute value (@{term abs}) *}
  1200 text {* Absolute value (@{term abs}) *}
  1277 
  1201 
  1278 lemma abs_number_of:
  1202 lemma abs_number_of: