src/ZF/ArithSimp.thy
author paulson
Sat Jun 29 21:33:06 2002 +0200 (2002-06-29 ago)
changeset 13259 01fa0c8dbc92
parent 9548 15bee2731e43
child 13328 703de709a64b
permissions -rw-r--r--
conversion of many files to Isar format
paulson@9548
     1
(*  Title:      ZF/ArithSimp.ML
paulson@9548
     2
    ID:         $Id$
paulson@9548
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@9548
     4
    Copyright   2000  University of Cambridge
paulson@9548
     5
paulson@9548
     6
Arithmetic with simplification
paulson@9548
     7
*)
paulson@9548
     8
paulson@9548
     9
theory ArithSimp = Arith
paulson@9548
    10
files "arith_data.ML":
paulson@13259
    11
paulson@13259
    12
(*** Difference ***)
paulson@13259
    13
paulson@13259
    14
lemma diff_self_eq_0: "m #- m = 0"
paulson@13259
    15
apply (subgoal_tac "natify (m) #- natify (m) = 0")
paulson@13259
    16
apply (rule_tac [2] natify_in_nat [THEN nat_induct], auto)
paulson@13259
    17
done
paulson@13259
    18
paulson@13259
    19
(**Addition is the inverse of subtraction**)
paulson@13259
    20
paulson@13259
    21
(*We need m:nat even if we replace the RHS by natify(m), for consider e.g.
paulson@13259
    22
  n=2, m=omega; then n + (m-n) = 2 + (0-2) = 2 ~= 0 = natify(m).*)
paulson@13259
    23
lemma add_diff_inverse: "[| n le m;  m:nat |] ==> n #+ (m#-n) = m"
paulson@13259
    24
apply (frule lt_nat_in_nat, erule nat_succI)
paulson@13259
    25
apply (erule rev_mp)
paulson@13259
    26
apply (rule_tac m = "m" and n = "n" in diff_induct, auto)
paulson@13259
    27
done
paulson@13259
    28
paulson@13259
    29
lemma add_diff_inverse2: "[| n le m;  m:nat |] ==> (m#-n) #+ n = m"
paulson@13259
    30
apply (frule lt_nat_in_nat, erule nat_succI)
paulson@13259
    31
apply (simp (no_asm_simp) add: add_commute add_diff_inverse)
paulson@13259
    32
done
paulson@13259
    33
paulson@13259
    34
(*Proof is IDENTICAL to that of add_diff_inverse*)
paulson@13259
    35
lemma diff_succ: "[| n le m;  m:nat |] ==> succ(m) #- n = succ(m#-n)"
paulson@13259
    36
apply (frule lt_nat_in_nat, erule nat_succI)
paulson@13259
    37
apply (erule rev_mp)
paulson@13259
    38
apply (rule_tac m = "m" and n = "n" in diff_induct)
paulson@13259
    39
apply (simp_all (no_asm_simp))
paulson@13259
    40
done
paulson@13259
    41
paulson@13259
    42
lemma zero_less_diff [simp]:
paulson@13259
    43
     "[| m: nat; n: nat |] ==> 0 < (n #- m)   <->   m<n"
paulson@13259
    44
apply (rule_tac m = "m" and n = "n" in diff_induct)
paulson@13259
    45
apply (simp_all (no_asm_simp))
paulson@13259
    46
done
paulson@13259
    47
paulson@13259
    48
paulson@13259
    49
(** Difference distributes over multiplication **)
paulson@13259
    50
paulson@13259
    51
lemma diff_mult_distrib: "(m #- n) #* k = (m #* k) #- (n #* k)"
paulson@13259
    52
apply (subgoal_tac " (natify (m) #- natify (n)) #* natify (k) = (natify (m) #* natify (k)) #- (natify (n) #* natify (k))")
paulson@13259
    53
apply (rule_tac [2] m = "natify (m) " and n = "natify (n) " in diff_induct)
paulson@13259
    54
apply (simp_all add: diff_cancel)
paulson@13259
    55
done
paulson@13259
    56
paulson@13259
    57
lemma diff_mult_distrib2: "k #* (m #- n) = (k #* m) #- (k #* n)"
paulson@13259
    58
apply (simp (no_asm) add: mult_commute [of k] diff_mult_distrib)
paulson@13259
    59
done
paulson@13259
    60
paulson@13259
    61
paulson@13259
    62
(*** Remainder ***)
paulson@13259
    63
paulson@13259
    64
(*We need m:nat even with natify*)
paulson@13259
    65
lemma div_termination: "[| 0<n;  n le m;  m:nat |] ==> m #- n < m"
paulson@13259
    66
apply (frule lt_nat_in_nat, erule nat_succI)
paulson@13259
    67
apply (erule rev_mp)
paulson@13259
    68
apply (erule rev_mp)
paulson@13259
    69
apply (rule_tac m = "m" and n = "n" in diff_induct)
paulson@13259
    70
apply (simp_all (no_asm_simp) add: diff_le_self)
paulson@13259
    71
done
paulson@13259
    72
paulson@13259
    73
(*for mod and div*)
paulson@13259
    74
lemmas div_rls = 
paulson@13259
    75
    nat_typechecks Ord_transrec_type apply_funtype 
paulson@13259
    76
    div_termination [THEN ltD]
