src/HOL/Code_Numeral.thy
changeset 74101 d804e93ae9ff
parent 71965 d45f5d4c41bd
child 74108 3146646a43a7
equal deleted inserted replaced
74100:fb9c119e5b49 74101:d804e93ae9ff
     3 *)
     3 *)
     4 
     4 
     5 section \<open>Numeric types for code generation onto target language numerals only\<close>
     5 section \<open>Numeric types for code generation onto target language numerals only\<close>
     6 
     6 
     7 theory Code_Numeral
     7 theory Code_Numeral
     8 imports Divides Lifting
     8 imports Divides Lifting Bit_Operations
     9 begin
     9 begin
    10 
    10 
    11 subsection \<open>Type of target language integers\<close>
    11 subsection \<open>Type of target language integers\<close>
    12 
    12 
    13 typedef integer = "UNIV :: int set"
    13 typedef integer = "UNIV :: int set"
  1201 lemma [code]: "m < n \<longleftrightarrow> integer_of_natural m < integer_of_natural n"
  1201 lemma [code]: "m < n \<longleftrightarrow> integer_of_natural m < integer_of_natural n"
  1202   by transfer simp
  1202   by transfer simp
  1203 
  1203 
  1204 hide_const (open) Nat
  1204 hide_const (open) Nat
  1205 
  1205 
  1206 lifting_update integer.lifting
       
  1207 lifting_forget integer.lifting
       
  1208 
       
  1209 lifting_update natural.lifting
       
  1210 lifting_forget natural.lifting
       
  1211 
       
  1212 code_reflect Code_Numeral
  1206 code_reflect Code_Numeral
  1213   datatypes natural
  1207   datatypes natural
  1214   functions "Code_Numeral.Suc" "0 :: natural" "1 :: natural"
  1208   functions "Code_Numeral.Suc" "0 :: natural" "1 :: natural"
  1215     "plus :: natural \<Rightarrow> _" "minus :: natural \<Rightarrow> _"
  1209     "plus :: natural \<Rightarrow> _" "minus :: natural \<Rightarrow> _"
  1216     "times :: natural \<Rightarrow> _" "divide :: natural \<Rightarrow> _"
  1210     "times :: natural \<Rightarrow> _" "divide :: natural \<Rightarrow> _"
  1217     "modulo :: natural \<Rightarrow> _"
  1211     "modulo :: natural \<Rightarrow> _"
  1218     integer_of_natural natural_of_integer
  1212     integer_of_natural natural_of_integer
  1219 
  1213 
  1220 end
  1214 
       
  1215 subsection \<open>Bit operations\<close>
       
  1216 
       
  1217 instantiation integer :: ring_bit_operations
       
  1218 begin
       
  1219 
       
  1220 lift_definition not_integer :: \<open>integer \<Rightarrow> integer\<close>
       
  1221   is not .
       
  1222 
       
  1223 lift_definition and_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close>
       
  1224   is \<open>and\<close> .
       
  1225 
       
  1226 lift_definition or_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close>
       
  1227   is or .
       
  1228 
       
  1229 lift_definition xor_integer ::  \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close>
       
  1230   is xor .
       
  1231 
       
  1232 lift_definition mask_integer :: \<open>nat \<Rightarrow> integer\<close>
       
  1233   is mask .
       
  1234 
       
  1235 lift_definition set_bit_integer :: \<open>nat \<Rightarrow> integer \<Rightarrow> integer\<close>
       
  1236   is set_bit .
       
  1237 
       
  1238 lift_definition unset_bit_integer :: \<open>nat \<Rightarrow> integer \<Rightarrow> integer\<close>
       
  1239   is unset_bit .
       
  1240 
       
  1241 lift_definition flip_bit_integer :: \<open>nat \<Rightarrow> integer \<Rightarrow> integer\<close>
       
  1242   is flip_bit .
       
  1243 
       
  1244 instance by (standard; transfer)
       
  1245   (simp_all add: minus_eq_not_minus_1 mask_eq_exp_minus_1
       
  1246     bit_not_iff bit_and_iff bit_or_iff bit_xor_iff
       
  1247     set_bit_def bit_unset_bit_iff flip_bit_def)
       
  1248 
       
  1249 end
       
  1250 
       
  1251 lemma [code]:
       
  1252   \<open>mask n = 2 ^ n - (1::integer)\<close>
       
  1253   by (simp add: mask_eq_exp_minus_1)
       
  1254 
       
  1255 instantiation natural :: semiring_bit_operations
       
  1256 begin
       
  1257 
       
  1258 lift_definition and_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close>
       
  1259   is \<open>and\<close> .
       
  1260 
       
  1261 lift_definition or_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close>
       
  1262   is or .
       
  1263 
       
  1264 lift_definition xor_natural ::  \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close>
       
  1265   is xor .
       
  1266 
       
  1267 lift_definition mask_natural :: \<open>nat \<Rightarrow> natural\<close>
       
  1268   is mask .
       
  1269 
       
  1270 lift_definition set_bit_natural :: \<open>nat \<Rightarrow> natural \<Rightarrow> natural\<close>
       
  1271   is set_bit .
       
  1272 
       
  1273 lift_definition unset_bit_natural :: \<open>nat \<Rightarrow> natural \<Rightarrow> natural\<close>
       
  1274   is unset_bit .
       
  1275 
       
  1276 lift_definition flip_bit_natural :: \<open>nat \<Rightarrow> natural \<Rightarrow> natural\<close>
       
  1277   is flip_bit .
       
  1278 
       
  1279 instance by (standard; transfer)
       
  1280   (simp_all add: mask_eq_exp_minus_1
       
  1281     bit_and_iff bit_or_iff bit_xor_iff
       
  1282     set_bit_def bit_unset_bit_iff flip_bit_def)
       
  1283 
       
  1284 end
       
  1285 
       
  1286 lemma [code]:
       
  1287   \<open>integer_of_natural (mask n) = mask n\<close>
       
  1288   by transfer (simp add: mask_eq_exp_minus_1 of_nat_diff)
       
  1289 
       
  1290 
       
  1291 lifting_update integer.lifting
       
  1292 lifting_forget integer.lifting
       
  1293 
       
  1294 lifting_update natural.lifting
       
  1295 lifting_forget natural.lifting
       
  1296 
       
  1297 end