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