src/CTT/arith.ML
author paulson
Mon Dec 07 18:26:25 1998 +0100 (1998-12-07)
changeset 6019 0e55c2fb2ebb
parent 0 a5a9c433f639
permissions -rw-r--r--
tidying
clasohm@0
     1
(*  Title: 	CTT/arith
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1991  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Theorems for arith.thy (Arithmetic operators)
clasohm@0
     7
clasohm@0
     8
Proofs about elementary arithmetic: addition, multiplication, etc.
clasohm@0
     9
Tests definitions and simplifier.
clasohm@0
    10
*)
clasohm@0
    11
clasohm@0
    12
open Arith;
clasohm@0
    13
val arith_defs = [add_def, diff_def, absdiff_def, mult_def, mod_def, div_def];
clasohm@0
    14
clasohm@0
    15
clasohm@0
    16
(** Addition *)
clasohm@0
    17
clasohm@0
    18
(*typing of add: short and long versions*)
clasohm@0
    19
clasohm@0
    20
val add_typing = prove_goal Arith.thy 
clasohm@0
    21
    "[| a:N;  b:N |] ==> a #+ b : N"
clasohm@0
    22
 (fn prems=>
clasohm@0
    23
  [ (rewrite_goals_tac arith_defs),
clasohm@0
    24
    (typechk_tac prems) ]);
clasohm@0
    25
clasohm@0
    26
val add_typingL = prove_goal Arith.thy 
clasohm@0
    27
    "[| a=c:N;  b=d:N |] ==> a #+ b = c #+ d : N"
clasohm@0
    28
 (fn prems=>
clasohm@0
    29
  [ (rewrite_goals_tac arith_defs),
clasohm@0
    30
    (equal_tac prems) ]);
clasohm@0
    31
clasohm@0
    32
clasohm@0
    33
(*computation for add: 0 and successor cases*)
clasohm@0
    34
clasohm@0
    35
val addC0 = prove_goal Arith.thy 
clasohm@0
    36
    "b:N ==> 0 #+ b = b : N"
clasohm@0
    37
 (fn prems=>
clasohm@0
    38
  [ (rewrite_goals_tac arith_defs),
clasohm@0
    39
    (rew_tac prems) ]);
clasohm@0
    40
clasohm@0
    41
val addC_succ = prove_goal Arith.thy 
clasohm@0
    42
    "[| a:N;  b:N |] ==> succ(a) #+ b = succ(a #+ b) : N"
clasohm@0
    43
 (fn prems=>
clasohm@0
    44
  [ (rewrite_goals_tac arith_defs),
clasohm@0
    45
    (rew_tac prems) ]); 
clasohm@0
    46
clasohm@0
    47
clasohm@0
    48
(** Multiplication *)
clasohm@0
    49
clasohm@0
    50
(*typing of mult: short and long versions*)
clasohm@0
    51
clasohm@0
    52
val mult_typing = prove_goal Arith.thy 
clasohm@0
    53
    "[| a:N;  b:N |] ==> a #* b : N"
clasohm@0
    54
 (fn prems=>
clasohm@0
    55
  [ (rewrite_goals_tac arith_defs),
clasohm@0
    56
    (typechk_tac([add_typing]@prems)) ]);
clasohm@0
    57
clasohm@0
    58
val mult_typingL = prove_goal Arith.thy 
clasohm@0
    59
    "[| a=c:N;  b=d:N |] ==> a #* b = c #* d : N"
clasohm@0
    60
 (fn prems=>
clasohm@0
    61
  [ (rewrite_goals_tac arith_defs),
clasohm@0
    62
    (equal_tac (prems@[add_typingL])) ]);
clasohm@0
    63
clasohm@0
    64
(*computation for mult: 0 and successor cases*)
clasohm@0
    65
clasohm@0
    66
val multC0 = prove_goal Arith.thy 
clasohm@0
    67
    "b:N ==> 0 #* b = 0 : N"
clasohm@0
    68
 (fn prems=>
clasohm@0
    69
  [ (rewrite_goals_tac arith_defs),
clasohm@0
    70
    (rew_tac prems) ]);
