src/HOL/Library/Code_Binary_Nat.thy
author haftmann
Sun, 23 Jun 2013 21:16:07 +0200
changeset 52435 6646bb548c6b
parent 51143 0a2371e7ced3
child 53069 d165213e3924
permissions -rw-r--r--
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50023
28f3263d4d1b refined stack of library theories implementing int and/or nat by target language numerals
haftmann
parents: 47108
diff changeset
     1
(*  Title:      HOL/Library/Code_Binary_Nat.thy
51113
222fb6cb2c3e factored out shared preprocessor setup into theory Code_Abstract_Nat, tuning descriptions
haftmann
parents: 50023
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     3
*)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     4
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     5
header {* Implementation of natural numbers as binary numerals *}
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     6
50023
28f3263d4d1b refined stack of library theories implementing int and/or nat by target language numerals
haftmann
parents: 47108
diff changeset
     7
theory Code_Binary_Nat
51113
222fb6cb2c3e factored out shared preprocessor setup into theory Code_Abstract_Nat, tuning descriptions
haftmann
parents: 50023
diff changeset
     8
imports Code_Abstract_Nat
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     9
begin
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    10
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    11
text {*
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    12
  When generating code for functions on natural numbers, the
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    13
  canonical representation using @{term "0::nat"} and
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    14
  @{term Suc} is unsuitable for computations involving large
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    15
  numbers.  This theory refines the representation of
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    16
  natural numbers for code generation to use binary
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    17
  numerals, which do not grow linear in size but logarithmic.
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    18
*}
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    19
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    20
subsection {* Representation *}
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    21
50023
28f3263d4d1b refined stack of library theories implementing int and/or nat by target language numerals
haftmann
parents: 47108
diff changeset
    22
code_datatype "0::nat" nat_of_num
28f3263d4d1b refined stack of library theories implementing int and/or nat by target language numerals
haftmann
parents: 47108
diff changeset
    23
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    24
lemma [code]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    25
  "num_of_nat 0 = Num.One"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    26
  "num_of_nat (nat_of_num k) = k"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    27
  by (simp_all add: nat_of_num_inverse)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    28
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    29
lemma [code]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    30
  "(1\<Colon>nat) = Numeral1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    31
  by simp
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    32
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    33
lemma [code_abbrev]: "Numeral1 = (1\<Colon>nat)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    34
  by simp
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    35
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    36
lemma [code]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    37
  "Suc n = n + 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    38
  by simp
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    39
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    40
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    41
subsection {* Basic arithmetic *}
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    42
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    43
lemma [code, code del]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    44
  "(plus :: nat \<Rightarrow> _) = plus" ..
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    45
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    46
lemma plus_nat_code [code]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    47
  "nat_of_num k + nat_of_num l = nat_of_num (k + l)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    48
  "m + 0 = (m::nat)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    49
  "0 + n = (n::nat)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    50
  by (simp_all add: nat_of_num_numeral)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    51
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    52
text {* Bounded subtraction needs some auxiliary *}
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    53
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    54
definition dup :: "nat \<Rightarrow> nat" where
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    55
  "dup n = n + n"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    56
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    57
lemma dup_code [code]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    58
  "dup 0 = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    59
  "dup (nat_of_num k) = nat_of_num (Num.Bit0 k)"
