src/HOL/Library/Code_Target_Nat.thy
author haftmann
Tue Dec 17 22:34:26 2013 +0100 (2013-12-17)
changeset 54796 cdc6d8cbf770
parent 53027 1774d569b604
child 55736 f1ed1e9cd080
permissions -rw-r--r--
avoid clashes of fact names
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
haftmann@51114
    13
lift_definition Nat :: "integer \<Rightarrow> nat"
haftmann@51114
    14
  is nat
haftmann@51114
    15
  .
haftmann@50023
    16
haftmann@51095
    17
lemma [code_post]:
haftmann@51095
    18
  "Nat 0 = 0"
haftmann@51095
    19
  "Nat 1 = 1"
haftmann@51095
    20
  "Nat (numeral k) = numeral k"
haftmann@51114
    21
  by (transfer, simp)+
haftmann@50023
    22
haftmann@51095
    23
lemma [code_abbrev]:
haftmann@51095
    24
  "integer_of_nat = of_nat"
haftmann@51114
    25
  by transfer rule
haftmann@50023
    26
haftmann@50023
    27
lemma [code_unfold]:
haftmann@50023
    28
  "Int.nat (int_of_integer k) = nat_of_integer k"
haftmann@51114
    29
  by transfer rule
haftmann@50023
    30
haftmann@50023
    31
lemma [code abstype]:
haftmann@50023
    32
  "Code_Target_Nat.Nat (integer_of_nat n) = n"
haftmann@51114
    33
  by transfer simp
haftmann@50023
    34
haftmann@50023
    35
lemma [code abstract]:
haftmann@50023
    36
  "integer_of_nat (nat_of_integer k) = max 0 k"
haftmann@51114
    37
  by transfer auto
haftmann@50023
    38
haftmann@50023
    39
lemma [code_abbrev]:
haftmann@51095
    40
  "nat_of_integer (numeral k) = nat_of_num k"
haftmann@51114
    41
  by transfer (simp add: nat_of_num_numeral)
haftmann@50023
    42
haftmann@50023
    43
lemma [code abstract]:
haftmann@50023
    44
  "integer_of_nat (nat_of_num n) = integer_of_num n"
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 0 = 0"
haftmann@51114
    49
  by transfer simp
haftmann@50023
    50
haftmann@50023
    51
lemma [code abstract]:
haftmann@50023
    52
  "integer_of_nat 1 = 1"
haftmann@51114
    53
  by transfer simp
haftmann@50023
    54
haftmann@51113
    55
lemma [code]:
haftmann@51113
    56
  "Suc n = n + 1"
haftmann@51113
    57
  by simp
haftmann@51113
    58
haftmann@50023
    59
lemma [code abstract]:
haftmann@50023
    60
  "integer_of_nat (m + n) = of_nat m + of_nat n"
haftmann@51114
    61
  by transfer simp
haftmann@50023
    62
haftmann@50023
    63
lemma [code abstract]:
haftmann@50023
    64
  "integer_of_nat (m - n) = max 0 (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) = of_nat m * of_nat n"
haftmann@51114
    69
  by transfer (simp add: of_nat_mult)
haftmann@50023
    70
haftmann@50023
    71
lemma [code abstract]:
haftmann@50023
    72
  "integer_of_nat (m div n) = of_nat m div of_nat n"
haftmann@51114
    73
  by transfer (simp add: zdiv_int)
haftmann@50023
    74
haftmann@50023
    75
lemma [code abstract]:
haftmann@50023
    76
  "integer_of_nat (m mod n) = of_nat m mod of_nat n"
haftmann@51114
    77
  by transfer (simp add: zmod_int)
haftmann@50023
    78
haftmann@50023
    79
lemma [code]:
haftmann@50023
    80
  "Divides.divmod_nat m n = (m div n, m mod n)"
haftmann@54796
    81
  by (fact divmod_nat_div_mod)
haftmann@50023
    82
haftmann@50023
    83
lemma [code]:
haftmann@50023
    84
  "HOL.equal m n = HOL.equal (of_nat m :: integer) (of_nat n)"
haftmann@51114
    85
  by transfer (simp add: equal)
haftmann@50023
    86
