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