src/HOL/Integ/NatBin.thy
author berghofe
Mon, 22 Sep 2003 16:04:49 +0200
changeset 14194 8953b566dfed
parent 13491 ddf6ae639f21
child 14272 5efbb548107d
permissions -rw-r--r--
Improved efficiency of code generated for functions int and nat.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7032
d6efb3b8e669 NatBin: binary arithmetic for the naturals
paulson
parents:
diff changeset
     1
(*  Title:      HOL/NatBin.thy
d6efb3b8e669 NatBin: binary arithmetic for the naturals
paulson
parents:
diff changeset
     2
    ID:         $Id$
d6efb3b8e669 NatBin: binary arithmetic for the naturals
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
d6efb3b8e669 NatBin: binary arithmetic for the naturals
paulson
parents:
diff changeset
     4
    Copyright   1999  University of Cambridge
12838
wenzelm
parents: 12440
diff changeset
     5
*)
7032
d6efb3b8e669 NatBin: binary arithmetic for the naturals
paulson
parents:
diff changeset
     6
12838
wenzelm
parents: 12440
diff changeset
     7
header {* Binary arithmetic for the natural numbers *}
7032
d6efb3b8e669 NatBin: binary arithmetic for the naturals
paulson
parents:
diff changeset
     8
10574
8f98f0301d67 Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents: 9509
diff changeset
     9
theory NatBin = IntPower
8f98f0301d67 Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents: 9509
diff changeset
    10
files ("nat_bin.ML"):
7032
d6efb3b8e669 NatBin: binary arithmetic for the naturals
paulson
parents:
diff changeset
    11
12838
wenzelm
parents: 12440
diff changeset
    12
text {*
wenzelm
parents: 12440
diff changeset
    13
  This case is simply reduced to that for the non-negative integers.
wenzelm
parents: 12440
diff changeset
    14
*}
wenzelm
parents: 12440
diff changeset
    15
wenzelm
parents: 12440
diff changeset
    16
wenzelm
parents: 12440
diff changeset
    17
instance nat :: number ..
wenzelm
parents: 12440
diff changeset
    18
wenzelm
parents: 12440
diff changeset
    19
defs (overloaded)
wenzelm
parents: 12440
diff changeset
    20
  nat_number_of_def: "(number_of::bin => nat) v == nat ((number_of :: bin => int) v)"
wenzelm
parents: 12440
diff changeset
    21
wenzelm
parents: 12440
diff changeset
    22
use "nat_bin.ML"
wenzelm
parents: 12440
diff changeset
    23
setup nat_bin_arith_setup
wenzelm
parents: 12440
diff changeset
    24
13189
81ed5c6de890 Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents: 13154
diff changeset
    25
(* Enable arith to deal with div/mod k where k is a numeral: *)
81ed5c6de890 Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents: 13154
diff changeset
    26
declare split_div[of _ _ "number_of k", standard, arith_split]
81ed5c6de890 Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents: 13154
diff changeset
    27
declare split_mod[of _ _ "number_of k", standard, arith_split]
13154
f1097ea60ba4 Set up arith to deal with div 2 and mod 2.
nipkow
parents: 13043
diff changeset
    28
13491
ddf6ae639f21 *** empty log message ***
nipkow
parents: 13189
diff changeset
    29
lemma nat_number_of_Pls: "number_of bin.Pls = (0::nat)"
12838
wenzelm
parents: 12440
diff changeset
    30
  by (simp add: number_of_Pls nat_number_of_def)
wenzelm
parents: 12440
diff changeset
    31
13491
ddf6ae639f21 *** empty log message ***
nipkow
parents: 13189
diff changeset
    32
lemma nat_number_of_Min: "number_of bin.Min = (0::nat)"
12838
wenzelm
parents: 12440
diff changeset
    33
  apply (simp only: number_of_Min nat_number_of_def nat_zminus_int)
wenzelm
parents: 12440
diff changeset
    34
  apply (simp add: neg_nat)
wenzelm
parents: 12440
diff changeset
    35
  done
7032
d6efb3b8e669 NatBin: binary arithmetic for the naturals
paulson
parents:
diff changeset
    36
12838
wenzelm
parents: 12440
diff changeset
    37
lemma nat_number_of_BIT_True:
wenzelm
parents: 12440
diff changeset
    38
  "number_of (w BIT True) =
wenzelm
parents: 12440
diff changeset
    39
    (if neg (number_of w) then 0
wenzelm
parents: 12440
diff changeset
    40
     else let n = number_of w in Suc (n + n))"
wenzelm
parents: 12440
diff changeset
    41
  apply (simp only: nat_number_of_def Let_def split: split_if)
wenzelm
parents: 12440
diff changeset
    42
  apply (intro conjI impI)
wenzelm
parents: 12440
diff changeset
    43
   apply (simp add: neg_nat neg_number_of_BIT)
wenzelm
parents: 12440
diff changeset
    44
  apply (rule int_int_eq [THEN iffD1])
wenzelm
parents: 12440
diff changeset
    45
  apply (simp only: not_neg_nat neg_number_of_BIT int_Suc zadd_int [symmetric] simp_thms)
wenzelm
parents: 12440
diff changeset
    46
  apply (simp only: number_of_BIT if_True zadd_assoc)
wenzelm
parents: 12440
diff changeset
    47
  done
7032
d6efb3b8e669 NatBin: binary arithmetic for the naturals
paulson
parents:
diff changeset
    48
12838
wenzelm
parents: 12440
diff changeset
    49