paulson@13259
    77
    nat_into_Ord not_lt_iff_le [THEN iffD1]
paulson@13259
    78
paulson@13259
    79
lemma raw_mod_type: "[| m:nat;  n:nat |] ==> raw_mod (m, n) : nat"
paulson@13259
    80
apply (unfold raw_mod_def)
paulson@13259
    81
apply (rule Ord_transrec_type)
paulson@13259
    82
apply (auto simp add: nat_into_Ord [THEN Ord_0_lt_iff])
paulson@13259
    83
apply (blast intro: div_rls) 
paulson@13259
    84
done
paulson@13259
    85
paulson@13259
    86
lemma mod_type [TC,iff]: "m mod n : nat"
paulson@13259
    87
apply (unfold mod_def)
paulson@13259
    88
apply (simp (no_asm) add: mod_def raw_mod_type)
paulson@13259
    89
done
paulson@13259
    90
paulson@13259
    91
paulson@13259
    92
(** Aribtrary definitions for division by zero.  Useful to simplify 
paulson@13259
    93
    certain equations **)
paulson@13259
    94
paulson@13259
    95
lemma DIVISION_BY_ZERO_DIV: "a div 0 = 0"
paulson@13259
    96
apply (unfold div_def)
paulson@13259
    97
apply (rule raw_div_def [THEN def_transrec, THEN trans])
paulson@13259
    98
apply (simp (no_asm_simp))
paulson@13259
    99
done  (*NOT for adding to default simpset*)
paulson@13259
   100
paulson@13259
   101
lemma DIVISION_BY_ZERO_MOD: "a mod 0 = natify(a)"
paulson@13259
   102
apply (unfold mod_def)
paulson@13259
   103
apply (rule raw_mod_def [THEN def_transrec, THEN trans])
paulson@13259
   104
apply (simp (no_asm_simp))
paulson@13259
   105
done  (*NOT for adding to default simpset*)
paulson@13259
   106
paulson@13259
   107
lemma raw_mod_less: "m<n ==> raw_mod (m,n) = m"
paulson@13259
   108
apply (rule raw_mod_def [THEN def_transrec, THEN trans])
paulson@13259
   109
apply (simp (no_asm_simp) add: div_termination [THEN ltD])
paulson@13259
   110
done
paulson@13259
   111
paulson@13259
   112
lemma mod_less [simp]: "[| m<n; n : nat |] ==> m mod n = m"
paulson@13259
   113
apply (frule lt_nat_in_nat, assumption)
paulson@13259
   114
apply (simp (no_asm_simp) add: mod_def raw_mod_less)
paulson@13259
   115
done
paulson@13259
   116
paulson@13259
   117
lemma raw_mod_geq:
paulson@13259
   118
     "[| 0<n; n le m;  m:nat |] ==> raw_mod (m, n) = raw_mod (m#-n, n)"
paulson@13259
   119
apply (frule lt_nat_in_nat, erule nat_succI)
paulson@13259
   120
apply (rule raw_mod_def [THEN def_transrec, THEN trans])
paulson@13259
   121
apply (simp add: div_termination [THEN ltD] not_lt_iff_le [THEN iffD2], blast)
paulson@13259
   122
done
paulson@13259
   123
paulson@13259
   124
paulson@13259
   125
lemma mod_geq: "[| n le m;  m:nat |] ==> m mod n = (m#-n) mod n"
paulson@13259
   126
apply (frule lt_nat_in_nat, erule nat_succI)
paulson@13259
   127
apply (case_tac "n=0")
paulson@13259
   128
 apply (simp add: DIVISION_BY_ZERO_MOD)
paulson@13259
   129
apply (simp add: mod_def raw_mod_geq nat_into_Ord [THEN Ord_0_lt_iff])
paulson@13259
   130
done
paulson@13259
   131
paulson@13259
   132
paulson@13259
   133
(*** Division ***)
paulson@13259
   134
paulson@13259
   135
lemma raw_div_type: "[| m:nat;  n:nat |] ==> raw_div (m, n) : nat"
paulson@13259
   136
apply (unfold raw_div_def)
paulson@13259
   137
apply (rule Ord_transrec_type)
paulson@13259
   138
apply (auto simp add: nat_into_Ord [THEN Ord_0_lt_iff])
paulson@13259
   139
apply (blast intro: div_rls) 
paulson@13259
   140
done
paulson@13259
   141
paulson@13259
   142
lemma div_type [TC,iff]: "m div n : nat"
paulson@13259
   143
apply (unfold div_def)
paulson@13259
   144
apply (simp (no_asm) add: div_def raw_div_type)
paulson@13259
   145
done
paulson@13259
   146
paulson@13259
   147
lemma raw_div_less: "m<n ==> raw_div (m,n) = 0"
paulson@13259
   148
apply (rule raw_div_def [THEN def_transrec, THEN trans])
paulson@13259
   149
apply (simp (no_asm_simp) add: div_termination [THEN ltD])
paulson@13259
   150
done
paulson@13259
   151
paulson@13259
   152
lemma div_less [simp]: "[| m<n; n : nat |] ==> m div n = 0"
paulson@13259
   153
apply (frule lt_nat_in_nat, assumption)
paulson@13259
   154
apply (simp (no_asm_simp) add: div_def raw_div_less)
paulson@13259
   155
