moved stuff to Char_nat.thy
authorhaftmann
Thu Apr 26 13:33:15 2007 +0200 (2007-04-26)
changeset 228051166a966e7b4
parent 22804 d3c23b90c6c6
child 22806 45ac82e7b887
moved stuff to Char_nat.thy
src/HOL/Library/Char_ord.thy
     1.1 --- a/src/HOL/Library/Char_ord.thy	Thu Apr 26 13:33:12 2007 +0200
     1.2 +++ b/src/HOL/Library/Char_ord.thy	Thu Apr 26 13:33:15 2007 +0200
     1.3 @@ -1,110 +1,63 @@
     1.4  (*  Title:      HOL/Library/Char_ord.thy
     1.5      ID:         $Id$
     1.6 -    Author:     Norbert Voelker
     1.7 +    Author:     Norbert Voelker, Florian Haftmann
     1.8  *)
     1.9  
    1.10  header {* Order on characters *}
    1.11  
    1.12  theory Char_ord
    1.13 -imports Product_ord
    1.14 +imports Product_ord Char_nat
    1.15  begin
    1.16  
    1.17 -text {* Conversions between nibbles and integers in [0..15]. *}
    1.18 -
    1.19 -fun
    1.20 -  nibble_to_int:: "nibble \<Rightarrow> int" where
    1.21 -  "nibble_to_int Nibble0 = 0"
    1.22 -  | "nibble_to_int Nibble1 = 1"
    1.23 -  | "nibble_to_int Nibble2 = 2"
    1.24 -  | "nibble_to_int Nibble3 = 3"
    1.25 -  | "nibble_to_int Nibble4 = 4"
    1.26 -  | "nibble_to_int Nibble5 = 5"
    1.27 -  | "nibble_to_int Nibble6 = 6"
    1.28 -  | "nibble_to_int Nibble7 = 7"
    1.29 -  | "nibble_to_int Nibble8 = 8"
    1.30 -  | "nibble_to_int Nibble9 = 9"
    1.31 -  | "nibble_to_int NibbleA = 10"
    1.32 -  | "nibble_to_int NibbleB = 11"
    1.33 -  | "nibble_to_int NibbleC = 12"
    1.34 -  | "nibble_to_int NibbleD = 13"
    1.35 -  | "nibble_to_int NibbleE = 14"
    1.36 -  | "nibble_to_int NibbleF = 15"
    1.37 +instance nibble :: linorder
    1.38 +  nibble_less_eq_def: "n \<le> m \<equiv> nat_of_nibble n \<le> nat_of_nibble m"
    1.39 +  nibble_less_def: "n < m \<equiv> nat_of_nibble n < nat_of_nibble m"
    1.40 +proof
    1.41 +  fix n :: nibble show "n \<le> n" unfolding nibble_less_eq_def nibble_less_def by auto
    1.42 +next
    1.43 +  fix n m q :: nibble
    1.44 +  assume "n \<le> m"
    1.45 +  and "m \<le> q"
    1.46 +  then show "n \<le> q" unfolding nibble_less_eq_def nibble_less_def by auto
    1.47 +next
    1.48 +  fix n m :: nibble
    1.49 +  assume "n \<le> m"
    1.50 +  and "m \<le> n"
    1.51 +  then show "n = m" unfolding nibble_less_eq_def nibble_less_def by (auto simp add: nat_of_nibble_eq)
    1.52 +next
    1.53 +  fix n m :: nibble
    1.54 +  show "n < m \<longleftrightarrow> n \<le> m \<and> n \<noteq> m"
    1.55 +  unfolding nibble_less_eq_def nibble_less_def less_le by (auto simp add: nat_of_nibble_eq)
    1.56 +next
    1.57 +  fix n m :: nibble
    1.58 +  show "n \<le> m \<or> m \<le> n"
    1.59 +  unfolding nibble_less_eq_def by auto
    1.60 +qed
    1.61  
    1.62 -definition
    1.63 -  int_to_nibble :: "int \<Rightarrow> nibble" where
    1.64 -  "int_to_nibble x = (let y = x mod 16 in
    1.65 -    if y = 0 then Nibble0 else
    1.66 -    if y = 1 then Nibble1 else
    1.67 -    if y = 2 then Nibble2 else
    1.68 -    if y = 3 then Nibble3 else
    1.69 -    if y = 4 then Nibble4 else
    1.70 -    if y = 5 then Nibble5 else
    1.71 -    if y = 6 then Nibble6 else
    1.72 -    if y = 7 then Nibble7 else
    1.73 -    if y = 8 then Nibble8 else
    1.74 -    if y = 9 then Nibble9 else
    1.75 -    if y = 10 then NibbleA else
    1.76 -    if y = 11 then NibbleB else
    1.77 -    if y = 12 then NibbleC else
    1.78 -    if y = 13 then NibbleD else
    1.79 -    if y = 14 then NibbleE else
    1.80 -    NibbleF)"
    1.81 -
    1.82 -lemma int_to_nibble_nibble_to_int: "int_to_nibble (nibble_to_int x) = x"
    1.83 -  by (cases x) (auto simp: int_to_nibble_def Let_def)
    1.84 -
    1.85 -lemma inj_nibble_to_int: "inj nibble_to_int"
    1.86 -  by (rule inj_on_inverseI) (rule int_to_nibble_nibble_to_int)
    1.87 -
    1.88 -lemmas nibble_to_int_eq = inj_nibble_to_int [THEN inj_eq]
    1.89 -
    1.90 -lemma nibble_to_int_ge_0: "0 \<le> nibble_to_int x"
    1.91 -  by (cases x) auto
    1.92 -
    1.93 -lemma nibble_to_int_less_16: "nibble_to_int x < 16"
    1.94 -  by (cases x) auto
    1.95 -
    1.96 -text {* Conversion between chars and int pairs. *}
    1.97 -
    1.98 -fun
    1.99 -  char_to_int_pair :: "char \<Rightarrow> int \<times> int" where
   1.100 -  "char_to_int_pair (Char a b) = (nibble_to_int a, nibble_to_int b)"
   1.101 -
   1.102 -lemma inj_char_to_int_pair: "inj char_to_int_pair"
   1.103 -  apply (rule inj_onI)
   1.104 -  apply (case_tac x, case_tac y)
   1.105 -  apply (auto simp: nibble_to_int_eq)
   1.106 -  done
   1.107 -
   1.108 -lemmas char_to_int_pair_eq = inj_char_to_int_pair [THEN inj_eq]
   1.109 -
   1.110 -
   1.111 -text {* Instantiation of order classes *}
   1.112 -
   1.113 -instance char :: ord
   1.114 -  char_le_def: "c \<le> d \<equiv> (char_to_int_pair c \<le> char_to_int_pair d)"
   1.115 -  char_less_def: "c < d \<equiv> (char_to_int_pair c < char_to_int_pair d)"  ..
   1.116 -
   1.117 -lemmas char_ord_defs = char_less_def char_le_def
   1.118 -
   1.119 -instance char :: order
   1.120 -  by default (auto simp: char_ord_defs char_to_int_pair_eq order_less_le)
   1.121 +instance nibble :: distrib_lattice
   1.122 +  "inf \<equiv> min"
   1.123 +  "sup \<equiv> max"
   1.124 +  by default
   1.125 +    (auto simp add: inf_nibble_def sup_nibble_def min_max.sup_inf_distrib1)
   1.126  
   1.127  instance char :: linorder
   1.128 -  by default (auto simp: char_le_def)
   1.129 +  char_less_eq_def: "c1 \<le> c2 \<equiv> case c1 of Char n1 m1 \<Rightarrow> case c2 of Char n2 m2 \<Rightarrow>
   1.130 +    n1 < n2 \<or> n1 = n2 \<and> m1 \<le> m2"
   1.131 +  char_less_def:    "c1 < c2 \<equiv> case c1 of Char n1 m1 \<Rightarrow> case c2 of Char n2 m2 \<Rightarrow>
   1.132 +    n1 < n2 \<or> n1 = n2 \<and> m1 < m2"
   1.133 +  by default (auto simp: char_less_eq_def char_less_def split: char.splits)
   1.134 +
   1.135 +lemmas [code nofunc] = char_less_eq_def char_less_def
   1.136  
   1.137  instance char :: distrib_lattice
   1.138    "inf \<equiv> min"
   1.139    "sup \<equiv> max"
   1.140 -  by intro_classes
   1.141 +  by default
   1.142      (auto simp add: inf_char_def sup_char_def min_max.sup_inf_distrib1)
   1.143  
   1.144 -
   1.145 -text {* code generator setup *}
   1.146 -
   1.147 -code_const char_to_int_pair
   1.148 -  (SML "raise/ Fail/ \"char'_to'_int'_pair\"")
   1.149 -  (OCaml "failwith \"char'_to'_int'_pair\"")
   1.150 -  (Haskell "error/ \"char'_to'_int'_pair\"")
   1.151 +lemma [simp, code func]:
   1.152 +  shows char_less_eq_simp: "Char n1 m1 \<le> Char n2 m2 \<longleftrightarrow> n1 < n2 \<or> n1 = n2 \<and> m1 \<le> m2"
   1.153 +  and char_less_simp:      "Char n1 m1 < Char n2 m2 \<longleftrightarrow> n1 < n2 \<or> n1 = n2 \<and> m1 < m2"
   1.154 +  unfolding char_less_eq_def char_less_def by simp_all
   1.155  
   1.156  end