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 mk_frac: typ -> int * (int * int) -> term
30 val numerals_to_Free: term -> term
31 val term_of_num: typ -> int -> term
32 val num_of_term: term -> int
33 val to_string: term -> string
34 val int_of_str: string -> int
35 val isastr_of_int: int -> string
36 val int_opt_of_string: string -> int option
38 val isalist2list: term -> term list
39 val list2isalist: typ -> term list -> term
40 val isapair2pair: term -> term * term (* rename to dest_pair, compare HOLogic.dest_string *)
42 val is_atom: term -> bool
43 val string_of_atom: term -> string
44 val is_const: term -> bool
45 val is_variable: term -> bool
46 val is_bdv: string -> bool
47 val is_bdv_subst: term -> bool
48 val guess_bdv_typ: term -> typ
49 val is_equality: term -> bool
50 val is_expliceq: term -> bool
51 val is_f_x: term -> bool
52 val is_list: term -> bool
53 val is_bool_list: term -> bool
54 val dest_listT: typ -> typ
55 val is_num: term -> bool
56 val is_num': string -> bool
57 val string_of_num: term -> string
58 val variable_constant_pair: term * term -> bool
60 val mk_add: term -> term -> term
61 val mk_free: typ -> string -> term
62 val mk_equality: term * term -> term
63 val mk_factroot: string -> typ -> int -> int -> term
64 val mk_Free: string * typ -> term
65 val mk_thmid: string -> string -> string -> string
66 val mk_num_op_num: typ -> typ -> string * typ -> int -> int -> term
67 val mk_num_op_var: term -> string -> typ -> typ -> int -> term
68 val mk_var_op_num: term -> string -> typ -> typ -> int -> term
70 val matches: theory -> term -> term -> bool
71 val parse: theory -> string -> cterm option
72 val parseN: theory -> string -> cterm option
73 val parseNEW: Proof.context -> string -> term option
74 val parseNEW': Proof.context -> string -> term
75 val parseNEW'': theory -> string -> term
76 val parseold: theory -> string -> cterm option
77 val parse_strict: theory -> string -> term
78 val parse_patt: theory -> string -> term
79 val perm: term -> term -> bool
81 val str_of_free_opt: term -> string option
82 val str_of_int: int -> string
83 val str2term: string -> term
84 val strip_imp_prems': term -> term option
85 val subst_atomic_all: (term * term) list -> term -> bool * term
86 val term_detail2str: term -> string
88 val pairt: term -> term -> term
89 val pairT: typ -> typ -> typ
90 val raise_type_conflicts: term list -> unit
91 val strip_trueprop: term -> term
93 val numbers_to_string: term -> term
94 val uminus_to_string: term -> term
96 val var2free: term -> term
97 val vars: term -> term list (* recognises numerals, should replace "fun vars_of" TODOO*)
98 val vars': term list -> term list
99 val vars_of: term -> term list (* deprecated TODOO: see differences in test/../termC.sml*)
100 val dest_list': term -> term list
101 val negates: term -> term -> bool
104 val mk_negative: typ -> term -> term
105 val scala_of_term: term -> string
106 val atomtyp(*<-- atom_typ TODO*): typ -> unit
107 val atomty: term -> unit
108 val atomw: term -> unit
109 val atomt: term -> unit
110 val atomwy: term -> unit
111 val atomty_thy: ThyC.id -> term -> unit
112 val free2var: term -> term
113 val typ_a2real: term -> term
115 val contains_one_of: thm * (string * typ) list -> bool
116 val contains_Const_typeless: term list -> term -> bool
117 (*----- unused code, kept as hints to design ideas ---------------------------------------------*)
118 val sym_trm : term -> term
122 structure TermC(**): TERM_ISAC(**) =
126 type as_string = UnparseC.term_as_string
127 val empty = UnparseC.term_empty
128 val typ_empty = UnparseC.typ_empty
130 datatype lrd = L (*t1 in "t1$t2"*)
131 | R (*t2 in "t1$t2"*) | D; (*b in Abs(_,_,b*)
132 type path = lrd list;
136 fun string_of_path k = (strs2str' o (map ldr2str)) k;
137 (*go to a location in a term and fetch the resective sub-term*)
139 | sub_at (D :: p) (Abs(_, _, body)) = sub_at p body
140 | sub_at (L :: p) (t1 $ _) = sub_at p t1
141 | sub_at (R :: p) (_ $ t2) = sub_at p t2
142 | sub_at l t = raise TERM ("sub_at: no " ^ string_of_path l ^ " for ", [t]);
144 if length l > 1 then sub_at (drop_last l) t else raise ERROR ("go_up [] " ^ UnparseC.term t)
146 fun isastr_of_int i = if i >= 0 then string_of_int i else "-" ^ string_of_int (abs i)
148 fun matches thy tm pa =
149 (Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true)
150 handle Pattern.MATCH => false
152 (** transform typ / term to a String to be parsed by Scala after transport via libisabelle **)
155 fun scala_of_typ (Type (s, typs)) =
156 enclose "Type(" ")" (quote s ^ ", " ^
157 (typs |> map scala_of_typ |> commas |> enclose "List(" ")"))
158 | scala_of_typ (TFree (s, sort)) =
159 enclose "TFree(" ")" (quote s ^ ", " ^ (sort |> map quote |> commas |> enclose "List(" ")"))
160 | scala_of_typ (TVar ((s, i), sort)) =
161 enclose "TVar(" ")" (
162 enclose "(" ")," (quote s ^ ", " ^ quote (string_of_int i)) ^
163 (sort |> map quote |> commas |> enclose "List(" ")"))
165 fun scala_of_term (Const (s, T)) =
166 enclose "Const (" ")" (quote s ^ ", " ^ scala_of_typ T)
167 | scala_of_term (Free (s, T)) =
168 enclose "Free(" ")" (quote s ^ ", " ^ scala_of_typ T)
169 | scala_of_term (Var ((s, i), T)) =
170 enclose "TVar(" ")" (
171 enclose "(" ")," (quote s ^ ", " ^ quote (string_of_int i)) ^
173 | scala_of_term (Bound i) = enclose "Bound(" ")" (string_of_int i)
174 | scala_of_term (Abs (s, T, t)) =
177 scala_of_typ T ^ ", " ^
179 | scala_of_term (t1 $ t2) =
180 enclose "App(" ")" (scala_of_term t1 ^ ", " ^ scala_of_term t2)
182 (* see structure's bare bones.
183 for Isabelle standard output compare 2017 "structure ML_PP" *)
186 fun ato n (Type (s, [])) = "\n*** " ^ indent n ^ "Type (" ^ s ^",[])"
187 | ato n (Type (s, Ts)) = "\n*** " ^ indent n ^ "Type (" ^ s ^ ",[" ^ atol (n + 1) Ts
188 | ato n (TFree (s, sort)) = "\n*** " ^ indent n ^ "TFree (" ^ s ^ ", " ^ strs2str' sort
189 | ato n (TVar ((s, i), sort)) =
190 "\n*** " ^ indent n ^ "TVar ((" ^ s ^ ", " ^ string_of_int i ^ strs2str' sort
191 and atol n [] = "\n*** " ^ indent n ^ "]"
192 | atol n (T :: Ts) = (ato n T ^ atol n Ts)
193 in tracing (ato 0 t ^ "\n") end;
196 fun ato (Const (a, _)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", _)"
197 | ato (Free (a, _)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", _)"
198 | ato (Var ((a, i), _)) n =
199 "\n*** " ^ indent n ^ "Var (" ^ a ^ ", " ^ string_of_int i ^ "), _)"
200 | ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
201 | ato (Abs (a, _, body)) n = "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1)
202 | ato (f $ t) n = (ato f n ^ ato t (n + 1))
204 fun atomw t = writeln ("\n*** -------------" ^ ato t 0 ^ "\n***");
205 fun atomt t = tracing ("\n*** -------------" ^ ato t 0 ^ "\n***");
209 fun term_detail2str t =
211 fun ato (Const (a, T)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ UnparseC.typ T ^ ")"
212 | ato (Free (a, T)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ UnparseC.typ T ^ ")"
213 | ato (Var ((a, i), T)) n =
214 "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), " ^ UnparseC.typ T ^ ")"
215 | ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
216 | ato (Abs(a, T, body)) n =
217 "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ UnparseC.typ T ^ ",.." ^ ato body (n + 1)
218 | ato (f $ t) n = ato f n ^ ato t (n + 1)
219 in "\n*** " ^ ato t 0 ^ "\n***" end;
222 fun term_detail2str_thy thy t =
224 fun ato (Const (a, T)) n =
225 "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ UnparseC.typ_by_thyID thy T ^ ")"
226 | ato (Free (a, T)) n =
227 "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ UnparseC.typ_by_thyID thy T ^ ")"
228 | ato (Var ((a, i), T)) n =
229 "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), " ^
230 UnparseC.typ_by_thyID thy T ^ ")"
232 "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
233 | ato (Abs(a, T, body)) n =
234 "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ UnparseC.typ_by_thyID thy T ^ ",.." ^
236 | ato (f $ t) n = ato f n ^ ato t (n + 1)
237 in "\n*** " ^ ato t 0 ^ "\n***" end;
238 fun atomwy t = (writeln o term_detail2str) t;
239 fun atomty t = (tracing o term_detail2str) t;
240 fun atomty_thy thy t = (tracing o (term_detail2str_thy thy)) t;
243 (* contains the term a VAR(("*",_),_) ? *)
244 fun contains_Var (Abs(_,_,body)) = contains_Var body
245 | contains_Var (f $ f') = contains_Var f orelse contains_Var f'
246 | contains_Var (Var _) = true
247 | contains_Var _ = false;
250 if n < 0 then "-" ^ ((string_of_int o abs) n)
251 else string_of_int n;
252 val int_of_str = Value.parse_int;
254 val int_opt_of_string = ThmC_Def.int_opt_of_string
255 fun is_num' str = case int_opt_of_string str of SOME _ => true | NONE => false;
257 fun is_num (Const (\<^const_name>\<open>numeral\<close>, _) $ _) = true
258 | is_num (Const (\<^const_name>\<open>uminus\<close>, _) $
259 (Const (\<^const_name>\<open>numeral\<close>, _) $ _)) = true
260 | is_num (Const (\<^const_name>\<open>one_class.one\<close>, _)) = true
261 | is_num (Const (\<^const_name>\<open>uminus\<close>, _) $ Const (\<^const_name>\<open>one_class.one\<close>, _)) = true
262 | is_num (Const (\<^const_name>\<open>zero_class.zero\<close>, _)) = true
263 | is_num (Const (\<^const_name>\<open>uminus\<close>, _) $ Const (\<^const_name>\<open>zero_class.zero\<close>, _)) = true
266 fun string_of_num n = (n |> HOLogic.dest_number |> snd |> string_of_int)
268 fun mk_negative T t = Const (\<^const_name>\<open>uminus\<close>, T --> T) $ t
269 fun mk_frac T (sg, (i1, i2)) =
271 if i2 = 1 then HOLogic.mk_number T i1
272 else Const (\<^const_name>\<open>divide_class.divide\<close>, T --> T --> T) $
273 HOLogic.mk_number T i1 $ HOLogic.mk_number T i2
274 else (*take negative*)
275 if i2 = 1 then mk_negative T (HOLogic.mk_number T i1)
276 else Const (\<^const_name>\<open>divide_class.divide\<close>, T --> T --> T) $
277 mk_negative T (HOLogic.mk_number T i1) $ HOLogic.mk_number T i2
279 val numerals_to_Free = (* Makarius 100308 *)
282 (case try HOLogic.dest_number t of
283 SOME (T, i) => SOME (Free (signed_string_of_int i, T))
285 fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
286 | to_str (t as (u1 $ u2)) =
287 (case dest_num t of SOME t' => t' | NONE => to_str u1 $ to_str u2)
288 | to_str t = perhaps dest_num t;
291 val term_of_num = HOLogic.mk_number;
292 fun num_of_term t = t |> HOLogic.dest_number |> snd;
293 (* accomodate string-representation for int to term-orders *)
294 fun to_string t = t |> num_of_term |> LibraryC.string_of_int'
296 fun is_const (Const _) = true | is_const _ = false;
297 fun is_variable (t as Free _) = not (is_num t)
298 | is_variable _ = false;
299 fun is_Free (Free _) = true | is_Free _ = false;
300 fun is_fun_id (Const _) = true
301 | is_fun_id (Free _) = true
302 | is_fun_id _ = false;
303 fun is_f_x (f $ x) = is_fun_id f andalso is_Free x
305 (* precondition: TermC.is_atom v andalso TermC.is_atom c *)
306 fun variable_constant_pair (v, c) =
307 if (is_variable v andalso (is_const c orelse is_num c)) orelse
308 (is_variable c andalso (is_const v orelse is_num v))
314 fun scan vs (Const _) = vs
315 | scan vs (t as Free _) = (*if is_num' s then vs else*) t :: vs
316 | scan vs (t as Var _) = t :: vs
317 | scan vs (Bound _) = vs
318 | scan vs (Abs (_, _, t)) = scan vs t
319 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
320 in ((distinct op =) o (scan [])) t end;
321 fun vars' ts = ts |> map vars |> flat |> distinct op =
323 (* bypass Isabelle's Pretty, which requires ctxt *)
326 fun scan vs (t as Const (s, _) $ arg) =
327 if is_num t then vs else scan (s :: vs) arg
328 | scan vs (Const (s as "Partial_Fractions.AA", _)) = s :: vs (*how get rid of spec.case?*)
329 | scan vs (Const _) = vs
330 | scan vs (Free (s, _)) = if is_num' s then vs else s :: vs
331 | scan vs (Var ((s, i), _)) = (s ^ "_" ^ string_of_int i) :: vs
332 | scan vs (Bound _) = vs
333 | scan vs (Abs (s, _, t)) = scan (s :: vs) t
334 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
335 in ((distinct op =) o (scan [])) t
337 fun is_bdv str = case Symbol.explode str of "b"::"d"::"v"::_ => true | _ => false;
338 (* instantiate #prop thm with bound variables (as Free) *)
339 fun inst_bdv [] t = t
340 | inst_bdv (instl: (term*term) list) t =
342 fun subst (v as Var((s, _), T)) =
343 (case Symbol.explode s of
344 "b"::"d"::"v"::_ => if_none (assoc(instl,Free(s,T))) (Free(s,T))
346 | subst (Abs(a, T, body)) = Abs(a, T, subst body)
347 | subst (f $ t') = subst f $ subst t'
348 | subst t = if_none (assoc (instl, t)) t
351 (* is a term a substitution for a bdv as found in programs and tactics *)
352 fun is_bdv_subst (Const (\<^const_name>\<open>Cons\<close>, _) $
353 (Const (\<^const_name>\<open>Pair\<close>, _) $ str $ _) $ _) = is_bdv (HOLogic.dest_string str)
354 | is_bdv_subst _ = false;
356 (* this shall be improved due to future requirements *)
357 fun guess_bdv_typ t = t |> vars |> hd |> type_of
359 fun free2str (Free (s, _)) = s
360 | free2str t = raise ERROR ("free2str not for " ^ UnparseC.term t);
361 fun str_of_free_opt (Free (s, _)) = SOME s
362 | str_of_free_opt _ = NONE
364 (* compare Logic.unvarify_global, which rejects Free *)
365 fun var2free (t as Const _) = t
366 | var2free (t as Free _) = t
367 | var2free (Var((s, _), T)) = Free (s,T)
368 | var2free (t as Bound _) = t
369 | var2free (Abs(s, T, t)) = Abs(s, T, var2free t)
370 | var2free (t1 $ t2) = (var2free t1) $ (var2free t2);
373 (* Logic.varify does NOT take care of 'Free ("1", _)'*)
374 fun free2var (t as Const _) = t
375 | free2var (t as Free (s, T)) = if is_num' s then t else Var ((s, 0), T)
376 | free2var (t as Var _) = t
377 | free2var (t as Bound _) = t
378 | free2var (Abs (s, T, t)) = Abs (s, T, free2var t)
379 | free2var (t1 $ t2) = (free2var t1) $ (free2var t2);
382 fun mk_listT T = Type (\<^type_name>\<open>list\<close>, [T]);
383 fun list_const T = Const (\<^const_name>\<open>Cons\<close>, [T, mk_listT T] ---> mk_listT T);
384 fun list2isalist T [] = Const (\<^const_name>\<open>Nil\<close>, mk_listT T)
385 | list2isalist T (t :: ts) = (list_const T) $ t $ (list2isalist T ts);
387 fun isapair2pair (Const (\<^const_name>\<open>Pair\<close>,_) $ a $ b) = (a, b)
389 raise ERROR ("isapair2pair called with " ^ UnparseC.term t);
390 fun isalist2list ls =
392 fun get es (Const(\<^const_name>\<open>Cons\<close>, _) $ t $ ls) = get (t :: es) ls
393 | get es (Const(\<^const_name>\<open>Nil\<close>, _)) = es
394 | get _ t = raise TERM ("isalist2list applied to NON-list: ", [t])
395 in (rev o (get [])) ls end;
397 fun is_list ((Const (\<^const_name>\<open>Cons\<close>, _)) $ _ $ _) = true
399 fun dest_listT (Type (\<^type_name>\<open>list\<close>, [T])) = T
400 | dest_listT T = raise TYPE ("dest_listT: list type expected", [T], []);
402 (if dest_listT (Term.type_of t) = HOLogic.boolT then true else false)
403 handle TYPE _ => false
406 fun dest_binop_typ (Type (\<^type_name>\<open>fun\<close>, [range, Type (\<^type_name>\<open>fun\<close>, [arg2, arg1])])) = (arg1, arg2, range)
407 | dest_binop_typ _ = raise ERROR "dest_binop_typ: not binary";
408 fun dest_equals (Const(\<^const_name>\<open>HOL.eq\<close>, _) $ t $ u) = (t, u) (* Pure/logic.ML: Const ("==", ..*)
409 | dest_equals t = raise TERM ("dest_equals'", [t]);
410 fun is_equality (Const(\<^const_name>\<open>HOL.eq\<close>,_) $ _ $ _) = true (* logic.ML: Const ("=="*)
411 | is_equality _ = false;
412 fun mk_equality (t, u) = (Const(\<^const_name>\<open>HOL.eq\<close>, [type_of t, type_of u] ---> HOLogic.boolT) $ t $ u);
413 fun is_expliceq (Const(\<^const_name>\<open>HOL.eq\<close>,_) $ (Free _) $ _) = true
414 | is_expliceq _ = false;
415 fun strip_trueprop (Const (\<^const_name>\<open>Trueprop\<close>, _) $ t) = t
416 | strip_trueprop t = t;
418 (* (A1==>...An==>B) goes to (A1==>...An==>) Pure/logic.ML: term -> term list*)
419 fun strip_imp_prems' (Const (\<^const_name>\<open>Pure.imp\<close>, _) $ A $ t) =
421 fun coll_prems As (Const(\<^const_name>\<open>Pure.imp\<close>, _) $ A $ t) =
422 coll_prems (As $ (Logic.implies $ A)) t
423 | coll_prems As _ = SOME As
424 in coll_prems (Logic.implies $ A) t end
425 | strip_imp_prems' _ = NONE; (* *)
427 (* (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch, 2002 Pure/thm.ML *)
428 fun ins_concl (Const (\<^const_name>\<open>Pure.imp\<close>, _) $ A $ t) B = Logic.implies $ A $ (ins_concl t B)
429 | ins_concl (Const (\<^const_name>\<open>Pure.imp\<close>, _) $ A ) B = Logic.implies $ A $ B
430 | ins_concl t B = raise TERM ("ins_concl", [t, B]);
432 fun vperm (Var _, Var _) = true (* 2002 Pure/thm.ML *)
433 | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
434 | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
435 | vperm (t, u) = (t = u);
437 (*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*)
438 fun mem_term (_, []) = false
439 | mem_term (t, t' :: ts) = t aconv t' orelse mem_term (t, ts);
440 fun subset_term ([], _) = true
441 | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term (xs, ys);
442 fun eq_set_term (xs, ys) =
443 xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
444 (*a total, irreflexive ordering on index names*)
445 fun xless ((a, i), (b, j): indexname) = i<j orelse (i = j andalso a < b);
446 (*a partial ordering (not reflexive) for atomic terms*)
447 fun atless (Const (a, _), Const (b, _)) = a < b
448 | atless (Free (a, _), Free (b, _)) = a < b
449 | atless (Var (v, _), Var (w, _)) = xless (v, w)
450 | atless (Bound i, Bound j) = i < j
452 (*insert atomic term into partially sorted list, suppressing duplicates (?)*)
453 fun insert_aterm (t,us) =
454 let fun inserta [] = [t]
455 | inserta (us as u::us') =
456 if atless(t,u) then t::us
457 else if t=u then us (*duplicate*)
458 else u :: inserta us'
461 (* Accumulates the Vars in the term, suppressing duplicates *)
462 fun add_term_vars (t, vars: term list) = case t of
463 Var _ => insert_aterm (t, vars)
464 | Abs (_, _, body) => add_term_vars (body, vars)
465 | f$t => add_term_vars (f, add_term_vars (t, vars))
467 fun term_vars t = add_term_vars (t, []);
469 (*2002 Pure/thm.ML *)
470 fun var_perm (t, u) = vperm (t, u) andalso eq_set_term (term_vars t, term_vars u);
471 (*2002 fun decomp_simp, Pure/thm.ML *)
472 fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs) andalso not (is_Var lhs);
475 fun pairT T1 T2 = Type (\<^type_name>\<open>prod\<close>, [T1, T2]);
476 fun PairT T1 T2 = ([T1, T2] ---> Type (\<^type_name>\<open>prod\<close>, [T1, T2]));
477 fun pairt t1 t2 = Const (\<^const_name>\<open>Pair\<close>, PairT (type_of t1) (type_of t2)) $ t1 $ t2;
479 fun mk_factroot op_(*=thy.sqrt*) T fact root =
480 Const (\<^const_name>\<open>times\<close>, [T, T] ---> T) $ (term_of_num T fact) $
481 (Const (op_, T --> T) $ term_of_num T root);
482 fun mk_var_op_num v op_ optype ntyp n = Const (op_, optype) $ v $ HOLogic.mk_number ntyp n;
483 fun mk_num_op_var v op_ optype ntyp n = Const (op_, optype) $ HOLogic.mk_number ntyp n $ v;
484 fun mk_num_op_num T1 T2 (op_, Top) n1 n2 =
485 Const (op_, Top) $ HOLogic.mk_number T1 n1 $ HOLogic.mk_number T2 n2;
486 fun mk_thmid thmid n1 n2 =
487 thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
490 val (T1, T2) = (type_of t1, type_of t2)
492 if T1 <> T2 then raise TYPE ("mk_add gets ", [T1, T2], [t1,t2])
493 else (Const (\<^const_name>\<open>plus\<close>, [T1, T2] ---> T1) $ t1 $ t2)
496 (** transform binary numeralsstrings **)
497 val numbers_to_string = ThmC_Def.num_to_Free
498 val uminus_to_string = ThmC_Def.uminus_to_string
500 fun mk_Free (s,T) = Free (s, T);
501 fun mk_free T s = Free (s, T);
503 (*Special case: one argument cp from Isabelle2002/src/Pure/term.ML*)
504 fun subst_bound (arg, t) =
506 fun subst (t as Bound i, lev) =
507 if i < lev then t (*var is locally bound*)
508 else if i = lev then incr_boundvars lev arg
509 else Bound (i - 1) (*loose: change it*)
510 | subst (Abs(a, T, body), lev) = Abs (a, T, subst (body, lev + 1))
511 | subst (f$t, lev) = subst(f, lev) $ subst(t, lev)
515 (* instantiate let; necessary for scan_up1 *)
516 fun inst_abs (Const sT) = Const sT
517 | inst_abs (Free sT) = Free sT
518 | inst_abs (Bound n) = Bound n
519 | inst_abs (Var iT) = Var iT
520 | inst_abs (Const (\<^const_name>\<open>Let\<close>,T1) $ e $ (Abs (v, T2, b))) =
521 let val b' = subst_bound (Free (v, T2), b); (*fun variant_abs: term.ML*)
522 in Const (\<^const_name>\<open>Let\<close>, T1) $ inst_abs e $ (Abs (v, T2, inst_abs b')) end
523 | inst_abs (t1 $ t2) = inst_abs t1 $ inst_abs t2
526 (* for parse and parse_patt: fix all types to real *)
527 fun T_a2real (Type (s, [])) =
528 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else Type (s, [])
529 | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts)
530 | T_a2real (TFree (s, srt)) =
531 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TFree (s, srt)
532 | T_a2real (TVar (("DUMMY", _), _)) = HOLogic.realT
533 | T_a2real (TVar ((s, i), srt)) =
534 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TVar ((s, i), srt)
535 fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T))
536 | typ_a2real (Free( s, T)) = (Free( s, T_a2real T))
537 | typ_a2real (Var( n, T)) = (Var( n, T_a2real T))
538 | typ_a2real (Bound i) = (Bound i)
539 | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t)
540 | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2);
542 (* TODO clarify parse with Test_Isac *)
543 fun parseold thy str = (* before 2002 *)
545 let val t = ((*typ_a2real o*) numbers_to_string) (Syntax.read_term_global thy str)
546 in Thm.global_cterm_of thy t end\<close>;
547 fun parseN thy str = (* introduced 2002 *)
549 let val t = (*(typ_a2real o numbers_to_string)*) (Syntax.read_term_global thy str)
550 in Thm.global_cterm_of thy t end\<close>;
552 fun parse_strict thy str = (typ_a2real o numbers_to_string) (Syntax.read_term_global thy str);
553 fun parse thy str = (* introduced 2010 *)
555 let val t = parse_strict thy str
556 in Thm.global_cterm_of thy t end\<close>;
558 (*WN110317 parseNEW will replace parse after introduction of ctxt completed*)
559 fun parseNEW ctxt str = \<^try>\<open>Syntax.read_term ctxt str |> numbers_to_string\<close>;
560 fun parseNEW' ctxt str =
561 case parseNEW ctxt str of
563 | NONE => raise TERM ("NO parseNEW' for " ^ str, [])
564 fun parseNEW'' thy str =
565 case parseNEW (ThyC.to_ctxt thy) str of
567 | NONE => raise TERM ("NO parseNEW'' for " ^ str, [])
569 (* parse term patterns; Var ("v",_), i.e. "?v", are required for instantiation
570 WN130613 probably compare to
571 http://www.mail-archive.com/isabelle-dev@mailbroy.informatik.tu-muenchen.de/msg04249.html*)
572 fun parse_patt thy str = (thy, str)
574 |-> Proof_Context.read_term_pattern
575 (*|> numbers_to_string TODO drop*)
576 |> typ_a2real; (*TODO drop*)
577 fun str2term str = parse_patt (ThyC.get_theory "Isac_Knowledge") str
579 fun is_atom (Const (\<^const_name>\<open>numeral\<close>, _) $ _) = true
580 | is_atom (Const (\<^const_name>\<open>one_class.one\<close>, _)) = true
581 | is_atom (Const (\<^const_name>\<open>zero_class.zero\<close>, _)) = true
582 | is_atom (Const _) = true
583 | is_atom (Free _) = true
584 | is_atom (Var _) = true
586 fun string_of_atom (t as Const (\<^const_name>\<open>numeral\<close>, _) $ _) = to_string t
587 | string_of_atom (t as Const (\<^const_name>\<open>one_class.one\<close>, _)) = to_string t
588 | string_of_atom (t as Const (\<^const_name>\<open>zero_class.zero\<close>, _)) = to_string t
589 | string_of_atom (Const (str, _)) = str
590 | string_of_atom (Free (str, _)) = str
591 | string_of_atom (Var ((str, int), _)) = str ^ "_" ^ string_of_int int
592 | string_of_atom _ = "DUMMY-string_of_atom";
594 (* from Pure/term.ML; reports if ALL Free's have found a substitution
595 (required for evaluating the preconditions of _incomplete_ models) *)
596 fun subst_atomic_all [] t = (false (*TODO may be 'true' for some terms ?*), t)
597 | subst_atomic_all instl t =
599 fun subst (Abs (a, T, body)) =
601 val (all, body') = subst body
602 in (all, Abs(a, T, body')) end
605 val (all1, f') = subst f
606 val (all2, tt') = subst tt
607 in (all1 andalso all2, f' $ tt') end
608 | subst (t as Free _) =
609 if is_num t then (true, t) (*numerals cannot be subst*)
610 else (case assoc (instl, t) of
611 SOME t' => (true, t')
612 | NONE => (false, t))
613 | subst t = (true, if_none (assoc(instl,t)) t)
616 fun op contains_one_of (thm, ids) =
617 Term.exists_Const (fn id => member op= ids id) (Thm.prop_of thm)
619 fun var_for vs (t as Const (str, _)) id =
621 else if id = strip_thy str then t :: vs else vs
622 | var_for vs (t as Free (str, _)) id = if id = str then t :: vs else vs
623 | var_for vs (t as Var (idn, _)) id = if id = Term.string_of_vname idn then t :: vs else vs
624 | var_for vs (Bound _) _ = vs
625 | var_for vs (Abs (_, _, t)) id = var_for vs t id
626 | var_for vs (t1 $ t2) id = (var_for vs t1 id) @ (var_for vs t2 id)
628 val poly_consts = (* TODO: adopt syntax-const from Isabelle*)
629 [\<^const_name>\<open>plus\<close>, \<^const_name>\<open>minus\<close>,
630 \<^const_name>\<open>divide\<close>, \<^const_name>\<open>times\<close>,
631 \<^const_name>\<open>powr\<close>];
632 (* treat Free, Const, Var as variables in polynomials *)
635 val var_ids = t |> ids2str |> subtract op = poly_consts |> map strip_thy |> sort string_ord
636 in (map (var_for [] t) var_ids) |> flat |> distinct op = end
638 (* this may decompose an object-language isa-list;
639 use only, if description is not available, eg. not input ?WN:14.5.03 ??!?*)
640 fun dest_list' t = if is_list t then isalist2list t else [t];
642 fun negat (Const (\<^const_name>\<open>Not\<close>, _) $ P, P') = P = P'
644 fun negates p1 p2 = negat (p1, p2) orelse negat (swap (p1, p2));
646 fun raise_type_conflicts ts =
648 val dups = duplicates (op =) (map (fst o dest_Free) ts)
649 val confl = filter (fn Free (str, _) => member op = dups str
654 else raise TYPE ("formalisation inconsistent w.r.t. type inference: ",
655 map (snd o dest_Free)confl, confl)
658 (* expects t as Const *)
659 fun contains_Const_typeless ts t = (t
661 |> member (fn (t1, t2) => fst (dest_Const t1) = fst (dest_Const t2)) ts
662 ) handle TERM("dest_Const", _) => raise TERM ("contains_Const_typeless", [t])
664 (* WN100910 weaker than fun sym_thm for Theory.axioms_of in isa02 *)
667 val (lhs, rhs) = (dest_equals o strip_trueprop o Logic.strip_imp_concl) trm
668 val trm' = case strip_imp_prems' trm of
669 NONE => mk_equality (rhs, lhs)
670 | SOME cs => ins_concl cs (mk_equality (rhs, lhs))