done
paulson@13259
   156
paulson@13259
   157
lemma raw_div_geq: "[| 0<n;  n le m;  m:nat |] ==> raw_div(m,n) = succ(raw_div(m#-n, n))"
paulson@13259
   158
apply (subgoal_tac "n ~= 0")
paulson@13259
   159
prefer 2 apply blast
paulson@13259
   160
apply (frule lt_nat_in_nat, erule nat_succI)
paulson@13259
   161
apply (rule raw_div_def [THEN def_transrec, THEN trans])
paulson@13259
   162
apply (simp add: div_termination [THEN ltD] not_lt_iff_le [THEN iffD2] ) 
paulson@13259
   163
done
paulson@13259
   164
paulson@13259
   165
lemma div_geq [simp]:
paulson@13259
   166
     "[| 0<n;  n le m;  m:nat |] ==> m div n = succ ((m#-n) div n)"
paulson@13259
   167
apply (frule lt_nat_in_nat, erule nat_succI)
paulson@13259
   168
apply (simp (no_asm_simp) add: div_def raw_div_geq)
paulson@13259
   169
done
paulson@13259
   170
paulson@13259
   171
declare div_less [simp] div_geq [simp]
paulson@13259
   172
paulson@13259
   173
paulson@13259
   174
(*A key result*)
paulson@13259
   175
lemma mod_div_lemma: "[| m: nat;  n: nat |] ==> (m div n)#*n #+ m mod n = m"
paulson@13259
   176
apply (case_tac "n=0")
paulson@13259
   177
 apply (simp add: DIVISION_BY_ZERO_MOD)
paulson@13259
   178
apply (simp add: nat_into_Ord [THEN Ord_0_lt_iff])
paulson@13259
   179
apply (erule complete_induct)
paulson@13259
   180
apply (case_tac "x<n")
paulson@13259
   181
txt{*case x<n*}
paulson@13259
   182
apply (simp (no_asm_simp))
paulson@13259
   183
txt{*case n le x*}
paulson@13259
   184
apply (simp add: not_lt_iff_le add_assoc mod_geq div_termination [THEN ltD] add_diff_inverse)
paulson@13259
   185
done
paulson@13259
   186
paulson@13259
   187
lemma mod_div_equality_natify: "(m div n)#*n #+ m mod n = natify(m)"
paulson@13259
   188
apply (subgoal_tac " (natify (m) div natify (n))#*natify (n) #+ natify (m) mod natify (n) = natify (m) ")
paulson@13259
   189
apply force 
paulson@13259
   190
apply (subst mod_div_lemma, auto)
paulson@13259
   191
done
paulson@13259
   192
paulson@13259
   193
lemma mod_div_equality: "m: nat ==> (m div n)#*n #+ m mod n = m"
paulson@13259
   194
apply (simp (no_asm_simp) add: mod_div_equality_natify)
paulson@13259
   195
done
paulson@13259
   196
paulson@13259
   197
paulson@13259
   198
(*** Further facts about mod (mainly for mutilated chess board) ***)
paulson@13259
   199
paulson@13259
   200
lemma mod_succ_lemma:
paulson@13259
   201
     "[| 0<n;  m:nat;  n:nat |]  
paulson@13259
   202
      ==> succ(m) mod n = (if succ(m mod n) = n then 0 else succ(m mod n))"
paulson@13259
   203
apply (erule complete_induct)
paulson@13259
   204
apply (case_tac "succ (x) <n")
paulson@13259
   205
txt{* case succ(x) < n *}
paulson@13259
   206
 apply (simp (no_asm_simp) add: nat_le_refl [THEN lt_trans] succ_neq_self)
paulson@13259
   207
 apply (simp add: ltD [THEN mem_imp_not_eq])
paulson@13259
   208
txt{* case n le succ(x) *}
paulson@13259
   209
apply (simp add: mod_geq not_lt_iff_le)
paulson@13259
   210
apply (erule leE)
paulson@13259
   211
 apply (simp (no_asm_simp) add: mod_geq div_termination [THEN ltD] diff_succ)
paulson@13259
   212
txt{*equality case*}
paulson@13259
   213
apply (simp add: diff_self_eq_0)
paulson@13259
   214
done
paulson@13259
   215
paulson@13259
   216
lemma mod_succ:
paulson@13259
   217
  "n:nat ==> succ(m) mod n = (if succ(m mod n) = n then 0 else succ(m mod n))"
paulson@13259
   218
apply (case_tac "n=0")
paulson@13259
   219
 apply (simp (no_asm_simp) add: natify_succ DIVISION_BY_ZERO_MOD)
paulson@13259
   220
apply (subgoal_tac "natify (succ (m)) mod n = (if succ (natify (m) mod n) = n then 0 else succ (natify (m) mod n))")
paulson@13259
   221
 prefer 2
paulson@13259
   222
 apply (subst natify_succ)
paulson@13259
   223
 apply (rule mod_succ_lemma)
paulson@13259
   224
  apply (auto simp del: natify_succ simp add: nat_into_Ord [THEN Ord_0_lt_iff])
paulson@13259
   225
done
paulson@13259
   226
paulson@13259
   227
lemma mod_less_divisor: "[| 0<n;  n:nat |] ==> m mod n < n"
paulson@13259
   228
apply (subgoal_tac "natify (m) mod n < n")
paulson@13259
   229
