| author | paulson | 
| Tue, 24 Feb 1998 11:35:33 +0100 | |
| changeset 4648 | f04da668581c | 
| parent 4152 | 451104c223e2 | 
| child 5068 | fb28eaa07e01 | 
| permissions | -rw-r--r-- | 
| 1461 | 1 | (* Title: ZF/ex/Term.ML | 
| 0 | 2 | ID: $Id$ | 
| 1461 | 3 | Author: Lawrence C Paulson, Cambridge University Computer Laboratory | 
| 486 | 4 | Copyright 1994 University of Cambridge | 
| 0 | 5 | |
| 6 | Datatype definition of terms over an alphabet. | |
| 7 | Illustrates the list functor (essentially the same type as in Trees & Forests) | |
| 8 | *) | |
| 9 | ||
| 515 | 10 | open Term; | 
| 0 | 11 | |
| 434 | 12 | goal Term.thy "term(A) = A * list(term(A))"; | 
| 529 
f0d16216e394
ZF/List, ex/Brouwer,Data,LList,Ntree,TF,Term: much simplified proof of _unfold
 lcp parents: 
515diff
changeset | 13 | let open term; val rew = rewrite_rule con_defs in | 
| 4091 | 14 | by (fast_tac (claset() addSIs (map rew intrs) addEs [rew elim]) 1) | 
| 529 
f0d16216e394
ZF/List, ex/Brouwer,Data,LList,Ntree,TF,Term: much simplified proof of _unfold
 lcp parents: 
