
nipkow@15737: (*  Title:      HOL/Library/Char_ord.thy
nipkow@15737:     ID:         $Id$
nipkow@15737:     Author:     Norbert Voelker
nipkow@15737: *)
nipkow@15737: 
wenzelm@17200: header {* Order on characters *}
nipkow@15737: 
nipkow@15737: theory Char_ord
nipkow@15737: imports Product_ord
nipkow@15737: begin
nipkow@15737: 
wenzelm@17200: text {* Conversions between nibbles and integers in [0..15]. *}
nipkow@15737: 
wenzelm@17200: consts
nipkow@15737:   nibble_to_int:: "nibble \<Rightarrow> int"
wenzelm@17200: primrec
wenzelm@17200:   "nibble_to_int Nibble0 = 0"
wenzelm@17200:   "nibble_to_int Nibble1 = 1"
wenzelm@17200:   "nibble_to_int Nibble2 = 2"
wenzelm@17200:   "nibble_to_int Nibble3 = 3"
wenzelm@17200:   "nibble_to_int Nibble4 = 4"
wenzelm@17200:   "nibble_to_int Nibble5 = 5"
wenzelm@17200:   "nibble_to_int Nibble6 = 6"
wenzelm@17200:   "nibble_to_int Nibble7 = 7"
wenzelm@17200:   "nibble_to_int Nibble8 = 8"
wenzelm@17200:   "nibble_to_int Nibble9 = 9"
wenzelm@17200:   "nibble_to_int NibbleA = 10"
wenzelm@17200:   "nibble_to_int NibbleB = 11"
wenzelm@17200:   "nibble_to_int NibbleC = 12"
wenzelm@17200:   "nibble_to_int NibbleD = 13"
wenzelm@17200:   "nibble_to_int NibbleE = 14"
nipkow@15737:   "nibble_to_int NibbleF = 15"
nipkow@15737: 
wenzelm@19736: definition
wenzelm@21404:   int_to_nibble :: "int \<Rightarrow> nibble" where
wenzelm@21404:   "int_to_nibble x = (let y = x mod 16 in
wenzelm@19736:     if y = 0 then Nibble0 else
wenzelm@19736:     if y = 1 then Nibble1 else
wenzelm@19736:     if y = 2 then Nibble2 else
wenzelm@19736:     if y = 3 then Nibble3 else
wenzelm@19736:     if y = 4 then Nibble4 else
wenzelm@19736:     if y = 5 then Nibble5 else
wenzelm@19736:     if y = 6 then Nibble6 else
wenzelm@19736:     if y = 7 then Nibble7 else
wenzelm@19736:     if y = 8 then Nibble8 else
wenzelm@19736:     if y = 9 then Nibble9 else
wenzelm@19736:     if y = 10 then NibbleA else
wenzelm@19736:     if y = 11 then NibbleB else
wenzelm@19736:     if y = 12 then NibbleC else
wenzelm@19736:     if y = 13 then NibbleD else
wenzelm@19736:     if y = 14 then NibbleE else
wenzelm@19736:     NibbleF)"
nipkow@15737: 
nipkow@15737: lemma int_to_nibble_nibble_to_int: "int_to_nibble(nibble_to_int x) = x"
wenzelm@17200:   by (cases x) (auto simp: int_to_nibble_def Let_def)
nipkow@15737: 
nipkow@15737: lemma inj_nibble_to_int: "inj nibble_to_int"
wenzelm@17200:   by (rule inj_on_inverseI) (rule int_to_nibble_nibble_to_int)
nipkow@15737: 
nipkow@15737: lemmas nibble_to_int_eq = inj_nibble_to_int [THEN inj_eq]
nipkow@15737: 
nipkow@15737: lemma nibble_to_int_ge_0: "0 \<le> nibble_to_int x"
wenzelm@17200:   by (cases x) auto
nipkow@15737: 
nipkow@15737: lemma nibble_to_int_less_16: "nibble_to_int x < 16"
wenzelm@17200:   by (cases x) auto
nipkow@15737: 
nipkow@15737: text {* Conversion between chars and int pairs. *}
nipkow@15737: 
wenzelm@17200: consts
nipkow@15737:   char_to_int_pair :: "char \<Rightarrow> int \<times> int"
nipkow@15737: primrec
wenzelm@17200:   "char_to_int_pair (Char a b) = (nibble_to_int a, nibble_to_int b)"
nipkow@15737: 
nipkow@15737: lemma inj_char_to_int_pair: "inj char_to_int_pair"
wenzelm@17200:   apply (rule inj_onI)
wenzelm@17200:   apply (case_tac x, case_tac y)
wenzelm@17200:   apply (auto simp: nibble_to_int_eq)
wenzelm@17200:   done
nipkow@15737: 
nipkow@15737: lemmas char_to_int_pair_eq = inj_char_to_int_pair [THEN inj_eq]
nipkow@15737: 
wenzelm@17200: text {* Instantiation of order classes *}
nipkow@15737: 
nipkow@15737: instance char :: ord ..
nipkow@15737: 
nipkow@15737: defs (overloaded)
wenzelm@17200:   char_le_def: "c \<le> d \<equiv> (char_to_int_pair c \<le> char_to_int_pair d)"
wenzelm@17200:   char_less_def: "c < d \<equiv> (char_to_int_pair c < char_to_int_pair d)"
nipkow@15737: 
nipkow@15737: lemmas char_ord_defs = char_less_def char_le_def
nipkow@15737: 
wenzelm@17200: instance char :: order
wenzelm@19736:   by default (auto simp: char_ord_defs char_to_int_pair_eq order_less_le)
nipkow@15737: 
wenzelm@19736: instance char :: linorder
wenzelm@19736:   by default (auto simp: char_le_def)
nipkow@15737: 
wenzelm@17200: end




