src/HOL/Integ/Bin.ML
author paulson
Wed, 22 Mar 2000 13:01:18 +0100
changeset 8552 8c4ff19a7286
parent 7708 d4d905127420
child 8764 3f976a7e81d3
permissions -rw-r--r--
tidied using new "inst" rule
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2224
4fc4b465be5b New material from Norbert Voelker for efficient binary comparisons
paulson
parents: 1894
diff changeset
     1
(*  Title:      HOL/Integ/Bin.ML
7074
e0730ffaafcc zadd_ac and zmult_ac are no longer included by default
paulson
parents: 7033
diff changeset
     2
    ID:         $Id$
2224
4fc4b465be5b New material from Norbert Voelker for efficient binary comparisons
paulson
parents: 1894
diff changeset
     3
    Authors:    Lawrence C Paulson, Cambridge University Computer Laboratory
4fc4b465be5b New material from Norbert Voelker for efficient binary comparisons
paulson
parents: 1894
diff changeset
     4
                David Spelt, University of Twente 
6060
d30d1dd2082d Instantiated lin.arith.
nipkow
parents: 6036
diff changeset
     5
                Tobias Nipkow, Technical University Munich
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
     6
    Copyright   1994  University of Cambridge
6060
d30d1dd2082d Instantiated lin.arith.
nipkow
parents: 6036
diff changeset
     7
    Copyright   1996  University of Twente
d30d1dd2082d Instantiated lin.arith.
nipkow
parents: 6036
diff changeset
     8
    Copyright   1999  TU Munich
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
     9
7708
d4d905127420 arithmetic now in IntArith;
wenzelm
parents: 7582
diff changeset
    10
Arithmetic on binary integers.
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    11
*)
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    12
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    13
(** extra rules for bin_succ, bin_pred, bin_add, bin_mult **)
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    14
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    15
Goal "NCons Pls False = Pls";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    16
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    17
qed "NCons_Pls_0";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    18
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    19
Goal "NCons Pls True = Pls BIT True";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    20
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    21
qed "NCons_Pls_1";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    22
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    23
Goal "NCons Min False = Min BIT False";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    24
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    25
qed "NCons_Min_0";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    26
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    27
Goal "NCons Min True = Min";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    28
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    29
qed "NCons_Min_1";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    30
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    31
Goal "bin_succ(w BIT True) = (bin_succ w) BIT False";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    32
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    33
qed "bin_succ_1";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    34
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    35
Goal "bin_succ(w BIT False) =  NCons w True";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    36
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    37
qed "bin_succ_0";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    38
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    39
Goal "bin_pred(w BIT True) = NCons w False";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    40
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    41
qed "bin_pred_1";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    42
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    43
Goal "bin_pred(w BIT False) = (bin_pred w) BIT True";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    44
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    45
qed "bin_pred_0";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    46
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    47
Goal "bin_minus(w BIT True) = bin_pred (NCons (bin_minus w) False)";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    48
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    49
qed "bin_minus_1";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    50
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    51
Goal "bin_minus(w BIT False) = (bin_minus w) BIT False";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    52
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    53
qed "bin_minus_0";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    54
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
    55
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    56
(*** bin_add: binary addition ***)
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    57
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    58
Goal "bin_add (v BIT True) (w BIT True) = \
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    59
\    NCons (bin_add v (bin_succ w)) False";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    60
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    61
qed "bin_add_BIT_11";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    62
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    63
Goal "bin_add (v BIT True) (w BIT False) = NCons (bin_add v w) True";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    64
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    65
qed "bin_add_BIT_10";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    66
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    67
Goal "bin_add (v BIT False) (w BIT y) = NCons (bin_add v w) y";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    68
by Auto_tac;
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    69
qed "bin_add_BIT_0";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    70
5551
ed5e19bc7e32 renamed some axioms; some new theorems
paulson
parents: 5540
diff changeset
    71
Goal "bin_add w Pls = w";
ed5e19bc7e32 renamed some axioms; some new theorems
paulson
parents: 5540
diff changeset
    72
