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