src/HOL/Library/Code_Target_Nat.thy
author haftmann
Thu Feb 14 12:24:56 2013 +0100 (2013-02-14)
changeset 51113 222fb6cb2c3e
parent 51095 7ae79f2e3cc7
child 51114 3e913a575dc6
permissions -rw-r--r--
factored out shared preprocessor setup into theory Code_Abstract_Nat, tuning descriptions
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@51113
     8
imports Code_Abstract_Nat Code_Numeral_Types
haftmann@50023
     9
begin
haftmann@50023
    10
haftmann@50023
    11
subsection {* Implementation for @{typ nat} *}
haftmann@50023
    12
haftmann@50023
    13
definition Nat :: "integer \<Rightarrow> nat"
haftmann@50023
    14
where
haftmann@50023
    15
  "Nat = nat_of_integer"
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@51095
    21
  by (simp_all add: Nat_def nat_of_integer_def)
haftmann@50023
    22
haftmann@51095
    23
lemma [code_abbrev]:
haftmann@51095
    24
  "integer_of_nat = of_nat"
haftmann@51095
    25
  by (fact integer_of_nat_def)
haftmann@50023
    26
haftmann@50023
    27
lemma [code_unfold]:
haftmann@50023
    28
  "Int.nat (int_of_integer k) = nat_of_integer k"
haftmann@50023
    29
  by (simp add: nat_of_integer_def)
haftmann@50023
    30
haftmann@50023
    31
lemma [code abstype]:
haftmann@50023
    32
  "Code_Target_Nat.Nat (integer_of_nat n) = n"
haftmann@50023
    33
  by (simp add: Nat_def integer_of_nat_def)
haftmann@50023
    34
haftmann@50023
    35
lemma [code abstract]:
haftmann@50023
    36
  "integer_of_nat (nat_of_integer k) = max 0 k"
haftmann@50023
    37
  by (simp add: integer_of_nat_def)
haftmann@50023
    38
haftmann@50023
    39
lemma [code_abbrev]:
haftmann@51095
    40
  "nat_of_integer (numeral k) = nat_of_num k"
haftmann@50023
    41
  by (simp add: nat_of_integer_def 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@50023
    45
  by (simp add: integer_eq_iff integer_of_num_def nat_of_num_numeral)
haftmann@50023
    46
haftmann@50023
    47
lemma [code abstract]:
haftmann@50023
    48
  "integer_of_nat 0 = 0"
haftmann@50023
    49
  by (simp add: integer_eq_iff integer_of_nat_def)
haftmann@50023
    50
haftmann@50023
    51
lemma [code abstract]:
haftmann@50023
    52
  "integer_of_nat 1 = 1"
haftmann@50023
    53
  by (simp add: integer_eq_iff integer_of_nat_def)
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@50023
    61
  by (simp add: integer_eq_iff integer_of_nat_def)
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@50023
    65
  by (simp add: integer_eq_iff integer_of_nat_def)
haftmann@50023
    66
haftmann@50023
    67
lemma [code abstract]:
haftmann@50023
    68
  "integer_of_nat (m * n) = of_nat m * of_nat n"
haftmann@50023
    69
  by (simp add: integer_eq_iff of_nat_mult integer_of_nat_def)
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@50023
    73
  by (simp add: integer_eq_iff zdiv_int integer_of_nat_def)
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@50023
    77
  by (simp add: integer_eq_iff zmod_int integer_of_nat_def)
haftmann@50023
    78
haftmann@50023
    79
lemma [code]:
haftmann@50023
    80
  "Divides.divmod_nat m n = (m div n, m mod n)"
haftmann@50023
    81
  by (simp add: prod_eq_iff)
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@50023
    85
  by (simp add: equal integer_eq_iff)
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@50023
    97
  by (simp add: fun_eq_iff num_of_integer_def integer_of_nat_def)
haftmann@50023
    98
haftmann@50023
    99
lemma (in semiring_1) of_nat_code:
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@50023
   108
    by (simp add: Let_def divmod_nat_div_mod mod_2_not_eq_zero_eq_one_nat
haftmann@50023
   109
      of_nat_add [symmetric])
haftmann@50023
   110
      (simp add: * mult_commute of_nat_mult add_commute)
haftmann@50023
   111
qed
haftmann@50023
   112
haftmann@50023
   113
declare of_nat_code [code]
haftmann@50023
   114
haftmann@50023
   115
definition int_of_nat :: "nat \<Rightarrow> int" where
haftmann@50023
   116
  [code_abbrev]: "int_of_nat = of_nat"
haftmann@50023
   117
haftmann@50023
   118
lemma [code]:
haftmann@50023
   119
  "int_of_nat n = int_of_integer (of_nat n)"
haftmann@50023
   120
  by (simp add: int_of_nat_def)
haftmann@50023
   121
haftmann@50023
   122
lemma [code abstract]:
haftmann@50023
   123
  "integer_of_nat (nat k) = max 0 (integer_of_int k)"
haftmann@50023
   124
  by (simp add: integer_of_nat_def of_int_of_nat max_def)
haftmann@50023
   125
haftmann@50023
   126
code_modulename SML
haftmann@50023
   127
  Code_Target_Nat Arith
haftmann@50023
   128
haftmann@50023
   129
code_modulename OCaml
haftmann@50023
   130
  Code_Target_Nat Arith
haftmann@50023
   131
haftmann@50023
   132
code_modulename Haskell
haftmann@50023
   133
  Code_Target_Nat Arith
haftmann@50023
   134
haftmann@50023
   135
end
haftmann@50023
   136