src/HOL/NatDef.ML
author paulson
Fri Feb 20 11:07:51 1998 +0100 (1998-02-20)
changeset 4635 c448e09d0fca
parent 4614 122015efd4e1
child 4640 ac6cf9f18653
permissions -rw-r--r--
New theorem eq_imp_le
nipkow@2608
     1
(*  Title:      HOL/NatDef.ML
nipkow@2608
     2
    ID:         $Id$
nipkow@2608
     3
    Author:     Tobias Nipkow, Cambridge University Computer Laboratory
nipkow@2608
     4
    Copyright   1991  University of Cambridge
nipkow@2608
     5
*)
nipkow@2608
     6
nipkow@2608
     7
goal thy "mono(%X. {Zero_Rep} Un (Suc_Rep``X))";
nipkow@2608
     8
by (REPEAT (ares_tac [monoI, subset_refl, image_mono, Un_mono] 1));
nipkow@2608
     9
qed "Nat_fun_mono";
nipkow@2608
    10
nipkow@2608
    11
val Nat_unfold = Nat_fun_mono RS (Nat_def RS def_lfp_Tarski);
nipkow@2608
    12
nipkow@2608
    13
(* Zero is a natural number -- this also justifies the type definition*)
nipkow@2608
    14
goal thy "Zero_Rep: Nat";
nipkow@2608
    15
by (stac Nat_unfold 1);
nipkow@2608
    16
by (rtac (singletonI RS UnI1) 1);
nipkow@2608
    17
qed "Zero_RepI";
nipkow@2608
    18
nipkow@2608
    19
val prems = goal thy "i: Nat ==> Suc_Rep(i) : Nat";
nipkow@2608
    20
by (stac Nat_unfold 1);
nipkow@2608
    21
by (rtac (imageI RS UnI2) 1);
nipkow@2608
    22
by (resolve_tac prems 1);
nipkow@2608
    23
qed "Suc_RepI";
nipkow@2608
    24
nipkow@2608
    25
(*** Induction ***)
nipkow@2608
    26
nipkow@2608
    27
val major::prems = goal thy
nipkow@2608
    28
    "[| i: Nat;  P(Zero_Rep);   \
nipkow@2608
    29
\       !!j. [| j: Nat; P(j) |] ==> P(Suc_Rep(j)) |]  ==> P(i)";
nipkow@2608
    30
by (rtac ([Nat_def, Nat_fun_mono, major] MRS def_induct) 1);
wenzelm@4089
    31
by (blast_tac (claset() addIs prems) 1);
nipkow@2608
    32
qed "Nat_induct";
nipkow@2608
    33
nipkow@2608
    34
val prems = goalw thy [Zero_def,Suc_def]
nipkow@2608
    35
    "[| P(0);   \
nipkow@3040
    36
\       !!n. P(n) ==> P(Suc(n)) |]  ==> P(n)";
nipkow@2608
    37
by (rtac (Rep_Nat_inverse RS subst) 1);   (*types force good instantiation*)
nipkow@2608
    38
by (rtac (Rep_Nat RS Nat_induct) 1);
nipkow@2608
    39
by (REPEAT (ares_tac prems 1
nipkow@2608
    40
     ORELSE eresolve_tac [Abs_Nat_inverse RS subst] 1));
nipkow@2608
    41
qed "nat_induct";
nipkow@2608
    42
nipkow@2608
    43
(*Perform induction on n. *)
paulson@3563
    44
local fun raw_nat_ind_tac a i = 
paulson@3563
    45
    res_inst_tac [("n",a)] nat_induct i  THEN  rename_last_tac a [""] (i+1)
paulson@3563
    46
in
paulson@3563
    47
val nat_ind_tac = Datatype.occs_in_prems raw_nat_ind_tac
paulson@3563
    48
end;
nipkow@3040
    49
nipkow@2608
    50
(*A special form of induction for reasoning about m<n and m-n*)
nipkow@2608
    51
val prems = goal thy
nipkow@2608
    52
    "[| !!x. P x 0;  \
nipkow@2608
    53
\       !!y. P 0 (Suc y);  \
nipkow@2608
    54
\       !!x y. [| P x y |] ==> P (Suc x) (Suc y)  \
nipkow@2608
    55
\    |] ==> P m n";
nipkow@2608
    56
by (res_inst_tac [("x","m")] spec 1);
nipkow@2608
    57
by (nat_ind_tac "n" 1);
nipkow@2608
    58
by (rtac allI 2);
nipkow@2608
    59
by (nat_ind_tac "x" 2);
nipkow@2608
    60
by (REPEAT (ares_tac (prems@[allI]) 1 ORELSE etac spec 1));
nipkow@2608
    61
qed "diff_induct";
nipkow@2608
    62
nipkow@2608
    63
(*Case analysis on the natural numbers*)
nipkow@2608
    64
val prems = goal thy 
nipkow@2608
    65
    "[| n=0 ==> P;  !!x. n = Suc(x) ==> P |] ==> P";
nipkow@2608
    66
by (subgoal_tac "n=0 | (EX x. n = Suc(x))" 1);
wenzelm@4089
    67
by (fast_tac (claset() addSEs prems) 1);
nipkow@2608
    68
by (nat_ind_tac "n" 1);
nipkow@2608
    69
by (rtac (refl RS disjI1) 1);
paulson@2891
    70
by (Blast_tac 1);
nipkow@2608
    71
qed "natE";
nipkow@2608
    72
nipkow@3282
    73
nipkow@2608
    74
(*** Isomorphisms: Abs_Nat and Rep_Nat ***)
nipkow@2608
    75
nipkow@2608
    76
(*We can't take these properties as axioms, or take Abs_Nat==Inv(Rep_Nat),
nipkow@2608
    77
  since we assume the isomorphism equations will one day be given by Isabelle*)
nipkow@2608
    78
nipkow@2608
    79
goal thy "inj(Rep_Nat)";
nipkow@2608
    80
by (rtac inj_inverseI 1);
nipkow@2608
    81
by (rtac Rep_Nat_inverse 1);
nipkow@2608
    82
qed "inj_Rep_Nat";
nipkow@2608
    83
nipkow@2608
    84
goal thy "inj_onto Abs_Nat Nat";
nipkow@2608
    85
by (rtac inj_onto_inverseI 1);
nipkow@2608
    86
by (etac Abs_Nat_inverse 1);
nipkow@2608
    87
qed "inj_onto_Abs_Nat";
nipkow@2608
    88
nipkow@2608
    89
(*** Distinctness of constructors ***)
nipkow@2608
    90
nipkow@2608
    91
