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