
diff -r 6960410f134d -r 852bce03412a src/HOL/Library/Code_Index.thy
--- a/src/HOL/Library/Code_Index.thy	Wed Jan 02 15:14:22 2008 +0100
+++ b/src/HOL/Library/Code_Index.thy	Wed Jan 02 15:14:23 2008 +0100
@@ -9,190 +9,129 @@
 begin
 
 text {*
-  Indices are isomorphic to HOL @{typ int} but
+  Indices are isomorphic to HOL @{typ nat} but
   mapped to target-language builtin integers
 *}
 
 subsection {* Datatype of indices *}
 
-datatype index = index_of_int int
+datatype index = index_of_nat nat
 
 lemmas [code func del] = index.recs index.cases
 
-fun
-  int_of_index :: "index \<Rightarrow> int"
+primrec
+  nat_of_index :: "index \<Rightarrow> nat"
 where
-  "int_of_index (index_of_int k) = k"
-lemmas [code func del] = int_of_index.simps
+  "nat_of_index (index_of_nat k) = k"
+lemmas [code func del] = nat_of_index.simps
 
 lemma index_id [simp]:
-  "index_of_int (int_of_index k) = k"
-  by (cases k) simp_all
+  "index_of_nat (nat_of_index n) = n"
+  by (cases n) simp_all
+
+lemma nat_of_index_inject [simp]:
+  "nat_of_index n = nat_of_index m \<longleftrightarrow> n = m"
+  by (cases n) auto
 
 lemma index:
-  "(\<And>k\<Colon>index. PROP P k) \<equiv> (\<And>k\<Colon>int. PROP P (index_of_int k))"
+  "(\<And>n\<Colon>index. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (index_of_nat n))"
 proof
-  fix k :: int
-  assume "\<And>k\<Colon>index. PROP P k"
-  then show "PROP P (index_of_int k)" .
+  fix n :: nat
+  assume "\<And>n\<Colon>index. PROP P n"
+  then show "PROP P (index_of_nat n)" .
 next
-  fix k :: index
-  assume "\<And>k\<Colon>int. PROP P (index_of_int k)"
-  then have "PROP P (index_of_int (int_of_index k))" .
-  then show "PROP P k" by simp
+  fix n :: index
+  assume "\<And>n\<Colon>nat. PROP P (index_of_nat n)"
+  then have "PROP P (index_of_nat (nat_of_index n))" .
+  then show "PROP P n" by simp
 qed
 
-lemma [code func]: "size (k\<Colon>index) = 0"
-  by (cases k) simp_all
+lemma [code func]: "size (n\<Colon>index) = 0"
+  by (cases n) simp_all
 
 
-subsection {* Built-in integers as datatype on numerals *}
+subsection {* Indices as datatype of ints *}
+
+instantiation index :: number
+begin
 
-instance index :: number
-  "number_of \<equiv> index_of_int" ..
+definition
+  "number_of = index_of_nat o nat"
+
+instance ..
+
+end
 
 code_datatype "number_of \<Colon> int \<Rightarrow> index"
 
-lemma number_of_index_id [simp]:
-  "number_of (int_of_index k) = k"
-  unfolding number_of_index_def by simp
-
-lemma number_of_index_shift:
-  "number_of k = index_of_int (number_of k)"
-  by (simp add: number_of_is_id number_of_index_def)
-
-lemma int_of_index_number_of [simp]:
-  "int_of_index (number_of k) = number_of k"
-  unfolding number_of_index_def number_of_is_id by simp
-
 
 subsection {* Basic arithmetic *}
 
-instance index :: zero
-  [simp]: "0 \<equiv> index_of_int 0" ..
-lemmas [code func del] = zero_index_def
-
-instance index :: one
-  [simp]: "1 \<equiv> index_of_int 1" ..
-lemmas [code func del] = one_index_def
-
-instance index :: plus
-  [simp]: "k + l \<equiv> index_of_int (int_of_index k + int_of_index l)" ..
-lemmas [code func del] = plus_index_def
-lemma plus_index_code [code func]:
-  "index_of_int k + index_of_int l = index_of_int (k + l)"
-  unfolding plus_index_def by simp
-
-instance index :: minus
-  [simp]: "- k \<equiv> index_of_int (- int_of_index k)"
-  [simp]: "k - l \<equiv> index_of_int (int_of_index k - int_of_index l)" ..
-lemmas [code func del] = uminus_index_def minus_index_def
-lemma uminus_index_code [code func]:
-  "- index_of_int k \<equiv> index_of_int (- k)"
-  unfolding uminus_index_def by simp
-lemma minus_index_code [code func]:
-  "index_of_int k - index_of_int l = index_of_int (k - l)"
-  unfolding minus_index_def by simp
-
-instance index :: times
-  [simp]: "k * l \<equiv> index_of_int (int_of_index k * int_of_index l)" ..
-lemmas [code func del] = times_index_def
-lemma times_index_code [code func]:
-  "index_of_int k * index_of_int l = index_of_int (k * l)"
-  unfolding times_index_def by simp
+instantiation index :: "{minus, ordered_semidom, Divides.div, linorder}"
+begin
 