by (induct_tac "w" 1);
ed5e19bc7e32 renamed some axioms; some new theorems
paulson
parents: 5540
diff changeset
    73
by Auto_tac;
ed5e19bc7e32 renamed some axioms; some new theorems
paulson
parents: 5540
diff changeset
    74
qed "bin_add_Pls_right";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    75
7517
bad2f36810e1 generalized the theorem bin_add_BIT_Min to bin_add_Min_right
paulson
parents: 7074
diff changeset
    76
Goal "bin_add w Min = bin_pred w";
bad2f36810e1 generalized the theorem bin_add_BIT_Min to bin_add_Min_right
paulson
parents: 7074
diff changeset
    77
by (induct_tac "w" 1);
bad2f36810e1 generalized the theorem bin_add_BIT_Min to bin_add_Min_right
paulson
parents: 7074
diff changeset
    78
by Auto_tac;
bad2f36810e1 generalized the theorem bin_add_BIT_Min to bin_add_Min_right
paulson
parents: 7074
diff changeset
    79
qed "bin_add_Min_right";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    80
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    81
Goal "bin_add (v BIT x) (w BIT y) = \
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    82
\    NCons(bin_add v (if x & y then (bin_succ w) else w)) (x~= y)";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    83
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    84
qed "bin_add_BIT_BIT";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    85
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    86
6036
1512f4b7d2e8 fixed comments
paulson
parents: 5779
diff changeset
    87
