src/ZF/ex/BT.ML
author lcp
Fri, 12 Aug 1994 12:28:46 +0200
changeset 515 abcc438e7c27
parent 477 53fc8ad84b33
child 760 f0200e91b272
permissions -rw-r--r--
installation of new inductive/datatype sections
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
515
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
     1
(*  Title: 	ZF/ex/BT.ML
0
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
515
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
     4
    Copyright   1994  University of Cambridge
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
Datatype definition of binary trees
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
515
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
     9
open BT;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
(*Perform induction on l, then prove the major premise using prems. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
fun bt_ind_tac a prems i = 
515
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    13
    EVERY [res_inst_tac [("x",a)] bt.induct i,
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
	   rename_last_tac a ["1","2"] (i+2),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
	   ares_tac prems i];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
(**  Lemmas to justify using "bt" in other recursive type definitions **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
515
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    20
goalw BT.thy bt.defs "!!A B. A<=B ==> bt(A) <= bt(B)";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
by (rtac lfp_mono 1);
515
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    22
by (REPEAT (rtac bt.bnd_mono 1));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
by (REPEAT (ares_tac (univ_mono::basic_monos) 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
val bt_mono = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
515
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    26
goalw BT.thy (bt.defs@bt.con_defs) "bt(univ(A)) <= univ(A)";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
by (rtac lfp_lowerbound 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
by (rtac (A_subset_univ RS univ_mono) 2);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
by (fast_tac (ZF_cs addSIs [zero_in_univ, Inl_in_univ, Inr_in_univ,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
			    Pair_in_univ]) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
val bt_univ = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
56
2caa6f49f06e ZF/ex/tf/tree,forest_unfold: streamlined the proofs
lcp
parents: 0
diff changeset
    33
val bt_subset_univ = standard ([bt_mono, bt_univ] MRS subset_trans);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
515
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    35
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    36
(** bt_rec -- by Vset recursion **)
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    37
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    38
goalw BT.thy bt.con_defs "rank(l) < rank(Br(a,l,r))";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    39
by (simp_tac rank_ss 1);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    40
val rank_Br1 = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    41
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    42
goalw BT.thy bt.con_defs "rank(r) < rank(Br(a,l,r))";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    43
by (simp_tac rank_ss 1);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    44
val rank_Br2 = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    45
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    46
goal BT.thy "bt_rec(Lf,c,h) = c";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    47
by (rtac (bt_rec_def RS def_Vrec RS trans) 1);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    48
by (simp_tac (ZF_ss addsimps bt.case_eqns) 1);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    49
val bt_rec_Lf = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    50
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    51
goal BT.thy
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    52
    "bt_rec(Br(a,l,r), c, h) = h(a, l, r, bt_rec(l,c,h), bt_rec(r,c,h))";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    53
by (rtac (bt_rec_def RS def_Vrec RS trans) 1);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    54
by (simp_tac (rank_ss addsimps (bt.case_eqns @ [rank_Br1, rank_Br2])) 1);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    55
val bt_rec_Br = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    56
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    57
(*Type checking -- proved by induction, as usual*)
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    58
val prems = goal BT.thy
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    59
    "[| t: bt(A);    \
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    60
\       c: C(Lf);       \
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    61
\       !!x y z r s. [| x:A;  y:bt(A);  z:bt(A);  r:C(y);  s:C(z) |] ==> \
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    62
\		     h(x,y,z,r,s): C(Br(x,y,z))  \
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    63
\    |] ==> bt_rec(t,c,h) : C(t)";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    64
by (bt_ind_tac "t" prems 1);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    65
by (ALLGOALS (asm_simp_tac (ZF_ss addsimps
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    66
			    (prems@[bt_rec_Lf,bt_rec_Br]))));
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    67
val bt_rec_type = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    68
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    69
(** Versions for use with definitions **)
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    70
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    71
val [rew] = goal BT.thy "[| !!t. j(t)==bt_rec(t, c, h) |] ==> j(Lf) = c";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    72
by (rewtac rew);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    73
by (rtac bt_rec_Lf 1);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    74
val def_bt_rec_Lf = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    75
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    76
val [rew] = goal BT.thy
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    77
    "[| !!t. j(t)==bt_rec(t, c, h) |] ==> j(Br(a,l,r)) = h(a,l,r,j(l),j(r))";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    78
by (rewtac rew);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    79
by (rtac bt_rec_Br 1);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    80
val def_bt_rec_Br = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    81
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    82
fun bt_recs def = map standard ([def] RL [def_bt_rec_Lf, def_bt_rec_Br]);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    83
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    84
(** n_nodes **)
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    85
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    86
val [n_nodes_Lf,n_nodes_Br] = bt_recs n_nodes_def;
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    87
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    88
val prems = goalw BT.thy [n_nodes_def] 
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    89
    "xs: bt(A) ==> n_nodes(xs) : nat";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    90
by (REPEAT (ares_tac (prems @ [bt_rec_type, nat_0I, nat_succI, add_type]) 1));
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    91
val n_nodes_type = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    92
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    93
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    94
(** n_leaves **)
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    95
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    96
val [n_leaves_Lf,n_leaves_Br] = bt_recs n_leaves_def;
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    97
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    98
val prems = goalw BT.thy [n_leaves_def] 
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
    99
    "xs: bt(A) ==> n_leaves(xs) : nat";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   100
by (REPEAT (ares_tac (prems @ [bt_rec_type, nat_0I, nat_succI, add_type]) 1));
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   101
val n_leaves_type = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   102
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   103
(** bt_reflect **)
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   104
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   105
val [bt_reflect_Lf, bt_reflect_Br] = bt_recs bt_reflect_def;
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   106
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   107
goalw BT.thy [bt_reflect_def] "!!xs. xs: bt(A) ==> bt_reflect(xs) : bt(A)";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   108
by (REPEAT (ares_tac (bt.intrs @ [bt_rec_type]) 1));
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   109
val bt_reflect_type = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   110
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   111
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   112
(** BT simplification **)
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   113
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   114
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   115
val bt_typechecks =
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   116
    bt.intrs @ [bt_rec_type, n_nodes_type, n_leaves_type, bt_reflect_type];
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   117
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   118
val bt_ss = arith_ss 
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   119
    addsimps bt.case_eqns
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   120
    addsimps bt_typechecks
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   121
    addsimps [bt_rec_Lf, bt_rec_Br, 
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   122
	     n_nodes_Lf, n_nodes_Br,
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   123
	     n_leaves_Lf, n_leaves_Br,
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   124
	     bt_reflect_Lf, bt_reflect_Br];
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   125
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   126
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   127
(*** theorems about n_leaves ***)
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   128
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   129
val prems = goal BT.thy
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   130
    "t: bt(A) ==> n_leaves(bt_reflect(t)) = n_leaves(t)";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   131
by (bt_ind_tac "t" prems 1);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   132
by (ALLGOALS (asm_simp_tac bt_ss));
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   133
by (REPEAT (ares_tac [add_commute, n_leaves_type] 1));
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   134
val n_leaves_reflect = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   135
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   136
val prems = goal BT.thy
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   137
    "t: bt(A) ==> n_leaves(t) = succ(n_nodes(t))";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   138
by (bt_ind_tac "t" prems 1);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   139
by (ALLGOALS (asm_simp_tac (bt_ss addsimps [add_succ_right])));
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   140
val n_leaves_nodes = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   141
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   142
(*** theorems about bt_reflect ***)
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   143
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   144
val prems = goal BT.thy
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   145
    "t: bt(A) ==> bt_reflect(bt_reflect(t))=t";
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   146
by (bt_ind_tac "t" prems 1);
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   147
by (ALLGOALS (asm_simp_tac bt_ss));
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   148
val bt_reflect_bt_reflect_ident = result();
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   149
abcc438e7c27 installation of new inductive/datatype sections
lcp
parents: 477
diff changeset
   150