src/ZF/arith.ML
author clasohm
Thu, 16 Sep 1993 12:20:38 +0200
changeset 0 a5a9c433f639
child 6 8ce8c4d13d4d
permissions -rw-r--r--
Initial revision
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     1
(*  Title: 	ZF/arith.ML
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1992  University of Cambridge
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
For arith.thy.  Arithmetic operators and their definitions
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
Proofs about elementary arithmetic: addition, multiplication, etc.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
Could prove def_rec_0, def_rec_succ...
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
open Arith;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
(*"Difference" is subtraction of natural numbers.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
  There are no negative numbers; we have
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
     m #- n = 0  iff  m<=n   and     m #- n = succ(k) iff m>n.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
  Also, rec(m, 0, %z w.z) is pred(m).   
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
(** rec -- better than nat_rec; the succ case has no type requirement! **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
val rec_trans = rec_def RS def_transrec RS trans;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
goal Arith.thy "rec(0,a,b) = a";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
by (rtac rec_trans 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
by (rtac nat_case_0 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
val rec_0 = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
goal Arith.thy "rec(succ(m),a,b) = b(m, rec(m,a,b))";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
val rec_ss = ZF_ss 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
      addcongs (mk_typed_congs Arith.thy [("b", "[i,i]=>i")])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
      addrews [nat_case_succ, nat_succI];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
by (rtac rec_trans 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    35
by (SIMP_TAC rec_ss 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    36
val rec_succ = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
val major::prems = goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
    "[| n: nat;  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
\       a: C(0);  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
\       !!m z. [| m: nat;  z: C(m) |] ==> b(m,z): C(succ(m))  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
\    |] ==> rec(n,a,b) : C(n)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
by (rtac (major RS nat_induct) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
by (ALLGOALS
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
    (ASM_SIMP_TAC (ZF_ss addrews (prems@[rec_0,rec_succ]))));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
val rec_type = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
val prems = goalw Arith.thy [rec_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
    "[| n=n';  a=a';  !!m z. b(m,z)=b'(m,z)  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
\    |] ==> rec(n,a,b)=rec(n',a',b')";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
by (SIMP_TAC (ZF_ss addcongs [transrec_cong,nat_case_cong] 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
		    addrews (prems RL [sym])) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
val rec_cong = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
val nat_typechecks = [rec_type,nat_0I,nat_1I,nat_succI,Ord_nat];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
val nat_ss = ZF_ss addcongs [nat_case_cong,rec_cong]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
	       	   addrews ([rec_0,rec_succ] @ nat_typechecks);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
(** Addition **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
val add_type = prove_goalw Arith.thy [add_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
    "[| m:nat;  n:nat |] ==> m #+ n : nat"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
 (fn prems=> [ (typechk_tac (prems@nat_typechecks@ZF_typechecks)) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
val add_0 = prove_goalw Arith.thy [add_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
    "0 #+ n = n"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
 (fn _ => [ (rtac rec_0 1) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
val add_succ = prove_goalw Arith.thy [add_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    72
    "succ(m) #+ n = succ(m #+ n)"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    73
 (fn _=> [ (rtac rec_succ 1) ]); 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
(** Multiplication **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
val mult_type = prove_goalw Arith.thy [mult_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
    "[| m:nat;  n:nat |] ==> m #* n : nat"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
  [ (typechk_tac (prems@[add_type]@nat_typechecks@ZF_typechecks)) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
val mult_0 = prove_goalw Arith.thy [mult_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
    "0 #* n = 0"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
 (fn _ => [ (rtac rec_0 1) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
val mult_succ = prove_goalw Arith.thy [mult_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
    "succ(m) #* n = n #+ (m #* n)"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
 (fn _ => [ (rtac rec_succ 1) ]); 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
(** Difference **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    92
val diff_type = prove_goalw Arith.thy [diff_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
    "[| m:nat;  n:nat |] ==> m #- n : nat"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    94
 (fn prems=> [ (typechk_tac (prems@nat_typechecks@ZF_typechecks)) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    95
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
val diff_0 = prove_goalw Arith.thy [diff_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
    "m #- 0 = m"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
 (fn _ => [ (rtac rec_0 1) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
val diff_0_eq_0 = prove_goalw Arith.thy [diff_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
    "n:nat ==> 0 #- n = 0"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
 (fn [prem]=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   103
  [ (rtac (prem RS nat_induct) 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
    (ALLGOALS (ASM_SIMP_TAC nat_ss)) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
(*Must simplify BEFORE the induction!!  (Else we get a critical pair)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
  succ(m) #- succ(n)   rewrites to   pred(succ(m) #- n)  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
val diff_succ_succ = prove_goalw Arith.thy [diff_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
    "[| m:nat;  n:nat |] ==> succ(m) #- succ(n) = m #- n"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
  [ (ASM_SIMP_TAC (nat_ss addrews prems) 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
    (nat_ind_tac "n" prems 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
    (ALLGOALS (ASM_SIMP_TAC (nat_ss addrews prems))) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
val prems = goal Arith.thy 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
    "[| m:nat;  n:nat |] ==> m #- n : succ(m)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
by (resolve_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
by (resolve_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
by (etac succE 3);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
by (ALLGOALS
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
    (ASM_SIMP_TAC
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
     (nat_ss addrews (prems@[diff_0,diff_0_eq_0,diff_succ_succ]))));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
val diff_leq = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
(*** Simplification over add, mult, diff ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
val arith_typechecks = [add_type, mult_type, diff_type];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
val arith_rews = [add_0, add_succ,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
		  mult_0, mult_succ,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
		  diff_0, diff_0_eq_0, diff_succ_succ];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
val arith_congs = mk_congs Arith.thy ["op #+", "op #-", "op #*"];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   135
val arith_ss = nat_ss addcongs arith_congs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
                      addrews  (arith_rews@arith_typechecks);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   137
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
(*** Addition ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
(*Associative law for addition*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   141
val add_assoc = prove_goal Arith.thy 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
    "m:nat ==> (m #+ n) #+ k = m #+ (n #+ k)"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
  [ (nat_ind_tac "m" prems 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
    (ALLGOALS (ASM_SIMP_TAC (arith_ss addrews prems))) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
(*The following two lemmas are used for add_commute and sometimes
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
  elsewhere, since they are safe for rewriting.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
val add_0_right = prove_goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
    "m:nat ==> m #+ 0 = m"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
  [ (nat_ind_tac "m" prems 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
    (ALLGOALS (ASM_SIMP_TAC (arith_ss addrews prems))) ]); 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   155
val add_succ_right = prove_goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
    "m:nat ==> m #+ succ(n) = succ(m #+ n)"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
  [ (nat_ind_tac "m" prems 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   159
    (ALLGOALS (ASM_SIMP_TAC (arith_ss addrews prems))) ]); 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
(*Commutative law for addition*)  
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
val add_commute = prove_goal Arith.thy 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
    "[| m:nat;  n:nat |] ==> m #+ n = n #+ m"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   165
  [ (nat_ind_tac "n" prems 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
    (ALLGOALS
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   167
     (ASM_SIMP_TAC
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   168
      (arith_ss addrews (prems@[add_0_right, add_succ_right])))) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   170
(*Cancellation law on the left*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   171
val [knat,eqn] = goal Arith.thy 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   172
    "[| k:nat;  k #+ m = k #+ n |] ==> m=n";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   173
by (rtac (eqn RS rev_mp) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   174
by (nat_ind_tac "k" [knat] 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   175
by (ALLGOALS (SIMP_TAC arith_ss));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   176
by (fast_tac ZF_cs 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   177
val add_left_cancel = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   178
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
(*** Multiplication ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   180
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   181
(*right annihilation in product*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   182
val mult_0_right = prove_goal Arith.thy 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   183
    "m:nat ==> m #* 0 = 0"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   184
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   185
  [ (nat_ind_tac "m" prems 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   186
    (ALLGOALS (ASM_SIMP_TAC (arith_ss addrews prems)))  ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   187
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   188
(*right successor law for multiplication*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   189
val mult_succ_right = prove_goal Arith.thy 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   190
    "[| m:nat;  n:nat |] ==> m #* succ(n) = m #+ (m #* n)"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   191
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   192
  [ (nat_ind_tac "m" prems 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   193
    (ALLGOALS (ASM_SIMP_TAC (arith_ss addrews ([add_assoc RS sym]@prems)))),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   194
       (*The final goal requires the commutative law for addition*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   195
    (REPEAT (ares_tac (prems@[refl,add_commute]@ZF_congs@arith_congs) 1))  ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   196
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   197
(*Commutative law for multiplication*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   198
val mult_commute = prove_goal Arith.thy 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   199
    "[| m:nat;  n:nat |] ==> m #* n = n #* m"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   200
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   201
  [ (nat_ind_tac "m" prems 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   202
    (ALLGOALS (ASM_SIMP_TAC
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   203
	       (arith_ss addrews (prems@[mult_0_right, mult_succ_right])))) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   204
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   205
(*addition distributes over multiplication*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   206
val add_mult_distrib = prove_goal Arith.thy 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   207
    "[| m:nat;  k:nat |] ==> (m #+ n) #* k = (m #* k) #+ (n #* k)"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   208
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   209
  [ (nat_ind_tac "m" prems 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   210
    (ALLGOALS (ASM_SIMP_TAC (arith_ss addrews ([add_assoc RS sym]@prems)))) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   211
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   212
(*Distributive law on the left; requires an extra typing premise*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   213
val add_mult_distrib_left = prove_goal Arith.thy 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   214
    "[| m:nat;  n:nat;  k:nat |] ==> k #* (m #+ n) = (k #* m) #+ (k #* n)"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   215
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
      let val mult_commute' = read_instantiate [("m","k")] mult_commute
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   217
          val ss = arith_ss addrews ([mult_commute',add_mult_distrib]@prems)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   218
      in [ (SIMP_TAC ss 1) ]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
      end);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   220
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   221
(*Associative law for multiplication*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   222
val mult_assoc = prove_goal Arith.thy 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   223
    "[| m:nat;  n:nat;  k:nat |] ==> (m #* n) #* k = m #* (n #* k)"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   224
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   225
  [ (nat_ind_tac "m" prems 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   226
    (ALLGOALS (ASM_SIMP_TAC (arith_ss addrews (prems@[add_mult_distrib])))) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   227
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   228
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   229
(*** Difference ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   230
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   231
val diff_self_eq_0 = prove_goal Arith.thy 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   232
    "m:nat ==> m #- m = 0"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
 (fn prems=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   234
  [ (nat_ind_tac "m" prems 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   235
    (ALLGOALS (ASM_SIMP_TAC (arith_ss addrews prems))) ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   236
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   237
(*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   238
val notless::prems = goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   239
    "[| ~m:n;  m:nat;  n:nat |] ==> n #+ (m#-n) = m";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   240
by (rtac (notless RS rev_mp) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   242
by (resolve_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   243
by (resolve_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   244
by (ALLGOALS (ASM_SIMP_TAC
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   245
	      (arith_ss addrews (prems@[succ_mem_succ_iff, Ord_0_mem_succ, 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   246
				  naturals_are_ordinals]))));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   247
val add_diff_inverse = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   248
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   249
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   250
(*Subtraction is the inverse of addition. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   251
val [mnat,nnat] = goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   252
    "[| m:nat;  n:nat |] ==> (n#+m) #-n = m";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   253
by (rtac (nnat RS nat_induct) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   254
by (ALLGOALS (ASM_SIMP_TAC (arith_ss addrews [mnat])));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   255
val diff_add_inverse = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   256
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   257
val [mnat,nnat] = goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   258
    "[| m:nat;  n:nat |] ==> n #- (n#+m) = 0";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   259
by (rtac (nnat RS nat_induct) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   260
by (ALLGOALS (ASM_SIMP_TAC (arith_ss addrews [mnat])));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   261
val diff_add_0 = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   262
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   263
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   264
(*** Remainder ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   265
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   266
(*In ordinary notation: if 0<n and n<=m then m-n < m *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   267
val prems = goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   268
    "[| 0:n; ~ m:n;  m:nat;  n:nat |] ==> m #- n : m";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   269
by (cut_facts_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   270
by (etac rev_mp 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   271
by (etac rev_mp 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   272
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   273
by (resolve_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   274
by (resolve_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   275
by (ALLGOALS (ASM_SIMP_TAC
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   276
	      (nat_ss addrews (prems@[diff_leq,diff_succ_succ]))));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   277
val div_termination = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   278
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   279
val div_rls =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   280
    [Ord_transrec_type, apply_type, div_termination, if_type] @ 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   281
    nat_typechecks;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   282
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   283
(*Type checking depends upon termination!*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   284
val prems = goalw Arith.thy [mod_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   285
    "[| 0:n;  m:nat;  n:nat |] ==> m mod n : nat";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   286
by (REPEAT (ares_tac (prems @ div_rls) 1 ORELSE etac Ord_trans 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   287
val mod_type = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   288
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   289
val div_ss = ZF_ss addrews [naturals_are_ordinals,div_termination];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   290
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   291
val prems = goal Arith.thy "[| 0:n;  m:n;  m:nat;  n:nat |] ==> m mod n = m";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   292
by (rtac (mod_def RS def_transrec RS trans) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   293
by (SIMP_TAC (div_ss addrews prems) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   294
val mod_less = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   295
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   296
val prems = goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   297
    "[| 0:n;  ~m:n;  m:nat;  n:nat |] ==> m mod n = (m#-n) mod n";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   298
by (rtac (mod_def RS def_transrec RS trans) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   299
by (SIMP_TAC (div_ss addrews prems) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   300
val mod_geq = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   301
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   302
(*** Quotient ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   303
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   304
(*Type checking depends upon termination!*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   305
val prems = goalw Arith.thy [div_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   306
    "[| 0:n;  m:nat;  n:nat |] ==> m div n : nat";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   307
by (REPEAT (ares_tac (prems @ div_rls) 1 ORELSE etac Ord_trans 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   308
val div_type = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   309
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   310
val prems = goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   311
    "[| 0:n;  m:n;  m:nat;  n:nat |] ==> m div n = 0";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   312
by (rtac (div_def RS def_transrec RS trans) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   313
by (SIMP_TAC (div_ss addrews prems) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   314
val div_less = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   315
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   316
val prems = goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   317
    "[| 0:n;  ~m:n;  m:nat;  n:nat |] ==> m div n = succ((m#-n) div n)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   318
by (rtac (div_def RS def_transrec RS trans) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   319
by (SIMP_TAC (div_ss addrews prems) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   320
val div_geq = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   321
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   322
(*Main Result.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   323
val prems = goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   324
    "[| 0:n;  m:nat;  n:nat |] ==> (m div n)#*n #+ m mod n = m";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   325
by (res_inst_tac [("i","m")] complete_induct 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   326
by (resolve_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   327
by (res_inst_tac [("Q","x:n")] (excluded_middle RS disjE) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   328
by (ALLGOALS 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   329
    (ASM_SIMP_TAC
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   330
     (arith_ss addrews ([mod_type,div_type] @ prems @
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   331
        [mod_less,mod_geq, div_less, div_geq,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   332
	 add_assoc, add_diff_inverse, div_termination]))));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   333
val mod_div_equality = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   334
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   335
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   336
(**** Additional theorems about "less than" ****)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   337
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   338
val [mnat,nnat] = goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   339
    "[| m:nat;  n:nat |] ==> ~ (m #+ n) : n";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   340
by (rtac (mnat RS nat_induct) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   341
by (ALLGOALS (ASM_SIMP_TAC (arith_ss addrews [mem_not_refl])));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   342
by (rtac notI 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   343
by (etac notE 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   344
by (etac (succI1 RS Ord_trans) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   345
by (rtac (nnat RS naturals_are_ordinals) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   346
val add_not_less_self = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   347
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   348
val [mnat,nnat] = goal Arith.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   349
    "[| m:nat;  n:nat |] ==> m : succ(m #+ n)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   350
by (rtac (mnat RS nat_induct) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   351
(*May not simplify even with ZF_ss because it would expand m:succ(...) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   352
by (rtac (add_0 RS ssubst) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   353
by (rtac (add_succ RS ssubst) 2);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   354
by (REPEAT (ares_tac [nnat, Ord_0_mem_succ, succ_mem_succI, 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   355
		      naturals_are_ordinals, nat_succI, add_type] 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   356
val add_less_succ_self = result();