src/HOL/Library/Code_Target_Nat.thy
author wenzelm
Mon Dec 28 01:28:28 2015 +0100 (2015-12-28)
changeset 61945 1135b8de26c3
parent 61585 a9599d3d7610
child 64242 93c6f0da5c70
permissions -rw-r--r--
more symbols;
haftmann@50023
     1
(*  Title:      HOL/Library/Code_Target_Nat.thy
haftmann@51113
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@50023
     3
*)
haftmann@50023
     4
wenzelm@60500
     5
section \<open>Implementation of natural numbers by target-language integers\<close>
haftmann@50023
     6
haftmann@50023
     7
theory Code_Target_Nat
haftmann@51143
     8
imports Code_Abstract_Nat
haftmann@50023
     9
begin
haftmann@50023
    10
wenzelm@60500
    11
subsection \<open>Implementation for @{typ nat}\<close>
haftmann@50023
    12
kuncar@55736
    13
context
kuncar@55736
    14
includes natural.lifting integer.lifting
kuncar@55736
    15
begin
kuncar@55736
    16
haftmann@51114
    17
lift_definition Nat :: "integer \<Rightarrow> nat"
haftmann@51114
    18
  is nat
haftmann@51114
    19
  .
haftmann@50023
    20
haftmann@51095
    21
lemma [code_post]:
haftmann@51095
    22
  "Nat 0 = 0"
haftmann@51095
    23
  "Nat 1 = 1"
haftmann@51095
    24
  "Nat (numeral k) = numeral k"
haftmann@51114
    25
  by (transfer, simp)+
haftmann@50023
    26
haftmann@51095
    27
lemma [code_abbrev]:
haftmann@51095
    28
  "integer_of_nat = of_nat"
haftmann@51114
    29
  by transfer rule
haftmann@50023
    30
haftmann@50023
    31
lemma [code_unfold]:
haftmann@50023
    32
  "Int.nat (int_of_integer k) = nat_of_integer k"
haftmann@51114
    33
  by transfer rule
haftmann@50023
    34
haftmann@50023
    35
lemma [code abstype]:
haftmann@50023
    36
  "Code_Target_Nat.Nat (integer_of_nat n) = n"
haftmann@51114
    37
  by transfer simp
haftmann@50023
    38
haftmann@50023
    39
lemma [code abstract]:
haftmann@50023
    40
  "integer_of_nat (nat_of_integer k) = max 0 k"
haftmann@51114
    41
  by transfer auto
haftmann@50023
    42
haftmann@50023
    43
lemma [code_abbrev]:
haftmann@51095
    44
  "nat_of_integer (numeral k) = nat_of_num k"
haftmann@51114
    45
  by transfer (simp add: nat_of_num_numeral)
haftmann@50023
    46
haftmann@50023
    47
lemma [code abstract]:
haftmann@50023
    48
  "integer_of_nat (nat_of_num n) = integer_of_num n"
haftmann@51114
    49
  by transfer (simp add: nat_of_num_numeral)
haftmann@50023
    50
haftmann@50023
    51
lemma [code abstract]:
haftmann@50023
    52
  "integer_of_nat 0 = 0"
haftmann@51114
    53
  by transfer simp
haftmann@50023
    54
haftmann@50023
    55
lemma [code abstract]:
haftmann@50023
    56
  "integer_of_nat 1 = 1"
haftmann@51114
    57
  by transfer simp
haftmann@50023
    58
haftmann@51113
    59
lemma [code]:
haftmann@51113
    60
  "Suc n = n + 1"
haftmann@51113
    61
  by simp
haftmann@51113
    62
haftmann@50023
    63
lemma [code abstract]:
haftmann@50023
    64
  "integer_of_nat (m + n) = of_nat m + of_nat n"
haftmann@51114
    65
  by transfer simp
haftmann@50023
    66
haftmann@50023
    67
lemma [code abstract]:
haftmann@50023
    68
  "integer_of_nat (m - n) = max 0 (of_nat m - of_nat n)"
haftmann@51114
    69
  by transfer simp
haftmann@50023
    70
haftmann@50023
    71
lemma [code abstract]:
haftmann@50023
    72
  "integer_of_nat (m * n) = of_nat m * of_nat n"
haftmann@51114
    73
  by transfer (simp add: of_nat_mult)
haftmann@50023
    74
haftmann@50023
    75
lemma [code abstract]:
haftmann@50023
    76
  "integer_of_nat (m div n) = of_nat m div of_nat n"
haftmann@51114
    77
  by transfer (simp add: zdiv_int)
haftmann@50023
    78
haftmann@50023
    79
lemma [code abstract]:
haftmann@50023
    80
  "integer_of_nat (m mod n) = of_nat m mod of_nat n"
haftmann@51114
    81
  by transfer (simp add: zmod_int)
haftmann@50023
    82
haftmann@50023
    83
lemma [code]:
haftmann@50023
    84
  "Divides.divmod_nat m n = (m div n, m mod n)"
haftmann@54796
    85
  by (fact divmod_nat_div_mod)