goalw thy [Zero_def,Suc_def] "Suc(m) ~= 0";
nipkow@2608
    92
by (rtac (inj_onto_Abs_Nat RS inj_onto_contraD) 1);
nipkow@2608
    93
by (rtac Suc_Rep_not_Zero_Rep 1);
nipkow@2608
    94
by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI, Zero_RepI] 1));
nipkow@2608
    95
qed "Suc_not_Zero";
nipkow@2608
    96
nipkow@2608
    97
bind_thm ("Zero_not_Suc", Suc_not_Zero RS not_sym);
nipkow@2608
    98
nipkow@2608
    99
AddIffs [Suc_not_Zero,Zero_not_Suc];
nipkow@2608
   100
nipkow@2608
   101
bind_thm ("Suc_neq_Zero", (Suc_not_Zero RS notE));
nipkow@2608
   102
val Zero_neq_Suc = sym RS Suc_neq_Zero;
nipkow@2608
   103
nipkow@2608
   104
(** Injectiveness of Suc **)
nipkow@2608
   105
nipkow@2608
   106
goalw thy [Suc_def] "inj(Suc)";
nipkow@2608
   107
by (rtac injI 1);
nipkow@2608
   108
by (dtac (inj_onto_Abs_Nat RS inj_ontoD) 1);
nipkow@2608
   109
by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI] 1));
nipkow@2608
   110
by (dtac (inj_Suc_Rep RS injD) 1);
nipkow@2608
   111
by (etac (inj_Rep_Nat RS injD) 1);
nipkow@2608
   112
qed "inj_Suc";
nipkow@2608
   113
nipkow@2608
   114
val Suc_inject = inj_Suc RS injD;
nipkow@2608
   115
nipkow@2608
   116
goal thy "(Suc(m)=Suc(n)) = (m=n)";
nipkow@2608
   117
by (EVERY1 [rtac iffI, etac Suc_inject, etac arg_cong]); 
nipkow@2608
   118
qed "Suc_Suc_eq";
nipkow@2608
   119
nipkow@2608
   120
AddIffs [Suc_Suc_eq];
nipkow@2608
   121
nipkow@2608
   122
goal thy "n ~= Suc(n)";
nipkow@2608
   123
by (nat_ind_tac "n" 1);
nipkow@2608
   124
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   125
qed "n_not_Suc_n";
nipkow@2608
   126
nipkow@2608
   127
bind_thm ("Suc_n_not_n", n_not_Suc_n RS not_sym);
nipkow@2608
   128
paulson@3236
   129
goal thy "!!n. n ~= 0 ==> EX m. n = Suc m";
paulson@3457
   130
by (rtac natE 1);
paulson@3236
   131
by (REPEAT (Blast_tac 1));
paulson@3236
   132
qed "not0_implies_Suc";
paulson@3236
   133
paulson@3236
   134
nipkow@2608
   135
(*** nat_case -- the selection operator for nat ***)
nipkow@2608
   136
nipkow@2608
   137
goalw thy [nat_case_def] "nat_case a f 0 = a";
oheimb@4535
   138
by (Blast_tac 1);
nipkow@2608
   139
qed "nat_case_0";
nipkow@2608
   140
nipkow@2608
   141
goalw thy [nat_case_def] "nat_case a f (Suc k) = f(k)";
oheimb@4535
   142
by (Blast_tac 1);
nipkow@2608
   143
qed "nat_case_Suc";
nipkow@2608
   144
paulson@3236
   145
goalw thy [wf_def, pred_nat_def] "wf(pred_nat)";
paulson@3718
   146
by (Clarify_tac 1);
nipkow@2608
   147
by (nat_ind_tac "x" 1);
paulson@3236
   148
by (ALLGOALS Blast_tac);
nipkow@2608
   149
qed "wf_pred_nat";
nipkow@2608
   150
nipkow@2608
   151
nipkow@2608
   152
(*** nat_rec -- by wf recursion on pred_nat ***)
nipkow@2608
   153
nipkow@2608
   154
(* The unrolling rule for nat_rec *)
nipkow@2608
   155
goal thy
nipkow@2608
   156
   "(%n. nat_rec c d n) = wfrec pred_nat (%f. nat_case ?c (%m. ?d m (f m)))";
nipkow@2608
   157
  by (simp_tac (HOL_ss addsimps [nat_rec_def]) 1);
nipkow@2608
   158
bind_thm("nat_rec_unfold", wf_pred_nat RS 
nipkow@2608
   159
                            ((result() RS eq_reflection) RS def_wfrec));
nipkow@2608
   160
nipkow@2608
   161
(*---------------------------------------------------------------------------
nipkow@2608
   162
 * Old:
nipkow@2608
   163
 * bind_thm ("nat_rec_unfold", (wf_pred_nat RS (nat_rec_def RS def_wfrec))); 
nipkow@2608
   164
 *---------------------------------------------------------------------------*)
nipkow@2608
   165
nipkow@2608
   166
(** conversion rules **)
nipkow@2608
   167
nipkow@2608
   168
goal thy "nat_rec c h 0 = c";
nipkow@2608
   169
by (rtac (nat_rec_unfold RS trans) 1);
wenzelm@4089
   170
by (simp_tac (simpset() addsimps [nat_case_0]) 1);
nipkow@2608
   171
qed "nat_rec_0";
nipkow@2608
   172
nipkow@2608
   173
goal thy "nat_rec c h (Suc n) = h n (nat_rec c h n)";
nipkow@2608
   174
by (rtac (nat_rec_unfold RS trans) 1);
wenzelm@4089
   175
by (simp_tac (simpset() addsimps [nat_case_Suc, pred_nat_def, cut_apply]) 1);
nipkow@2608
   176
qed "nat_rec_Suc";
nipkow@2608
   177
nipkow@2608
   178
(*These 2 rules ease the use of primitive recursion.  NOTE USE OF == *)
nipkow@2608
   179
val [rew] = goal thy
nipkow@2608
   180
    "[| !!n. f(n) == nat_rec c h n |] ==> f(0) = c";
nipkow@2608
   181
by (rewtac rew);
nipkow@2608
   182
by (rtac nat_rec_0 1);
nipkow@2608
   183
qed "def_nat_rec_0";
nipkow@2608
   184
nipkow@2608
   185
val [rew] = goal thy
nipkow@2608
   186
    "[| !!n. f(n) == nat_rec c h n |] ==> f(Suc(n)) = h n (f n)";
nipkow@2608
   187
by (rewtac rew);
nipkow@2608
   188
by (rtac nat_rec_Suc 1);
nipkow@2608
   189