515diff
changeset | 15 | end; | 
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 16 | qed "term_unfold"; | 
| 434 | 17 | |
| 0 | 18 | (*Induction on term(A) followed by induction on List *) | 
| 19 | val major::prems = goal Term.thy | |
| 20 | "[| t: term(A); \ | |
| 21 | \ !!x. [| x: A |] ==> P(Apply(x,Nil)); \ | |
| 22 | \ !!x z zs. [| x: A; z: term(A); zs: list(term(A)); P(Apply(x,zs)) \ | |
| 23 | \ |] ==> P(Apply(x, Cons(z,zs))) \ | |
| 24 | \ |] ==> P(t)"; | |
| 515 | 25 | by (rtac (major RS term.induct) 1); | 
| 26 | by (etac list.induct 1); | |
| 0 | 27 | by (etac CollectE 2); | 
| 28 | by (REPEAT (ares_tac (prems@[list_CollectD]) 1)); | |
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 29 | qed "term_induct2"; | 
| 0 | 30 | |
| 31 | (*Induction on term(A) to prove an equation*) | |
| 515 | 32 | val major::prems = goal Term.thy | 
| 0 | 33 | "[| t: term(A); \ | 
| 34 | \ !!x zs. [| x: A; zs: list(term(A)); map(f,zs) = map(g,zs) |] ==> \ | |
| 35 | \ f(Apply(x,zs)) = g(Apply(x,zs)) \ | |
| 36 | \ |] ==> f(t)=g(t)"; | |
| 515 | 37 | by (rtac (major RS term.induct) 1); | 
| 0 | 38 | by (resolve_tac prems 1); | 
| 39 | by (REPEAT (eresolve_tac [asm_rl, map_list_Collect, list_CollectD] 1)); | |
| 760 | 40 | qed "term_induct_eqn"; | 
| 0 | 41 | |
| 42 | (** Lemmas to justify using "term" in other recursive type definitions **) | |
| 43 | ||
| 515 | 44 | goalw Term.thy term.defs "!!A B. A<=B ==> term(A) <= term(B)"; | 
| 0 | 45 | by (rtac lfp_mono 1); | 
| 515 | 46 | by (REPEAT (rtac term.bnd_mono 1)); | 
| 0 | 47 | by (REPEAT (ares_tac (univ_mono::basic_monos) 1)); | 
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 48 | qed "term_mono"; | 
| 0 | 49 | |
| 50 | (*Easily provable by induction also*) | |
| 515 | 51 | goalw Term.thy (term.defs@term.con_defs) "term(univ(A)) <= univ(A)"; | 
| 0 | 52 | by (rtac lfp_lowerbound 1); | 
| 53 | by (rtac (A_subset_univ RS univ_mono) 2); | |
| 4152 | 54 | by Safe_tac; | 
| 0 | 55 | by (REPEAT (ares_tac [Pair_in_univ, list_univ RS subsetD] 1)); | 
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 56 | qed "term_univ"; | 
| 0 | 57 | |
| 434 | 58 | val term_subset_univ = | 
| 59 | term_mono RS (term_univ RSN (2,subset_trans)) |> standard; | |
| 0 | 60 | |
| 434 | 61 | goal Term.thy "!!t A B. [| t: term(A); A <= univ(B) |] ==> t: univ(B)"; | 
| 62 | by (REPEAT (ares_tac [term_subset_univ RS subsetD] 1)); | |
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 63 | qed "term_into_univ"; | 
| 515 | 64 | |
| 65 | ||
| 66 | (*** term_rec -- by Vset recursion ***) | |
| 67 | ||
| 68 | (*Lemma: map works correctly on the underlying list of terms*) | |
| 69 | val [major,ordi] = goal list.thy | |
| 70 | "[| l: list(A); Ord(i) |] ==> \ | |
| 71 | \ rank(l)<i --> map(%z. (lam x:Vset(i).h(x)) ` z, l) = map(h,l)"; | |
| 72 | by (rtac (major RS list.induct) 1); | |
| 2469 | 73 | by (Simp_tac 1); | 
| 515 | 74 | by (rtac impI 1); | 
| 75 | by (forward_tac [rank_Cons1 RS lt_trans] 1); | |
| 76 | by (dtac (rank_Cons2 RS lt_trans) 1); | |
| 4091 | 77 | by (asm_simp_tac (simpset() addsimps [ordi, VsetI]) 1); | 
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 78 | qed "map_lemma"; | 
| 515 | 79 | |
| 80 | (*Typing premise is necessary to invoke map_lemma*) | |
| 81 | val [prem] = goal Term.thy | |
| 82 | "ts: list(A) ==> \ | |
| 83 | \ term_rec(Apply(a,ts), d) = d(a, ts, map (%z. term_rec(z,d), ts))"; | |
| 84 | by (rtac (term_rec_def RS def_Vrec RS trans) 1); | |
| 85 | by (rewrite_goals_tac term.con_defs); | |
| 4091 | 86 | by (simp_tac (simpset() addsimps [Ord_rank, rank_pair2, prem RS map_lemma]) 1);; | 
| 760 | 87 | qed "term_rec"; | 
| 515 | 88 | |
| 89 | (*Slightly odd typing condition on r in the second premise!*) | |
| 90 | val major::prems = goal Term.thy | |
| 1461 | 91 | "[| t: term(A); \ | 
| 92 | \ !!x zs r. [| x: A; zs: list(term(A)); \ | |
| 93 | \ r: list(UN t:term(A). C(t)) |] \ | |
| 94 | \ ==> d(x, zs, r): C(Apply(x,zs)) \ | |
| 515 | 95 | \ |] ==> term_rec(t,d) : C(t)"; | 
| 96 | by (rtac (major RS term.induct) 1); | |
| 97 | by (forward_tac [list_CollectD] 1); | |
| 2034 | 98 | by (stac term_rec 1); | 
| 515 | 99 | by (REPEAT (ares_tac prems 1)); | 
| 100 | by (etac list.induct 1); | |
| 4091 | 101 | by (ALLGOALS (asm_simp_tac (simpset() addsimps [term_rec]))); | 
| 515 | 102 | by (etac CollectE 1); | 
| 103 | by (REPEAT (ares_tac [list.Cons_I, UN_I] 1)); | |
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 104 | qed "term_rec_type"; | 
| 515 | 105 | |
| 106 | val [rew,tslist] = goal Term.thy | |
| 107 | "[| !!t. j(t)==term_rec(t,d); ts: list(A) |] ==> \ | |
| 3840 | 108 | \ j(Apply(a,ts)) = d(a, ts, map(%Z. j(Z), ts))"; | 
| 515 | 109 | by (rewtac rew); | 
| 110 | by (rtac (tslist RS term_rec) 1); | |
| 760 | 111 | qed "def_term_rec"; | 
| 515 | 112 | |
| 113 | val prems = goal Term.thy | |
| 1461 | 114 | "[| t: term(A); \ | 
| 515 | 115 | \ !!x zs r. [| x: A; zs: list(term(A)); r: list(C) |] \ | 
| 1461 | 116 | \ ==> d(x, zs, r): C \ | 
| 515 | 117 | \ |] ==> term_rec(t,d) : C"; | 
| 118 | by (REPEAT (ares_tac (term_rec_type::prems) 1)); | |
| 119 | by (etac (subset_refl RS UN_least RS list_mono RS subsetD) 1); | |
| 760 | 120 | qed "term_rec_simple_type"; | 
| 515 | 121 | |
| 122 | ||
| 123 | (** term_map **) | |
| 124 | ||
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 125 | bind_thm ("term_map", (term_map_def RS def_term_rec));
 | 
| 515 | 126 | |
| 127 | val prems = goalw Term.thy [term_map_def] | |
| 128 | "[| t: term(A); !!x. x: A ==> f(x): B |] ==> term_map(f,t) : term(B)"; | |
| 129 | by (REPEAT (ares_tac ([term_rec_simple_type, term.Apply_I] @ prems) 1)); | |
| 760 | 130 | qed "term_map_type"; | 
| 515 | 131 | |
| 132 | val [major] = goal Term.thy | |
| 133 |     "t: term(A) ==> term_map(f,t) : term({f(u). u:A})";
 | |
| 134 | by (rtac (major RS term_map_type) 1); | |
| 135 | by (etac RepFunI 1); | |
| 760 | 136 | qed "term_map_type2"; | 
| 515 | 137 | |
| 138 | ||
| 139 | (** term_size **) | |
| 140 | ||
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 141 | bind_thm ("term_size", (term_size_def RS def_term_rec));
 | 
| 515 | 142 | |
| 143 | goalw Term.thy [term_size_def] "!!t A. t: term(A) ==> term_size(t) : nat"; | |
| 144 | by (REPEAT (ares_tac [term_rec_simple_type, list_add_type, nat_succI] 1)); | |
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 145 | qed "term_size_type"; | 
| 515 | 146 | |
| 147 | ||
| 148 | (** reflect **) | |
| 149 | ||
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 150 | bind_thm ("reflect", (reflect_def RS def_term_rec));
 | 
| 515 | 151 | |
| 152 | goalw Term.thy [reflect_def] "!!t A. t: term(A) ==> reflect(t) : term(A)"; | |
| 153 | by (REPEAT (ares_tac [term_rec_simple_type, rev_type, term.Apply_I] 1)); | |
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 154 | qed "reflect_type"; | 
| 515 | 155 | |
| 156 | (** preorder **) | |
| 157 | ||
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 158 | bind_thm ("preorder", (preorder_def RS def_term_rec));
 | 
| 515 | 159 | |
| 160 | goalw Term.thy [preorder_def] | |
| 161 | "!!t A. t: term(A) ==> preorder(t) : list(A)"; | |
| 162 | by (REPEAT (ares_tac [term_rec_simple_type, list.Cons_I, flat_type] 1)); | |
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 163 | qed "preorder_type"; | 
| 515 | 164 | |
| 165 | ||
| 166 | (** Term simplification **) | |
| 167 | ||
| 168 | val term_typechecks = | |
| 169 | [term.Apply_I, term_map_type, term_map_type2, term_size_type, | |
| 170 | reflect_type, preorder_type]; | |
| 171 | ||
| 172 | (*map_type2 and term_map_type2 instantiate variables*) | |
| 4091 | 173 | simpset_ref() := simpset() | 
| 515 | 174 | addsimps [term_rec, term_map, term_size, reflect, preorder] | 
| 2637 
e9b203f854ae
reflecting my recent changes of the simplifier and classical reasoner
 oheimb parents: 
2496diff
changeset | 175 | setSolver type_auto_tac (list_typechecks@term_typechecks); | 
| 515 | 176 | |
| 177 | ||
| 178 | (** theorems about term_map **) | |
| 179 | ||
| 3840 | 180 | goal Term.thy "!!t A. t: term(A) ==> term_map(%u. u, t) = t"; | 
| 515 | 181 | by (etac term_induct_eqn 1); | 
| 4091 | 182 | by (asm_simp_tac (simpset() addsimps [map_ident]) 1); | 
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 183 | qed "term_map_ident"; | 
| 515 | 184 | |
| 185 | goal Term.thy | |
| 3840 | 186 | "!!t A. t: term(A) ==> term_map(f, term_map(g,t)) = term_map(%u. f(g(u)), t)"; | 
| 515 | 187 | by (etac term_induct_eqn 1); | 
| 4091 | 188 | by (asm_simp_tac (simpset() addsimps [map_compose]) 1); | 
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 189 | qed "term_map_compose"; | 
| 515 | 190 | |
| 191 | goal Term.thy | |
| 192 | "!!t A. t: term(A) ==> term_map(f, reflect(t)) = reflect(term_map(f,t))"; | |
| 193 | by (etac term_induct_eqn 1); | |
| 4091 | 194 | by (asm_simp_tac (simpset() addsimps [rev_map_distrib RS sym, map_compose]) 1); | 
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 195 | qed "term_map_reflect"; | 
| 515 | 196 | |
| 197 | ||
| 198 | (** theorems about term_size **) | |
| 199 | ||
| 200 | goal Term.thy | |
| 201 | "!!t A. t: term(A) ==> term_size(term_map(f,t)) = term_size(t)"; | |
| 202 | by (etac term_induct_eqn 1); | |
| 4091 | 203 | by (asm_simp_tac (simpset() addsimps [map_compose]) 1); | 
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 204 | qed "term_size_term_map"; | 
| 515 | 205 | |
| 206 | goal Term.thy "!!t A. t: term(A) ==> term_size(reflect(t)) = term_size(t)"; | |
| 207 | by (etac term_induct_eqn 1); | |
| 4091 | 208 | by (asm_simp_tac (simpset() addsimps [rev_map_distrib RS sym, map_compose, | 
| 1461 | 209 | list_add_rev]) 1); | 
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 210 | qed "term_size_reflect"; | 
| 515 | 211 | |
| 212 | goal Term.thy "!!t A. t: term(A) ==> term_size(t) = length(preorder(t))"; | |
| 213 | by (etac term_induct_eqn 1); | |
| 4091 | 214 | by (asm_simp_tac (simpset() addsimps [length_flat, map_compose]) 1); | 
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 215 | qed "term_size_length"; | 
| 515 | 216 | |
| 217 | ||
| 218 | (** theorems about reflect **) | |
| 219 | ||
| 220 | goal Term.thy "!!t A. t: term(A) ==> reflect(reflect(t)) = t"; | |
| 221 | by (etac term_induct_eqn 1); | |
| 4091 | 222 | by (asm_simp_tac (simpset() addsimps [rev_map_distrib, map_compose, | 
| 1461 | 223 | map_ident, rev_rev_ident]) 1); | 
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 224 | qed "reflect_reflect_ident"; | 
| 515 | 225 | |
| 226 | ||
| 227 | (** theorems about preorder **) | |
| 228 | ||
| 229 | goal Term.thy | |
| 230 | "!!t A. t: term(A) ==> preorder(term_map(f,t)) = map(f, preorder(t))"; | |
| 231 | by (etac term_induct_eqn 1); | |
| 4091 | 232 | by (asm_simp_tac (simpset() addsimps [map_compose, map_flat]) 1); | 
| 782 
200a16083201
added bind_thm for theorems defined by "standard ..."
 clasohm parents: 
760diff
changeset | 233 | qed "preorder_term_map"; | 
| 515 | 234 | |
| 235 | (** preorder(reflect(t)) = rev(postorder(t)) **) | |
| 236 | ||
| 237 | writeln"Reached end of file."; |