clasohm@0
    71
clasohm@0
    72
val multC_succ = prove_goal Arith.thy 
clasohm@0
    73
    "[| a:N;  b:N |] ==> succ(a) #* b = b #+ (a #* b) : N"
clasohm@0
    74
 (fn prems=>
clasohm@0
    75
  [ (rewrite_goals_tac arith_defs),
clasohm@0
    76
    (rew_tac prems) ]);
clasohm@0
    77
clasohm@0
    78
clasohm@0
    79
(** Difference *)
clasohm@0
    80
clasohm@0
    81
(*typing of difference*)
clasohm@0
    82
clasohm@0
    83
val diff_typing = prove_goal Arith.thy 
clasohm@0
    84
    "[| a:N;  b:N |] ==> a - b : N"
clasohm@0
    85
 (fn prems=>
clasohm@0
    86
  [ (rewrite_goals_tac arith_defs),
clasohm@0
    87
    (typechk_tac prems) ]);
clasohm@0
    88
clasohm@0
    89
val diff_typingL = prove_goal Arith.thy 
clasohm@0
    90
    "[| a=c:N;  b=d:N |] ==> a - b = c - d : N"
clasohm@0
    91
 (fn prems=>
clasohm@0
    92
  [ (rewrite_goals_tac arith_defs),
clasohm@0
    93
    (equal_tac prems) ]);
clasohm@0
    94
clasohm@0
    95
clasohm@0
    96
clasohm@0
    97
(*computation for difference: 0 and successor cases*)
clasohm@0
    98
clasohm@0
    99
val diffC0 = prove_goal Arith.thy 
clasohm@0
   100
    "a:N ==> a - 0 = a : N"
clasohm@0
   101
 (fn prems=>
clasohm@0
   102
  [ (rewrite_goals_tac arith_defs),
clasohm@0
   103
    (rew_tac prems) ]);
clasohm@0
   104
clasohm@0
   105
(*Note: rec(a, 0, %z w.z) is pred(a). *)
clasohm@0
   106
clasohm@0
   107
val diff_0_eq_0 = prove_goal Arith.thy 
clasohm@0
   108
    "b:N ==> 0 - b = 0 : N"
clasohm@0
   109
 (fn prems=>
clasohm@0
   110
  [ (NE_tac "b" 1),
clasohm@0
   111
    (rewrite_goals_tac arith_defs),
clasohm@0
   112
    (hyp_rew_tac prems) ]);
clasohm@0
   113
clasohm@0
   114
clasohm@0
   115
(*Essential to simplify FIRST!!  (Else we get a critical pair)
clasohm@0
   116
  succ(a) - succ(b) rewrites to   pred(succ(a) - b)  *)
clasohm@0
   117
val diff_succ_succ = prove_goal Arith.thy 
clasohm@0
   118
    "[| a:N;  b:N |] ==> succ(a) - succ(b) = a - b : N"
clasohm@0
   119
 (fn prems=>
clasohm@0
   120
  [ (rewrite_goals_tac arith_defs),
clasohm@0
   121
    (hyp_rew_tac prems),
clasohm@0
   122
    (NE_tac "b" 1),
clasohm@0
   123
    (hyp_rew_tac prems) ]);
clasohm@0
   124
clasohm@0
   125
clasohm@0
   126
clasohm@0
   127
(*** Simplification *)
clasohm@0
   128
clasohm@0
   129
val arith_typing_rls =
clasohm@0
   130
  [add_typing, mult_typing, diff_typing];
clasohm@0
   131
clasohm@0
   132
val arith_congr_rls =
clasohm@0
   133
  [add_typingL, mult_typingL, diff_typingL];
clasohm@0
   134
clasohm@0
   135
val congr_rls = arith_congr_rls@standard_congr_rls;
clasohm@0
   136
clasohm@0
   137
val arithC_rls =
clasohm@0
   138
  [addC0, addC_succ,
clasohm@0
   139
   multC0, multC_succ,
clasohm@0
   140
   diffC0, diff_0_eq_0, diff_succ_succ];
clasohm@0
   141
clasohm@0
   142
clasohm@0
   143
