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