# Theory Char_ord

Up to index of Isabelle/HOL/Library

theory Char_ord
imports Product_ord
begin

    (*  Title:      HOL/Library/Char_ord.thy
        ID:         $Id: Char_ord.thy,v 1.2 2005/08/31 13:46:37 wenzelm Exp $
        Author:     Norbert Voelker
    *)

    header {* Order on characters *}

    theory Char_ord
    imports Product_ord
    begin

    text {* Conversions between nibbles and integers in [0..15]. *}

    consts
      nibble_to_int:: "nibble => int"
      int_to_nibble:: "int => nibble"

    primrec
      "nibble_to_int Nibble0 = 0"
      "nibble_to_int Nibble1 = 1"
      "nibble_to_int Nibble2 = 2"
      "nibble_to_int Nibble3 = 3"
      "nibble_to_int Nibble4 = 4"
      "nibble_to_int Nibble5 = 5"
      "nibble_to_int Nibble6 = 6"
      "nibble_to_int Nibble7 = 7"
      "nibble_to_int Nibble8 = 8"
      "nibble_to_int Nibble9 = 9"
      "nibble_to_int NibbleA = 10"
      "nibble_to_int NibbleB = 11"
      "nibble_to_int NibbleC = 12"
      "nibble_to_int NibbleD = 13"
      "nibble_to_int NibbleE = 14"
      "nibble_to_int NibbleF = 15"

    defs
      int_to_nibble_def:
        "int_to_nibble x ≡ (let y = x mod 16 in
           if y = 0 then Nibble0 else
           if y = 1 then Nibble1 else
           if y = 2 then Nibble2 else
           if y = 3 then Nibble3 else
           if y = 4 then Nibble4 else
           if y = 5 then Nibble5 else
           if y = 6 then Nibble6 else
           if y = 7 then Nibble7 else
           if y = 8 then Nibble8 else
           if y = 9 then Nibble9 else
           if y = 10 then NibbleA else
           if y = 11 then NibbleB else
           if y = 12 then NibbleC else
           if y = 13 then NibbleD else
           if y = 14 then NibbleE else
           NibbleF)"

    lemma int_to_nibble_nibble_to_int: "int_to_nibble(nibble_to_int x) = x"
      by (cases x) (auto simp: int_to_nibble_def Let_def)

    lemma inj_nibble_to_int: "inj nibble_to_int"
      by (rule inj_on_inverseI) (rule int_to_nibble_nibble_to_int)

    lemmas nibble_to_int_eq = inj_nibble_to_int [THEN inj_eq]

    lemma nibble_to_int_ge_0: "0 ≤ nibble_to_int x"
      by (cases x) auto

    lemma nibble_to_int_less_16: "nibble_to_int x < 16"
      by (cases x) auto

    text {* Conversion between chars and int pairs. *}

    consts
      char_to_int_pair :: "char => int × int"
    primrec
      "char_to_int_pair (Char a b) = (nibble_to_int a, nibble_to_int b)"

    lemma inj_char_to_int_pair: "inj char_to_int_pair"
      apply (rule inj_onI)
      apply (case_tac x, case_tac y)
      apply (auto simp: nibble_to_int_eq)
      done

    lemmas char_to_int_pair_eq = inj_char_to_int_pair [THEN inj_eq]

    text {* Instantiation of order classes *}

    instance char :: ord ..

    defs (overloaded)
      char_le_def: "c ≤ d ≡ (char_to_int_pair c ≤ char_to_int_pair d)"
      char_less_def: "c < d ≡ (char_to_int_pair c < char_to_int_pair d)"

    lemmas char_ord_defs = char_less_def char_le_def

    instance char :: order
      apply intro_classes
         apply (unfold char_ord_defs)
         apply (auto simp: char_to_int_pair_eq order_less_le)
      done

    instance char::linorder
      apply intro_classes
      apply (unfold char_le_def)
      apply auto
      done

    end

lemma int_to_nibble_nibble_to_int:

      int_to_nibble (nibble_to_int x) = x

lemma inj_nibble_to_int:

      inj nibble_to_int

lemmas nibble_to_int_eq:

      (nibble_to_int x = nibble_to_int y) = (x = y)

lemmas nibble_to_int_eq:

      (nibble_to_int x = nibble_to_int y) = (x = y)

lemma nibble_to_int_ge_0:

      0 ≤ nibble_to_int x

lemma nibble_to_int_less_16:

      nibble_to_int x < 16

lemma inj_char_to_int_pair:

      inj char_to_int_pair

lemmas char_to_int_pair_eq:

      (char_to_int_pair x = char_to_int_pair y) = (x = y)

lemmas char_to_int_pair_eq:

      (char_to_int_pair x = char_to_int_pair y) = (x = y)

lemmas char_ord_defs:

      c < d == char_to_int_pair c < char_to_int_pair d

      c ≤ d == char_to_int_pair c ≤ char_to_int_pair d

lemmas char_ord_defs:

      c < d == char_to_int_pair c < char_to_int_pair d

      c ≤ d == char_to_int_pair c ≤ char_to_int_pair d