structure Arith_simp_data: TSIMP_DATA =
clasohm@0
   144
  struct
clasohm@0
   145
  val refl		= refl_elem
clasohm@0
   146
  val sym		= sym_elem
clasohm@0
   147
  val trans		= trans_elem
clasohm@0
   148
  val refl_red		= refl_red
clasohm@0
   149
  val trans_red		= trans_red
clasohm@0
   150
  val red_if_equal	= red_if_equal
clasohm@0
   151
  val default_rls 	= arithC_rls @ comp_rls
clasohm@0
   152
  val routine_tac 	= routine_tac (arith_typing_rls @ routine_rls)
clasohm@0
   153
  end;
clasohm@0
   154
clasohm@0
   155
structure Arith_simp = TSimpFun (Arith_simp_data);
clasohm@0
   156
clasohm@0
   157
fun arith_rew_tac prems = make_rew_tac
clasohm@0
   158
    (Arith_simp.norm_tac(congr_rls, prems));
clasohm@0
   159
clasohm@0
   160
fun hyp_arith_rew_tac prems = make_rew_tac
clasohm@0
   161
    (Arith_simp.cond_norm_tac(prove_cond_tac, congr_rls, prems));
clasohm@0
   162
clasohm@0
   163
clasohm@0
   164
(**********
clasohm@0
   165
  Addition
clasohm@0
   166
 **********)
clasohm@0
   167
clasohm@0
   168
(*Associative law for addition*)
clasohm@0
   169
val add_assoc = prove_goal Arith.thy 
clasohm@0
   170
    "[| a:N;  b:N;  c:N |] ==> (a #+ b) #+ c = a #+ (b #+ c) : N"
clasohm@0
   171
 (fn prems=>
clasohm@0
   172
  [ (NE_tac "a" 1),
clasohm@0
   173
    (hyp_arith_rew_tac prems) ]);
clasohm@0
   174
clasohm@0
   175
clasohm@0
   176
(*Commutative law for addition.  Can be proved using three inductions.
clasohm@0
   177
  Must simplify after first induction!  Orientation of rewrites is delicate*)  
clasohm@0
   178
val add_commute = prove_goal Arith.thy 
clasohm@0
   179
    "[| a:N;  b:N |] ==> a #+ b = b #+ a : N"
clasohm@0
   180
 (fn prems=>
clasohm@0
   181
  [ (NE_tac "a" 1),
clasohm@0
   182
    (hyp_arith_rew_tac prems),
clasohm@0
   183
    (NE_tac "b" 2),
clasohm@0
   184
    (resolve_tac [sym_elem] 1),
clasohm@0
   185
    (NE_tac "b" 1),
clasohm@0
   186
    (hyp_arith_rew_tac prems) ]);
clasohm@0
   187
clasohm@0
   188
clasohm@0
   189
(****************
clasohm@0
   190
  Multiplication
clasohm@0
   191
 ****************)
clasohm@0
   192
clasohm@0
   193
(*Commutative law for multiplication
clasohm@0
   194
val mult_commute = prove_goal Arith.thy 
clasohm@0
   195
    "[| a:N;  b:N |] ==> a #* b = b #* a : N"
clasohm@0
   196
 (fn prems=>
clasohm@0
   197
  [ (NE_tac "a" 1),
clasohm@0
   198
    (hyp_arith_rew_tac prems),
clasohm@0
   199
    (NE_tac "b" 2),
clasohm@0
   200
    (resolve_tac [sym_elem] 1),
clasohm@0
   201
    (NE_tac "b" 1),
clasohm@0
   202
    (hyp_arith_rew_tac prems) ]);   NEEDS COMMUTATIVE MATCHING
clasohm@0
   203
***************)
clasohm@0
   204
clasohm@0
   205
(*right annihilation in product*)
clasohm@0
   206
val mult_0_right = prove_goal Arith.thy 
clasohm@0
   207
    "a:N ==> a #* 0 = 0 : N"
clasohm@0
   208
 (fn prems=>
clasohm@0
   209
  [ (NE_tac "a" 1),
clasohm@0
   210
    (hyp_arith_rew_tac prems) ]);