haftmann@50023
    86
haftmann@50023
    87
lemma [code]:
haftmann@61275
    88
  "divmod m n = map_prod nat_of_integer nat_of_integer (divmod m n)"
haftmann@61275
    89
  by (simp only: prod_eq_iff divmod_def map_prod_def case_prod_beta fst_conv snd_conv)
haftmann@61275
    90
    (transfer, simp_all only: nat_div_distrib nat_mod_distrib
haftmann@61275
    91
        zero_le_numeral nat_numeral)
haftmann@61275
    92
  
haftmann@61275
    93
lemma [code]:
haftmann@50023
    94
  "HOL.equal m n = HOL.equal (of_nat m :: integer) (of_nat n)"
haftmann@51114
    95
  by transfer (simp add: equal)
haftmann@50023
    96
haftmann@50023
    97
lemma [code]:
haftmann@50023
    98
  "m \<le> n \<longleftrightarrow> (of_nat m :: integer) \<le> of_nat n"
haftmann@50023
    99
  by simp
haftmann@50023
   100
haftmann@50023
   101
lemma [code]:
haftmann@50023
   102
  "m < n \<longleftrightarrow> (of_nat m :: integer) < of_nat n"
haftmann@50023
   103
  by simp
haftmann@50023
   104
haftmann@50023
   105
lemma num_of_nat_code [code]:
haftmann@50023
   106
  "num_of_nat = num_of_integer \<circ> of_nat"
haftmann@51114
   107
  by transfer (simp add: fun_eq_iff)
haftmann@50023
   108
kuncar@55736
   109
end
kuncar@55736
   110
haftmann@54796
   111
lemma (in semiring_1) of_nat_code_if:
haftmann@50023
   112
  "of_nat n = (if n = 0 then 0
haftmann@50023
   113
     else let
haftmann@61433
   114
       (m, q) = Divides.divmod_nat n 2;
haftmann@50023
   115
       m' = 2 * of_nat m
haftmann@50023
   116
     in if q = 0 then m' else m' + 1)"
haftmann@50023
   117
proof -
haftmann@50023
   118
  from mod_div_equality have *: "of_nat n = of_nat (n div 2 * 2 + n mod 2)" by simp
haftmann@50023
   119
  show ?thesis
haftmann@54796
   120
    by (simp add: Let_def divmod_nat_div_mod of_nat_add [symmetric])
haftmann@57512
   121
      (simp add: * mult.commute of_nat_mult add.commute)
haftmann@50023
   122
qed
haftmann@50023
   123
haftmann@54796
   124
declare of_nat_code_if [code]
haftmann@50023
   125
haftmann@50023
   126
definition int_of_nat :: "nat \<Rightarrow> int" where
haftmann@50023
   127
  [code_abbrev]: "int_of_nat = of_nat"
haftmann@50023
   128
haftmann@50023
   129
lemma [code]:
haftmann@50023
   130
  "int_of_nat n = int_of_integer (of_nat n)"
haftmann@50023
   131
  by (simp add: int_of_nat_def)
haftmann@50023
   132
haftmann@50023
   133
lemma [code abstract]:
haftmann@50023
   134
  "integer_of_nat (nat k) = max 0 (integer_of_int k)"
kuncar@55736
   135
  including integer.lifting by transfer auto
haftmann@50023
   136
Andreas@53027
   137
lemma term_of_nat_code [code]:
wenzelm@61585
   138
  \<comment> \<open>Use @{term Code_Numeral.nat_of_integer} in term reconstruction
Andreas@53027
   139
        instead of @{term Code_Target_Nat.Nat} such that reconstructed
wenzelm@60500
   140
        terms can be fed back to the code generator\<close>
Andreas@53027
   141
  "term_of_class.term_of n =
Andreas@53027
   142
   Code_Evaluation.App
Andreas@53027
   143
     (Code_Evaluation.Const (STR ''Code_Numeral.nat_of_integer'')
Andreas@53027
   144
        (typerep.Typerep (STR ''fun'')
Andreas@53027
   145
           [typerep.Typerep (STR ''Code_Numeral.integer'') [],
Andreas@53027
   146
         typerep.Typerep (STR ''Nat.nat'') []]))
Andreas@53027
   147
     (term_of_class.term_of (integer_of_nat n))"
haftmann@54796
   148
  by (simp add: term_of_anything)
Andreas@53027
   149
Andreas@53027
   150
lemma nat_of_integer_code_post [code_post]:
Andreas@53027
   151
  "nat_of_integer 0 = 0"
Andreas@53027
   152
  "nat_of_integer 1 = 1"
Andreas@53027
   153
  "nat_of_integer (numeral k) = numeral k"
kuncar@55736
   154
  including integer.lifting by (transfer, simp)+
Andreas@53027
   155
haftmann@52435
   156
code_identifier
haftmann@52435
   157
  code_module Code_Target_Nat \<rightharpoonup>
haftmann@52435
   158
    (SML) Arith and (OCaml) Arith and (Haskell) Arith
haftmann@50023
   159
haftmann@50023
   160
end