src/HOL/Library/Code_Index.thy
changeset 25335 182a001a7ea4
parent 24999 1dbe785ed529
child 25502 9200b36280c0
equal deleted inserted replaced
25334:db0365e89f5a 25335:182a001a7ea4
    59 
    59 
    60 lemma number_of_index_shift:
    60 lemma number_of_index_shift:
    61   "number_of k = index_of_int (number_of k)"
    61   "number_of k = index_of_int (number_of k)"
    62   by (simp add: number_of_is_id number_of_index_def)
    62   by (simp add: number_of_is_id number_of_index_def)
    63 
    63 
       
    64 lemma int_of_index_number_of [simp]:
       
    65   "int_of_index (number_of k) = number_of k"
       
    66   unfolding number_of_index_def number_of_is_id by simp
       
    67 
    64 
    68 
    65 subsection {* Basic arithmetic *}
    69 subsection {* Basic arithmetic *}
    66 
    70 
    67 instance index :: zero
    71 instance index :: zero
    68   [simp]: "0 \<equiv> index_of_int 0" ..
    72   [simp]: "0 \<equiv> index_of_int 0" ..
   106   unfolding less_eq_index_def by simp
   110   unfolding less_eq_index_def by simp
   107 lemma less_index_code [code func]:
   111 lemma less_index_code [code func]:
   108   "index_of_int k < index_of_int l \<longleftrightarrow> k < l"
   112   "index_of_int k < index_of_int l \<longleftrightarrow> k < l"
   109   unfolding less_index_def by simp
   113   unfolding less_index_def by simp
   110 
   114 
       
   115 instance index :: "Divides.div"
       
   116   [simp]: "k div l \<equiv> index_of_int (int_of_index k div int_of_index l)"
       
   117   [simp]: "k mod l \<equiv> index_of_int (int_of_index k mod int_of_index l)" ..
       
   118 
   111 instance index :: ring_1
   119 instance index :: ring_1
   112   by default (auto simp add: left_distrib right_distrib)
   120   by default (auto simp add: left_distrib right_distrib)
   113 
   121 
   114 lemma of_nat_index: "of_nat n = index_of_int (of_nat n)"
   122 lemma of_nat_index: "of_nat n = index_of_int (of_nat n)"
   115 proof (induct n)
   123 proof (induct n)
   142     else if k = -1 then -1
   150     else if k = -1 then -1
   143     else let (l, m) = divAlg (k, 2) in 2 * index_of_int l +
   151     else let (l, m) = divAlg (k, 2) in 2 * index_of_int l +
   144       (if m = 0 then 0 else 1))"
   152       (if m = 0 then 0 else 1))"
   145   by (simp add: number_of_index_shift Let_def split_def divAlg_mod_div) arith
   153   by (simp add: number_of_index_shift Let_def split_def divAlg_mod_div) arith
   146 
   154 
       
   155 lemma int_of_index [code func]:
       
   156   "int_of_index k = (if k = 0 then 0
       
   157     else if k = -1 then -1
       
   158     else let l = k div 2; m = k mod 2 in 2 * int_of_index l +
       
   159       (if m = 0 then 0 else 1))"
       
   160   by (auto simp add: number_of_index_shift Let_def split_def) arith
       
   161 
   147 
   162 
   148 subsection {* Conversion to and from @{typ nat} *}
   163 subsection {* Conversion to and from @{typ nat} *}
   149 
   164 
   150 definition
   165 definition
   151   nat_of_index :: "index \<Rightarrow> nat"
   166   nat_of_index :: "index \<Rightarrow> nat"