src/HOL/Nat.ML
author clasohm
Tue Jan 30 15:24:36 1996 +0100 (1996-01-30)
changeset 1465 5d7a7e439cec
parent 1327 6c29cfab679c
child 1475 7f5a4cd08209
permissions -rw-r--r--
expanded tabs
     1 (*  Title:      HOL/nat
     2     ID:         $Id$
     3     Author:     Tobias Nipkow, Cambridge University Computer Laboratory
     4     Copyright   1991  University of Cambridge
     5 
     6 For nat.thy.  Type nat is defined as a set (Nat) over the type ind.
     7 *)
     8 
     9 open Nat;
    10 
    11 goal Nat.thy "mono(%X. {Zero_Rep} Un (Suc_Rep``X))";
    12 by (REPEAT (ares_tac [monoI, subset_refl, image_mono, Un_mono] 1));
    13 qed "Nat_fun_mono";
    14 
    15 val Nat_unfold = Nat_fun_mono RS (Nat_def RS def_lfp_Tarski);
    16 
    17 (* Zero is a natural number -- this also justifies the type definition*)
    18 goal Nat.thy "Zero_Rep: Nat";
    19 by (rtac (Nat_unfold RS ssubst) 1);
    20 by (rtac (singletonI RS UnI1) 1);
    21 qed "Zero_RepI";
    22 
    23 val prems = goal Nat.thy "i: Nat ==> Suc_Rep(i) : Nat";
    24 by (rtac (Nat_unfold RS ssubst) 1);
    25 by (rtac (imageI RS UnI2) 1);
    26 by (resolve_tac prems 1);
    27 qed "Suc_RepI";
    28 
    29 (*** Induction ***)
    30 
    31 val major::prems = goal Nat.thy
    32     "[| i: Nat;  P(Zero_Rep);   \
    33 \       !!j. [| j: Nat; P(j) |] ==> P(Suc_Rep(j)) |]  ==> P(i)";
    34 by (rtac ([Nat_def, Nat_fun_mono, major] MRS def_induct) 1);
    35 by (fast_tac (set_cs addIs prems) 1);
    36 qed "Nat_induct";
    37 
    38 val prems = goalw Nat.thy [Zero_def,Suc_def]
    39     "[| P(0);   \
    40 \       !!k. P(k) ==> P(Suc(k)) |]  ==> P(n)";
    41 by (rtac (Rep_Nat_inverse RS subst) 1);   (*types force good instantiation*)
    42 by (rtac (Rep_Nat RS Nat_induct) 1);
    43 by (REPEAT (ares_tac prems 1
    44      ORELSE eresolve_tac [Abs_Nat_inverse RS subst] 1));
    45 qed "nat_induct";
    46 
    47 (*Perform induction on n. *)
    48 fun nat_ind_tac a i = 
    49     EVERY [res_inst_tac [("n",a)] nat_induct i,
    50            rename_last_tac a ["1"] (i+1)];
    51 
    52 (*A special form of induction for reasoning about m<n and m-n*)
    53 val prems = goal Nat.thy
    54     "[| !!x. P x 0;  \
    55 \       !!y. P 0 (Suc y);  \
    56 \       !!x y. [| P x y |] ==> P (Suc x) (Suc y)  \
    57 \    |] ==> P m n";
    58 by (res_inst_tac [("x","m")] spec 1);
    59 by (nat_ind_tac "n" 1);
    60 by (rtac allI 2);
    61 by (nat_ind_tac "x" 2);
    62 by (REPEAT (ares_tac (prems@[allI]) 1 ORELSE etac spec 1));
    63 qed "diff_induct";
    64 
    65 (*Case analysis on the natural numbers*)
    66 val prems = goal Nat.thy 
    67     "[| n=0 ==> P;  !!x. n = Suc(x) ==> P |] ==> P";
    68 by (subgoal_tac "n=0 | (EX x. n = Suc(x))" 1);
    69 by (fast_tac (HOL_cs addSEs prems) 1);
    70 by (nat_ind_tac "n" 1);
    71 by (rtac (refl RS disjI1) 1);
    72 by (fast_tac HOL_cs 1);
    73 qed "natE";
    74 
    75 (*** Isomorphisms: Abs_Nat and Rep_Nat ***)
    76 
    77 (*We can't take these properties as axioms, or take Abs_Nat==Inv(Rep_Nat),
    78   since we assume the isomorphism equations will one day be given by Isabelle*)
    79 
    80 goal Nat.thy "inj(Rep_Nat)";
    81 by (rtac inj_inverseI 1);
    82 by (rtac Rep_Nat_inverse 1);
    83 qed "inj_Rep_Nat";
    84 
    85 goal Nat.thy "inj_onto Abs_Nat Nat";
    86 by (rtac inj_onto_inverseI 1);
    87 by (etac Abs_Nat_inverse 1);
    88 qed "inj_onto_Abs_Nat";
    89 
    90 (*** Distinctness of constructors ***)
    91 
    92 goalw Nat.thy [Zero_def,Suc_def] "Suc(m) ~= 0";
    93 by (rtac (inj_onto_Abs_Nat RS inj_onto_contraD) 1);
    94 by (rtac Suc_Rep_not_Zero_Rep 1);
    95 by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI, Zero_RepI] 1));
    96 qed "Suc_not_Zero";
    97 
    98 bind_thm ("Zero_not_Suc", (Suc_not_Zero RS not_sym));
    99 
   100 Addsimps [Suc_not_Zero,Zero_not_Suc];
   101 
   102 bind_thm ("Suc_neq_Zero", (Suc_not_Zero RS notE));
   103 val Zero_neq_Suc = sym RS Suc_neq_Zero;
   104 
   105 (** Injectiveness of Suc **)
   106 
   107 goalw Nat.thy [Suc_def] "inj(Suc)";
   108 by (rtac injI 1);
   109 by (dtac (inj_onto_Abs_Nat RS inj_ontoD) 1);
   110 by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI] 1));
   111 by (dtac (inj_Suc_Rep RS injD) 1);
   112 by (etac (inj_Rep_Nat RS injD) 1);
   113 qed "inj_Suc";
   114 
   115 val Suc_inject = inj_Suc RS injD;
   116 
   117 goal Nat.thy "(Suc(m)=Suc(n)) = (m=n)";
   118 by (EVERY1 [rtac iffI, etac Suc_inject, etac arg_cong]); 
   119 qed "Suc_Suc_eq";
   120 
   121 goal Nat.thy "n ~= Suc(n)";
   122 by (nat_ind_tac "n" 1);
   123 by (ALLGOALS(asm_simp_tac (!simpset addsimps [Suc_Suc_eq])));
   124 qed "n_not_Suc_n";
   125 
   126 val Suc_n_not_n = n_not_Suc_n RS not_sym;
   127 
   128 (*** nat_case -- the selection operator for nat ***)
   129 
   130 goalw Nat.thy [nat_case_def] "nat_case a f 0 = a";
   131 by (fast_tac (set_cs addIs [select_equality] addEs [Zero_neq_Suc]) 1);
   132 qed "nat_case_0";
   133 
   134 goalw Nat.thy [nat_case_def] "nat_case a f (Suc k) = f(k)";
   135 by (fast_tac (set_cs addIs [select_equality] 
   136                        addEs [make_elim Suc_inject, Suc_neq_Zero]) 1);
   137 qed "nat_case_Suc";
   138 
   139 (** Introduction rules for 'pred_nat' **)
   140 
   141 goalw Nat.thy [pred_nat_def] "(n, Suc(n)) : pred_nat";
   142 by (fast_tac set_cs 1);
   143 qed "pred_natI";
   144 
   145 val major::prems = goalw Nat.thy [pred_nat_def]
   146     "[| p : pred_nat;  !!x n. [| p = (n, Suc(n)) |] ==> R \
   147 \    |] ==> R";
   148 by (rtac (major RS CollectE) 1);
   149 by (REPEAT (eresolve_tac ([asm_rl,exE]@prems) 1));
   150 qed "pred_natE";
   151 
   152 goalw Nat.thy [wf_def] "wf(pred_nat)";
   153 by (strip_tac 1);
   154 by (nat_ind_tac "x" 1);
   155 by (fast_tac (HOL_cs addSEs [mp, pred_natE, Pair_inject, 
   156                              make_elim Suc_inject]) 2);
   157 by (fast_tac (HOL_cs addSEs [mp, pred_natE, Pair_inject, Zero_neq_Suc]) 1);
   158 qed "wf_pred_nat";
   159 
   160 
   161 (*** nat_rec -- by wf recursion on pred_nat ***)
   162 
   163 bind_thm ("nat_rec_unfold", (wf_pred_nat RS (nat_rec_def RS def_wfrec)));
   164 
   165 (** conversion rules **)
   166 
   167 goal Nat.thy "nat_rec 0 c h = c";
   168 by (rtac (nat_rec_unfold RS trans) 1);
   169 by (simp_tac (!simpset addsimps [nat_case_0]) 1);
   170 qed "nat_rec_0";
   171 
   172 goal Nat.thy "nat_rec (Suc n) c h = h n (nat_rec n c h)";
   173 by (rtac (nat_rec_unfold RS trans) 1);
   174 by (simp_tac (!simpset addsimps [nat_case_Suc, pred_natI, cut_apply]) 1);
   175 qed "nat_rec_Suc";
   176 
   177 (*These 2 rules ease the use of primitive recursion.  NOTE USE OF == *)
   178 val [rew] = goal Nat.thy
   179     "[| !!n. f(n) == nat_rec n c h |] ==> f(0) = c";
   180 by (rewtac rew);
   181 by (rtac nat_rec_0 1);
   182 qed "def_nat_rec_0";
   183 
   184 val [rew] = goal Nat.thy
   185     "[| !!n. f(n) == nat_rec n c h |] ==> f(Suc(n)) = h n (f n)";
   186 by (rewtac rew);
   187 by (rtac nat_rec_Suc 1);
   188 qed "def_nat_rec_Suc";
   189 
   190 fun nat_recs def =
   191       [standard (def RS def_nat_rec_0),
   192        standard (def RS def_nat_rec_Suc)];
   193 
   194 
   195 (*** Basic properties of "less than" ***)
   196 
   197 (** Introduction properties **)
   198 
   199 val prems = goalw Nat.thy [less_def] "[| i<j;  j<k |] ==> i<(k::nat)";
   200 by (rtac (trans_trancl RS transD) 1);
   201 by (resolve_tac prems 1);
   202 by (resolve_tac prems 1);
   203 qed "less_trans";
   204 
   205 goalw Nat.thy [less_def] "n < Suc(n)";
   206 by (rtac (pred_natI RS r_into_trancl) 1);
   207 qed "lessI";
   208 Addsimps [lessI];
   209 
   210 (* i(j ==> i(Suc(j) *)
   211 val less_SucI = lessI RSN (2, less_trans);
   212 
   213 goal Nat.thy "0 < Suc(n)";
   214 by (nat_ind_tac "n" 1);
   215 by (rtac lessI 1);
   216 by (etac less_trans 1);
   217 by (rtac lessI 1);
   218 qed "zero_less_Suc";
   219 Addsimps [zero_less_Suc];
   220 
   221 (** Elimination properties **)
   222 
   223 val prems = goalw Nat.thy [less_def] "n<m ==> ~ m<(n::nat)";
   224 by(fast_tac (HOL_cs addIs ([wf_pred_nat, wf_trancl RS wf_asym]@prems))1);
   225 qed "less_not_sym";
   226 
   227 (* [| n(m; m(n |] ==> R *)
   228 bind_thm ("less_asym", (less_not_sym RS notE));
   229 
   230 goalw Nat.thy [less_def] "~ n<(n::nat)";
   231 by (rtac notI 1);
   232 by (etac (wf_pred_nat RS wf_trancl RS wf_anti_refl) 1);
   233 qed "less_not_refl";
   234 
   235 (* n(n ==> R *)
   236 bind_thm ("less_anti_refl", (less_not_refl RS notE));
   237 
   238 goal Nat.thy "!!m. n<m ==> m ~= (n::nat)";
   239 by(fast_tac (HOL_cs addEs [less_anti_refl]) 1);
   240 qed "less_not_refl2";
   241 
   242 
   243 val major::prems = goalw Nat.thy [less_def]
   244     "[| i<k;  k=Suc(i) ==> P;  !!j. [| i<j;  k=Suc(j) |] ==> P \
   245 \    |] ==> P";
   246 by (rtac (major RS tranclE) 1);
   247 by (REPEAT_FIRST (bound_hyp_subst_tac ORELSE'
   248                   eresolve_tac (prems@[pred_natE, Pair_inject])));
   249 by (rtac refl 1);
   250 qed "lessE";
   251 
   252 goal Nat.thy "~ n<0";
   253 by (rtac notI 1);
   254 by (etac lessE 1);
   255 by (etac Zero_neq_Suc 1);
   256 by (etac Zero_neq_Suc 1);
   257 qed "not_less0";
   258 Addsimps [not_less0];
   259 
   260 (* n<0 ==> R *)
   261 bind_thm ("less_zeroE", (not_less0 RS notE));
   262 
   263 val [major,less,eq] = goal Nat.thy
   264     "[| m < Suc(n);  m<n ==> P;  m=n ==> P |] ==> P";
   265 by (rtac (major RS lessE) 1);
   266 by (rtac eq 1);
   267 by (fast_tac (HOL_cs addSDs [Suc_inject]) 1);
   268 by (rtac less 1);
   269 by (fast_tac (HOL_cs addSDs [Suc_inject]) 1);
   270 qed "less_SucE";
   271 
   272 goal Nat.thy "(m < Suc(n)) = (m < n | m = n)";
   273 by (fast_tac (HOL_cs addSIs [lessI]
   274                      addEs  [less_trans, less_SucE]) 1);
   275 qed "less_Suc_eq";
   276 
   277 val prems = goal Nat.thy "m<n ==> n ~= 0";
   278 by(res_inst_tac [("n","n")] natE 1);
   279 by(cut_facts_tac prems 1);
   280 by(ALLGOALS Asm_full_simp_tac);
   281 qed "gr_implies_not0";
   282 Addsimps [gr_implies_not0];
   283 
   284 (** Inductive (?) properties **)
   285 
   286 val [prem] = goal Nat.thy "Suc(m) < n ==> m<n";
   287 by (rtac (prem RS rev_mp) 1);
   288 by (nat_ind_tac "n" 1);
   289 by (rtac impI 1);
   290 by (etac less_zeroE 1);
   291 by (fast_tac (HOL_cs addSIs [lessI RS less_SucI]
   292                      addSDs [Suc_inject]
   293                      addEs  [less_trans, lessE]) 1);
   294 qed "Suc_lessD";
   295 
   296 val [major,minor] = goal Nat.thy 
   297     "[| Suc(i)<k;  !!j. [| i<j;  k=Suc(j) |] ==> P \
   298 \    |] ==> P";
   299 by (rtac (major RS lessE) 1);
   300 by (etac (lessI RS minor) 1);
   301 by (etac (Suc_lessD RS minor) 1);
   302 by (assume_tac 1);
   303 qed "Suc_lessE";
   304 
   305 val [major] = goal Nat.thy "Suc(m) < Suc(n) ==> m<n";
   306 by (rtac (major RS lessE) 1);
   307 by (REPEAT (rtac lessI 1
   308      ORELSE eresolve_tac [make_elim Suc_inject, ssubst, Suc_lessD] 1));
   309 qed "Suc_less_SucD";
   310 
   311 val prems = goal Nat.thy "m<n ==> Suc(m) < Suc(n)";
   312 by (subgoal_tac "m<n --> Suc(m) < Suc(n)" 1);
   313 by (fast_tac (HOL_cs addIs prems) 1);
   314 by (nat_ind_tac "n" 1);
   315 by (rtac impI 1);
   316 by (etac less_zeroE 1);
   317 by (fast_tac (HOL_cs addSIs [lessI]
   318                      addSDs [Suc_inject]
   319                      addEs  [less_trans, lessE]) 1);
   320 qed "Suc_mono";
   321 
   322 goal Nat.thy "(Suc(m) < Suc(n)) = (m<n)";
   323 by (EVERY1 [rtac iffI, etac Suc_less_SucD, etac Suc_mono]);
   324 qed "Suc_less_eq";
   325 Addsimps [Suc_less_eq];
   326 
   327 goal Nat.thy "~(Suc(n) < n)";
   328 by(fast_tac (HOL_cs addEs [Suc_lessD RS less_anti_refl]) 1);
   329 qed "not_Suc_n_less_n";
   330 Addsimps [not_Suc_n_less_n];
   331 
   332 goal Nat.thy "!!i. i<j ==> j<k --> Suc i < k";
   333 by(nat_ind_tac "k" 1);
   334 by(ALLGOALS (asm_simp_tac (!simpset addsimps [less_Suc_eq])));
   335 by(fast_tac (HOL_cs addDs [Suc_lessD]) 1);
   336 bind_thm("less_trans_Suc",result() RS mp);
   337 
   338 (*"Less than" is a linear ordering*)
   339 goal Nat.thy "m<n | m=n | n<(m::nat)";
   340 by (nat_ind_tac "m" 1);
   341 by (nat_ind_tac "n" 1);
   342 by (rtac (refl RS disjI1 RS disjI2) 1);
   343 by (rtac (zero_less_Suc RS disjI1) 1);
   344 by (fast_tac (HOL_cs addIs [lessI, Suc_mono, less_SucI] addEs [lessE]) 1);
   345 qed "less_linear";
   346 
   347 (*Can be used with less_Suc_eq to get n=m | n<m *)
   348 goal Nat.thy "(~ m < n) = (n < Suc(m))";
   349 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   350 by(ALLGOALS Asm_simp_tac);
   351 qed "not_less_eq";
   352 
   353 (*Complete induction, aka course-of-values induction*)
   354 val prems = goalw Nat.thy [less_def]
   355     "[| !!n. [| ! m::nat. m<n --> P(m) |] ==> P(n) |]  ==>  P(n)";
   356 by (wf_ind_tac "n" [wf_pred_nat RS wf_trancl] 1);
   357 by (eresolve_tac prems 1);
   358 qed "less_induct";
   359 
   360 
   361 (*** Properties of <= ***)
   362 
   363 goalw Nat.thy [le_def] "0 <= n";
   364 by (rtac not_less0 1);
   365 qed "le0";
   366 
   367 goalw Nat.thy [le_def] "~ Suc n <= n";
   368 by(Simp_tac 1);
   369 qed "Suc_n_not_le_n";
   370 
   371 goalw Nat.thy [le_def] "(i <= 0) = (i = 0)";
   372 by(nat_ind_tac "i" 1);
   373 by(ALLGOALS Asm_simp_tac);
   374 qed "le_0";
   375 
   376 Addsimps [less_not_refl,
   377           less_Suc_eq, le0, le_0,
   378           Suc_Suc_eq, Suc_n_not_le_n,
   379           n_not_Suc_n, Suc_n_not_n,
   380           nat_case_0, nat_case_Suc, nat_rec_0, nat_rec_Suc];
   381 
   382 (*Prevents simplification of f and g: much faster*)
   383 qed_goal "nat_case_weak_cong" Nat.thy
   384   "m=n ==> nat_case a f m = nat_case a f n"
   385   (fn [prem] => [rtac (prem RS arg_cong) 1]);
   386 
   387 qed_goal "nat_rec_weak_cong" Nat.thy
   388   "m=n ==> nat_rec m a f = nat_rec n a f"
   389   (fn [prem] => [rtac (prem RS arg_cong) 1]);
   390 
   391 val prems = goalw Nat.thy [le_def] "~(n<m) ==> m<=(n::nat)";
   392 by (resolve_tac prems 1);
   393 qed "leI";
   394 
   395 val prems = goalw Nat.thy [le_def] "m<=n ==> ~(n<(m::nat))";
   396 by (resolve_tac prems 1);
   397 qed "leD";
   398 
   399 val leE = make_elim leD;
   400 
   401 goalw Nat.thy [le_def] "!!m. ~ m <= n ==> n<(m::nat)";
   402 by (fast_tac HOL_cs 1);
   403 qed "not_leE";
   404 
   405 goalw Nat.thy [le_def] "!!m. m < n ==> Suc(m) <= n";
   406 by(Simp_tac 1);
   407 by (fast_tac (HOL_cs addEs [less_anti_refl,less_asym]) 1);
   408 qed "lessD";
   409 
   410 goalw Nat.thy [le_def] "!!m. Suc(m) <= n ==> m <= n";
   411 by(Asm_full_simp_tac 1);
   412 by(fast_tac HOL_cs 1);
   413 qed "Suc_leD";
   414 
   415 goalw Nat.thy [le_def] "!!m. m <= n ==> m <= Suc n";
   416 by (fast_tac (HOL_cs addDs [Suc_lessD]) 1);
   417 qed "le_SucI";
   418 Addsimps[le_SucI];
   419 
   420 goalw Nat.thy [le_def] "!!m. m < n ==> m <= (n::nat)";
   421 by (fast_tac (HOL_cs addEs [less_asym]) 1);
   422 qed "less_imp_le";
   423 
   424 goalw Nat.thy [le_def] "!!m. m <= n ==> m < n | m=(n::nat)";
   425 by (cut_facts_tac [less_linear] 1);
   426 by (fast_tac (HOL_cs addEs [less_anti_refl,less_asym]) 1);
   427 qed "le_imp_less_or_eq";
   428 
   429 goalw Nat.thy [le_def] "!!m. m<n | m=n ==> m <=(n::nat)";
   430 by (cut_facts_tac [less_linear] 1);
   431 by (fast_tac (HOL_cs addEs [less_anti_refl,less_asym]) 1);
   432 by (flexflex_tac);
   433 qed "less_or_eq_imp_le";
   434 
   435 goal Nat.thy "(x <= (y::nat)) = (x < y | x=y)";
   436 by (REPEAT(ares_tac [iffI,less_or_eq_imp_le,le_imp_less_or_eq] 1));
   437 qed "le_eq_less_or_eq";
   438 
   439 goal Nat.thy "n <= (n::nat)";
   440 by(simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1);
   441 qed "le_refl";
   442 
   443 val prems = goal Nat.thy "!!i. [| i <= j; j < k |] ==> i < (k::nat)";
   444 by (dtac le_imp_less_or_eq 1);
   445 by (fast_tac (HOL_cs addIs [less_trans]) 1);
   446 qed "le_less_trans";
   447 
   448 goal Nat.thy "!!i. [| i < j; j <= k |] ==> i < (k::nat)";
   449 by (dtac le_imp_less_or_eq 1);
   450 by (fast_tac (HOL_cs addIs [less_trans]) 1);
   451 qed "less_le_trans";
   452 
   453 goal Nat.thy "!!i. [| i <= j; j <= k |] ==> i <= (k::nat)";
   454 by (EVERY1[dtac le_imp_less_or_eq, dtac le_imp_less_or_eq,
   455           rtac less_or_eq_imp_le, fast_tac (HOL_cs addIs [less_trans])]);
   456 qed "le_trans";
   457 
   458 val prems = goal Nat.thy "!!m. [| m <= n; n <= m |] ==> m = (n::nat)";
   459 by (EVERY1[dtac le_imp_less_or_eq, dtac le_imp_less_or_eq,
   460           fast_tac (HOL_cs addEs [less_anti_refl,less_asym])]);
   461 qed "le_anti_sym";
   462 
   463 goal Nat.thy "(Suc(n) <= Suc(m)) = (n <= m)";
   464 by (simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1);
   465 qed "Suc_le_mono";
   466 
   467 Addsimps [le_refl,Suc_le_mono];