| 20866 |      1 | (*  Title:      HOL/ex/Hex_Bin_Examples.thy
 | 
|  |      2 |     Author:     Gerwin Klein, NICTA
 | 
|  |      3 | *)
 | 
|  |      4 | 
 | 
| 61343 |      5 | section \<open>Examples for hexadecimal and binary numerals\<close>
 | 
| 20866 |      6 | 
 | 
|  |      7 | theory Hex_Bin_Examples imports Main 
 | 
|  |      8 | begin
 | 
|  |      9 | 
 | 
|  |     10 | 
 | 
|  |     11 | text "Hex and bin numerals can be used like normal decimal numerals in input"
 | 
|  |     12 | lemma "0xFF = 255" by (rule refl)
 | 
|  |     13 | lemma "0xF = 0b1111" by (rule refl)
 | 
|  |     14 | 
 | 
| 61343 |     15 | text \<open>
 | 
| 20866 |     16 |   Just like decimal numeral they are polymorphic, for arithmetic 
 | 
|  |     17 |   they need to be constrained
 | 
| 61343 |     18 | \<close>
 | 
| 20866 |     19 | lemma "0x0A + 0x10 = (0x1A :: nat)" by simp
 | 
|  |     20 | 
 | 
|  |     21 | text "The number of leading zeros is irrelevant"
 | 
|  |     22 | lemma "0b00010000 = 0x10" by (rule refl) 
 | 
|  |     23 | 
 | 
|  |     24 | text "Unary minus works as for decimal numerals"
 | 
|  |     25 | lemma "- 0x0A = - 10" by (rule refl)
 | 
|  |     26 | 
 | 
| 61343 |     27 | text \<open>
 | 
| 69597 |     28 |   Hex and bin numerals are printed as decimal: \<^term>\<open>0b10\<close>
 | 
| 61343 |     29 | \<close>
 | 
| 20866 |     30 | term "0b10"
 | 
|  |     31 | term "0x0A"
 | 
|  |     32 | 
 | 
| 61343 |     33 | text \<open>
 | 
| 20866 |     34 |   The numerals 0 and 1 are syntactically different from the 
 | 
|  |     35 |   constants 0 and 1. For the usual numeric types, their values 
 | 
|  |     36 |   are the same, though.
 | 
| 61343 |     37 | \<close>
 | 
| 20866 |     38 | lemma "0x01 = 1" oops 
 | 
|  |     39 | lemma "0x00 = 0" oops 
 | 
|  |     40 | 
 | 
|  |     41 | lemma "0x01 = (1::nat)" by simp
 | 
|  |     42 | lemma "0b0000 = (0::int)" by simp
 | 
|  |     43 | 
 | 
|  |     44 | 
 | 
|  |     45 | end
 | 
|  |     46 | 
 |