qed "def_nat_rec_Suc";
nipkow@2608
   190
nipkow@2608
   191
fun nat_recs def =
nipkow@2608
   192
      [standard (def RS def_nat_rec_0),
nipkow@2608
   193
       standard (def RS def_nat_rec_Suc)];
nipkow@2608
   194
nipkow@2608
   195
nipkow@2608
   196
(*** Basic properties of "less than" ***)
nipkow@2608
   197
paulson@3378
   198
(*Used in TFL/post.sml*)
paulson@3378
   199
goalw thy [less_def] "(m,n) : pred_nat^+ = (m<n)";
paulson@3378
   200
by (rtac refl 1);
paulson@3378
   201
qed "less_eq";
paulson@3378
   202
nipkow@2608
   203
(** Introduction properties **)
nipkow@2608
   204
nipkow@2608
   205
val prems = goalw thy [less_def] "[| i<j;  j<k |] ==> i<(k::nat)";
nipkow@2608
   206
by (rtac (trans_trancl RS transD) 1);
nipkow@2608
   207
by (resolve_tac prems 1);
nipkow@2608
   208
by (resolve_tac prems 1);
nipkow@2608
   209
qed "less_trans";
nipkow@2608
   210
paulson@3236
   211
goalw thy [less_def, pred_nat_def] "n < Suc(n)";
wenzelm@4089
   212
by (simp_tac (simpset() addsimps [r_into_trancl]) 1);
nipkow@2608
   213
qed "lessI";
nipkow@2608
   214
AddIffs [lessI];
nipkow@2608
   215
nipkow@2608
   216
(* i<j ==> i<Suc(j) *)
nipkow@2608
   217
bind_thm("less_SucI", lessI RSN (2, less_trans));
nipkow@2608
   218
Addsimps [less_SucI];
nipkow@2608
   219
nipkow@2608
   220
goal thy "0 < Suc(n)";
nipkow@2608
   221
by (nat_ind_tac "n" 1);
nipkow@2608
   222
by (rtac lessI 1);
nipkow@2608
   223
by (etac less_trans 1);
nipkow@2608
   224
by (rtac lessI 1);
nipkow@2608
   225
qed "zero_less_Suc";
nipkow@2608
   226
AddIffs [zero_less_Suc];
nipkow@2608
   227
nipkow@2608
   228
(** Elimination properties **)
nipkow@2608
   229
nipkow@2608
   230
val prems = goalw thy [less_def] "n<m ==> ~ m<(n::nat)";
wenzelm@4089
   231
by (blast_tac (claset() addIs ([wf_pred_nat, wf_trancl RS wf_asym]@prems))1);
nipkow@2608
   232
qed "less_not_sym";
nipkow@2608
   233
nipkow@2608
   234
(* [| n<m; m<n |] ==> R *)
nipkow@2608
   235
bind_thm ("less_asym", (less_not_sym RS notE));
nipkow@2608
   236
nipkow@2608
   237
goalw thy [less_def] "~ n<(n::nat)";
nipkow@2608
   238
by (rtac notI 1);
nipkow@2608
   239
by (etac (wf_pred_nat RS wf_trancl RS wf_irrefl) 1);
nipkow@2608
   240
qed "less_not_refl";
nipkow@2608
   241
nipkow@2608
   242
(* n<n ==> R *)
nipkow@2608
   243
bind_thm ("less_irrefl", (less_not_refl RS notE));
nipkow@2608
   244
nipkow@2608
   245
goal thy "!!m. n<m ==> m ~= (n::nat)";
wenzelm@4089
   246
by (blast_tac (claset() addSEs [less_irrefl]) 1);
nipkow@2608
   247
qed "less_not_refl2";
nipkow@2608
   248
nipkow@2608
   249
paulson@3236
   250
val major::prems = goalw thy [less_def, pred_nat_def]
nipkow@2608
   251
    "[| i<k;  k=Suc(i) ==> P;  !!j. [| i<j;  k=Suc(j) |] ==> P \
nipkow@2608
   252
\    |] ==> P";
nipkow@2608
   253
by (rtac (major RS tranclE) 1);
paulson@3236
   254
by (ALLGOALS Full_simp_tac); 
nipkow@2608
   255
by (REPEAT_FIRST (bound_hyp_subst_tac ORELSE'
paulson@3236
   256
                  eresolve_tac (prems@[asm_rl, Pair_inject])));
nipkow@2608
   257
qed "lessE";
nipkow@2608
   258
nipkow@2608
   259
goal thy "~ n<0";
nipkow@2608
   260
by (rtac notI 1);
nipkow@2608
   261
by (etac lessE 1);
nipkow@2608
   262
by (etac Zero_neq_Suc 1);
nipkow@2608
   263
by (etac Zero_neq_Suc 1);
nipkow@2608
   264
qed "not_less0";
nipkow@2608
   265
nipkow@2608
   266
AddIffs [not_less0];
nipkow@2608
   267
nipkow@2608
   268
(* n<0 ==> R *)
nipkow@2608
   269
bind_thm ("less_zeroE", not_less0 RS notE);
nipkow@2608
   270
nipkow@2608
   271
val [major,less,eq] = goal thy
nipkow@2608
   272
    "[| m < Suc(n);  m<n ==> P;  m=n ==> P |] ==> P";
nipkow@2608
   273
by (rtac (major RS lessE) 1);
nipkow@2608
   274
by (rtac eq 1);
paulson@2891
   275
by (Blast_tac 1);
nipkow@2608
   276
by (rtac less 1);
paulson@2891
   277
by (Blast_tac 1);
nipkow@2608
   278
qed "less_SucE";
nipkow@2608
   279
nipkow@2608
   280
goal thy "(m < Suc(n)) = (m < n | m = n)";
wenzelm@4089
   281
by (blast_tac (claset() addSEs [less_SucE] addIs [less_trans]) 1);
nipkow@2608
   282
qed "less_Suc_eq";
nipkow@2608
   283
nipkow@3484
   284
goal thy "(n<1) = (n=0)";
wenzelm@4089
   285
by (simp_tac (simpset() addsimps [less_Suc_eq]) 1);
nipkow@3484
   286
qed "less_one";
nipkow@3484
   287
AddIffs [less_one];
nipkow@3484
   288
nipkow@2608
   289
val prems = goal thy "m<n ==> n ~= 0";
nipkow@2608
   290
by (res_inst_tac [("n","n")] natE 1);
nipkow@2608
   291
by (cut_facts_tac prems 1);
nipkow@2608
   292
by (ALLGOALS Asm_full_simp_tac);
nipkow@2608
   293
