src/HOL/Library/Code_Target_Nat.thy
author kuncar
Tue Feb 25 19:07:14 2014 +0100 (2014-02-25)
changeset 55736 f1ed1e9cd080
parent 54796 cdc6d8cbf770
child 57512 cc97b347b301
permissions -rw-r--r--
unregister lifting setup following the best practice of Lifting
haftmann@50023
     1
(*  Title:      HOL/Library/Code_Target_Nat.thy
haftmann@51113
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@50023
     3
*)
haftmann@50023
     4
haftmann@50023
     5
header {* Implementation of natural numbers by target-language integers *}
haftmann@50023
     6
haftmann@50023
     7
theory Code_Target_Nat
haftmann@51143
     8
imports Code_Abstract_Nat
haftmann@50023
     9
begin
haftmann@50023
    10
haftmann@50023
    11
subsection {* Implementation for @{typ nat} *}
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@50023
    88
  "HOL.equal m n = HOL.equal (of_nat m :: integer) (of_nat n)"
haftmann@51114
    89
  by transfer (simp add: equal)
haftmann@50023
    90
haftmann@50023
    91
lemma [code]:
haftmann@50023
    92
  "m \<le> n \<longleftrightarrow> (of_nat m :: integer) \<le> of_nat n"
haftmann@50023
    93
  by simp
haftmann@50023
    94
haftmann@50023
    95
lemma [code]:
haftmann@50023
    96
  "m < n \<longleftrightarrow> (of_nat m :: integer) < of_nat n"
haftmann@50023
    97
  by simp
haftmann@50023
    98
haftmann@50023
    99
lemma num_of_nat_code [code]:
haftmann@50023
   100
  "num_of_nat = num_of_integer \<circ> of_nat"
haftmann@51114
   101
  by transfer (simp add: fun_eq_iff)
haftmann@50023
   102
kuncar@55736
   103
end
kuncar@55736
   104
haftmann@54796
   105
lemma (in semiring_1) of_nat_code_if:
haftmann@50023
   106
  "of_nat n = (if n = 0 then 0
haftmann@50023
   107
     else let
haftmann@50023
   108
       (m, q) = divmod_nat n 2;
haftmann@50023
   109
       m' = 2 * of_nat m
haftmann@50023
   110
     in if q = 0 then m' else m' + 1)"
haftmann@50023
   111
proof -
haftmann@50023
   112
  from mod_div_equality have *: "of_nat n = of_nat (n div 2 * 2 + n mod 2)" by simp
haftmann@50023
   113
  show ?thesis
haftmann@54796
   114
    by (simp add: Let_def divmod_nat_div_mod of_nat_add [symmetric])
haftmann@50023
   115
      (simp add: * mult_commute of_nat_mult add_commute)
haftmann@50023
   116
qed
haftmann@50023
   117
haftmann@54796
   118
declare of_nat_code_if [code]
haftmann@50023
   119
haftmann@50023
   120
definition int_of_nat :: "nat \<Rightarrow> int" where
haftmann@50023
   121
  [code_abbrev]: "int_of_nat = of_nat"
haftmann@50023
   122
haftmann@50023
   123
lemma [code]:
haftmann@50023
   124
  "int_of_nat n = int_of_integer (of_nat n)"
haftmann@50023
   125
  by (simp add: int_of_nat_def)
haftmann@50023
   126
haftmann@50023
   127
lemma [code abstract]:
haftmann@50023
   128
  "integer_of_nat (nat k) = max 0 (integer_of_int k)"
kuncar@55736
   129
  including integer.lifting by transfer auto
haftmann@50023
   130
Andreas@53027
   131
lemma term_of_nat_code [code]:
Andreas@53027
   132
  -- {* Use @{term Code_Numeral.nat_of_integer} in term reconstruction
Andreas@53027
   133
        instead of @{term Code_Target_Nat.Nat} such that reconstructed
Andreas@53027
   134
        terms can be fed back to the code generator *}
Andreas@53027
   135
  "term_of_class.term_of n =
Andreas@53027
   136
   Code_Evaluation.App
Andreas@53027
   137
     (Code_Evaluation.Const (STR ''Code_Numeral.nat_of_integer'')
Andreas@53027
   138
        (typerep.Typerep (STR ''fun'')
Andreas@53027
   139
           [typerep.Typerep (STR ''Code_Numeral.integer'') [],
Andreas@53027
   140
         typerep.Typerep (STR ''Nat.nat'') []]))
Andreas@53027
   141
     (term_of_class.term_of (integer_of_nat n))"
haftmann@54796
   142
  by (simp add: term_of_anything)
Andreas@53027
   143
Andreas@53027
   144
lemma nat_of_integer_code_post [code_post]:
Andreas@53027
   145
  "nat_of_integer 0 = 0"
Andreas@53027
   146
  "nat_of_integer 1 = 1"
Andreas@53027
   147
  "nat_of_integer (numeral k) = numeral k"
kuncar@55736
   148
  including integer.lifting by (transfer, simp)+
Andreas@53027
   149
haftmann@52435
   150
code_identifier
haftmann@52435
   151
  code_module Code_Target_Nat \<rightharpoonup>
haftmann@52435
   152
    (SML) Arith and (OCaml) Arith and (Haskell) Arith
haftmann@50023
   153
haftmann@50023
   154
end