clasohm@0
   211
clasohm@0
   212
(*right successor law for multiplication*)
clasohm@0
   213
val mult_succ_right = prove_goal Arith.thy 
clasohm@0
   214
    "[| a:N;  b:N |] ==> a #* succ(b) = a #+ (a #* b) : N"
clasohm@0
   215
 (fn prems=>
clasohm@0
   216
  [ (NE_tac "a" 1),
clasohm@0
   217
(*swap round the associative law of addition*)
clasohm@0
   218
    (hyp_arith_rew_tac (prems @ [add_assoc RS sym_elem])),  
clasohm@0
   219
(*leaves a goal involving a commutative law*)
clasohm@0
   220
    (REPEAT (assume_tac 1  ORELSE  
clasohm@0
   221
            resolve_tac
clasohm@0
   222
             (prems@[add_commute,mult_typingL,add_typingL]@
clasohm@0
   223
	       intrL_rls@[refl_elem])   1)) ]);
clasohm@0
   224
clasohm@0
   225
(*Commutative law for multiplication*)
clasohm@0
   226
val mult_commute = prove_goal Arith.thy 
clasohm@0
   227
    "[| a:N;  b:N |] ==> a #* b = b #* a : N"
clasohm@0
   228
 (fn prems=>
clasohm@0
   229
  [ (NE_tac "a" 1),
clasohm@0
   230
    (hyp_arith_rew_tac (prems @ [mult_0_right, mult_succ_right])) ]);
clasohm@0
   231
clasohm@0
   232
(*addition distributes over multiplication*)
clasohm@0
   233
val add_mult_distrib = prove_goal Arith.thy 
clasohm@0
   234
    "[| a:N;  b:N;  c:N |] ==> (a #+ b) #* c = (a #* c) #+ (b #* c) : N"
clasohm@0
   235
 (fn prems=>
clasohm@0
   236
  [ (NE_tac "a" 1),
clasohm@0
   237
(*swap round the associative law of addition*)
clasohm@0
   238
    (hyp_arith_rew_tac (prems @ [add_assoc RS sym_elem])) ]);
clasohm@0
   239
clasohm@0
   240
clasohm@0
   241
(*Associative law for multiplication*)
clasohm@0
   242
val mult_assoc = prove_goal Arith.thy 
clasohm@0
   243
    "[| a:N;  b:N;  c:N |] ==> (a #* b) #* c = a #* (b #* c) : N"
clasohm@0
   244
 (fn prems=>
clasohm@0
   245
  [ (NE_tac "a" 1),
clasohm@0
   246
    (hyp_arith_rew_tac (prems @ [add_mult_distrib])) ]);
clasohm@0
   247
clasohm@0
   248
clasohm@0
   249
(************
clasohm@0
   250
  Difference
clasohm@0
   251
 ************
clasohm@0
   252
clasohm@0
   253
Difference on natural numbers, without negative numbers
clasohm@0
   254
  a - b = 0  iff  a<=b    a - b = succ(c) iff a>b   *)
clasohm@0
   255
clasohm@0
   256
val diff_self_eq_0 = prove_goal Arith.thy 
clasohm@0
   257
    "a:N ==> a - a = 0 : N"
clasohm@0
   258
 (fn prems=>
clasohm@0
   259
  [ (NE_tac "a" 1),
clasohm@0
   260
    (hyp_arith_rew_tac prems) ]);
clasohm@0
   261
clasohm@0
   262
clasohm@0
   263
