1 (* extends Isabelle/src/Pure/term.ML |
|
2 (c) Walther Neuper 1999 |
|
3 |
|
4 use"ProgLang/term.sml"; |
|
5 use"term.sml"; |
|
6 *) |
|
7 |
|
8 (* |
|
9 > (cterm_of thy) a_term; |
|
10 val it = "empty" : cterm *) |
|
11 |
|
12 (*2003 fun match thy t pat = |
|
13 (snd (Pattern.match (Sign.tsig_of (sign_of thy)) (pat, t))) |
|
14 handle _ => []; |
|
15 fn : theory -> |
|
16 Term.term -> Term.term -> (Term.indexname * Term.term) list*) |
|
17 (*see src/Tools/eqsubst.ML fun clean_match*) |
|
18 (*2003 fun matches thy tm pa = if match thy tm pa = [] then false else true;*) |
|
19 fun matches thy tm pa = |
|
20 (Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true) |
|
21 handle _ => false |
|
22 |
|
23 fun atomtyp t = (*WN10 see raw_pp_typ*) |
|
24 let |
|
25 fun ato n (Type (s,[])) = |
|
26 ("\n*** "^indent n^"Type ("^s^",[])") |
|
27 | ato n (Type (s,Ts)) = |
|
28 ("\n*** "^indent n^"Type ("^s^",["^ atol (n+1) Ts) |
|
29 |
|
30 | ato n (TFree (s,sort)) = |
|
31 ("\n*** "^indent n^"TFree ("^s^",["^ strs2str' sort) |
|
32 |
|
33 | ato n (TVar ((s,i),sort)) = |
|
34 ("\n*** "^indent n^"TVar (("^s^","^ |
|
35 string_of_int i ^ strs2str' sort) |
|
36 and atol n [] = |
|
37 ("\n*** "^indent n^"]") |
|
38 | atol n (T::Ts) = (ato n T ^ atol n Ts) |
|
39 (*in print (ato 0 t ^ "\n") end; TODO TUM10*) |
|
40 in tracing (ato 0 t) end; |
|
41 (* |
|
42 > val T = (type_of o term_of o the o (parse thy)) "a::[real,int] => nat"; |
|
43 > atomtyp T; |
|
44 *** Type (fun,[ |
|
45 *** Type (RealDef.real,[]) |
|
46 *** Type (fun,[ |
|
47 *** Type (IntDef.int,[]) |
|
48 *** Type (nat,[]) |
|
49 *** ] |
|
50 *** ] |
|
51 *) |
|
52 |
|
53 (*Prog.Tutorial.p.34, Makarius 1005 does the above like this..*) |
|
54 local |
|
55 fun pp_pair (x, y) = Pretty.list "(" ")" [x, y] |
|
56 fun pp_list xs = Pretty.list "[" "]" xs |
|
57 fun pp_str s = Pretty.str s |
|
58 fun pp_qstr s = Pretty.quote (pp_str s) |
|
59 fun pp_int i = pp_str (string_of_int i) |
|
60 fun pp_sort S = pp_list (map pp_qstr S) |
|
61 fun pp_constr a args = Pretty.block [pp_str a, Pretty.brk 1, args] |
|
62 in |
|
63 fun raw_pp_typ (TVar ((a, i), S)) = |
|
64 pp_constr "TVar" (pp_pair (pp_pair (pp_qstr a, pp_int i), pp_sort S)) |
|
65 | raw_pp_typ (TFree (a, S)) = |
|
66 pp_constr "TFree" (pp_pair (pp_qstr a, pp_sort S)) |
|
67 | raw_pp_typ (Type (a, tys)) = |
|
68 pp_constr "Type" (pp_pair (pp_qstr a, pp_list (map raw_pp_typ tys))) |
|
69 end |
|
70 (* install |
|
71 PolyML.addPrettyPrinter |
|
72 (fn _ => fn _ => ml_pretty o Pretty.to_ML o raw_pp_typ); |
|
73 de-install |
|
74 PolyML.addPrettyPrinter |
|
75 (fn _ => fn _ => ml_pretty o Pretty.to_ML o Proof_Display.pp_typ Pure.thy); |
|
76 *) |
|
77 |
|
78 fun atomt t = |
|
79 let fun ato (Const (a, _)) n = |
|
80 "\n*** " ^ indent n ^ "Const (" ^ a ^ ", _)" |
|
81 | ato (Free (a, _)) n = |
|
82 "\n*** " ^ indent n ^ "Free (" ^ a ^ ", _)" |
|
83 | ato (Var ((a, i), _)) n = |
|
84 "\n*** " ^ indent n ^ "Var (" ^ a ^ ", " ^ |
|
85 string_of_int i ^ "), _)" |
|
86 | ato (Bound i) n = |
|
87 "\n*** " ^ indent n ^ "Bound " ^ string_of_int i |
|
88 | ato (Abs (a, _, body)) n = |
|
89 "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1) |
|
90 | ato (f $ t) n = (ato f n ^ ato t (n + 1)) |
|
91 in tracing ("\n*** -------------" ^ ato t 0 ^ "\n***") end; |
|
92 |
|
93 fun term_detail2str t = |
|
94 let fun ato (Const (a, T)) n = |
|
95 "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ string_of_typ T ^ ")" |
|
96 | ato (Free (a, T)) n = |
|
97 "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ string_of_typ T ^ ")" |
|
98 | ato (Var ((a, i), T)) n = |
|
99 "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), "^ |
|
100 string_of_typ T ^ ")" |
|
101 | ato (Bound i) n = |
|
102 "\n*** " ^ indent n ^ "Bound " ^ string_of_int i |
|
103 | ato (Abs(a, T, body)) n = |
|
104 "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ string_of_typ T ^ ",.." |
|
105 ^ ato body (n + 1) |
|
106 | ato (f $ t) n = ato f n ^ ato t (n + 1) |
|
107 in "\n*** " ^ ato t 0 ^ "\n***" end; |
|
108 fun atomty t = (tracing o term_detail2str) t; (*WN100907 broken*) |
|
109 |
|
110 fun term_str thy (Const(s,_)) = s |
|
111 | term_str thy (Free(s,_)) = s |
|
112 | term_str thy (Var((s,i),_)) = s^(string_of_int i) |
|
113 | term_str thy (Bound i) = "B."^(string_of_int i) |
|
114 | term_str thy (Abs(s,_,_)) = s |
|
115 | term_str thy t = raise error("term_str not for "^term2str t); |
|
116 |
|
117 (*.contains the fst argument the second argument (a leave! of term).*) |
|
118 fun contains_term (Abs(_,_,body)) t = contains_term body t |
|
119 | contains_term (f $ f') t = |
|
120 contains_term f t orelse contains_term f' t |
|
121 | contains_term s t = t = s; |
|
122 (*.contains the term a VAR(("*",_),_) ?.*) |
|
123 fun contains_Var (Abs(_,_,body)) = contains_Var body |
|
124 | contains_Var (f $ f') = contains_Var f orelse contains_Var f' |
|
125 | contains_Var (Var _) = true |
|
126 | contains_Var _ = false; |
|
127 (* contains_Var (str2term "?z = 3") (*true*); |
|
128 contains_Var (str2term "z = 3") (*false*); |
|
129 *) |
|
130 |
|
131 (*fun int_of_str str = |
|
132 let val ss = explode str |
|
133 val str' = case ss of |
|
134 "("::s => drop_last s | _ => ss |
|
135 in case BasisLibrary.Int.fromString (implode str') of |
|
136 SOME i => SOME i |
|
137 | NONE => NONE end;*) |
|
138 fun int_of_str str = |
|
139 let val ss = explode str |
|
140 val str' = case ss of |
|
141 "("::s => drop_last s | _ => ss |
|
142 in (SOME (Thy_Output.integer (implode str'))) handle _ => NONE end; |
|
143 (* |
|
144 > int_of_str "123"; |
|
145 val it = SOME 123 : int option |
|
146 > int_of_str "(-123)"; |
|
147 val it = SOME 123 : int option |
|
148 > int_of_str "#123"; |
|
149 val it = NONE : int option |
|
150 > int_of_str "-123"; |
|
151 val it = SOME ~123 : int option |
|
152 *) |
|
153 fun int_of_str' str = |
|
154 case int_of_str str of |
|
155 SOME i => i |
|
156 | NONE => raise TERM ("int_of_string: no int-string",[]); |
|
157 val str2int = int_of_str'; |
|
158 |
|
159 fun is_numeral str = case int_of_str str of |
|
160 SOME _ => true |
|
161 | NONE => false; |
|
162 val is_no = is_numeral; |
|
163 fun is_num (Free (s,_)) = if is_numeral s then true else false |
|
164 | is_num _ = false; |
|
165 (*> |
|
166 > is_num ((term_of o the o (parse thy)) "#1"); |
|
167 val it = true : bool |
|
168 > is_num ((term_of o the o (parse thy)) "#-1"); |
|
169 val it = true : bool |
|
170 > is_num ((term_of o the o (parse thy)) "a123"); |
|
171 val it = false : bool |
|
172 *) |
|
173 |
|
174 (*fun int_of_Free (Free (intstr, _)) = |
|
175 (case BasisLibrary.Int.fromString intstr of |
|
176 SOME i => i |
|
177 | NONE => raise error ("int_of_Free ( "^ intstr ^", _)")) |
|
178 | int_of_Free t = raise error ("int_of_Free ( "^ term2str t ^" )");*) |
|
179 fun int_of_Free (Free (intstr, _)) = (Thy_Output.integer intstr |
|
180 handle _ => raise error ("int_of_Free ( "^ intstr ^", _)")) |
|
181 | int_of_Free t = raise error ("int_of_Free ( "^ term2str t ^" )"); |
|
182 |
|
183 fun vars t = |
|
184 let |
|
185 fun scan vs (Const(s,T)) = vs |
|
186 | scan vs (t as Free(s,T)) = if is_no s then vs else t::vs |
|
187 | scan vs (t as Var((s,i),T)) = t::vs |
|
188 | scan vs (Bound i) = vs |
|
189 | scan vs (Abs(s,T,t)) = scan vs t |
|
190 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2) |
|
191 in (distinct o (scan [])) t end; |
|
192 |
|
193 fun is_Free (Free _) = true |
|
194 | is_Free _ = false; |
|
195 fun is_fun_id (Const _) = true |
|
196 | is_fun_id (Free _) = true |
|
197 | is_fun_id _ = false; |
|
198 fun is_f_x (f $ x) = is_fun_id f andalso is_Free x |
|
199 | is_f_x _ = false; |
|
200 (* is_f_x (str2term "q_0/2 * L * x") (*false*); |
|
201 is_f_x (str2term "M_b x") (*true*); |
|
202 *) |
|
203 fun vars_str t = |
|
204 let |
|
205 fun scan vs (Const(s,T)) = vs |
|
206 | scan vs (t as Free(s,T)) = if is_no s then vs else s::vs |
|
207 | scan vs (t as Var((s,i),T)) = (s^"_"^(string_of_int i))::vs |
|
208 | scan vs (Bound i) = vs |
|
209 | scan vs (Abs(s,T,t)) = scan vs t |
|
210 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2) |
|
211 in (distinct o (scan [])) t end; |
|
212 |
|
213 fun ids2str t = |
|
214 let |
|
215 fun scan vs (Const(s,T)) = if is_no s then vs else s::vs |
|
216 | scan vs (t as Free(s,T)) = if is_no s then vs else s::vs |
|
217 | scan vs (t as Var((s,i),T)) = (s^"_"^(string_of_int i))::vs |
|
218 | scan vs (Bound i) = vs |
|
219 | scan vs (Abs(s,T,t)) = scan (s::vs) t |
|
220 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2) |
|
221 in (distinct o (scan [])) t end; |
|
222 fun is_bdv str = |
|
223 case explode str of |
|
224 "b"::"d"::"v"::_ => true |
|
225 | _ => false; |
|
226 fun is_bdv_ (Free (s,_)) = is_bdv s |
|
227 | is_bdv_ _ = false; |
|
228 |
|
229 fun free2str (Free (s,_)) = s |
|
230 | free2str t = raise error ("free2str not for "^ term2str t); |
|
231 fun free2int (t as Free (s, _)) = ((str2int s) |
|
232 handle _ => raise error ("free2int: "^term_detail2str t)) |
|
233 | free2int t = raise error ("free2int: "^term_detail2str t); |
|
234 |
|
235 (*27.8.01: unused*) |
|
236 fun var2free (t as Const(s,T)) = t |
|
237 | var2free (t as Free(s,T)) = t |
|
238 | var2free (Var((s,i),T)) = Free(s,T) |
|
239 | var2free (t as Bound i) = t |
|
240 | var2free (Abs(s,T,t)) = Abs(s,T,var2free t) |
|
241 | var2free (t1 $ t2) = (var2free t1) $ (var2free t2); |
|
242 |
|
243 (*27.8.01: doesn't find some subterm ???!???*) |
|
244 (*2010 Logic.varify !!!*) |
|
245 fun free2var (t as Const(s,T)) = t |
|
246 | free2var (t as Free(s,T)) = if is_no s then t else Var((s,0),T) |
|
247 | free2var (t as Var((s,i),T)) = t |
|
248 | free2var (t as Bound i) = t |
|
249 | free2var (Abs(s,T,t)) = Abs(s,T,free2var t) |
|
250 | free2var (t1 $ t2) = (free2var t1) $ (free2var t2); |
|
251 |
|
252 |
|
253 fun mk_listT T = Type ("List.list", [T]); |
|
254 fun list_const T = |
|
255 Const("List.list.Cons", [T, mk_listT T] ---> mk_listT T); |
|
256 (*28.8.01: TODO: get type from head of list: 1 arg less!!!*) |
|
257 fun list2isalist T [] = Const("List.list.Nil",mk_listT T) |
|
258 | list2isalist T (t::ts) = (list_const T) $ t $ (list2isalist T ts); |
|
259 (* |
|
260 > val tt = (term_of o the o (parse thy)) "R=(R::real)"; |
|
261 > val TT = type_of tt; |
|
262 > val ss = list2isalist TT [tt,tt,tt]; |
|
263 > (cterm_of thy) ss; |
|
264 val it = "[R = R, R = R, R = R]" : cterm *) |
|
265 |
|
266 fun isapair2pair (Const ("Pair",_) $ a $ b) = (a,b) |
|
267 | isapair2pair t = |
|
268 raise error ("isapair2pair called with "^term2str t); |
|
269 |
|
270 val listType = Type ("List.list",[Type ("bool",[])]); |
|
271 fun isalist2list ls = |
|
272 let |
|
273 fun get es (Const("List.list.Cons",_) $ t $ ls) = get (t::es) ls |
|
274 | get es (Const("List.list.Nil",_)) = es |
|
275 | get _ t = |
|
276 raise error ("isalist2list applied to NON-list '"^term2str t^"'") |
|
277 in (rev o (get [])) ls end; |
|
278 (* |
|
279 > val il = str2term "[a=b,c=d,e=f]"; |
|
280 > val l = isalist2list il; |
|
281 > (tracing o terms2str) l; |
|
282 ["a = b","c = d","e = f"] |
|
283 |
|
284 > val il = str2term "ss___::bool list"; |
|
285 > val l = isalist2list il; |
|
286 [Free ("ss___", "bool List.list")] |
|
287 *) |
|
288 |
|
289 |
|
290 (*review Isabelle2009/src/HOL/Tools/hologic.ML*) |
|
291 val prop = Type ("prop",[]); (* ~/Diss.99/Integers-Isa/tools.sml*) |
|
292 val bool = Type ("bool",[]); (* 2002 Integ.int *) |
|
293 val Trueprop = Const("Trueprop",bool-->prop); |
|
294 fun mk_prop t = Trueprop $ t; |
|
295 val true_as_term = Const("True",bool); |
|
296 val false_as_term = Const("False",bool); |
|
297 val true_as_cterm = cterm_of (theory "HOL") true_as_term; |
|
298 val false_as_cterm = cterm_of (theory "HOL") false_as_term; |
|
299 |
|
300 infixr 5 -->; (*2002 /Pure/term.ML *) |
|
301 infixr --->; (*2002 /Pure/term.ML *) |
|
302 fun S --> T = Type("fun",[S,T]); (*2002 /Pure/term.ML *) |
|
303 val op ---> = foldr (op -->); (*2002 /Pure/term.ML *) |
|
304 fun list_implies ([], B) = B : term (*2002 /term.ML *) |
|
305 | list_implies (A::AS, B) = Logic.implies $ A $ list_implies(AS,B); |
|
306 |
|
307 |
|
308 |
|
309 (** substitution **) |
|
310 |
|
311 fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) = (* = thm.ML *) |
|
312 match_bvs(s, t, if x="" orelse y="" then al |
|
313 else (x,y)::al) |
|
314 | match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al)) |
|
315 | match_bvs(_,_,al) = al; |
|
316 fun ren_inst(insts,prop,pat,obj) = (* = thm.ML *) |
|
317 let val ren = match_bvs(pat,obj,[]) |
|
318 fun renAbs(Abs(x,T,b)) = |
|
319 Abs(case assoc_string(ren,x) of NONE => x |
|
320 | SOME(y) => y, T, renAbs(b)) |
|
321 | renAbs(f$t) = renAbs(f) $ renAbs(t) |
|
322 | renAbs(t) = t |
|
323 in subst_vars insts (if null(ren) then prop else renAbs(prop)) end; |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 fun dest_equals' (Const("op =",_) $ t $ u) = (t,u)(* logic.ML: Const("=="*) |
|
331 | dest_equals' t = raise TERM("dest_equals'", [t]); |
|
332 val lhs_ = (fst o dest_equals'); |
|
333 val rhs_ = (snd o dest_equals'); |
|
334 |
|
335 fun is_equality (Const("op =",_) $ t $ u) = true (* logic.ML: Const("=="*) |
|
336 | is_equality _ = false; |
|
337 fun mk_equality (t,u) = (Const("op =",[type_of t,type_of u]--->bool) $ t $ u); |
|
338 fun is_expliceq (Const("op =",_) $ (Free _) $ u) = true |
|
339 | is_expliceq _ = false; |
|
340 fun strip_trueprop (Const("Trueprop",_) $ t) = t |
|
341 | strip_trueprop t = t; |
|
342 (* | strip_trueprop t = raise TERM("strip_trueprop", [t]); |
|
343 *) |
|
344 |
|
345 (*.(A1==>...An==>B) goes to (A1==>...An==>).*) |
|
346 fun strip_imp_prems' (Const("==>", T) $ A $ t) = |
|
347 let fun coll_prems As (Const("==>", _) $ A $ t) = |
|
348 coll_prems (As $ (Logic.implies $ A)) t |
|
349 | coll_prems As _ = SOME As |
|
350 in coll_prems (Logic.implies $ A) t end |
|
351 | strip_imp_prems' _ = NONE; (* logic.ML: term -> term list*) |
|
352 (* |
|
353 val thm = real_mult_div_cancel1; |
|
354 val prop = (#prop o rep_thm) thm; |
|
355 atomt prop; |
|
356 *** ------------- |
|
357 *** Const ( ==>) |
|
358 *** . Const ( Trueprop) |
|
359 *** . . Const ( Not) |
|
360 *** . . . Const ( op =) |
|
361 *** . . . . Var ((k, 0), ) |
|
362 *** . . . . Const ( 0) |
|
363 *** . Const ( Trueprop) |
|
364 *** . . Const ( op =) *** ............. |
|
365 val SOME t = strip_imp_prems' ((#prop o rep_thm) thm); |
|
366 atomt t; |
|
367 *** ------------- |
|
368 *** Const ( ==>) |
|
369 *** . Const ( Trueprop) |
|
370 *** . . Const ( Not) |
|
371 *** . . . Const ( op =) |
|
372 *** . . . . Var ((k, 0), ) |
|
373 *** . . . . Const ( 0) |
|
374 |
|
375 val thm = real_le_anti_sym; |
|
376 val prop = (#prop o rep_thm) thm; |
|
377 atomt prop; |
|
378 *** ------------- |
|
379 *** Const ( ==>) |
|
380 *** . Const ( Trueprop) |
|
381 *** . . Const ( op <=) |
|
382 *** . . . Var ((z, 0), ) |
|
383 *** . . . Var ((w, 0), ) |
|
384 *** . Const ( ==>) |
|
385 *** . . Const ( Trueprop) |
|
386 *** . . . Const ( op <=) |
|
387 *** . . . . Var ((w, 0), ) |
|
388 *** . . . . Var ((z, 0), ) |
|
389 *** . . Const ( Trueprop) |
|
390 *** . . . Const ( op =) |
|
391 *** ............. |
|
392 val SOME t = strip_imp_prems' ((#prop o rep_thm) thm); |
|
393 atomt t; |
|
394 *** ------------- |
|
395 *** Const ( ==>) |
|
396 *** . Const ( Trueprop) |
|
397 *** . . Const ( op <=) |
|
398 *** . . . Var ((z, 0), ) |
|
399 *** . . . Var ((w, 0), ) |
|
400 *** . Const ( ==>) |
|
401 *** . . Const ( Trueprop) |
|
402 *** . . . Const ( op <=) |
|
403 *** . . . . Var ((w, 0), ) |
|
404 *** . . . . Var ((z, 0), ) |
|
405 *) |
|
406 |
|
407 (*. (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch.*) |
|
408 fun ins_concl (Const("==>", T) $ A $ t) B = Logic.implies $ A $ (ins_concl t B) |
|
409 | ins_concl (Const("==>", T) $ A ) B = Logic.implies $ A $ B |
|
410 | ins_concl t B = raise TERM("ins_concl", [t, B]); |
|
411 (* |
|
412 val thm = real_le_anti_sym; |
|
413 val prop = (#prop o rep_thm) thm; |
|
414 val concl = Logic.strip_imp_concl prop; |
|
415 val SOME prems = strip_imp_prems' prop; |
|
416 val prop' = ins_concl prems concl; |
|
417 prop = prop'; |
|
418 atomt prop; |
|
419 atomt prop'; |
|
420 *) |
|
421 |
|
422 |
|
423 fun vperm (Var _, Var _) = true (*2002 Pure/thm.ML *) |
|
424 | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t) |
|
425 | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2) |
|
426 | vperm (t, u) = (t = u); |
|
427 |
|
428 (*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*) |
|
429 fun mem_term (_, []) = false |
|
430 | mem_term (t, t'::ts) = t aconv t' orelse mem_term(t,ts); |
|
431 fun subset_term ([], ys) = true |
|
432 | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term(xs, ys); |
|
433 fun eq_set_term (xs, ys) = |
|
434 xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs)); |
|
435 (*a total, irreflexive ordering on index names*) |
|
436 fun xless ((a,i), (b,j): indexname) = i<j orelse (i=j andalso a<b); |
|
437 (*a partial ordering (not reflexive) for atomic terms*) |
|
438 fun atless (Const (a,_), Const (b,_)) = a<b |
|
439 | atless (Free (a,_), Free (b,_)) = a<b |
|
440 | atless (Var(v,_), Var(w,_)) = xless(v,w) |
|
441 | atless (Bound i, Bound j) = i<j |
|
442 | atless _ = false; |
|
443 (*insert atomic term into partially sorted list, suppressing duplicates (?)*) |
|
444 fun insert_aterm (t,us) = |
|
445 let fun inserta [] = [t] |
|
446 | inserta (us as u::us') = |
|
447 if atless(t,u) then t::us |
|
448 else if t=u then us (*duplicate*) |
|
449 else u :: inserta(us') |
|
450 in inserta us end; |
|
451 |
|
452 (*Accumulates the Vars in the term, suppressing duplicates*) |
|
453 fun add_term_vars (t, vars: term list) = case t of |
|
454 Var _ => insert_aterm(t,vars) |
|
455 | Abs (_,_,body) => add_term_vars(body,vars) |
|
456 | f$t => add_term_vars (f, add_term_vars(t, vars)) |
|
457 | _ => vars; |
|
458 fun term_vars t = add_term_vars(t,[]); |
|
459 |
|
460 |
|
461 fun var_perm (t, u) = (*2002 Pure/thm.ML *) |
|
462 vperm (t, u) andalso eq_set_term (term_vars t, term_vars u); |
|
463 |
|
464 (*2002 fun decomp_simp, Pure/thm.ML *) |
|
465 fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs) |
|
466 andalso not (is_Var lhs); |
|
467 |
|
468 |
|
469 fun str_of_int n = |
|
470 if n < 0 then "-"^((string_of_int o abs) n) |
|
471 else string_of_int n; |
|
472 (* |
|
473 > str_of_int 1; |
|
474 val it = "1" : string > str_of_int ~1; |
|
475 val it = "-1" : string |
|
476 *) |
|
477 |
|
478 |
|
479 fun power b 0 = 1 |
|
480 | power b n = |
|
481 if n>0 then b*(power b (n-1)) |
|
482 else raise error ("power "^(str_of_int b)^" "^(str_of_int n)); |
|
483 (* |
|
484 > power 2 3; |
|
485 val it = 8 : int |
|
486 > power ~2 3; |
|
487 val it = ~8 : int |
|
488 > power ~3 2; |
|
489 val it = 9 : int |
|
490 > power 3 ~2; |
|
491 *) |
|
492 fun gcd 0 b = b |
|
493 | gcd a b = if a < b then gcd (b mod a) a |
|
494 else gcd (a mod b) b; |
|
495 fun sign n = if n < 0 then ~1 |
|
496 else if n = 0 then 0 else 1; |
|
497 fun sign2 n1 n2 = (sign n1) * (sign n2); |
|
498 |
|
499 infix dvd; |
|
500 fun d dvd n = n mod d = 0; |
|
501 |
|
502 fun divisors n = |
|
503 let fun pdiv ds d n = |
|
504 if d=n then d::ds |
|
505 else if d dvd n then pdiv (d::ds) d (n div d) |
|
506 else pdiv ds (d+1) n |
|
507 in pdiv [] 2 n end; |
|
508 |
|
509 divisors 30; |
|
510 divisors 32; |
|
511 divisors 60; |
|
512 divisors 11; |
|
513 |
|
514 fun doubles ds = (* ds is ordered *) |
|
515 let fun dbls ds [] = ds |
|
516 | dbls ds [i] = ds |
|
517 | dbls ds (i::i'::is) = if i=i' then dbls (i::ds) is |
|
518 else dbls ds (i'::is) |
|
519 in dbls [] ds end; |
|
520 (*> doubles [2,3,4]; |
|
521 val it = [] : int list |
|
522 > doubles [2,3,3,5,5,7]; |
|
523 val it = [5,3] : int list*) |
|
524 |
|
525 fun squfact 0 = 0 |
|
526 | squfact 1 = 1 |
|
527 | squfact n = foldl op* (1, (doubles o divisors) n); |
|
528 (*> squfact 30; |
|
529 val it = 1 : int |
|
530 > squfact 32; |
|
531 val it = 4 : int |
|
532 > squfact 60; |
|
533 val it = 2 : int |
|
534 > squfact 11; |
|
535 val it = 1 : int*) |
|
536 |
|
537 |
|
538 fun dest_type (Type(T,[])) = T |
|
539 | dest_type T = |
|
540 (atomtyp T; |
|
541 raise error ("... dest_type: not impl. for this type")); |
|
542 |
|
543 fun term_of_num ntyp n = Free (str_of_int n, ntyp); |
|
544 |
|
545 fun pairT T1 T2 = Type ("*", [T1, T2]); |
|
546 (*> val t = str2term "(1,2)"; |
|
547 > type_of t = pairT HOLogic.realT HOLogic.realT; |
|
548 val it = true : bool |
|
549 *) |
|
550 fun PairT T1 T2 = ([T1, T2] ---> Type ("*", [T1, T2])); |
|
551 (*> val t = str2term "(1,2)"; |
|
552 > val Const ("Pair",pT) $ _ $ _ = t; |
|
553 > pT = PairT HOLogic.realT HOLogic.realT; |
|
554 val it = true : bool |
|
555 *) |
|
556 fun pairt t1 t2 = |
|
557 Const ("Pair", PairT (type_of t1) (type_of t2)) $ t1 $ t2; |
|
558 (*> val t = str2term "(1,2)"; |
|
559 > val (t1, t2) = (str2term "1", str2term "2"); |
|
560 > t = pairt t1 t2; |
|
561 val it = true : bool*) |
|
562 |
|
563 |
|
564 fun num_of_term (t as Free (s,_)) = |
|
565 (case int_of_str s of |
|
566 SOME s' => s' |
|
567 | NONE => raise error ("num_of_term not for "^ term2str t)) |
|
568 | num_of_term t = raise error ("num_of_term not for "^term2str t); |
|
569 |
|
570 fun mk_factroot op_(*=thy.sqrt*) T fact root = |
|
571 Const ("op *", [T, T] ---> T) $ (term_of_num T fact) $ |
|
572 (Const (op_, T --> T) $ term_of_num T root); |
|
573 (* |
|
574 val T = (type_of o term_of o the) (parse thy "#12::real"); |
|
575 val t = mk_factroot "SqRoot.sqrt" T 2 3; |
|
576 (cterm_of thy) t; |
|
577 val it = "#2 * sqrt #3 " : cterm |
|
578 *) |
|
579 fun var_op_num v op_ optype ntyp n = |
|
580 Const (op_, optype) $ v $ |
|
581 Free (str_of_int n, ntyp); |
|
582 |
|
583 fun num_op_var v op_ optype ntyp n = |
|
584 Const (op_,optype) $ |
|
585 Free (str_of_int n, ntyp) $ v; |
|
586 |
|
587 fun num_op_num T1 T2 (op_,Top) n1 n2 = |
|
588 Const (op_,Top) $ |
|
589 Free (str_of_int n1, T1) $ Free (str_of_int n2, T2); |
|
590 (* |
|
591 > val t = num_op_num "Int" 3 4; |
|
592 > atomty t; |
|
593 > string_of_cterm ((cterm_of thy) t); |
|
594 *) |
|
595 |
|
596 fun const_in str (Const _) = false |
|
597 | const_in str (Free (s,_)) = if strip_thy s = str then true else false |
|
598 | const_in str (Bound _) = false |
|
599 | const_in str (Var _) = false |
|
600 | const_in str (Abs (_,_,body)) = const_in str body |
|
601 | const_in str (f$u) = const_in str f orelse const_in str u; |
|
602 (* |
|
603 > val t = (term_of o the o (parse thy)) "6 + 5 * sqrt 4 + 3"; |
|
604 > const_in "sqrt" t; |
|
605 val it = true : bool |
|
606 > val t = (term_of o the o (parse thy)) "6 + 5 * 4 + 3"; |
|
607 > const_in "sqrt" t; |
|
608 val it = false : bool |
|
609 *) |
|
610 |
|
611 (*used for calculating built in binary operations in Isabelle2002->Float.ML*) |
|
612 (*fun calc "Groups.plus_class.plus" (n1, n2) = n1+n2 |
|
613 | calc "Groups.minus_class.minus" (n1, n2) = n1-n2 |
|
614 | calc "op *" (n1, n2) = n1*n2 |
|
615 | calc "Rings.inverse_class.divide"(n1, n2) = n1 div n2 |
|
616 | calc "Atools.pow"(n1, n2) = power n1 n2 |
|
617 | calc op_ _ = raise error ("calc: operator = "^op_^" not defined");-----*) |
|
618 fun calc_equ "op <" (n1, n2) = n1 < n2 |
|
619 | calc_equ "op <=" (n1, n2) = n1 <= n2 |
|
620 | calc_equ op_ _ = |
|
621 raise error ("calc_equ: operator = "^op_^" not defined"); |
|
622 fun sqrt (n:int) = if n < 0 then 0 |
|
623 (*FIXME ~~~*) else (trunc o Math.sqrt o Real.fromInt) n; |
|
624 |
|
625 fun mk_thmid thmid op_ n1 n2 = |
|
626 thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2); |
|
627 |
|
628 fun dest_binop_typ (Type("fun",[range,Type("fun",[arg2,arg1])])) = |
|
629 (arg1,arg2,range) |
|
630 | dest_binop_typ _ = raise error "dest_binop_typ: not binary"; |
|
631 (* ----- |
|
632 > val t = (term_of o the o (parse thy)) "#3^#4"; |
|
633 > val hT = type_of (head_of t); |
|
634 > dest_binop_typ hT; |
|
635 val it = ("'a","nat","'a") : typ * typ * typ |
|
636 ----- *) |
|
637 |
|
638 |
|
639 (** transform binary numeralsstrings **) |
|
640 (*Makarius 100308, hacked by WN*) |
|
641 val numbers_to_string = |
|
642 let |
|
643 fun dest_num t = |
|
644 (case try HOLogic.dest_number t of |
|
645 SOME (T, i) => |
|
646 (*if T = @{typ int} orelse T = @{typ real} then WN*) |
|
647 SOME (Free (signed_string_of_int i, T)) |
|
648 (*else NONE WN*) |
|
649 | NONE => NONE); |
|
650 |
|
651 fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b) |
|
652 | to_str (t as (u1 $ u2)) = |
|
653 (case dest_num t of |
|
654 SOME t' => t' |
|
655 | NONE => to_str u1 $ to_str u2) |
|
656 | to_str t = perhaps dest_num t; |
|
657 in to_str end |
|
658 |
|
659 (*.make uminus uniform: |
|
660 Const ("uminus", _) $ Free ("2", "RealDef.real") --> Free ("-2", _) |
|
661 to be used immediately before evaluation of numerals; |
|
662 see Scripts/calculate.sml .*) |
|
663 (*2002 fun(*app_num_tr'2 (Const("0",T)) = Free("0",T) |
|
664 | app_num_tr'2 (Const("1",T)) = Free("1",T) |
|
665 |*)app_num_tr'2 (t as Const("uminus",_) $ Free(s,T)) = |
|
666 (case int_of_str s of SOME i => |
|
667 if i > 0 then Free("-"^s,T) else Free(s,T) |
|
668 | NONE => t) |
|
669 (*| app_num_tr'2 (t as Const(s,T)) = t |
|
670 | app_num_tr'2 (Const("Numeral.number_of",Type ("fun", [_, T])) $ t) = |
|
671 Free(NumeralSyntax.dest_bin_str t, T) |
|
672 | app_num_tr'2 (t as Free(s,T)) = t |
|
673 | app_num_tr'2 (t as Var(n,T)) = t |
|
674 | app_num_tr'2 (t as Bound i) = t |
|
675 *)| app_num_tr'2 (Abs(s,T,body)) = Abs(s,T, app_num_tr'2 body) |
|
676 | app_num_tr'2 (t1 $ t2) = (app_num_tr'2 t1) $ (app_num_tr'2 t2) |
|
677 | app_num_tr'2 t = t; |
|
678 *) |
|
679 val uminus_to_string = |
|
680 let |
|
681 fun dest_num t = |
|
682 (case t of |
|
683 (Const ("HOL.uminus_class.uminus", _) $ Free (s, T)) => |
|
684 (case int_of_str s of |
|
685 SOME i => |
|
686 SOME (Free (signed_string_of_int (~1 * i), T)) |
|
687 | NONE => NONE) |
|
688 | _ => NONE); |
|
689 |
|
690 fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b) |
|
691 | to_str (t as (u1 $ u2)) = |
|
692 (case dest_num t of |
|
693 SOME t' => t' |
|
694 | NONE => to_str u1 $ to_str u2) |
|
695 | to_str t = perhaps dest_num t; |
|
696 in to_str end; |
|
697 |
|
698 |
|
699 (*2002 fun num_str thm = |
|
700 let |
|
701 val {sign_ref = sign_ref, der = der, maxidx = maxidx, |
|
702 shyps = shyps, hyps = hyps, (*tpairs = tpairs,*) prop = prop} = |
|
703 rep_thm_G thm; |
|
704 val prop' = app_num_tr'1 prop; |
|
705 in assbl_thm sign_ref der maxidx shyps hyps (*tpairs*) prop' end;*) |
|
706 fun num_str thm = |
|
707 let val (deriv, |
|
708 {thy_ref = thy_ref, tags = tags, maxidx = maxidx, shyps = shyps, |
|
709 hyps = hyps, tpairs = tpairs, prop = prop}) = rep_thm_G thm |
|
710 val prop' = numbers_to_string prop; |
|
711 in assbl_thm deriv thy_ref tags maxidx shyps hyps tpairs prop' end; |
|
712 |
|
713 fun get_thm' xstring = (*?covers 2009 Thm?!, replaces 2002 fun get_thm : |
|
714 val it = fn : theory -> xstring -> Thm.thm*) |
|
715 Thm (xstring, |
|
716 num_str (ProofContext.get_thm (thy2ctxt' "Isac") xstring)); |
|
717 |
|
718 (** get types of Free and Abs for parse' **) |
|
719 (*11.1.00: not used, fix-typed +,*,-,^ instead *) |
|
720 |
|
721 val dummyT = Type ("dummy",[]); |
|
722 val dummyT = TVar (("DUMMY",0),[]); |
|
723 |
|
724 (* assumes only 1 type for numerals |
|
725 and different identifiers for Const, Free and Abs *) |
|
726 fun get_types t = |
|
727 let |
|
728 fun get ts (Const(s,T)) = (s,T)::ts |
|
729 | get ts (Free(s,T)) = if is_no s |
|
730 then ("#",T)::ts else (s,T)::ts |
|
731 | get ts (Var(n,T)) = ts |
|
732 | get ts (Bound i) = ts |
|
733 | get ts (Abs(s,T,body)) = get ((s,T)::ts) body |
|
734 | get ts (t1 $ t2) = (get ts t1) @ (get ts t2) |
|
735 in distinct (get [] t) end; |
|
736 (* |
|
737 val t = (term_of o the o (parse thy)) "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)"; |
|
738 get_types t; |
|
739 *) |
|
740 |
|
741 (*11.1.00: not used, fix-typed +,*,-,^ instead *) |
|
742 fun set_types al (Const(s,T)) = |
|
743 (case assoc (al,s) of |
|
744 SOME T' => Const(s,T') |
|
745 | NONE => (warning ("set_types: no type for "^s); Const(s,dummyT))) |
|
746 | set_types al (Free(s,T)) = |
|
747 if is_no s then |
|
748 (case assoc (al,"#") of |
|
749 SOME T' => Free(s,T') |
|
750 | NONE => (warning ("set_types: no type for numerals"); Free(s,T))) |
|
751 else (case assoc (al,s) of |
|
752 SOME T' => Free(s,T') |
|
753 | NONE => (warning ("set_types: no type for "^s); Free(s,T))) |
|
754 | set_types al (Var(n,T)) = Var(n,T) |
|
755 | set_types al (Bound i) = Bound i |
|
756 | set_types al (Abs(s,T,body)) = |
|
757 (case assoc (al,s) of |
|
758 SOME T' => Abs(s,T', set_types al body) |
|
759 | NONE => (warning ("set_types: no type for "^s); |
|
760 Abs(s,T, set_types al body))) |
|
761 | set_types al (t1 $ t2) = (set_types al t1) $ (set_types al t2); |
|
762 (* |
|
763 val t = (term_of o the o (parse thy)) "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)"; |
|
764 val al = get_types t; |
|
765 |
|
766 val t = (term_of o the o (parse thy)) "x = #0 + #-1 * #-4"; |
|
767 atomty t; (* 'a *) |
|
768 val t' = set_types al t; |
|
769 atomty t'; (*real*) |
|
770 (cterm_of thy) t'; |
|
771 val it = "x = #0 + #-1 * #-4" : cterm |
|
772 |
|
773 val t = (term_of o the o (parse thy)) |
|
774 "#5 * x + x ^^^ #2 = (#2 + x) ^^^ #2"; |
|
775 atomty t; |
|
776 val t' = set_types al t; |
|
777 atomty t'; |
|
778 (cterm_of thy) t'; |
|
779 uncaught exception TYPE (*^^^ is new, NOT in al*) |
|
780 *) |
|
781 |
|
782 |
|
783 (** from Descript.ML **) |
|
784 |
|
785 (** decompose an isa-list to an ML-list |
|
786 i.e. [] belong to the meta-language, too **) |
|
787 |
|
788 fun is_list ((Const("List.list.Cons",_)) $ _ $ _) = true |
|
789 | is_list _ = false; |
|
790 (* val (SOME ct) = parse thy "lll::real list"; |
|
791 > val ty = (#t o rep_cterm) ct; |
|
792 > is_list ty; |
|
793 val it = false : bool |
|
794 > val (SOME ct) = parse thy "[lll]"; |
|
795 > val ty = (#t o rep_cterm) ct; |
|
796 > is_list ty; |
|
797 val it = true : bool *) |
|
798 |
|
799 |
|
800 |
|
801 fun mk_Free (s,T) = Free(s,T); |
|
802 fun mk_free T s = Free(s,T); |
|
803 |
|
804 (*Special case: one argument cp from Isabelle2002/src/Pure/term.ML*) |
|
805 fun subst_bound (arg, t) : term = (*WN100908 neglects 'raise Same.SAME'*) |
|
806 let fun subst (t as Bound i, lev) = |
|
807 if i<lev then t (*var is locally bound*) |
|
808 else if i=lev then incr_boundvars lev arg |
|
809 else Bound(i-1) (*loose: change it*) |
|
810 | subst (Abs(a,T,body), lev) = Abs(a, T, subst(body,lev+1)) |
|
811 | subst (f$t, lev) = subst(f,lev) $ subst(t,lev) |
|
812 | subst (t,lev) = t |
|
813 in subst (t,0) end; |
|
814 |
|
815 (*instantiate let; necessary for ass_up*) |
|
816 fun inst_abs thy (Const sT) = Const sT (*TODO.WN100907 drop thy*) |
|
817 | inst_abs thy (Free sT) = Free sT |
|
818 | inst_abs thy (Bound n) = Bound n |
|
819 | inst_abs thy (Var iT) = Var iT |
|
820 | inst_abs thy (Const ("Let",T1) $ e $ (Abs (v, T2, b))) = |
|
821 let val b' = subst_bound (Free (v, T2), b); |
|
822 (*fun variant_abs: term.ML*) |
|
823 in Const ("Let", T1) $ inst_abs thy e $ (Abs (v, T2, inst_abs thy b')) end |
|
824 | inst_abs thy (t1 $ t2) = inst_abs thy t1 $ inst_abs thy t2 |
|
825 | inst_abs thy t = t; |
|
826 (*val scr = |
|
827 "Script Make_fun_by_explicit (f_::real) (v_::real) (eqs_::bool list) = \ |
|
828 \ (let h_ = (hd o (filterVar f_)) eqs_; \ |
|
829 \ e_1 = hd (dropWhile (ident h_) eqs_); \ |
|
830 \ vs_ = dropWhile (ident f_) (Vars h_); \ |
|
831 \ v_1 = hd (dropWhile (ident v_) vs_); \ |
|
832 \ (s_1::bool list)=(SubProblem(DiffApp_,[univar,equation],[no_met])\ |
|
833 \ [BOOL e_1, REAL v_1])\ |
|
834 \ in Substitute [(v_1 = (rhs o hd) s_1)] h_)"; |
|
835 > val ttt = (term_of o the o (parse thy)) scr; |
|
836 > tracing(term2str ttt); |
|
837 > atomt ttt; |
|
838 *** ------------- |
|
839 *** Const ( DiffApp.Make'_fun'_by'_explicit) |
|
840 *** . Free ( f_, ) |
|
841 *** . Free ( v_, ) |
|
842 *** . Free ( eqs_, ) |
|
843 *** . Const ( Let) |
|
844 *** . . Const ( Fun.op o) |
|
845 *** . . . Const ( List.hd) |
|
846 *** . . . Const ( DiffApp.filterVar) |
|
847 *** . . . . Free ( f_, ) |
|
848 *** . . . Free ( eqs_, ) |
|
849 *** . . Abs( h_,.. |
|
850 *** . . . Const ( Let) |
|
851 *** . . . . Const ( List.hd) |
|
852 *** . . . . . Const ( List.dropWhile) |
|
853 *** . . . . . . Const ( Atools.ident) |
|
854 *** . . . . . . . Bound 0 <---- Free ( h_, ) |
|
855 *** . . . . . . Free ( eqs_, ) |
|
856 *** . . . . Abs( e_1,.. |
|
857 *** . . . . . Const ( Let) |
|
858 *** . . . . . . Const ( List.dropWhile) |
|
859 *** . . . . . . . Const ( Atools.ident) |
|
860 *** . . . . . . . . Free ( f_, ) |
|
861 *** . . . . . . . Const ( Tools.Vars) |
|
862 *** . . . . . . . . Bound 1 <---- Free ( h_, ) |
|
863 *** . . . . . . Abs( vs_,.. |
|
864 *** . . . . . . . Const ( Let) |
|
865 *** . . . . . . . . Const ( List.hd) |
|
866 *** . . . . . . . . . Const ( List.dropWhile) |
|
867 *** . . . . . . . . . . Const ( Atools.ident) |
|
868 *** . . . . . . . . . . . Free ( v_, ) |
|
869 *** . . . . . . . . . . Bound 0 <---- Free ( vs_, ) |
|
870 *** . . . . . . . . Abs( v_1,.. |
|
871 *** . . . . . . . . . Const ( Let) |
|
872 *** . . . . . . . . . . Const ( Script.SubProblem) |
|
873 *** . . . . . . . . . . . Const ( Pair) |
|
874 *** . . . . . . . . . . . . Free ( DiffApp_, ) |
|
875 *** . . . . . . . . . . . . Const ( Pair) |
|
876 *** . . . . . . . . . . . . . Const ( List.list.Cons) |
|
877 *** . . . . . . . . . . . . . . Free ( univar, ) |
|
878 *** . . . . . . . . . . . . . . Const ( List.list.Cons) |
|
879 *** . . . . . . . . . . . . . . . Free ( equation, ) |
|
880 *** . . . . . . . . . . . . . . . Const ( List.list.Nil) |
|
881 *** . . . . . . . . . . . . . Const ( List.list.Cons) |
|
882 *** . . . . . . . . . . . . . . Free ( no_met, ) |
|
883 *** . . . . . . . . . . . . . . Const ( List.list.Nil) |
|
884 *** . . . . . . . . . . . Const ( List.list.Cons) |
|
885 *** . . . . . . . . . . . . Const ( Script.BOOL) |
|
886 *** . . . . . . . . . . . . . Bound 2 <----- Free ( e_1, ) |
|
887 *** . . . . . . . . . . . . Const ( List.list.Cons) |
|
888 *** . . . . . . . . . . . . . Const ( Script.real_) |
|
889 *** . . . . . . . . . . . . . . Bound 0 <----- Free ( v_1, ) |
|
890 *** . . . . . . . . . . . . . Const ( List.list.Nil) |
|
891 *** . . . . . . . . . . Abs( s_1,.. |
|
892 *** . . . . . . . . . . . Const ( Script.Substitute) |
|
893 *** . . . . . . . . . . . . Const ( List.list.Cons) |
|
894 *** . . . . . . . . . . . . . Const ( Pair) |
|
895 *** . . . . . . . . . . . . . . Bound 1 <----- Free ( v_1, ) |
|
896 *** . . . . . . . . . . . . . . Const ( Fun.op o) |
|
897 *** . . . . . . . . . . . . . . . Const ( Tools.rhs) |
|
898 *** . . . . . . . . . . . . . . . Const ( List.hd) |
|
899 *** . . . . . . . . . . . . . . . Bound 0 <----- Free ( s_1, ) |
|
900 *** . . . . . . . . . . . . . Const ( List.list.Nil) |
|
901 *** . . . . . . . . . . . . Bound 4 <----- Free ( h_, ) |
|
902 |
|
903 > val ttt' = inst_abs thy ttt; |
|
904 > tracing(term2str ttt'); |
|
905 Script Make_fun_by_explicit f_ v_ eqs_ = |
|
906 ... as above ... |
|
907 > atomt ttt'; |
|
908 *** ------------- |
|
909 *** Const ( DiffApp.Make'_fun'_by'_explicit) |
|
910 *** . Free ( f_, ) |
|
911 *** . Free ( v_, ) |
|
912 *** . Free ( eqs_, ) |
|
913 *** . Const ( Let) |
|
914 *** . . Const ( Fun.op o) |
|
915 *** . . . Const ( List.hd) |
|
916 *** . . . Const ( DiffApp.filterVar) |
|
917 *** . . . . Free ( f_, ) |
|
918 *** . . . Free ( eqs_, ) |
|
919 *** . . Abs( h_,.. |
|
920 *** . . . Const ( Let) |
|
921 *** . . . . Const ( List.hd) |
|
922 *** . . . . . Const ( List.dropWhile) |
|
923 *** . . . . . . Const ( Atools.ident) |
|
924 *** . . . . . . . Free ( h_, ) <---- Bound 0 |
|
925 *** . . . . . . Free ( eqs_, ) |
|
926 *** . . . . Abs( e_1,.. |
|
927 *** . . . . . Const ( Let) |
|
928 *** . . . . . . Const ( List.dropWhile) |
|
929 *** . . . . . . . Const ( Atools.ident) |
|
930 *** . . . . . . . . Free ( f_, ) |
|
931 *** . . . . . . . Const ( Tools.Vars) |
|
932 *** . . . . . . . . Free ( h_, ) <---- Bound 1 |
|
933 *** . . . . . . Abs( vs_,.. |
|
934 *** . . . . . . . Const ( Let) |
|
935 *** . . . . . . . . Const ( List.hd) |
|
936 *** . . . . . . . . . Const ( List.dropWhile) |
|
937 *** . . . . . . . . . . Const ( Atools.ident) |
|
938 *** . . . . . . . . . . . Free ( v_, ) |
|
939 *** . . . . . . . . . . Free ( vs_, ) <---- Bound 0 |
|
940 *** . . . . . . . . Abs( v_1,.. |
|
941 *** . . . . . . . . . Const ( Let) |
|
942 *** . . . . . . . . . . Const ( Script.SubProblem) |
|
943 *** . . . . . . . . . . . Const ( Pair) |
|
944 *** . . . . . . . . . . . . Free ( DiffApp_, ) |
|
945 *** . . . . . . . . . . . . Const ( Pair) |
|
946 *** . . . . . . . . . . . . . Const ( List.list.Cons) |
|
947 *** . . . . . . . . . . . . . . Free ( univar, ) |
|
948 *** . . . . . . . . . . . . . . Const ( List.list.Cons) |
|
949 *** . . . . . . . . . . . . . . . Free ( equation, ) |
|
950 *** . . . . . . . . . . . . . . . Const ( List.list.Nil) |
|
951 *** . . . . . . . . . . . . . Const ( List.list.Cons) |
|
952 *** . . . . . . . . . . . . . . Free ( no_met, ) |
|
953 *** . . . . . . . . . . . . . . Const ( List.list.Nil) |
|
954 *** . . . . . . . . . . . Const ( List.list.Cons) |
|
955 *** . . . . . . . . . . . . Const ( Script.BOOL) |
|
956 *** . . . . . . . . . . . . . Free ( e_1, ) <----- Bound 2 |
|
957 *** . . . . . . . . . . . . Const ( List.list.Cons) |
|
958 *** . . . . . . . . . . . . . Const ( Script.real_) |
|
959 *** . . . . . . . . . . . . . . Free ( v_1, ) <----- Bound 0 |
|
960 *** . . . . . . . . . . . . . Const ( List.list.Nil) |
|
961 *** . . . . . . . . . . Abs( s_1,.. |
|
962 *** . . . . . . . . . . . Const ( Script.Substitute) |
|
963 *** . . . . . . . . . . . . Const ( List.list.Cons) |
|
964 *** . . . . . . . . . . . . . Const ( Pair) |
|
965 *** . . . . . . . . . . . . . . Free ( v_1, ) <----- Bound 1 |
|
966 *** . . . . . . . . . . . . . . Const ( Fun.op o) |
|
967 *** . . . . . . . . . . . . . . . Const ( Tools.rhs) |
|
968 *** . . . . . . . . . . . . . . . Const ( List.hd) |
|
969 *** . . . . . . . . . . . . . . . Free ( s_1, ) <----- Bound 0 |
|
970 *** . . . . . . . . . . . . . Const ( List.list.Nil) |
|
971 *** . . . . . . . . . . . . Free ( h_, ) <----- Bound 4 |
|
972 |
|
973 Note numbering of de Bruijn indexes ! |
|
974 |
|
975 Script Make_fun_by_explicit f_ v_ eqs_ = |
|
976 let h_ = (hd o filterVar f_) eqs_; |
|
977 e_1 = hd (dropWhile (ident h_ BOUND_0) eqs_); |
|
978 vs_ = dropWhile (ident f_) (Vars h_ BOUND_1); |
|
979 v_1 = hd (dropWhile (ident v_) vs_ BOUND_0); |
|
980 s_1 = |
|
981 SubProblem (DiffApp_, [univar, equation], [no_met]) |
|
982 [BOOL e_1 BOUND_2, REAL v_1 BOUND_0] |
|
983 in Substitute [(v_1 BOUND_1 = (rhs o hd) s_1 BOUND_0)] h_ BOUND_4 |
|
984 *) |
|
985 |
|
986 |
|
987 fun T_a2real (Type (s, [])) = |
|
988 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else Type (s, []) |
|
989 | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts) |
|
990 | T_a2real (TFree (s, srt)) = |
|
991 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TFree (s, srt) |
|
992 | T_a2real (TVar (("DUMMY",_),srt)) = HOLogic.realT; |
|
993 |
|
994 (*FIXME .. fixes the type (+see Typefix.thy*) |
|
995 fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T)) |
|
996 | typ_a2real (Free( s, T)) = (Free( s, T_a2real T)) |
|
997 | typ_a2real (Var( n, T)) = (Var( n, T_a2real T)) |
|
998 | typ_a2real (Bound i) = (Bound i) |
|
999 | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t) |
|
1000 | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2); |
|
1001 (* |
|
1002 ----------------6.8.02--------------------------------------------------- |
|
1003 val str = "1"; |
|
1004 val t = read_cterm (sign_of thy) (str,(TVar(("DUMMY",0),[]))); |
|
1005 atomty (term_of t); |
|
1006 *** ------------- |
|
1007 *** Const ( 1, 'a) |
|
1008 val t = (app_num_tr' o term_of) t; |
|
1009 atomty t; |
|
1010 *** ------------- |
|
1011 *** Const ( 1, 'a) |
|
1012 val t = typ_a2real t; |
|
1013 atomty t; |
|
1014 *** ------------- |
|
1015 *** Const ( 1, real) |
|
1016 |
|
1017 val str = "2"; |
|
1018 val t = read_cterm (sign_of thy) (str,(TVar(("DUMMY",0),[]))); |
|
1019 atomty (term_of t); |
|
1020 *** ------------- |
|
1021 *** Const ( Numeral.number_of, bin => 'a) |
|
1022 *** . Const ( Numeral.bin.Bit, [bin, bool] => bin) |
|
1023 *** . . Const ( Numeral.bin.Bit, [bin, bool] => bin) |
|
1024 *** . . . Const ( Numeral.bin.Pls, bin) |
|
1025 *** . . . Const ( True, bool) |
|
1026 *** . . Const ( False, bool) |
|
1027 val t = (app_num_tr' o term_of) t; |
|
1028 atomty t; |
|
1029 *** ------------- |
|
1030 *** Free ( 2, 'a) |
|
1031 val t = typ_a2real t; |
|
1032 atomty t; |
|
1033 *** ------------- |
|
1034 *** Free ( 2, real) |
|
1035 ----------------6.8.02--------------------------------------------------- |
|
1036 |
|
1037 |
|
1038 > val str = "R"; |
|
1039 > val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[])))); |
|
1040 val t = Free ("R","?DUMMY") : term |
|
1041 > val t' = typ_a2real t; |
|
1042 > (cterm_of thy) t'; |
|
1043 val it = "R::RealDef.real" : cterm |
|
1044 |
|
1045 > val str = "R=R"; |
|
1046 > val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[])))); |
|
1047 > atomty (typ_a2real t); |
|
1048 *** ------------- |
|
1049 *** Const ( op =, [RealDef.real, RealDef.real] => bool) |
|
1050 *** Free ( R, RealDef.real) |
|
1051 *** Free ( R, RealDef.real) |
|
1052 > val t' = typ_a2real t; |
|
1053 > (cterm_of thy) t'; |
|
1054 val it = "(R::RealDef.real) = R" : cterm |
|
1055 |
|
1056 > val str = "fixed_values [R=R]"; |
|
1057 > val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[])))); |
|
1058 > val t' = typ_a2real t; |
|
1059 > (cterm_of thy) t'; |
|
1060 val it = "fixed_values [(R::RealDef.real) = R]" : cterm |
|
1061 *) |
|
1062 |
|
1063 (*TODO.WN0609: parse should return a term or a string |
|
1064 (or even more comprehensive datastructure for error-messages) |
|
1065 i.e. in wrapping with SOME term or NONE the latter is not sufficient*) |
|
1066 (*2002 fun parseold thy str = |
|
1067 (let |
|
1068 val sgn = sign_of thy; |
|
1069 val t = ((*typ_a2real o*) app_num_tr'1 o term_of) |
|
1070 (read_cterm sgn (str,(TVar(("DUMMY",0),[])))); |
|
1071 in SOME (cterm_of sgn t) end) |
|
1072 handle _ => NONE;*) |
|
1073 |
|
1074 |
|
1075 |
|
1076 fun parseold thy str = |
|
1077 (let val t = ((*typ_a2real o*) numbers_to_string) |
|
1078 (Syntax.read_term_global thy str) |
|
1079 in SOME (cterm_of thy t) end) |
|
1080 handle _ => NONE; |
|
1081 (*2002 fun parseN thy str = |
|
1082 (let |
|
1083 val sgn = sign_of thy; |
|
1084 val t = ((*typ_a2real o app_num_tr'1 o*) term_of) |
|
1085 (read_cterm sgn (str,(TVar(("DUMMY",0),[])))); |
|
1086 in SOME (cterm_of sgn t) end) |
|
1087 handle _ => NONE;*) |
|
1088 fun parseN thy str = |
|
1089 (let val t = (*(typ_a2real o numbers_to_string)*) |
|
1090 (Syntax.read_term_global thy str) |
|
1091 in SOME (cterm_of thy t) end) |
|
1092 handle _ => NONE; |
|
1093 (*2002 fun parse thy str = |
|
1094 (let |
|
1095 val sgn = sign_of thy; |
|
1096 val t = (typ_a2real o app_num_tr'1 o term_of) |
|
1097 (read_cterm sgn (str,(TVar(("DUMMY",0),[])))); |
|
1098 in SOME (cterm_of sgn t) end) (*FIXXXXME 10.8.02: return term !!!*) |
|
1099 handle _ => NONE;*) |
|
1100 (*2010 fun parse thy str = |
|
1101 (let val t = (typ_a2real o app_num_tr'1) (Syntax.read_term_global thy str) |
|
1102 in SOME (cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*) |
|
1103 handle _ => NONE;*) |
|
1104 fun parse thy str = |
|
1105 (let val t = (typ_a2real o numbers_to_string) |
|
1106 (Syntax.read_term_global thy str) |
|
1107 in SOME (cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*) |
|
1108 handle _ => NONE; |
|
1109 (* |
|
1110 > val (SOME ct) = parse thy "(-#5)^^^#3"; |
|
1111 > atomty (term_of ct); |
|
1112 *** ------------- |
|
1113 *** Const ( Nat.op ^, ['a, nat] => 'a) |
|
1114 *** Const ( uminus, 'a => 'a) |
|
1115 *** Free ( #5, 'a) |
|
1116 *** Free ( #3, nat) |
|
1117 > val (SOME ct) = parse thy "R=R"; |
|
1118 > atomty (term_of ct); |
|
1119 *** ------------- |
|
1120 *** Const ( op =, [real, real] => bool) |
|
1121 *** Free ( R, real) |
|
1122 *** Free ( R, real) |
|
1123 |
|
1124 THIS IS THE OUTPUT FOR VERSION (3) above at typ_a2real !!!!! |
|
1125 *** ------------- |
|
1126 *** Const ( op =, [RealDef.real, RealDef.real] => bool) |
|
1127 *** Free ( R, RealDef.real) |
|
1128 *** Free ( R, RealDef.real) *) |
|
1129 |
|
1130 fun parse_patt thy str = |
|
1131 ProofContext.read_term_pattern (thy2ctxt thy) str; |
|
1132 |
|
1133 (*version for testing local to theories*) |
|
1134 fun str2term_ thy str = (term_of o the o (parse thy)) str; |
|
1135 fun str2term str = (term_of o the o (parse (theory "Isac"))) str; |
|
1136 fun strs2terms ss = map str2term ss; |
|
1137 fun str2termN str = (term_of o the o (parseN (theory "Isac"))) str; |
|
1138 |
|
1139 (*+ makes a substitution from the output of Pattern.match +*) |
|
1140 (*fun mk_subs ((id, _):indexname, t:term) = (Free (id,type_of t), t);*) |
|
1141 fun mk_subs (subs: ((string * int) * (Term.typ * Term.term)) list) = |
|
1142 let fun mk_sub ((id, _), (ty, tm)) = (Free (id, ty), tm) in |
|
1143 map mk_sub subs end; |
|
1144 |
|
1145 val atomthm = atomt o #prop o rep_thm; |
|
1146 |
|
1147 (*.instantiate #prop thm with bound variables (as Free).*) |
|
1148 fun inst_bdv [] t = t : term |
|
1149 | inst_bdv (instl: (term*term) list) t = |
|
1150 let fun subst (v as Var((s,_),T)) = |
|
1151 (case explode s of |
|
1152 "b"::"d"::"v"::_ => |
|
1153 if_none (assoc(instl,Free(s,T))) (Free(s,T)) |
|
1154 | _ => v) |
|
1155 | subst (Abs(a,T,body)) = Abs(a, T, subst body) |
|
1156 | subst (f$t') = subst f $ subst t' |
|
1157 | subst t = if_none (assoc(instl,t)) t |
|
1158 in subst t end; |
|
1159 |
|
1160 |
|
1161 (*WN050829 caution: is_atom (str2term"q_0/2 * L * x") = true !!! |
|
1162 use length (vars term) = 1 instead*) |
|
1163 fun is_atom (Const ("Float.Float",_) $ _) = true |
|
1164 | is_atom (Const ("ComplexI.I'_'_",_)) = true |
|
1165 | is_atom (Const ("op *",_) $ t $ Const ("ComplexI.I'_'_",_)) = is_atom t |
|
1166 | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ Const ("ComplexI.I'_'_",_)) = is_atom t1 |
|
1167 | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ |
|
1168 (Const ("op *",_) $ t2 $ Const ("ComplexI.I'_'_",_))) = |
|
1169 is_atom t1 andalso is_atom t2 |
|
1170 | is_atom (Const _) = true |
|
1171 | is_atom (Free _) = true |
|
1172 | is_atom (Var _) = true |
|
1173 | is_atom _ = false; |
|
1174 (* val t = str2term "q_0/2 * L * x"; |
|
1175 |
|
1176 |
|
1177 *) |
|
1178 (*val t = str2term "Float ((1,2),(0,0))"; |
|
1179 > is_atom t; |
|
1180 val it = true : bool |
|
1181 > val t = str2term "Float ((1,2),(0,0)) * I__"; |
|
1182 > is_atom t; |
|
1183 val it = true : bool |
|
1184 > val t = str2term "Float ((1,2),(0,0)) + Float ((3,4),(0,0)) * I__"; |
|
1185 > is_atom t; |
|
1186 val it = true : bool |
|
1187 > val t = str2term "1 + 2*I__"; |
|
1188 > val Const ("Groups.plus_class.plus",_) $ t1 $ (Const ("op *",_) $ t2 $ Const ("ComplexI.I'_'_",_)) = t; |
|
1189 *) |
|
1190 |
|
1191 (*.adaption from Isabelle/src/Pure/term.ML; reports if ALL Free's |
|
1192 have found a substitution (required for evaluating the preconditions |
|
1193 of _incomplete_ models).*) |
|
1194 fun subst_atomic_all [] t = (false, (*TODO may be 'true' for some terms ?*) |
|
1195 t : term) |
|
1196 | subst_atomic_all (instl: (term*term) list) t = |
|
1197 let fun subst (Abs(a,T,body)) = |
|
1198 let val (all, body') = subst body |
|
1199 in (all, Abs(a, T, body')) end |
|
1200 | subst (f$tt) = |
|
1201 let val (all1, f') = subst f |
|
1202 val (all2, tt') = subst tt |
|
1203 in (all1 andalso all2, f' $ tt') end |
|
1204 | subst (t as Free _) = |
|
1205 if is_num t then (true, t) (*numerals cannot be subst*) |
|
1206 else (case assoc(instl,t) of |
|
1207 SOME t' => (true, t') |
|
1208 | NONE => (false, t)) |
|
1209 | subst t = (true, if_none (assoc(instl,t)) t) |
|
1210 in subst t end; |
|
1211 |
|
1212 (*.add two terms with a type given.*) |
|
1213 fun mk_add t1 t2 = |
|
1214 let val T1 = type_of t1 |
|
1215 val T2 = type_of t2 |
|
1216 in if T1 <> T2 then raise TYPE ("mk_add gets ",[T1, T2],[t1,t2]) |
|
1217 else (Const ("Groups.plus_class.plus", [T1, T2] ---> T1) $ t1 $ t2) |
|
1218 end; |
|
1219 |
|