src/ZF/nat.ML
author clasohm
Thu, 16 Sep 1993 12:20:38 +0200
changeset 0 a5a9c433f639
child 6 8ce8c4d13d4d
permissions -rw-r--r--
Initial revision
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     1
(*  Title: 	ZF/nat.ML
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1992  University of Cambridge
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
For nat.thy.  Natural numbers in Zermelo-Fraenkel Set Theory 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
open Nat;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
goal Nat.thy "bnd_mono(Inf, %X. {0} Un {succ(i). i:X})";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
by (rtac bnd_monoI 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
by (REPEAT (ares_tac [subset_refl, RepFun_mono, Un_mono] 2)); 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
by (cut_facts_tac [infinity] 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
by (fast_tac ZF_cs 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
val nat_bnd_mono = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
(* nat = {0} Un {succ(x). x:nat} *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
val nat_unfold = nat_bnd_mono RS (nat_def RS def_lfp_Tarski);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
(** Type checking of 0 and successor **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
goal Nat.thy "0 : nat";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
by (rtac (nat_unfold RS ssubst) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
by (rtac (singletonI RS UnI1) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
val nat_0I = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
val prems = goal Nat.thy "n : nat ==> succ(n) : nat";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
by (rtac (nat_unfold RS ssubst) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
by (rtac (RepFunI RS UnI2) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
by (resolve_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
val nat_succI = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
goalw Nat.thy [one_def] "1 : nat";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    35
by (rtac (nat_0I RS nat_succI) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    36
val nat_1I = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
goal Nat.thy "bool <= nat";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
by (REPEAT (ares_tac [subsetI,nat_0I,nat_1I] 1 ORELSE etac boolE 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
val bool_subset_nat = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
val bool_into_nat = bool_subset_nat RS subsetD;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
(** Injectivity properties and induction **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
(*Mathematical induction*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
val major::prems = goal Nat.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
    "[| n: nat;  P(0);  !!x. [| x: nat;  P(x) |] ==> P(succ(x)) |] ==> P(n)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
by (rtac ([nat_def, nat_bnd_mono, major] MRS def_induct) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
by (fast_tac (ZF_cs addIs prems) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
val nat_induct = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
(*Perform induction on n, then prove the n:nat subgoal using prems. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
fun nat_ind_tac a prems i = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
    EVERY [res_inst_tac [("n",a)] nat_induct i,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
	   rename_last_tac a ["1"] (i+2),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
	   ares_tac prems i];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
val major::prems = goal Nat.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
    "[| n: nat;  n=0 ==> P;  !!x. [| x: nat; n=succ(x) |] ==> P |] ==> P";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
br (major RS (nat_unfold RS equalityD1 RS subsetD) RS UnE) 1;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
by (DEPTH_SOLVE (eresolve_tac [singletonE,RepFunE] 1
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
          ORELSE ares_tac prems 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
val natE = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
val prems = goal Nat.thy "n: nat ==> Ord(n)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
by (nat_ind_tac "n" prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
by (REPEAT (ares_tac [Ord_0, Ord_succ] 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
val naturals_are_ordinals = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    72
goal Nat.thy "!!n. n: nat ==> n=0 | 0:n";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    73
by (etac nat_induct 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
by (fast_tac ZF_cs 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
by (fast_tac (ZF_cs addIs [naturals_are_ordinals RS Ord_0_mem_succ]) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
val natE0 = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
goal Nat.thy "Ord(nat)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
by (rtac OrdI 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
by (etac (naturals_are_ordinals RS Ord_is_Transset) 2);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
by (rewtac Transset_def);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
by (rtac ballI 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
by (etac nat_induct 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
by (REPEAT (ares_tac [empty_subsetI,succ_subsetI] 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
val Ord_nat = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
(** Variations on mathematical induction **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
(*complete induction*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
val complete_induct = Ord_nat RSN (2, Ord_induct);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    92
val prems = goal Nat.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
    "[| m: nat;  n: nat;  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    94
\       !!x. [| x: nat;  m<=x;  P(x) |] ==> P(succ(x)) \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    95
\    |] ==> m <= n --> P(m) --> P(n)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
by (nat_ind_tac "n" prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
by (ALLGOALS
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
    (ASM_SIMP_TAC
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
     (ZF_ss addrews (prems@distrib_rews@[subset_empty_iff, subset_succ_iff, 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
					 Ord_nat RS Ord_in_Ord]))));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
val nat_induct_from_lemma = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   103
(*Induction starting from m rather than 0*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
val prems = goal Nat.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
    "[| m <= n;  m: nat;  n: nat;  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
\       P(m);  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
\       !!x. [| x: nat;  m<=x;  P(x) |] ==> P(succ(x)) \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
\    |] ==> P(n)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
by (rtac (nat_induct_from_lemma RS mp RS mp) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
by (REPEAT (ares_tac prems 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
val nat_induct_from = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
(*Induction suitable for subtraction and less-than*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
val prems = goal Nat.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
    "[| m: nat;  n: nat;  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
\       !!x. [| x: nat |] ==> P(x,0);  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
\       !!y. [| y: nat |] ==> P(0,succ(y));  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
\       !!x y. [| x: nat;  y: nat;  P(x,y) |] ==> P(succ(x),succ(y))  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
\    |] ==> P(m,n)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
by (res_inst_tac [("x","m")] bspec 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
by (resolve_tac prems 2);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
by (nat_ind_tac "n" prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
by (rtac ballI 2);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
by (nat_ind_tac "x" [] 2);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
by (REPEAT (ares_tac (prems@[ballI]) 1 ORELSE etac bspec 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
val diff_induct = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
(** nat_case **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
goalw Nat.thy [nat_case_def] "nat_case(0,a,b) = a";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
by (fast_tac (ZF_cs addIs [the_equality]) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
val nat_case_0 = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
goalw Nat.thy [nat_case_def] "nat_case(succ(m),a,b) = b(m)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   135
by (fast_tac (ZF_cs addIs [the_equality]) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
val nat_case_succ = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   137
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
val major::prems = goal Nat.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
    "[| n: nat;  a: C(0);  !!m. m: nat ==> b(m): C(succ(m))  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
\    |] ==> nat_case(n,a,b) : C(n)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   141
by (rtac (major RS nat_induct) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
by (REPEAT (resolve_tac [nat_case_0 RS ssubst,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
			 nat_case_succ RS ssubst] 1 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
       THEN resolve_tac prems 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
by (assume_tac 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
val nat_case_type = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
val prems = goalw Nat.thy [nat_case_def]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
    "[| n=n';  a=a';  !!m z. b(m)=b'(m)  \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
\    |] ==> nat_case(n,a,b)=nat_case(n',a',b')";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
by (REPEAT (resolve_tac [the_cong,disj_cong,ex_cong] 1
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
     ORELSE EVERY1 (map rtac ((prems RL [ssubst]) @ [iff_refl]))));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
val nat_case_cong = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   155
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
(** nat_rec -- used to define eclose and transrec, then obsolete **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
val nat_rec_trans = wf_Memrel RS (nat_rec_def RS def_wfrec RS trans);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   159
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
goal Nat.thy "nat_rec(0,a,b) = a";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
by (rtac nat_rec_trans 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
by (rtac nat_case_0 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
val nat_rec_0 = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   165
val [prem] = goal Nat.thy 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
    "m: nat ==> nat_rec(succ(m),a,b) = b(m, nat_rec(m,a,b))";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   167
val nat_rec_ss = ZF_ss 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   168
      addcongs (mk_typed_congs Nat.thy [("b", "[i,i]=>i")])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
      addrews [prem, nat_case_succ, nat_succI, Memrel_iff, 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   170
	       vimage_singleton_iff];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   171
by (rtac nat_rec_trans 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   172
by (SIMP_TAC nat_rec_ss 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   173
val nat_rec_succ = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   174
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   175
(** The union of two natural numbers is a natural number -- their maximum **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   176
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   177
(*  [| ?i : nat; ?j : nat |] ==> ?i Un ?j : nat  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   178
val Un_nat_type = standard (Ord_nat RSN (3,Ord_member_UnI));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   180
(*  [| ?i : nat; ?j : nat |] ==> ?i Int ?j : nat  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   181
val Int_nat_type = standard (Ord_nat RSN (3,Ord_member_IntI));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   182