src/HOL/Library/Char_ord.thy
author haftmann
Tue Mar 20 15:52:40 2007 +0100 (2007-03-20)
changeset 22483 86064f2f2188
parent 21911 e29bcab0c81c
child 22805 1166a966e7b4
permissions -rw-r--r--
added instance for lattice
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
haftmann@22483
    14
fun
haftmann@22483
    15
  nibble_to_int:: "nibble \<Rightarrow> int" where
wenzelm@17200
    16
  "nibble_to_int Nibble0 = 0"
haftmann@22483
    17
  | "nibble_to_int Nibble1 = 1"
haftmann@22483
    18
  | "nibble_to_int Nibble2 = 2"
haftmann@22483
    19
  | "nibble_to_int Nibble3 = 3"
haftmann@22483
    20
  | "nibble_to_int Nibble4 = 4"
haftmann@22483
    21
  | "nibble_to_int Nibble5 = 5"
haftmann@22483
    22
  | "nibble_to_int Nibble6 = 6"
haftmann@22483
    23
  | "nibble_to_int Nibble7 = 7"
haftmann@22483
    24
  | "nibble_to_int Nibble8 = 8"
haftmann@22483
    25
  | "nibble_to_int Nibble9 = 9"
haftmann@22483
    26
  | "nibble_to_int NibbleA = 10"
haftmann@22483
    27
  | "nibble_to_int NibbleB = 11"
haftmann@22483
    28
  | "nibble_to_int NibbleC = 12"
haftmann@22483
    29
  | "nibble_to_int NibbleD = 13"
haftmann@22483
    30
  | "nibble_to_int NibbleE = 14"
haftmann@22483
    31
  | "nibble_to_int NibbleF = 15"
nipkow@15737
    32
wenzelm@19736
    33
definition
wenzelm@21404
    34
  int_to_nibble :: "int \<Rightarrow> nibble" where
wenzelm@21404
    35
  "int_to_nibble x = (let y = x mod 16 in
wenzelm@19736
    36
    if y = 0 then Nibble0 else
wenzelm@19736
    37
    if y = 1 then Nibble1 else
wenzelm@19736
    38
    if y = 2 then Nibble2 else
wenzelm@19736
    39
    if y = 3 then Nibble3 else
wenzelm@19736
    40
    if y = 4 then Nibble4 else
wenzelm@19736
    41
    if y = 5 then Nibble5 else
wenzelm@19736
    42
    if y = 6 then Nibble6 else
wenzelm@19736
    43
    if y = 7 then Nibble7 else
wenzelm@19736
    44
    if y = 8 then Nibble8 else
wenzelm@19736
    45
    if y = 9 then Nibble9 else
wenzelm@19736
    46
    if y = 10 then NibbleA else
wenzelm@19736
    47
    if y = 11 then NibbleB else
wenzelm@19736
    48
    if y = 12 then NibbleC else
wenzelm@19736
    49
    if y = 13 then NibbleD else
wenzelm@19736
    50
    if y = 14 then NibbleE else
wenzelm@19736
    51
    NibbleF)"
nipkow@15737
    52
haftmann@22483
    53
lemma int_to_nibble_nibble_to_int: "int_to_nibble (nibble_to_int x) = x"
wenzelm@17200
    54
  by (cases x) (auto simp: int_to_nibble_def Let_def)
nipkow@15737
    55
nipkow@15737
    56
lemma inj_nibble_to_int: "inj nibble_to_int"
wenzelm@17200
    57
  by (rule inj_on_inverseI) (rule int_to_nibble_nibble_to_int)
nipkow@15737
    58
nipkow@15737
    59
lemmas nibble_to_int_eq = inj_nibble_to_int [THEN inj_eq]
nipkow@15737
    60
nipkow@15737
    61
lemma nibble_to_int_ge_0: "0 \<le> nibble_to_int x"
wenzelm@17200
    62
  by (cases x) auto
nipkow@15737
    63
nipkow@15737
    64
lemma nibble_to_int_less_16: "nibble_to_int x < 16"
wenzelm@17200
    65
  by (cases x) auto
nipkow@15737
    66
nipkow@15737
    67
text {* Conversion between chars and int pairs. *}
nipkow@15737
    68
haftmann@22483
    69
fun
haftmann@22483
    70
  char_to_int_pair :: "char \<Rightarrow> int \<times> int" where
wenzelm@17200
    71
  "char_to_int_pair (Char a b) = (nibble_to_int a, nibble_to_int b)"
nipkow@15737
    72
nipkow@15737
    73
lemma inj_char_to_int_pair: "inj char_to_int_pair"
wenzelm@17200
    74
  apply (rule inj_onI)
wenzelm@17200
    75
  apply (case_tac x, case_tac y)
wenzelm@17200
    76
  apply (auto simp: nibble_to_int_eq)
wenzelm@17200
    77
  done
nipkow@15737
    78
nipkow@15737
    79
lemmas char_to_int_pair_eq = inj_char_to_int_pair [THEN inj_eq]
nipkow@15737
    80
haftmann@22483
    81
wenzelm@17200
    82
text {* Instantiation of order classes *}
nipkow@15737
    83
haftmann@22483
    84
instance char :: ord
wenzelm@17200
    85
  char_le_def: "c \<le> d \<equiv> (char_to_int_pair c \<le> char_to_int_pair d)"
haftmann@22483
    86
  char_less_def: "c < d \<equiv> (char_to_int_pair c < char_to_int_pair d)"  ..
nipkow@15737
    87
nipkow@15737
    88
lemmas char_ord_defs = char_less_def char_le_def
nipkow@15737
    89
wenzelm@17200
    90
instance char :: order
wenzelm@19736
    91
  by default (auto simp: char_ord_defs char_to_int_pair_eq order_less_le)
nipkow@15737
    92
wenzelm@19736
    93
instance char :: linorder
wenzelm@19736
    94
  by default (auto simp: char_le_def)
nipkow@15737
    95
haftmann@22483
    96
instance char :: distrib_lattice
haftmann@22483
    97
  "inf \<equiv> min"
haftmann@22483
    98
  "sup \<equiv> max"
haftmann@22483
    99
  by intro_classes
haftmann@22483
   100
    (auto simp add: inf_char_def sup_char_def min_max.sup_inf_distrib1)
haftmann@22483
   101
haftmann@22483
   102
haftmann@22483
   103
text {* code generator setup *}
haftmann@22483
   104
haftmann@21871
   105
code_const char_to_int_pair
haftmann@21871
   106
  (SML "raise/ Fail/ \"char'_to'_int'_pair\"")
haftmann@21911
   107
  (OCaml "failwith \"char'_to'_int'_pair\"")
haftmann@21871
   108
  (Haskell "error/ \"char'_to'_int'_pair\"")
haftmann@21871
   109
wenzelm@17200
   110
end