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