src/HOL/Library/Char_ord.thy
author wenzelm
Wed Aug 31 15:46:37 2005 +0200 (2005-08-31)
changeset 17200 3a4d03d1a31b
parent 15737 c7e522520910
child 19736 d8d0f8f51d69
permissions -rw-r--r--
tuned presentation;
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"
nipkow@15737
    16
  int_to_nibble:: "int \<Rightarrow> nibble"
nipkow@15737
    17
wenzelm@17200
    18
primrec
wenzelm@17200
    19
  "nibble_to_int Nibble0 = 0"
wenzelm@17200
    20
  "nibble_to_int Nibble1 = 1"
wenzelm@17200
    21
  "nibble_to_int Nibble2 = 2"
wenzelm@17200
    22
  "nibble_to_int Nibble3 = 3"
wenzelm@17200
    23
  "nibble_to_int Nibble4 = 4"
wenzelm@17200
    24
  "nibble_to_int Nibble5 = 5"
wenzelm@17200
    25
  "nibble_to_int Nibble6 = 6"
wenzelm@17200
    26
  "nibble_to_int Nibble7 = 7"
wenzelm@17200
    27
  "nibble_to_int Nibble8 = 8"
wenzelm@17200
    28
  "nibble_to_int Nibble9 = 9"
wenzelm@17200
    29
  "nibble_to_int NibbleA = 10"
wenzelm@17200
    30
  "nibble_to_int NibbleB = 11"
wenzelm@17200
    31
  "nibble_to_int NibbleC = 12"
wenzelm@17200
    32
  "nibble_to_int NibbleD = 13"
wenzelm@17200
    33
  "nibble_to_int NibbleE = 14"
nipkow@15737
    34
  "nibble_to_int NibbleF = 15"
nipkow@15737
    35
wenzelm@17200
    36
defs
wenzelm@17200
    37
  int_to_nibble_def:
wenzelm@17200
    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"
wenzelm@17200
    57
  by (cases x) (auto simp: int_to_nibble_def Let_def)
nipkow@15737
    58
nipkow@15737
    59
lemma inj_nibble_to_int: "inj nibble_to_int"
wenzelm@17200
    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"
wenzelm@17200
    65
  by (cases x) auto
nipkow@15737
    66
nipkow@15737
    67
lemma nibble_to_int_less_16: "nibble_to_int x < 16"
wenzelm@17200
    68
  by (cases x) auto
nipkow@15737
    69
nipkow@15737
    70
text {* Conversion between chars and int pairs. *}
nipkow@15737
    71
wenzelm@17200
    72
consts
nipkow@15737
    73
  char_to_int_pair :: "char \<Rightarrow> int \<times> int"
nipkow@15737
    74
primrec
wenzelm@17200
    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"
wenzelm@17200
    78
  apply (rule inj_onI)
wenzelm@17200
    79
  apply (case_tac x, case_tac y)
wenzelm@17200
    80
  apply (auto simp: nibble_to_int_eq)
wenzelm@17200
    81
  done
nipkow@15737
    82
nipkow@15737
    83
lemmas char_to_int_pair_eq = inj_char_to_int_pair [THEN inj_eq]
nipkow@15737
    84
wenzelm@17200
    85
text {* Instantiation of order classes *}
nipkow@15737
    86
nipkow@15737
    87
instance char :: ord ..
nipkow@15737
    88
nipkow@15737
    89
defs (overloaded)
wenzelm@17200
    90
  char_le_def: "c \<le> d \<equiv> (char_to_int_pair c \<le> char_to_int_pair d)"
wenzelm@17200
    91
  char_less_def: "c < d \<equiv> (char_to_int_pair c < char_to_int_pair d)"
nipkow@15737
    92
nipkow@15737
    93
lemmas char_ord_defs = char_less_def char_le_def
nipkow@15737
    94
wenzelm@17200
    95
instance char :: order
wenzelm@17200
    96
  apply intro_classes
wenzelm@17200
    97
     apply (unfold char_ord_defs)
wenzelm@17200
    98
     apply (auto simp: char_to_int_pair_eq order_less_le)
wenzelm@17200
    99
  done
nipkow@15737
   100
nipkow@15737
   101
instance char::linorder
wenzelm@17200
   102
  apply intro_classes
wenzelm@17200
   103
  apply (unfold char_le_def)
wenzelm@17200
   104
  apply auto
wenzelm@17200
   105
  done
nipkow@15737
   106
wenzelm@17200
   107
end