1 (* Title: extends Isabelle/src/Pure/term.ML
2 Author: Walther Neuper 1999, Mathias Lehnfeld
3 (c) due to copyright terms
8 (*val bool \<longrightarrow> HOLogic.boolT*)
9 val const_in: string -> term -> bool
10 val contains_Var: term -> bool
11 val dest_binop_typ: typ -> typ * typ * typ
12 val (*dest_equals'*) dest_equals: term -> term * term
13 val free2str: term -> string
14 val ids2str: term -> string list
15 val ins_concl: term -> term -> term
16 val inst_abs: term -> term
17 val inst_bdv: (term * term) list -> term -> term
19 val term_of_num: typ -> int -> term
20 val (*num_of_term \<longrightarrow>*) num_of_term: term -> int
21 val (*int_of_str \<longrightarrow>*) int_of_str_opt: string -> int option
22 val (*int_of_str' int2str \<longrightarrow>*)int_of_str: string -> int
23 val isastr_of_int: int -> string
25 val isalist2list: term -> term list
26 val list2isalist: typ -> term list -> term
27 val isapair2pair: term -> term * term
29 val is_atom: term -> bool
30 val is_bdv: string -> bool
31 val is_bdv_subst: term -> bool
32 val is_equality: term -> bool
33 val is_expliceq: term -> bool
34 val is_f_x: term -> bool
35 val is_list: term -> bool
36 val is_num: term -> bool
37 val (*is_no is_numeral \<longrightarrow>*)is_num': string -> bool
39 val mk_add: term -> term -> term
40 val mk_free: typ -> string -> term
41 val mk_equality: term * term -> term
42 val mk_factroot: string -> typ -> int -> int -> term
43 val mk_Free: string * typ -> term
44 val mk_thmid: string -> string -> string -> string
45 val mk_num_op_num(*<-- num_op_num*): typ -> typ -> string * typ -> int -> int -> term
46 val mk_num_op_var(*<-- num_op_var*): term -> string -> typ -> typ -> int -> term
47 val mk_var_op_num(*<-- var_op_num*): term -> string -> typ -> typ -> int -> term
49 (*val list_implies: term list * term -> term --> Logic.list_implies*)
50 val matches: theory -> term -> term -> bool
51 val parse: theory -> string -> cterm option
52 val parseN: theory -> string -> cterm option
53 val parseNEW: Proof.context -> string -> term option
54 val parseold: theory -> string -> cterm option
55 val parse_patt: theory -> string -> term
56 val perm: term -> term -> bool
58 val calc_equ: string -> int * int -> bool
59 val power: int -> int -> int
60 val sign_mult: int -> int -> int
61 val squfact: int -> int
62 val gcd: int -> int -> int
64 val str_of_free_opt: term -> string option
65 val str_of_int: int -> string
66 val str2term: string -> term
67 val strip_imp_prems': term -> term option
68 val subst_atomic_all: (term * term) list -> term -> bool * term
69 val term_detail2str: term -> string
71 val num_str: thm -> thm
72 val pairt: term -> term -> term
73 val pairT: typ -> typ -> typ
74 val strip_trueprop: term -> term
76 val uminus_to_string: term -> term
77 val var2free: term -> term
78 val vars: term -> term list
79 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
80 val atomtyp(*<-- atom_typ TODO*): typ -> unit
81 val scala_of_term: term -> string
82 val atomty: term -> unit
83 val atomw: term -> unit
84 val atomwy: term -> unit
85 val atomty_thy: thyID -> term -> unit
86 val free2var: term -> term
89 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
90 (*============================================= shift up during Test_Isac ======================
91 val --->: typ list * typ -> typ
92 val -->: typ * typ -> typ
93 val PairT: typ -> typ -> typ
94 val T_a2real: typ -> typ
95 val add_term_vars: term * term list -> term list
96 val atless: term * term -> bool
97 val atomt: term -> unit
98 val atomthm: thm -> unit
99 val contains_term: term -> term -> bool
100 val divisors: int -> int list
101 val doubles: ''a list -> ''a list
102 val dvd: int * int -> bool
103 val eq_set_term: term list * term list -> bool
104 val insert_aterm: term * term list -> term list
105 val is_Free: term -> bool
106 val is_fun_id: term -> bool
107 val list_const: typ -> term
108 val mem_term: term * term list -> bool
109 val mk_listT: typ -> typ
110 val mk_subs: ((string * int) * (typ * term)) list -> (term * term) list
111 val numbers_to_string: term -> term
112 val raw_pp_typ: typ -> Pretty.T
113 val scala_of_typ: typ -> string
114 val str2termN: string -> term
115 val str2term_: theory -> string -> term
116 val str_of_int: int -> string
117 val strs2terms: string list -> term list
118 val subset_term: term list * term list -> bool
119 val subst_bound: term * term -> term
120 val term_detail2str_thy: thyID -> term -> string
121 val term_of_num: typ -> int -> term
122 val term_vars: term -> term list
123 val typ_a2real: term -> term
124 val var_perm: term * term -> bool
125 val vperm: term * term -> bool
126 val xless: (string * int) * indexname -> bool
127 *============================================= shift up during Test_Isac ======================*)
128 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
132 structure TermC(**): TERMC(**) =
136 fun isastr_of_int i = if i >= 0 then string_of_int i else "-" ^ string_of_int (abs i)
138 fun matches thy tm pa =
139 (Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true)
140 handle Pattern.MATCH => false
142 (** transform typ / term to a String to be parsed by Scala after transport via libisabelle **)
144 fun scala_of_typ (Type (s, typs)) =
145 enclose "Type(" ")" (quote s ^ ", " ^
146 (typs |> map scala_of_typ |> commas |> enclose "List(" ")"))
147 | scala_of_typ (TFree (s, sort)) =
148 enclose "TFree(" ")" (quote s ^ ", " ^ (sort |> map quote |> commas |> enclose "List(" ")"))
149 | scala_of_typ (TVar ((s, i), sort)) =
150 enclose "TVar(" ")" (
151 enclose "(" ")," (quote s ^ "," ^ quote (string_of_int i)) ^
152 (sort |> map quote |> commas |> enclose "List(" ")"))
153 fun scala_of_term (Const (s, T)) =
154 enclose "Const(" ")" (quote s ^ ", " ^ scala_of_typ T)
155 | scala_of_term (Free (s, T)) =
156 enclose "Free(" ")" (quote s ^ ", " ^ scala_of_typ T)
157 | scala_of_term (Var ((s, i), T)) =
158 enclose "TVar(" ")" (
159 enclose "(" ")," (quote s ^ "," ^ quote (string_of_int i)) ^
161 | scala_of_term (Bound i) = enclose "Bound(" ")" (string_of_int i)
162 | scala_of_term (Abs (s, T, t)) =
165 scala_of_typ T ^ ", " ^
167 | scala_of_term (t1 $ t2) =
168 enclose "App(" ")" (scala_of_term t1 ^ ", " ^ scala_of_term t2)
170 (* see structure's bare bones.
171 for Isabelle standard output compare 2017 "structure ML_PP" *)
174 fun ato n (Type (s, [])) = "\n*** " ^ indent n ^ "Type (" ^ s ^",[])"
175 | ato n (Type (s, Ts)) = "\n*** " ^ indent n ^ "Type (" ^ s ^ ",[" ^ atol (n + 1) Ts
176 | ato n (TFree (s, sort)) = "\n*** " ^ indent n ^ "TFree (" ^ s ^ "," ^ strs2str' sort
177 | ato n (TVar ((s, i), sort)) =
178 "\n*** " ^ indent n ^ "TVar ((" ^ s ^ "," ^ string_of_int i ^ strs2str' sort
179 and atol n [] = "\n*** " ^ indent n ^ "]"
180 | atol n (T :: Ts) = (ato n T ^ atol n Ts)
181 in tracing (ato 0 t ^ "\n") end;
184 fun ato (Const (a, _)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", _)"
185 | ato (Free (a, _)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", _)"
186 | ato (Var ((a, i), _)) n =
187 "\n*** " ^ indent n ^ "Var (" ^ a ^ ", " ^ string_of_int i ^ "), _)"
188 | ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
189 | ato (Abs (a, _, body)) n = "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1)
190 | ato (f $ t) n = (ato f n ^ ato t (n + 1))
192 fun atomw t = writeln ("\n*** -------------" ^ ato t 0 ^ "\n***");
193 fun atomt t = tracing ("\n*** -------------" ^ ato t 0 ^ "\n***");
196 fun term_detail2str t =
198 fun ato (Const (a, T)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ string_of_typ T ^ ")"
199 | ato (Free (a, T)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ string_of_typ T ^ ")"
200 | ato (Var ((a, i), T)) n =
201 "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), " ^ string_of_typ T ^ ")"
202 | ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
203 | ato (Abs(a, T, body)) n =
204 "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ string_of_typ T ^ ",.." ^ ato body (n + 1)
205 | ato (f $ t) n = ato f n ^ ato t (n + 1)
206 in "\n*** " ^ ato t 0 ^ "\n***" end;
207 fun term_detail2str_thy thy t =
209 fun ato (Const (a, T)) n =
210 "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ string_of_typ_thy thy T ^ ")"
211 | ato (Free (a, T)) n =
212 "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ string_of_typ_thy thy T ^ ")"
213 | ato (Var ((a, i), T)) n =
214 "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), " ^
215 string_of_typ_thy thy T ^ ")"
217 "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
218 | ato (Abs(a, T, body)) n =
219 "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ string_of_typ_thy thy T ^ ",.." ^
221 | ato (f $ t) n = ato f n ^ ato t (n + 1)
222 in "\n*** " ^ ato t 0 ^ "\n***" end;
223 fun atomwy t = (writeln o term_detail2str) t;
224 fun atomty t = (tracing o term_detail2str) t;
225 fun atomty_thy thy t = (tracing o (term_detail2str_thy thy)) t;
227 (* contains the term a VAR(("*",_),_) ? *)
228 fun contains_Var (Abs(_,_,body)) = contains_Var body
229 | contains_Var (f $ f') = contains_Var f orelse contains_Var f'
230 | contains_Var (Var _) = true
231 | contains_Var _ = false;
234 if n < 0 then "-" ^ ((string_of_int o abs) n)
235 else string_of_int n;
236 val int_of_str = Thy_Output.integer;
237 fun int_of_str_opt str =
239 val ss = Symbol.explode str
240 val ss' = case ss of "(" :: s => drop_last s | _ => ss
241 val (sign, istr) = case ss' of "-" :: istr => (~1, istr) | _ => (1, ss')
243 case Library.read_int istr of (i, []) => SOME (sign * i) | _ => NONE
245 fun is_num' str = case int_of_str_opt str of SOME _ => true | NONE => false;
246 fun is_num (Free (s, _)) = if is_num' s then true else false | is_num _ = false;
247 fun term_of_num ntyp n = Free (str_of_int n, ntyp);
248 fun num_of_term (t as (Free (istr, _))) =
249 (case int_of_str_opt istr of SOME i => i | NONE => raise TERM ("num_of_term: NOT int ", [t]))
250 | num_of_term t = raise TERM ("num_of_term: NOT Free ", [t])
252 fun is_Free (Free _) = true | is_Free _ = false;
253 fun is_fun_id (Const _) = true
254 | is_fun_id (Free _) = true
255 | is_fun_id _ = false;
256 fun is_f_x (f $ x) = is_fun_id f andalso is_Free x
261 fun scan vs (Const _) = vs
262 | scan vs (t as Free (s, _)) = if is_num' s then vs else t :: vs
263 | scan vs (t as Var _) = t :: vs
264 | scan vs (Bound _) = vs
265 | scan vs (Abs (_, _, t)) = scan vs t
266 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
267 in (distinct o (scan [])) t end;
268 (* bypass Isabelle's Pretty, which requires ctxt *)
271 fun scan vs (Const (s, _)) = if is_num' s then vs else s :: vs
272 | scan vs (Free (s, _)) = if is_num' s then vs else s :: vs
273 | scan vs (Var ((s, i), _)) = (s ^ "_" ^ string_of_int i) :: vs
274 | scan vs (Bound _) = vs
275 | scan vs (Abs (s, _, t)) = scan (s :: vs) t
276 | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
277 in (distinct o (scan [])) t end;
278 fun is_bdv str = case Symbol.explode str of "b"::"d"::"v"::_ => true | _ => false;
280 (* is a term a substitution for a bdv as found in programs *)
281 fun is_bdv_subst (Const ("List.list.Cons", _) $
282 (Const ("Product_Type.Pair", _) $ Free (str, _) $ _) $ _) = is_bdv str
283 | is_bdv_subst _ = false;
285 fun free2str (Free (s, _)) = s
286 | free2str t = error ("free2str not for " ^ term2str t);
287 fun str_of_free_opt (Free (s, _)) = SOME s
288 | str_of_free_opt _ = NONE
290 (* compare Logic.unvarify_global, which rejects Free *)
291 fun var2free (t as Const _) = t
292 | var2free (t as Free _) = t
293 | var2free (Var((s, _), T)) = Free (s,T)
294 | var2free (t as Bound _) = t
295 | var2free (Abs(s, T, t)) = Abs(s, T, var2free t)
296 | var2free (t1 $ t2) = (var2free t1) $ (var2free t2);
298 (* Logic.varify does NOT take care of 'Free ("1", _)'*)
299 fun free2var (t as Const _) = t
300 | free2var (t as Free (s, T)) = if is_num' s then t else Var ((s, 0), T)
301 | free2var (t as Var _) = t
302 | free2var (t as Bound _) = t
303 | free2var (Abs (s, T, t)) = Abs (s, T, free2var t)
304 | free2var (t1 $ t2) = (free2var t1) $ (free2var t2);
306 fun mk_listT T = Type ("List.list", [T]);
307 fun list_const T = Const ("List.list.Cons", [T, mk_listT T] ---> mk_listT T);
308 fun list2isalist T [] = Const ("List.list.Nil", mk_listT T)
309 | list2isalist T (t :: ts) = (list_const T) $ t $ (list2isalist T ts);
311 fun isapair2pair (Const ("Product_Type.Pair",_) $ a $ b) = (a, b)
313 error ("isapair2pair called with "^term2str t);
314 fun isalist2list ls =
316 fun get es (Const("List.list.Cons", _) $ t $ ls) = get (t :: es) ls
317 | get es (Const("List.list.Nil", _)) = es
318 | get _ t = error ("isalist2list applied to NON-list '"^term2str t^"'")
319 in (rev o (get [])) ls end;
321 fun is_list ((Const ("List.list.Cons", _)) $ _ $ _) = true
323 fun dest_binop_typ (Type ("fun", [range, Type ("fun", [arg2, arg1])])) = (arg1, arg2, range)
324 | dest_binop_typ _ = raise ERROR "dest_binop_typ: not binary";
325 fun dest_equals (Const("HOL.eq", _) $ t $ u) = (t, u) (* Pure/logic.ML: Const ("==", ..*)
326 | dest_equals t = raise TERM ("dest_equals'", [t]);
327 fun is_equality (Const("HOL.eq",_) $ _ $ _) = true (* logic.ML: Const("=="*)
328 | is_equality _ = false;
329 fun mk_equality (t, u) = (Const("HOL.eq", [type_of t, type_of u] ---> HOLogic.boolT) $ t $ u);
330 fun is_expliceq (Const("HOL.eq",_) $ (Free _) $ _) = true
331 | is_expliceq _ = false;
332 fun strip_trueprop (Const ("HOL.Trueprop", _) $ t) = t
333 | strip_trueprop t = t;
335 (* (A1==>...An==>B) goes to (A1==>...An==>) Pure/logic.ML: term -> term list*)
336 fun strip_imp_prems' (Const ("==>", _) $ A $ t) =
338 fun coll_prems As (Const("==>", _) $ A $ t) =
339 coll_prems (As $ (Logic.implies $ A)) t
340 | coll_prems As _ = SOME As
341 in coll_prems (Logic.implies $ A) t end
342 | strip_imp_prems' _ = NONE; (* *)
344 (* (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch, 2002 Pure/thm.ML *)
345 fun ins_concl (Const ("==>", _) $ A $ t) B = Logic.implies $ A $ (ins_concl t B)
346 | ins_concl (Const ("==>", _) $ A ) B = Logic.implies $ A $ B
347 | ins_concl t B = raise TERM ("ins_concl", [t, B]);
349 fun vperm (Var _, Var _) = true (* 2002 Pure/thm.ML *)
350 | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
351 | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
352 | vperm (t, u) = (t = u);
354 (*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*)
355 fun mem_term (_, []) = false
356 | mem_term (t, t' :: ts) = t aconv t' orelse mem_term (t, ts);
357 fun subset_term ([], _) = true
358 | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term (xs, ys);
359 fun eq_set_term (xs, ys) =
360 xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
361 (*a total, irreflexive ordering on index names*)
362 fun xless ((a, i), (b, j): indexname) = i<j orelse (i = j andalso a < b);
363 (*a partial ordering (not reflexive) for atomic terms*)
364 fun atless (Const (a, _), Const (b, _)) = a < b
365 | atless (Free (a, _), Free (b, _)) = a < b
366 | atless (Var (v, _), Var (w, _)) = xless (v, w)
367 | atless (Bound i, Bound j) = i < j
369 (*insert atomic term into partially sorted list, suppressing duplicates (?)*)
370 fun insert_aterm (t,us) =
371 let fun inserta [] = [t]
372 | inserta (us as u::us') =
373 if atless(t,u) then t::us
374 else if t=u then us (*duplicate*)
375 else u :: inserta us'
378 (* Accumulates the Vars in the term, suppressing duplicates *)
379 fun add_term_vars (t, vars: term list) = case t of
380 Var _ => insert_aterm (t, vars)
381 | Abs (_, _, body) => add_term_vars (body, vars)
382 | f$t => add_term_vars (f, add_term_vars (t, vars))
384 fun term_vars t = add_term_vars (t, []);
386 (*2002 Pure/thm.ML *)
387 fun var_perm (t, u) = vperm (t, u) andalso eq_set_term (term_vars t, term_vars u);
388 (*2002 fun decomp_simp, Pure/thm.ML *)
389 fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs) andalso not (is_Var lhs);
391 (* TODO: shift to calculate.sml? *)
392 fun calc_equ "less" (n1, n2) = n1 < n2
393 | calc_equ "less_eq" (n1, n2) = n1 <= n2
394 | calc_equ op_ _ = error ("calc_equ: operator = " ^ op_ ^ " not defined");
395 fun sqrt (n:int) = if n < 0 then 0 else (trunc o Math.sqrt o Real.fromInt (*FIXME*)) n;
398 if n > 0 then b* (power b (n - 1))
399 else error ("power " ^ str_of_int b ^ " " ^ str_of_int n);
401 | gcd a b = if a < b then gcd (b mod a) a else gcd (a mod b) b;
404 else if n = 0 then 0 else 1;
405 fun sign_mult n1 n2 = (sign n1) * (sign n2);
408 fun d dvd n = n mod d = 0;
410 let fun pdiv ds d n =
411 if d = n then d :: ds
412 else if d dvd n then pdiv (d :: ds) d (n div d)
413 else pdiv ds (d + 1) n
416 fun doubles ds = (* ds is ordered *)
417 let fun dbls ds [] = ds
419 | dbls ds (i :: i' :: is) = if i = i' then dbls (i :: ds) is
420 else dbls ds (i'::is)
424 | squfact n = foldl op* (1, (doubles o divisors) n);
427 fun pairT T1 T2 = Type ("*", [T1, T2]);
428 fun PairT T1 T2 = ([T1, T2] ---> Type ("*", [T1, T2]));
429 fun pairt t1 t2 = Const ("Product_Type.Pair", PairT (type_of t1) (type_of t2)) $ t1 $ t2;
431 fun mk_factroot op_(*=thy.sqrt*) T fact root =
432 Const ("Groups.times_class.times", [T, T] ---> T) $ (term_of_num T fact) $
433 (Const (op_, T --> T) $ term_of_num T root);
434 fun mk_var_op_num v op_ optype ntyp n = Const (op_, optype) $ v $ Free (str_of_int n, ntyp);
435 fun mk_num_op_var v op_ optype ntyp n = Const (op_, optype) $ Free (str_of_int n, ntyp) $ v;
436 fun mk_num_op_num T1 T2 (op_, Top) n1 n2 =
437 Const (op_, Top) $ Free (str_of_int n1, T1) $ Free (str_of_int n2, T2);
438 fun mk_thmid thmid n1 n2 =
439 thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
442 val (T1, T2) = (type_of t1, type_of t2)
444 if T1 <> T2 then raise TYPE ("mk_add gets ", [T1, T2], [t1,t2])
445 else (Const ("Groups.plus_class.plus", [T1, T2] ---> T1) $ t1 $ t2)
448 fun const_in _ (Const _) = false
449 | const_in str (Free (s, _)) = if strip_thy s = str then true else false
450 | const_in _ (Bound _) = false
451 | const_in _ (Var _) = false
452 | const_in str (Abs (_, _, body)) = const_in str body
453 | const_in str (f $ u) = const_in str f orelse const_in str u;
459 (** transform binary numeralsstrings **)
460 (*Makarius 100308, hacked by WN*)
461 val numbers_to_string =
464 (case try HOLogic.dest_number t of
466 (*if T = @{typ int} orelse T = @{typ real} then WN*)
467 SOME (Free (signed_string_of_int i, T))
470 fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
471 | to_str (t as (u1 $ u2)) =
474 | NONE => to_str u1 $ to_str u2)
475 | to_str t = perhaps dest_num t;
477 val uminus_to_string =
481 (Const ("Groups.uminus_class.uminus", _) $ Free (s, T)) =>
482 (case int_of_str_opt s of
483 SOME i => SOME (Free (signed_string_of_int (~1 * i), T))
486 fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
487 | to_str (t as (u1 $ u2)) =
488 (case dest_num t of SOME t' => t' | NONE => to_str u1 $ to_str u2)
489 | to_str t = perhaps dest_num t;
494 {cert = cert, tags = tags, maxidx = maxidx, shyps = shyps,
495 hyps = hyps, tpairs = tpairs, prop = prop}) = Thm.rep_thm_G thm
496 val prop' = numbers_to_string prop;
497 in Thm.assbl_thm deriv cert tags maxidx shyps hyps tpairs prop' end;
499 fun mk_Free (s,T) = Free (s, T);
500 fun mk_free T s = Free (s, T);
502 (*Special case: one argument cp from Isabelle2002/src/Pure/term.ML*)
503 fun subst_bound (arg, t) =
505 fun subst (t as Bound i, lev) =
506 if i < lev then t (*var is locally bound*)
507 else if i = lev then incr_boundvars lev arg
508 else Bound (i - 1) (*loose: change it*)
509 | subst (Abs(a, T, body), lev) = Abs (a, T, subst (body, lev + 1))
510 | subst (f$t, lev) = subst(f, lev) $ subst(t, lev)
514 (* instantiate let; necessary for ass_up *)
515 fun inst_abs (Const sT) = Const sT (*TODO.WN100907 drop thy*)
516 | inst_abs (Free sT) = Free sT
517 | inst_abs (Bound n) = Bound n
518 | inst_abs (Var iT) = Var iT
519 | inst_abs (Const ("HOL.Let",T1) $ e $ (Abs (v, T2, b))) =
520 let val b' = subst_bound (Free (v, T2), b);
521 (*fun variant_abs: term.ML*)
522 in Const ("HOL.Let", T1) $ inst_abs e $ (Abs (v, T2, inst_abs b')) end
523 | inst_abs (t1 $ t2) = inst_abs t1 $ inst_abs t2
526 "Script Make_fun_by_explicit (f_::real) (v_::real) (eqs_::bool list) = \
527 \ (let h_ = (hd o (filterVar f_)) eqs_; \
528 \ e_1 = hd (dropWhile (ident h_) eqs_); \
529 \ vs_ = dropWhile (ident f_) (Vars h_); \
530 \ v_1 = hd (dropWhile (ident v_) vs_); \
531 \ (s_1::bool list)=(SubProblem(DiffApp_,[univar,equation],[no_met])\
532 \ [BOOL e_1, REAL v_1])\
533 \ in Substitute [(v_1 = (rhs o hd) s_1)] h_)";
534 > val ttt = (Thm.term_of o the o (parse thy)) scr;
535 > tracing(term2str ttt);
538 *** Const ( DiffApp.Make'_fun'_by'_explicit)
543 *** . . Const ( Fun.op o)
544 *** . . . Const ( List.hd)
545 *** . . . Const ( DiffApp.filterVar)
546 *** . . . . Free ( f_, )
547 *** . . . Free ( eqs_, )
549 *** . . . Const ( Let)
550 *** . . . . Const ( List.hd)
551 *** . . . . . Const ( List.dropWhile)
552 *** . . . . . . Const ( Atools.ident)
553 *** . . . . . . . Bound 0 <---- Free ( h_, )
554 *** . . . . . . Free ( eqs_, )
555 *** . . . . Abs( e_1,..
556 *** . . . . . Const ( Let)
557 *** . . . . . . Const ( List.dropWhile)
558 *** . . . . . . . Const ( Atools.ident)
559 *** . . . . . . . . Free ( f_, )
560 *** . . . . . . . Const ( Tools.Vars)
561 *** . . . . . . . . Bound 1 <---- Free ( h_, )
562 *** . . . . . . Abs( vs_,..
563 *** . . . . . . . Const ( Let)
564 *** . . . . . . . . Const ( List.hd)
565 *** . . . . . . . . . Const ( List.dropWhile)
566 *** . . . . . . . . . . Const ( Atools.ident)
567 *** . . . . . . . . . . . Free ( v_, )
568 *** . . . . . . . . . . Bound 0 <---- Free ( vs_, )
569 *** . . . . . . . . Abs( v_1,..
570 *** . . . . . . . . . Const ( Let)
571 *** . . . . . . . . . . Const ( Script.SubProblem)
572 *** . . . . . . . . . . . Const ( Pair)
573 *** . . . . . . . . . . . . Free ( DiffApp_, )
574 *** . . . . . . . . . . . . Const ( Pair)
575 *** . . . . . . . . . . . . . Const ( List.list.Cons)
576 *** . . . . . . . . . . . . . . Free ( univar, )
577 *** . . . . . . . . . . . . . . Const ( List.list.Cons)
578 *** . . . . . . . . . . . . . . . Free ( equation, )
579 *** . . . . . . . . . . . . . . . Const ( List.list.Nil)
580 *** . . . . . . . . . . . . . Const ( List.list.Cons)
581 *** . . . . . . . . . . . . . . Free ( no_met, )
582 *** . . . . . . . . . . . . . . Const ( List.list.Nil)
583 *** . . . . . . . . . . . Const ( List.list.Cons)
584 *** . . . . . . . . . . . . Const ( Script.BOOL)
585 *** . . . . . . . . . . . . . Bound 2 <----- Free ( e_1, )
586 *** . . . . . . . . . . . . Const ( List.list.Cons)
587 *** . . . . . . . . . . . . . Const ( Script.real_)
588 *** . . . . . . . . . . . . . . Bound 0 <----- Free ( v_1, )
589 *** . . . . . . . . . . . . . Const ( List.list.Nil)
590 *** . . . . . . . . . . Abs( s_1,..
591 *** . . . . . . . . . . . Const ( Script.Substitute)
592 *** . . . . . . . . . . . . Const ( List.list.Cons)
593 *** . . . . . . . . . . . . . Const ( Pair)
594 *** . . . . . . . . . . . . . . Bound 1 <----- Free ( v_1, )
595 *** . . . . . . . . . . . . . . Const ( Fun.op o)
596 *** . . . . . . . . . . . . . . . Const ( Tools.rhs)
597 *** . . . . . . . . . . . . . . . Const ( List.hd)
598 *** . . . . . . . . . . . . . . . Bound 0 <----- Free ( s_1, )
599 *** . . . . . . . . . . . . . Const ( List.list.Nil)
600 *** . . . . . . . . . . . . Bound 4 <----- Free ( h_, )
602 > val ttt' = inst_abs thy ttt;
603 > tracing(term2str ttt');
604 Script Make_fun_by_explicit f_ v_ eqs_ =
608 *** Const ( DiffApp.Make'_fun'_by'_explicit)
613 *** . . Const ( Fun.op o)
614 *** . . . Const ( List.hd)
615 *** . . . Const ( DiffApp.filterVar)
616 *** . . . . Free ( f_, )
617 *** . . . Free ( eqs_, )
619 *** . . . Const ( Let)
620 *** . . . . Const ( List.hd)
621 *** . . . . . Const ( List.dropWhile)
622 *** . . . . . . Const ( Atools.ident)
623 *** . . . . . . . Free ( h_, ) <---- Bound 0
624 *** . . . . . . Free ( eqs_, )
625 *** . . . . Abs( e_1,..
626 *** . . . . . Const ( Let)
627 *** . . . . . . Const ( List.dropWhile)
628 *** . . . . . . . Const ( Atools.ident)
629 *** . . . . . . . . Free ( f_, )
630 *** . . . . . . . Const ( Tools.Vars)
631 *** . . . . . . . . Free ( h_, ) <---- Bound 1
632 *** . . . . . . Abs( vs_,..
633 *** . . . . . . . Const ( Let)
634 *** . . . . . . . . Const ( List.hd)
635 *** . . . . . . . . . Const ( List.dropWhile)
636 *** . . . . . . . . . . Const ( Atools.ident)
637 *** . . . . . . . . . . . Free ( v_, )
638 *** . . . . . . . . . . Free ( vs_, ) <---- Bound 0
639 *** . . . . . . . . Abs( v_1,..
640 *** . . . . . . . . . Const ( Let)
641 *** . . . . . . . . . . Const ( Script.SubProblem)
642 *** . . . . . . . . . . . Const ( Pair)
643 *** . . . . . . . . . . . . Free ( DiffApp_, )
644 *** . . . . . . . . . . . . Const ( Pair)
645 *** . . . . . . . . . . . . . Const ( List.list.Cons)
646 *** . . . . . . . . . . . . . . Free ( univar, )
647 *** . . . . . . . . . . . . . . Const ( List.list.Cons)
648 *** . . . . . . . . . . . . . . . Free ( equation, )
649 *** . . . . . . . . . . . . . . . Const ( List.list.Nil)
650 *** . . . . . . . . . . . . . Const ( List.list.Cons)
651 *** . . . . . . . . . . . . . . Free ( no_met, )
652 *** . . . . . . . . . . . . . . Const ( List.list.Nil)
653 *** . . . . . . . . . . . Const ( List.list.Cons)
654 *** . . . . . . . . . . . . Const ( Script.BOOL)
655 *** . . . . . . . . . . . . . Free ( e_1, ) <----- Bound 2
656 *** . . . . . . . . . . . . Const ( List.list.Cons)
657 *** . . . . . . . . . . . . . Const ( Script.real_)
658 *** . . . . . . . . . . . . . . Free ( v_1, ) <----- Bound 0
659 *** . . . . . . . . . . . . . Const ( List.list.Nil)
660 *** . . . . . . . . . . Abs( s_1,..
661 *** . . . . . . . . . . . Const ( Script.Substitute)
662 *** . . . . . . . . . . . . Const ( List.list.Cons)
663 *** . . . . . . . . . . . . . Const ( Pair)
664 *** . . . . . . . . . . . . . . Free ( v_1, ) <----- Bound 1
665 *** . . . . . . . . . . . . . . Const ( Fun.op o)
666 *** . . . . . . . . . . . . . . . Const ( Tools.rhs)
667 *** . . . . . . . . . . . . . . . Const ( List.hd)
668 *** . . . . . . . . . . . . . . . Free ( s_1, ) <----- Bound 0
669 *** . . . . . . . . . . . . . Const ( List.list.Nil)
670 *** . . . . . . . . . . . . Free ( h_, ) <----- Bound 4
672 Note numbering of de Bruijn indexes !
674 Script Make_fun_by_explicit f_ v_ eqs_ =
675 let h_ = (hd o filterVar f_) eqs_;
676 e_1 = hd (dropWhile (ident h_ BOUND_0) eqs_);
677 vs_ = dropWhile (ident f_) (Vars h_ BOUND_1);
678 v_1 = hd (dropWhile (ident v_) vs_ BOUND_0);
680 SubProblem (DiffApp_, [univar, equation], [no_met])
681 [BOOL e_1 BOUND_2, REAL v_1 BOUND_0]
682 in Substitute [(v_1 BOUND_1 = (rhs o hd) s_1 BOUND_0)] h_ BOUND_4
685 (* for parse and parse_patt: fix all types to real *)
686 fun T_a2real (Type (s, [])) =
687 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT
689 | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts)
690 | T_a2real (TFree (s, srt)) =
691 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT
693 | T_a2real (TVar ((s, i), srt)) =
694 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT
695 else TVar ((s, i), srt)
696 | T_a2real (TVar (("DUMMY",_), srt)) = HOLogic.realT;
698 (*FIXME .. fixes the type (+see Typefix.thy*)
699 fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T))
700 | typ_a2real (Free( s, T)) = (Free( s, T_a2real T))
701 | typ_a2real (Var( n, T)) = (Var( n, T_a2real T))
702 | typ_a2real (Bound i) = (Bound i)
703 | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t)
704 | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2);
708 fun parseold thy str =
709 (let val t = ((*typ_a2real o*) numbers_to_string)
710 (Syntax.read_term_global thy str)
711 in SOME (Thm.global_cterm_of thy t) end)
713 (*2002 fun parseN thy str =
715 val sgn = sign_of thy;
716 val t = ((*typ_a2real o app_num_tr'1 o*) term_of)
717 (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
718 in SOME (Thm.global_cterm_of sgn t) end)
721 (let val t = (*(typ_a2real o numbers_to_string)*)
722 (Syntax.read_term_global thy str)
723 in SOME (Thm.global_cterm_of thy t) end)
725 (*2002 fun parse thy str =
727 val sgn = sign_of thy;
728 val t = (typ_a2real o app_num_tr'1 o Thm.term_of)
729 (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
730 in SOME (Thm.global_cterm_of sgn t) end) (*FIXXXXME 10.8.02: return term !!!*)
732 (*2010 fun parse thy str =
733 (let val t = (typ_a2real o app_num_tr'1) (Syntax.read_term_global thy str)
734 in SOME (Thm.global_cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*)
737 (let val t = (typ_a2real o numbers_to_string)
738 (Syntax.read_term_global thy str)
739 in SOME (Thm.global_cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*)
742 > val (SOME ct) = parse thy "(-#5)^^^#3";
743 > atomty (Thm.term_of ct);
745 *** Const ( Nat.op ^, ['a, nat] => 'a)
746 *** Const ( uminus, 'a => 'a)
749 > val (SOME ct) = parse thy "R=R";
750 > atomty (Thm.term_of ct);
752 *** Const ( op =, [real, real] => bool)
756 THIS IS THE OUTPUT FOR VERSION (3) above at typ_a2real !!!!!
758 *** Const ( op =, [RealDef.real, RealDef.real] => bool)
759 *** Free ( R, RealDef.real)
760 *** Free ( R, RealDef.real) *)
762 (*WN110317 parseNEW will replace parse after introduction of ctxt completed*)
763 fun parseNEW ctxt str = SOME (Syntax.read_term ctxt str |> numbers_to_string)
766 (* parse term patterns; Var ("v",_), i.e. "?v", are required for instantiation
767 WN130613 probably compare to
768 http://www.mail-archive.com/isabelle-dev@mailbroy.informatik.tu-muenchen.de/msg04249.html*)
769 fun parse_patt thy str = (thy, str) |>> thy2ctxt
770 |-> Proof_Context.read_term_pattern
771 |> numbers_to_string (*TODO drop*)
772 |> typ_a2real; (*TODO drop*)
774 (*version for testing local to theories*)
775 fun str2term_ thy str = (Thm.term_of o the o (parse thy)) str;
777 fun str2term str = (Thm.term_of o the o (parse (Thy_Info_get_theory "Isac"))) str;*)
778 fun str2term str = parse_patt (Thy_Info_get_theory "Isac") str
779 fun strs2terms ss = map str2term ss;
780 fun str2termN str = (Thm.term_of o the o (parseN (Thy_Info_get_theory "Isac"))) str;
782 (*+ makes a substitution from the output of Pattern.match +*)
783 (*fun mk_subs ((id, _):indexname, t:term) = (Free (id,type_of t), t);*)
784 fun mk_subs (subs: ((string * int) * (Term.typ * Term.term)) list) =
785 let fun mk_sub ((id, _), (ty, tm)) = (Free (id, ty), tm) in
788 val atomthm = atomt o #prop o Thm.rep_thm;
790 (*.instantiate #prop thm with bound variables (as Free).*)
791 fun inst_bdv [] t = t : term
792 | inst_bdv (instl: (term*term) list) t =
793 let fun subst (v as Var((s,_),T)) =
794 (case Symbol.explode s of
796 if_none (assoc(instl,Free(s,T))) (Free(s,T))
798 | subst (Abs(a,T,body)) = Abs(a, T, subst body)
799 | subst (f$t') = subst f $ subst t'
800 | subst t = if_none (assoc(instl,t)) t
804 (*WN050829 caution: is_atom (str2term"q_0/2 * L * x") = true !!!
805 use length (vars term) = 1 instead*)
806 fun is_atom (Const ("Float.Float",_) $ _) = true
807 | is_atom (Const ("ComplexI.I'_'_",_)) = true
808 | is_atom (Const ("Groups.times_class.times",_) $ t $ Const ("ComplexI.I'_'_",_)) = is_atom t
809 | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ Const ("ComplexI.I'_'_",_)) = is_atom t1
810 | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $
811 (Const ("Groups.times_class.times",_) $ t2 $ Const ("ComplexI.I'_'_",_))) =
812 is_atom t1 andalso is_atom t2
813 | is_atom (Const _) = true
814 | is_atom (Free _) = true
815 | is_atom (Var _) = true
817 (* val t = str2term "q_0/2 * L * x";
821 (*val t = str2term "Float ((1,2),(0,0))";
824 > val t = str2term "Float ((1,2),(0,0)) * I__";
827 > val t = str2term "Float ((1,2),(0,0)) + Float ((3,4),(0,0)) * I__";
830 > val t = str2term "1 + 2*I__";
831 > val Const ("Groups.plus_class.plus",_) $ t1 $ (Const ("Groups.times_class.times",_) $ t2 $ Const ("ComplexI.I'_'_",_)) = t;
834 (*.adaption from Isabelle/src/Pure/term.ML; reports if ALL Free's
835 have found a substitution (required for evaluating the preconditions
836 of _incomplete_ models).*)
837 fun subst_atomic_all [] t = (false, (*TODO may be 'true' for some terms ?*)
839 | subst_atomic_all (instl: (term*term) list) t =
840 let fun subst (Abs(a,T,body)) =
841 let val (all, body') = subst body
842 in (all, Abs(a, T, body')) end
844 let val (all1, f') = subst f
845 val (all2, tt') = subst tt
846 in (all1 andalso all2, f' $ tt') end
847 | subst (t as Free _) =
848 if is_num t then (true, t) (*numerals cannot be subst*)
849 else (case assoc(instl,t) of
850 SOME t' => (true, t')
851 | NONE => (false, t))
852 | subst t = (true, if_none (assoc(instl,t)) t)