src/HOL/Library/ML_Int.thy
changeset 24700 291665d063e4
parent 24423 ae9cd0e92423
child 24716 483f0a64271f
equal deleted inserted replaced
24699:c6674504103f 24700:291665d063e4
   129 
   129 
   130 instance ml_int :: abs
   130 instance ml_int :: abs
   131   "\<bar>k\<bar> \<equiv> if k < 0 then -k else k" ..
   131   "\<bar>k\<bar> \<equiv> if k < 0 then -k else k" ..
   132 
   132 
   133 
   133 
   134 subsection {* Conversion to @{typ nat} *}
   134 subsection {* Conversion to and from @{typ nat} *}
   135 
   135 
   136 definition
   136 definition
   137   nat_of_ml_int :: "ml_int \<Rightarrow> nat"
   137   nat_of_ml_int :: "ml_int \<Rightarrow> nat"
   138 where
   138 where
   139   "nat_of_ml_int = nat o int_of_ml_int"
   139   [code func del]: "nat_of_ml_int = nat o int_of_ml_int"
   140 
   140 
   141 definition
   141 definition
   142   nat_of_ml_int_aux :: "ml_int \<Rightarrow> nat \<Rightarrow> nat" where
   142   nat_of_ml_int_aux :: "ml_int \<Rightarrow> nat \<Rightarrow> nat" where
   143   "nat_of_ml_int_aux i n = nat_of_ml_int i + n"
   143   [code func del]: "nat_of_ml_int_aux i n = nat_of_ml_int i + n"
   144 
   144 
   145 lemma nat_of_ml_int_aux_code [code]:
   145 lemma nat_of_ml_int_aux_code [code]:
   146   "nat_of_ml_int_aux i n = (if i \<le> 0 then n else nat_of_ml_int_aux (i - 1) (Suc n))"
   146   "nat_of_ml_int_aux i n = (if i \<le> 0 then n else nat_of_ml_int_aux (i - 1) (Suc n))"
   147   by (auto simp add: nat_of_ml_int_aux_def nat_of_ml_int_def)
   147   by (auto simp add: nat_of_ml_int_aux_def nat_of_ml_int_def)
   148 
   148 
   149 lemma nat_of_ml_int_code [code]:
   149 lemma nat_of_ml_int_code [code]:
   150   "nat_of_ml_int i = nat_of_ml_int_aux i 0"
   150   "nat_of_ml_int i = nat_of_ml_int_aux i 0"
   151   by (simp add: nat_of_ml_int_aux_def)
   151   by (simp add: nat_of_ml_int_aux_def)
   152 
   152 
       
   153 definition
       
   154   ml_int_of_nat :: "nat \<Rightarrow> ml_int"
       
   155 where
       
   156   [code func del]: "ml_int_of_nat = ml_int_of_int o of_nat"
       
   157 
       
   158 lemma ml_int_of_nat [code func]:
       
   159   "ml_int_of_nat 0 = 0"
       
   160   "ml_int_of_nat (Suc n) = ml_int_of_nat n + 1"
       
   161   unfolding ml_int_of_nat_def by simp_all
       
   162 
       
   163 lemma ml_int_nat_id [simp]:
       
   164   "nat_of_ml_int (ml_int_of_nat n) = n"
       
   165   "ml_int_of_nat (nat_of_ml_int i) = (if i \<le> 0 then 0 else i)"
       
   166   unfolding ml_int_of_nat_def nat_of_ml_int_def by simp_all
       
   167 
   153 
   168 
   154 subsection {* ML interface *}
   169 subsection {* ML interface *}
   155 
   170 
   156 ML {*
   171 ML {*
   157 structure ML_Int =
   172 structure ML_Int =
   168 code_type ml_int
   183 code_type ml_int
   169   (SML "int")
   184   (SML "int")
   170 
   185 
   171 setup {*
   186 setup {*
   172   CodeTarget.add_pretty_numeral "SML" false
   187   CodeTarget.add_pretty_numeral "SML" false
   173     @{const_name ml_int_of_int}
   188     @{const_name number_ml_int_inst.number_of_ml_int}
   174     @{const_name Numeral.B0} @{const_name Numeral.B1}
   189     @{const_name Numeral.B0} @{const_name Numeral.B1}
   175     @{const_name Numeral.Pls} @{const_name Numeral.Min}
   190     @{const_name Numeral.Pls} @{const_name Numeral.Min}
   176     @{const_name Numeral.Bit}
   191     @{const_name Numeral.Bit}
   177 *}
   192 *}
   178 
   193 
   198 
   213 
   199 code_const "op < \<Colon> ml_int \<Rightarrow> ml_int \<Rightarrow> bool"
   214 code_const "op < \<Colon> ml_int \<Rightarrow> ml_int \<Rightarrow> bool"
   200   (SML "Int.< ((_), (_))")
   215   (SML "Int.< ((_), (_))")
   201 
   216 
   202 end
   217 end
   203 
       
   204