src/HOL/Word/Bits_Int.thy
changeset 70191 bdc835d934b7
parent 70190 ff9efdc84289
child 70192 b4bf82ed0ad5
equal deleted inserted replaced
70190:ff9efdc84289 70191:bdc835d934b7
  1183   done
  1183   done
  1184 
  1184 
  1185 
  1185 
  1186 subsection \<open>Logical operations\<close>
  1186 subsection \<open>Logical operations\<close>
  1187 
  1187 
  1188 text "bit-wise logical operations on the int type"
  1188 primrec bin_sc :: "nat \<Rightarrow> bool \<Rightarrow> int \<Rightarrow> int"
  1189 
  1189   where
  1190 instantiation int :: bit
  1190     Z: "bin_sc 0 b w = bin_rest w BIT b"
       
  1191   | Suc: "bin_sc (Suc n) b w = bin_sc n b (bin_rest w) BIT bin_last w"
       
  1192 
       
  1193 lemma bin_nth_sc [simp]: "bin_nth (bin_sc n b w) n \<longleftrightarrow> b"
       
  1194   by (induct n arbitrary: w) auto
       
  1195 
       
  1196 lemma bin_sc_sc_same [simp]: "bin_sc n c (bin_sc n b w) = bin_sc n c w"
       
  1197   by (induct n arbitrary: w) auto
       
  1198 
       
  1199 lemma bin_sc_sc_diff: "m \<noteq> n \<Longrightarrow> bin_sc m c (bin_sc n b w) = bin_sc n b (bin_sc m c w)"
       
  1200   apply (induct n arbitrary: w m)
       
  1201    apply (case_tac [!] m)
       
  1202      apply auto
       
  1203   done
       
  1204 
       
  1205 lemma bin_nth_sc_gen: "bin_nth (bin_sc n b w) m = (if m = n then b else bin_nth w m)"
       
  1206   by (induct n arbitrary: w m) (case_tac [!] m, auto)
       
  1207 
       
  1208 lemma bin_sc_nth [simp]: "bin_sc n (bin_nth w n) w = w"
       
  1209   by (induct n arbitrary: w) auto
       
  1210 
       
  1211 lemma bin_sign_sc [simp]: "bin_sign (bin_sc n b w) = bin_sign w"
       
  1212   by (induct n arbitrary: w) auto
       
  1213 
       
  1214 lemma bin_sc_bintr [simp]: "bintrunc m (bin_sc n x (bintrunc m (w))) = bintrunc m (bin_sc n x w)"
       
  1215   apply (induct n arbitrary: w m)
       
  1216    apply (case_tac [!] w rule: bin_exhaust)
       
  1217    apply (case_tac [!] m, auto)
       
  1218   done
       
  1219 
       
  1220 lemma bin_clr_le: "bin_sc n False w \<le> w"
       
  1221   apply (induct n arbitrary: w)
       
  1222    apply (case_tac [!] w rule: bin_exhaust)
       
  1223    apply (auto simp: le_Bits)
       
  1224   done
       
  1225 
       
  1226 lemma bin_set_ge: "bin_sc n True w \<ge> w"
       
  1227   apply (induct n arbitrary: w)
       
  1228    apply (case_tac [!] w rule: bin_exhaust)
       
  1229    apply (auto simp: le_Bits)
       
  1230   done
       
  1231 
       
  1232 lemma bintr_bin_clr_le: "bintrunc n (bin_sc m False w) \<le> bintrunc n w"
       
  1233   apply (induct n arbitrary: w m)
       
  1234    apply simp
       
  1235   apply (case_tac w rule: bin_exhaust)
       
  1236   apply (case_tac m)
       
  1237    apply (auto simp: le_Bits)
       
  1238   done
       
  1239 
       
  1240 lemma bintr_bin_set_ge: "bintrunc n (bin_sc m True w) \<ge> bintrunc n w"
       
  1241   apply (induct n arbitrary: w m)
       
  1242    apply simp
       
  1243   apply (case_tac w rule: bin_exhaust)
       
  1244   apply (case_tac m)
       
  1245    apply (auto simp: le_Bits)
       
  1246   done
       
  1247 
       
  1248 lemma bin_sc_FP [simp]: "bin_sc n False 0 = 0"
       
  1249   by (induct n) auto
       
  1250 
       
  1251 lemma bin_sc_TM [simp]: "bin_sc n True (- 1) = - 1"
       
  1252   by (induct n) auto
       
  1253 
       
  1254 lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP
       
  1255 
       
  1256 lemma bin_sc_minus: "0 < n \<Longrightarrow> bin_sc (Suc (n - 1)) b w = bin_sc n b w"
       
  1257   by auto
       
  1258 
       
  1259 lemmas bin_sc_Suc_minus =
       
  1260   trans [OF bin_sc_minus [symmetric] bin_sc.Suc]
       
  1261 
       
  1262 lemma bin_sc_numeral [simp]:
       
  1263   "bin_sc (numeral k) b w =
       
  1264     bin_sc (pred_numeral k) b (bin_rest w) BIT bin_last w"
       
  1265   by (simp add: numeral_eq_Suc)
       
  1266 
       
  1267 instantiation int :: bit_operations
  1191 begin
  1268 begin
  1192 
  1269 
  1193 definition int_not_def: "bitNOT = (\<lambda>x::int. - x - 1)"
  1270 definition int_not_def: "bitNOT = (\<lambda>x::int. - x - 1)"
  1194 
  1271 
  1195 function bitAND_int
  1272 function bitAND_int
  1205 
  1282 
  1206 definition int_or_def: "bitOR = (\<lambda>x y::int. NOT (NOT x AND NOT y))"
  1283 definition int_or_def: "bitOR = (\<lambda>x y::int. NOT (NOT x AND NOT y))"
  1207 
  1284 
  1208 definition int_xor_def: "bitXOR = (\<lambda>x y::int. (x AND NOT y) OR (NOT x AND y))"
  1285 definition int_xor_def: "bitXOR = (\<lambda>x y::int. (x AND NOT y) OR (NOT x AND y))"
  1209 
  1286 
       
  1287 definition [iff]: "i !! n \<longleftrightarrow> bin_nth i n"
       
  1288 
       
  1289 definition "lsb i = i !! 0" for i :: int
       
  1290 
       
  1291 definition "set_bit i n b = bin_sc n b i"
       
  1292 
       
  1293 definition
       
  1294   "set_bits f =
       
  1295     (if \<exists>n. \<forall>n'\<ge>n. \<not> f n' then
       
  1296       let n = LEAST n. \<forall>n'\<ge>n. \<not> f n'
       
  1297       in bl_to_bin (rev (map f [0..<n]))
       
  1298      else if \<exists>n. \<forall>n'\<ge>n. f n' then
       
  1299       let n = LEAST n. \<forall>n'\<ge>n. f n'
       
  1300       in sbintrunc n (bl_to_bin (True # rev (map f [0..<n])))
       
  1301      else 0 :: int)"
       
  1302 
       
  1303 definition "shiftl x n = x * 2 ^ n" for x :: int
       
  1304 
       
  1305 definition "shiftr x n = x div 2 ^ n" for x :: int
       
  1306 
       
  1307 definition "msb x \<longleftrightarrow> x < 0" for x :: int
       
  1308 
  1210 instance ..
  1309 instance ..
  1211 
  1310 
  1212 end
  1311 end
       
  1312 
  1213 
  1313 
  1214 subsubsection \<open>Basic simplification rules\<close>
  1314 subsubsection \<open>Basic simplification rules\<close>
  1215 
  1315 
  1216 lemma int_not_BIT [simp]: "NOT (w BIT b) = (NOT w) BIT (\<not> b)"
  1316 lemma int_not_BIT [simp]: "NOT (w BIT b) = (NOT w) BIT (\<not> b)"
  1217   by (cases b) (simp_all add: int_not_def Bit_def)
  1317   by (cases b) (simp_all add: int_not_def Bit_def)
  1869 by(simp add: int_not_def)
  1969 by(simp add: int_not_def)
  1870 
  1970 
  1871 
  1971 
  1872 subsection \<open>Setting and clearing bits\<close>
  1972 subsection \<open>Setting and clearing bits\<close>
  1873 
  1973 
  1874 primrec bin_sc :: "nat \<Rightarrow> bool \<Rightarrow> int \<Rightarrow> int"
  1974 
  1875   where
       
  1876     Z: "bin_sc 0 b w = bin_rest w BIT b"
       
  1877   | Suc: "bin_sc (Suc n) b w = bin_sc n b (bin_rest w) BIT bin_last w"
       
  1878 
       
  1879 lemma bin_nth_sc [simp]: "bin_nth (bin_sc n b w) n \<longleftrightarrow> b"
       
  1880   by (induct n arbitrary: w) auto
       
  1881 
       
  1882 lemma bin_sc_sc_same [simp]: "bin_sc n c (bin_sc n b w) = bin_sc n c w"
       
  1883   by (induct n arbitrary: w) auto
       
  1884 
       
  1885 lemma bin_sc_sc_diff: "m \<noteq> n \<Longrightarrow> bin_sc m c (bin_sc n b w) = bin_sc n b (bin_sc m c w)"
       
  1886   apply (induct n arbitrary: w m)
       
  1887    apply (case_tac [!] m)
       
  1888      apply auto
       
  1889   done
       
  1890 
       
  1891 lemma bin_nth_sc_gen: "bin_nth (bin_sc n b w) m = (if m = n then b else bin_nth w m)"
       
  1892   by (induct n arbitrary: w m) (case_tac [!] m, auto)
       
  1893 
       
  1894 lemma bin_sc_nth [simp]: "bin_sc n (bin_nth w n) w = w"
       
  1895   by (induct n arbitrary: w) auto
       
  1896 
       
  1897 lemma bin_sign_sc [simp]: "bin_sign (bin_sc n b w) = bin_sign w"
       
  1898   by (induct n arbitrary: w) auto
       
  1899 
       
  1900 lemma bin_sc_bintr [simp]: "bintrunc m (bin_sc n x (bintrunc m (w))) = bintrunc m (bin_sc n x w)"
       
  1901   apply (induct n arbitrary: w m)
       
  1902    apply (case_tac [!] w rule: bin_exhaust)
       
  1903    apply (case_tac [!] m, auto)
       
  1904   done
       
  1905 
       
  1906 lemma bin_clr_le: "bin_sc n False w \<le> w"
       
  1907   apply (induct n arbitrary: w)
       
  1908    apply (case_tac [!] w rule: bin_exhaust)
       
  1909    apply (auto simp: le_Bits)
       
  1910   done
       
  1911 
       
  1912 lemma bin_set_ge: "bin_sc n True w \<ge> w"
       
  1913   apply (induct n arbitrary: w)
       
  1914    apply (case_tac [!] w rule: bin_exhaust)
       
  1915    apply (auto simp: le_Bits)
       
  1916   done
       
  1917 
       
  1918 lemma bintr_bin_clr_le: "bintrunc n (bin_sc m False w) \<le> bintrunc n w"
       
  1919   apply (induct n arbitrary: w m)
       
  1920    apply simp
       
  1921   apply (case_tac w rule: bin_exhaust)
       
  1922   apply (case_tac m)
       
  1923    apply (auto simp: le_Bits)
       
  1924   done
       
  1925 
       
  1926 lemma bintr_bin_set_ge: "bintrunc n (bin_sc m True w) \<ge> bintrunc n w"
       
  1927   apply (induct n arbitrary: w m)
       
  1928    apply simp
       
  1929   apply (case_tac w rule: bin_exhaust)
       
  1930   apply (case_tac m)
       
  1931    apply (auto simp: le_Bits)
       
  1932   done
       
  1933 
       
  1934 lemma bin_sc_FP [simp]: "bin_sc n False 0 = 0"
       
  1935   by (induct n) auto
       
  1936 
       
  1937 lemma bin_sc_TM [simp]: "bin_sc n True (- 1) = - 1"
       
  1938   by (induct n) auto
       
  1939 
       
  1940 lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP
       
  1941 
       
  1942 lemma bin_sc_minus: "0 < n \<Longrightarrow> bin_sc (Suc (n - 1)) b w = bin_sc n b w"
       
  1943   by auto
       
  1944 
       
  1945 lemmas bin_sc_Suc_minus =
       
  1946   trans [OF bin_sc_minus [symmetric] bin_sc.Suc]
       
  1947 
       
  1948 lemma bin_sc_numeral [simp]:
       
  1949   "bin_sc (numeral k) b w =
       
  1950     bin_sc (pred_numeral k) b (bin_rest w) BIT bin_last w"
       
  1951   by (simp add: numeral_eq_Suc)
       
  1952 
       
  1953 instantiation int :: bits
       
  1954 begin
       
  1955 
       
  1956 definition [iff]: "i !! n \<longleftrightarrow> bin_nth i n"
       
  1957 
       
  1958 definition "lsb i = i !! 0" for i :: int
       
  1959 
       
  1960 definition "set_bit i n b = bin_sc n b i"
       
  1961 
       
  1962 definition
       
  1963   "set_bits f =
       
  1964     (if \<exists>n. \<forall>n'\<ge>n. \<not> f n' then
       
  1965       let n = LEAST n. \<forall>n'\<ge>n. \<not> f n'
       
  1966       in bl_to_bin (rev (map f [0..<n]))
       
  1967      else if \<exists>n. \<forall>n'\<ge>n. f n' then
       
  1968       let n = LEAST n. \<forall>n'\<ge>n. f n'
       
  1969       in sbintrunc n (bl_to_bin (True # rev (map f [0..<n])))
       
  1970      else 0 :: int)"
       
  1971 
       
  1972 definition "shiftl x n = x * 2 ^ n" for x :: int
       
  1973 
       
  1974 definition "shiftr x n = x div 2 ^ n" for x :: int
       
  1975 
       
  1976 definition "msb x \<longleftrightarrow> x < 0" for x :: int
       
  1977 
       
  1978 instance ..
       
  1979 
       
  1980 end
       
  1981 
  1975 
  1982 lemma int_lsb_BIT [simp]: fixes x :: int shows
  1976 lemma int_lsb_BIT [simp]: fixes x :: int shows
  1983   "lsb (x BIT b) \<longleftrightarrow> b"
  1977   "lsb (x BIT b) \<longleftrightarrow> b"
  1984 by(simp add: lsb_int_def)
  1978 by(simp add: lsb_int_def)
  1985 
  1979