# HG changeset patch # User haftmann # Date 1199283263 -3600 # Node ID 852bce03412ac9b535f099bbd07a0fb5309ca8b5 # Parent 6960410f134dacd83fc076dbae88cbbedf443e1a index now a copy of nat rather than int 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 \ int" +primrec + nat_of_index :: "index \ 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 \ n = m" + by (cases n) auto lemma index: - "(\k\index. PROP P k) \ (\k\int. PROP P (index_of_int k))" + "(\n\index. PROP P n) \ (\n\nat. PROP P (index_of_nat n))" proof - fix k :: int - assume "\k\index. PROP P k" - then show "PROP P (index_of_int k)" . + fix n :: nat + assume "\n\index. PROP P n" + then show "PROP P (index_of_nat n)" . next - fix k :: index - assume "\k\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 "\n\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\index) = 0" - by (cases k) simp_all +lemma [code func]: "size (n\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 \ index_of_int" .. +definition + "number_of = index_of_nat o nat" + +instance .. + +end code_datatype "number_of \ int \ 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 \ index_of_int 0" .. -lemmas [code func del] = zero_index_def - -instance index :: one - [simp]: "1 \ index_of_int 1" .. -lemmas [code func del] = one_index_def - -instance index :: plus - [simp]: "k + l \ 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 \ index_of_int (- int_of_index k)" - [simp]: "k - l \ 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 \ 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 \ 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 \ l \ int_of_index k \ int_of_index l" - [simp]: "k < l \ 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 \ index_of_int l \ k \ l" - unfolding less_eq_index_def by simp -lemma less_index_code [code func]: - "index_of_int k < index_of_int l \ k < l" - unfolding less_index_def by simp - -instance index :: "Divides.div" - [simp]: "k div l \ index_of_int (int_of_index k div int_of_index l)" - [simp]: "k mod l \ 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\index) = index_of_nat 0" lemma zero_index_code [code inline, code func]: "(0\index) = Numeral0" - by simp + by (simp add: number_of_index_def Pls_def) + +definition [simp, code func del]: + "(1\index) = index_of_nat 1" lemma one_index_code [code inline, code func]: "(1\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 - "\k\index\ \ 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 \ 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 \ nat \ nat" where - [code func del]: "nat_of_index_aux i n = nat_of_index i + n" +definition [simp, code func del]: + "n \ m \ nat_of_index n \ nat_of_index m" -lemma nat_of_index_aux_code [code]: - "nat_of_index_aux i n = (if i \ 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 \ nat_of_index n < nat_of_index m" -definition - index_of_nat :: "nat \ 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 \ index_of_nat m \ n \ 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 \ 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 \ 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 \ index \ 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 \ index \ index" - (SML "Int.~") - (OCaml "Pervasives.~-") - (Haskell "negate") - -code_const "op - \ index \ index \ index" +code_const "minus_index' \ index \ index \ index" (SML "Int.- ((_), (_))") (OCaml "Pervasives.-") (Haskell infixl 6 "-") @@ -264,6 +212,16 @@ (OCaml "!((_ : Pervasives.int) < _)") (Haskell infix 4 "<") +code_const "op div \ index \ index \ index" + (SML "IntInf.div ((_), (_))") + (OCaml "Big'_int.div'_big'_int") + (Haskell "div") + +code_const "op mod \ index \ index \ index" + (SML "IntInf.mod ((_), (_))") + (OCaml "Big'_int.mod'_big'_int") + (Haskell "mod") + code_reserved SML Int code_reserved OCaml Pervasives diff -r 6960410f134d -r 852bce03412a src/HOL/Library/Code_Integer.thy --- a/src/HOL/Library/Code_Integer.thy Wed Jan 02 15:14:22 2008 +0100 +++ b/src/HOL/Library/Code_Integer.thy Wed Jan 02 15:14:23 2008 +0100 @@ -88,10 +88,10 @@ (OCaml "Big'_int.lt'_big'_int") (Haskell infix 4 "<") -code_const index_of_int and int_of_index +(*code_const index_of_int and int_of_index (SML "IntInf.toInt" and "IntInf.fromInt") (OCaml "Big'_int.int'_of'_big'_int" and "Big'_int.big'_int'_of'_int") - (Haskell "_" and "_") + (Haskell "_" and "_") FIXME perhaps recover this if neccessary *) code_reserved SML IntInf code_reserved OCaml Big_int diff -r 6960410f134d -r 852bce03412a src/HOL/Library/Efficient_Nat.thy --- a/src/HOL/Library/Efficient_Nat.thy Wed Jan 02 15:14:22 2008 +0100 +++ b/src/HOL/Library/Efficient_Nat.thy Wed Jan 02 15:14:23 2008 +0100 @@ -165,14 +165,6 @@ then show ?thesis unfolding int_aux_def int_of_nat_def by auto qed -lemma index_of_nat_code [code func, code inline]: - "index_of_nat n = index_of_int (int_of_nat n)" - unfolding index_of_nat_def int_of_nat_def by simp - -lemma nat_of_index_code [code func, code inline]: - "nat_of_index k = nat (int_of_index k)" - unfolding nat_of_index_def by simp - subsection {* Code generator setup for basic functions *} @@ -221,11 +213,22 @@ (OCaml "_") (Haskell "_") +code_const index_of_nat + (SML "_") + (OCaml "_") + (Haskell "_") + code_const nat_of_int (SML "_") (OCaml "_") (Haskell "_") +code_const nat_of_index + (SML "_") + (OCaml "_") + (Haskell "_") + + text {* Using target language div and mod *} code_const "op div \ nat \ nat \ nat"