lemma nat_number_of_BIT_False:
wenzelm
parents: 12440
diff changeset
    50
    "number_of (w BIT False) = (let n::nat = number_of w in n + n)"
wenzelm
parents: 12440
diff changeset
    51
  apply (simp only: nat_number_of_def Let_def)
wenzelm
parents: 12440
diff changeset
    52
  apply (cases "neg (number_of w)")
wenzelm
parents: 12440
diff changeset
    53
   apply (simp add: neg_nat neg_number_of_BIT)
wenzelm
parents: 12440
diff changeset
    54
  apply (rule int_int_eq [THEN iffD1])
wenzelm
parents: 12440
diff changeset
    55
  apply (simp only: not_neg_nat neg_number_of_BIT int_Suc zadd_int [symmetric] simp_thms)
wenzelm
parents: 12440
diff changeset
    56
  apply (simp only: number_of_BIT if_False zadd_0 zadd_assoc)
wenzelm
parents: 12440
diff changeset
    57
  done
wenzelm
parents: 12440
diff changeset
    58
13043
ad1828b479b7 renamed nat_number_of to nat_number (avoid clash with separate theorem);
wenzelm
parents: 12933
diff changeset
    59
lemmas nat_number =
12838
wenzelm
parents: 12440
diff changeset
    60
  nat_number_of_Pls nat_number_of_Min
wenzelm
parents: 12440
diff changeset
    61
  nat_number_of_BIT_True nat_number_of_BIT_False
wenzelm
parents: 12440
diff changeset
    62
wenzelm
parents: 12440
diff changeset
    63
lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
wenzelm
parents: 12440
diff changeset
    64
  by (simp add: Let_def)
10574
8f98f0301d67 Linear arithmetic now copes with mixed nat/int formulae.
nipkow
parents: 9509
diff changeset
    65
12440
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
    66
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
    67
subsection {* Configuration of the code generator *}
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
    68
12933
b85c62c4e826 Introduced variants of operators + * ~ constrained to type int
berghofe
parents: 12838
diff changeset
    69
ML {*
b85c62c4e826 Introduced variants of operators + * ~ constrained to type int
berghofe
parents: 12838
diff changeset
    70
infix 7 `*;
b85c62c4e826 Introduced variants of operators + * ~ constrained to type int
berghofe
parents: 12838
diff changeset
    71
infix 6 `+;
b85c62c4e826 Introduced variants of operators + * ~ constrained to type int
berghofe
parents: 12838
diff changeset
    72
b85c62c4e826 Introduced variants of operators + * ~ constrained to type int
berghofe
parents: 12838
diff changeset
    73
val op `* = op * : int * int -> int;
b85c62c4e826 Introduced variants of operators + * ~ constrained to type int
berghofe
parents: 12838
diff changeset
    74
val op `+ = op + : int * int -> int;
b85c62c4e826 Introduced variants of operators + * ~ constrained to type int
berghofe
parents: 12838
diff changeset
    75
val `~ = ~ : int -> int;
b85c62c4e826 Introduced variants of operators + * ~ constrained to type int
berghofe
parents: 12838
diff changeset
    76
*}
b85c62c4e826 Introduced variants of operators + * ~ constrained to type int
berghofe
parents: 12838
diff changeset
    77
12440
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
    78
types_code
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
    79
  "int" ("int")
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
    80
14194
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    81
constdefs
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    82
  int_aux :: "int \<Rightarrow> nat \<Rightarrow> int"
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    83
  "int_aux i n == (i + int n)"
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    84
  nat_aux :: "nat \<Rightarrow> int \<Rightarrow> nat"
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    85
  "nat_aux n i == (n + nat i)"
12440
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
    86
14194
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    87
lemma [code]:
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    88
  "int_aux i 0 = i"
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    89
  "int_aux i (Suc n) = int_aux (i + 1) n" -- {* tail recursive *}
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    90
  "int n = int_aux 0 n"
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    91
  by (simp add: int_aux_def)+
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    92
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    93
lemma [code]: "nat_aux n i = (if i <= 0 then n else nat_aux (Suc n) (i - 1))"
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    94
  by (simp add: nat_aux_def Suc_nat_eq_nat_zadd1) -- {* tail recursive *}
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    95
lemma [code]: "nat i = nat_aux 0 i"
8953b566dfed Improved efficiency of code generated for functions int and nat.
berghofe
parents: 13491
diff changeset
    96
  by (simp add: nat_aux_def)
12440
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
    97
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
    98
consts_code
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
    99
  "0" :: "int"                  ("0")
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
   100
  "1" :: "int"                  ("1")
12933
b85c62c4e826 Introduced variants of operators + * ~ constrained to type int
berghofe
parents: 12838
diff changeset
   101
  "uminus" :: "int => int"      ("`~")
b85c62c4e826 Introduced variants of operators + * ~ constrained to type int
berghofe
parents: 12838
diff changeset
   102
  "op +" :: "int => int => int" ("(_ `+/ _)")
b85c62c4e826 Introduced variants of operators + * ~ constrained to type int
berghofe
parents: 12838
diff changeset
   103
  "op *" :: "int => int => int" ("(_ `*/ _)")
12440
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
   104
  "neg"                         ("(_ < 0)")
fb5851b71a82 Added code generator setup.
berghofe
parents: 11468
diff changeset
   105
7032
d6efb3b8e669 NatBin: binary arithmetic for the naturals
paulson
parents:
diff changeset
   106
end