src/HOL/Library/Char_ord.thy
author paulson
Tue Jun 28 15:27:45 2005 +0200 (2005-06-28)
changeset 16587 b34c8aa657a5
parent 15737 c7e522520910
child 17200 3a4d03d1a31b
permissions -rw-r--r--
Constant "If" is now local
     1 (*  Title:      HOL/Library/Char_ord.thy
     2     ID:         $Id$
     3     Author:     Norbert Voelker
     4 *)
     5 
     6 header {* Instantiation of order classes type char *}
     7 
     8 theory Char_ord
     9 imports Product_ord
    10 begin
    11 
    12 text {* Conversions between nibbles and integers in [0..15]. *} 
    13 
    14 consts 
    15   nibble_to_int:: "nibble \<Rightarrow> int"
    16   int_to_nibble:: "int \<Rightarrow> nibble"
    17 
    18 primrec 
    19   "nibble_to_int Nibble0 = 0"  
    20   "nibble_to_int Nibble1 = 1" 
    21   "nibble_to_int Nibble2 = 2" 
    22   "nibble_to_int Nibble3 = 3" 
    23   "nibble_to_int Nibble4 = 4" 
    24   "nibble_to_int Nibble5 = 5" 
    25   "nibble_to_int Nibble6 = 6" 
    26   "nibble_to_int Nibble7 = 7" 
    27   "nibble_to_int Nibble8 = 8" 
    28   "nibble_to_int Nibble9 = 9" 
    29   "nibble_to_int NibbleA = 10" 
    30   "nibble_to_int NibbleB = 11" 
    31   "nibble_to_int NibbleC = 12" 
    32   "nibble_to_int NibbleD = 13" 
    33   "nibble_to_int NibbleE = 14" 
    34   "nibble_to_int NibbleF = 15"
    35 
    36 defs 
    37   int_to_nibble_def:  
    38     "int_to_nibble x \<equiv> (let y = x mod 16 in 
    39        if y = 0 then Nibble0 else
    40        if y = 1 then Nibble1 else
    41        if y = 2 then Nibble2 else
    42        if y = 3 then Nibble3 else
    43        if y = 4 then Nibble4 else
    44        if y = 5 then Nibble5 else
    45        if y = 6 then Nibble6 else
    46        if y = 7 then Nibble7 else
    47        if y = 8 then Nibble8 else
    48        if y = 9 then Nibble9 else
    49        if y = 10 then NibbleA else
    50        if y = 11 then NibbleB else
    51        if y = 12 then NibbleC else
    52        if y = 13 then NibbleD else
    53        if y = 14 then NibbleE else
    54        NibbleF)"
    55 
    56 lemma int_to_nibble_nibble_to_int: "int_to_nibble(nibble_to_int x) = x"
    57   by (case_tac x, auto simp: int_to_nibble_def Let_def)
    58 
    59 lemma inj_nibble_to_int: "inj nibble_to_int"
    60   by (rule inj_on_inverseI, rule int_to_nibble_nibble_to_int)
    61 
    62 lemmas nibble_to_int_eq = inj_nibble_to_int [THEN inj_eq]
    63 
    64 lemma nibble_to_int_ge_0: "0 \<le> nibble_to_int x"
    65   by (case_tac x, auto)
    66 
    67 lemma nibble_to_int_less_16: "nibble_to_int x < 16"
    68   by (case_tac x, auto)
    69 
    70 text {* Conversion between chars and int pairs. *}
    71 
    72 consts 
    73   char_to_int_pair :: "char \<Rightarrow> int \<times> int"
    74 primrec
    75   "char_to_int_pair(Char a b) = (nibble_to_int a, nibble_to_int b)" 
    76 
    77 lemma inj_char_to_int_pair: "inj char_to_int_pair"
    78   by (rule inj_onI, case_tac x, case_tac y, auto simp: nibble_to_int_eq)
    79 
    80 lemmas char_to_int_pair_eq = inj_char_to_int_pair [THEN inj_eq]
    81 
    82 text {* Instantiation of order classes *} 
    83 
    84 instance char :: ord ..
    85 
    86 defs (overloaded)
    87   char_le_def:  "c \<le> d \<equiv> (char_to_int_pair c \<le> char_to_int_pair d)" 
    88   char_less_def: "c < d \<equiv> (char_to_int_pair c < char_to_int_pair d)" 
    89 
    90 lemmas char_ord_defs = char_less_def char_le_def
    91 
    92 instance char::order
    93   apply (intro_classes, unfold char_ord_defs)
    94   by (auto simp: char_to_int_pair_eq order_less_le)
    95 
    96 instance char::linorder
    97   by (intro_classes, unfold char_le_def, auto)
    98 
    99 end