# Theory Code_Numeral

Up to index of Isabelle/HOL-Proofs

theory Code_Numeral
imports Divides
`(* Author: Florian Haftmann, TU Muenchen *)header {* Type of target language numerals *}theory Code_Numeralimports Nat_Transfer Dividesbegintext {*  Code numerals are isomorphic to HOL @{typ nat} but  mapped to target-language builtin numerals.*}subsection {* Datatype of target language numerals *}typedef code_numeral = "UNIV :: nat set"  morphisms nat_of of_nat ..lemma of_nat_nat_of [simp]:  "of_nat (nat_of k) = k"  by (rule nat_of_inverse)lemma nat_of_of_nat [simp]:  "nat_of (of_nat n) = n"  by (rule of_nat_inverse) (rule UNIV_I)lemma [measure_function]:  "is_measure nat_of" by (rule is_measure_trivial)lemma code_numeral:  "(!!n::code_numeral. PROP P n) ≡ (!!n::nat. PROP P (of_nat n))"proof  fix n :: nat  assume "!!n::code_numeral. PROP P n"  then show "PROP P (of_nat n)" .next  fix n :: code_numeral  assume "!!n::nat. PROP P (of_nat n)"  then have "PROP P (of_nat (nat_of n))" .  then show "PROP P n" by simpqedlemma code_numeral_case:  assumes "!!n. k = of_nat n ==> P"  shows P  by (rule assms [of "nat_of k"]) simplemma code_numeral_induct_raw:  assumes "!!n. P (of_nat n)"  shows "P k"proof -  from assms have "P (of_nat (nat_of k))" .  then show ?thesis by simpqedlemma nat_of_inject [simp]:  "nat_of k = nat_of l <-> k = l"  by (rule nat_of_inject)lemma of_nat_inject [simp]:  "of_nat n = of_nat m <-> n = m"  by (rule of_nat_inject) (rule UNIV_I)+instantiation code_numeral :: zerobegindefinition [simp, code del]:  "0 = of_nat 0"instance ..enddefinition Suc where [simp]:  "Suc k = of_nat (Nat.Suc (nat_of k))"rep_datatype "0 :: code_numeral" Sucproof -  fix P :: "code_numeral => bool"  fix k :: code_numeral  assume "P 0" then have init: "P (of_nat 0)" by simp  assume "!!k. P k ==> P (Suc k)"    then have "!!n. P (of_nat n) ==> P (Suc (of_nat n))" .    then have step: "!!n. P (of_nat n) ==> P (of_nat (Nat.Suc n))" by simp  from init step have "P (of_nat (nat_of k))"    by (induct ("nat_of k")) simp_all  then show "P k" by simpqed simp_alldeclare code_numeral_case [case_names nat, cases type: code_numeral]declare code_numeral.induct [case_names nat, induct type: code_numeral]lemma code_numeral_decr [termination_simp]:  "k ≠ of_nat 0 ==> nat_of k - Nat.Suc 0 < nat_of k"  by (cases k) simplemma [simp, code]:  "code_numeral_size = nat_of"proof (rule ext)  fix k  have "code_numeral_size k = nat_size (nat_of k)"    by (induct k rule: code_numeral.induct) (simp_all del: zero_code_numeral_def Suc_def, simp_all)  also have "nat_size (nat_of k) = nat_of k" by (induct ("nat_of k")) simp_all  finally show "code_numeral_size k = nat_of k" .qedlemma [simp, code]:  "size = nat_of"proof (rule ext)  fix k  show "size k = nat_of k"  by (induct k) (simp_all del: zero_code_numeral_def Suc_def, simp_all)qedlemmas [code del] = code_numeral.recs code_numeral.caseslemma [code]:  "HOL.equal k l <-> HOL.equal (nat_of k) (nat_of l)"  by (cases k, cases l) (simp add: equal)lemma [code nbe]:  "HOL.equal (k::code_numeral) k <-> True"  by (rule equal_refl)subsection {* Basic arithmetic *}instantiation code_numeral :: "{minus, linordered_semidom, semiring_div, linorder}"begindefinition [simp, code del]:  "(1::code_numeral) = of_nat 1"definition [simp, code del]:  "n + m = of_nat (nat_of n + nat_of m)"definition [simp, code del]:  "n - m = of_nat (nat_of n - nat_of m)"definition [simp, code del]:  "n * m = of_nat (nat_of n * nat_of m)"definition [simp, code del]:  "n div m = of_nat (nat_of n div nat_of m)"definition [simp, code del]:  "n mod m = of_nat (nat_of n mod nat_of m)"definition [simp, code del]:  "n ≤ m <-> nat_of n ≤ nat_of m"definition [simp, code del]:  "n < m <-> nat_of n < nat_of m"instance proofqed (auto simp add: code_numeral distrib_right intro: mult_commute)endlemma nat_of_numeral [simp]: "nat_of (numeral k) = numeral k"  by (induct k rule: num_induct) (simp_all add: numeral_inc)definition Num :: "num => code_numeral"  where [simp, code_abbrev]: "Num = numeral"code_datatype "0::code_numeral" Numlemma one_code_numeral_code [code]:  "(1::code_numeral) = Numeral1"  by simplemma [code_abbrev]: "Numeral1 = (1::code_numeral)"  using one_code_numeral_code ..lemma plus_code_numeral_code [code nbe]:  "of_nat n + of_nat m = of_nat (n + m)"  by simplemma minus_code_numeral_code [code nbe]:  "of_nat n - of_nat m = of_nat (n - m)"  by simplemma times_code_numeral_code [code nbe]:  "of_nat n * of_nat m = of_nat (n * m)"  by simplemma less_eq_code_numeral_code [code nbe]:  "of_nat n ≤ of_nat m <-> n ≤ m"  by simplemma less_code_numeral_code [code nbe]:  "of_nat n < of_nat m <-> n < m"  by simplemma code_numeral_zero_minus_one:  "(0::code_numeral) - 1 = 0"  by simplemma Suc_code_numeral_minus_one:  "Suc n - 1 = n"  by simplemma of_nat_code [code]:  "of_nat = Nat.of_nat"proof  fix n :: nat  have "Nat.of_nat n = of_nat n"    by (induct n) simp_all  then show "of_nat n = Nat.of_nat n"    by (rule sym)qedlemma code_numeral_not_eq_zero: "i ≠ of_nat 0 <-> i ≥ 1"  by (cases i) autodefinition nat_of_aux :: "code_numeral => nat => nat" where  "nat_of_aux i n = nat_of i + n"lemma nat_of_aux_code [code]:  "nat_of_aux i n = (if i = 0 then n else nat_of_aux (i - 1) (Nat.Suc n))"  by (auto simp add: nat_of_aux_def code_numeral_not_eq_zero)lemma nat_of_code [code]:  "nat_of i = nat_of_aux i 0"  by (simp add: nat_of_aux_def)definition div_mod :: "code_numeral => code_numeral => code_numeral × code_numeral" where  [code del]: "div_mod n m = (n div m, n mod m)"lemma [code]:  "div_mod n m = (if m = 0 then (0, n) else (n div m, n mod m))"  unfolding div_mod_def by autolemma [code]:  "n div m = fst (div_mod n m)"  unfolding div_mod_def by simplemma [code]:  "n mod m = snd (div_mod n m)"  unfolding div_mod_def by simpdefinition int_of :: "code_numeral => int" where  "int_of = Nat.of_nat o nat_of"lemma int_of_code [code]:  "int_of k = (if k = 0 then 0    else (if k mod 2 = 0 then 2 * int_of (k div 2) else 2 * int_of (k div 2) + 1))"proof -  have "(nat_of k div 2) * 2 + nat_of k mod 2 = nat_of k"     by (rule mod_div_equality)  then have "int ((nat_of k div 2) * 2 + nat_of k mod 2) = int (nat_of k)"     by simp  then have "int (nat_of k) = int (nat_of k div 2) * 2 + int (nat_of k mod 2)"     unfolding of_nat_mult of_nat_add by simp  then show ?thesis by (auto simp add: int_of_def mult_ac)qedhide_const (open) of_nat nat_of Suc int_ofsubsection {* Code generator setup *}text {* Implementation of code numerals by bounded integers *}code_type code_numeral  (SML "int")  (OCaml "Big'_int.big'_int")  (Haskell "Integer")  (Scala "BigInt")code_instance code_numeral :: equal  (Haskell -)setup {*  Numeral.add_code @{const_name Num}    false Code_Printer.literal_naive_numeral "SML"  #> fold (Numeral.add_code @{const_name Num}    false Code_Printer.literal_numeral) ["OCaml", "Haskell", "Scala"]*}code_reserved SML Int intcode_reserved Eval Integercode_const "0::code_numeral"  (SML "0")  (OCaml "Big'_int.zero'_big'_int")  (Haskell "0")  (Scala "BigInt(0)")code_const "plus :: code_numeral => code_numeral => code_numeral"  (SML "Int.+/ ((_),/ (_))")  (OCaml "Big'_int.add'_big'_int")  (Haskell infixl 6 "+")  (Scala infixl 7 "+")  (Eval infixl 8 "+")code_const "minus :: code_numeral => code_numeral => code_numeral"  (SML "Int.max/ (0 : int,/ Int.-/ ((_),/ (_)))")  (OCaml "Big'_int.max'_big'_int/ Big'_int.zero'_big'_int/ (Big'_int.sub'_big'_int/ _/ _)")  (Haskell "Prelude.max/ (0 :: Integer)/ (_/ -/ _)")  (Scala "!(_/ -/ _).max(0)")  (Eval "Integer.max/ 0/ (_/ -/ _)")code_const "times :: code_numeral => code_numeral => code_numeral"  (SML "Int.*/ ((_),/ (_))")  (OCaml "Big'_int.mult'_big'_int")  (Haskell infixl 7 "*")  (Scala infixl 8 "*")  (Eval infixl 8 "*")code_const Code_Numeral.div_mod  (SML "!(fn n => fn m =>/ if m = 0/ then (0, n) else/ (Int.div (n, m), Int.mod (n, m)))")  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")  (Haskell "divMod")  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")  (Eval "!(fn n => fn m =>/ if m = 0/ then (0, n) else/ (Integer.div'_mod n m))")code_const "HOL.equal :: code_numeral => code_numeral => bool"  (SML "!((_ : Int.int) = _)")  (OCaml "Big'_int.eq'_big'_int")  (Haskell infix 4 "==")  (Scala infixl 5 "==")  (Eval "!((_ : int) = _)")code_const "less_eq :: code_numeral => code_numeral => bool"  (SML "Int.<=/ ((_),/ (_))")  (OCaml "Big'_int.le'_big'_int")  (Haskell infix 4 "<=")  (Scala infixl 4 "<=")  (Eval infixl 6 "<=")code_const "less :: code_numeral => code_numeral => bool"  (SML "Int.</ ((_),/ (_))")  (OCaml "Big'_int.lt'_big'_int")  (Haskell infix 4 "<")  (Scala infixl 4 "<")  (Eval infixl 6 "<")code_modulename SML  Code_Numeral Arithcode_modulename OCaml  Code_Numeral Arithcode_modulename Haskell  Code_Numeral Arithend`