qed "gr_implies_not0";
nipkow@4356
   294
nipkow@4356
   295
goal thy "(n ~= 0) = (0 < n)";
wenzelm@4423
   296
by (rtac iffI 1);
wenzelm@4423
   297
 by (etac gr_implies_not0 2);
wenzelm@4423
   298
by (rtac natE 1);
wenzelm@4423
   299
 by (contr_tac 1);
wenzelm@4423
   300
by (etac ssubst 1);
wenzelm@4423
   301
by (rtac zero_less_Suc 1);
nipkow@4356
   302
qed "neq0_conv";
paulson@4614
   303
AddIffs [neq0_conv];
paulson@4614
   304
paulson@4635
   305
(*This theorem is useful with blast_tac: (n=0 ==> False) ==> 0<n *)
paulson@4635
   306
bind_thm ("gr0I", [neq0_conv, notI] MRS iffD1);
nipkow@2608
   307
nipkow@4356
   308
goal thy "(~(0 < n)) = (n=0)";
wenzelm@4423
   309
by (rtac iffI 1);
wenzelm@4423
   310
 by (etac swap 1);
wenzelm@4423
   311
 by (ALLGOALS Asm_full_simp_tac);
nipkow@4356
   312
qed "not_gr0";
nipkow@4356
   313
Addsimps [not_gr0];
nipkow@4356
   314
nipkow@4356
   315
goal thy "!!m. m<n ==> 0 < n";
nipkow@4356
   316
by (dtac gr_implies_not0 1);
nipkow@4356
   317
by (Asm_full_simp_tac 1);
nipkow@4356
   318
qed "gr_implies_gr0";
nipkow@4356
   319
Addsimps [gr_implies_gr0];
nipkow@4356
   320
nipkow@2608
   321
nipkow@2608
   322
(** Inductive (?) properties **)
nipkow@2608
   323
nipkow@2608
   324
val [prem] = goal thy "Suc(m) < n ==> m<n";
nipkow@2608
   325
by (rtac (prem RS rev_mp) 1);
nipkow@2608
   326
by (nat_ind_tac "n" 1);
wenzelm@4089
   327
by (ALLGOALS (fast_tac (claset() addSIs [lessI RS less_SucI]
nipkow@2608
   328
                                addEs  [less_trans, lessE])));
nipkow@2608
   329
qed "Suc_lessD";
nipkow@2608
   330
nipkow@2608
   331
val [major,minor] = goal thy 
nipkow@2608
   332
    "[| Suc(i)<k;  !!j. [| i<j;  k=Suc(j) |] ==> P \
nipkow@2608
   333
\    |] ==> P";
nipkow@2608
   334
by (rtac (major RS lessE) 1);
nipkow@2608
   335
by (etac (lessI RS minor) 1);
nipkow@2608
   336
by (etac (Suc_lessD RS minor) 1);
nipkow@2608
   337
by (assume_tac 1);
nipkow@2608
   338
qed "Suc_lessE";
nipkow@2608
   339
nipkow@2608
   340
goal thy "!!m n. Suc(m) < Suc(n) ==> m<n";
wenzelm@4089
   341
by (blast_tac (claset() addEs [lessE, make_elim Suc_lessD]) 1);
nipkow@2608
   342
qed "Suc_less_SucD";
nipkow@2608
   343
nipkow@2608
   344
goal thy "!!m n. m<n ==> Suc(m) < Suc(n)";
nipkow@2608
   345
by (etac rev_mp 1);
nipkow@2608
   346
by (nat_ind_tac "n" 1);
wenzelm@4089
   347
by (ALLGOALS (fast_tac (claset() addEs  [less_trans, lessE])));
nipkow@2608
   348
qed "Suc_mono";
nipkow@2608
   349
nipkow@2608
   350
nipkow@2608
   351
goal thy "(Suc(m) < Suc(n)) = (m<n)";
nipkow@2608
   352
by (EVERY1 [rtac iffI, etac Suc_less_SucD, etac Suc_mono]);
nipkow@2608
   353
qed "Suc_less_eq";
nipkow@2608
   354
Addsimps [Suc_less_eq];
nipkow@2608
   355
nipkow@2608
   356
goal thy "~(Suc(n) < n)";
wenzelm@4089
   357
by (blast_tac (claset() addEs [Suc_lessD RS less_irrefl]) 1);
nipkow@2608
   358
qed "not_Suc_n_less_n";
nipkow@2608
   359
Addsimps [not_Suc_n_less_n];
nipkow@2608
   360
nipkow@2608
   361
goal thy "!!i. i<j ==> j<k --> Suc i < k";
nipkow@2608
   362
by (nat_ind_tac "k" 1);
wenzelm@4089
   363
by (ALLGOALS (asm_simp_tac (simpset())));
wenzelm@4089
   364