apply (rule_tac [2] i = "natify (m) " in complete_induct)
paulson@13259
   230
apply (case_tac [3] "x<n", auto) 
paulson@13259
   231
txt{* case n le x*}
paulson@13259
   232
apply (simp add: mod_geq not_lt_iff_le div_termination [THEN ltD])
paulson@13259
   233
done
paulson@13259
   234
paulson@13259
   235
lemma mod_1_eq [simp]: "m mod 1 = 0"
paulson@13259
   236
by (cut_tac n = "1" in mod_less_divisor, auto)
paulson@13259
   237
paulson@13259
   238
lemma mod2_cases: "b<2 ==> k mod 2 = b | k mod 2 = (if b=1 then 0 else 1)"
paulson@13259
   239
apply (subgoal_tac "k mod 2: 2")
paulson@13259
   240
 prefer 2 apply (simp add: mod_less_divisor [THEN ltD])
paulson@13259
   241
apply (drule ltD, auto)
paulson@13259
   242
done
paulson@13259
   243
paulson@13259
   244
lemma mod2_succ_succ [simp]: "succ(succ(m)) mod 2 = m mod 2"
paulson@13259
   245
apply (subgoal_tac "m mod 2: 2")
paulson@13259
   246
 prefer 2 apply (simp add: mod_less_divisor [THEN ltD])
paulson@13259
   247
apply (auto simp add: mod_succ)
paulson@13259
   248
done
paulson@13259
   249
paulson@13259
   250
lemma mod2_add_more [simp]: "(m#+m#+n) mod 2 = n mod 2"
paulson@13259
   251
apply (subgoal_tac " (natify (m) #+natify (m) #+n) mod 2 = n mod 2")
paulson@13259
   252
apply (rule_tac [2] n = "natify (m) " in nat_induct)
paulson@13259
   253
apply auto
paulson@13259
   254
done
paulson@13259
   255
paulson@13259
   256
lemma mod2_add_self [simp]: "(m#+m) mod 2 = 0"
paulson@13259
   257
by (cut_tac n = "0" in mod2_add_more, auto)
paulson@13259
   258
paulson@13259
   259
paulson@13259
   260
(**** Additional theorems about "le" ****)
paulson@13259
   261
paulson@13259
   262
lemma add_le_self: "m:nat ==> m le (m #+ n)"
paulson@13259
   263
apply (simp (no_asm_simp))
paulson@13259
   264
done
paulson@13259
   265
paulson@13259
   266
lemma add_le_self2: "m:nat ==> m le (n #+ m)"
paulson@13259
   267
apply (simp (no_asm_simp))
paulson@13259
   268
done
paulson@13259
   269
paulson@13259
   270
(*** Monotonicity of Multiplication ***)
paulson@13259
   271
paulson@13259
   272
lemma mult_le_mono1: "[| i le j; j:nat |] ==> (i#*k) le (j#*k)"
paulson@13259
   273
apply (subgoal_tac "natify (i) #*natify (k) le j#*natify (k) ")
paulson@13259
   274
apply (frule_tac [2] lt_nat_in_nat)
paulson@13259
   275
apply (rule_tac [3] n = "natify (k) " in nat_induct)
paulson@13259
   276
apply (simp_all add: add_le_mono)
paulson@13259
   277
done
paulson@13259
   278
paulson@13259
   279
(* le monotonicity, BOTH arguments*)
paulson@13259
   280
lemma mult_le_mono: "[| i le j; k le l; j:nat; l:nat |] ==> i#*k le j#*l"
paulson@13259
   281
apply (rule mult_le_mono1 [THEN le_trans], assumption+)
paulson@13259
   282
apply (subst mult_commute, subst mult_commute, rule mult_le_mono1, assumption+)
paulson@13259
   283
done
paulson@13259
   284
paulson@13259
   285