(*  [| c : N; 0 : N; c : N |] ==> c #+ 0 = c : N  *)
clasohm@0
   264
val add_0_right = addC0 RSN (3, add_commute RS trans_elem);
clasohm@0
   265
clasohm@0
   266
(*Addition is the inverse of subtraction: if b<=x then b#+(x-b) = x.
clasohm@0
   267
  An example of induction over a quantified formula (a product).
clasohm@0
   268
  Uses rewriting with a quantified, implicative inductive hypothesis.*)
clasohm@0
   269
val prems =
clasohm@0
   270
goal Arith.thy 
clasohm@0
   271
    "b:N ==> ?a : PROD x:N. Eq(N, b-x, 0) --> Eq(N, b #+ (x-b), x)";
clasohm@0
   272
by (NE_tac "b" 1);
clasohm@0
   273
(*strip one "universal quantifier" but not the "implication"*)
clasohm@0
   274
by (resolve_tac intr_rls 3);  
clasohm@0
   275
(*case analysis on x in
clasohm@0
   276
    (succ(u) <= x) --> (succ(u)#+(x-succ(u)) = x) *)
clasohm@0
   277
by (NE_tac "x" 4 THEN assume_tac 4); 
clasohm@0
   278
(*Prepare for simplification of types -- the antecedent succ(u)<=x *)
clasohm@0
   279
by (resolve_tac [replace_type] 5);
clasohm@0
   280
by (resolve_tac [replace_type] 4);
clasohm@0
   281
by (arith_rew_tac prems); 
clasohm@0
   282
(*Solves first 0 goal, simplifies others.  Two sugbgoals remain.
clasohm@0
   283
  Both follow by rewriting, (2) using quantified induction hyp*)
clasohm@0
   284
by (intr_tac[]);  (*strips remaining PRODs*)
clasohm@0
   285
by (hyp_arith_rew_tac (prems@[add_0_right]));  
clasohm@0
   286
by (assume_tac 1);
clasohm@0
   287
val add_diff_inverse_lemma = result();
clasohm@0
   288
clasohm@0
   289
clasohm@0
   290
(*Version of above with premise   b-a=0   i.e.    a >= b.
clasohm@0
   291
  Using ProdE does not work -- for ?B(?a) is ambiguous.
clasohm@0
   292
  Instead, add_diff_inverse_lemma states the desired induction scheme;
clasohm@0
   293
    the use of RS below instantiates Vars in ProdE automatically. *)
clasohm@0
   294
val prems =
clasohm@0
   295
goal Arith.thy "[| a:N;  b:N;  b-a = 0 : N |] ==> b #+ (a-b) = a : N";
clasohm@0
   296
by (resolve_tac [EqE] 1);
clasohm@0
   297
by (resolve_tac [ add_diff_inverse_lemma RS ProdE RS ProdE ] 1);
clasohm@0
   298
by (REPEAT (resolve_tac (prems@[EqI]) 1));
clasohm@0
   299
val add_diff_inverse = result();
clasohm@0
   300
clasohm@0
   301
clasohm@0
   302
(********************
clasohm@0
   303
  Absolute difference
clasohm@0
   304
 ********************)
clasohm@0
   305
clasohm@0
   306
(*typing of absolute difference: short and long versions*)
clasohm@0
   307
clasohm@0
   308
val absdiff_typing = prove_goal Arith.thy 
clasohm@0
   309
    "[| a:N;  b:N |] ==> a |-| b : N"
clasohm@0
   310
 (fn prems=>
clasohm@0
   311
  [ (rewrite_goals_tac arith_defs),
clasohm@0
   312
    (typechk_tac prems) ]);
clasohm@0
   313
clasohm@0
   314
val absdiff_typingL = prove_goal Arith.thy 
clasohm@0
   315
    "[| a=c:N;  b=d:N |] ==> a |-| b = c |-| d : N"
clasohm@0
   316
 (fn prems=>
clasohm@0
   317
  [ (rewrite_goals_tac arith_defs),
clasohm@0
   318
    (equal_tac prems) ]);
clasohm@0
   319
clasohm@0
   320
val absdiff_self_eq_0 = prove_goal Arith.thy 
clasohm@0
   321
    "a:N ==> a |-| a = 0 : N"
clasohm@0
   322
 (fn prems=>
clasohm@0
   323
  [ (rewrite_goals_tac [absdiff_def]),
clasohm@0
   324
    (arith_rew_tac (prems@[diff_self_eq_0])) ]);
clasohm@0
   325
clasohm@0
   326
val absdiffC0 = prove_goal Arith.thy 
clasohm@0
   327
    "a:N ==> 0 |-| a = a : N"
clasohm@0
   328
 (fn prems=>
clasohm@0
   329
  [ (rewrite_goals_tac [absdiff_def]),
clasohm@0
   330
    (hyp_arith_rew_tac prems) ]);
clasohm@0
   331
clasohm@0
   332
clasohm@0
   333
val absdiff_succ_succ = prove_goal Arith.thy 
clasohm@0
   334
    "[| a:N;  b:N |] ==> succ(a) |-| succ(b)  =  a |-| b : N"
clasohm@0
   335
 (fn prems=>
clasohm@0
   336
  [ (rewrite_goals_tac [absdiff_def]),
clasohm@0
   337
    (hyp_arith_rew_tac prems) ]);
clasohm@0
   338
clasohm@0
   339
(*Note how easy using commutative laws can be?  ...not always... *)
clasohm@0
   340
val prems = goal Arith.thy "[| a:N;  b:N |] ==> a |-| b = b |-| a : N";
clasohm@0
   341
by (rewrite_goals_tac [absdiff_def]);
clasohm@0
   342
by (resolve_tac [add_commute] 1);
clasohm@0
   343
by (typechk_tac ([diff_typing]@prems));
clasohm@0
   344
val absdiff_commute = result();
clasohm@0
   345
clasohm@0
   346
(*If a+b=0 then a=0.   Surprisingly tedious*)
clasohm@0
   347
val prems =
clasohm@0
   348
goal Arith.thy "[| a:N;  b:N |] ==> ?c : PROD u: Eq(N,a#+b,0) .  Eq(N,a,0)";
clasohm@0
   349
by (NE_tac "a" 1);
clasohm@0
   350
by (resolve_tac [replace_type] 3);
clasohm@0
   351
by (arith_rew_tac prems);
clasohm@0
   352
by (intr_tac[]);  (*strips remaining PRODs*)
clasohm@0
   353
by (resolve_tac [ zero_ne_succ RS FE ] 2);
clasohm@0
   354
by (etac (EqE RS sym_elem) 3);
clasohm@0
   355
by (typechk_tac ([add_typing] @prems));
clasohm@0
   356
val add_eq0_lemma = result();
clasohm@0
   357
clasohm@0
   358
(*Version of above with the premise  a+b=0.
clasohm@0
   359
  Again, resolution instantiates variables in ProdE *)
clasohm@0
   360
val prems =
clasohm@0
   361
goal Arith.thy "[| a:N;  b:N;  a #+ b = 0 : N |] ==> a = 0 : N";
clasohm@0
   362
by (resolve_tac [EqE] 1);
clasohm@0
   363
by (resolve_tac [add_eq0_lemma RS ProdE] 1);
clasohm@0
   364
by (resolve_tac [EqI] 3);
clasohm@0
   365
by (ALLGOALS (resolve_tac prems));
clasohm@0
   366
val add_eq0 = result();
clasohm@0
   367
clasohm@0
   368
(*Here is a lemma to infer a-b=0 and b-a=0 from a|-|b=0, below. *)
clasohm@0
   369
val prems = goal Arith.thy
clasohm@0
   370
    "[| a:N;  b:N;  a |-| b = 0 : N |] ==> \
clasohm@0
   371
\    ?a : SUM v: Eq(N, a-b, 0) . Eq(N, b-a, 0)";
clasohm@0
   372
by (intr_tac[]);
clasohm@0
   373
by eqintr_tac;
clasohm@0
   374
by (resolve_tac [add_eq0] 2);
clasohm@0
   375
by (resolve_tac [add_eq0] 1);
clasohm@0
   376
by (resolve_tac [add_commute RS trans_elem] 6);
clasohm@0
   377
by (typechk_tac (diff_typing:: map (rewrite_rule [absdiff_def]) prems));
clasohm@0
   378
val absdiff_eq0_lem = result();
clasohm@0
   379
clasohm@0
   380
(*if  a |-| b = 0  then  a = b  
clasohm@0
   381
  proof: a-b=0 and b-a=0, so b = a+(b-a) = a+0 = a*)
clasohm@0
   382
val prems =
clasohm@0
   383
goal Arith.thy "[| a |-| b = 0 : N;  a:N;  b:N |] ==> a = b : N";
clasohm@0
   384
by (resolve_tac [EqE] 1);
clasohm@0
   385
by (resolve_tac [absdiff_eq0_lem RS SumE] 1);
clasohm@0
   386
by (TRYALL (resolve_tac prems));
clasohm@0
   387
by eqintr_tac;
clasohm@0
   388
by (resolve_tac [add_diff_inverse RS sym_elem RS trans_elem] 1);
clasohm@0
   389
by (resolve_tac [EqE] 3  THEN  assume_tac 3);
clasohm@0
   390
by (hyp_arith_rew_tac (prems@[add_0_right]));
clasohm@0
   391
val absdiff_eq0 = result();
clasohm@0
   392
clasohm@0
   393
(***********************
clasohm@0
   394
  Remainder and Quotient
clasohm@0
   395
 ***********************)
clasohm@0
   396
clasohm@0
   397
(*typing of remainder: short and long versions*)
clasohm@0
   398
clasohm@0
   399
val mod_typing = prove_goal Arith.thy
clasohm@0
   400
    "[| a:N;  b:N |] ==> a mod b : N"
clasohm@0
   401
 (fn prems=>
clasohm@0
   402
  [ (rewrite_goals_tac [mod_def]),
clasohm@0
   403
    (typechk_tac (absdiff_typing::prems)) ]);
clasohm@0
   404
 
clasohm@0
   405
val mod_typingL = prove_goal Arith.thy
clasohm@0
   406
    "[| a=c:N;  b=d:N |] ==> a mod b = c mod d : N"
clasohm@0
   407
 (fn prems=>
clasohm@0
   408
  [ (rewrite_goals_tac [mod_def]),
clasohm@0
   409
    (equal_tac (prems@[absdiff_typingL])) ]);
clasohm@0
   410
 
clasohm@0
   411
clasohm@0
   412
(*computation for  mod : 0 and successor cases*)
clasohm@0
   413
clasohm@0
   414
val modC0 = prove_goal Arith.thy "b:N ==> 0 mod b = 0 : N"
clasohm@0
   415
 (fn prems=>
clasohm@0
   416
  [ (rewrite_goals_tac [mod_def]),
clasohm@0
   417
    (rew_tac(absdiff_typing::prems)) ]);
clasohm@0
   418
clasohm@0
   419
val modC_succ = prove_goal Arith.thy 
clasohm@0
   420
"[| a:N; b:N |] ==> succ(a) mod b = rec(succ(a mod b) |-| b, 0, %x y.succ(a mod b)) : N"
clasohm@0
   421
 (fn prems=>
clasohm@0
   422
  [ (rewrite_goals_tac [mod_def]),
clasohm@0
   423
    (rew_tac(absdiff_typing::prems)) ]);
clasohm@0
   424
clasohm@0
   425
clasohm@0
   426
(*typing of quotient: short and long versions*)
clasohm@0
   427
clasohm@0
   428
val div_typing = prove_goal Arith.thy "[| a:N;  b:N |] ==> a div b : N"
clasohm@0
   429
 (fn prems=>
clasohm@0
   430
  [ (rewrite_goals_tac [div_def]),
clasohm@0
   431
    (typechk_tac ([absdiff_typing,mod_typing]@prems)) ]);
clasohm@0
   432
clasohm@0
   433
val div_typingL = prove_goal Arith.thy
clasohm@0
   434
   "[| a=c:N;  b=d:N |] ==> a div b = c div d : N"
clasohm@0
   435
 (fn prems=>
clasohm@0
   436
  [ (rewrite_goals_tac [div_def]),
clasohm@0
   437
    (equal_tac (prems @ [absdiff_typingL, mod_typingL])) ]);
clasohm@0
   438
clasohm@0
   439
val div_typing_rls = [mod_typing, div_typing, absdiff_typing];
clasohm@0
   440
clasohm@0
   441
clasohm@0
   442
(*computation for quotient: 0 and successor cases*)
clasohm@0
   443
clasohm@0
   444
val divC0 = prove_goal Arith.thy "b:N ==> 0 div b = 0 : N"
clasohm@0
   445
 (fn prems=>
clasohm@0
   446
  [ (rewrite_goals_tac [div_def]),
clasohm@0
   447
    (rew_tac([mod_typing, absdiff_typing] @ prems)) ]);
clasohm@0
   448
clasohm@0
   449
val divC_succ =
clasohm@0
   450
prove_goal Arith.thy "[| a:N;  b:N |] ==> succ(a) div b = \
clasohm@0
   451
\    rec(succ(a) mod b, succ(a div b), %x y. a div b) : N"
clasohm@0
   452
 (fn prems=>
clasohm@0
   453
  [ (rewrite_goals_tac [div_def]),
clasohm@0
   454
    (rew_tac([mod_typing]@prems)) ]);
clasohm@0
   455
clasohm@0
   456
clasohm@0
   457
(*Version of above with same condition as the  mod  one*)
clasohm@0
   458
val divC_succ2 = prove_goal Arith.thy
clasohm@0
   459
    "[| a:N;  b:N |] ==> \
clasohm@0
   460
\    succ(a) div b =rec(succ(a mod b) |-| b, succ(a div b), %x y. a div b) : N"
clasohm@0
   461
 (fn prems=>
clasohm@0
   462
  [ (resolve_tac [ divC_succ RS trans_elem ] 1),
clasohm@0
   463
    (rew_tac(div_typing_rls @ prems @ [modC_succ])),
clasohm@0
   464
    (NE_tac "succ(a mod b)|-|b" 1),
clasohm@0
   465
    (rew_tac ([mod_typing, div_typing, absdiff_typing] @prems)) ]);
clasohm@0
   466
clasohm@0
   467
(*for case analysis on whether a number is 0 or a successor*)
clasohm@0
   468
val iszero_decidable = prove_goal Arith.thy
clasohm@0
   469
    "a:N ==> rec(a, inl(eq), %ka kb.inr(<ka, eq>)) : \
clasohm@0
   470
\		      Eq(N,a,0) + (SUM x:N. Eq(N,a, succ(x)))"
clasohm@0
   471
 (fn prems=>
clasohm@0
   472
  [ (NE_tac "a" 1),
clasohm@0
   473
    (resolve_tac [PlusI_inr] 3),
clasohm@0
   474
    (resolve_tac [PlusI_inl] 2),
clasohm@0
   475
    eqintr_tac,
clasohm@0
   476
    (equal_tac prems) ]);
clasohm@0
   477
clasohm@0
   478
(*Main Result.  Holds when b is 0 since   a mod 0 = a     and    a div 0 = 0  *)
clasohm@0
   479
val prems =
clasohm@0
   480
goal Arith.thy "[| a:N;  b:N |] ==> a mod b  #+  (a div b) #* b = a : N";
clasohm@0
   481
by (NE_tac "a" 1);
clasohm@0
   482
by (arith_rew_tac (div_typing_rls@prems@[modC0,modC_succ,divC0,divC_succ2])); 
clasohm@0
   483
by (resolve_tac [EqE] 1);
clasohm@0
   484
(*case analysis on   succ(u mod b)|-|b  *)
clasohm@0
   485
by (res_inst_tac [("a1", "succ(u mod b) |-| b")] 
clasohm@0
   486
                 (iszero_decidable RS PlusE) 1);
clasohm@0
   487
by (etac SumE 3);
clasohm@0
   488
by (hyp_arith_rew_tac (prems @ div_typing_rls @
clasohm@0
   489
	[modC0,modC_succ, divC0, divC_succ2])); 
clasohm@0
   490
(*Replace one occurence of  b  by succ(u mod b).  Clumsy!*)
clasohm@0
   491
by (resolve_tac [ add_typingL RS trans_elem ] 1);
clasohm@0
   492
by (eresolve_tac [EqE RS absdiff_eq0 RS sym_elem] 1);
clasohm@0
   493
by (resolve_tac [refl_elem] 3);
clasohm@0
   494
by (hyp_arith_rew_tac (prems @ div_typing_rls)); 
clasohm@0
   495
val mod_div_equality = result();
clasohm@0
   496
clasohm@0
   497
writeln"Reached end of file.";