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