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: Proof.context -> path -> term -> term
20 val perm: term -> term -> bool (*old original Isabelle code*)
21 val contains_Var: term -> bool
22 val dest_binop_typ: typ -> typ * typ * typ
23 val dest_equals: term -> term * term
24 val free2str: term -> string
25 val ids2str: term -> string list
26 val ins_concl: term -> term -> term
27 val inst_abs: term -> term
28 val inst_bdv: LibraryC.subst -> term -> term
33 val mk_frac: typ -> int * (int * int) -> term
34 val numerals_to_Free: term -> term
35 val term_of_num: typ -> int -> term
36 val num_of_term: term -> int
37 val to_string: term -> string
38 val int_of_str: string -> int
39 val isastr_of_int: int -> string
40 val int_opt_of_string: string -> int option
42 val isalist2list: term -> term list
43 val list2isalist: typ -> term list -> term
44 val isapair2pair: term -> term * term (* rename to dest_pair, compare HOLogic.dest_string *)
46 val is_atom: term -> bool
47 val string_of_atom: term -> string
48 val is_variable: term -> bool
49 val is_bdv: string -> bool
50 val is_bdv_subst: term -> bool
51 val guess_bdv_typ: term -> typ
52 val is_equality: term -> bool
53 val is_expliceq: term -> bool
54 val is_f_x: term -> bool
55 val is_list: term -> bool
56 val is_bool_list: term -> bool
57 val dest_listT: typ -> typ
58 val is_num: term -> bool
59 val is_num': string -> bool
60 val string_of_num: term -> string
61 val variable_constant_pair: term * term -> bool
63 val mk_add: term -> term -> term
64 val mk_free: typ -> string -> term
65 val mk_equality: term * term -> term
66 val mk_factroot: string -> typ -> int -> int -> term
67 val mk_Free: string * typ -> term
68 val mk_thmid: string -> string -> string -> string
69 val mk_num_op_num: typ -> typ -> string * typ -> int -> int -> term
70 val mk_num_op_var: term -> string -> typ -> typ -> int -> term
71 val mk_var_op_num: term -> string -> typ -> typ -> int -> term
73 val matches: theory -> term -> term -> bool
75 val str_of_free_opt: term -> string option
76 val str_of_int: int -> string
77 val strip_imp_prems': term -> term option
78 val subst_atomic_all: LibraryC.subst -> term -> bool * term
80 val pairt: term -> term -> term
81 val pairT: typ -> typ -> typ
82 val raise_type_conflicts: term list -> unit
83 val strip_trueprop: term -> term
85 val var2free: term -> term
86 val vars: term -> term list (* recognises numerals, should replace "fun vars_of" TODOO*)
87 val vars': term list -> term list
88 val vars_of: term -> term list (* deprecated TODOO: see differences in test/../termC.sml*)
89 val dest_list': term -> term list
90 val negates: term -> term -> bool
92 val contains_one_of: thm * (string * typ) list -> bool
93 val contains_Const_typeless: term list -> term -> bool
94 val sym_trm : term -> term (* unused code, kept as hints to design ideas *)
96 val string_of_detail: Proof.context -> term -> string
97 (*from isac_test for Minisubpbl*)
98 val atom_typ: Proof.context -> typ -> unit
101 val mk_negative: typ -> term -> term
102 val mk_Var: term -> term
103 val scala_of_term: term -> string
105 val atom_write: Proof.context -> term -> unit
106 val atom_trace: Proof.context -> term -> unit
108 val atom_write_detail: Proof.context -> term -> unit
109 val atom_trace_detail: Proof.context -> term -> unit
114 structure TermC(**): TERM_ISAC(**) =
118 type as_string = UnparseC.term_as_string
119 val empty = UnparseC.term_empty
120 val typ_empty = UnparseC.typ_empty
122 datatype lrd = L (*t1 in "t1$t2"*)
123 | R (*t2 in "t1$t2"*) | D; (*b in Abs(_,_,b*)
124 type path = lrd list;
128 fun string_of_path k = (strs2str' o (map ldr2str)) k;
129 (*go to a location in a term and fetch the resective sub-term*)
131 | sub_at (D :: p) (Abs(_, _, body)) = sub_at p body
132 | sub_at (L :: p) (t1 $ _) = sub_at p t1
133 | sub_at (R :: p) (_ $ t2) = sub_at p t2
134 | sub_at l t = raise TERM ("sub_at: no " ^ string_of_path l ^ " for ", [t]);
136 if length l > 1 then sub_at (drop_last l) t else raise ERROR ("go_up [] " ^ UnparseC.term ctxt t)
138 fun isastr_of_int i = if i >= 0 then string_of_int i else "-" ^ string_of_int (abs i)
140 fun matches thy tm pa =
141 (Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true)
142 handle Pattern.MATCH => false
144 (** transform typ / term to a String to be parsed by Scala after transport via libisabelle **)
145 fun string_of_detail ctxt t =
147 fun ato (Const (a, T)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ UnparseC.typ ctxt T ^ ")"
148 | ato (Free (a, T)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ UnparseC.typ ctxt T ^ ")"
149 | ato (Var ((a, i), T)) n =
150 "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), " ^ UnparseC.typ ctxt T ^ ")"
151 | ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
152 | ato (Abs(a, T, body)) n =
153 "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ UnparseC.typ ctxt T ^ ",.." ^ ato body (n + 1)
154 | ato (f $ t) n = ato f n ^ ato t (n + 1)
155 in "\n*** " ^ ato t 0 ^ "\n***" end;
159 fun scala_of_typ (Type (s, typs)) =
160 enclose "Type(" ")" (quote s ^ ", " ^
161 (typs |> map scala_of_typ |> commas |> enclose "List(" ")"))
162 | scala_of_typ (TFree (s, sort)) =
163 enclose "TFree(" ")" (quote s ^ ", " ^ (sort |> map quote |> commas |> enclose "List(" ")"))
164 | scala_of_typ (TVar ((s, i), sort)) =
165 enclose "TVar(" ")" (
166 enclose "(" ")," (quote s ^ ", " ^ quote (string_of_int i)) ^
167 (sort |> map quote |> commas |> enclose "List(" ")"))
169 fun scala_of_term (Const (s, T)) =
170 enclose "Const (" ")" (quote s ^ ", " ^ scala_of_typ T)
171 | scala_of_term (Free (s, T)) =
172 enclose "Free(" ")" (quote s ^ ", " ^ scala_of_typ T)
173 | scala_of_term (Var ((s, i), T)) =
174 enclose "TVar(" ")" (
175 enclose "(" ")," (quote s ^ ", " ^ quote (string_of_int i)) ^
177 | scala_of_term (Bound i) = enclose "Bound(" ")" (string_of_int i)
178 | scala_of_term (Abs (s, T, t)) =
181 scala_of_typ T ^ ", " ^
183 | scala_of_term (t1 $ t2) =
184 enclose "App(" ")" (scala_of_term t1 ^ ", " ^ scala_of_term t2)
187 (* see structure's bare bones.
188 for Isabelle standard output compare 2017 "structure ML_PP" *)
191 fun ato n (Type (s, [])) = "\n*** " ^ indent n ^ "Type (" ^ s ^",[])"
192 | ato n (Type (s, Ts)) = "\n*** " ^ indent n ^ "Type (" ^ s ^ ",[" ^ atol (n + 1) Ts
193 | ato n (TFree (s, sort)) = "\n*** " ^ indent n ^ "TFree (" ^ s ^ ", " ^ strs2str' sort
194 | ato n (TVar ((s, i), sort)) =
195 "\n*** " ^ indent n ^ "TVar ((" ^ s ^ ", " ^ string_of_int i ^ strs2str' sort
196 and atol n [] = "\n*** " ^ indent n ^ "]"
197 | atol n (T :: Ts) = (ato n T ^ atol n Ts)
198 in tracing (ato 0 t ^ "\n") end;
203 fun ato (Const (a, _)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", _)"
204 | ato (Free (a, _)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", _)"
205 | ato (Var ((a, i), _)) n =
206 "\n*** " ^ indent n ^ "Var (" ^ a ^ ", " ^ string_of_int i ^ "), _)"
207 | ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
208 | ato (Abs (a, _, body)) n = "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1)
209 | ato (f $ t) n = (ato f n ^ ato t (n + 1))
211 fun atom_write _ t = writeln ("\n*** -------------" ^ ato t 0 ^ "\n***");
212 fun atom_trace _ t = tracing ("\n*** -------------" ^ ato t 0 ^ "\n***");
215 fun atom_write_detail ctxt t = (writeln o (string_of_detail ctxt)) t;
216 fun atom_trace_detail ctxt t = (tracing o (string_of_detail ctxt)) t;
219 (* contains the term a VAR(("*",_),_) ? *)
220 fun contains_Var (Abs(_,_,body)) = contains_Var body
221 | contains_Var (f $ f') = contains_Var f orelse contains_Var f'
222 | contains_Var (Var _) = true
223 | contains_Var _ = false;
226 if n < 0 then "-" ^ ((string_of_int o abs) n)
227 else string_of_int n;
228 val int_of_str = Value.parse_int;
230 val int_opt_of_string = ThmC_Def.int_opt_of_string
231 fun is_num' str = case int_opt_of_string str of SOME _ => true | NONE => false;
233 fun is_num (Const (\<^const_name>\<open>numeral\<close>, _) $ _) = true
234 | is_num (Const (\<^const_name>\<open>uminus\<close>, _) $
235 (Const (\<^const_name>\<open>numeral\<close>, _) $ _)) = true
236 | is_num (Const (\<^const_name>\<open>one_class.one\<close>, _)) = true
237 | is_num (Const (\<^const_name>\<open>uminus\<close>, _) $ Const (\<^const_name>\<open>one_class.one\<close>, _)) = true
238 | is_num (Const (\<^const_name>\<open>zero_class.zero\<close>, _)) = true
239 | is_num (Const (\<^const_name>\<open>uminus\<close>, _) $ Const (\<^const_name>\<open>zero_class.zero\<close>, _)) = true
242 fun string_of_num n = (n |> HOLogic.dest_number |> snd |> string_of_int)
244 fun lhs (Const (\<^const_name>\<open>HOL.eq\<close>,_) $ l $ _) = l
245 | lhs t = raise TERM ("lhs called with ", [t]);
246 fun rhs (Const (\<^const_name>\<open>HOL.eq\<close>,_) $ _ $ r) = r
247 | rhs t = raise TERM ("rhs called with ", [t]);
250 fun mk_negative T t = Const (\<^const_name>\<open>uminus\<close>, T --> T) $ t
251 fun mk_frac T (sg, (i1, i2)) =
253 if i2 = 1 then HOLogic.mk_number T i1
254 else Const (\<^const_name>\<open>divide_class.divide\<close>, T --> T --> T) $
255 HOLogic.mk_number T i1 $ HOLogic.mk_number T i2
256 else (*take negative*)
257 if i2 = 1 then mk_negative T (HOLogic.mk_number T i1)
258 else Const (\<^const_name>\<open>divide_class.divide\<close>, T --> T --> T) $
259 mk_negative T (HOLogic.mk_number T i1) $ HOLogic.mk_number T i2
261 val numerals_to_Free = (* Makarius 100308 *)
264 (case try HOLogic.dest_number t of
265 SOME (T, i) => SOME (Free (signed_string_of_int i, T))
267 fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
268 | to_str (t as (u1 $ u2)) =
269 (case dest_num t of SOME t' => t' | NONE => to_str u1 $ to_str u2)
270 | to_str t = perhaps dest_num t;
273 val term_of_num = HOLogic.mk_number;
274 fun num_of_term t = t |> HOLogic.dest_number |> snd;
275 (* accomodate string-representation for int to term-orders *)
276 fun to_string t = t |> num_of_term |> signed_string_of_int
278 fun is_variable (t as Free _) = not (is_num t)
279 | is_variable _ = false;
280 fun is_Free (Free _) = true | is_Free _ = false;
281 fun is_fun_id (Const _) = true
282 | is_fun_id (Free _) = true
283 | is_fun_id _ = false;
284 fun is_f_x (f $ x) = is_fun_id f andalso is_Free x
286 (* precondition: TermC.is_atom v andalso TermC.is_atom c *)
287 fun is_const (Const _) = true | is_const _ = false;
288 fun variable_constant_pair (v, c) =
289 if (is_variable v andalso (is_const c orelse is_num c)) orelse
290 (is_variable c andalso (is_const v orelse is_num v))
296 fun scan vs (Const _) = vs
297 | scan vs (t as Free _) = (*if is_num' s then vs else*) t :: vs
298 | scan vs (t as Var _) = t :: vs
299 | scan vs (Bound _) = vs
300 | scan vs (Abs (_, _, t)) = scan vs t
301 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
302 in ((distinct op =) o (scan [])) t end;
303 fun vars' ts = ts |> map vars |> flat |> distinct op =
305 (* bypass Isabelle's Pretty, which requires ctxt *)
308 fun scan vs (t as Const (s, _) $ arg) =
309 if is_num t then vs else scan (s :: vs) arg
310 | scan vs (Const (s as "Partial_Fractions.AA", _)) = s :: vs (*how get rid of spec.case?*)
311 | scan vs (Const (s as "Partial_Fractions.BB", _)) = s :: vs (*how get rid of spec.case?*)
312 | scan vs (Const _) = vs
313 | scan vs (Free (s, _)) = if is_num' s then vs else s :: vs
314 | scan vs (Var ((s, i), _)) = (s ^ "_" ^ string_of_int i) :: vs
315 | scan vs (Bound _) = vs
316 | scan vs (Abs (s, _, t)) = scan (s :: vs) t
317 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
318 in ((distinct op =) o (scan [])) t
320 fun is_bdv str = case Symbol.explode str of "b"::"d"::"v"::_ => true | _ => false;
321 (* instantiate #prop thm with bound vars_of (as Free) *)
322 fun inst_bdv [] t = t
323 | inst_bdv (instl: (term*term) list) t =
325 fun subst (v as Var((s, _), T)) =
326 (case Symbol.explode s of
327 "b"::"d"::"v"::_ => if_none (assoc(instl,Free(s,T))) (Free(s,T))
329 | subst (Abs(a, T, body)) = Abs(a, T, subst body)
330 | subst (f $ t') = subst f $ subst t'
331 | subst t = if_none (assoc (instl, t)) t
334 (* is a term a substitution for a bdv as found in programs and tactics *)
335 fun is_bdv_subst (Const (\<^const_name>\<open>Cons\<close>, _) $
336 (Const (\<^const_name>\<open>Pair\<close>, _) $ str $ _) $ _) = is_bdv (HOLogic.dest_string str)
337 | is_bdv_subst _ = false;
339 (* this shall be improved due to future requirements *)
340 fun guess_bdv_typ t = t |> vars |> hd |> type_of
342 fun free2str (Free (s, _)) = s
343 | free2str t = raise TERM ("free2str not for ", [t]);
344 fun str_of_free_opt (Free (s, _)) = SOME s
345 | str_of_free_opt _ = NONE
347 (* compare Logic.unvarify_global, which rejects Free *)
348 fun var2free (t as Const _) = t
349 | var2free (t as Free _) = t
350 | var2free (Var((s, _), T)) = Free (s,T)
351 | var2free (t as Bound _) = t
352 | var2free (Abs(s, T, t)) = Abs(s, T, var2free t)
353 | var2free (t1 $ t2) = (var2free t1) $ (var2free t2);
356 \<^isac_test>\<open> (*TODO: check with new numerals --vv*)
357 (* Logic.varify does NOT take care of 'Free ("1", _)'*)
358 fun mk_Var (t as Const _) = t
359 | mk_Var (t as Free (s, T)) = if is_num' s then t else Var ((s, 0), T)
360 | mk_Var (t as Var _) = t
361 | mk_Var (t as Bound _) = t
362 | mk_Var (Abs (s, T, t)) = Abs (s, T, mk_Var t)
363 | mk_Var (t1 $ t2) = (mk_Var t1) $ (mk_Var t2);
366 fun mk_listT T = Type (\<^type_name>\<open>list\<close>, [T]);
367 fun list_const T = Const (\<^const_name>\<open>Cons\<close>, [T, mk_listT T] ---> mk_listT T);
368 fun list2isalist T [] = Const (\<^const_name>\<open>Nil\<close>, mk_listT T)
369 | list2isalist T (t :: ts) = (list_const T) $ t $ (list2isalist T ts);
371 fun isapair2pair (Const (\<^const_name>\<open>Pair\<close>,_) $ a $ b) = (a, b)
373 raise TERM ("isapair2pair called with ", [t]);
374 fun isalist2list ls =
376 fun get es (Const(\<^const_name>\<open>Cons\<close>, _) $ t $ ls) = get (t :: es) ls
377 | get es (Const(\<^const_name>\<open>Nil\<close>, _)) = es
378 | get _ t = raise TERM ("isalist2list applied to NON-list: ", [t])
379 in (rev o (get [])) ls end;
381 fun is_list ((Const (\<^const_name>\<open>Cons\<close>, _)) $ _ $ _) = true
383 fun dest_listT (Type (\<^type_name>\<open>list\<close>, [T])) = T
384 | dest_listT T = raise TYPE ("dest_listT: list type expected", [T], []);
386 (if dest_listT (Term.type_of t) = HOLogic.boolT then true else false)
387 handle TYPE _ => false
390 fun dest_binop_typ (Type (\<^type_name>\<open>fun\<close>, [range, Type (\<^type_name>\<open>fun\<close>, [arg2, arg1])])) = (arg1, arg2, range)
391 | dest_binop_typ _ = raise ERROR "dest_binop_typ: not binary";
392 fun dest_equals (Const(\<^const_name>\<open>HOL.eq\<close>, _) $ t $ u) = (t, u) (* Pure/logic.ML: Const ("==", ..*)
393 | dest_equals t = raise TERM ("dest_equals'", [t]);
394 fun is_equality (Const(\<^const_name>\<open>HOL.eq\<close>,_) $ _ $ _) = true (* logic.ML: Const ("=="*)
395 | is_equality _ = false;
396 fun mk_equality (t, u) = (Const(\<^const_name>\<open>HOL.eq\<close>, [type_of t, type_of u] ---> HOLogic.boolT) $ t $ u);
397 fun is_expliceq (Const(\<^const_name>\<open>HOL.eq\<close>,_) $ (Free _) $ _) = true
398 | is_expliceq _ = false;
399 fun strip_trueprop (Const (\<^const_name>\<open>Trueprop\<close>, _) $ t) = t
400 | strip_trueprop t = t;
402 (* (A1==>...An==>B) goes to (A1==>...An==>) Pure/logic.ML: term -> term list*)
403 fun strip_imp_prems' (Const (\<^const_name>\<open>Pure.imp\<close>, _) $ A $ t) =
405 fun coll_prems As (Const(\<^const_name>\<open>Pure.imp\<close>, _) $ A $ t) =
406 coll_prems (As $ (Logic.implies $ A)) t
407 | coll_prems As _ = SOME As
408 in coll_prems (Logic.implies $ A) t end
409 | strip_imp_prems' _ = NONE; (* *)
411 (* (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch, 2002 Pure/thm.ML *)
412 fun ins_concl (Const (\<^const_name>\<open>Pure.imp\<close>, _) $ A $ t) B = Logic.implies $ A $ (ins_concl t B)
413 | ins_concl (Const (\<^const_name>\<open>Pure.imp\<close>, _) $ A ) B = Logic.implies $ A $ B
414 | ins_concl t B = raise TERM ("ins_concl", [t, B]);
416 fun vperm (Var _, Var _) = true (* 2002 Pure/thm.ML *)
417 | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
418 | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
419 | vperm (t, u) = (t = u);
421 (*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*)
422 fun mem_term (_, []) = false
423 | mem_term (t, t' :: ts) = t aconv t' orelse mem_term (t, ts);
424 fun subset_term ([], _) = true
425 | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term (xs, ys);
426 fun eq_set_term (xs, ys) =
427 xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
428 (*a total, irreflexive ordering on index names*)
429 fun xless ((a, i), (b, j): indexname) = i<j orelse (i = j andalso a < b);
430 (*a partial ordering (not reflexive) for atomic terms*)
431 fun atless (Const (a, _), Const (b, _)) = a < b
432 | atless (Free (a, _), Free (b, _)) = a < b
433 | atless (Var (v, _), Var (w, _)) = xless (v, w)
434 | atless (Bound i, Bound j) = i < j
436 (*insert atomic term into partially sorted list, suppressing duplicates (?)*)
437 fun insert_aterm (t,us) =
438 let fun inserta [] = [t]
439 | inserta (us as u::us') =
440 if atless(t,u) then t::us
441 else if t=u then us (*duplicate*)
442 else u :: inserta us'
445 (* Accumulates the Vars in the term, suppressing duplicates *)
446 fun add_term_vars (t, vars: term list) = case t of
447 Var _ => insert_aterm (t, vars)
448 | Abs (_, _, body) => add_term_vars (body, vars)
449 | f$t => add_term_vars (f, add_term_vars (t, vars))
451 fun term_vars t = add_term_vars (t, []);
453 (*2002 Pure/thm.ML *)
454 fun var_perm (t, u) = vperm (t, u) andalso eq_set_term (term_vars t, term_vars u);
455 (*2002 fun decomp_simp, Pure/thm.ML *)
456 fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs) andalso not (is_Var lhs);
459 fun pairT T1 T2 = Type (\<^type_name>\<open>prod\<close>, [T1, T2]);
460 fun PairT T1 T2 = ([T1, T2] ---> Type (\<^type_name>\<open>prod\<close>, [T1, T2]));
461 fun pairt t1 t2 = Const (\<^const_name>\<open>Pair\<close>, PairT (type_of t1) (type_of t2)) $ t1 $ t2;
463 fun mk_factroot op_(*=thy.sqrt*) T fact root =
464 Const (\<^const_name>\<open>times\<close>, [T, T] ---> T) $ (term_of_num T fact) $
465 (Const (op_, T --> T) $ term_of_num T root);
466 fun mk_var_op_num v op_ optype ntyp n = Const (op_, optype) $ v $ HOLogic.mk_number ntyp n;
467 fun mk_num_op_var v op_ optype ntyp n = Const (op_, optype) $ HOLogic.mk_number ntyp n $ v;
468 fun mk_num_op_num T1 T2 (op_, Top) n1 n2 =
469 Const (op_, Top) $ HOLogic.mk_number T1 n1 $ HOLogic.mk_number T2 n2;
470 fun mk_thmid thmid n1 n2 =
471 thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
474 val (T1, T2) = (type_of t1, type_of t2)
476 if T1 <> T2 then raise TYPE ("mk_add gets ", [T1, T2], [t1,t2])
477 else (Const (\<^const_name>\<open>plus\<close>, [T1, T2] ---> T1) $ t1 $ t2)
480 (** transform binary numeralsstrings **)
481 fun mk_Free (s,T) = Free (s, T);
482 fun mk_free T s = Free (s, T);
484 (*Special case: one argument cp from Isabelle2002/src/Pure/term.ML*)
485 fun subst_bound (arg, t) =
487 fun subst (t as Bound i, lev) =
488 if i < lev then t (*var is locally bound*)
489 else if i = lev then incr_boundvars lev arg
490 else Bound (i - 1) (*loose: change it*)
491 | subst (Abs(a, T, body), lev) = Abs (a, T, subst (body, lev + 1))
492 | subst (f$t, lev) = subst(f, lev) $ subst(t, lev)
496 (* instantiate let; necessary for scan_up1 *)
497 fun inst_abs (Const sT) = Const sT
498 | inst_abs (Free sT) = Free sT
499 | inst_abs (Bound n) = Bound n
500 | inst_abs (Var iT) = Var iT
501 | inst_abs (Const (\<^const_name>\<open>Let\<close>,T1) $ e $ (Abs (v, T2, b))) =
502 let val b' = subst_bound (Free (v, T2), b); (*fun variant_abs: term.ML*)
503 in Const (\<^const_name>\<open>Let\<close>, T1) $ inst_abs e $ (Abs (v, T2, inst_abs b')) end
504 | inst_abs (t1 $ t2) = inst_abs t1 $ inst_abs t2
507 fun is_atom (Const _) = true
508 | is_atom (Free _) = true
509 | is_atom (Var _) = true
510 | is_atom t = is_num t;
511 fun string_of_atom (t as Const (\<^const_name>\<open>numeral\<close>, _) $ _) = to_string t
512 | string_of_atom (t as Const (\<^const_name>\<open>one_class.one\<close>, _)) = to_string t
513 | string_of_atom (t as Const (\<^const_name>\<open>zero_class.zero\<close>, _)) = to_string t
514 | string_of_atom (Const (str, _)) = str
515 | string_of_atom (Free (str, _)) = str
516 | string_of_atom (Var ((str, int), _)) = str ^ "_" ^ string_of_int int
517 | string_of_atom _ = "DUMMY-string_of_atom";
519 (* from Pure/term.ML; reports if ALL Free's have found a substitution
520 (required for evaluating the preconditions of _incomplete_ models) *)
521 fun subst_atomic_all [] t = (false (*TODO may be 'true' for some terms ?*), t)
522 | subst_atomic_all instl t =
524 fun subst (Abs (a, T, body)) =
526 val (all, body') = subst body
527 in (all, Abs(a, T, body')) end
530 val (all1, f') = subst f
531 val (all2, tt') = subst tt
532 in (all1 andalso all2, f' $ tt') end
533 | subst (t as Free _) =
534 if is_num t then (true, t) (*numerals cannot be subst*)
535 else (case assoc (instl, t) of
536 SOME t' => (true, t')
537 | NONE => (false, t))
538 | subst t = (true, if_none (assoc(instl,t)) t)
541 fun op contains_one_of (thm, ids) =
542 Term.exists_Const (fn id => member op= ids id) (Thm.prop_of thm)
544 fun var_for vs (t as Const (str, _)) id =
546 else if id = strip_thy str then t :: vs else vs
547 | var_for vs (t as Free (str, _)) id = if id = str then t :: vs else vs
548 | var_for vs (t as Var (idn, _)) id = if id = Term.string_of_vname idn then t :: vs else vs
549 | var_for vs (Bound _) _ = vs
550 | var_for vs (Abs (_, _, t)) id = var_for vs t id
551 | var_for vs (t1 $ t2) id = (var_for vs t1 id) @ (var_for vs t2 id)
554 [\<^const_name>\<open>plus\<close>, \<^const_name>\<open>minus\<close>,
555 \<^const_name>\<open>divide\<close>, \<^const_name>\<open>times\<close>,
556 \<^const_name>\<open>realpow\<close>];
557 (* treat Free, Const, Var as vars_of in polynomials *)
560 val var_ids = t |> ids2str |> subtract op = poly_consts |> map strip_thy |> sort string_ord
561 in (map (var_for [] t) var_ids) |> flat |> distinct op = end
563 (* this may decompose an object-language isa-list;
564 use only, if description is not available, eg. not input ?WN:14.5.03 ??!?*)
565 fun dest_list' t = if is_list t then isalist2list t else [t];
567 fun negat (Const (\<^const_name>\<open>Not\<close>, _) $ P, P') = P = P'
569 fun negates p1 p2 = negat (p1, p2) orelse negat (swap (p1, p2));
571 fun raise_type_conflicts ts =
573 val dups = duplicates (op =) (map (fst o dest_Free) ts)
574 val confl = filter (fn Free (str, _) => member op = dups str
579 else raise TYPE ("formalisation inconsistent w.r.t. type inference: ",
580 map (snd o dest_Free)confl, confl)
583 (* expects t as Const *)
584 fun contains_Const_typeless ts t = (t
586 |> member (fn (t1, t2) => fst (dest_Const t1) = fst (dest_Const t2)) ts
587 ) handle TERM("dest_Const", _) => raise TERM ("contains_Const_typeless", [t])
589 (* WN100910 weaker than fun sym_thm for Theory.axioms_of in isa02 *)
592 val (lhs, rhs) = (dest_equals o strip_trueprop o Logic.strip_imp_concl) trm
593 val trm' = case strip_imp_prems' trm of
594 NONE => mk_equality (rhs, lhs)
595 | SOME cs => ins_concl cs (mk_equality (rhs, lhs))