author | paulson |
Mon, 26 May 1997 12:27:58 +0200 | |
changeset 3330 | ab7161e593c8 |
parent 3302 | 404fe31fd8d2 |
child 3353 | 9112a2efb9a3 |
permissions | -rw-r--r-- |
3302 | 1 |
(* Title: TFL/usyntax |
2 |
ID: $Id$ |
|
3 |
Author: Konrad Slind, Cambridge University Computer Laboratory |
|
4 |
Copyright 1997 University of Cambridge |
|
5 |
||
6 |
Emulation of HOL's abstract syntax functions |
|
7 |
*) |
|
8 |
||
2112 | 9 |
structure USyntax : USyntax_sig = |
10 |
struct |
|
11 |
||
12 |
structure Utils = Utils; |
|
13 |
open Utils; |
|
14 |
open Mask; |
|
15 |
||
16 |
infix 7 |->; |
|
17 |
infix 4 ##; |
|
18 |
||
19 |
fun ERR{func,mesg} = Utils.ERR{module = "USyntax", func = func, mesg = mesg}; |
|
20 |
||
21 |
||
22 |
(*--------------------------------------------------------------------------- |
|
23 |
* |
|
24 |
* Types |
|
25 |
* |
|
26 |
*---------------------------------------------------------------------------*) |
|
27 |
val mk_prim_vartype = TVar; |
|
28 |
fun mk_vartype s = mk_prim_vartype((s,0),["term"]); |
|
29 |
||
30 |
(* But internally, it's useful *) |
|
31 |
fun dest_vtype (TVar x) = x |
|
32 |
| dest_vtype _ = raise ERR{func = "dest_vtype", |
|
33 |
mesg = "not a flexible type variable"}; |
|
34 |
||
35 |
val is_vartype = Utils.can dest_vtype; |
|
36 |
||
37 |
val type_vars = map mk_prim_vartype o typ_tvars |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
38 |
fun type_varsl L = Utils.mk_set (curry op=) |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
39 |
(Utils.rev_itlist (curry op @ o type_vars) L []); |
2112 | 40 |
|
41 |
val alpha = mk_vartype "'a" |
|
42 |
val beta = mk_vartype "'b" |
|
43 |
||
44 |
fun match_type ty1 ty2 = raise ERR{func="match_type",mesg="not implemented"}; |
|
45 |
||
46 |
||
47 |
(* What nonsense *) |
|
48 |
nonfix -->; |
|
49 |
val --> = -->; |
|
50 |
infixr 3 -->; |
|
51 |
||
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
52 |
fun strip_type ty = (binder_types ty, body_type ty); |
2112 | 53 |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
54 |
fun strip_prod_type (Type("*", [ty1,ty2])) = |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
55 |
strip_prod_type ty1 @ strip_prod_type ty2 |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
56 |
| strip_prod_type ty = [ty]; |
2112 | 57 |
|
58 |
||
59 |
||
60 |
(*--------------------------------------------------------------------------- |
|
61 |
* |
|
62 |
* Terms |
|
63 |
* |
|
64 |
*---------------------------------------------------------------------------*) |
|
65 |
nonfix aconv; |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
66 |
val aconv = curry (op aconv); |
2112 | 67 |
|
68 |
fun free_vars tm = add_term_frees(tm,[]); |
|
69 |
||
70 |
||
71 |
(* Free variables, in order of occurrence, from left to right in the |
|
72 |
* syntax tree. *) |
|
73 |
fun free_vars_lr tm = |
|
74 |
let fun memb x = let fun m[] = false | m(y::rst) = (x=y)orelse m rst in m end |
|
75 |
fun add (t, frees) = case t of |
|
76 |
Free _ => if (memb t frees) then frees else t::frees |
|
77 |
| Abs (_,_,body) => add(body,frees) |
|
78 |
| f$t => add(t, add(f, frees)) |
|
79 |
| _ => frees |
|
80 |
in rev(add(tm,[])) |
|
81 |
end; |
|
82 |
||
83 |
||
84 |
||
85 |
fun free_varsl L = Utils.mk_set aconv |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
86 |
(Utils.rev_itlist (curry op @ o free_vars) L []); |
2112 | 87 |
|
88 |
val type_vars_in_term = map mk_prim_vartype o term_tvars; |
|
89 |
||
90 |
(* Can't really be very exact in Isabelle *) |
|
91 |
fun all_vars tm = |
|
92 |
let fun memb x = let fun m[] = false | m(y::rst) = (x=y)orelse m rst in m end |
|
93 |
fun add (t, A) = case t of |
|
94 |
Free _ => if (memb t A) then A else t::A |
|
95 |
| Abs (s,ty,body) => add(body, add(Free(s,ty),A)) |
|
96 |
| f$t => add(t, add(f, A)) |
|
97 |
| _ => A |
|
98 |
in rev(add(tm,[])) |
|
99 |
end; |
|
100 |
fun all_varsl L = Utils.mk_set aconv |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
101 |
(Utils.rev_itlist (curry op @ o all_vars) L []); |
2112 | 102 |
|
103 |
||
104 |
(* Prelogic *) |
|
105 |
val subst = subst_free o map (fn (a |-> b) => (a,b)); |
|
106 |
||
107 |
fun dest_tybinding (v |-> ty) = (#1(dest_vtype v),ty) |
|
108 |
fun inst theta = subst_vars (map dest_tybinding theta,[]) |
|
109 |
||
110 |
fun beta_conv((t1 as Abs _ ) $ t2) = betapply(t1,t2) |
|
111 |
| beta_conv _ = raise ERR{func = "beta_conv", mesg = "Not a beta-redex"}; |
|
112 |
||
113 |
||
114 |
(* Construction routines *) |
|
115 |
fun mk_var{Name,Ty} = Free(Name,Ty); |
|
116 |
val mk_prim_var = Var; |
|
117 |
||
118 |
val string_variant = variant; |
|
119 |
||
120 |
local fun var_name(Var((Name,_),_)) = Name |
|
121 |
| var_name(Free(s,_)) = s |
|
122 |
| var_name _ = raise ERR{func = "variant", |
|
123 |
mesg = "list elem. is not a variable"} |
|
124 |
in |
|
125 |
fun variant [] v = v |
|
126 |
| variant vlist (Var((Name,i),ty)) = |
|
127 |
Var((string_variant (map var_name vlist) Name,i),ty) |
|
128 |
| variant vlist (Free(Name,ty)) = |
|
129 |
Free(string_variant (map var_name vlist) Name,ty) |
|
130 |
| variant _ _ = raise ERR{func = "variant", |
|
131 |
mesg = "2nd arg. should be a variable"} |
|
132 |
end; |
|
133 |
||
134 |
fun mk_const{Name,Ty} = Const(Name,Ty) |
|
135 |
fun mk_comb{Rator,Rand} = Rator $ Rand; |
|
136 |
||
137 |
fun mk_abs{Bvar as Var((s,_),ty),Body} = Abs(s,ty,abstract_over(Bvar,Body)) |
|
138 |
| mk_abs{Bvar as Free(s,ty),Body} = Abs(s,ty,abstract_over(Bvar,Body)) |
|
139 |
| mk_abs _ = raise ERR{func = "mk_abs", mesg = "Bvar is not a variable"}; |
|
140 |
||
141 |
||
142 |
fun mk_imp{ant,conseq} = |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
143 |
let val c = mk_const{Name = "op -->", Ty = HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT} |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
144 |
in list_comb(c,[ant,conseq]) |
2112 | 145 |
end; |
146 |
||
147 |
fun mk_select (r as {Bvar,Body}) = |
|
148 |
let val ty = type_of Bvar |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
149 |
val c = mk_const{Name = "Eps", Ty = (ty --> HOLogic.boolT) --> ty} |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
150 |
in list_comb(c,[mk_abs r]) |
2112 | 151 |
end; |
152 |
||
153 |
fun mk_forall (r as {Bvar,Body}) = |
|
154 |
let val ty = type_of Bvar |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
155 |
val c = mk_const{Name = "All", Ty = (ty --> HOLogic.boolT) --> HOLogic.boolT} |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
156 |
in list_comb(c,[mk_abs r]) |
2112 | 157 |
end; |
158 |
||
159 |
fun mk_exists (r as {Bvar,Body}) = |
|
160 |
let val ty = type_of Bvar |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
161 |
val c = mk_const{Name = "Ex", Ty = (ty --> HOLogic.boolT) --> HOLogic.boolT} |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
162 |
in list_comb(c,[mk_abs r]) |
2112 | 163 |
end; |
164 |
||
165 |
||
166 |
fun mk_conj{conj1,conj2} = |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
167 |
let val c = mk_const{Name = "op &", Ty = HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT} |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
168 |
in list_comb(c,[conj1,conj2]) |
2112 | 169 |
end; |
170 |
||
171 |
fun mk_disj{disj1,disj2} = |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
172 |
let val c = mk_const{Name = "op |", Ty = HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT} |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
173 |
in list_comb(c,[disj1,disj2]) |
2112 | 174 |
end; |
175 |
||
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
176 |
fun prod_ty ty1 ty2 = Type("*", [ty1,ty2]); |
2112 | 177 |
|
178 |
local |
|
179 |
fun mk_uncurry(xt,yt,zt) = |
|
180 |
mk_const{Name = "split", Ty = (xt --> yt --> zt) --> prod_ty xt yt --> zt} |
|
181 |
fun dest_pair(Const("Pair",_) $ M $ N) = {fst=M, snd=N} |
|
182 |
| dest_pair _ = raise ERR{func = "dest_pair", mesg = "not a pair"} |
|
183 |
fun is_var(Var(_)) = true | is_var (Free _) = true | is_var _ = false |
|
184 |
in |
|
185 |
fun mk_pabs{varstruct,body} = |
|
186 |
let fun mpa(varstruct,body) = |
|
187 |
if (is_var varstruct) |
|
188 |
then mk_abs{Bvar = varstruct, Body = body} |
|
189 |
else let val {fst,snd} = dest_pair varstruct |
|
190 |
in mk_comb{Rator= mk_uncurry(type_of fst,type_of snd,type_of body), |
|
191 |
Rand = mpa(fst,mpa(snd,body))} |
|
192 |
end |
|
193 |
in mpa(varstruct,body) |
|
194 |
end |
|
195 |
handle _ => raise ERR{func = "mk_pabs", mesg = ""}; |
|
196 |
end; |
|
197 |
||
198 |
(* Destruction routines *) |
|
199 |
||
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
200 |
datatype lambda = VAR of {Name : string, Ty : typ} |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
201 |
| CONST of {Name : string, Ty : typ} |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
202 |
| COMB of {Rator: term, Rand : term} |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
203 |
| LAMB of {Bvar : term, Body : term}; |
2112 | 204 |
|
205 |
||
206 |
fun dest_term(Var((s,i),ty)) = VAR{Name = s, Ty = ty} |
|
207 |
| dest_term(Free(s,ty)) = VAR{Name = s, Ty = ty} |
|
208 |
| dest_term(Const(s,ty)) = CONST{Name = s, Ty = ty} |
|
209 |
| dest_term(M$N) = COMB{Rator=M,Rand=N} |
|
210 |
| dest_term(Abs(s,ty,M)) = let val v = mk_var{Name = s, Ty = ty} |
|
211 |
in LAMB{Bvar = v, Body = betapply (M,v)} |
|
212 |
end |
|
213 |
| dest_term(Bound _) = raise ERR{func = "dest_term",mesg = "Bound"}; |
|
214 |
||
215 |
fun dest_const(Const(s,ty)) = {Name = s, Ty = ty} |
|
216 |
| dest_const _ = raise ERR{func = "dest_const", mesg = "not a constant"}; |
|
217 |
||
218 |
fun dest_comb(t1 $ t2) = {Rator = t1, Rand = t2} |
|
219 |
| dest_comb _ = raise ERR{func = "dest_comb", mesg = "not a comb"}; |
|
220 |
||
221 |
fun dest_abs(a as Abs(s,ty,M)) = |
|
222 |
let val v = mk_var{Name = s, Ty = ty} |
|
223 |
in {Bvar = v, Body = betapply (a,v)} |
|
224 |
end |
|
225 |
| dest_abs _ = raise ERR{func = "dest_abs", mesg = "not an abstraction"}; |
|
226 |
||
227 |
fun dest_eq(Const("op =",_) $ M $ N) = {lhs=M, rhs=N} |
|
228 |
| dest_eq _ = raise ERR{func = "dest_eq", mesg = "not an equality"}; |
|
229 |
||
230 |
fun dest_imp(Const("op -->",_) $ M $ N) = {ant=M, conseq=N} |
|
231 |
| dest_imp _ = raise ERR{func = "dest_imp", mesg = "not an implication"}; |
|
232 |
||
233 |
fun dest_select(Const("Eps",_) $ (a as Abs _)) = dest_abs a |
|
234 |
| dest_select _ = raise ERR{func = "dest_select", mesg = "not a select"}; |
|
235 |
||
236 |
fun dest_forall(Const("All",_) $ (a as Abs _)) = dest_abs a |
|
237 |
| dest_forall _ = raise ERR{func = "dest_forall", mesg = "not a forall"}; |
|
238 |
||
239 |
fun dest_exists(Const("Ex",_) $ (a as Abs _)) = dest_abs a |
|
240 |
| dest_exists _ = raise ERR{func = "dest_exists", mesg="not an existential"}; |
|
241 |
||
242 |
fun dest_neg(Const("not",_) $ M) = M |
|
243 |
| dest_neg _ = raise ERR{func = "dest_neg", mesg = "not a negation"}; |
|
244 |
||
245 |
fun dest_conj(Const("op &",_) $ M $ N) = {conj1=M, conj2=N} |
|
246 |
| dest_conj _ = raise ERR{func = "dest_conj", mesg = "not a conjunction"}; |
|
247 |
||
248 |
fun dest_disj(Const("op |",_) $ M $ N) = {disj1=M, disj2=N} |
|
249 |
| dest_disj _ = raise ERR{func = "dest_disj", mesg = "not a disjunction"}; |
|
250 |
||
251 |
fun mk_pair{fst,snd} = |
|
252 |
let val ty1 = type_of fst |
|
253 |
val ty2 = type_of snd |
|
254 |
val c = mk_const{Name = "Pair", Ty = ty1 --> ty2 --> prod_ty ty1 ty2} |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
255 |
in list_comb(c,[fst,snd]) |
2112 | 256 |
end; |
257 |
||
258 |
fun dest_pair(Const("Pair",_) $ M $ N) = {fst=M, snd=N} |
|
259 |
| dest_pair _ = raise ERR{func = "dest_pair", mesg = "not a pair"}; |
|
260 |
||
261 |
||
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
262 |
local fun ucheck t = (if #Name(dest_const t) = "split" then t |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
263 |
else raise Match) |
2112 | 264 |
in |
265 |
fun dest_pabs tm = |
|
266 |
let val {Bvar,Body} = dest_abs tm |
|
267 |
in {varstruct = Bvar, body = Body} |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
268 |
end |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
269 |
handle |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
270 |
_ => let val {Rator,Rand} = dest_comb tm |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
271 |
val _ = ucheck Rator |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
272 |
val {varstruct = lv,body} = dest_pabs Rand |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
273 |
val {varstruct = rv,body} = dest_pabs body |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
274 |
in {varstruct = mk_pair{fst = lv, snd = rv}, body = body} |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
275 |
end |
2112 | 276 |
end; |
277 |
||
278 |
||
279 |
(* Garbage - ought to be dropped *) |
|
280 |
val lhs = #lhs o dest_eq |
|
281 |
val rhs = #rhs o dest_eq |
|
282 |
val rator = #Rator o dest_comb |
|
283 |
val rand = #Rand o dest_comb |
|
284 |
val bvar = #Bvar o dest_abs |
|
285 |
val body = #Body o dest_abs |
|
286 |
||
287 |
||
288 |
(* Query routines *) |
|
289 |
val is_comb = can dest_comb |
|
290 |
val is_abs = can dest_abs |
|
291 |
val is_eq = can dest_eq |
|
292 |
val is_imp = can dest_imp |
|
293 |
val is_forall = can dest_forall |
|
294 |
val is_exists = can dest_exists |
|
295 |
val is_neg = can dest_neg |
|
296 |
val is_conj = can dest_conj |
|
297 |
val is_disj = can dest_disj |
|
298 |
val is_pair = can dest_pair |
|
299 |
val is_pabs = can dest_pabs |
|
300 |
||
301 |
||
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
302 |
(* Construction of a cterm from a list of Terms *) |
2112 | 303 |
|
304 |
fun list_mk_abs(L,tm) = itlist (fn v => fn M => mk_abs{Bvar=v, Body=M}) L tm; |
|
305 |
||
306 |
(* These others are almost never used *) |
|
307 |
fun list_mk_imp(A,c) = itlist(fn a => fn tm => mk_imp{ant=a,conseq=tm}) A c; |
|
308 |
fun list_mk_exists(V,t) = itlist(fn v => fn b => mk_exists{Bvar=v, Body=b})V t; |
|
309 |
fun list_mk_forall(V,t) = itlist(fn v => fn b => mk_forall{Bvar=v, Body=b})V t; |
|
310 |
val list_mk_conj = end_itlist(fn c1 => fn tm => mk_conj{conj1=c1, conj2=tm}) |
|
311 |
val list_mk_disj = end_itlist(fn d1 => fn tm => mk_disj{disj1=d1, disj2=tm}) |
|
312 |
||
313 |
||
314 |
(* Need to reverse? *) |
|
315 |
fun gen_all tm = list_mk_forall(free_vars tm, tm); |
|
316 |
||
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
317 |
(* Destructing a cterm to a list of Terms *) |
2112 | 318 |
fun strip_comb tm = |
319 |
let fun dest(M$N, A) = dest(M, N::A) |
|
320 |
| dest x = x |
|
321 |
in dest(tm,[]) |
|
322 |
end; |
|
323 |
||
324 |
fun strip_abs(tm as Abs _) = |
|
325 |
let val {Bvar,Body} = dest_abs tm |
|
326 |
val (bvs, core) = strip_abs Body |
|
327 |
in (Bvar::bvs, core) |
|
328 |
end |
|
329 |
| strip_abs M = ([],M); |
|
330 |
||
331 |
||
332 |
fun strip_imp fm = |
|
333 |
if (is_imp fm) |
|
334 |
then let val {ant,conseq} = dest_imp fm |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
335 |
val (was,wb) = strip_imp conseq |
2112 | 336 |
in ((ant::was), wb) |
337 |
end |
|
338 |
else ([],fm); |
|
339 |
||
340 |
fun strip_forall fm = |
|
341 |
if (is_forall fm) |
|
342 |
then let val {Bvar,Body} = dest_forall fm |
|
343 |
val (bvs,core) = strip_forall Body |
|
344 |
in ((Bvar::bvs), core) |
|
345 |
end |
|
346 |
else ([],fm); |
|
347 |
||
348 |
||
349 |
fun strip_exists fm = |
|
350 |
if (is_exists fm) |
|
351 |
then let val {Bvar, Body} = dest_exists fm |
|
352 |
val (bvs,core) = strip_exists Body |
|
353 |
in (Bvar::bvs, core) |
|
354 |
end |
|
355 |
else ([],fm); |
|
356 |
||
357 |
fun strip_conj w = |
|
358 |
if (is_conj w) |
|
359 |
then let val {conj1,conj2} = dest_conj w |
|
360 |
in (strip_conj conj1@strip_conj conj2) |
|
361 |
end |
|
362 |
else [w]; |
|
363 |
||
364 |
fun strip_disj w = |
|
365 |
if (is_disj w) |
|
366 |
then let val {disj1,disj2} = dest_disj w |
|
367 |
in (strip_disj disj1@strip_disj disj2) |
|
368 |
end |
|
369 |
else [w]; |
|
370 |
||
371 |
fun strip_pair tm = |
|
372 |
if (is_pair tm) |
|
373 |
then let val {fst,snd} = dest_pair tm |
|
374 |
fun dtuple t = |
|
375 |
if (is_pair t) |
|
376 |
then let val{fst,snd} = dest_pair t |
|
377 |
in (fst :: dtuple snd) |
|
378 |
end |
|
379 |
else [t] |
|
380 |
in fst::dtuple snd |
|
381 |
end |
|
382 |
else [tm]; |
|
383 |
||
384 |
||
385 |
fun mk_preterm tm = #t(rep_cterm tm); |
|
386 |
||
387 |
(* Miscellaneous *) |
|
388 |
||
389 |
fun mk_vstruct ty V = |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
390 |
let fun follow_prod_type (Type("*",[ty1,ty2])) vs = |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
391 |
let val (ltm,vs1) = follow_prod_type ty1 vs |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
392 |
val (rtm,vs2) = follow_prod_type ty2 vs1 |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
393 |
in (mk_pair{fst=ltm, snd=rtm}, vs2) end |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
394 |
| follow_prod_type _ (v::vs) = (v,vs) |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
395 |
in #1 (follow_prod_type ty V) end; |
2112 | 396 |
|
397 |
||
398 |
(* Search a term for a sub-term satisfying the predicate p. *) |
|
399 |
fun find_term p = |
|
400 |
let fun find tm = |
|
401 |
if (p tm) |
|
402 |
then tm |
|
403 |
else if (is_abs tm) |
|
404 |
then find (#Body(dest_abs tm)) |
|
405 |
else let val {Rator,Rand} = dest_comb tm |
|
406 |
in find Rator handle _ => find Rand |
|
407 |
end handle _ => raise ERR{func = "find_term",mesg = ""} |
|
408 |
in find |
|
409 |
end; |
|
410 |
||
411 |
(******************************************************************* |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
412 |
* find_terms: (term -> HOLogic.boolT) -> term -> term list |
2112 | 413 |
* |
414 |
* Find all subterms in a term that satisfy a given predicate p. |
|
415 |
* |
|
416 |
*******************************************************************) |
|
417 |
fun find_terms p = |
|
418 |
let fun accum tl tm = |
|
419 |
let val tl' = if (p tm) then (tm::tl) else tl |
|
420 |
in if (is_abs tm) |
|
421 |
then accum tl' (#Body(dest_abs tm)) |
|
422 |
else let val {Rator,Rand} = dest_comb tm |
|
423 |
in accum (accum tl' Rator) Rand |
|
424 |
end handle _ => tl' |
|
425 |
end |
|
426 |
in accum [] |
|
427 |
end; |
|
428 |
||
429 |
||
430 |
fun dest_relation tm = |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
431 |
if (type_of tm = HOLogic.boolT) |
2112 | 432 |
then let val (Const("op :",_) $ (Const("Pair",_)$y$x) $ R) = tm |
433 |
in (R,y,x) |
|
434 |
end handle _ => raise ERR{func="dest_relation", |
|
435 |
mesg="unexpected term structure"} |
|
436 |
else raise ERR{func="dest_relation",mesg="not a boolean term"}; |
|
437 |
||
438 |
fun is_WFR tm = (#Name(dest_const(rator tm)) = "wf") handle _ => false; |
|
439 |
||
440 |
fun ARB ty = mk_select{Bvar=mk_var{Name="v",Ty=ty}, |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
2112
diff
changeset
|
441 |
Body=mk_const{Name="True",Ty=HOLogic.boolT}}; |
2112 | 442 |
|
443 |
end; (* Syntax *) |