(*strict, in 1st argument; proof is by induction on k>0.
paulson@13259
   286
  I can't see how to relax the typing conditions.*)
paulson@13259
   287
lemma mult_lt_mono2: "[| i<j; 0<k; j:nat; k:nat |] ==> k#*i < k#*j"
paulson@13259
   288
apply (erule zero_lt_natE)
paulson@13259
   289
apply (frule_tac [2] lt_nat_in_nat)
paulson@13259
   290
apply (simp_all (no_asm_simp))
paulson@13259
   291
apply (induct_tac "x")
paulson@13259
   292
apply (simp_all (no_asm_simp) add: add_lt_mono)
paulson@13259
   293
done
paulson@13259
   294
paulson@13259
   295
lemma mult_lt_mono1: "[| i<j; 0<k; j:nat; k:nat |] ==> i#*k < j#*k"
paulson@13259
   296
apply (simp (no_asm_simp) add: mult_lt_mono2 mult_commute [of _ k])
paulson@13259
   297
done
paulson@13259
   298
paulson@13259
   299
lemma add_eq_0_iff [iff]: "m#+n = 0 <-> natify(m)=0 & natify(n)=0"
paulson@13259
   300
apply (subgoal_tac "natify (m) #+ natify (n) = 0 <-> natify (m) =0 & natify (n) =0")
paulson@13259
   301
apply (rule_tac [2] n = "natify (m) " in natE)
paulson@13259
   302
 apply (rule_tac [4] n = "natify (n) " in natE)
paulson@13259
   303
apply auto
paulson@13259
   304
done
paulson@13259
   305
paulson@13259
   306
lemma zero_lt_mult_iff [iff]: "0 < m#*n <-> 0 < natify(m) & 0 < natify(n)"
paulson@13259
   307
apply (subgoal_tac "0 < natify (m) #*natify (n) <-> 0 < natify (m) & 0 < natify (n) ")
paulson@13259
   308
apply (rule_tac [2] n = "natify (m) " in natE)
paulson@13259
   309
 apply (rule_tac [4] n = "natify (n) " in natE)
paulson@13259
   310
  apply (rule_tac [3] n = "natify (n) " in natE)
paulson@13259
   311
apply auto
paulson@13259
   312
done
paulson@13259
   313
paulson@13259
   314
lemma mult_eq_1_iff [iff]: "m#*n = 1 <-> natify(m)=1 & natify(n)=1"
paulson@13259
   315
apply (subgoal_tac "natify (m) #* natify (n) = 1 <-> natify (m) =1 & natify (n) =1")
paulson@13259
   316
apply (rule_tac [2] n = "natify (m) " in natE)
paulson@13259
   317
 apply (rule_tac [4] n = "natify (n) " in natE)
paulson@13259
   318
apply auto
paulson@13259
   319
done
paulson@13259
   320
paulson@13259
   321
paulson@13259
   322
lemma mult_is_zero: "[|m: nat; n: nat|] ==> (m #* n = 0) <-> (m = 0 | n = 0)"
paulson@13259
   323
apply auto
paulson@13259
   324
apply (erule natE)
paulson@13259
   325
apply (erule_tac [2] natE, auto)
paulson@13259
   326
done
paulson@13259
   327
paulson@13259
   328
lemma mult_is_zero_natify [iff]:
paulson@13259
   329
     "(m #* n = 0) <-> (natify(m) = 0 | natify(n) = 0)"
paulson@13259
   330
apply (cut_tac m = "natify (m) " and n = "natify (n) " in mult_is_zero)
paulson@13259
   331
apply auto
paulson@13259
   332
done
paulson@13259
   333
paulson@13259
   334
paulson@13259
   335
(** Cancellation laws for common factors in comparisons **)
paulson@13259
   336
paulson@13259
   337
lemma mult_less_cancel_lemma:
paulson@13259
   338
     "[| k: nat; m: nat; n: nat |] ==> (m#*k < n#*k) <-> (0<k & m<n)"
paulson@13259
   339
apply (safe intro!: mult_lt_mono1)
paulson@13259
   340
apply (erule natE, auto)
paulson@13259
   341
apply (rule not_le_iff_lt [THEN iffD1])
paulson@13259
   342
apply (drule_tac [3] not_le_iff_lt [THEN [2] rev_iffD2])
paulson@13259
   343
prefer 5 apply (blast intro: mult_le_mono1, auto)
paulson@13259
   344
done
paulson@13259
   345
paulson@13259
   346
lemma mult_less_cancel2 [simp]:
paulson@13259
   347
     "(m#*k < n#*k) <-> (0 < natify(k) & natify(m) < natify(n))"
paulson@13259
   348
apply (rule iff_trans)
paulson@13259
   349
apply (rule_tac [2] mult_less_cancel_lemma, auto)
paulson@13259
   350
done
paulson@13259
   351
paulson@13259
   352
lemma mult_less_cancel1 [simp]:
paulson@13259
   353
     "(k#*m < k#*n) <-> (0 < natify(k) & natify(m) < natify(n))"
paulson@13259
   354
apply (simp (no_asm) add: mult_less_cancel2 mult_commute [of k])
paulson@13259
   355
done
paulson@13259
   356
paulson@13259
   357
lemma mult_le_cancel2 [simp]: "(m#*k le n#*k) <-> (0 < natify(k) --> natify(m) le natify(n))"
paulson@13259
   358
apply (simp (no_asm_simp) add: not_lt_iff_le [THEN iff_sym])
paulson@13259
   359
apply auto
paulson@13259
   360
done
paulson@13259
   361
paulson@13259
   362
lemma mult_le_cancel1 [simp]: "(k#*m le k#*n) <-> (0 < natify(k) --> natify(m) le natify(n))"
paulson@13259
   363
apply (simp (no_asm_simp) add: not_lt_iff_le [THEN iff_sym])
paulson@13259
   364
apply auto
paulson@13259
   365
done
paulson@13259
   366
paulson@13259
   367
lemma mult_le_cancel_le1: "k : nat ==> k #* m le k \<longleftrightarrow> (0 < k \<longrightarrow> natify(m) le 1)"
paulson@13259
   368
by (cut_tac k = "k" and m = "m" and n = "1" in mult_le_cancel1, auto)
paulson@13259
   369
paulson@13259
   370
lemma Ord_eq_iff_le: "[| Ord(m); Ord(n) |] ==> m=n <-> (m le n & n le m)"
paulson@13259
   371
by (blast intro: le_anti_sym)
paulson@13259
   372
paulson@13259
   373
lemma mult_cancel2_lemma:
paulson@13259
   374
     "[| k: nat; m: nat; n: nat |] ==> (m#*k = n#*k) <-> (m=n | k=0)"
paulson@13259
   375
apply (simp (no_asm_simp) add: Ord_eq_iff_le [of "m#*k"] Ord_eq_iff_le [of m])
paulson@13259
   376
apply (auto simp add: Ord_0_lt_iff)
paulson@13259
   377
done
paulson@13259
   378
paulson@13259
   379
lemma mult_cancel2 [simp]:
paulson@13259
   380
     "(m#*k = n#*k) <-> (natify(m) = natify(n) | natify(k) = 0)"
paulson@13259
   381
apply (rule iff_trans)
paulson@13259
   382
apply (rule_tac [2] mult_cancel2_lemma, auto)
paulson@13259
   383
done
paulson@13259
   384
paulson@13259
   385
lemma mult_cancel1 [simp]:
paulson@13259
   386
     "(k#*m = k#*n) <-> (natify(m) = natify(n) | natify(k) = 0)"
paulson@13259
   387
apply (simp (no_asm) add: mult_cancel2 mult_commute [of k])
paulson@13259
   388
done
paulson@13259
   389
paulson@13259
   390
paulson@13259
   391
(** Cancellation law for division **)
paulson@13259
   392
paulson@13259
   393
lemma div_cancel_raw:
paulson@13259
   394
     "[| 0<n; 0<k; k:nat; m:nat; n:nat |] ==> (k#*m) div (k#*n) = m div n"
paulson@13259
   395
apply (erule_tac i = "m" in complete_induct)
paulson@13259
   396
apply (case_tac "x<n")
paulson@13259
   397
 apply (simp add: div_less zero_lt_mult_iff mult_lt_mono2)
paulson@13259
   398
apply (simp add: not_lt_iff_le zero_lt_mult_iff le_refl [THEN mult_le_mono]
paulson@13259
   399
          div_geq diff_mult_distrib2 [symmetric] div_termination [THEN ltD])
paulson@13259
   400
done
paulson@13259
   401
paulson@13259
   402
lemma div_cancel:
paulson@13259
   403
     "[| 0 < natify(n);  0 < natify(k) |] ==> (k#*m) div (k#*n) = m div n"
paulson@13259
   404
apply (cut_tac k = "natify (k) " and m = "natify (m)" and n = "natify (n)" 
paulson@13259
   405
       in div_cancel_raw)
paulson@13259
   406
apply auto
paulson@13259
   407
done
paulson@13259
   408
paulson@13259
   409
paulson@13259
   410
(** Distributive law for remainder (mod) **)
paulson@13259
   411
paulson@13259
   412
lemma mult_mod_distrib_raw:
paulson@13259
   413
     "[| k:nat; m:nat; n:nat |] ==> (k#*m) mod (k#*n) = k #* (m mod n)"
paulson@13259
   414
apply (case_tac "k=0")
paulson@13259
   415
 apply (simp add: DIVISION_BY_ZERO_MOD)
paulson@13259
   416
apply (case_tac "n=0")
paulson@13259
   417
 apply (simp add: DIVISION_BY_ZERO_MOD)
paulson@13259
   418
apply (simp add: nat_into_Ord [THEN Ord_0_lt_iff])
paulson@13259
   419
apply (erule_tac i = "m" in complete_induct)
paulson@13259
   420
apply (case_tac "x<n")
paulson@13259
   421
 apply (simp (no_asm_simp) add: mod_less zero_lt_mult_iff mult_lt_mono2)
paulson@13259
   422
apply (simp add: not_lt_iff_le zero_lt_mult_iff le_refl [THEN mult_le_mono] 
paulson@13259
   423
         mod_geq diff_mult_distrib2 [symmetric] div_termination [THEN ltD])
paulson@13259
   424
done
paulson@13259
   425
paulson@13259
   426
lemma mod_mult_distrib2: "k #* (m mod n) = (k#*m) mod (k#*n)"
paulson@13259
   427
apply (cut_tac k = "natify (k) " and m = "natify (m)" and n = "natify (n)" 
paulson@13259
   428
       in mult_mod_distrib_raw)
paulson@13259
   429
apply auto
paulson@13259
   430
done
paulson@13259
   431
paulson@13259
   432
lemma mult_mod_distrib: "(m mod n) #* k = (m#*k) mod (n#*k)"
paulson@13259
   433
apply (simp (no_asm) add: mult_commute mod_mult_distrib2)
paulson@13259
   434
done
paulson@13259
   435
paulson@13259
   436
lemma mod_add_self2_raw: "n \<in> nat ==> (m #+ n) mod n = m mod n"
paulson@13259
   437
apply (subgoal_tac " (n #+ m) mod n = (n #+ m #- n) mod n")
paulson@13259
   438
apply (simp add: add_commute) 
paulson@13259
   439
apply (subst mod_geq [symmetric], auto) 
paulson@13259
   440
done
paulson@13259
   441
paulson@13259
   442
lemma mod_add_self2 [simp]: "(m #+ n) mod n = m mod n"
paulson@13259
   443
apply (cut_tac n = "natify (n) " in mod_add_self2_raw)
paulson@13259
   444
apply auto
paulson@13259
   445
done
paulson@13259
   446
paulson@13259
   447
lemma mod_add_self1 [simp]: "(n#+m) mod n = m mod n"
paulson@13259
   448
apply (simp (no_asm_simp) add: add_commute mod_add_self2)
paulson@13259
   449
done
paulson@13259
   450
paulson@13259
   451
lemma mod_mult_self1_raw: "k \<in> nat ==> (m #+ k#*n) mod n = m mod n"
paulson@13259
   452
apply (erule nat_induct)
paulson@13259
   453
apply (simp_all (no_asm_simp) add: add_left_commute [of _ n])
paulson@13259
   454
done
paulson@13259
   455
paulson@13259
   456
lemma mod_mult_self1 [simp]: "(m #+ k#*n) mod n = m mod n"
paulson@13259
   457
apply (cut_tac k = "natify (k) " in mod_mult_self1_raw)
paulson@13259
   458
apply auto
paulson@13259
   459
done
paulson@13259
   460
paulson@13259
   461
lemma mod_mult_self2 [simp]: "(m #+ n#*k) mod n = m mod n"
paulson@13259
   462
apply (simp (no_asm) add: mult_commute mod_mult_self1)
paulson@13259
   463
done
paulson@13259
   464
paulson@13259
   465
(*Lemma for gcd*)
paulson@13259
   466
lemma mult_eq_self_implies_10: "m = m#*n ==> natify(n)=1 | m=0"
paulson@13259
   467
apply (subgoal_tac "m: nat")
paulson@13259
   468
 prefer 2 
paulson@13259
   469
 apply (erule ssubst)
paulson@13259
   470
 apply simp  
paulson@13259
   471
apply (rule disjCI)
paulson@13259
   472
apply (drule sym)
paulson@13259
   473
apply (rule Ord_linear_lt [of "natify(n)" 1])
paulson@13259
   474
apply simp_all  
paulson@13259
   475
 apply (subgoal_tac "m #* n = 0", simp) 
paulson@13259
   476
 apply (subst mult_natify2 [symmetric])
paulson@13259
   477
 apply (simp del: mult_natify2)
paulson@13259
   478
apply (drule nat_into_Ord [THEN Ord_0_lt, THEN [2] mult_lt_mono2], auto)
paulson@13259
   479
done
paulson@13259
   480
paulson@13259
   481
lemma less_imp_succ_add [rule_format]:
paulson@13259
   482
     "[| m<n; n: nat |] ==> EX k: nat. n = succ(m#+k)"
paulson@13259
   483
apply (frule lt_nat_in_nat, assumption)
paulson@13259
   484
apply (erule rev_mp)
paulson@13259
   485
apply (induct_tac "n")
paulson@13259
   486
apply (simp_all (no_asm) add: le_iff)
paulson@13259
   487
apply (blast elim!: leE intro!: add_0_right [symmetric] add_succ_right [symmetric])
paulson@13259
   488
done
paulson@13259
   489
paulson@13259
   490
lemma less_iff_succ_add:
paulson@13259
   491
     "[| m: nat; n: nat |] ==> (m<n) <-> (EX k: nat. n = succ(m#+k))"
paulson@13259
   492
by (auto intro: less_imp_succ_add)
paulson@13259
   493
paulson@13259
   494
(* on nat *)
paulson@13259
   495
paulson@13259
   496
lemma diff_is_0_lemma:
paulson@13259
   497
     "[| m: nat; n: nat |] ==> m #- n = 0 <-> m le n"
paulson@13259
   498
apply (rule_tac m = "m" and n = "n" in diff_induct, simp_all)
paulson@13259
   499
done
paulson@13259
   500
paulson@13259
   501
lemma diff_is_0_iff: "m #- n = 0 <-> natify(m) le natify(n)"
paulson@13259
   502
by (simp add: diff_is_0_lemma [symmetric])
paulson@13259
   503
paulson@13259
   504
lemma nat_lt_imp_diff_eq_0:
paulson@13259
   505
     "[| a:nat; b:nat; a<b |] ==> a #- b = 0"
paulson@13259
   506
by (simp add: diff_is_0_iff le_iff) 
paulson@13259
   507
paulson@13259
   508
lemma nat_diff_split:
paulson@13259
   509
     "[| a:nat; b:nat |] ==>  
paulson@13259
   510
      (P(a #- b)) <-> ((a < b -->P(0)) & (ALL d:nat. a = b #+ d --> P(d)))"
paulson@13259
   511
apply (case_tac "a < b")
paulson@13259
   512
 apply (force simp add: nat_lt_imp_diff_eq_0)
paulson@13259
   513
apply (rule iffI, simp_all) 
paulson@13259
   514
 apply clarify 
paulson@13259
   515
 apply (rotate_tac -1) 
paulson@13259
   516
 apply simp 
paulson@13259
   517
apply (drule_tac x="a#-b" in bspec)
paulson@13259
   518
apply (simp_all add: Ordinal.not_lt_iff_le add_diff_inverse) 
paulson@13259
   519
done
paulson@13259
   520
paulson@13259
   521
ML
paulson@13259
   522
{*
paulson@13259
   523
val diff_self_eq_0 = thm "diff_self_eq_0";
paulson@13259
   524
val add_diff_inverse = thm "add_diff_inverse";
paulson@13259
   525
val add_diff_inverse2 = thm "add_diff_inverse2";
paulson@13259
   526
val diff_succ = thm "diff_succ";
paulson@13259
   527
val zero_less_diff = thm "zero_less_diff";
paulson@13259
   528
val diff_mult_distrib = thm "diff_mult_distrib";
paulson@13259
   529
val diff_mult_distrib2 = thm "diff_mult_distrib2";
paulson@13259
   530
val div_termination = thm "div_termination";
paulson@13259
   531
val raw_mod_type = thm "raw_mod_type";
paulson@13259
   532
val mod_type = thm "mod_type";
paulson@13259
   533
val DIVISION_BY_ZERO_DIV = thm "DIVISION_BY_ZERO_DIV";
paulson@13259
   534
val DIVISION_BY_ZERO_MOD = thm "DIVISION_BY_ZERO_MOD";
paulson@13259
   535
val raw_mod_less = thm "raw_mod_less";
paulson@13259
   536
val mod_less = thm "mod_less";
paulson@13259
   537
val raw_mod_geq = thm "raw_mod_geq";
paulson@13259
   538
val mod_geq = thm "mod_geq";
paulson@13259
   539
val raw_div_type = thm "raw_div_type";
paulson@13259
   540
val div_type = thm "div_type";
paulson@13259
   541
val raw_div_less = thm "raw_div_less";
paulson@13259
   542
val div_less = thm "div_less";
paulson@13259
   543
val raw_div_geq = thm "raw_div_geq";
paulson@13259
   544
val div_geq = thm "div_geq";
paulson@13259
   545
val mod_div_equality_natify = thm "mod_div_equality_natify";
paulson@13259
   546
val mod_div_equality = thm "mod_div_equality";
paulson@13259
   547
val mod_succ = thm "mod_succ";
paulson@13259
   548
val mod_less_divisor = thm "mod_less_divisor";
paulson@13259
   549
val mod_1_eq = thm "mod_1_eq";
paulson@13259
   550
val mod2_cases = thm "mod2_cases";
paulson@13259
   551
val mod2_succ_succ = thm "mod2_succ_succ";
paulson@13259
   552
val mod2_add_more = thm "mod2_add_more";
paulson@13259
   553
val mod2_add_self = thm "mod2_add_self";
paulson@13259
   554
val add_le_self = thm "add_le_self";
paulson@13259
   555
val add_le_self2 = thm "add_le_self2";
paulson@13259
   556
val mult_le_mono1 = thm "mult_le_mono1";
paulson@13259
   557
val mult_le_mono = thm "mult_le_mono";
paulson@13259
   558
val mult_lt_mono2 = thm "mult_lt_mono2";
paulson@13259
   559
val mult_lt_mono1 = thm "mult_lt_mono1";
paulson@13259
   560
val add_eq_0_iff = thm "add_eq_0_iff";
paulson@13259
   561
val zero_lt_mult_iff = thm "zero_lt_mult_iff";
paulson@13259
   562
val mult_eq_1_iff = thm "mult_eq_1_iff";
paulson@13259
   563
val mult_is_zero = thm "mult_is_zero";
paulson@13259
   564
val mult_is_zero_natify = thm "mult_is_zero_natify";
paulson@13259
   565
val mult_less_cancel2 = thm "mult_less_cancel2";
paulson@13259
   566
val mult_less_cancel1 = thm "mult_less_cancel1";
paulson@13259
   567
val mult_le_cancel2 = thm "mult_le_cancel2";
paulson@13259
   568
val mult_le_cancel1 = thm "mult_le_cancel1";
paulson@13259
   569
val mult_le_cancel_le1 = thm "mult_le_cancel_le1";
paulson@13259
   570
val Ord_eq_iff_le = thm "Ord_eq_iff_le";
paulson@13259
   571
val mult_cancel2 = thm "mult_cancel2";
paulson@13259
   572
val mult_cancel1 = thm "mult_cancel1";
paulson@13259
   573
val div_cancel_raw = thm "div_cancel_raw";
paulson@13259
   574
val div_cancel = thm "div_cancel";
paulson@13259
   575
val mult_mod_distrib_raw = thm "mult_mod_distrib_raw";
paulson@13259
   576
val mod_mult_distrib2 = thm "mod_mult_distrib2";
paulson@13259
   577
val mult_mod_distrib = thm "mult_mod_distrib";
paulson@13259
   578
val mod_add_self2_raw = thm "mod_add_self2_raw";
paulson@13259
   579
val mod_add_self2 = thm "mod_add_self2";
paulson@13259
   580
val mod_add_self1 = thm "mod_add_self1";
paulson@13259
   581
val mod_mult_self1_raw = thm "mod_mult_self1_raw";
paulson@13259
   582
val mod_mult_self1 = thm "mod_mult_self1";
paulson@13259
   583
val mod_mult_self2 = thm "mod_mult_self2";
paulson@13259
   584
val mult_eq_self_implies_10 = thm "mult_eq_self_implies_10";
paulson@13259
   585
val less_imp_succ_add = thm "less_imp_succ_add";
paulson@13259
   586
val less_iff_succ_add = thm "less_iff_succ_add";
paulson@13259
   587
val diff_is_0_iff = thm "diff_is_0_iff";
paulson@13259
   588
val nat_lt_imp_diff_eq_0 = thm "nat_lt_imp_diff_eq_0";
paulson@13259
   589
val nat_diff_split = thm "nat_diff_split";
paulson@13259
   590
*}
paulson@13259
   591
paulson@9548
   592
end