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