haftmann@50023
    87
lemma [code]:
haftmann@50023
    88
  "m \<le> n \<longleftrightarrow> (of_nat m :: integer) \<le> of_nat n"
haftmann@50023
    89
  by simp
haftmann@50023
    90
haftmann@50023
    91
lemma [code]:
haftmann@50023
    92
  "m < n \<longleftrightarrow> (of_nat m :: integer) < of_nat n"
haftmann@50023
    93
  by simp
haftmann@50023
    94
haftmann@50023
    95
lemma num_of_nat_code [code]:
haftmann@50023
    96
  "num_of_nat = num_of_integer \<circ> of_nat"
haftmann@51114
    97
  by transfer (simp add: fun_eq_iff)
haftmann@50023
    98
haftmann@54796
    99
lemma (in semiring_1) of_nat_code_if:
haftmann@50023
   100
  "of_nat n = (if n = 0 then 0
haftmann@50023
   101
     else let
haftmann@50023
   102
       (m, q) = divmod_nat n 2;
haftmann@50023
   103
       m' = 2 * of_nat m
haftmann@50023
   104
     in if q = 0 then m' else m' + 1)"
haftmann@50023
   105
proof -
haftmann@50023
   106
  from mod_div_equality have *: "of_nat n = of_nat (n div 2 * 2 + n mod 2)" by simp
haftmann@50023
   107
  show ?thesis
haftmann@54796
   108
    by (simp add: Let_def divmod_nat_div_mod of_nat_add [symmetric])
haftmann@50023
   109
      (simp add: * mult_commute of_nat_mult add_commute)
haftmann@50023
   110
qed
haftmann@50023
   111
haftmann@54796
   112
declare of_nat_code_if [code]
haftmann@50023
   113
haftmann@50023
   114
definition int_of_nat :: "nat \<Rightarrow> int" where
haftmann@50023
   115
  [code_abbrev]: "int_of_nat = of_nat"
haftmann@50023
   116
haftmann@50023
   117
lemma [code]:
haftmann@50023
   118
  "int_of_nat n = int_of_integer (of_nat n)"
haftmann@50023
   119
  by (simp add: int_of_nat_def)
haftmann@50023
   120
haftmann@50023
   121
lemma [code abstract]:
haftmann@50023
   122
  "integer_of_nat (nat k) = max 0 (integer_of_int k)"
haftmann@51114
   123
  by transfer auto
haftmann@50023
   124
Andreas@53027
   125
lemma term_of_nat_code [code]:
Andreas@53027
   126
  -- {* Use @{term Code_Numeral.nat_of_integer} in term reconstruction
Andreas@53027
   127
        instead of @{term Code_Target_Nat.Nat} such that reconstructed
Andreas@53027
   128
        terms can be fed back to the code generator *}
Andreas@53027
   129
  "term_of_class.term_of n =
Andreas@53027
   130
   Code_Evaluation.App
Andreas@53027
   131
     (Code_Evaluation.Const (STR ''Code_Numeral.nat_of_integer'')
Andreas@53027
   132
        (typerep.Typerep (STR ''fun'')
Andreas@53027
   133
           [typerep.Typerep (STR ''Code_Numeral.integer'') [],
Andreas@53027
   134
         typerep.Typerep (STR ''Nat.nat'') []]))
Andreas@53027
   135
     (term_of_class.term_of (integer_of_nat n))"
haftmann@54796
   136
  by (simp add: term_of_anything)
Andreas@53027
   137
Andreas@53027
   138
lemma nat_of_integer_code_post [code_post]:
Andreas@53027
   139
  "nat_of_integer 0 = 0"
Andreas@53027
   140
  "nat_of_integer 1 = 1"
Andreas@53027
   141
  "nat_of_integer (numeral k) = numeral k"
haftmann@54796
   142
  by (transfer, simp)+
Andreas@53027
   143
haftmann@52435
   144
code_identifier
haftmann@52435
   145
  code_module Code_Target_Nat \<rightharpoonup>
haftmann@52435
   146
    (SML) Arith and (OCaml) Arith and (Haskell) Arith
haftmann@50023
   147
haftmann@50023
   148
end