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