author | lcp |
Thu, 06 Apr 1995 11:49:42 +0200 | |
changeset 246 | 0f9230a24164 |
parent 202 | c533bc92e882 |
permissions | -rw-r--r-- |
127 | 1 |
(* Title: HOL/ex/Term |
0 | 2 |
ID: $Id$ |
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1992 University of Cambridge |
|
5 |
||
127 | 6 |
Terms over a given alphabet -- function applications; illustrates list functor |
0 | 7 |
(essentially the same type as in Trees & Forests) |
8 |
*) |
|
9 |
||
10 |
open Term; |
|
11 |
||
12 |
(*** Monotonicity and unfolding of the function ***) |
|
13 |
||
127 | 14 |
goal Term.thy "term(A) = A <*> list(term(A))"; |
15 |
by (fast_tac (univ_cs addSIs (equalityI :: term.intrs) |
|
16 |
addEs [term.elim]) 1); |
|
171 | 17 |
qed "term_unfold"; |
0 | 18 |
|
127 | 19 |
(*This justifies using term in other recursive type definitions*) |
20 |
goalw Term.thy term.defs "!!A B. A<=B ==> term(A) <= term(B)"; |
|
21 |
by (REPEAT (ares_tac ([lfp_mono, list_mono] @ basic_monos) 1)); |
|
171 | 22 |
qed "term_mono"; |
0 | 23 |
|
127 | 24 |
(** Type checking -- term creates well-founded sets **) |
0 | 25 |
|
127 | 26 |
goalw Term.thy term.defs "term(sexp) <= sexp"; |
0 | 27 |
by (rtac lfp_lowerbound 1); |
127 | 28 |
by (fast_tac (univ_cs addIs [sexp.SconsI, list_sexp RS subsetD]) 1); |
171 | 29 |
qed "term_sexp"; |
0 | 30 |
|
127 | 31 |
(* A <= sexp ==> term(A) <= sexp *) |
202 | 32 |
bind_thm ("term_subset_sexp", ([term_mono, term_sexp] MRS subset_trans)); |
0 | 33 |
|
34 |
||
127 | 35 |
(** Elimination -- structural induction on the set term(A) **) |
0 | 36 |
|
127 | 37 |
(*Induction for the set term(A) *) |
0 | 38 |
val [major,minor] = goal Term.thy |
127 | 39 |
"[| M: term(A); \ |
40 |
\ !!x zs. [| x: A; zs: list(term(A)); zs: list({x.R(x)}) \ |
|
48
21291189b51e
changed "." to "$" and Cons to infix "#" to eliminate ambiguity
clasohm
parents:
0
diff
changeset
|
41 |
\ |] ==> R(x$zs) \ |
0 | 42 |
\ |] ==> R(M)"; |
127 | 43 |
by (rtac (major RS term.induct) 1); |
44 |
by (REPEAT (eresolve_tac ([minor] @ |
|
45 |
([Int_lower1,Int_lower2] RL [list_mono RS subsetD])) 1)); |
|
46 |
(*Proof could also use mono_Int RS subsetD RS IntE *) |
|
180 | 47 |
qed "Term_induct"; |
0 | 48 |
|
127 | 49 |
(*Induction on term(A) followed by induction on list *) |
0 | 50 |
val major::prems = goal Term.thy |
127 | 51 |
"[| M: term(A); \ |
48
21291189b51e
changed "." to "$" and Cons to infix "#" to eliminate ambiguity
clasohm
parents:
0
diff
changeset
|
52 |
\ !!x. [| x: A |] ==> R(x$NIL); \ |
127 | 53 |
\ !!x z zs. [| x: A; z: term(A); zs: list(term(A)); R(x$zs) \ |
48
21291189b51e
changed "." to "$" and Cons to infix "#" to eliminate ambiguity
clasohm
parents:
0
diff
changeset
|
54 |
\ |] ==> R(x $ CONS(z,zs)) \ |
0 | 55 |
\ |] ==> R(M)"; |
180 | 56 |
by (rtac (major RS Term_induct) 1); |
127 | 57 |
by (etac list.induct 1); |
0 | 58 |
by (REPEAT (ares_tac prems 1)); |
180 | 59 |
qed "Term_induct2"; |
0 | 60 |
|
61 |
(*** Structural Induction on the abstract type 'a term ***) |
|
62 |
||
63 |
val list_all_ss = map_ss addsimps [list_all_Nil, list_all_Cons]; |
|
64 |
||
127 | 65 |
val Rep_term_in_sexp = |
66 |
Rep_term RS (range_Leaf_subset_sexp RS term_subset_sexp RS subsetD); |
|
0 | 67 |
|
68 |
(*Induction for the abstract type 'a term*) |
|
127 | 69 |
val prems = goalw Term.thy [App_def,Rep_Tlist_def,Abs_Tlist_def] |
0 | 70 |
"[| !!x ts. list_all(R,ts) ==> R(App(x,ts)) \ |
71 |
\ |] ==> R(t)"; |
|
127 | 72 |
by (rtac (Rep_term_inverse RS subst) 1); (*types force good instantiation*) |
73 |
by (res_inst_tac [("P","Rep_term(t) : sexp")] conjunct2 1); |
|
180 | 74 |
by (rtac (Rep_term RS Term_induct) 1); |
127 | 75 |
by (REPEAT (ares_tac [conjI, sexp.SconsI, term_subset_sexp RS |
76 |
list_subset_sexp, range_Leaf_subset_sexp] 1 |
|
0 | 77 |
ORELSE etac rev_subsetD 1)); |
127 | 78 |
by (eres_inst_tac [("A1","term(?u)"), ("f1","Rep_term"), ("g1","Abs_term")] |
0 | 79 |
(Abs_map_inverse RS subst) 1); |
127 | 80 |
by (rtac (range_Leaf_subset_sexp RS term_subset_sexp) 1); |
81 |
by (etac Abs_term_inverse 1); |
|
0 | 82 |
by (etac rangeE 1); |
83 |
by (hyp_subst_tac 1); |
|
84 |
by (resolve_tac prems 1); |
|
127 | 85 |
by (etac list.induct 1); |
0 | 86 |
by (etac CollectE 2); |
87 |
by (stac Abs_map_CONS 2); |
|
88 |
by (etac conjunct1 2); |
|
89 |
by (etac rev_subsetD 2); |
|
127 | 90 |
by (rtac list_subset_sexp 2); |
0 | 91 |
by (fast_tac set_cs 2); |
92 |
by (ALLGOALS (asm_simp_tac list_all_ss)); |
|
171 | 93 |
qed "term_induct"; |
0 | 94 |
|
95 |
(*Induction for the abstract type 'a term*) |
|
96 |
val prems = goal Term.thy |
|
97 |
"[| !!x. R(App(x,Nil)); \ |
|
48
21291189b51e
changed "." to "$" and Cons to infix "#" to eliminate ambiguity
clasohm
parents:
0
diff
changeset
|
98 |
\ !!x t ts. R(App(x,ts)) ==> R(App(x, t#ts)) \ |
0 | 99 |
\ |] ==> R(t)"; |
100 |
by (rtac term_induct 1); (*types force good instantiation*) |
|
101 |
by (etac rev_mp 1); |
|
102 |
by (rtac list_induct 1); (*types force good instantiation*) |
|
103 |
by (ALLGOALS (asm_simp_tac (list_all_ss addsimps prems))); |
|
171 | 104 |
qed "term_induct2"; |
0 | 105 |
|
106 |
(*Perform induction on xs. *) |
|
107 |
fun term_ind2_tac a i = |
|
108 |
EVERY [res_inst_tac [("t",a)] term_induct2 i, |
|
109 |
rename_last_tac a ["1","s"] (i+1)]; |
|
110 |
||
111 |
||
112 |
||
127 | 113 |
(*** Term_rec -- by wf recursion on pred_sexp ***) |
0 | 114 |
|
115 |
val Term_rec_unfold = |
|
127 | 116 |
wf_pred_sexp RS wf_trancl RS (Term_rec_def RS def_wfrec); |
0 | 117 |
|
118 |
(** conversion rules **) |
|
119 |
||
120 |
val [prem] = goal Term.thy |
|
127 | 121 |
"N: list(term(A)) ==> \ |
122 |
\ !M. <N,M>: pred_sexp^+ --> \ |
|
123 |
\ Abs_map(cut(h, pred_sexp^+, M), N) = \ |
|
0 | 124 |
\ Abs_map(h,N)"; |
127 | 125 |
by (rtac (prem RS list.induct) 1); |
0 | 126 |
by (simp_tac list_all_ss 1); |
127 |
by (strip_tac 1); |
|
127 | 128 |
by (etac (pred_sexp_CONS_D RS conjE) 1); |
129 |
by (asm_simp_tac (list_all_ss addsimps [trancl_pred_sexpD1, cut_apply]) 1); |
|
171 | 130 |
qed "Abs_map_lemma"; |
0 | 131 |
|
127 | 132 |
val [prem1,prem2,A_subset_sexp] = goal Term.thy |
133 |
"[| M: sexp; N: list(term(A)); A<=sexp |] ==> \ |
|
48
21291189b51e
changed "." to "$" and Cons to infix "#" to eliminate ambiguity
clasohm
parents:
0
diff
changeset
|
134 |
\ Term_rec(M$N, d) = d(M, N, Abs_map(%Z. Term_rec(Z,d), N))"; |
0 | 135 |
by (rtac (Term_rec_unfold RS trans) 1); |
136 |
by (simp_tac (HOL_ss addsimps |
|
114 | 137 |
[Split, |
127 | 138 |
prem2 RS Abs_map_lemma RS spec RS mp, pred_sexpI2 RS r_into_trancl, |
139 |
prem1, prem2 RS rev_subsetD, list_subset_sexp, |
|
140 |
term_subset_sexp, A_subset_sexp])1); |
|
171 | 141 |
qed "Term_rec"; |
0 | 142 |
|
143 |
(*** term_rec -- by Term_rec ***) |
|
144 |
||
145 |
local |
|
146 |
val Rep_map_type1 = read_instantiate_sg (sign_of Term.thy) |
|
127 | 147 |
[("f","Rep_term")] Rep_map_type; |
148 |
val Rep_Tlist = Rep_term RS Rep_map_type1; |
|
149 |
val Rep_Term_rec = range_Leaf_subset_sexp RSN (2,Rep_Tlist RSN(2,Term_rec)); |
|
0 | 150 |
|
127 | 151 |
(*Now avoids conditional rewriting with the premise N: list(term(A)), |
0 | 152 |
since A will be uninstantiated and will cause rewriting to fail. *) |
153 |
val term_rec_ss = HOL_ss |
|
127 | 154 |
addsimps [Rep_Tlist RS (rangeI RS term.APP_I RS Abs_term_inverse), |
155 |
Rep_term_in_sexp, Rep_Term_rec, Rep_term_inverse, |
|
0 | 156 |
inj_Leaf, Inv_f_f, |
127 | 157 |
Abs_Rep_map, map_ident, sexp.LeafI] |
0 | 158 |
in |
159 |
||
160 |
val term_rec = prove_goalw Term.thy |
|
127 | 161 |
[term_rec_def, App_def, Rep_Tlist_def, Abs_Tlist_def] |
0 | 162 |
"term_rec(App(f,ts), d) = d(f, ts, map (%t. term_rec(t,d), ts))" |
163 |
(fn _ => [simp_tac term_rec_ss 1]) |
|
164 |
||
165 |
end; |