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