src/HOL/Library/Char_ord.thy
changeset 15737 c7e522520910
child 17200 3a4d03d1a31b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Char_ord.thy	Fri Apr 15 14:14:24 2005 +0200
     1.3 @@ -0,0 +1,99 @@
     1.4 +(*  Title:      HOL/Library/Char_ord.thy
     1.5 +    ID:         $Id$
     1.6 +    Author:     Norbert Voelker
     1.7 +*)
     1.8 +
     1.9 +header {* Instantiation of order classes type char *}
    1.10 +
    1.11 +theory Char_ord
    1.12 +imports Product_ord
    1.13 +begin
    1.14 +
    1.15 +text {* Conversions between nibbles and integers in [0..15]. *} 
    1.16 +
    1.17 +consts 
    1.18 +  nibble_to_int:: "nibble \<Rightarrow> int"
    1.19 +  int_to_nibble:: "int \<Rightarrow> nibble"
    1.20 +
    1.21 +primrec 
    1.22 +  "nibble_to_int Nibble0 = 0"  
    1.23 +  "nibble_to_int Nibble1 = 1" 
    1.24 +  "nibble_to_int Nibble2 = 2" 
    1.25 +  "nibble_to_int Nibble3 = 3" 
    1.26 +  "nibble_to_int Nibble4 = 4" 
    1.27 +  "nibble_to_int Nibble5 = 5" 
    1.28 +  "nibble_to_int Nibble6 = 6" 
    1.29 +  "nibble_to_int Nibble7 = 7" 
    1.30 +  "nibble_to_int Nibble8 = 8" 
    1.31 +  "nibble_to_int Nibble9 = 9" 
    1.32 +  "nibble_to_int NibbleA = 10" 
    1.33 +  "nibble_to_int NibbleB = 11" 
    1.34 +  "nibble_to_int NibbleC = 12" 
    1.35 +  "nibble_to_int NibbleD = 13" 
    1.36 +  "nibble_to_int NibbleE = 14" 
    1.37 +  "nibble_to_int NibbleF = 15"
    1.38 +
    1.39 +defs 
    1.40 +  int_to_nibble_def:  
    1.41 +    "int_to_nibble x \<equiv> (let y = x mod 16 in 
    1.42 +       if y = 0 then Nibble0 else
    1.43 +       if y = 1 then Nibble1 else
    1.44 +       if y = 2 then Nibble2 else
    1.45 +       if y = 3 then Nibble3 else
    1.46 +       if y = 4 then Nibble4 else
    1.47 +       if y = 5 then Nibble5 else
    1.48 +       if y = 6 then Nibble6 else
    1.49 +       if y = 7 then Nibble7 else
    1.50 +       if y = 8 then Nibble8 else
    1.51 +       if y = 9 then Nibble9 else
    1.52 +       if y = 10 then NibbleA else
    1.53 +       if y = 11 then NibbleB else
    1.54 +       if y = 12 then NibbleC else
    1.55 +       if y = 13 then NibbleD else
    1.56 +       if y = 14 then NibbleE else
    1.57 +       NibbleF)"
    1.58 +
    1.59 +lemma int_to_nibble_nibble_to_int: "int_to_nibble(nibble_to_int x) = x"
    1.60 +  by (case_tac x, auto simp: int_to_nibble_def Let_def)
    1.61 +
    1.62 +lemma inj_nibble_to_int: "inj nibble_to_int"
    1.63 +  by (rule inj_on_inverseI, rule int_to_nibble_nibble_to_int)
    1.64 +
    1.65 +lemmas nibble_to_int_eq = inj_nibble_to_int [THEN inj_eq]
    1.66 +
    1.67 +lemma nibble_to_int_ge_0: "0 \<le> nibble_to_int x"
    1.68 +  by (case_tac x, auto)
    1.69 +
    1.70 +lemma nibble_to_int_less_16: "nibble_to_int x < 16"
    1.71 +  by (case_tac x, auto)
    1.72 +
    1.73 +text {* Conversion between chars and int pairs. *}
    1.74 +
    1.75 +consts 
    1.76 +  char_to_int_pair :: "char \<Rightarrow> int \<times> int"
    1.77 +primrec
    1.78 +  "char_to_int_pair(Char a b) = (nibble_to_int a, nibble_to_int b)" 
    1.79 +
    1.80 +lemma inj_char_to_int_pair: "inj char_to_int_pair"
    1.81 +  by (rule inj_onI, case_tac x, case_tac y, auto simp: nibble_to_int_eq)
    1.82 +
    1.83 +lemmas char_to_int_pair_eq = inj_char_to_int_pair [THEN inj_eq]
    1.84 +
    1.85 +text {* Instantiation of order classes *} 
    1.86 +
    1.87 +instance char :: ord ..
    1.88 +
    1.89 +defs (overloaded)
    1.90 +  char_le_def:  "c \<le> d \<equiv> (char_to_int_pair c \<le> char_to_int_pair d)" 
    1.91 +  char_less_def: "c < d \<equiv> (char_to_int_pair c < char_to_int_pair d)" 
    1.92 +
    1.93 +lemmas char_ord_defs = char_less_def char_le_def
    1.94 +
    1.95 +instance char::order
    1.96 +  apply (intro_classes, unfold char_ord_defs)
    1.97 +  by (auto simp: char_to_int_pair_eq order_less_le)
    1.98 +
    1.99 +instance char::linorder
   1.100 +  by (intro_classes, unfold char_le_def, auto)
   1.101 +
   1.102 +end
   1.103 \ No newline at end of file