1 (* Title: extends Isabelle/src/Pure/term.ML
2 Author: Walther Neuper 1999, Mathias Lehnfeld
3 (c) due to copyright terms
7 (* TERM_C extends Isabelle's naming conventions: "C" indicates Isac add-ons to an Isabelle module *)
14 datatype lrd = D | L | R
16 val string_of_path: path -> string
17 val sub_at: path -> term -> term
18 val go_up: path -> term -> term
20 val contains_Var: term -> bool
21 val dest_binop_typ: typ -> typ * typ * typ
22 val dest_equals: term -> term * term
23 val free2str: term -> string
24 val ids2str: term -> string list
25 val ins_concl: term -> term -> term
26 val inst_abs: term -> term
27 val inst_bdv: (term * term) list -> term -> term
29 val term_of_num: typ -> int -> term
30 val num_of_term: term -> int
31 val int_of_str: string -> int
32 val isastr_of_int: int -> string
33 val int_opt_of_string: string -> int option (* belongs to TermC *)
35 val isalist2list: term -> term list
36 val list2isalist: typ -> term list -> term
37 val isapair2pair: term -> term * term (* rename to dest_pair, compare HOLogic.dest_string *)
39 val is_atom: term -> bool
40 val is_const: term -> bool
41 val is_variable: term -> bool
42 val is_bdv: string -> bool
43 val is_bdv_subst: term -> bool
44 val guess_bdv_typ: term -> typ
45 val is_equality: term -> bool
46 val is_expliceq: term -> bool
47 val is_f_x: term -> bool
48 val is_list: term -> bool
49 val is_bool_list: term -> bool
50 val dest_listT: typ -> typ
51 val is_num: term -> bool
52 val is_num': string -> bool
53 val variable_constant_pair: term * term -> bool
55 val mk_add: term -> term -> term
56 val mk_free: typ -> string -> term
57 val mk_equality: term * term -> term
58 val mk_factroot: string -> typ -> int -> int -> term
59 val mk_Free: string * typ -> term
60 val mk_thmid: string -> string -> string -> string
61 val mk_num_op_num: typ -> typ -> string * typ -> int -> int -> term
62 val mk_num_op_var: term -> string -> typ -> typ -> int -> term
63 val mk_var_op_num: term -> string -> typ -> typ -> int -> term
65 val matches: theory -> term -> term -> bool
66 val parse: theory -> string -> cterm option
67 val parseN: theory -> string -> cterm option
68 val parseNEW: Proof.context -> string -> term option
69 val parseNEW': Proof.context -> string -> term
70 val parseNEW'': theory -> string -> term
71 val parseold: theory -> string -> cterm option
72 val parse_patt: theory -> string -> term
73 val perm: term -> term -> bool
75 val str_of_free_opt: term -> string option
76 val str_of_int: int -> string
77 val str2term: string -> term
78 val strip_imp_prems': term -> term option
79 val subst_atomic_all: (term * term) list -> term -> bool * term
80 val term_detail2str: term -> string
82 val pairt: term -> term -> term
83 val pairT: typ -> typ -> typ
84 val raise_type_conflicts: term list -> unit
85 val strip_trueprop: term -> term
87 val numbers_to_string: term -> term
88 val uminus_to_string: term -> term
90 val var2free: term -> term
91 val vars: term -> term list (* recognises numerals, should replace "fun vars_of" *)
92 val vars': term list -> term list
93 val vars_of: term -> term list (* deprecated *)
94 val dest_list': term -> term list
95 val negates: term -> term -> bool
98 val scala_of_term: term -> string
99 val atomtyp(*<-- atom_typ TODO*): typ -> unit
100 val atomty: term -> unit
101 val atomw: term -> unit
102 val atomt: term -> unit
103 val atomwy: term -> unit
104 val atomty_thy: ThyC.id -> term -> unit
105 val free2var: term -> term
106 val typ_a2real: term -> term
108 val contains_one_of: thm * (string * typ) list -> bool
109 val contains_Const_typeless: term list -> term -> bool
110 (*----- unused code, kept as hints to design ideas ---------------------------------------------*)
111 val sym_trm : term -> term
115 structure TermC(**): TERM_ISAC(**) =
119 type as_string = UnparseC.term_as_string
120 val empty = UnparseC.term_empty
121 val typ_empty = UnparseC.typ_empty
123 datatype lrd = L (*t1 in "t1$t2"*)
124 | R (*t2 in "t1$t2"*) | D; (*b in Abs(_,_,b*)
125 type path = lrd list;
129 fun string_of_path k = (strs2str' o (map ldr2str)) k;
130 (*go to a location in a term and fetch the resective sub-term*)
132 | sub_at (D :: p) (Abs(_, _, body)) = sub_at p body
133 | sub_at (L :: p) (t1 $ _) = sub_at p t1
134 | sub_at (R :: p) (_ $ t2) = sub_at p t2
135 | sub_at l t = raise TERM ("sub_at: no " ^ string_of_path l ^ " for ", [t]);
137 if length l > 1 then sub_at (drop_last l) t else raise ERROR ("go_up [] " ^ UnparseC.term t)
139 fun isastr_of_int i = if i >= 0 then string_of_int i else "-" ^ string_of_int (abs i)
141 fun matches thy tm pa =
142 (Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true)
143 handle Pattern.MATCH => false
145 (** transform typ / term to a String to be parsed by Scala after transport via libisabelle **)
148 fun scala_of_typ (Type (s, typs)) =
149 enclose "Type(" ")" (quote s ^ ", " ^
150 (typs |> map scala_of_typ |> commas |> enclose "List(" ")"))
151 | scala_of_typ (TFree (s, sort)) =
152 enclose "TFree(" ")" (quote s ^ ", " ^ (sort |> map quote |> commas |> enclose "List(" ")"))
153 | scala_of_typ (TVar ((s, i), sort)) =
154 enclose "TVar(" ")" (
155 enclose "(" ")," (quote s ^ ", " ^ quote (string_of_int i)) ^
156 (sort |> map quote |> commas |> enclose "List(" ")"))
158 fun scala_of_term (Const (s, T)) =
159 enclose "Const(" ")" (quote s ^ ", " ^ scala_of_typ T)
160 | scala_of_term (Free (s, T)) =
161 enclose "Free(" ")" (quote s ^ ", " ^ scala_of_typ T)
162 | scala_of_term (Var ((s, i), T)) =
163 enclose "TVar(" ")" (
164 enclose "(" ")," (quote s ^ ", " ^ quote (string_of_int i)) ^
166 | scala_of_term (Bound i) = enclose "Bound(" ")" (string_of_int i)
167 | scala_of_term (Abs (s, T, t)) =
170 scala_of_typ T ^ ", " ^
172 | scala_of_term (t1 $ t2) =
173 enclose "App(" ")" (scala_of_term t1 ^ ", " ^ scala_of_term t2)
175 (* see structure's bare bones.
176 for Isabelle standard output compare 2017 "structure ML_PP" *)
179 fun ato n (Type (s, [])) = "\n*** " ^ indent n ^ "Type (" ^ s ^",[])"
180 | ato n (Type (s, Ts)) = "\n*** " ^ indent n ^ "Type (" ^ s ^ ",[" ^ atol (n + 1) Ts
181 | ato n (TFree (s, sort)) = "\n*** " ^ indent n ^ "TFree (" ^ s ^ ", " ^ strs2str' sort
182 | ato n (TVar ((s, i), sort)) =
183 "\n*** " ^ indent n ^ "TVar ((" ^ s ^ ", " ^ string_of_int i ^ strs2str' sort
184 and atol n [] = "\n*** " ^ indent n ^ "]"
185 | atol n (T :: Ts) = (ato n T ^ atol n Ts)
186 in tracing (ato 0 t ^ "\n") end;
189 fun ato (Const (a, _)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", _)"
190 | ato (Free (a, _)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", _)"
191 | ato (Var ((a, i), _)) n =
192 "\n*** " ^ indent n ^ "Var (" ^ a ^ ", " ^ string_of_int i ^ "), _)"
193 | ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
194 | ato (Abs (a, _, body)) n = "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1)
195 | ato (f $ t) n = (ato f n ^ ato t (n + 1))
197 fun atomw t = writeln ("\n*** -------------" ^ ato t 0 ^ "\n***");
198 fun atomt t = tracing ("\n*** -------------" ^ ato t 0 ^ "\n***");
202 fun term_detail2str t =
204 fun ato (Const (a, T)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ UnparseC.typ T ^ ")"
205 | ato (Free (a, T)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ UnparseC.typ T ^ ")"
206 | ato (Var ((a, i), T)) n =
207 "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), " ^ UnparseC.typ T ^ ")"
208 | ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
209 | ato (Abs(a, T, body)) n =
210 "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ UnparseC.typ T ^ ",.." ^ ato body (n + 1)
211 | ato (f $ t) n = ato f n ^ ato t (n + 1)
212 in "\n*** " ^ ato t 0 ^ "\n***" end;
215 fun term_detail2str_thy thy t =
217 fun ato (Const (a, T)) n =
218 "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ UnparseC.typ_by_thyID thy T ^ ")"
219 | ato (Free (a, T)) n =
220 "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ UnparseC.typ_by_thyID thy T ^ ")"
221 | ato (Var ((a, i), T)) n =
222 "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), " ^
223 UnparseC.typ_by_thyID thy T ^ ")"
225 "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
226 | ato (Abs(a, T, body)) n =
227 "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ UnparseC.typ_by_thyID thy T ^ ",.." ^
229 | ato (f $ t) n = ato f n ^ ato t (n + 1)
230 in "\n*** " ^ ato t 0 ^ "\n***" end;
231 fun atomwy t = (writeln o term_detail2str) t;
232 fun atomty t = (tracing o term_detail2str) t;
233 fun atomty_thy thy t = (tracing o (term_detail2str_thy thy)) t;
236 (* contains the term a VAR(("*",_),_) ? *)
237 fun contains_Var (Abs(_,_,body)) = contains_Var body
238 | contains_Var (f $ f') = contains_Var f orelse contains_Var f'
239 | contains_Var (Var _) = true
240 | contains_Var _ = false;
243 if n < 0 then "-" ^ ((string_of_int o abs) n)
244 else string_of_int n;
245 val int_of_str = Value.parse_int;
247 val int_opt_of_string = ThmC_Def.int_opt_of_string
249 fun is_num' str = case int_opt_of_string str of SOME _ => true | NONE => false;
250 fun is_num (Free (s, _)) = if is_num' s then true else false | is_num _ = false;
251 fun term_of_num ntyp n = Free (str_of_int n, ntyp);
252 fun num_of_term (t as (Free (istr, _))) =
253 (case int_opt_of_string istr of SOME i => i | NONE => raise TERM ("num_of_term: NOT int ", [t]))
254 | num_of_term t = raise TERM ("num_of_term: NOT Free ", [t])
256 fun is_const (Const _) = true | is_const _ = false;
257 fun is_variable (t as Free _) = not (is_num t)
258 | is_variable _ = false;
259 fun is_Free (Free _) = true | is_Free _ = false;
260 fun is_fun_id (Const _) = true
261 | is_fun_id (Free _) = true
262 | is_fun_id _ = false;
263 fun is_f_x (f $ x) = is_fun_id f andalso is_Free x
265 (* precondition: TermC.is_atom v andalso TermC.is_atom c *)
266 fun variable_constant_pair (v, c) =
267 if (is_variable v andalso (is_const c orelse is_num c)) orelse
268 (is_variable c andalso (is_const v orelse is_num v))
274 fun scan vs (Const _) = vs
275 | scan vs (t as Free (s, _)) = if is_num' s then vs else t :: vs
276 | scan vs (t as Var _) = t :: vs
277 | scan vs (Bound _) = vs
278 | scan vs (Abs (_, _, t)) = scan vs t
279 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
280 in ((distinct op =) o (scan [])) t end;
281 fun vars' ts = ts |> map vars |> flat |> distinct op =
283 (* bypass Isabelle's Pretty, which requires ctxt *)
286 fun scan vs (Const (s, _)) = if is_num' s then vs else s :: vs
287 | scan vs (Free (s, _)) = if is_num' s then vs else s :: vs
288 | scan vs (Var ((s, i), _)) = (s ^ "_" ^ string_of_int i) :: vs
289 | scan vs (Bound _) = vs
290 | scan vs (Abs (s, _, t)) = scan (s :: vs) t
291 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
292 in ((distinct op =) o (scan [])) t end;
293 fun is_bdv str = case Symbol.explode str of "b"::"d"::"v"::_ => true | _ => false;
294 (* instantiate #prop thm with bound variables (as Free) *)
295 fun inst_bdv [] t = t
296 | inst_bdv (instl: (term*term) list) t =
298 fun subst (v as Var((s, _), T)) =
299 (case Symbol.explode s of
300 "b"::"d"::"v"::_ => if_none (assoc(instl,Free(s,T))) (Free(s,T))
302 | subst (Abs(a, T, body)) = Abs(a, T, subst body)
303 | subst (f $ t') = subst f $ subst t'
304 | subst t = if_none (assoc (instl, t)) t
307 (* is a term a substitution for a bdv as found in programs and tactics *)
308 fun is_bdv_subst (Const ("List.list.Cons", _) $
309 (Const ("Product_Type.Pair", _) $ str $ _) $ _) = is_bdv (HOLogic.dest_string str)
310 | is_bdv_subst _ = false;
312 (* this shall be improved due to future requirements *)
313 fun guess_bdv_typ t = t |> vars |> hd |> type_of
315 fun free2str (Free (s, _)) = s
316 | free2str t = raise ERROR ("free2str not for " ^ UnparseC.term t);
317 fun str_of_free_opt (Free (s, _)) = SOME s
318 | str_of_free_opt _ = NONE
320 (* compare Logic.unvarify_global, which rejects Free *)
321 fun var2free (t as Const _) = t
322 | var2free (t as Free _) = t
323 | var2free (Var((s, _), T)) = Free (s,T)
324 | var2free (t as Bound _) = t
325 | var2free (Abs(s, T, t)) = Abs(s, T, var2free t)
326 | var2free (t1 $ t2) = (var2free t1) $ (var2free t2);
329 (* Logic.varify does NOT take care of 'Free ("1", _)'*)
330 fun free2var (t as Const _) = t
331 | free2var (t as Free (s, T)) = if is_num' s then t else Var ((s, 0), T)
332 | free2var (t as Var _) = t
333 | free2var (t as Bound _) = t
334 | free2var (Abs (s, T, t)) = Abs (s, T, free2var t)
335 | free2var (t1 $ t2) = (free2var t1) $ (free2var t2);
338 fun mk_listT T = Type ("List.list", [T]);
339 fun list_const T = Const ("List.list.Cons", [T, mk_listT T] ---> mk_listT T);
340 fun list2isalist T [] = Const ("List.list.Nil", mk_listT T)
341 | list2isalist T (t :: ts) = (list_const T) $ t $ (list2isalist T ts);
343 fun isapair2pair (Const ("Product_Type.Pair",_) $ a $ b) = (a, b)
345 raise ERROR ("isapair2pair called with " ^ UnparseC.term t);
346 fun isalist2list ls =
348 fun get es (Const("List.list.Cons", _) $ t $ ls) = get (t :: es) ls
349 | get es (Const("List.list.Nil", _)) = es
350 | get _ t = raise TERM ("isalist2list applied to NON-list: ", [t])
351 in (rev o (get [])) ls end;
353 fun is_list ((Const ("List.list.Cons", _)) $ _ $ _) = true
355 fun dest_listT (Type ("List.list", [T])) = T
356 | dest_listT T = raise TYPE ("dest_listT: list type expected", [T], []);
358 (if dest_listT (Term.type_of t) = HOLogic.boolT then true else false)
359 handle TYPE _ => false
362 fun dest_binop_typ (Type ("fun", [range, Type ("fun", [arg2, arg1])])) = (arg1, arg2, range)
363 | dest_binop_typ _ = raise ERROR "dest_binop_typ: not binary";
364 fun dest_equals (Const("HOL.eq", _) $ t $ u) = (t, u) (* Pure/logic.ML: Const ("==", ..*)
365 | dest_equals t = raise TERM ("dest_equals'", [t]);
366 fun is_equality (Const("HOL.eq",_) $ _ $ _) = true (* logic.ML: Const("=="*)
367 | is_equality _ = false;
368 fun mk_equality (t, u) = (Const("HOL.eq", [type_of t, type_of u] ---> HOLogic.boolT) $ t $ u);
369 fun is_expliceq (Const("HOL.eq",_) $ (Free _) $ _) = true
370 | is_expliceq _ = false;
371 fun strip_trueprop (Const ("HOL.Trueprop", _) $ t) = t
372 | strip_trueprop t = t;
374 (* (A1==>...An==>B) goes to (A1==>...An==>) Pure/logic.ML: term -> term list*)
375 fun strip_imp_prems' (Const ("Pure.imp", _) $ A $ t) =
377 fun coll_prems As (Const("Pure.imp", _) $ A $ t) =
378 coll_prems (As $ (Logic.implies $ A)) t
379 | coll_prems As _ = SOME As
380 in coll_prems (Logic.implies $ A) t end
381 | strip_imp_prems' _ = NONE; (* *)
383 (* (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch, 2002 Pure/thm.ML *)
384 fun ins_concl (Const ("Pure.imp", _) $ A $ t) B = Logic.implies $ A $ (ins_concl t B)
385 | ins_concl (Const ("Pure.imp", _) $ A ) B = Logic.implies $ A $ B
386 | ins_concl t B = raise TERM ("ins_concl", [t, B]);
388 fun vperm (Var _, Var _) = true (* 2002 Pure/thm.ML *)
389 | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
390 | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
391 | vperm (t, u) = (t = u);
393 (*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*)
394 fun mem_term (_, []) = false
395 | mem_term (t, t' :: ts) = t aconv t' orelse mem_term (t, ts);
396 fun subset_term ([], _) = true
397 | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term (xs, ys);
398 fun eq_set_term (xs, ys) =
399 xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
400 (*a total, irreflexive ordering on index names*)
401 fun xless ((a, i), (b, j): indexname) = i<j orelse (i = j andalso a < b);
402 (*a partial ordering (not reflexive) for atomic terms*)
403 fun atless (Const (a, _), Const (b, _)) = a < b
404 | atless (Free (a, _), Free (b, _)) = a < b
405 | atless (Var (v, _), Var (w, _)) = xless (v, w)
406 | atless (Bound i, Bound j) = i < j
408 (*insert atomic term into partially sorted list, suppressing duplicates (?)*)
409 fun insert_aterm (t,us) =
410 let fun inserta [] = [t]
411 | inserta (us as u::us') =
412 if atless(t,u) then t::us
413 else if t=u then us (*duplicate*)
414 else u :: inserta us'
417 (* Accumulates the Vars in the term, suppressing duplicates *)
418 fun add_term_vars (t, vars: term list) = case t of
419 Var _ => insert_aterm (t, vars)
420 | Abs (_, _, body) => add_term_vars (body, vars)
421 | f$t => add_term_vars (f, add_term_vars (t, vars))
423 fun term_vars t = add_term_vars (t, []);
425 (*2002 Pure/thm.ML *)
426 fun var_perm (t, u) = vperm (t, u) andalso eq_set_term (term_vars t, term_vars u);
427 (*2002 fun decomp_simp, Pure/thm.ML *)
428 fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs) andalso not (is_Var lhs);
431 fun pairT T1 T2 = Type ("*", [T1, T2]);
432 fun PairT T1 T2 = ([T1, T2] ---> Type ("*", [T1, T2]));
433 fun pairt t1 t2 = Const ("Product_Type.Pair", PairT (type_of t1) (type_of t2)) $ t1 $ t2;
435 fun mk_factroot op_(*=thy.sqrt*) T fact root =
436 Const ("Groups.times_class.times", [T, T] ---> T) $ (term_of_num T fact) $
437 (Const (op_, T --> T) $ term_of_num T root);
438 fun mk_var_op_num v op_ optype ntyp n = Const (op_, optype) $ v $ Free (str_of_int n, ntyp);
439 fun mk_num_op_var v op_ optype ntyp n = Const (op_, optype) $ Free (str_of_int n, ntyp) $ v;
440 fun mk_num_op_num T1 T2 (op_, Top) n1 n2 =
441 Const (op_, Top) $ Free (str_of_int n1, T1) $ Free (str_of_int n2, T2);
442 fun mk_thmid thmid n1 n2 =
443 thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
446 val (T1, T2) = (type_of t1, type_of t2)
448 if T1 <> T2 then raise TYPE ("mk_add gets ", [T1, T2], [t1,t2])
449 else (Const ("Groups.plus_class.plus", [T1, T2] ---> T1) $ t1 $ t2)
452 (** transform binary numeralsstrings **)
453 val numbers_to_string = ThmC_Def.num_to_Free
454 val uminus_to_string = ThmC_Def.uminus_to_string
456 fun mk_Free (s,T) = Free (s, T);
457 fun mk_free T s = Free (s, T);
459 (*Special case: one argument cp from Isabelle2002/src/Pure/term.ML*)
460 fun subst_bound (arg, t) =
462 fun subst (t as Bound i, lev) =
463 if i < lev then t (*var is locally bound*)
464 else if i = lev then incr_boundvars lev arg
465 else Bound (i - 1) (*loose: change it*)
466 | subst (Abs(a, T, body), lev) = Abs (a, T, subst (body, lev + 1))
467 | subst (f$t, lev) = subst(f, lev) $ subst(t, lev)
471 (* instantiate let; necessary for scan_up1 *)
472 fun inst_abs (Const sT) = Const sT
473 | inst_abs (Free sT) = Free sT
474 | inst_abs (Bound n) = Bound n
475 | inst_abs (Var iT) = Var iT
476 | inst_abs (Const ("HOL.Let",T1) $ e $ (Abs (v, T2, b))) =
477 let val b' = subst_bound (Free (v, T2), b); (*fun variant_abs: term.ML*)
478 in Const ("HOL.Let", T1) $ inst_abs e $ (Abs (v, T2, inst_abs b')) end
479 | inst_abs (t1 $ t2) = inst_abs t1 $ inst_abs t2
482 (* for parse and parse_patt: fix all types to real *)
483 fun T_a2real (Type (s, [])) =
484 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else Type (s, [])
485 | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts)
486 | T_a2real (TFree (s, srt)) =
487 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TFree (s, srt)
488 | T_a2real (TVar (("DUMMY", _), _)) = HOLogic.realT
489 | T_a2real (TVar ((s, i), srt)) =
490 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TVar ((s, i), srt)
491 fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T))
492 | typ_a2real (Free( s, T)) = (Free( s, T_a2real T))
493 | typ_a2real (Var( n, T)) = (Var( n, T_a2real T))
494 | typ_a2real (Bound i) = (Bound i)
495 | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t)
496 | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2);
498 (* TODO clarify parse with Test_Isac *)
499 fun parseold thy str = (* before 2002 *)
501 let val t = ((*typ_a2real o*) numbers_to_string) (Syntax.read_term_global thy str)
502 in Thm.global_cterm_of thy t end\<close>;
503 fun parseN thy str = (* introduced 2002 *)
505 let val t = (*(typ_a2real o numbers_to_string)*) (Syntax.read_term_global thy str)
506 in Thm.global_cterm_of thy t end\<close>;
507 fun parse thy str = (* introduced 2010 *)
509 let val t = (typ_a2real o numbers_to_string) (Syntax.read_term_global thy str)
510 in Thm.global_cterm_of thy t end\<close>;
512 (*WN110317 parseNEW will replace parse after introduction of ctxt completed*)
513 fun parseNEW ctxt str = \<^try>\<open>Syntax.read_term ctxt str |> numbers_to_string\<close>;
514 fun parseNEW' ctxt str =
515 case parseNEW ctxt str of
517 | NONE => raise TERM ("NO parseNEW' for " ^ str, [])
518 fun parseNEW'' thy str =
519 case parseNEW (ThyC.to_ctxt thy) str of
521 | NONE => raise TERM ("NO parseNEW'' for " ^ str, [])
523 (* parse term patterns; Var ("v",_), i.e. "?v", are required for instantiation
524 WN130613 probably compare to
525 http://www.mail-archive.com/isabelle-dev@mailbroy.informatik.tu-muenchen.de/msg04249.html*)
526 fun parse_patt thy str = (thy, str)
528 |-> Proof_Context.read_term_pattern
529 |> numbers_to_string (*TODO drop*)
530 |> typ_a2real; (*TODO drop*)
531 fun str2term str = parse_patt (ThyC.get_theory "Isac_Knowledge") str
533 fun is_atom (Const ("Float.Float",_) $ _) = true
534 | is_atom (Const _) = true
535 | is_atom (Free _) = true
536 | is_atom (Var _) = true
539 (* from Pure/term.ML; reports if ALL Free's have found a substitution
540 (required for evaluating the preconditions of _incomplete_ models) *)
541 fun subst_atomic_all [] t = (false (*TODO may be 'true' for some terms ?*), t)
542 | subst_atomic_all instl t =
544 fun subst (Abs (a, T, body)) =
546 val (all, body') = subst body
547 in (all, Abs(a, T, body')) end
550 val (all1, f') = subst f
551 val (all2, tt') = subst tt
552 in (all1 andalso all2, f' $ tt') end
553 | subst (t as Free _) =
554 if is_num t then (true, t) (*numerals cannot be subst*)
555 else (case assoc (instl, t) of
556 SOME t' => (true, t')
557 | NONE => (false, t))
558 | subst t = (true, if_none (assoc(instl,t)) t)
561 fun op contains_one_of (thm, ids) =
562 Term.exists_Const (fn id => member op= ids id) (Thm.prop_of thm)
564 fun var_for vs (t as Const (str, _)) id = if id = strip_thy str then t :: vs else vs
565 | var_for vs (t as Free (str, _)) id = if id = str then t :: vs else vs
566 | var_for vs (t as Var (idn, _)) id = if id = Term.string_of_vname idn then t :: vs else vs
567 | var_for vs (Bound _) _ = vs
568 | var_for vs (Abs (_, _, t)) id = var_for vs t id
569 | var_for vs (t1 $ t2) id = (var_for vs t1 id) @ (var_for vs t2 id)
571 val poly_consts = (* TODO: adopt syntax-const from Isabelle*)
572 ["Groups.plus_class.plus", "Groups.minus_class.minus",
573 "Rings.divide_class.divide", "Groups.times_class.times",
574 "Transcendental.powr"];
575 (* treat Free, Const, Var as variables in polynomials *)
578 val var_ids = t |> ids2str |> subtract op = poly_consts |> map strip_thy |> sort string_ord
579 in (map (var_for [] t) var_ids) |> flat |> distinct op = end
581 (* this may decompose an object-language isa-list;
582 use only, if description is not available, eg. not input ?WN:14.5.03 ??!?*)
583 fun dest_list' t = if is_list t then isalist2list t else [t];
585 fun negat (Const ("HOL.Not", _) $ P, P') = P = P'
587 fun negates p1 p2 = negat (p1, p2) orelse negat (swap (p1, p2));
589 fun raise_type_conflicts ts =
591 val dups = duplicates (op =) (map (fst o dest_Free) ts)
592 val confl = filter (fn Free (str, _) => member op = dups str
597 else raise TYPE ("formalisation inconsistent w.r.t. type inference: ",
598 map (snd o dest_Free)confl, confl)
601 (* expects t as Const *)
602 fun contains_Const_typeless ts t = (t
604 |> member (fn (t1, t2) => fst (dest_Const t1) = fst (dest_Const t2)) ts
605 ) handle TERM("dest_Const", _) => raise TERM ("contains_Const_typeless", [t])
607 (* WN100910 weaker than fun sym_thm for Theory.axioms_of in isa02 *)
610 val (lhs, rhs) = (dest_equals o strip_trueprop o Logic.strip_imp_concl) trm
611 val trm' = case strip_imp_prems' trm of
612 NONE => mk_equality (rhs, lhs)
613 | SOME cs => ins_concl cs (mk_equality (rhs, lhs))