50023
28f3263d4d1b refined stack of library theories implementing int and/or nat by target language numerals
haftmann
parents: 47108
diff changeset
    60
  by (simp_all add: dup_def numeral_Bit0)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    61
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    62
definition sub :: "num \<Rightarrow> num \<Rightarrow> nat option" where
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    63
  "sub k l = (if k \<ge> l then Some (numeral k - numeral l) else None)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    64
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    65
lemma sub_code [code]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    66
  "sub Num.One Num.One = Some 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    67
  "sub (Num.Bit0 m) Num.One = Some (nat_of_num (Num.BitM m))"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    68
  "sub (Num.Bit1 m) Num.One = Some (nat_of_num (Num.Bit0 m))"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    69
  "sub Num.One (Num.Bit0 n) = None"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    70
  "sub Num.One (Num.Bit1 n) = None"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    71
  "sub (Num.Bit0 m) (Num.Bit0 n) = Option.map dup (sub m n)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    72
  "sub (Num.Bit1 m) (Num.Bit1 n) = Option.map dup (sub m n)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    73
  "sub (Num.Bit1 m) (Num.Bit0 n) = Option.map (\<lambda>q. dup q + 1) (sub m n)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    74
  "sub (Num.Bit0 m) (Num.Bit1 n) = (case sub m n of None \<Rightarrow> None
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    75
     | Some q \<Rightarrow> if q = 0 then None else Some (dup q - 1))"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    76
  apply (auto simp add: nat_of_num_numeral
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    77
    Num.dbl_def Num.dbl_inc_def Num.dbl_dec_def
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    78
    Let_def le_imp_diff_is_add BitM_plus_one sub_def dup_def)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    79
  apply (simp_all add: sub_non_positive)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    80
  apply (simp_all add: sub_non_negative [symmetric, where ?'a = int])
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    81
  done
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    82
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    83
lemma [code, code del]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    84
  "(minus :: nat \<Rightarrow> _) = minus" ..
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    85
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    86
lemma minus_nat_code [code]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    87
  "nat_of_num k - nat_of_num l = (case sub k l of None \<Rightarrow> 0 | Some j \<Rightarrow> j)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    88
  "m - 0 = (m::nat)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    89
  "0 - n = (0::nat)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    90
  by (simp_all add: nat_of_num_numeral sub_non_positive sub_def)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    91
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    92
lemma [code, code del]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    93
  "(times :: nat \<Rightarrow> _) = times" ..
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    94
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    95
lemma times_nat_code [code]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    96
  "nat_of_num k * nat_of_num l = nat_of_num (k * l)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    97
  "m * 0 = (0::nat)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    98
  "0 * n = (0::nat)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    99
  by (simp_all add: nat_of_num_numeral)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   100
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   101
lemma [code, code del]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   102
  "(HOL.equal :: nat \<Rightarrow> _) = HOL.equal" ..
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   103
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   104
lemma equal_nat_code [code]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   105
  "HOL.equal 0 (0::nat) \<longleftrightarrow> True"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   106
  "HOL.equal 0 (nat_of_num l) \<longleftrightarrow> False"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   107
  "HOL.equal (nat_of_num k) 0 \<longleftrightarrow> False"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   108
  "HOL.equal (nat_of_num k) (nat_of_num l) \<longleftrightarrow> HOL.equal k l"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   109
  by (simp_all add: nat_of_num_numeral equal)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   110
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   111
lemma equal_nat_refl [code nbe]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   112
  "HOL.equal (n::nat) n \<longleftrightarrow> True"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   113
  by (rule equal_refl)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   114
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   115
lemma [code, code del]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   116
  "(less_eq :: nat \<Rightarrow> _) = less_eq" ..
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   117
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   118
lemma less_eq_nat_code [code]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   119
  "0 \<le> (n::nat) \<longleftrightarrow> True"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   120
  "nat_of_num k \<le> 0 \<longleftrightarrow> False"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   121
  "nat_of_num k \<le> nat_of_num l \<longleftrightarrow> k \<le> l"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   122
  by (simp_all add: nat_of_num_numeral)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   123
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   124
lemma [code, code del]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   125
  "(less :: nat \<Rightarrow> _) = less" ..
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   126
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   127
lemma less_nat_code [code]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   128
  "(m::nat) < 0 \<longleftrightarrow> False"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   129
  "0 < nat_of_num l \<longleftrightarrow> True"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   130
  "nat_of_num k < nat_of_num l \<longleftrightarrow> k < l"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   131
  by (simp_all add: nat_of_num_numeral)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   132
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   133
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   134
subsection {* Conversions *}
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   135
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   136
lemma [code, code del]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   137
  "of_nat = of_nat" ..
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   138
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   139
lemma of_nat_code [code]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   140
  "of_nat 0 = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   141
  "of_nat (nat_of_num k) = numeral k"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   142
  by (simp_all add: nat_of_num_numeral)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   143
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   144
52435
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51143
diff changeset
   145
code_identifier
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51143
diff changeset
   146
  code_module Code_Binary_Nat \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51143
diff changeset
   147
    (SML) Arith and (OCaml) Arith and (Haskell) Arith
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   148
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   149
hide_const (open) dup sub
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   150
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
   151
end
50023
28f3263d4d1b refined stack of library theories implementing int and/or nat by target language numerals
haftmann
parents: 47108
diff changeset
   152