-instance index :: ord
-  [simp]: "k \<le> l \<equiv> int_of_index k \<le> int_of_index l"
-  [simp]: "k < l \<equiv> int_of_index k < int_of_index l" ..
-lemmas [code func del] = less_eq_index_def less_index_def
-lemma less_eq_index_code [code func]:
-  "index_of_int k \<le> index_of_int l \<longleftrightarrow> k \<le> l"
-  unfolding less_eq_index_def by simp
-lemma less_index_code [code func]:
-  "index_of_int k < index_of_int l \<longleftrightarrow> k < l"
-  unfolding less_index_def by simp
-
-instance index :: "Divides.div"
-  [simp]: "k div l \<equiv> index_of_int (int_of_index k div int_of_index l)"
-  [simp]: "k mod l \<equiv> index_of_int (int_of_index k mod int_of_index l)" ..
-
-instance index :: ring_1
-  by default (auto simp add: left_distrib right_distrib)
-
-lemma of_nat_index: "of_nat n = index_of_int (of_nat n)"
-proof (induct n)
-  case 0 show ?case by simp
-next
-  case (Suc n)
-  then have "int_of_index (index_of_int (int n))
-    = int_of_index (of_nat n)" by simp
-  then have "int n = int_of_index (of_nat n)" by simp
-  then show ?case by simp
-qed
-
-instance index :: number_ring
-  by default
-    (simp_all add: left_distrib number_of_index_def of_int_of_nat of_nat_index)
+definition [simp, code func del]:
+  "(0\<Colon>index) = index_of_nat 0"
 
 lemma zero_index_code [code inline, code func]:
   "(0\<Colon>index) = Numeral0"
-  by simp
+  by (simp add: number_of_index_def Pls_def)
+
+definition [simp, code func del]:
+  "(1\<Colon>index) = index_of_nat 1"
 
 lemma one_index_code [code inline, code func]:
   "(1\<Colon>index) = Numeral1"
+  by (simp add: number_of_index_def Pls_def Bit_def)
+
+definition [simp, code func del]:
+  "n + m = index_of_nat (nat_of_index n + nat_of_index m)"
+
+lemma plus_index_code [code func]:
+  "index_of_nat n + index_of_nat m = index_of_nat (n + m)"
+  by simp
+
+definition [simp, code func del]:
+  "n - m = index_of_nat (nat_of_index n - nat_of_index m)"
+
+definition [simp, code func del]:
+  "n * m = index_of_nat (nat_of_index n * nat_of_index m)"
+
+lemma times_index_code [code func]:
+  "index_of_nat n * index_of_nat m = index_of_nat (n * m)"
   by simp
 
-instance index :: abs
-  "\<bar>k\<Colon>index\<bar> \<equiv> if k < 0 then -k else k" ..
+definition [simp, code func del]:
+  "n div m = index_of_nat (nat_of_index n div nat_of_index m)"
 
-lemma index_of_int [code func]:
-  "index_of_int k = (if k = 0 then 0
-    else if k = -1 then -1
-    else let (l, m) = divAlg (k, 2) in 2 * index_of_int l +
-      (if m = 0 then 0 else 1))"
-  by (simp add: number_of_index_shift Let_def split_def divAlg_mod_div) arith
+definition [simp, code func del]:
+  "n mod m = index_of_nat (nat_of_index n mod nat_of_index m)"
 
-lemma int_of_index [code func]:
-  "int_of_index k = (if k = 0 then 0
-    else if k = -1 then -1
-    else let l = k div 2; m = k mod 2 in 2 * int_of_index l +
-      (if m = 0 then 0 else 1))"
-  by (auto simp add: number_of_index_shift Let_def split_def) arith
+lemma div_index_code [code func]:
+  "index_of_nat n div index_of_nat m = index_of_nat (n div m)"
+  by simp
 