by (asm_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
wenzelm@4089
   365
by (blast_tac (claset() addDs [Suc_lessD]) 1);
nipkow@2608
   366
qed_spec_mp "less_trans_Suc";
nipkow@2608
   367
nipkow@2608
   368
(*"Less than" is a linear ordering*)
nipkow@2608
   369
goal thy "m<n | m=n | n<(m::nat)";
nipkow@2608
   370
by (nat_ind_tac "m" 1);
nipkow@2608
   371
by (nat_ind_tac "n" 1);
nipkow@2608
   372
by (rtac (refl RS disjI1 RS disjI2) 1);
nipkow@2608
   373
by (rtac (zero_less_Suc RS disjI1) 1);
wenzelm@4089
   374
by (blast_tac (claset() addIs [Suc_mono, less_SucI] addEs [lessE]) 1);
nipkow@2608
   375
qed "less_linear";
nipkow@2608
   376
nipkow@2608
   377
qed_goal "nat_less_cases" thy 
nipkow@2608
   378
   "[| (m::nat)<n ==> P n m; m=n ==> P n m; n<m ==> P n m |] ==> P n m"
paulson@2935
   379
( fn [major,eqCase,lessCase] =>
nipkow@2608
   380
        [
paulson@2935
   381
        (rtac (less_linear RS disjE) 1),
nipkow@2608
   382
        (etac disjE 2),
paulson@2935
   383
        (etac lessCase 1),
paulson@2935
   384
        (etac (sym RS eqCase) 1),
paulson@2935
   385
        (etac major 1)
nipkow@2608
   386
        ]);
nipkow@2608
   387
nipkow@2608
   388
(*Can be used with less_Suc_eq to get n=m | n<m *)
nipkow@2608
   389
goal thy "(~ m < n) = (n < Suc(m))";
nipkow@2608
   390
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
nipkow@2608
   391
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   392
qed "not_less_eq";
nipkow@2608
   393
nipkow@2608
   394
(*Complete induction, aka course-of-values induction*)
nipkow@2608
   395
val prems = goalw thy [less_def]
nipkow@2608
   396
    "[| !!n. [| ! m::nat. m<n --> P(m) |] ==> P(n) |]  ==>  P(n)";
nipkow@2608
   397
by (wf_ind_tac "n" [wf_pred_nat RS wf_trancl] 1);
nipkow@2608
   398
by (eresolve_tac prems 1);
nipkow@2608
   399
qed "less_induct";
nipkow@2608
   400
nipkow@2608
   401
qed_goal "nat_induct2" thy 
nipkow@2608
   402
"[| P 0; P 1; !!k. P k ==> P (Suc (Suc k)) |] ==> P n" (fn prems => [
paulson@3023
   403
        cut_facts_tac prems 1,
paulson@3023
   404
        rtac less_induct 1,
paulson@3023
   405
        res_inst_tac [("n","n")] natE 1,
paulson@3023
   406
         hyp_subst_tac 1,
paulson@3023
   407
         atac 1,
paulson@3023
   408
        hyp_subst_tac 1,
paulson@3023
   409
        res_inst_tac [("n","x")] natE 1,
paulson@3023
   410
         hyp_subst_tac 1,
paulson@3023
   411
         atac 1,
paulson@3023
   412
        hyp_subst_tac 1,
paulson@3023
   413
        resolve_tac prems 1,
paulson@3023
   414
        dtac spec 1,
paulson@3023
   415
        etac mp 1,
paulson@3023
   416
        rtac (lessI RS less_trans) 1,
paulson@3023
   417
        rtac (lessI RS Suc_mono) 1]);
nipkow@2608
   418
nipkow@2608
   419
(*** Properties of <= ***)
nipkow@2608
   420
nipkow@2608
   421
goalw thy [le_def] "(m <= n) = (m < Suc n)";
nipkow@2608
   422
by (rtac not_less_eq 1);
nipkow@2608
   423
qed "le_eq_less_Suc";
nipkow@2608
   424
paulson@3343
   425
(*  m<=n ==> m < Suc n  *)
paulson@3343
   426
bind_thm ("le_imp_less_Suc", le_eq_less_Suc RS iffD1);
paulson@3343
   427
nipkow@2608
   428
goalw thy [le_def] "0 <= n";
nipkow@2608
   429
by (rtac not_less0 1);
nipkow@2608
   430
qed "le0";
nipkow@2608
   431
nipkow@2608
   432
goalw thy [le_def] "~ Suc n <= n";
nipkow@2608
   433
by (Simp_tac 1);
nipkow@2608
   434
qed "Suc_n_not_le_n";
nipkow@2608
   435
nipkow@2608
   436
goalw thy [le_def] "(i <= 0) = (i = 0)";
nipkow@2608
   437
by (nat_ind_tac "i" 1);
nipkow@2608
   438
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   439
qed "le_0_eq";
paulson@4614
   440
AddIffs [le_0_eq];
nipkow@2608
   441
nipkow@2608
   442
Addsimps [(*less_Suc_eq, makes simpset non-confluent*) le0, le_0_eq,
nipkow@2608
   443
          Suc_n_not_le_n,
nipkow@2608
   444
          n_not_Suc_n, Suc_n_not_n,
nipkow@2608
   445
          nat_case_0, nat_case_Suc, nat_rec_0, nat_rec_Suc];
nipkow@2608
   446
paulson@3355
   447
goal thy "!!m. (m <= Suc(n)) = (m<=n | m = Suc n)";
wenzelm@4089
   448
by (simp_tac (simpset() addsimps [le_eq_less_Suc]) 1);
wenzelm@4089
   449
by (blast_tac (claset() addSEs [less_SucE] addIs [less_SucI]) 1);
paulson@3355
   450
qed "le_Suc_eq";
paulson@3355
   451
paulson@4614
   452
(* [| m <= Suc n;  m <= n ==> R;  m = Suc n ==> R |] ==> R *)
paulson@4614
   453
bind_thm ("le_SucE", le_Suc_eq RS iffD1 RS disjE);
paulson@4614
   454
nipkow@2608
   455
(*
nipkow@2608
   456
goal thy "(Suc m < n | Suc m = n) = (m < n)";
nipkow@2608
   457
by (stac (less_Suc_eq RS sym) 1);
nipkow@2608
   458
by (rtac Suc_less_eq 1);
nipkow@2608
   459
qed "Suc_le_eq";
nipkow@2608
   460
nipkow@2608
   461
this could make the simpset (with less_Suc_eq added again) more confluent,
nipkow@2608
   462
but less_Suc_eq makes additional problems with terms of the form 0 < Suc (...)
nipkow@2608
   463
*)
nipkow@2608
   464
nipkow@2608
   465
(*Prevents simplification of f and g: much faster*)
nipkow@2608
   466
qed_goal "nat_case_weak_cong" thy
nipkow@2608
   467
  "m=n ==> nat_case a f m = nat_case a f n"
nipkow@2608
   468
  (fn [prem] => [rtac (prem RS arg_cong) 1]);
nipkow@2608
   469
nipkow@2608
   470
qed_goal "nat_rec_weak_cong" thy
nipkow@2608
   471
  "m=n ==> nat_rec a f m = nat_rec a f n"
nipkow@2608
   472
  (fn [prem] => [rtac (prem RS arg_cong) 1]);
nipkow@2608
   473
nipkow@2608
   474
qed_goal "expand_nat_case" thy
nipkow@2608
   475
  "P(nat_case z s n) = ((n=0 --> P z) & (!m. n = Suc m --> P(s m)))"
nipkow@2608
   476
  (fn _ => [nat_ind_tac "n" 1, ALLGOALS Asm_simp_tac]);
nipkow@2608
   477
nipkow@2608
   478
val prems = goalw thy [le_def] "~n<m ==> m<=(n::nat)";
nipkow@2608
   479
by (resolve_tac prems 1);
nipkow@2608
   480
qed "leI";
nipkow@2608
   481
nipkow@2608
   482
val prems = goalw thy [le_def] "m<=n ==> ~ n < (m::nat)";
nipkow@2608
   483
by (resolve_tac prems 1);
nipkow@2608
   484
qed "leD";
nipkow@2608
   485
nipkow@2608
   486
val leE = make_elim leD;
nipkow@2608
   487
nipkow@2608
   488
goal thy "(~n<m) = (m<=(n::nat))";
wenzelm@4089
   489
by (blast_tac (claset() addIs [leI] addEs [leE]) 1);
nipkow@2608
   490
qed "not_less_iff_le";
nipkow@2608
   491
nipkow@2608
   492
goalw thy [le_def] "!!m. ~ m <= n ==> n<(m::nat)";
paulson@2891
   493
by (Blast_tac 1);
nipkow@2608
   494
qed "not_leE";
nipkow@2608
   495
paulson@4599
   496
goalw thy [le_def] "(~n<=m) = (m<(n::nat))";
paulson@4599
   497
by (Simp_tac 1);
paulson@4599
   498
qed "not_le_iff_less";
paulson@4599
   499
nipkow@2608
   500
goalw thy [le_def] "!!m. m < n ==> Suc(m) <= n";
wenzelm@4089
   501
by (simp_tac (simpset() addsimps [less_Suc_eq]) 1);
wenzelm@4089
   502
by (blast_tac (claset() addSEs [less_irrefl,less_asym]) 1);
paulson@3343
   503
qed "Suc_leI";  (*formerly called lessD*)
nipkow@2608
   504
nipkow@2608
   505
goalw thy [le_def] "!!m. Suc(m) <= n ==> m <= n";
wenzelm@4089
   506
by (asm_full_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
nipkow@2608
   507
qed "Suc_leD";
nipkow@2608
   508
nipkow@2608
   509
(* stronger version of Suc_leD *)
nipkow@2608
   510
goalw thy [le_def] 
nipkow@2608
   511
        "!!m. Suc m <= n ==> m < n";
wenzelm@4089
   512
by (asm_full_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
nipkow@2608
   513
by (cut_facts_tac [less_linear] 1);
paulson@2891
   514
by (Blast_tac 1);
nipkow@2608
   515
qed "Suc_le_lessD";
nipkow@2608
   516
nipkow@2608
   517
goal thy "(Suc m <= n) = (m < n)";
wenzelm@4089
   518
by (blast_tac (claset() addIs [Suc_leI, Suc_le_lessD]) 1);
nipkow@2608
   519
qed "Suc_le_eq";
nipkow@2608
   520
nipkow@2608
   521
goalw thy [le_def] "!!m. m <= n ==> m <= Suc n";
wenzelm@4089
   522
by (blast_tac (claset() addDs [Suc_lessD]) 1);
nipkow@2608
   523
qed "le_SucI";
nipkow@2608
   524
Addsimps[le_SucI];
nipkow@2608
   525
nipkow@2608
   526
bind_thm ("le_Suc", not_Suc_n_less_n RS leI);
nipkow@2608
   527
nipkow@2608
   528
goalw thy [le_def] "!!m. m < n ==> m <= (n::nat)";
wenzelm@4089
   529
by (blast_tac (claset() addEs [less_asym]) 1);
nipkow@2608
   530
qed "less_imp_le";
nipkow@2608
   531
paulson@3343
   532
(** Equivalence of m<=n and  m<n | m=n **)
paulson@3343
   533
nipkow@2608
   534
goalw thy [le_def] "!!m. m <= n ==> m < n | m=(n::nat)";
nipkow@2608
   535
by (cut_facts_tac [less_linear] 1);
wenzelm@4089
   536
by (blast_tac (claset() addEs [less_irrefl,less_asym]) 1);
nipkow@2608
   537
qed "le_imp_less_or_eq";
nipkow@2608
   538
nipkow@2608
   539
goalw thy [le_def] "!!m. m<n | m=n ==> m <=(n::nat)";
nipkow@2608
   540
by (cut_facts_tac [less_linear] 1);
wenzelm@4089
   541
by (blast_tac (claset() addSEs [less_irrefl] addEs [less_asym]) 1);
nipkow@2608
   542
qed "less_or_eq_imp_le";
nipkow@2608
   543
paulson@3343
   544
goal thy "(m <= (n::nat)) = (m < n | m=n)";
nipkow@2608
   545
by (REPEAT(ares_tac [iffI,less_or_eq_imp_le,le_imp_less_or_eq] 1));
nipkow@2608
   546
qed "le_eq_less_or_eq";
nipkow@2608
   547
paulson@4635
   548
(*Useful with Blast_tac.   m=n ==> m<=n *)
paulson@4635
   549
bind_thm ("eq_imp_le", disjI2 RS less_or_eq_imp_le);
paulson@4635
   550
nipkow@2608
   551
goal thy "n <= (n::nat)";
wenzelm@4089
   552
by (simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
nipkow@2608
   553
qed "le_refl";
nipkow@2608
   554
paulson@4468
   555
goal thy "!!i. [| i <= j; j < k |] ==> i < (k::nat)";
paulson@4468
   556
by (blast_tac (claset() addSDs [le_imp_less_or_eq]
paulson@4468
   557
	                addIs [less_trans]) 1);
nipkow@2608
   558
qed "le_less_trans";
nipkow@2608
   559
nipkow@2608
   560
goal thy "!!i. [| i < j; j <= k |] ==> i < (k::nat)";
paulson@4468
   561
by (blast_tac (claset() addSDs [le_imp_less_or_eq]
paulson@4468
   562
	                addIs [less_trans]) 1);
nipkow@2608
   563
qed "less_le_trans";
nipkow@2608
   564
nipkow@2608
   565
goal thy "!!i. [| i <= j; j <= k |] ==> i <= (k::nat)";
paulson@4468
   566
by (blast_tac (claset() addSDs [le_imp_less_or_eq]
paulson@4468
   567
	                addIs [less_or_eq_imp_le, less_trans]) 1);
nipkow@2608
   568
qed "le_trans";
nipkow@2608
   569
paulson@2891
   570
goal thy "!!m. [| m <= n; n <= m |] ==> m = (n::nat)";
paulson@4468
   571
(*order_less_irrefl could make this proof fail*)
paulson@4468
   572
by (blast_tac (claset() addSDs [le_imp_less_or_eq]
paulson@4468
   573
	                addSEs [less_irrefl] addEs [less_asym]) 1);
nipkow@2608
   574
qed "le_anti_sym";
nipkow@2608
   575
nipkow@2608
   576
goal thy "(Suc(n) <= Suc(m)) = (n <= m)";
wenzelm@4089
   577
by (simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
nipkow@2608
   578
qed "Suc_le_mono";
nipkow@2608
   579
nipkow@2608
   580
AddIffs [Suc_le_mono];
nipkow@2608
   581
nipkow@2608
   582
(* Axiom 'order_le_less' of class 'order': *)
nipkow@2608
   583
goal thy "(m::nat) < n = (m <= n & m ~= n)";
paulson@3023
   584
by (rtac iffI 1);
paulson@3023
   585
 by (rtac conjI 1);
paulson@3023
   586
  by (etac less_imp_le 1);
paulson@3023
   587
 by (etac (less_not_refl2 RS not_sym) 1);
wenzelm@4089
   588
by (blast_tac (claset() addSDs [le_imp_less_or_eq]) 1);
nipkow@2608
   589
qed "nat_less_le";
nipkow@2608
   590
paulson@4599
   591
(** max **)
paulson@4599
   592
paulson@4599
   593
goalw thy [max_def] "!!z::nat. (z <= max x y) = (z <= x | z <= y)";
paulson@4599
   594
by (simp_tac (simpset() addsimps [not_le_iff_less] addsplits [expand_if]) 1);
paulson@4599
   595
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 1);
paulson@4599
   596
qed "le_max_iff_disj";
paulson@4599
   597
paulson@4599
   598
goalw thy [max_def] "!!z::nat. (max x y <= z) = (x <= z & y <= z)";
paulson@4599
   599
by (simp_tac (simpset() addsimps [not_le_iff_less] addsplits [expand_if]) 1);
paulson@4599
   600
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 1);
paulson@4599
   601
qed "max_le_iff_conj";
paulson@4599
   602
paulson@4599
   603
paulson@4599
   604
(** min **)
paulson@4599
   605
paulson@4599
   606
goalw thy [min_def] "!!z::nat. (z <= min x y) = (z <= x & z <= y)";
paulson@4599
   607
by (simp_tac (simpset() addsimps [not_le_iff_less] addsplits [expand_if]) 1);
paulson@4599
   608
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 1);
paulson@4599
   609
qed "le_min_iff_conj";
paulson@4599
   610
paulson@4599
   611
goalw thy [min_def] "!!z::nat. (min x y <= z) = (x <= z | y <= z)";
paulson@4599
   612
by (simp_tac (simpset() addsimps [not_le_iff_less] addsplits [expand_if]) 1);
paulson@4599
   613
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 1);
paulson@4599
   614
qed "min_le_iff_disj";
paulson@4599
   615
paulson@4599
   616
nipkow@2608
   617
(** LEAST -- the least number operator **)
nipkow@2608
   618
nipkow@3143
   619
goal thy "(! m::nat. P m --> n <= m) = (! m. m < n --> ~ P m)";
wenzelm@4089
   620
by (blast_tac (claset() addIs [leI] addEs [leE]) 1);
nipkow@3143
   621
val lemma = result();
nipkow@3143
   622
nipkow@3143
   623
(* This is an old def of Least for nat, which is derived for compatibility *)
nipkow@3143
   624
goalw thy [Least_def]
nipkow@3143
   625
  "(LEAST n::nat. P n) == (@n. P(n) & (ALL m. m < n --> ~P(m)))";
wenzelm@4089
   626
by (simp_tac (simpset() addsimps [lemma]) 1);
paulson@3457
   627
by (rtac eq_reflection 1);
paulson@3457
   628
by (rtac refl 1);
nipkow@3143
   629
qed "Least_nat_def";
nipkow@3143
   630
nipkow@3143
   631
val [prem1,prem2] = goalw thy [Least_nat_def]
wenzelm@3842
   632
    "[| P(k::nat);  !!x. x<k ==> ~P(x) |] ==> (LEAST x. P(x)) = k";
nipkow@2608
   633
by (rtac select_equality 1);
wenzelm@4089
   634
by (blast_tac (claset() addSIs [prem1,prem2]) 1);
nipkow@2608
   635
by (cut_facts_tac [less_linear] 1);
wenzelm@4089
   636
by (blast_tac (claset() addSIs [prem1] addSDs [prem2]) 1);
nipkow@2608
   637
qed "Least_equality";
nipkow@2608
   638
wenzelm@3842
   639
val [prem] = goal thy "P(k::nat) ==> P(LEAST x. P(x))";
nipkow@2608
   640
by (rtac (prem RS rev_mp) 1);
nipkow@2608
   641
by (res_inst_tac [("n","k")] less_induct 1);
nipkow@2608
   642
by (rtac impI 1);
nipkow@2608
   643
by (rtac classical 1);
nipkow@2608
   644
by (res_inst_tac [("s","n")] (Least_equality RS ssubst) 1);
nipkow@2608
   645
by (assume_tac 1);
nipkow@2608
   646
by (assume_tac 2);
paulson@2891
   647
by (Blast_tac 1);
nipkow@2608
   648
qed "LeastI";
nipkow@2608
   649
nipkow@2608
   650
(*Proof is almost identical to the one above!*)
wenzelm@3842
   651
val [prem] = goal thy "P(k::nat) ==> (LEAST x. P(x)) <= k";
nipkow@2608
   652
by (rtac (prem RS rev_mp) 1);
nipkow@2608
   653
by (res_inst_tac [("n","k")] less_induct 1);
nipkow@2608
   654
by (rtac impI 1);
nipkow@2608
   655
by (rtac classical 1);
nipkow@2608
   656
by (res_inst_tac [("s","n")] (Least_equality RS ssubst) 1);
nipkow@2608
   657
by (assume_tac 1);
nipkow@2608
   658
by (rtac le_refl 2);
wenzelm@4089
   659
by (blast_tac (claset() addIs [less_imp_le,le_trans]) 1);
nipkow@2608
   660
qed "Least_le";
nipkow@2608
   661
wenzelm@3842
   662
val [prem] = goal thy "k < (LEAST x. P(x)) ==> ~P(k::nat)";
nipkow@2608
   663
by (rtac notI 1);
nipkow@2608
   664
by (etac (rewrite_rule [le_def] Least_le RS notE) 1);
nipkow@2608
   665
by (rtac prem 1);
nipkow@2608
   666
qed "not_less_Least";
nipkow@2608
   667
nipkow@3143
   668
qed_goalw "Least_Suc" thy [Least_nat_def]
nipkow@2608
   669
 "!!P. [| ? n. P(Suc n); ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))"
nipkow@2608
   670
 (fn _ => [
nipkow@2608
   671
        rtac select_equality 1,
nipkow@3143
   672
        fold_goals_tac [Least_nat_def],
wenzelm@4089
   673
        safe_tac (claset() addSEs [LeastI]),
nipkow@2608
   674
        rename_tac "j" 1,
nipkow@2608
   675
        res_inst_tac [("n","j")] natE 1,
paulson@2891
   676
        Blast_tac 1,
wenzelm@4089
   677
        blast_tac (claset() addDs [Suc_less_SucD, not_less_Least]) 1,
nipkow@2608
   678
        rename_tac "k n" 1,
nipkow@2608
   679
        res_inst_tac [("n","k")] natE 1,
paulson@2891
   680
        Blast_tac 1,
nipkow@2608
   681
        hyp_subst_tac 1,
nipkow@3143
   682
        rewtac Least_nat_def,
nipkow@2608
   683
        rtac (select_equality RS arg_cong RS sym) 1,
paulson@4153
   684
        Safe_tac,
nipkow@2608
   685
        dtac Suc_mono 1,
paulson@2891
   686
        Blast_tac 1,
nipkow@2608
   687
        cut_facts_tac [less_linear] 1,
paulson@4153
   688
        Safe_tac,
nipkow@2608
   689
        atac 2,
paulson@2891
   690
        Blast_tac 2,
nipkow@2608
   691
        dtac Suc_mono 1,
paulson@2891
   692
        Blast_tac 1]);
nipkow@2608
   693
nipkow@2608
   694
nipkow@2608
   695
(*** Instantiation of transitivity prover ***)
nipkow@2608
   696
nipkow@2608
   697
structure Less_Arith =
nipkow@2608
   698
struct
nipkow@2608
   699
val nat_leI = leI;
nipkow@2608
   700
val nat_leD = leD;
nipkow@2608
   701
val lessI = lessI;
nipkow@2608
   702
val zero_less_Suc = zero_less_Suc;
nipkow@2608
   703
val less_reflE = less_irrefl;
nipkow@2608
   704
val less_zeroE = less_zeroE;
nipkow@2608
   705
val less_incr = Suc_mono;
nipkow@2608
   706
val less_decr = Suc_less_SucD;
nipkow@2608
   707
val less_incr_rhs = Suc_mono RS Suc_lessD;
nipkow@2608
   708
val less_decr_lhs = Suc_lessD;
nipkow@2608
   709
val less_trans_Suc = less_trans_Suc;
paulson@3343
   710
val leI = Suc_leI RS (Suc_le_mono RS iffD1);
nipkow@2608
   711
val not_lessI = leI RS leD
nipkow@2608
   712
val not_leI = prove_goal thy "!!m::nat. n < m ==> ~ m <= n"
nipkow@2608
   713
  (fn _ => [etac swap2 1, etac leD 1]);
nipkow@2608
   714
val eqI = prove_goal thy "!!m. [| m < Suc n; n < Suc m |] ==> m=n"
nipkow@2608
   715
  (fn _ => [etac less_SucE 1,
wenzelm@4089
   716
            blast_tac (claset() addSDs [Suc_less_SucD] addSEs [less_irrefl]
paulson@2891
   717
                              addDs [less_trans_Suc]) 1,
paulson@2935
   718
            assume_tac 1]);
nipkow@2608
   719
val leD = le_eq_less_Suc RS iffD1;
nipkow@2608
   720
val not_lessD = nat_leI RS leD;
nipkow@2608
   721
val not_leD = not_leE
nipkow@2608
   722
val eqD1 = prove_goal thy  "!!n. m = n ==> m < Suc n"
nipkow@2608
   723
 (fn _ => [etac subst 1, rtac lessI 1]);
nipkow@2608
   724
val eqD2 = sym RS eqD1;
nipkow@2608
   725
nipkow@2608
   726
fun is_zero(t) =  t = Const("0",Type("nat",[]));
nipkow@2608
   727
nipkow@2608
   728
fun nnb T = T = Type("fun",[Type("nat",[]),
nipkow@2608
   729
                            Type("fun",[Type("nat",[]),
nipkow@2608
   730
                                        Type("bool",[])])])
nipkow@2608
   731
nipkow@2608
   732
fun decomp_Suc(Const("Suc",_)$t) = let val (a,i) = decomp_Suc t in (a,i+1) end
nipkow@2608
   733
  | decomp_Suc t = (t,0);
nipkow@2608
   734
nipkow@2608
   735
fun decomp2(rel,T,lhs,rhs) =
nipkow@2608
   736
  if not(nnb T) then None else
nipkow@2608
   737
  let val (x,i) = decomp_Suc lhs
nipkow@2608
   738
      val (y,j) = decomp_Suc rhs
nipkow@2608
   739
  in case rel of
nipkow@2608
   740
       "op <"  => Some(x,i,"<",y,j)
nipkow@2608
   741
     | "op <=" => Some(x,i,"<=",y,j)
nipkow@2608
   742
     | "op ="  => Some(x,i,"=",y,j)
nipkow@2608
   743
     | _       => None
nipkow@2608
   744
  end;
nipkow@2608
   745
nipkow@2608
   746
fun negate(Some(x,i,rel,y,j)) = Some(x,i,"~"^rel,y,j)
nipkow@2608
   747
  | negate None = None;
nipkow@2608
   748
nipkow@2608
   749
fun decomp(_$(Const(rel,T)$lhs$rhs)) = decomp2(rel,T,lhs,rhs)
paulson@2718
   750
  | decomp(_$(Const("Not",_)$(Const(rel,T)$lhs$rhs))) =
nipkow@2608
   751
      negate(decomp2(rel,T,lhs,rhs))
nipkow@2608
   752
  | decomp _ = None
nipkow@2608
   753
nipkow@2608
   754
end;
nipkow@2608
   755
nipkow@2608
   756
structure Trans_Tac = Trans_Tac_Fun(Less_Arith);
nipkow@2608
   757
nipkow@2608
   758
open Trans_Tac;
nipkow@2608
   759
nipkow@2608
   760
(*** eliminates ~= in premises, which trans_tac cannot deal with ***)
nipkow@2608
   761
qed_goal "nat_neqE" thy
nipkow@2608
   762
  "[| (m::nat) ~= n; m < n ==> P; n < m ==> P |] ==> P"
nipkow@2608
   763
  (fn major::prems => [cut_facts_tac [less_linear] 1,
nipkow@2608
   764
                       REPEAT(eresolve_tac ([disjE,major RS notE]@prems) 1)]);
pusch@2680
   765
pusch@2680
   766
pusch@2680
   767
pusch@2680
   768
(* add function nat_add_primrec *) 
nipkow@4032
   769
val (_, nat_add_primrec, _, _) = Datatype.add_datatype
nipkow@3308
   770
([], "nat", [("0", [], Mixfix ("0", [], max_pri)), ("Suc", [dtTyp ([],
wenzelm@3768
   771
"nat")], NoSyn)]) (Theory.add_name "Arith" HOL.thy);
wenzelm@3768
   772
(*pretend Arith is part of the basic theory to fool package*)