src/HOL/Code_Numeral.thy
changeset 31205 98370b26c2ce
parent 31203 5c8fb4fd67e0
child 31266 55e70b6d812e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Code_Numeral.thy	Tue May 19 16:54:55 2009 +0200
     1.3 @@ -0,0 +1,325 @@
     1.4 +(* Author: Florian Haftmann, TU Muenchen *)
     1.5 +
     1.6 +header {* Type of target language numerals *}
     1.7 +
     1.8 +theory Code_Numeral
     1.9 +imports Nat_Numeral
    1.10 +begin
    1.11 +
    1.12 +text {*
    1.13 +  Code numerals are isomorphic to HOL @{typ nat} but
    1.14 +  mapped to target-language builtin numerals.
    1.15 +*}
    1.16 +
    1.17 +subsection {* Datatype of target language numerals *}
    1.18 +
    1.19 +typedef (open) code_numeral = "UNIV \<Colon> nat set"
    1.20 +  morphisms nat_of of_nat by rule
    1.21 +
    1.22 +lemma of_nat_nat_of [simp]:
    1.23 +  "of_nat (nat_of k) = k"
    1.24 +  by (rule nat_of_inverse)
    1.25 +
    1.26 +lemma nat_of_of_nat [simp]:
    1.27 +  "nat_of (of_nat n) = n"
    1.28 +  by (rule of_nat_inverse) (rule UNIV_I)
    1.29 +
    1.30 +lemma [measure_function]:
    1.31 +  "is_measure nat_of" by (rule is_measure_trivial)
    1.32 +
    1.33 +lemma code_numeral:
    1.34 +  "(\<And>n\<Colon>code_numeral. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (of_nat n))"
    1.35 +proof
    1.36 +  fix n :: nat
    1.37 +  assume "\<And>n\<Colon>code_numeral. PROP P n"
    1.38 +  then show "PROP P (of_nat n)" .
    1.39 +next
    1.40 +  fix n :: code_numeral
    1.41 +  assume "\<And>n\<Colon>nat. PROP P (of_nat n)"
    1.42 +  then have "PROP P (of_nat (nat_of n))" .
    1.43 +  then show "PROP P n" by simp
    1.44 +qed
    1.45 +
    1.46 +lemma code_numeral_case:
    1.47 +  assumes "\<And>n. k = of_nat n \<Longrightarrow> P"
    1.48 +  shows P
    1.49 +  by (rule assms [of "nat_of k"]) simp
    1.50 +
    1.51 +lemma code_numeral_induct_raw:
    1.52 +  assumes "\<And>n. P (of_nat n)"
    1.53 +  shows "P k"
    1.54 +proof -
    1.55 +  from assms have "P (of_nat (nat_of k))" .
    1.56 +  then show ?thesis by simp
    1.57 +qed
    1.58 +
    1.59 +lemma nat_of_inject [simp]:
    1.60 +  "nat_of k = nat_of l \<longleftrightarrow> k = l"
    1.61 +  by (rule nat_of_inject)
    1.62 +
    1.63 +lemma of_nat_inject [simp]:
    1.64 +  "of_nat n = of_nat m \<longleftrightarrow> n = m"
    1.65 +  by (rule of_nat_inject) (rule UNIV_I)+
    1.66 +
    1.67 +instantiation code_numeral :: zero
    1.68 +begin
    1.69 +
    1.70 +definition [simp, code del]:
    1.71 +  "0 = of_nat 0"
    1.72 +
    1.73 +instance ..
    1.74 +
    1.75 +end
    1.76 +
    1.77 +definition [simp]:
    1.78 +  "Suc_code_numeral k = of_nat (Suc (nat_of k))"
    1.79 +
    1.80 +rep_datatype "0 \<Colon> code_numeral" Suc_code_numeral
    1.81 +proof -
    1.82 +  fix P :: "code_numeral \<Rightarrow> bool"
    1.83 +  fix k :: code_numeral
    1.84 +  assume "P 0" then have init: "P (of_nat 0)" by simp
    1.85 +  assume "\<And>k. P k \<Longrightarrow> P (Suc_code_numeral k)"
    1.86 +    then have "\<And>n. P (of_nat n) \<Longrightarrow> P (Suc_code_numeral (of_nat n))" .
    1.87 +    then have step: "\<And>n. P (of_nat n) \<Longrightarrow> P (of_nat (Suc n))" by simp
    1.88 +  from init step have "P (of_nat (nat_of k))"
    1.89 +    by (induct "nat_of k") simp_all
    1.90 +  then show "P k" by simp
    1.91 +qed simp_all
    1.92 +
    1.93 +declare code_numeral_case [case_names nat, cases type: code_numeral]
    1.94 +declare code_numeral.induct [case_names nat, induct type: code_numeral]
    1.95 +
    1.96 +lemma code_numeral_decr [termination_simp]:
    1.97 +  "k \<noteq> of_nat 0 \<Longrightarrow> nat_of k - Suc 0 < nat_of k"
    1.98 +  by (cases k) simp
    1.99 +
   1.100 +lemma [simp, code]:
   1.101 +  "code_numeral_size = nat_of"
   1.102 +proof (rule ext)
   1.103 +  fix k
   1.104 +  have "code_numeral_size k = nat_size (nat_of k)"
   1.105 +    by (induct k rule: code_numeral.induct) (simp_all del: zero_code_numeral_def Suc_code_numeral_def, simp_all)
   1.106 +  also have "nat_size (nat_of k) = nat_of k" by (induct "nat_of k") simp_all
   1.107 +  finally show "code_numeral_size k = nat_of k" .
   1.108 +qed
   1.109 +
   1.110 +lemma [simp, code]:
   1.111 +  "size = nat_of"
   1.112 +proof (rule ext)
   1.113 +  fix k
   1.114 +  show "size k = nat_of k"
   1.115 +  by (induct k) (simp_all del: zero_code_numeral_def Suc_code_numeral_def, simp_all)
   1.116 +qed
   1.117 +
   1.118 +lemmas [code del] = code_numeral.recs code_numeral.cases
   1.119 +
   1.120 +lemma [code]:
   1.121 +  "eq_class.eq k l \<longleftrightarrow> eq_class.eq (nat_of k) (nat_of l)"
   1.122 +  by (cases k, cases l) (simp add: eq)
   1.123 +
   1.124 +lemma [code nbe]:
   1.125 +  "eq_class.eq (k::code_numeral) k \<longleftrightarrow> True"
   1.126 +  by (rule HOL.eq_refl)
   1.127 +
   1.128 +
   1.129 +subsection {* Indices as datatype of ints *}
   1.130 +
   1.131 +instantiation code_numeral :: number
   1.132 +begin
   1.133 +
   1.134 +definition
   1.135 +  "number_of = of_nat o nat"
   1.136 +
   1.137 +instance ..
   1.138 +
   1.139 +end
   1.140 +
   1.141 +lemma nat_of_number [simp]:
   1.142 +  "nat_of (number_of k) = number_of k"
   1.143 +  by (simp add: number_of_code_numeral_def nat_number_of_def number_of_is_id)
   1.144 +
   1.145 +code_datatype "number_of \<Colon> int \<Rightarrow> code_numeral"
   1.146 +
   1.147 +
   1.148 +subsection {* Basic arithmetic *}
   1.149 +
   1.150 +instantiation code_numeral :: "{minus, ordered_semidom, semiring_div, linorder}"
   1.151 +begin
   1.152 +
   1.153 +definition [simp, code del]:
   1.154 +  "(1\<Colon>code_numeral) = of_nat 1"
   1.155 +
   1.156 +definition [simp, code del]:
   1.157 +  "n + m = of_nat (nat_of n + nat_of m)"
   1.158 +
   1.159 +definition [simp, code del]:
   1.160 +  "n - m = of_nat (nat_of n - nat_of m)"
   1.161 +
   1.162 +definition [simp, code del]:
   1.163 +  "n * m = of_nat (nat_of n * nat_of m)"
   1.164 +
   1.165 +definition [simp, code del]:
   1.166 +  "n div m = of_nat (nat_of n div nat_of m)"
   1.167 +
   1.168 +definition [simp, code del]:
   1.169 +  "n mod m = of_nat (nat_of n mod nat_of m)"
   1.170 +
   1.171 +definition [simp, code del]:
   1.172 +  "n \<le> m \<longleftrightarrow> nat_of n \<le> nat_of m"
   1.173 +
   1.174 +definition [simp, code del]:
   1.175 +  "n < m \<longleftrightarrow> nat_of n < nat_of m"
   1.176 +
   1.177 +instance proof
   1.178 +qed (auto simp add: code_numeral left_distrib div_mult_self1)
   1.179 +
   1.180 +end
   1.181 +
   1.182 +lemma zero_code_numeral_code [code inline, code]:
   1.183 +  "(0\<Colon>code_numeral) = Numeral0"
   1.184 +  by (simp add: number_of_code_numeral_def Pls_def)
   1.185 +lemma [code post]: "Numeral0 = (0\<Colon>code_numeral)"
   1.186 +  using zero_code_numeral_code ..
   1.187 +
   1.188 +lemma one_code_numeral_code [code inline, code]:
   1.189 +  "(1\<Colon>code_numeral) = Numeral1"
   1.190 +  by (simp add: number_of_code_numeral_def Pls_def Bit1_def)
   1.191 +lemma [code post]: "Numeral1 = (1\<Colon>code_numeral)"
   1.192 +  using one_code_numeral_code ..
   1.193 +
   1.194 +lemma plus_code_numeral_code [code nbe]:
   1.195 +  "of_nat n + of_nat m = of_nat (n + m)"
   1.196 +  by simp
   1.197 +
   1.198 +definition subtract_code_numeral :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
   1.199 +  [simp, code del]: "subtract_code_numeral = op -"
   1.200 +
   1.201 +lemma subtract_code_numeral_code [code nbe]:
   1.202 +  "subtract_code_numeral (of_nat n) (of_nat m) = of_nat (n - m)"
   1.203 +  by simp
   1.204 +
   1.205 +lemma minus_code_numeral_code [code]:
   1.206 +  "n - m = subtract_code_numeral n m"
   1.207 +  by simp
   1.208 +
   1.209 +lemma times_code_numeral_code [code nbe]:
   1.210 +  "of_nat n * of_nat m = of_nat (n * m)"
   1.211 +  by simp
   1.212 +
   1.213 +lemma less_eq_code_numeral_code [code nbe]:
   1.214 +  "of_nat n \<le> of_nat m \<longleftrightarrow> n \<le> m"
   1.215 +  by simp
   1.216 +
   1.217 +lemma less_code_numeral_code [code nbe]:
   1.218 +  "of_nat n < of_nat m \<longleftrightarrow> n < m"
   1.219 +  by simp
   1.220 +
   1.221 +lemma Suc_code_numeral_minus_one: "Suc_code_numeral n - 1 = n" by simp
   1.222 +
   1.223 +lemma of_nat_code [code]:
   1.224 +  "of_nat = Nat.of_nat"
   1.225 +proof
   1.226 +  fix n :: nat
   1.227 +  have "Nat.of_nat n = of_nat n"
   1.228 +    by (induct n) simp_all
   1.229 +  then show "of_nat n = Nat.of_nat n"
   1.230 +    by (rule sym)
   1.231 +qed
   1.232 +
   1.233 +lemma code_numeral_not_eq_zero: "i \<noteq> of_nat 0 \<longleftrightarrow> i \<ge> 1"
   1.234 +  by (cases i) auto
   1.235 +
   1.236 +definition nat_of_aux :: "code_numeral \<Rightarrow> nat \<Rightarrow> nat" where
   1.237 +  "nat_of_aux i n = nat_of i + n"
   1.238 +
   1.239 +lemma nat_of_aux_code [code]:
   1.240 +  "nat_of_aux i n = (if i = 0 then n else nat_of_aux (i - 1) (Suc n))"
   1.241 +  by (auto simp add: nat_of_aux_def code_numeral_not_eq_zero)
   1.242 +
   1.243 +lemma nat_of_code [code]:
   1.244 +  "nat_of i = nat_of_aux i 0"
   1.245 +  by (simp add: nat_of_aux_def)
   1.246 +
   1.247 +definition div_mod_code_numeral ::  "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral \<times> code_numeral" where
   1.248 +  [code del]: "div_mod_code_numeral n m = (n div m, n mod m)"
   1.249 +
   1.250 +lemma [code]:
   1.251 +  "div_mod_code_numeral n m = (if m = 0 then (0, n) else (n div m, n mod m))"
   1.252 +  unfolding div_mod_code_numeral_def by auto
   1.253 +
   1.254 +lemma [code]:
   1.255 +  "n div m = fst (div_mod_code_numeral n m)"
   1.256 +  unfolding div_mod_code_numeral_def by simp
   1.257 +
   1.258 +lemma [code]:
   1.259 +  "n mod m = snd (div_mod_code_numeral n m)"
   1.260 +  unfolding div_mod_code_numeral_def by simp
   1.261 +
   1.262 +definition int_of :: "code_numeral \<Rightarrow> int" where
   1.263 +  "int_of = Nat.of_nat o nat_of"
   1.264 +
   1.265 +lemma int_of_code [code]:
   1.266 +  "int_of k = (if k = 0 then 0
   1.267 +    else (if k mod 2 = 0 then 2 * int_of (k div 2) else 2 * int_of (k div 2) + 1))"
   1.268 +  by (auto simp add: int_of_def mod_div_equality')
   1.269 +
   1.270 +hide (open) const of_nat nat_of int_of
   1.271 +
   1.272 +
   1.273 +subsection {* Code generator setup *}
   1.274 +
   1.275 +text {* Implementation of indices by bounded integers *}
   1.276 +
   1.277 +code_type code_numeral
   1.278 +  (SML "int")
   1.279 +  (OCaml "int")
   1.280 +  (Haskell "Int")
   1.281 +
   1.282 +code_instance code_numeral :: eq
   1.283 +  (Haskell -)
   1.284 +
   1.285 +setup {*
   1.286 +  fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
   1.287 +    false false) ["SML", "OCaml", "Haskell"]
   1.288 +*}
   1.289 +
   1.290 +code_reserved SML Int int
   1.291 +code_reserved OCaml Pervasives int
   1.292 +
   1.293 +code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   1.294 +  (SML "Int.+/ ((_),/ (_))")
   1.295 +  (OCaml "Pervasives.( + )")
   1.296 +  (Haskell infixl 6 "+")
   1.297 +
   1.298 +code_const "subtract_code_numeral \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   1.299 +  (SML "Int.max/ (_/ -/ _,/ 0 : int)")
   1.300 +  (OCaml "Pervasives.max/ (_/ -/ _)/ (0 : int) ")
   1.301 +  (Haskell "max/ (_/ -/ _)/ (0 :: Int)")
   1.302 +
   1.303 +code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   1.304 +  (SML "Int.*/ ((_),/ (_))")
   1.305 +  (OCaml "Pervasives.( * )")
   1.306 +  (Haskell infixl 7 "*")
   1.307 +
   1.308 +code_const div_mod_code_numeral
   1.309 +  (SML "(fn n => fn m =>/ if m = 0/ then (0, n) else/ (n div m, n mod m))")
   1.310 +  (OCaml "(fun n -> fun m ->/ if m = 0/ then (0, n) else/ (n '/ m, n mod m))")
   1.311 +  (Haskell "divMod")
   1.312 +
   1.313 +code_const "eq_class.eq \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   1.314 +  (SML "!((_ : Int.int) = _)")
   1.315 +  (OCaml "!((_ : int) = _)")
   1.316 +  (Haskell infixl 4 "==")
   1.317 +
   1.318 +code_const "op \<le> \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   1.319 +  (SML "Int.<=/ ((_),/ (_))")
   1.320 +  (OCaml "!((_ : int) <= _)")
   1.321 +  (Haskell infix 4 "<=")
   1.322 +
   1.323 +code_const "op < \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   1.324 +  (SML "Int.</ ((_),/ (_))")
   1.325 +  (OCaml "!((_ : int) < _)")
   1.326 +  (Haskell infix 4 "<")
   1.327 +
   1.328 +end