1 (* Title: extends Isabelle/src/Pure/term.ML
2 Author: Walther Neuper 1999
3 (c) due to copyright terms
7 > (cterm_of thy) a_term;
8 val it = "empty" : cterm *)
10 (*2003 fun match thy t pat =
11 (snd (Pattern.match (Sign.tsig_of (sign_of thy)) (pat, t)))
14 Term.term -> Term.term -> (Term.indexname * Term.term) list*)
15 (*see src/Tools/eqsubst.ML fun clean_match*)
16 (*2003 fun matches thy tm pa = if match thy tm pa = [] then false else true;*)
17 fun matches thy tm pa =
18 (Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true)
21 fun atomtyp t = (*WN10 see raw_pp_typ*)
23 fun ato n (Type (s,[])) =
24 ("\n*** "^indent n^"Type ("^s^",[])")
25 | ato n (Type (s,Ts)) =
26 ("\n*** "^indent n^"Type ("^s^",["^ atol (n+1) Ts)
28 | ato n (TFree (s,sort)) =
29 ("\n*** "^indent n^"TFree ("^s^",["^ strs2str' sort)
31 | ato n (TVar ((s,i),sort)) =
32 ("\n*** "^indent n^"TVar (("^s^","^
33 string_of_int i ^ strs2str' sort)
35 ("\n*** "^indent n^"]")
36 | atol n (T::Ts) = (ato n T ^ atol n Ts)
37 in writeln (ato 0 t ^ "\n") end;
39 > val T = (type_of o term_of o the o (parse thy)) "a::[real,int] => nat";
42 *** Type (RealDef.real,[])
44 *** Type (IntDef.int,[])
50 (*Prog.Tutorial.p.34, Makarius 1005 does the above like this..*)
52 fun pp_pair (x, y) = Pretty.list "(" ")" [x, y]
53 fun pp_list xs = Pretty.list "[" "]" xs
54 fun pp_str s = Pretty.str s
55 fun pp_qstr s = Pretty.quote (pp_str s)
56 fun pp_int i = pp_str (string_of_int i)
57 fun pp_sort S = pp_list (map pp_qstr S)
58 fun pp_constr a args = Pretty.block [pp_str a, Pretty.brk 1, args]
60 fun raw_pp_typ (TVar ((a, i), S)) =
61 pp_constr "TVar" (pp_pair (pp_pair (pp_qstr a, pp_int i), pp_sort S))
62 | raw_pp_typ (TFree (a, S)) =
63 pp_constr "TFree" (pp_pair (pp_qstr a, pp_sort S))
64 | raw_pp_typ (Type (a, tys)) =
65 pp_constr "Type" (pp_pair (pp_qstr a, pp_list (map raw_pp_typ tys)))
68 PolyML.addPrettyPrinter
69 (fn _ => fn _ => ml_pretty o Pretty.to_ML o raw_pp_typ);
71 PolyML.addPrettyPrinter
72 (fn _ => fn _ => ml_pretty o Pretty.to_ML o Proof_Display.pp_typ Pure.thy);
76 let fun ato (Const (a, _)) n =
77 "\n*** " ^ indent n ^ "Const (" ^ a ^ ", _)"
78 | ato (Free (a, _)) n =
79 "\n*** " ^ indent n ^ "Free (" ^ a ^ ", _)"
80 | ato (Var ((a, i), _)) n =
81 "\n*** " ^ indent n ^ "Var (" ^ a ^ ", " ^
82 string_of_int i ^ "), _)"
84 "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
85 | ato (Abs (a, _, body)) n =
86 "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1)
87 | ato (f $ t) n = (ato f n ^ ato t (n + 1))
88 in writeln ("\n*** -------------" ^ ato t 0 ^ "\n***") end;
90 fun term_detail2str t =
91 let fun ato (Const (a, T)) n =
92 "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ string_of_typ T ^ ")"
93 | ato (Free (a, T)) n =
94 "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ string_of_typ T ^ ")"
95 | ato (Var ((a, i), T)) n =
96 "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), "^
99 "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
100 | ato (Abs(a, T, body)) n =
101 "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ string_of_typ T ^ ",.."
103 | ato (f $ t) n = ato f n ^ ato t (n + 1)
104 in "\n*** " ^ ato t 0 ^ "\n***" end;
105 fun atomty t = (writeln o term_detail2str) t; (*WN100907 broken*)
107 fun term_str thy (Const(s,_)) = s
108 | term_str thy (Free(s,_)) = s
109 | term_str thy (Var((s,i),_)) = s^(string_of_int i)
110 | term_str thy (Bound i) = "B."^(string_of_int i)
111 | term_str thy (Abs(s,_,_)) = s
112 | term_str thy t = error("term_str not for "^term2str t);
114 (*.contains the fst argument the second argument (a leave! of term).*)
115 fun contains_term (Abs(_,_,body)) t = contains_term body t
116 | contains_term (f $ f') t =
117 contains_term f t orelse contains_term f' t
118 | contains_term s t = t = s;
119 (*.contains the term a VAR(("*",_),_) ?.*)
120 fun contains_Var (Abs(_,_,body)) = contains_Var body
121 | contains_Var (f $ f') = contains_Var f orelse contains_Var f'
122 | contains_Var (Var _) = true
123 | contains_Var _ = false;
124 (* contains_Var (str2term "?z = 3") (*true*);
125 contains_Var (str2term "z = 3") (*false*);
128 (*fun int_of_str str =
129 let val ss = explode str
130 val str' = case ss of
131 "("::s => drop_last s | _ => ss
132 in case BasisLibrary.Int.fromString (implode str') of
134 | NONE => NONE end;*)
136 let val ss = explode str
137 val str' = case ss of
138 "("::s => drop_last s | _ => ss
139 in (SOME (Thy_Output.integer (implode str'))) handle _ => NONE end;
142 val it = SOME 123 : int option
143 > int_of_str "(-123)";
144 val it = SOME 123 : int option
146 val it = NONE : int option
148 val it = SOME ~123 : int option
150 fun int_of_str' str =
151 case int_of_str str of
153 | NONE => raise TERM ("int_of_string: no int-string",[]);
154 val str2int = int_of_str';
156 fun is_numeral str = case int_of_str str of
159 val is_no = is_numeral;
160 fun is_num (Free (s,_)) = if is_numeral s then true else false
163 > is_num ((term_of o the o (parse thy)) "#1");
165 > is_num ((term_of o the o (parse thy)) "#-1");
167 > is_num ((term_of o the o (parse thy)) "a123");
168 val it = false : bool
171 (*fun int_of_Free (Free (intstr, _)) =
172 (case BasisLibrary.Int.fromString intstr of
174 | NONE => error ("int_of_Free ( "^ intstr ^", _)"))
175 | int_of_Free t = error ("int_of_Free ( "^ term2str t ^" )");*)
176 fun int_of_Free (Free (intstr, _)) = (Thy_Output.integer intstr
177 handle _ => error ("int_of_Free ( "^ intstr ^", _)"))
178 | int_of_Free t = error ("int_of_Free ( "^ term2str t ^" )");
182 fun scan vs (Const(s,T)) = vs
183 | scan vs (t as Free(s,T)) = if is_no s then vs else t::vs
184 | scan vs (t as Var((s,i),T)) = t::vs
185 | scan vs (Bound i) = vs
186 | scan vs (Abs(s,T,t)) = scan vs t
187 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
188 in (distinct o (scan [])) t end;
190 fun is_Free (Free _) = true
192 fun is_fun_id (Const _) = true
193 | is_fun_id (Free _) = true
194 | is_fun_id _ = false;
195 fun is_f_x (f $ x) = is_fun_id f andalso is_Free x
197 (* is_f_x (str2term "q_0/2 * L * x") (*false*);
198 is_f_x (str2term "M_b x") (*true*);
202 fun scan vs (Const(s,T)) = vs
203 | scan vs (t as Free(s,T)) = if is_no s then vs else s::vs
204 | scan vs (t as Var((s,i),T)) = (s^"_"^(string_of_int i))::vs
205 | scan vs (Bound i) = vs
206 | scan vs (Abs(s,T,t)) = scan vs t
207 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
208 in (distinct o (scan [])) t end;
212 fun scan vs (Const(s,T)) = if is_no s then vs else s::vs
213 | scan vs (t as Free(s,T)) = if is_no s then vs else s::vs
214 | scan vs (t as Var((s,i),T)) = (s^"_"^(string_of_int i))::vs
215 | scan vs (Bound i) = vs
216 | scan vs (Abs(s,T,t)) = scan (s::vs) t
217 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
218 in (distinct o (scan [])) t end;
221 "b"::"d"::"v"::_ => true
223 fun is_bdv_ (Free (s,_)) = is_bdv s
226 fun free2str (Free (s,_)) = s
227 | free2str t = error ("free2str not for "^ term2str t);
228 fun free2int (t as Free (s, _)) = ((str2int s)
229 handle _ => error ("free2int: "^term_detail2str t))
230 | free2int t = error ("free2int: "^term_detail2str t);
233 fun var2free (t as Const(s,T)) = t
234 | var2free (t as Free(s,T)) = t
235 | var2free (Var((s,i),T)) = Free(s,T)
236 | var2free (t as Bound i) = t
237 | var2free (Abs(s,T,t)) = Abs(s,T,var2free t)
238 | var2free (t1 $ t2) = (var2free t1) $ (var2free t2);
240 (*27.8.01: doesn't find some subterm ???!???*)
241 (*2010 free2var -> Logic.varify, but take care of 'Free ("1",_)'*)
242 fun free2var (t as Const (s, T)) = t
243 | free2var (t as Free (s, T)) = if is_no s then t else Var ((s, 0), T)
244 | free2var (t as Var ((s, i), T)) = t
245 | free2var (t as Bound i) = t
246 | free2var (Abs (s, T, t)) = Abs (s, T, free2var t)
247 | free2var (t1 $ t2) = (free2var t1) $ (free2var t2);
250 fun mk_listT T = Type ("List.list", [T]);
252 Const("List.list.Cons", [T, mk_listT T] ---> mk_listT T);
253 (*28.8.01: TODO: get type from head of list: 1 arg less!!!*)
254 fun list2isalist T [] = Const("List.list.Nil",mk_listT T)
255 | list2isalist T (t::ts) = (list_const T) $ t $ (list2isalist T ts);
257 > val tt = (term_of o the o (parse thy)) "R=(R::real)";
258 > val TT = type_of tt;
259 > val ss = list2isalist TT [tt,tt,tt];
261 val it = "[R = R, R = R, R = R]" : cterm *)
263 fun isapair2pair (Const ("Pair",_) $ a $ b) = (a,b)
265 error ("isapair2pair called with "^term2str t);
267 val listType = Type ("List.list",[Type ("bool",[])]);
268 fun isalist2list ls =
270 fun get es (Const("List.list.Cons",_) $ t $ ls) = get (t::es) ls
271 | get es (Const("List.list.Nil",_)) = es
273 error ("isalist2list applied to NON-list '"^term2str t^"'")
274 in (rev o (get [])) ls end;
276 > val il = str2term "[a=b,c=d,e=f]";
277 > val l = isalist2list il;
278 > (tracing o terms2str) l;
279 ["a = b","c = d","e = f"]
281 > val il = str2term "ss___::bool list";
282 > val l = isalist2list il;
283 [Free ("ss___", "bool List.list")]
287 (*review Isabelle2009/src/HOL/Tools/hologic.ML*)
288 val prop = Type ("prop",[]); (* ~/Diss.99/Integers-Isa/tools.sml*)
289 val bool = Type ("bool",[]); (* 2002 Integ.int *)
290 val Trueprop = Const("Trueprop",bool-->prop);
291 fun mk_prop t = Trueprop $ t;
292 val true_as_term = Const("True",bool);
293 val false_as_term = Const("False",bool);
294 val true_as_cterm = cterm_of (theory "HOL") true_as_term;
295 val false_as_cterm = cterm_of (theory "HOL") false_as_term;
297 infixr 5 -->; (*2002 /Pure/term.ML *)
298 infixr --->; (*2002 /Pure/term.ML *)
299 fun S --> T = Type("fun",[S,T]); (*2002 /Pure/term.ML *)
300 val op ---> = foldr (op -->); (*2002 /Pure/term.ML *)
301 fun list_implies ([], B) = B : term (*2002 /term.ML *)
302 | list_implies (A::AS, B) = Logic.implies $ A $ list_implies(AS,B);
308 fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) = (* = thm.ML *)
309 match_bvs(s, t, if x="" orelse y="" then al
311 | match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al))
312 | match_bvs(_,_,al) = al;
313 fun ren_inst(insts,prop,pat,obj) = (* = thm.ML *)
314 let val ren = match_bvs(pat,obj,[])
315 fun renAbs(Abs(x,T,b)) =
316 Abs(case assoc_string(ren,x) of NONE => x
317 | SOME(y) => y, T, renAbs(b))
318 | renAbs(f$t) = renAbs(f) $ renAbs(t)
320 in subst_vars insts (if null(ren) then prop else renAbs(prop)) end;
327 fun dest_equals' (Const("op =",_) $ t $ u) = (t,u)(* logic.ML: Const("=="*)
328 | dest_equals' t = raise TERM("dest_equals'", [t]);
329 val lhs_ = (fst o dest_equals');
330 val rhs_ = (snd o dest_equals');
332 fun is_equality (Const("op =",_) $ t $ u) = true (* logic.ML: Const("=="*)
333 | is_equality _ = false;
334 fun mk_equality (t,u) = (Const("op =",[type_of t,type_of u]--->bool) $ t $ u);
335 fun is_expliceq (Const("op =",_) $ (Free _) $ u) = true
336 | is_expliceq _ = false;
337 fun strip_trueprop (Const("Trueprop",_) $ t) = t
338 | strip_trueprop t = t;
339 (* | strip_trueprop t = raise TERM("strip_trueprop", [t]);
342 (*.(A1==>...An==>B) goes to (A1==>...An==>).*)
343 fun strip_imp_prems' (Const("==>", T) $ A $ t) =
344 let fun coll_prems As (Const("==>", _) $ A $ t) =
345 coll_prems (As $ (Logic.implies $ A)) t
346 | coll_prems As _ = SOME As
347 in coll_prems (Logic.implies $ A) t end
348 | strip_imp_prems' _ = NONE; (* logic.ML: term -> term list*)
350 val thm = real_mult_div_cancel1;
351 val prop = (#prop o rep_thm) thm;
355 *** . Const ( Trueprop)
357 *** . . . Const ( op =)
358 *** . . . . Var ((k, 0), )
359 *** . . . . Const ( 0)
360 *** . Const ( Trueprop)
361 *** . . Const ( op =) *** .............
362 val SOME t = strip_imp_prems' ((#prop o rep_thm) thm);
366 *** . Const ( Trueprop)
368 *** . . . Const ( op =)
369 *** . . . . Var ((k, 0), )
370 *** . . . . Const ( 0)
372 val thm = real_le_anti_sym;
373 val prop = (#prop o rep_thm) thm;
377 *** . Const ( Trueprop)
378 *** . . Const ( op <=)
379 *** . . . Var ((z, 0), )
380 *** . . . Var ((w, 0), )
382 *** . . Const ( Trueprop)
383 *** . . . Const ( op <=)
384 *** . . . . Var ((w, 0), )
385 *** . . . . Var ((z, 0), )
386 *** . . Const ( Trueprop)
387 *** . . . Const ( op =)
389 val SOME t = strip_imp_prems' ((#prop o rep_thm) thm);
393 *** . Const ( Trueprop)
394 *** . . Const ( op <=)
395 *** . . . Var ((z, 0), )
396 *** . . . Var ((w, 0), )
398 *** . . Const ( Trueprop)
399 *** . . . Const ( op <=)
400 *** . . . . Var ((w, 0), )
401 *** . . . . Var ((z, 0), )
404 (*. (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch.*)
405 fun ins_concl (Const("==>", T) $ A $ t) B = Logic.implies $ A $ (ins_concl t B)
406 | ins_concl (Const("==>", T) $ A ) B = Logic.implies $ A $ B
407 | ins_concl t B = raise TERM("ins_concl", [t, B]);
409 val thm = real_le_anti_sym;
410 val prop = (#prop o rep_thm) thm;
411 val concl = Logic.strip_imp_concl prop;
412 val SOME prems = strip_imp_prems' prop;
413 val prop' = ins_concl prems concl;
420 fun vperm (Var _, Var _) = true (*2002 Pure/thm.ML *)
421 | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
422 | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
423 | vperm (t, u) = (t = u);
425 (*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*)
426 fun mem_term (_, []) = false
427 | mem_term (t, t'::ts) = t aconv t' orelse mem_term(t,ts);
428 fun subset_term ([], ys) = true
429 | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term(xs, ys);
430 fun eq_set_term (xs, ys) =
431 xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
432 (*a total, irreflexive ordering on index names*)
433 fun xless ((a,i), (b,j): indexname) = i<j orelse (i=j andalso a<b);
434 (*a partial ordering (not reflexive) for atomic terms*)
435 fun atless (Const (a,_), Const (b,_)) = a<b
436 | atless (Free (a,_), Free (b,_)) = a<b
437 | atless (Var(v,_), Var(w,_)) = xless(v,w)
438 | atless (Bound i, Bound j) = i<j
440 (*insert atomic term into partially sorted list, suppressing duplicates (?)*)
441 fun insert_aterm (t,us) =
442 let fun inserta [] = [t]
443 | inserta (us as u::us') =
444 if atless(t,u) then t::us
445 else if t=u then us (*duplicate*)
446 else u :: inserta(us')
449 (*Accumulates the Vars in the term, suppressing duplicates*)
450 fun add_term_vars (t, vars: term list) = case t of
451 Var _ => insert_aterm(t,vars)
452 | Abs (_,_,body) => add_term_vars(body,vars)
453 | f$t => add_term_vars (f, add_term_vars(t, vars))
455 fun term_vars t = add_term_vars(t,[]);
458 fun var_perm (t, u) = (*2002 Pure/thm.ML *)
459 vperm (t, u) andalso eq_set_term (term_vars t, term_vars u);
461 (*2002 fun decomp_simp, Pure/thm.ML *)
462 fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs)
463 andalso not (is_Var lhs);
467 if n < 0 then "-"^((string_of_int o abs) n)
468 else string_of_int n;
471 val it = "1" : string > str_of_int ~1;
472 val it = "-1" : string
478 if n>0 then b*(power b (n-1))
479 else error ("power "^(str_of_int b)^" "^(str_of_int n));
490 | gcd a b = if a < b then gcd (b mod a) a
491 else gcd (a mod b) b;
492 fun sign n = if n < 0 then ~1
493 else if n = 0 then 0 else 1;
494 fun sign2 n1 n2 = (sign n1) * (sign n2);
497 fun d dvd n = n mod d = 0;
500 let fun pdiv ds d n =
502 else if d dvd n then pdiv (d::ds) d (n div d)
511 fun doubles ds = (* ds is ordered *)
512 let fun dbls ds [] = ds
514 | dbls ds (i::i'::is) = if i=i' then dbls (i::ds) is
515 else dbls ds (i'::is)
518 val it = [] : int list
519 > doubles [2,3,3,5,5,7];
520 val it = [5,3] : int list*)
524 | squfact n = foldl op* (1, (doubles o divisors) n);
535 fun dest_type (Type(T,[])) = T
538 error ("... dest_type: not impl. for this type"));
540 fun term_of_num ntyp n = Free (str_of_int n, ntyp);
542 fun pairT T1 T2 = Type ("*", [T1, T2]);
543 (*> val t = str2term "(1,2)";
544 > type_of t = pairT HOLogic.realT HOLogic.realT;
547 fun PairT T1 T2 = ([T1, T2] ---> Type ("*", [T1, T2]));
548 (*> val t = str2term "(1,2)";
549 > val Const ("Pair",pT) $ _ $ _ = t;
550 > pT = PairT HOLogic.realT HOLogic.realT;
554 Const ("Pair", PairT (type_of t1) (type_of t2)) $ t1 $ t2;
555 (*> val t = str2term "(1,2)";
556 > val (t1, t2) = (str2term "1", str2term "2");
558 val it = true : bool*)
561 fun num_of_term (t as Free (s,_)) =
562 (case int_of_str s of
564 | NONE => error ("num_of_term not for "^ term2str t))
565 | num_of_term t = error ("num_of_term not for "^term2str t);
567 fun mk_factroot op_(*=thy.sqrt*) T fact root =
568 Const ("Groups.times_class.times", [T, T] ---> T) $ (term_of_num T fact) $
569 (Const (op_, T --> T) $ term_of_num T root);
571 val T = (type_of o term_of o the) (parse thy "#12::real");
572 val t = mk_factroot "SqRoot.sqrt" T 2 3;
574 val it = "#2 * sqrt #3 " : cterm
576 fun var_op_num v op_ optype ntyp n =
577 Const (op_, optype) $ v $
578 Free (str_of_int n, ntyp);
580 fun num_op_var v op_ optype ntyp n =
582 Free (str_of_int n, ntyp) $ v;
584 fun num_op_num T1 T2 (op_,Top) n1 n2 =
586 Free (str_of_int n1, T1) $ Free (str_of_int n2, T2);
588 > val t = num_op_num "Int" 3 4;
590 > string_of_cterm ((cterm_of thy) t);
593 fun const_in str (Const _) = false
594 | const_in str (Free (s,_)) = if strip_thy s = str then true else false
595 | const_in str (Bound _) = false
596 | const_in str (Var _) = false
597 | const_in str (Abs (_,_,body)) = const_in str body
598 | const_in str (f$u) = const_in str f orelse const_in str u;
600 > val t = (term_of o the o (parse thy)) "6 + 5 * sqrt 4 + 3";
603 > val t = (term_of o the o (parse thy)) "6 + 5 * 4 + 3";
605 val it = false : bool
608 (*used for calculating built in binary operations in Isabelle2002->Float.ML*)
609 (*fun calc "Groups.plus_class.plus" (n1, n2) = n1+n2
610 | calc "Groups.minus_class.minus" (n1, n2) = n1-n2
611 | calc "Groups.times_class.times" (n1, n2) = n1*n2
612 | calc "Rings.inverse_class.divide"(n1, n2) = n1 div n2
613 | calc "Atools.pow"(n1, n2) = power n1 n2
614 | calc op_ _ = error ("calc: operator = "^op_^" not defined");-----*)
615 fun calc_equ "op <" (n1, n2) = n1 < n2
616 | calc_equ "op <=" (n1, n2) = n1 <= n2
618 error ("calc_equ: operator = "^op_^" not defined");
619 fun sqrt (n:int) = if n < 0 then 0
620 (*FIXME ~~~*) else (trunc o Math.sqrt o Real.fromInt) n;
622 fun mk_thmid thmid op_ n1 n2 =
623 thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
625 fun dest_binop_typ (Type("fun",[range,Type("fun",[arg2,arg1])])) =
627 | dest_binop_typ _ = error "dest_binop_typ: not binary";
629 > val t = (term_of o the o (parse thy)) "#3^#4";
630 > val hT = type_of (head_of t);
632 val it = ("'a","nat","'a") : typ * typ * typ
636 (** transform binary numeralsstrings **)
637 (*Makarius 100308, hacked by WN*)
638 val numbers_to_string =
641 (case try HOLogic.dest_number t of
643 (*if T = @{typ int} orelse T = @{typ real} then WN*)
644 SOME (Free (signed_string_of_int i, T))
648 fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
649 | to_str (t as (u1 $ u2)) =
652 | NONE => to_str u1 $ to_str u2)
653 | to_str t = perhaps dest_num t;
656 (*.make uminus uniform:
657 Const ("uminus", _) $ Free ("2", "RealDef.real") --> Free ("-2", _)
658 to be used immediately before evaluation of numerals;
659 see Scripts/calculate.sml .*)
660 (*2002 fun(*app_num_tr'2 (Const("0",T)) = Free("0",T)
661 | app_num_tr'2 (Const("1",T)) = Free("1",T)
662 |*)app_num_tr'2 (t as Const("uminus",_) $ Free(s,T)) =
663 (case int_of_str s of SOME i =>
664 if i > 0 then Free("-"^s,T) else Free(s,T)
666 (*| app_num_tr'2 (t as Const(s,T)) = t
667 | app_num_tr'2 (Const("Numeral.number_of",Type ("fun", [_, T])) $ t) =
668 Free(NumeralSyntax.dest_bin_str t, T)
669 | app_num_tr'2 (t as Free(s,T)) = t
670 | app_num_tr'2 (t as Var(n,T)) = t
671 | app_num_tr'2 (t as Bound i) = t
672 *)| app_num_tr'2 (Abs(s,T,body)) = Abs(s,T, app_num_tr'2 body)
673 | app_num_tr'2 (t1 $ t2) = (app_num_tr'2 t1) $ (app_num_tr'2 t2)
674 | app_num_tr'2 t = t;
676 val uminus_to_string =
680 (Const ("Groups.uminus_class.uminus", _) $ Free (s, T)) =>
681 (case int_of_str s of
683 SOME (Free (signed_string_of_int (~1 * i), T))
687 fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
688 | to_str (t as (u1 $ u2)) =
691 | NONE => to_str u1 $ to_str u2)
692 | to_str t = perhaps dest_num t;
696 (*2002 fun num_str thm =
698 val {sign_ref = sign_ref, der = der, maxidx = maxidx,
699 shyps = shyps, hyps = hyps, (*tpairs = tpairs,*) prop = prop} =
701 val prop' = app_num_tr'1 prop;
702 in assbl_thm sign_ref der maxidx shyps hyps (*tpairs*) prop' end;*)
705 {thy_ref = thy_ref, tags = tags, maxidx = maxidx, shyps = shyps,
706 hyps = hyps, tpairs = tpairs, prop = prop}) = rep_thm_G thm
707 val prop' = numbers_to_string prop;
708 in assbl_thm deriv thy_ref tags maxidx shyps hyps tpairs prop' end;
710 fun get_thm' xstring = (*?covers 2009 Thm?!, replaces 2002 fun get_thm :
711 val it = fn : theory -> xstring -> Thm.thm*)
713 num_str (ProofContext.get_thm (thy2ctxt' "Isac") xstring));
715 (** get types of Free and Abs for parse' **)
716 (*11.1.00: not used, fix-typed +,*,-,^ instead *)
718 val dummyT = Type ("dummy",[]);
719 val dummyT = TVar (("DUMMY",0),[]);
721 (* assumes only 1 type for numerals
722 and different identifiers for Const, Free and Abs *)
725 fun get ts (Const(s,T)) = (s,T)::ts
726 | get ts (Free(s,T)) = if is_no s
727 then ("#",T)::ts else (s,T)::ts
728 | get ts (Var(n,T)) = ts
729 | get ts (Bound i) = ts
730 | get ts (Abs(s,T,body)) = get ((s,T)::ts) body
731 | get ts (t1 $ t2) = (get ts t1) @ (get ts t2)
732 in distinct (get [] t) end;
734 val t = (term_of o the o (parse thy)) "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)";
738 (*11.1.00: not used, fix-typed +,*,-,^ instead *)
739 fun set_types al (Const(s,T)) =
740 (case assoc (al,s) of
741 SOME T' => Const(s,T')
742 | NONE => (warning ("set_types: no type for "^s); Const(s,dummyT)))
743 | set_types al (Free(s,T)) =
745 (case assoc (al,"#") of
746 SOME T' => Free(s,T')
747 | NONE => (warning ("set_types: no type for numerals"); Free(s,T)))
748 else (case assoc (al,s) of
749 SOME T' => Free(s,T')
750 | NONE => (warning ("set_types: no type for "^s); Free(s,T)))
751 | set_types al (Var(n,T)) = Var(n,T)
752 | set_types al (Bound i) = Bound i
753 | set_types al (Abs(s,T,body)) =
754 (case assoc (al,s) of
755 SOME T' => Abs(s,T', set_types al body)
756 | NONE => (warning ("set_types: no type for "^s);
757 Abs(s,T, set_types al body)))
758 | set_types al (t1 $ t2) = (set_types al t1) $ (set_types al t2);
760 val t = (term_of o the o (parse thy)) "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)";
761 val al = get_types t;
763 val t = (term_of o the o (parse thy)) "x = #0 + #-1 * #-4";
765 val t' = set_types al t;
768 val it = "x = #0 + #-1 * #-4" : cterm
770 val t = (term_of o the o (parse thy))
771 "#5 * x + x ^^^ #2 = (#2 + x) ^^^ #2";
773 val t' = set_types al t;
776 uncaught exception TYPE (*^^^ is new, NOT in al*)
780 (** from Descript.ML **)
782 (** decompose an isa-list to an ML-list
783 i.e. [] belong to the meta-language, too **)
785 fun is_list ((Const("List.list.Cons",_)) $ _ $ _) = true
787 (* val (SOME ct) = parse thy "lll::real list";
788 > val ty = (#t o rep_cterm) ct;
790 val it = false : bool
791 > val (SOME ct) = parse thy "[lll]";
792 > val ty = (#t o rep_cterm) ct;
794 val it = true : bool *)
798 fun mk_Free (s,T) = Free(s,T);
799 fun mk_free T s = Free(s,T);
801 (*Special case: one argument cp from Isabelle2002/src/Pure/term.ML*)
802 fun subst_bound (arg, t) : term = (*WN100908 neglects 'raise Same.SAME'*)
803 let fun subst (t as Bound i, lev) =
804 if i<lev then t (*var is locally bound*)
805 else if i=lev then incr_boundvars lev arg
806 else Bound(i-1) (*loose: change it*)
807 | subst (Abs(a,T,body), lev) = Abs(a, T, subst(body,lev+1))
808 | subst (f$t, lev) = subst(f,lev) $ subst(t,lev)
812 (*instantiate let; necessary for ass_up*)
813 fun inst_abs thy (Const sT) = Const sT (*TODO.WN100907 drop thy*)
814 | inst_abs thy (Free sT) = Free sT
815 | inst_abs thy (Bound n) = Bound n
816 | inst_abs thy (Var iT) = Var iT
817 | inst_abs thy (Const ("Let",T1) $ e $ (Abs (v, T2, b))) =
818 let val b' = subst_bound (Free (v, T2), b);
819 (*fun variant_abs: term.ML*)
820 in Const ("Let", T1) $ inst_abs thy e $ (Abs (v, T2, inst_abs thy b')) end
821 | inst_abs thy (t1 $ t2) = inst_abs thy t1 $ inst_abs thy t2
822 | inst_abs thy t = t;
824 "Script Make_fun_by_explicit (f_::real) (v_::real) (eqs_::bool list) = \
825 \ (let h_ = (hd o (filterVar f_)) eqs_; \
826 \ e_1 = hd (dropWhile (ident h_) eqs_); \
827 \ vs_ = dropWhile (ident f_) (Vars h_); \
828 \ v_1 = hd (dropWhile (ident v_) vs_); \
829 \ (s_1::bool list)=(SubProblem(DiffApp_,[univar,equation],[no_met])\
830 \ [BOOL e_1, REAL v_1])\
831 \ in Substitute [(v_1 = (rhs o hd) s_1)] h_)";
832 > val ttt = (term_of o the o (parse thy)) scr;
833 > tracing(term2str ttt);
836 *** Const ( DiffApp.Make'_fun'_by'_explicit)
841 *** . . Const ( Fun.op o)
842 *** . . . Const ( List.hd)
843 *** . . . Const ( DiffApp.filterVar)
844 *** . . . . Free ( f_, )
845 *** . . . Free ( eqs_, )
847 *** . . . Const ( Let)
848 *** . . . . Const ( List.hd)
849 *** . . . . . Const ( List.dropWhile)
850 *** . . . . . . Const ( Atools.ident)
851 *** . . . . . . . Bound 0 <---- Free ( h_, )
852 *** . . . . . . Free ( eqs_, )
853 *** . . . . Abs( e_1,..
854 *** . . . . . Const ( Let)
855 *** . . . . . . Const ( List.dropWhile)
856 *** . . . . . . . Const ( Atools.ident)
857 *** . . . . . . . . Free ( f_, )
858 *** . . . . . . . Const ( Tools.Vars)
859 *** . . . . . . . . Bound 1 <---- Free ( h_, )
860 *** . . . . . . Abs( vs_,..
861 *** . . . . . . . Const ( Let)
862 *** . . . . . . . . Const ( List.hd)
863 *** . . . . . . . . . Const ( List.dropWhile)
864 *** . . . . . . . . . . Const ( Atools.ident)
865 *** . . . . . . . . . . . Free ( v_, )
866 *** . . . . . . . . . . Bound 0 <---- Free ( vs_, )
867 *** . . . . . . . . Abs( v_1,..
868 *** . . . . . . . . . Const ( Let)
869 *** . . . . . . . . . . Const ( Script.SubProblem)
870 *** . . . . . . . . . . . Const ( Pair)
871 *** . . . . . . . . . . . . Free ( DiffApp_, )
872 *** . . . . . . . . . . . . Const ( Pair)
873 *** . . . . . . . . . . . . . Const ( List.list.Cons)
874 *** . . . . . . . . . . . . . . Free ( univar, )
875 *** . . . . . . . . . . . . . . Const ( List.list.Cons)
876 *** . . . . . . . . . . . . . . . Free ( equation, )
877 *** . . . . . . . . . . . . . . . Const ( List.list.Nil)
878 *** . . . . . . . . . . . . . Const ( List.list.Cons)
879 *** . . . . . . . . . . . . . . Free ( no_met, )
880 *** . . . . . . . . . . . . . . Const ( List.list.Nil)
881 *** . . . . . . . . . . . Const ( List.list.Cons)
882 *** . . . . . . . . . . . . Const ( Script.BOOL)
883 *** . . . . . . . . . . . . . Bound 2 <----- Free ( e_1, )
884 *** . . . . . . . . . . . . Const ( List.list.Cons)
885 *** . . . . . . . . . . . . . Const ( Script.real_)
886 *** . . . . . . . . . . . . . . Bound 0 <----- Free ( v_1, )
887 *** . . . . . . . . . . . . . Const ( List.list.Nil)
888 *** . . . . . . . . . . Abs( s_1,..
889 *** . . . . . . . . . . . Const ( Script.Substitute)
890 *** . . . . . . . . . . . . Const ( List.list.Cons)
891 *** . . . . . . . . . . . . . Const ( Pair)
892 *** . . . . . . . . . . . . . . Bound 1 <----- Free ( v_1, )
893 *** . . . . . . . . . . . . . . Const ( Fun.op o)
894 *** . . . . . . . . . . . . . . . Const ( Tools.rhs)
895 *** . . . . . . . . . . . . . . . Const ( List.hd)
896 *** . . . . . . . . . . . . . . . Bound 0 <----- Free ( s_1, )
897 *** . . . . . . . . . . . . . Const ( List.list.Nil)
898 *** . . . . . . . . . . . . Bound 4 <----- Free ( h_, )
900 > val ttt' = inst_abs thy ttt;
901 > tracing(term2str ttt');
902 Script Make_fun_by_explicit f_ v_ eqs_ =
906 *** Const ( DiffApp.Make'_fun'_by'_explicit)
911 *** . . Const ( Fun.op o)
912 *** . . . Const ( List.hd)
913 *** . . . Const ( DiffApp.filterVar)
914 *** . . . . Free ( f_, )
915 *** . . . Free ( eqs_, )
917 *** . . . Const ( Let)
918 *** . . . . Const ( List.hd)
919 *** . . . . . Const ( List.dropWhile)
920 *** . . . . . . Const ( Atools.ident)
921 *** . . . . . . . Free ( h_, ) <---- Bound 0
922 *** . . . . . . Free ( eqs_, )
923 *** . . . . Abs( e_1,..
924 *** . . . . . Const ( Let)
925 *** . . . . . . Const ( List.dropWhile)
926 *** . . . . . . . Const ( Atools.ident)
927 *** . . . . . . . . Free ( f_, )
928 *** . . . . . . . Const ( Tools.Vars)
929 *** . . . . . . . . Free ( h_, ) <---- Bound 1
930 *** . . . . . . Abs( vs_,..
931 *** . . . . . . . Const ( Let)
932 *** . . . . . . . . Const ( List.hd)
933 *** . . . . . . . . . Const ( List.dropWhile)
934 *** . . . . . . . . . . Const ( Atools.ident)
935 *** . . . . . . . . . . . Free ( v_, )
936 *** . . . . . . . . . . Free ( vs_, ) <---- Bound 0
937 *** . . . . . . . . Abs( v_1,..
938 *** . . . . . . . . . Const ( Let)
939 *** . . . . . . . . . . Const ( Script.SubProblem)
940 *** . . . . . . . . . . . Const ( Pair)
941 *** . . . . . . . . . . . . Free ( DiffApp_, )
942 *** . . . . . . . . . . . . Const ( Pair)
943 *** . . . . . . . . . . . . . Const ( List.list.Cons)
944 *** . . . . . . . . . . . . . . Free ( univar, )
945 *** . . . . . . . . . . . . . . Const ( List.list.Cons)
946 *** . . . . . . . . . . . . . . . Free ( equation, )
947 *** . . . . . . . . . . . . . . . Const ( List.list.Nil)
948 *** . . . . . . . . . . . . . Const ( List.list.Cons)
949 *** . . . . . . . . . . . . . . Free ( no_met, )
950 *** . . . . . . . . . . . . . . Const ( List.list.Nil)
951 *** . . . . . . . . . . . Const ( List.list.Cons)
952 *** . . . . . . . . . . . . Const ( Script.BOOL)
953 *** . . . . . . . . . . . . . Free ( e_1, ) <----- Bound 2
954 *** . . . . . . . . . . . . Const ( List.list.Cons)
955 *** . . . . . . . . . . . . . Const ( Script.real_)
956 *** . . . . . . . . . . . . . . Free ( v_1, ) <----- Bound 0
957 *** . . . . . . . . . . . . . Const ( List.list.Nil)
958 *** . . . . . . . . . . Abs( s_1,..
959 *** . . . . . . . . . . . Const ( Script.Substitute)
960 *** . . . . . . . . . . . . Const ( List.list.Cons)
961 *** . . . . . . . . . . . . . Const ( Pair)
962 *** . . . . . . . . . . . . . . Free ( v_1, ) <----- Bound 1
963 *** . . . . . . . . . . . . . . Const ( Fun.op o)
964 *** . . . . . . . . . . . . . . . Const ( Tools.rhs)
965 *** . . . . . . . . . . . . . . . Const ( List.hd)
966 *** . . . . . . . . . . . . . . . Free ( s_1, ) <----- Bound 0
967 *** . . . . . . . . . . . . . Const ( List.list.Nil)
968 *** . . . . . . . . . . . . Free ( h_, ) <----- Bound 4
970 Note numbering of de Bruijn indexes !
972 Script Make_fun_by_explicit f_ v_ eqs_ =
973 let h_ = (hd o filterVar f_) eqs_;
974 e_1 = hd (dropWhile (ident h_ BOUND_0) eqs_);
975 vs_ = dropWhile (ident f_) (Vars h_ BOUND_1);
976 v_1 = hd (dropWhile (ident v_) vs_ BOUND_0);
978 SubProblem (DiffApp_, [univar, equation], [no_met])
979 [BOOL e_1 BOUND_2, REAL v_1 BOUND_0]
980 in Substitute [(v_1 BOUND_1 = (rhs o hd) s_1 BOUND_0)] h_ BOUND_4
984 fun T_a2real (Type (s, [])) =
985 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else Type (s, [])
986 | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts)
987 | T_a2real (TFree (s, srt)) =
988 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TFree (s, srt)
989 | T_a2real (TVar (("DUMMY",_),srt)) = HOLogic.realT;
991 (*FIXME .. fixes the type (+see Typefix.thy*)
992 fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T))
993 | typ_a2real (Free( s, T)) = (Free( s, T_a2real T))
994 | typ_a2real (Var( n, T)) = (Var( n, T_a2real T))
995 | typ_a2real (Bound i) = (Bound i)
996 | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t)
997 | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2);
999 ----------------6.8.02---------------------------------------------------
1001 val t = read_cterm (sign_of thy) (str,(TVar(("DUMMY",0),[])));
1005 val t = (app_num_tr' o term_of) t;
1009 val t = typ_a2real t;
1012 *** Const ( 1, real)
1015 val t = read_cterm (sign_of thy) (str,(TVar(("DUMMY",0),[])));
1018 *** Const ( Numeral.number_of, bin => 'a)
1019 *** . Const ( Numeral.bin.Bit, [bin, bool] => bin)
1020 *** . . Const ( Numeral.bin.Bit, [bin, bool] => bin)
1021 *** . . . Const ( Numeral.bin.Pls, bin)
1022 *** . . . Const ( True, bool)
1023 *** . . Const ( False, bool)
1024 val t = (app_num_tr' o term_of) t;
1028 val t = typ_a2real t;
1032 ----------------6.8.02---------------------------------------------------
1036 > val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[]))));
1037 val t = Free ("R","?DUMMY") : term
1038 > val t' = typ_a2real t;
1039 > (cterm_of thy) t';
1040 val it = "R::RealDef.real" : cterm
1043 > val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[]))));
1044 > atomty (typ_a2real t);
1046 *** Const ( op =, [RealDef.real, RealDef.real] => bool)
1047 *** Free ( R, RealDef.real)
1048 *** Free ( R, RealDef.real)
1049 > val t' = typ_a2real t;
1050 > (cterm_of thy) t';
1051 val it = "(R::RealDef.real) = R" : cterm
1053 > val str = "fixed_values [R=R]";
1054 > val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[]))));
1055 > val t' = typ_a2real t;
1056 > (cterm_of thy) t';
1057 val it = "fixed_values [(R::RealDef.real) = R]" : cterm
1060 (*TODO.WN0609: parse should return a term or a string
1061 (or even more comprehensive datastructure for error-messages)
1062 i.e. in wrapping with SOME term or NONE the latter is not sufficient*)
1063 (*2002 fun parseold thy str =
1065 val sgn = sign_of thy;
1066 val t = ((*typ_a2real o*) app_num_tr'1 o term_of)
1067 (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
1068 in SOME (cterm_of sgn t) end)
1073 fun parseold thy str =
1074 (let val t = ((*typ_a2real o*) numbers_to_string)
1075 (Syntax.read_term_global thy str)
1076 in SOME (cterm_of thy t) end)
1078 (*2002 fun parseN thy str =
1080 val sgn = sign_of thy;
1081 val t = ((*typ_a2real o app_num_tr'1 o*) term_of)
1082 (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
1083 in SOME (cterm_of sgn t) end)
1085 fun parseN thy str =
1086 (let val t = (*(typ_a2real o numbers_to_string)*)
1087 (Syntax.read_term_global thy str)
1088 in SOME (cterm_of thy t) end)
1090 (*2002 fun parse thy str =
1092 val sgn = sign_of thy;
1093 val t = (typ_a2real o app_num_tr'1 o term_of)
1094 (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
1095 in SOME (cterm_of sgn t) end) (*FIXXXXME 10.8.02: return term !!!*)
1097 (*2010 fun parse thy str =
1098 (let val t = (typ_a2real o app_num_tr'1) (Syntax.read_term_global thy str)
1099 in SOME (cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*)
1102 (let val t = (typ_a2real o numbers_to_string)
1103 (Syntax.read_term_global thy str)
1104 in SOME (cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*)
1107 > val (SOME ct) = parse thy "(-#5)^^^#3";
1108 > atomty (term_of ct);
1110 *** Const ( Nat.op ^, ['a, nat] => 'a)
1111 *** Const ( uminus, 'a => 'a)
1114 > val (SOME ct) = parse thy "R=R";
1115 > atomty (term_of ct);
1117 *** Const ( op =, [real, real] => bool)
1121 THIS IS THE OUTPUT FOR VERSION (3) above at typ_a2real !!!!!
1123 *** Const ( op =, [RealDef.real, RealDef.real] => bool)
1124 *** Free ( R, RealDef.real)
1125 *** Free ( R, RealDef.real) *)
1127 (*fun parse_patt thy str =
1128 (**** prep_pbt: syntax error in '#Where' of ["equation"]
1129 *** Exception- TOPLEVEL_ERROR raised
1130 *** At command "ML" (line 172 of "/usr/local/isabisac/src/Tools/isac/Knowledge/Poly.thy").*)
1131 (typ_a2real o numbers_to_string o
1132 ProofContext.read_term_pattern (thy2ctxt thy)) str;*)
1133 fun parse_patt thy str =
1134 ProofContext.read_term_pattern (thy2ctxt thy) str;
1135 (*fun parse_patt thy str = (thy, str) |>> thy2ctxt
1136 |-> ProofContext.read_term_pattern
1137 |> numbers_to_string
1140 (*version for testing local to theories*)
1141 fun str2term_ thy str = (term_of o the o (parse thy)) str;
1142 fun str2term str = (term_of o the o (parse (theory "Isac"))) str;
1143 fun strs2terms ss = map str2term ss;
1144 fun str2termN str = (term_of o the o (parseN (theory "Isac"))) str;
1146 (*+ makes a substitution from the output of Pattern.match +*)
1147 (*fun mk_subs ((id, _):indexname, t:term) = (Free (id,type_of t), t);*)
1148 fun mk_subs (subs: ((string * int) * (Term.typ * Term.term)) list) =
1149 let fun mk_sub ((id, _), (ty, tm)) = (Free (id, ty), tm) in
1150 map mk_sub subs end;
1152 val atomthm = atomt o #prop o rep_thm;
1154 (*.instantiate #prop thm with bound variables (as Free).*)
1155 fun inst_bdv [] t = t : term
1156 | inst_bdv (instl: (term*term) list) t =
1157 let fun subst (v as Var((s,_),T)) =
1160 if_none (assoc(instl,Free(s,T))) (Free(s,T))
1162 | subst (Abs(a,T,body)) = Abs(a, T, subst body)
1163 | subst (f$t') = subst f $ subst t'
1164 | subst t = if_none (assoc(instl,t)) t
1168 (*WN050829 caution: is_atom (str2term"q_0/2 * L * x") = true !!!
1169 use length (vars term) = 1 instead*)
1170 fun is_atom (Const ("Float.Float",_) $ _) = true
1171 | is_atom (Const ("ComplexI.I'_'_",_)) = true
1172 | is_atom (Const ("Groups.times_class.times",_) $ t $ Const ("ComplexI.I'_'_",_)) = is_atom t
1173 | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ Const ("ComplexI.I'_'_",_)) = is_atom t1
1174 | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $
1175 (Const ("Groups.times_class.times",_) $ t2 $ Const ("ComplexI.I'_'_",_))) =
1176 is_atom t1 andalso is_atom t2
1177 | is_atom (Const _) = true
1178 | is_atom (Free _) = true
1179 | is_atom (Var _) = true
1180 | is_atom _ = false;
1181 (* val t = str2term "q_0/2 * L * x";
1185 (*val t = str2term "Float ((1,2),(0,0))";
1187 val it = true : bool
1188 > val t = str2term "Float ((1,2),(0,0)) * I__";
1190 val it = true : bool
1191 > val t = str2term "Float ((1,2),(0,0)) + Float ((3,4),(0,0)) * I__";
1193 val it = true : bool
1194 > val t = str2term "1 + 2*I__";
1195 > val Const ("Groups.plus_class.plus",_) $ t1 $ (Const ("Groups.times_class.times",_) $ t2 $ Const ("ComplexI.I'_'_",_)) = t;
1198 (*.adaption from Isabelle/src/Pure/term.ML; reports if ALL Free's
1199 have found a substitution (required for evaluating the preconditions
1200 of _incomplete_ models).*)
1201 fun subst_atomic_all [] t = (false, (*TODO may be 'true' for some terms ?*)
1203 | subst_atomic_all (instl: (term*term) list) t =
1204 let fun subst (Abs(a,T,body)) =
1205 let val (all, body') = subst body
1206 in (all, Abs(a, T, body')) end
1208 let val (all1, f') = subst f
1209 val (all2, tt') = subst tt
1210 in (all1 andalso all2, f' $ tt') end
1211 | subst (t as Free _) =
1212 if is_num t then (true, t) (*numerals cannot be subst*)
1213 else (case assoc(instl,t) of
1214 SOME t' => (true, t')
1215 | NONE => (false, t))
1216 | subst t = (true, if_none (assoc(instl,t)) t)
1219 (*.add two terms with a type given.*)
1221 let val T1 = type_of t1
1223 in if T1 <> T2 then raise TYPE ("mk_add gets ",[T1, T2],[t1,t2])
1224 else (Const ("Groups.plus_class.plus", [T1, T2] ---> T1) $ t1 $ t2)