src/HOL/Code_Numeral.thy
author haftmann
Fri Feb 24 09:40:02 2012 +0100 (2012-02-24)
changeset 46638 fc315796794e
parent 46635 cde737f9c911
child 46664 1f6c140f9c72
permissions -rw-r--r--
given up disfruitful branch
     1 (* Author: Florian Haftmann, TU Muenchen *)
     2 
     3 header {* Type of target language numerals *}
     4 
     5 theory Code_Numeral
     6 imports Nat_Numeral Nat_Transfer Divides
     7 begin
     8 
     9 text {*
    10   Code numerals are isomorphic to HOL @{typ nat} but
    11   mapped to target-language builtin numerals.
    12 *}
    13 
    14 subsection {* Datatype of target language numerals *}
    15 
    16 typedef (open) code_numeral = "UNIV \<Colon> nat set"
    17   morphisms nat_of of_nat ..
    18 
    19 lemma of_nat_nat_of [simp]:
    20   "of_nat (nat_of k) = k"
    21   by (rule nat_of_inverse)
    22 
    23 lemma nat_of_of_nat [simp]:
    24   "nat_of (of_nat n) = n"
    25   by (rule of_nat_inverse) (rule UNIV_I)
    26 
    27 lemma [measure_function]:
    28   "is_measure nat_of" by (rule is_measure_trivial)
    29 
    30 lemma code_numeral:
    31   "(\<And>n\<Colon>code_numeral. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (of_nat n))"
    32 proof
    33   fix n :: nat
    34   assume "\<And>n\<Colon>code_numeral. PROP P n"
    35   then show "PROP P (of_nat n)" .
    36 next
    37   fix n :: code_numeral
    38   assume "\<And>n\<Colon>nat. PROP P (of_nat n)"
    39   then have "PROP P (of_nat (nat_of n))" .
    40   then show "PROP P n" by simp
    41 qed
    42 
    43 lemma code_numeral_case:
    44   assumes "\<And>n. k = of_nat n \<Longrightarrow> P"
    45   shows P
    46   by (rule assms [of "nat_of k"]) simp
    47 
    48 lemma code_numeral_induct_raw:
    49   assumes "\<And>n. P (of_nat n)"
    50   shows "P k"
    51 proof -
    52   from assms have "P (of_nat (nat_of k))" .
    53   then show ?thesis by simp
    54 qed
    55 
    56 lemma nat_of_inject [simp]:
    57   "nat_of k = nat_of l \<longleftrightarrow> k = l"
    58   by (rule nat_of_inject)
    59 
    60 lemma of_nat_inject [simp]:
    61   "of_nat n = of_nat m \<longleftrightarrow> n = m"
    62   by (rule of_nat_inject) (rule UNIV_I)+
    63 
    64 instantiation code_numeral :: zero
    65 begin
    66 
    67 definition [simp, code del]:
    68   "0 = of_nat 0"
    69 
    70 instance ..
    71 
    72 end
    73 
    74 definition Suc where [simp]:
    75   "Suc k = of_nat (Nat.Suc (nat_of k))"
    76 
    77 rep_datatype "0 \<Colon> code_numeral" Suc
    78 proof -
    79   fix P :: "code_numeral \<Rightarrow> bool"
    80   fix k :: code_numeral
    81   assume "P 0" then have init: "P (of_nat 0)" by simp
    82   assume "\<And>k. P k \<Longrightarrow> P (Suc k)"
    83     then have "\<And>n. P (of_nat n) \<Longrightarrow> P (Suc (of_nat n))" .
    84     then have step: "\<And>n. P (of_nat n) \<Longrightarrow> P (of_nat (Nat.Suc n))" by simp
    85   from init step have "P (of_nat (nat_of k))"
    86     by (induct ("nat_of k")) simp_all
    87   then show "P k" by simp
    88 qed simp_all
    89 
    90 declare code_numeral_case [case_names nat, cases type: code_numeral]
    91 declare code_numeral.induct [case_names nat, induct type: code_numeral]
    92 
    93 lemma code_numeral_decr [termination_simp]:
    94   "k \<noteq> of_nat 0 \<Longrightarrow> nat_of k - Nat.Suc 0 < nat_of k"
    95   by (cases k) simp
    96 
    97 lemma [simp, code]:
    98   "code_numeral_size = nat_of"
    99 proof (rule ext)
   100   fix k
   101   have "code_numeral_size k = nat_size (nat_of k)"
   102     by (induct k rule: code_numeral.induct) (simp_all del: zero_code_numeral_def Suc_def, simp_all)
   103   also have "nat_size (nat_of k) = nat_of k" by (induct ("nat_of k")) simp_all
   104   finally show "code_numeral_size k = nat_of k" .
   105 qed
   106 
   107 lemma [simp, code]:
   108   "size = nat_of"
   109 proof (rule ext)
   110   fix k
   111   show "size k = nat_of k"
   112   by (induct k) (simp_all del: zero_code_numeral_def Suc_def, simp_all)
   113 qed
   114 
   115 lemmas [code del] = code_numeral.recs code_numeral.cases
   116 
   117 lemma [code]:
   118   "HOL.equal k l \<longleftrightarrow> HOL.equal (nat_of k) (nat_of l)"
   119   by (cases k, cases l) (simp add: equal)
   120 
   121 lemma [code nbe]:
   122   "HOL.equal (k::code_numeral) k \<longleftrightarrow> True"
   123   by (rule equal_refl)
   124 
   125 
   126 subsection {* Code numerals as datatype of ints *}
   127 
   128 instantiation code_numeral :: number
   129 begin
   130 
   131 definition
   132   "number_of = of_nat o nat"
   133 
   134 instance ..
   135 
   136 end
   137 
   138 lemma nat_of_number [simp]:
   139   "nat_of (number_of k) = number_of k"
   140   by (simp add: number_of_code_numeral_def nat_number_of_def number_of_is_id)
   141 
   142 code_datatype "number_of \<Colon> int \<Rightarrow> code_numeral"
   143 
   144 
   145 subsection {* Basic arithmetic *}
   146 
   147 instantiation code_numeral :: "{minus, linordered_semidom, semiring_div, linorder}"
   148 begin
   149 
   150 definition [simp, code del]:
   151   "(1\<Colon>code_numeral) = of_nat 1"
   152 
   153 definition [simp, code del]:
   154   "n + m = of_nat (nat_of n + nat_of m)"
   155 
   156 definition [simp, code del]:
   157   "n - m = of_nat (nat_of n - nat_of m)"
   158 
   159 definition [simp, code del]:
   160   "n * m = of_nat (nat_of n * nat_of m)"
   161 
   162 definition [simp, code del]:
   163   "n div m = of_nat (nat_of n div nat_of m)"
   164 
   165 definition [simp, code del]:
   166   "n mod m = of_nat (nat_of n mod nat_of m)"
   167 
   168 definition [simp, code del]:
   169   "n \<le> m \<longleftrightarrow> nat_of n \<le> nat_of m"
   170 
   171 definition [simp, code del]:
   172   "n < m \<longleftrightarrow> nat_of n < nat_of m"
   173 
   174 instance proof
   175 qed (auto simp add: code_numeral left_distrib intro: mult_commute)
   176 
   177 end
   178 
   179 lemma zero_code_numeral_code [code]:
   180   "(0\<Colon>code_numeral) = Numeral0"
   181   by (simp add: number_of_code_numeral_def Pls_def)
   182 
   183 lemma [code_abbrev]: "Numeral0 = (0\<Colon>code_numeral)"
   184   using zero_code_numeral_code ..
   185 
   186 lemma one_code_numeral_code [code]:
   187   "(1\<Colon>code_numeral) = Numeral1"
   188   by (simp add: number_of_code_numeral_def Pls_def Bit1_def)
   189 
   190 lemma [code_abbrev]: "Numeral1 = (1\<Colon>code_numeral)"
   191   using one_code_numeral_code ..
   192 
   193 lemma plus_code_numeral_code [code nbe]:
   194   "of_nat n + of_nat m = of_nat (n + m)"
   195   by simp
   196 
   197 definition subtract :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
   198   [simp]: "subtract = minus"
   199 
   200 lemma subtract_code [code nbe]:
   201   "subtract (of_nat n) (of_nat m) = of_nat (n - m)"
   202   by simp
   203 
   204 lemma minus_code_numeral_code [code]:
   205   "minus = subtract"
   206   by simp
   207 
   208 lemma times_code_numeral_code [code nbe]:
   209   "of_nat n * of_nat m = of_nat (n * m)"
   210   by simp
   211 
   212 lemma less_eq_code_numeral_code [code nbe]:
   213   "of_nat n \<le> of_nat m \<longleftrightarrow> n \<le> m"
   214   by simp
   215 
   216 lemma less_code_numeral_code [code nbe]:
   217   "of_nat n < of_nat m \<longleftrightarrow> n < m"
   218   by simp
   219 
   220 lemma code_numeral_zero_minus_one:
   221   "(0::code_numeral) - 1 = 0"
   222   by simp
   223 
   224 lemma Suc_code_numeral_minus_one:
   225   "Suc n - 1 = n"
   226   by simp
   227 
   228 lemma of_nat_code [code]:
   229   "of_nat = Nat.of_nat"
   230 proof
   231   fix n :: nat
   232   have "Nat.of_nat n = of_nat n"
   233     by (induct n) simp_all
   234   then show "of_nat n = Nat.of_nat n"
   235     by (rule sym)
   236 qed
   237 
   238 lemma code_numeral_not_eq_zero: "i \<noteq> of_nat 0 \<longleftrightarrow> i \<ge> 1"
   239   by (cases i) auto
   240 
   241 definition nat_of_aux :: "code_numeral \<Rightarrow> nat \<Rightarrow> nat" where
   242   "nat_of_aux i n = nat_of i + n"
   243 
   244 lemma nat_of_aux_code [code]:
   245   "nat_of_aux i n = (if i = 0 then n else nat_of_aux (i - 1) (Nat.Suc n))"
   246   by (auto simp add: nat_of_aux_def code_numeral_not_eq_zero)
   247 
   248 lemma nat_of_code [code]:
   249   "nat_of i = nat_of_aux i 0"
   250   by (simp add: nat_of_aux_def)
   251 
   252 definition div_mod :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral \<times> code_numeral" where
   253   [code del]: "div_mod n m = (n div m, n mod m)"
   254 
   255 lemma [code]:
   256   "div_mod n m = (if m = 0 then (0, n) else (n div m, n mod m))"
   257   unfolding div_mod_def by auto
   258 
   259 lemma [code]:
   260   "n div m = fst (div_mod n m)"
   261   unfolding div_mod_def by simp
   262 
   263 lemma [code]:
   264   "n mod m = snd (div_mod n m)"
   265   unfolding div_mod_def by simp
   266 
   267 definition int_of :: "code_numeral \<Rightarrow> int" where
   268   "int_of = Nat.of_nat o nat_of"
   269 
   270 lemma int_of_code [code]:
   271   "int_of k = (if k = 0 then 0
   272     else (if k mod 2 = 0 then 2 * int_of (k div 2) else 2 * int_of (k div 2) + 1))"
   273 proof -
   274   have "(nat_of k div 2) * 2 + nat_of k mod 2 = nat_of k" 
   275     by (rule mod_div_equality)
   276   then have "int ((nat_of k div 2) * 2 + nat_of k mod 2) = int (nat_of k)" 
   277     by simp
   278   then have "int (nat_of k) = int (nat_of k div 2) * 2 + int (nat_of k mod 2)" 
   279     unfolding of_nat_mult of_nat_add by simp
   280   then show ?thesis by (auto simp add: int_of_def mult_ac)
   281 qed
   282 
   283 
   284 text {* Lazy Evaluation of an indexed function *}
   285 
   286 function iterate_upto :: "(code_numeral \<Rightarrow> 'a) \<Rightarrow> code_numeral \<Rightarrow> code_numeral \<Rightarrow> 'a Predicate.pred"
   287 where
   288   "iterate_upto f n m =
   289     Predicate.Seq (%u. if n > m then Predicate.Empty
   290      else Predicate.Insert (f n) (iterate_upto f (n + 1) m))"
   291 by pat_completeness auto
   292 
   293 termination by (relation "measure (%(f, n, m). Code_Numeral.nat_of (m + 1 - n))") auto
   294 
   295 hide_const (open) of_nat nat_of Suc  subtract int_of iterate_upto
   296 
   297 
   298 subsection {* Code generator setup *}
   299 
   300 text {* Implementation of code numerals by bounded integers *}
   301 
   302 code_type code_numeral
   303   (SML "int")
   304   (OCaml "Big'_int.big'_int")
   305   (Haskell "Integer")
   306   (Scala "BigInt")
   307 
   308 code_instance code_numeral :: equal
   309   (Haskell -)
   310 
   311 setup {*
   312   Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
   313     false Code_Printer.literal_naive_numeral "SML"
   314   #> fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
   315     false Code_Printer.literal_numeral) ["OCaml", "Haskell", "Scala"]
   316 *}
   317 
   318 code_reserved SML Int int
   319 code_reserved Eval Integer
   320 
   321 code_const "plus \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   322   (SML "Int.+/ ((_),/ (_))")
   323   (OCaml "Big'_int.add'_big'_int")
   324   (Haskell infixl 6 "+")
   325   (Scala infixl 7 "+")
   326   (Eval infixl 8 "+")
   327 
   328 code_const "Code_Numeral.subtract \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   329   (SML "Int.max/ (_/ -/ _,/ 0 : int)")
   330   (OCaml "Big'_int.max'_big'_int/ (Big'_int.sub'_big'_int/ _/ _)/ Big'_int.zero'_big'_int")
   331   (Haskell "max/ (_/ -/ _)/ (0 :: Integer)")
   332   (Scala "!(_/ -/ _).max(0)")
   333   (Eval "Integer.max/ (_/ -/ _)/ 0")
   334 
   335 code_const "times \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   336   (SML "Int.*/ ((_),/ (_))")
   337   (OCaml "Big'_int.mult'_big'_int")
   338   (Haskell infixl 7 "*")
   339   (Scala infixl 8 "*")
   340   (Eval infixl 8 "*")
   341 
   342 code_const Code_Numeral.div_mod
   343   (SML "!(fn n => fn m =>/ if m = 0/ then (0, n) else/ (Int.div (n, m), Int.mod (n, m)))")
   344   (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
   345   (Haskell "divMod")
   346   (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
   347   (Eval "!(fn n => fn m =>/ if m = 0/ then (0, n) else/ (Integer.div'_mod n m))")
   348 
   349 code_const "HOL.equal \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   350   (SML "!((_ : Int.int) = _)")
   351   (OCaml "Big'_int.eq'_big'_int")
   352   (Haskell infix 4 "==")
   353   (Scala infixl 5 "==")
   354   (Eval "!((_ : int) = _)")
   355 
   356 code_const "less_eq \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   357   (SML "Int.<=/ ((_),/ (_))")
   358   (OCaml "Big'_int.le'_big'_int")
   359   (Haskell infix 4 "<=")
   360   (Scala infixl 4 "<=")
   361   (Eval infixl 6 "<=")
   362 
   363 code_const "less \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   364   (SML "Int.</ ((_),/ (_))")
   365   (OCaml "Big'_int.lt'_big'_int")
   366   (Haskell infix 4 "<")
   367   (Scala infixl 4 "<")
   368   (Eval infixl 6 "<")
   369 
   370 code_modulename SML
   371   Code_Numeral Arith
   372 
   373 code_modulename OCaml
   374   Code_Numeral Arith
   375 
   376 code_modulename Haskell
   377   Code_Numeral Arith
   378 
   379 end