(*** bin_mult: binary multiplication ***)
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    88
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    89
Goal "bin_mult (v BIT True) w = bin_add (NCons (bin_mult v w) False) w";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    90
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    91
qed "bin_mult_1";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    92
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    93
Goal "bin_mult (v BIT False) w = NCons (bin_mult v w) False";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    94
by (Simp_tac 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
    95
qed "bin_mult_0";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    96
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    97
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    98
(**** The carry/borrow functions, bin_succ and bin_pred ****)
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
    99
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   100
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   101
(**** number_of ****)
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   102
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
   103
Goal "number_of(NCons w b) = (number_of(w BIT b)::int)";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
   104
by (induct_tac "w" 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
   105
by (ALLGOALS Asm_simp_tac);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
   106
qed "number_of_NCons";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   107
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   108
Addsimps [number_of_NCons];
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   109
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
   110
Goal "number_of(bin_succ w) = int 1 + number_of w";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
   111
by (induct_tac "w" 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
   112
by (ALLGOALS (asm_simp_tac (simpset() addsimps zadd_ac)));
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
   113
qed "number_of_succ";
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   114
7008
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
   115
Goal "number_of(bin_pred w) = - (int 1) + number_of w";
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
   116
by (induct_tac "w" 1);
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
   117
by (ALLGOALS (asm_simp_tac (simpset() addsimps zadd_ac)));
6def5ce146e2 qed_goal -> Goal; new theorems nat_le_0, nat_le_eq_zle and zdiff_int
paulson
parents: 6997
diff changeset
   118
qed "number_of_pred";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   119
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   120
Goal "number_of(bin_minus w) = (- (number_of w)::int)";
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   121
by (induct_tac "w" 1);
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   122
by (Simp_tac 1);
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   123
by (Simp_tac 1);
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   124
by (asm_simp_tac (simpset()
5551
ed5e19bc7e32 renamed some axioms; some new theorems
paulson
parents: 5540
diff changeset
   125
		  delsimps [bin_pred_Pls, bin_pred_Min, bin_pred_BIT]
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   126
		  addsimps [number_of_succ,number_of_pred,
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   127
			    zadd_assoc]) 1);
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   128
qed "number_of_minus";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   129
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   130
 
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   131
val bin_add_simps = [bin_add_BIT_BIT, number_of_succ, number_of_pred];
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   132
6036
1512f4b7d2e8 fixed comments
paulson
parents: 5779
diff changeset
   133
(*This proof is complicated by the mutual recursion*)
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   134
Goal "! w. number_of(bin_add v w) = (number_of v + number_of w::int)";
5184
9b8547a9496a Adapted to new datatype package.
berghofe
parents: 5069
diff changeset
   135
by (induct_tac "v" 1);
4686
74a12e86b20b Removed `addsplits [expand_if]'
nipkow
parents: 4641
diff changeset
   136
by (simp_tac (simpset() addsimps bin_add_simps) 1);
74a12e86b20b Removed `addsplits [expand_if]'
nipkow
parents: 4641
diff changeset
   137
by (simp_tac (simpset() addsimps bin_add_simps) 1);
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   138
by (rtac allI 1);
5184
9b8547a9496a Adapted to new datatype package.
berghofe
parents: 5069
diff changeset
   139
by (induct_tac "w" 1);
5540
0f16c3b66ab4 much renaming and reorganization
paulson
parents: 5512
diff changeset
   140
by (ALLGOALS (asm_simp_tac (simpset() addsimps bin_add_simps @ zadd_ac)));
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   141
qed_spec_mp "number_of_add";
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   142
5779
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   143
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   144
(*Subtraction*)
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   145
Goalw [zdiff_def]
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   146
     "number_of v - number_of w = (number_of(bin_add v (bin_minus w))::int)";
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   147
by (simp_tac (simpset() addsimps [number_of_add, number_of_minus]) 1);
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   148
qed "diff_number_of_eq";
5779
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   149
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   150
val bin_mult_simps = [zmult_zminus, number_of_minus, number_of_add];
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   151
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   152
Goal "number_of(bin_mult v w) = (number_of v * number_of w::int)";
5184
9b8547a9496a Adapted to new datatype package.
berghofe
parents: 5069
diff changeset
   153
by (induct_tac "v" 1);
4686
74a12e86b20b Removed `addsplits [expand_if]'
nipkow
parents: 4641
diff changeset
   154
by (simp_tac (simpset() addsimps bin_mult_simps) 1);
74a12e86b20b Removed `addsplits [expand_if]'
nipkow
parents: 4641
diff changeset
   155
by (simp_tac (simpset() addsimps bin_mult_simps) 1);
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   156
by (asm_simp_tac
5540
0f16c3b66ab4 much renaming and reorganization
paulson
parents: 5512
diff changeset
   157
    (simpset() addsimps bin_mult_simps @ [zadd_zmult_distrib] @ zadd_ac) 1);
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   158
qed "number_of_mult";
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   159
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   160
6941
f52c70a449fb products of signs as equivalences
paulson
parents: 6917
diff changeset
   161
(*The correctness of shifting.  But it doesn't seem to give a measurable
f52c70a449fb products of signs as equivalences
paulson
parents: 6917
diff changeset
   162
  speed-up.*)
f52c70a449fb products of signs as equivalences
paulson
parents: 6917
diff changeset
   163
Goal "(#2::int) * number_of w = number_of (w BIT False)";
f52c70a449fb products of signs as equivalences
paulson
parents: 6917
diff changeset
   164
by (induct_tac "w" 1);
f52c70a449fb products of signs as equivalences
paulson
parents: 6917
diff changeset
   165
by (ALLGOALS (asm_simp_tac
f52c70a449fb products of signs as equivalences
paulson
parents: 6917
diff changeset
   166
    (simpset() addsimps bin_mult_simps @ [zadd_zmult_distrib] @ zadd_ac)));
f52c70a449fb products of signs as equivalences
paulson
parents: 6917
diff changeset
   167
qed "double_number_of_BIT";
f52c70a449fb products of signs as equivalences
paulson
parents: 6917
diff changeset
   168
f52c70a449fb products of signs as equivalences
paulson
parents: 6917
diff changeset
   169
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   170
(** Simplification rules with integer constants **)
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   171
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   172
Goal "#0 + z = (z::int)";
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   173
by (Simp_tac 1);
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   174
qed "zadd_0";
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   175
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   176
Goal "z + #0 = (z::int)";
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   177
by (Simp_tac 1);
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   178
qed "zadd_0_right";
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   179
5592
64697e426048 better handling of literals
paulson
parents: 5582
diff changeset
   180
Addsimps [zadd_0, zadd_0_right];
64697e426048 better handling of literals
paulson
parents: 5582
diff changeset
   181
64697e426048 better handling of literals
paulson
parents: 5582
diff changeset
   182
64697e426048 better handling of literals
paulson
parents: 5582
diff changeset
   183
(** Converting simple cases of (int n) to numerals **)
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   184
5592
64697e426048 better handling of literals
paulson
parents: 5582
diff changeset
   185
(*int 0 = #0 *)
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   186
bind_thm ("int_0", number_of_Pls RS sym);
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   187
5592
64697e426048 better handling of literals
paulson
parents: 5582
diff changeset
   188
Goal "int (Suc n) = #1 + int n";
64697e426048 better handling of literals
paulson
parents: 5582
diff changeset
   189
by (simp_tac (simpset() addsimps [zadd_int]) 1);
64697e426048 better handling of literals
paulson
parents: 5582
diff changeset
   190
qed "int_Suc";
5510
ad120f7c52ad improved (but still flawed) treatment of binary arithmetic
paulson
parents: 5491
diff changeset
   191
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   192
Goal "- (#0) = (#0::int)";
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   193
by (Simp_tac 1);
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   194
qed "zminus_0";
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   195
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   196
Addsimps [zminus_0];
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   197
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   198
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   199
Goal "(#0::int) - x = -x";
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   200
by (simp_tac (simpset() addsimps [zdiff_def]) 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   201
qed "zdiff0";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   202
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   203
Goal "x - (#0::int) = x";
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   204
by (simp_tac (simpset() addsimps [zdiff_def]) 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   205
qed "zdiff0_right";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   206
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   207
Goal "x - x = (#0::int)";
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   208
by (simp_tac (simpset() addsimps [zdiff_def]) 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   209
qed "zdiff_self";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   210
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   211
Addsimps [zdiff0, zdiff0_right, zdiff_self];
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   212
6917
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   213
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   214
(** Special simplification, for constants only **)
6838
941c4f70db91 rewrite rules to distribute CONSTANT multiplication over sum and difference;
paulson
parents: 6716
diff changeset
   215
7074
e0730ffaafcc zadd_ac and zmult_ac are no longer included by default
paulson
parents: 7033
diff changeset
   216
(*Distributive laws for literals*)
6917
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   217
Addsimps (map (inst "w" "number_of ?v")
6838
941c4f70db91 rewrite rules to distribute CONSTANT multiplication over sum and difference;
paulson
parents: 6716
diff changeset
   218
	  [zadd_zmult_distrib, zadd_zmult_distrib2,
941c4f70db91 rewrite rules to distribute CONSTANT multiplication over sum and difference;
paulson
parents: 6716
diff changeset
   219
	   zdiff_zmult_distrib, zdiff_zmult_distrib2]);
941c4f70db91 rewrite rules to distribute CONSTANT multiplication over sum and difference;
paulson
parents: 6716
diff changeset
   220
6917
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   221
Addsimps (map (inst "x" "number_of ?v") 
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   222
	  [zless_zminus, zle_zminus, equation_zminus]);
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   223
Addsimps (map (inst "y" "number_of ?v") 
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   224
	  [zminus_zless, zminus_zle, zminus_equation]);
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   225
7074
e0730ffaafcc zadd_ac and zmult_ac are no longer included by default
paulson
parents: 7033
diff changeset
   226
(*Moving negation out of products*)
e0730ffaafcc zadd_ac and zmult_ac are no longer included by default
paulson
parents: 7033
diff changeset
   227
Addsimps [zmult_zminus, zmult_zminus_right];
6917
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   228
6838
941c4f70db91 rewrite rules to distribute CONSTANT multiplication over sum and difference;
paulson
parents: 6716
diff changeset
   229
(** Special-case simplification for small constants **)
941c4f70db91 rewrite rules to distribute CONSTANT multiplication over sum and difference;
paulson
parents: 6716
diff changeset
   230
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   231
Goal "#0 * z = (#0::int)";
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   232
by (Simp_tac 1);
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   233
qed "zmult_0";
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   234
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   235
Goal "z * #0 = (#0::int)";
6838
941c4f70db91 rewrite rules to distribute CONSTANT multiplication over sum and difference;
paulson
parents: 6716
diff changeset
   236
by (Simp_tac 1);
941c4f70db91 rewrite rules to distribute CONSTANT multiplication over sum and difference;
paulson
parents: 6716
diff changeset
   237
qed "zmult_0_right";
941c4f70db91 rewrite rules to distribute CONSTANT multiplication over sum and difference;
paulson
parents: 6716
diff changeset
   238
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   239
Goal "#1 * z = (z::int)";
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   240
by (Simp_tac 1);
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   241
qed "zmult_1";
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   242
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   243
Goal "z * #1 = (z::int)";
6838
941c4f70db91 rewrite rules to distribute CONSTANT multiplication over sum and difference;
paulson
parents: 6716
diff changeset
   244
by (Simp_tac 1);
941c4f70db91 rewrite rules to distribute CONSTANT multiplication over sum and difference;
paulson
parents: 6716
diff changeset
   245
qed "zmult_1_right";
941c4f70db91 rewrite rules to distribute CONSTANT multiplication over sum and difference;
paulson
parents: 6716
diff changeset
   246
6917
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   247
Goal "#-1 * z = -(z::int)";
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   248
by (simp_tac (simpset() addsimps zcompare_rls@[zmult_zminus]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   249
qed "zmult_minus1";
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   250
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   251
Goal "z * #-1 = -(z::int)";
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   252
by (simp_tac (simpset() addsimps zcompare_rls@[zmult_zminus_right]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   253
qed "zmult_minus1_right";
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   254
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   255
Addsimps [zmult_0, zmult_0_right, 
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   256
	  zmult_1, zmult_1_right,
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   257
	  zmult_minus1, zmult_minus1_right];
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   258
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   259
(*For specialist use: NOT as default simprules*)
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   260
Goal "#2 * z = (z+z::int)";
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   261
by (simp_tac (simpset() addsimps [zadd_zmult_distrib]) 1);
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   262
qed "zmult_2";
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   263
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   264
Goal "z * #2 = (z+z::int)";
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   265
by (simp_tac (simpset() addsimps [zadd_zmult_distrib2]) 1);
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   266
qed "zmult_2_right";
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   267
6917
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   268
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   269
(** Inequality reasoning **)
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   270
6989
dd3e8bd86cc6 new theorem zmult_eq_0_iff
paulson
parents: 6973
diff changeset
   271
Goal "(m*n = (#0::int)) = (m = #0 | n = #0)";
dd3e8bd86cc6 new theorem zmult_eq_0_iff
paulson
parents: 6973
diff changeset
   272
by (stac (int_0 RS sym) 1 THEN rtac zmult_eq_int0_iff 1);
dd3e8bd86cc6 new theorem zmult_eq_0_iff
paulson
parents: 6973
diff changeset
   273
qed "zmult_eq_0_iff";
dd3e8bd86cc6 new theorem zmult_eq_0_iff
paulson
parents: 6973
diff changeset
   274
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   275
Goal "(w < z + (#1::int)) = (w<z | w=z)";
5592
64697e426048 better handling of literals
paulson
parents: 5582
diff changeset
   276
by (simp_tac (simpset() addsimps [zless_add_int_Suc_eq]) 1);
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   277
qed "zless_add1_eq";
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   278
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   279
Goal "(w + (#1::int) <= z) = (w<z)";
5592
64697e426048 better handling of literals
paulson
parents: 5582
diff changeset
   280
by (simp_tac (simpset() addsimps [add_int_Suc_zle_eq]) 1);
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   281
qed "add1_zle_eq";
6997
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   282
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   283
Goal "((#1::int) + w <= z) = (w<z)";
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   284
by (stac zadd_commute 1);
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   285
by (rtac add1_zle_eq 1);
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   286
qed "add1_left_zle_eq";
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   287
5540
0f16c3b66ab4 much renaming and reorganization
paulson
parents: 5512
diff changeset
   288
Goal "neg x = (x < #0)";
6917
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   289
by (simp_tac (simpset() addsimps [neg_eq_less_int0]) 1); 
5540
0f16c3b66ab4 much renaming and reorganization
paulson
parents: 5512
diff changeset
   290
qed "neg_eq_less_0"; 
5510
ad120f7c52ad improved (but still flawed) treatment of binary arithmetic
paulson
parents: 5491
diff changeset
   291
6989
dd3e8bd86cc6 new theorem zmult_eq_0_iff
paulson
parents: 6973
diff changeset
   292
Goal "(~neg x) = (#0 <= x)";
6917
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   293
by (simp_tac (simpset() addsimps [not_neg_eq_ge_int0]) 1); 
5540
0f16c3b66ab4 much renaming and reorganization
paulson
parents: 5512
diff changeset
   294
qed "not_neg_eq_ge_0"; 
5510
ad120f7c52ad improved (but still flawed) treatment of binary arithmetic
paulson
parents: 5491
diff changeset
   295
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   296
Goal "#0 <= int m";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   297
by (Simp_tac 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   298
qed "zero_zle_int";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   299
AddIffs [zero_zle_int];
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   300
5510
ad120f7c52ad improved (but still flawed) treatment of binary arithmetic
paulson
parents: 5491
diff changeset
   301
5747
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   302
(** Needed because (int 0) rewrites to #0.
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   303
    Can these be generalized without evaluating large numbers?**)
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   304
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   305
Goal "~ (int k < #0)";
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   306
by (Simp_tac 1);
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   307
qed "int_less_0_conv";
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   308
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   309
Goal "(int k <= #0) = (k=0)";
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   310
by (Simp_tac 1);
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   311
qed "int_le_0_conv";
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   312
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   313
Goal "(int k = #0) = (k=0)";
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   314
by (Simp_tac 1);
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   315
qed "int_eq_0_conv";
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   316
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   317
Goal "(#0 = int k) = (k=0)";
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   318
by Auto_tac;
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   319
qed "int_eq_0_conv'";
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   320
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   321
Addsimps [int_less_0_conv, int_le_0_conv, int_eq_0_conv, int_eq_0_conv'];
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   322
387b5bf9326a Now users will never see (int 0)
paulson
parents: 5592
diff changeset
   323
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   324
(** Simplification rules for comparison of binary numbers (Norbert Voelker) **)
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   325
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   326
(** Equals (=) **)
1632
39e146ac224c Binary integers and their numeric syntax
paulson
parents:
diff changeset
   327
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   328
Goalw [iszero_def]
6997
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   329
  "((number_of x::int) = number_of y) = \
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   330
\  iszero (number_of (bin_add x (bin_minus y)))"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   331
by (simp_tac (simpset() addsimps
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   332
              (zcompare_rls @ [number_of_add, number_of_minus])) 1); 
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   333
qed "eq_number_of_eq"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   334
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   335
Goalw [iszero_def] "iszero ((number_of Pls)::int)"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   336
by (Simp_tac 1); 
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   337
qed "iszero_number_of_Pls"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   338
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   339
Goalw [iszero_def] "~ iszero ((number_of Min)::int)"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   340
by (Simp_tac 1);
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   341
qed "nonzero_number_of_Min"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   342
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   343
Goalw [iszero_def]
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   344
     "iszero (number_of (w BIT x)) = (~x & iszero (number_of w::int))"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   345
by (Simp_tac 1);
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   346
by (int_case_tac "number_of w" 1); 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   347
by (ALLGOALS (asm_simp_tac 
5540
0f16c3b66ab4 much renaming and reorganization
paulson
parents: 5512
diff changeset
   348
	      (simpset() addsimps zcompare_rls @ 
0f16c3b66ab4 much renaming and reorganization
paulson
parents: 5512
diff changeset
   349
				  [zminus_zadd_distrib RS sym, 
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5562
diff changeset
   350
				   zadd_int]))); 
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   351
qed "iszero_number_of_BIT"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   352
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   353
Goal "iszero (number_of (w BIT False)) = iszero (number_of w::int)"; 
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   354
by (simp_tac (HOL_ss addsimps [iszero_number_of_BIT]) 1); 
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   355
qed "iszero_number_of_0"; 
5779
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   356
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   357
Goal "~ iszero (number_of (w BIT True)::int)"; 
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   358
by (simp_tac (HOL_ss addsimps [iszero_number_of_BIT]) 1); 
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   359
qed "iszero_number_of_1"; 
5779
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   360
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   361
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   362
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   363
(** Less-than (<) **)
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   364
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   365
Goalw [zless_def,zdiff_def] 
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   366
    "(number_of x::int) < number_of y \
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   367
\    = neg (number_of (bin_add x (bin_minus y)))";
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   368
by (simp_tac (simpset() addsimps bin_mult_simps) 1);
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   369
qed "less_number_of_eq_neg"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   370
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   371
Goal "~ neg (number_of Pls)"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   372
by (Simp_tac 1); 
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   373
qed "not_neg_number_of_Pls"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   374
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   375
Goal "neg (number_of Min)"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   376
by (Simp_tac 1);
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   377
qed "neg_number_of_Min"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   378
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   379
Goal "neg (number_of (w BIT x)) = neg (number_of w)"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   380
by (Asm_simp_tac 1); 
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   381
by (int_case_tac "number_of w" 1); 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   382
by (ALLGOALS (asm_simp_tac 
6917
eba301caceea Introduction of integer division algorithm
paulson
parents: 6910
diff changeset
   383
	      (simpset() addsimps [zadd_int, neg_eq_less_int0, 
5540
0f16c3b66ab4 much renaming and reorganization
paulson
parents: 5512
diff changeset
   384
				   symmetric zdiff_def] @ zcompare_rls))); 
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   385
qed "neg_number_of_BIT"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   386
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   387
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   388
(** Less-than-or-equals (<=) **)
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   389
7033
c7479ae352b1 removal of rewrites for Suc(Suc(Suc...)))
paulson
parents: 7008
diff changeset
   390
Goal "(number_of x <= (number_of y::int)) = \
c7479ae352b1 removal of rewrites for Suc(Suc(Suc...)))
paulson
parents: 7008
diff changeset
   391
\     (~ number_of y < (number_of x::int))";
c7479ae352b1 removal of rewrites for Suc(Suc(Suc...)))
paulson
parents: 7008
diff changeset
   392
by (rtac (linorder_not_less RS sym) 1);
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   393
qed "le_number_of_eq_not_less"; 
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   394
5540
0f16c3b66ab4 much renaming and reorganization
paulson
parents: 5512
diff changeset
   395
(*Delete the original rewrites, with their clumsy conditional expressions*)
5551
ed5e19bc7e32 renamed some axioms; some new theorems
paulson
parents: 5540
diff changeset
   396
Delsimps [bin_succ_BIT, bin_pred_BIT, bin_minus_BIT, 
ed5e19bc7e32 renamed some axioms; some new theorems
paulson
parents: 5540
diff changeset
   397
          NCons_Pls, NCons_Min, bin_add_BIT, bin_mult_BIT];
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   398
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   399
(*Hide the binary representation of integer constants*)
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   400
Delsimps [number_of_Pls, number_of_Min, number_of_BIT];
5491
22f8331cdf47 revised treatment of integers
paulson
parents: 5224
diff changeset
   401
5779
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   402
(*simplification of arithmetic operations on integer constants*)
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   403
val bin_arith_extra_simps =
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   404
    [number_of_add RS sym,
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   405
     number_of_minus RS sym,
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   406
     number_of_mult RS sym,
5779
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   407
     bin_succ_1, bin_succ_0, 
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   408
     bin_pred_1, bin_pred_0, 
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   409
     bin_minus_1, bin_minus_0,  
7517
bad2f36810e1 generalized the theorem bin_add_BIT_Min to bin_add_Min_right
paulson
parents: 7074
diff changeset
   410
     bin_add_Pls_right, bin_add_Min_right,
5779
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   411
     bin_add_BIT_0, bin_add_BIT_10, bin_add_BIT_11,
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   412
     diff_number_of_eq, 
5779
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   413
     bin_mult_1, bin_mult_0, 
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   414
     NCons_Pls_0, NCons_Pls_1, 
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   415
     NCons_Min_0, NCons_Min_1, NCons_BIT];
2224
4fc4b465be5b New material from Norbert Voelker for efficient binary comparisons
paulson
parents: 1894
diff changeset
   416
5779
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   417
(*For making a minimal simpset, one must include these default simprules
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   418
  of thy.  Also include simp_thms, or at least (~False)=True*)
5779
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   419
val bin_arith_simps =
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   420
    [bin_pred_Pls, bin_pred_Min,
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   421
     bin_succ_Pls, bin_succ_Min,
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   422
     bin_add_Pls, bin_add_Min,
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   423
     bin_minus_Pls, bin_minus_Min,
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   424
     bin_mult_Pls, bin_mult_Min] @ bin_arith_extra_simps;
2224
4fc4b465be5b New material from Norbert Voelker for efficient binary comparisons
paulson
parents: 1894
diff changeset
   425
5779
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   426
(*Simplification of relational operations*)
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   427
val bin_rel_simps =
6910
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   428
    [eq_number_of_eq, iszero_number_of_Pls, nonzero_number_of_Min,
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   429
     iszero_number_of_0, iszero_number_of_1,
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   430
     less_number_of_eq_neg,
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   431
     not_neg_number_of_Pls, neg_number_of_Min, neg_number_of_BIT,
7c3503ae3d78 use generic numeral encoding and syntax;
wenzelm
parents: 6838
diff changeset
   432
     le_number_of_eq_not_less];
2224
4fc4b465be5b New material from Norbert Voelker for efficient binary comparisons
paulson
parents: 1894
diff changeset
   433
5779
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   434
Addsimps bin_arith_extra_simps;
5c74f003a68e Explicit (and improved) simprules for binary arithmetic.
paulson
parents: 5747
diff changeset
   435
Addsimps bin_rel_simps;
5510
ad120f7c52ad improved (but still flawed) treatment of binary arithmetic
paulson
parents: 5491
diff changeset
   436
6997
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   437
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   438
(** Constant folding inside parentheses **)
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   439
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   440
Goal "number_of v + (number_of w + c) = number_of(bin_add v w) + (c::int)";
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   441
by (stac (zadd_assoc RS sym) 1);
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   442
by (stac number_of_add 1);
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   443
by Auto_tac;
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   444
qed "nested_number_of_add";
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   445
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   446
Goalw [zdiff_def]
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   447
    "number_of v + (number_of w - c) = number_of(bin_add v w) - (c::int)";
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   448
by (rtac nested_number_of_add 1);
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   449
qed "nested_diff1_number_of_add";
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   450
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   451
Goal "number_of v + (c - number_of w) = \
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   452
\    number_of (bin_add v (bin_minus w)) + (c::int)";
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   453
by (stac (diff_number_of_eq RS sym) 1);
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   454
by Auto_tac;
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   455
qed "nested_diff2_number_of_add";
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   456
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   457
Goal "number_of v * (number_of w * c) = number_of(bin_mult v w) * (c::int)";
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   458
by (stac (zmult_assoc RS sym) 1);
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   459
by (stac number_of_mult 1);
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   460
by Auto_tac;
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   461
qed "nested_number_of_mult";
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   462
Addsimps [nested_number_of_add, nested_diff1_number_of_add,
1833bdd83ebf new constant folding rewrites
paulson
parents: 6989
diff changeset
   463
	  nested_diff2_number_of_add, nested_number_of_mult];