-
-subsection {* Conversion to and from @{typ nat} *}
-
-definition
-  nat_of_index :: "index \<Rightarrow> nat"
-where
-  [code func del]: "nat_of_index = nat o int_of_index"
+lemma mod_index_code [code func]:
+  "index_of_nat n mod index_of_nat m = index_of_nat (n mod m)"
+  by simp
 
-definition
-  nat_of_index_aux :: "index \<Rightarrow> nat \<Rightarrow> nat" where
-  [code func del]: "nat_of_index_aux i n = nat_of_index i + n"
+definition [simp, code func del]:
+  "n \<le> m \<longleftrightarrow> nat_of_index n \<le> nat_of_index m"
 
-lemma nat_of_index_aux_code [code]:
-  "nat_of_index_aux i n = (if i \<le> 0 then n else nat_of_index_aux (i - 1) (Suc n))"
-  by (auto simp add: nat_of_index_aux_def nat_of_index_def)
-
-lemma nat_of_index_code [code]:
-  "nat_of_index i = nat_of_index_aux i 0"
-  by (simp add: nat_of_index_aux_def)
+definition [simp, code func del]:
+  "n < m \<longleftrightarrow> nat_of_index n < nat_of_index m"
 
-definition
-  index_of_nat :: "nat \<Rightarrow> index"
-where
-  [code func del]: "index_of_nat = index_of_int o of_nat"
+lemma less_eq_index_code [code func]:
+  "index_of_nat n \<le> index_of_nat m \<longleftrightarrow> n \<le> m"
+  by simp
 
-lemma index_of_nat [code func]:
-  "index_of_nat 0 = 0"
-  "index_of_nat (Suc n) = index_of_nat n + 1"
-  unfolding index_of_nat_def by simp_all
+lemma less_index_code [code func]:
+  "index_of_nat n < index_of_nat m \<longleftrightarrow> n < m"
+  by simp
 
-lemma index_nat_id [simp]:
-  "nat_of_index (index_of_nat n) = n"
-  "index_of_nat (nat_of_index i) = (if i \<le> 0 then 0 else i)"
-  unfolding index_of_nat_def nat_of_index_def by simp_all
+instance by default (auto simp add: left_distrib index)
+
+end
 
 
 subsection {* ML interface *}
@@ -201,7 +140,7 @@
 structure Index =
 struct
 
-fun mk k = @{term index_of_int} $ HOLogic.mk_number @{typ index} k;
+fun mk k = @{term index_of_nat} $ HOLogic.mk_number @{typ index} k;
 
 end;
 *}
@@ -209,6 +148,20 @@
 
 subsection {* Code serialization *}
 
+text {* Pecularity for operations with potentially negative result *}
+
+definition
+  minus_index' :: "index \<Rightarrow> index \<Rightarrow> index"
+where
+  [code func del]: "minus_index' = op -"
+
+lemma minus_index_code [code func]:
+  "n - m = (let q = minus_index' n m
+    in if q < 0 then 0 else q)"
+  by (simp add: minus_index'_def Let_def)
+
+text {* Implementation of indices by bounded integers *}
+
 code_type index
   (SML "int")
   (OCaml "int")
@@ -234,12 +187,7 @@
   (OCaml "Pervasives.+")
   (Haskell infixl 6 "+")
 
-code_const "uminus \<Colon> index \<Rightarrow> index"
-  (SML "Int.~")
-  (OCaml "Pervasives.~-")
-  (Haskell "negate")
-
-code_const "op - \<Colon> index \<Rightarrow> index \<Rightarrow> index"
+code_const "minus_index' \<Colon> index \<Rightarrow> index \<Rightarrow> index"
   (SML "Int.- ((_), (_))")
   (OCaml "Pervasives.-")
   (Haskell infixl 6 "-")
@@ -264,6 +212,16 @@
   (OCaml "!((_ : Pervasives.int) < _)")
   (Haskell infix 4 "<")
 
+code_const "op div \<Colon> index \<Rightarrow> index \<Rightarrow> index"
+  (SML "IntInf.div ((_), (_))")
+  (OCaml "Big'_int.div'_big'_int")
+  (Haskell "div")
+
+code_const "op mod \<Colon> index \<Rightarrow> index \<Rightarrow> index"
+  (SML "IntInf.mod ((_), (_))")
+  (OCaml "Big'_int.mod'_big'_int")
+  (Haskell "mod")
+
 code_reserved SML Int
 code_reserved OCaml Pervasives
 




