src/Tools/isac/ProgLang/termC.sml
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 21 Nov 2013 11:17:42 +0100
changeset 55275 f08422eeef24
parent 52105 2786cc9704c8
child 59184 831fa972f73b
permissions -rw-r--r--
Isabelle2013 --> 2013-1: remove left-over legacy "uses" "axiom"

and pushed updated hooks from Isabelle to isac
     1 (* Title: extends Isabelle/src/Pure/term.ML
     2    Author: Walther Neuper 1999, Mathias Lehnfeld
     3    (c) due to copyright terms
     4 *)
     5 
     6 fun isastr_of_int i = if i >= 0 then string_of_int i else "-" ^ string_of_int (abs i)
     7 
     8 (*
     9 > (cterm_of thy) a_term; 
    10 val it = "empty" : cterm        *)
    11 
    12 (*2003 fun match thy t pat =
    13     (snd (Pattern.match (Sign.tsig_of (sign_of thy)) (pat, t)))
    14     handle _ => [];
    15 fn : theory ->
    16      Term.term -> Term.term -> (Term.indexname * Term.term) list*)
    17 (*see src/Tools/eqsubst.ML fun clean_match*)
    18 (*2003 fun matches thy tm pa = if match thy tm pa = [] then false else true;*)
    19 fun matches thy tm pa = 
    20     (Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true)
    21     handle _ => false
    22 
    23 fun atomtyp t = (*WN10 see raw_pp_typ*)
    24   let
    25     fun ato n (Type (s,[])) = 
    26       ("\n*** "^indent n^"Type ("^s^",[])")
    27       | ato n (Type (s,Ts)) =
    28       ("\n*** "^indent n^"Type ("^s^",["^ atol (n+1) Ts)
    29 
    30       | ato n (TFree (s,sort)) =
    31       ("\n*** "^indent n^"TFree ("^s^",["^ strs2str' sort)
    32 
    33       | ato n (TVar ((s,i),sort)) =
    34       ("\n*** "^indent n^"TVar (("^s^","^ 
    35        string_of_int i ^ strs2str' sort)
    36     and atol n [] = 
    37       ("\n*** "^indent n^"]")
    38       | atol n (T::Ts) = (ato n T ^ atol n Ts)
    39 in tracing (ato 0 t ^ "\n") end;
    40 (*
    41 > val T = (type_of o term_of o the o (parse thy)) "a::[real,int] => nat";
    42 > atomtyp T;
    43 *** Type (fun,[
    44 ***   Type (RealDef.real,[])
    45 ***   Type (fun,[
    46 ***     Type (IntDef.int,[])
    47 ***     Type (nat,[])
    48 ***     ]
    49 ***   ]
    50 *)
    51 
    52 (*Prog.Tutorial.p.34, Makarius 1005 does the above like this..*)
    53 local
    54    fun pp_pair (x, y) = Pretty.list "(" ")" [x, y]
    55    fun pp_list xs = Pretty.list "[" "]" xs
    56    fun pp_str s   = Pretty.str s
    57    fun pp_qstr s = Pretty.quote (pp_str s)
    58    fun pp_int i   = pp_str (string_of_int i)
    59    fun pp_sort S = pp_list (map pp_qstr S)
    60    fun pp_constr a args = Pretty.block [pp_str a, Pretty.brk 1, args]
    61 in
    62 fun raw_pp_typ (TVar ((a, i), S)) =
    63        pp_constr "TVar" (pp_pair (pp_pair (pp_qstr a, pp_int i), pp_sort S))
    64    | raw_pp_typ (TFree (a, S)) =
    65        pp_constr "TFree" (pp_pair (pp_qstr a, pp_sort S))
    66    | raw_pp_typ (Type (a, tys)) =
    67        pp_constr "Type" (pp_pair (pp_qstr a, pp_list (map raw_pp_typ tys)))
    68 end
    69 (* install
    70 PolyML.addPrettyPrinter
    71   (fn _ => fn _ => ml_pretty o Pretty.to_ML o raw_pp_typ);
    72 de-install
    73 PolyML.addPrettyPrinter
    74   (fn _ => fn _ => ml_pretty o Pretty.to_ML o Proof_Display.pp_typ Pure.thy);
    75 *)
    76 
    77 local 
    78   fun ato (Const (a, _)) n = 
    79 	           "\n*** " ^ indent n ^ "Const (" ^ a ^ ", _)"
    80 	  | ato (Free (a, _)) n =  
    81 	           "\n*** " ^ indent n ^ "Free (" ^ a ^ ", _)"
    82 	  | ato (Var ((a, i), _)) n =
    83 	           "\n*** " ^ indent n ^ "Var (" ^ a ^ ", " ^ 
    84                                                string_of_int i ^ "), _)"
    85 	  | ato (Bound i) n = 
    86 	           "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
    87 	  | ato (Abs (a, _, body)) n = 
    88 	           "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1)
    89 	  | ato (f $ t) n = (ato f n ^ ato t (n + 1))
    90 in
    91 fun atomw t = writeln ("\n*** -------------" ^ ato t 0 ^ "\n***");
    92 fun atomt t = tracing ("\n*** -------------" ^ ato t 0 ^ "\n***");
    93 end;
    94 
    95 fun term_detail2str t =
    96     let fun ato (Const (a, T)) n = 
    97 	    "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ string_of_typ T ^ ")"
    98 	  | ato (Free (a, T)) n =  
    99 	    "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ string_of_typ T ^ ")"
   100 	  | ato (Var ((a, i), T)) n =
   101 	    "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), "^
   102 	    string_of_typ T ^ ")"
   103 	  | ato (Bound i) n = 
   104 	    "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
   105 	  | ato (Abs(a, T, body))  n = 
   106 	    "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ string_of_typ T ^ ",.."
   107 	    ^ ato body (n + 1)
   108 	  | ato (f $ t) n = ato f n ^ ato t (n + 1)
   109     in "\n*** " ^ ato t 0 ^ "\n***" end;
   110 fun term_detail2str_thy thy t =
   111     let fun ato (Const (a, T)) n = 
   112 	    "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ string_of_typ_thy thy T ^ ")"
   113 	  | ato (Free (a, T)) n =  
   114 	    "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ string_of_typ_thy thy T ^ ")"
   115 	  | ato (Var ((a, i), T)) n =
   116 	    "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), "^
   117 	    string_of_typ_thy thy T ^ ")"
   118 	  | ato (Bound i) n = 
   119 	    "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
   120 	  | ato (Abs(a, T, body))  n = 
   121 	    "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ string_of_typ_thy thy T ^ ",.."
   122 	    ^ ato body (n + 1)
   123 	  | ato (f $ t) n = ato f n ^ ato t (n + 1)
   124     in "\n*** " ^ ato t 0 ^ "\n***" end;
   125 fun atomwy t = (writeln o term_detail2str) t;
   126 fun atomty t = (tracing o term_detail2str) t;
   127 fun atomty_thy thy t = (tracing o (term_detail2str_thy thy)) t;
   128 
   129 fun term_str thy (Const(s,_)) = s
   130   | term_str thy (Free(s,_)) = s
   131   | term_str thy (Var((s,i),_)) = s^(string_of_int i)
   132   | term_str thy (Bound i) = "B."^(string_of_int i)
   133   | term_str thy (Abs(s,_,_)) = s
   134   | term_str thy t = error("term_str not for "^term2str t);
   135 
   136 (*.contains the fst argument the second argument (a leave! of term).*)
   137 fun contains_term (Abs(_,_,body)) t = contains_term body t 
   138   | contains_term (f $ f') t = 
   139     contains_term f t orelse contains_term f' t
   140   | contains_term s t = t = s;
   141 (*.contains the term a VAR(("*",_),_) ?.*)
   142 fun contains_Var (Abs(_,_,body)) = contains_Var body
   143   | contains_Var (f $ f') = contains_Var f orelse contains_Var f'
   144   | contains_Var (Var _) = true
   145   | contains_Var _ = false;
   146 (* contains_Var (str2term "?z = 3") (*true*);
   147    contains_Var (str2term "z = 3")  (*false*);
   148    *)
   149 
   150 (*fun int_of_str str =
   151     let val ss = Symbol.explode str
   152 	val str' = case ss of
   153 	   "("::s => drop_last s | _ => ss
   154     in case BasisLibrary.Int.fromString (implode str') of
   155 	     SOME i => SOME i
   156 	   | NONE => NONE end;*)
   157 fun int_of_str str =
   158     let val ss = Symbol.explode str
   159 	val str' = case ss of
   160 	   "("::s => drop_last s | _ => ss
   161     in (SOME (Thy_Output.integer (implode str'))) handle _ => NONE end;
   162 (*
   163 > int_of_str "123";
   164 val it = SOME 123 : int option
   165 > int_of_str "(-123)";
   166 val it = SOME 123 : int option
   167 > int_of_str "#123";
   168 val it = NONE : int option
   169 > int_of_str "-123";
   170 val it = SOME ~123 : int option
   171 *)
   172 fun int_of_str' str = 
   173     case int_of_str str of
   174 	SOME i => i
   175       | NONE => raise TERM ("int_of_string: no int-string",[]);
   176 val str2int = int_of_str';
   177     
   178 fun is_numeral str = case int_of_str str of
   179 			 SOME _ => true
   180 		       | NONE => false;
   181 val is_no = is_numeral;
   182 (* see /home/neuper/repos/FHpoly/src/HOL/Decision_Procs/Approximation.thy:
   183 
   184   val mk_int = HOLogic.mk_number @{typ int} o @{code integer_of_int};
   185   val dest_int = @{code int_of_integer} o snd o HOLogic.dest_number;
   186 
   187   fun nat_of_term t = @{code nat_of_integer}
   188     (HOLogic.dest_nat t handle TERM _ => snd (HOLogic.dest_number t));
   189   etc...
   190 *)
   191 fun is_num (Free (s,_)) = if is_numeral s then true else false
   192   | is_num _ = false;
   193 (*>
   194 > is_num ((term_of o the o (parse thy)) "#1");
   195 val it = true : bool
   196 > is_num ((term_of o the o (parse thy)) "#-1");
   197 val it = true : bool
   198 > is_num ((term_of o the o (parse thy)) "a123");
   199 val it = false : bool
   200 *)
   201 
   202 (*fun int_of_Free (Free (intstr, _)) =
   203     (case BasisLibrary.Int.fromString intstr of
   204 	     SOME i => i
   205 	   | NONE => error ("int_of_Free ( "^ intstr ^", _)"))
   206   | int_of_Free t = error ("int_of_Free ( "^ term2str t ^" )");*)
   207 fun int_of_Free (Free (intstr, _)) = (Thy_Output.integer intstr
   208     handle _ => error ("int_of_Free ( "^ intstr ^", _)"))
   209   | int_of_Free t = error ("int_of_Free ( "^ term2str t ^" )");
   210 
   211 fun vars t =
   212   let
   213     fun scan vs (Const(s,T)) = vs
   214       | scan vs (t as Free(s,T)) = if is_no s then vs else t::vs
   215       | scan vs (t as Var((s,i),T)) = t::vs
   216       | scan vs (Bound i) = vs 
   217       | scan vs (Abs(s,T,t)) = scan vs t
   218       | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
   219   in (distinct o (scan [])) t end;
   220 
   221 fun is_Free (Free _) = true
   222   | is_Free _ = false;
   223 fun is_fun_id (Const _) = true
   224   | is_fun_id (Free _) = true
   225   | is_fun_id _ = false;
   226 fun is_f_x (f $ x) = is_fun_id f andalso is_Free x
   227   | is_f_x _ = false;
   228 (* is_f_x (str2term "q_0/2 * L * x") (*false*);
   229    is_f_x (str2term "M_b x") (*true*);
   230   *)
   231 fun vars_str t =
   232   let
   233     fun scan vs (Const(s,T)) = vs
   234       | scan vs (t as Free(s,T)) = if is_no s then vs else s::vs
   235       | scan vs (t as Var((s,i),T)) = (s^"_"^(string_of_int i))::vs
   236       | scan vs (Bound i) = vs 
   237       | scan vs (Abs(s,T,t)) = scan vs t
   238       | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
   239   in (distinct o (scan [])) t end;
   240 
   241 fun ids2str t =
   242   let
   243     fun scan vs (Const(s,T)) = if is_no s then vs else s::vs
   244       | scan vs (t as Free(s,T)) = if is_no s then vs else s::vs
   245       | scan vs (t as Var((s,i),T)) = (s^"_"^(string_of_int i))::vs
   246       | scan vs (Bound i) = vs 
   247       | scan vs (Abs(s,T,t)) = scan (s::vs) t
   248       | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
   249   in (distinct o (scan [])) t end;
   250 fun is_bdv str =
   251     case Symbol.explode str of
   252 	"b"::"d"::"v"::_ => true
   253       | _ => false;
   254 fun is_bdv_ (Free (s,_)) = is_bdv s
   255   | is_bdv_ _ = false;
   256 
   257 (* is a term a substitution for a bdv as found in programs *)
   258 fun is_bdv_subst (Const ("List.list.Cons", _) $
   259       (Const ("Product_Type.Pair", _) $ Free (str, _) $ _) $ _) = is_bdv str
   260   | is_bdv_subst _ = false;
   261 
   262 fun free2str (Free (s,_)) = s
   263   | free2str t = error ("free2str not for " ^ term2str t);
   264 fun str_of_free_opt (Free (s,_)) = SOME s
   265   | str_of_free_opt _ = NONE
   266 fun free2int (t as Free (s, _)) = ((str2int s)
   267     handle _ => error ("free2int: " ^ term_detail2str t))
   268   | free2int t = error ("free2int: " ^ term_detail2str t);
   269 
   270 (*compare Logic.unvarify_global, which rejects Free*)
   271 fun var2free (t as Const(s,T)) = t
   272   | var2free (t as Free(s,T)) = t
   273   | var2free (Var((s,i),T)) = Free(s,T)
   274   | var2free (t as Bound i) = t 
   275   | var2free (Abs(s,T,t)) = Abs(s,T,var2free t)
   276   | var2free (t1 $ t2) = (var2free t1) $ (var2free t2);
   277   
   278 (*27.8.01: doesn't find some subterm ???!???*)
   279 (*2010 free2var -> Logic.varify, but take care of 'Free ("1",_)'*)
   280 fun free2var (t as Const (s, T)) = t
   281   | free2var (t as Free (s, T)) = if is_no s then t else Var ((s, 0), T)
   282   | free2var (t as Var ((s, i), T)) = t
   283   | free2var (t as Bound i) = t 
   284   | free2var (Abs (s, T, t)) = Abs (s, T, free2var t)
   285   | free2var (t1 $ t2) = (free2var t1) $ (free2var t2);
   286   
   287 
   288 fun mk_listT T = Type ("List.list", [T]);
   289 fun list_const T = 
   290   Const("List.list.Cons", [T, mk_listT T] ---> mk_listT T);
   291 (*28.8.01: TODO: get type from head of list: 1 arg less!!!*)
   292 fun list2isalist T [] = Const("List.list.Nil",mk_listT T)
   293   | list2isalist T (t::ts) = (list_const T) $ t $ (list2isalist T ts);
   294 (*
   295 > val tt = (term_of o the o (parse thy)) "R=(R::real)";
   296 > val TT = type_of tt;
   297 > val ss = list2isalist TT [tt,tt,tt];
   298 > (cterm_of thy) ss;
   299 val it = "[R = R, R = R, R = R]" : cterm  *)
   300 
   301 fun isapair2pair (Const ("Product_Type.Pair",_) $ a $ b) = (a,b)
   302   | isapair2pair t = 
   303     error ("isapair2pair called with "^term2str t);
   304 
   305 val listType = Type ("List.list",[Type ("bool",[])]);
   306 fun isalist2list ls =
   307   let
   308     fun get es (Const("List.list.Cons",_) $ t $ ls) = get (t::es) ls
   309       | get es (Const("List.list.Nil",_)) = es
   310       | get _ t = 
   311 	error ("isalist2list applied to NON-list '"^term2str t^"'")
   312   in (rev o (get [])) ls end;
   313 (*      
   314 > val il = str2term "[a=b,c=d,e=f]";
   315 > val l = isalist2list il;
   316 > (tracing o terms2str) l;
   317 ["a = b","c = d","e = f"]
   318 
   319 > val il = str2term "ss___::bool list";
   320 > val l = isalist2list il;
   321 [Free ("ss___", "bool List.list")]
   322 *)
   323 
   324 (*review Isabelle2009/src/HOL/Tools/hologic.ML*)
   325 (*val prop = Type ("HOL.Trueprop",[]);      ~/Diss.99/Integers-Isa/tools.sml*)
   326 val bool = Type ("HOL.bool",[]);     (* 2002 Integ.int *)
   327 val Trueprop = HOLogic.Trueprop;
   328 fun mk_prop t = HOLogic.mk_Trueprop t;
   329 val true_as_cterm = cterm_of (Thy_Info.get_theory "HOL") @{term True};
   330 val false_as_cterm = cterm_of (Thy_Info.get_theory "HOL") @{term False};
   331 
   332 infixr 5 -->;                    (*2002 /Pure/term.ML *)
   333 infixr --->;			 (*2002 /Pure/term.ML *)
   334 fun S --> T = Type("fun",[S,T]); (*2002 /Pure/term.ML *)
   335 val op ---> = foldr (op -->);    (*2002 /Pure/term.ML *)
   336 fun list_implies ([], B) = B : term (*2002 /term.ML *)
   337   | list_implies (A::AS, B) = Logic.implies $ A $ list_implies(AS,B);
   338 
   339 
   340 (** substitution **)
   341 
   342 fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) =      (* = thm.ML *)
   343       match_bvs(s, t, if x="" orelse y="" then al
   344                                           else (x,y)::al)
   345   | match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al))
   346   | match_bvs(_,_,al) = al;
   347 fun ren_inst(insts,prop,pat,obj) =              (* = thm.ML *)
   348   let val ren = match_bvs(pat,obj,[])
   349       fun renAbs(Abs(x,T,b)) =
   350             Abs(case assoc_string(ren,x) of NONE => x 
   351 	  | SOME(y) => y, T, renAbs(b))
   352         | renAbs(f$t) = renAbs(f) $ renAbs(t)
   353         | renAbs(t) = t
   354   in subst_vars insts (if null(ren) then prop else renAbs(prop)) end;
   355 
   356 
   357 
   358 
   359 
   360 
   361 fun dest_equals' (Const("HOL.eq",_) $ t $ u)  =  (t,u)(* logic.ML: Const("=="*)
   362   | dest_equals' t = raise TERM("dest_equals'", [t]);
   363 val lhs_ = (fst o dest_equals');
   364 val rhs_ = (snd o dest_equals');
   365 
   366 fun is_equality (Const("HOL.eq",_) $ t $ u)  =  true  (* logic.ML: Const("=="*)
   367   | is_equality _ = false;
   368 fun mk_equality (t,u) = (Const("HOL.eq",[type_of t,type_of u]--->bool) $ t $ u); 
   369 fun is_expliceq (Const("HOL.eq",_) $ (Free _) $ u)  =  true
   370   | is_expliceq _ = false;
   371 fun strip_trueprop (Const("HOL.Trueprop",_) $ t) = t
   372   | strip_trueprop t = t;
   373 (*  | strip_trueprop t = raise TERM("strip_trueprop", [t]);
   374 *)
   375 
   376 (*.(A1==>...An==>B) goes to (A1==>...An==>).*)
   377 fun strip_imp_prems' (Const("==>", T) $ A $ t) = 
   378     let fun coll_prems As (Const("==>", _) $ A $ t) = 
   379 	    coll_prems (As $ (Logic.implies $ A)) t
   380 	  | coll_prems As _ = SOME As
   381     in coll_prems (Logic.implies $ A) t end
   382   | strip_imp_prems' _ = NONE;  (* logic.ML: term -> term list*)
   383 (*
   384   val thm = real_mult_div_cancel1;
   385   val prop = (#prop o rep_thm) thm;
   386   atomt prop;
   387 *** -------------
   388 *** Const ( ==>)
   389 *** . Const ( Trueprop)
   390 *** . . Const ( Not)
   391 *** . . . Const ( op =)
   392 *** . . . . Var ((k, 0), )
   393 *** . . . . Const ( 0)
   394 *** . Const ( Trueprop)
   395 *** . . Const ( op =)                                                          *** .............
   396   val SOME t = strip_imp_prems' ((#prop o rep_thm) thm);
   397   atomt t;
   398 *** -------------
   399 *** Const ( ==>)
   400 *** . Const ( Trueprop)
   401 *** . . Const ( Not)
   402 *** . . . Const ( op =)
   403 *** . . . . Var ((k, 0), )
   404 *** . . . . Const ( 0)
   405 
   406   val thm = real_le_anti_sym;
   407   val prop = (#prop o rep_thm) thm;
   408   atomt prop;
   409 *** -------------
   410 *** Const ( ==>)
   411 *** . Const ( Trueprop)
   412 *** . . Const ( op <=)
   413 *** . . . Var ((z, 0), )
   414 *** . . . Var ((w, 0), )
   415 *** . Const ( ==>)
   416 *** . . Const ( Trueprop)
   417 *** . . . Const ( op <=)
   418 *** . . . . Var ((w, 0), )
   419 *** . . . . Var ((z, 0), )
   420 *** . . Const ( Trueprop)
   421 *** . . . Const ( op =)
   422 *** .............
   423   val SOME t = strip_imp_prems' ((#prop o rep_thm) thm);
   424   atomt t;
   425 *** -------------
   426 *** Const ( ==>)
   427 *** . Const ( Trueprop)
   428 *** . . Const ( op <=)
   429 *** . . . Var ((z, 0), )
   430 *** . . . Var ((w, 0), )
   431 *** . Const ( ==>)
   432 *** . . Const ( Trueprop)
   433 *** . . . Const ( op <=)
   434 *** . . . . Var ((w, 0), )
   435 *** . . . . Var ((z, 0), )
   436 *)
   437 
   438 (*. (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch.*)
   439 fun ins_concl (Const("==>", T) $ A $ t) B = Logic.implies $ A $ (ins_concl t B)
   440   | ins_concl (Const("==>", T) $ A    ) B = Logic.implies $ A $ B
   441   | ins_concl t B =  raise TERM("ins_concl", [t, B]);
   442 (*
   443   val thm = real_le_anti_sym;
   444   val prop = (#prop o rep_thm) thm;
   445   val concl = Logic.strip_imp_concl prop;
   446   val SOME prems = strip_imp_prems' prop;
   447   val prop' = ins_concl prems concl;
   448   prop = prop';
   449   atomt prop;
   450   atomt prop';
   451 *)
   452 
   453 
   454 fun vperm (Var _, Var _) = true  (*2002 Pure/thm.ML *)
   455   | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
   456   | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
   457   | vperm (t, u) = (t = u);
   458 
   459 (*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*)
   460 fun mem_term (_, []) = false
   461   | mem_term (t, t'::ts) = t aconv t' orelse mem_term(t,ts);
   462 fun subset_term ([], ys) = true
   463   | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term(xs, ys);
   464 fun eq_set_term (xs, ys) =
   465     xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
   466 (*a total, irreflexive ordering on index names*)
   467 fun xless ((a,i), (b,j): indexname) = i<j  orelse  (i=j andalso a<b);
   468 (*a partial ordering (not reflexive) for atomic terms*)
   469 fun atless (Const (a,_), Const (b,_))  =  a<b
   470   | atless (Free (a,_), Free (b,_)) =  a<b
   471   | atless (Var(v,_), Var(w,_))  =  xless(v,w)
   472   | atless (Bound i, Bound j)  =   i<j
   473   | atless _  =  false;
   474 (*insert atomic term into partially sorted list, suppressing duplicates (?)*)
   475 fun insert_aterm (t,us) =
   476   let fun inserta [] = [t]
   477         | inserta (us as u::us') =
   478               if atless(t,u) then t::us
   479               else if t=u then us (*duplicate*)
   480               else u :: inserta(us')
   481   in  inserta us  end;
   482 
   483 (*Accumulates the Vars in the term, suppressing duplicates*)
   484 fun add_term_vars (t, vars: term list) = case t of
   485     Var   _ => insert_aterm(t,vars)
   486   | Abs (_,_,body) => add_term_vars(body,vars)
   487   | f$t =>  add_term_vars (f, add_term_vars(t, vars))
   488   | _ => vars;
   489 fun term_vars t = add_term_vars(t,[]);
   490 
   491 
   492 fun var_perm (t, u) = (*2002 Pure/thm.ML *)
   493   vperm (t, u) andalso eq_set_term (term_vars t, term_vars u);
   494     
   495 (*2002 fun decomp_simp, Pure/thm.ML *)
   496 fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs)
   497     andalso not (is_Var lhs);
   498 
   499 
   500 fun str_of_int n = 
   501   if n < 0 then "-"^((string_of_int o abs) n)
   502   else string_of_int n;
   503 (*
   504 > str_of_int 1;
   505 val it = "1" : string                                                          > str_of_int ~1;
   506 val it = "-1" : string
   507 *)
   508 
   509 
   510 fun power b 0 = 1
   511   | power b n = 
   512   if n>0 then b*(power b (n-1))
   513   else error ("power "^(str_of_int b)^" "^(str_of_int n));
   514 (*
   515 > power 2 3;
   516 val it = 8 : int
   517 > power ~2 3;
   518 val it = ~8 : int
   519 > power ~3 2;
   520 val it = 9 : int
   521 > power 3 ~2;
   522 *)
   523 fun gcd 0 b = b
   524   | gcd a b = if a < b then gcd (b mod a) a
   525 	      else gcd (a mod b) b;
   526 fun sign n = if n < 0 then ~1
   527 	     else if n = 0 then 0 else 1;
   528 fun sign2 n1 n2 = (sign n1) * (sign n2);
   529 
   530 infix dvd;
   531 fun d dvd n = n mod d = 0;
   532 
   533 fun divisors n =
   534   let fun pdiv ds d n = 
   535     if d=n then d::ds
   536     else if d dvd n then pdiv (d::ds) d (n div d)
   537 	 else pdiv ds (d+1) n
   538   in pdiv [] 2 n end;
   539 
   540 divisors 30;
   541 divisors 32;
   542 divisors 60;
   543 divisors 11;
   544 
   545 fun doubles ds = (* ds is ordered *)
   546   let fun dbls ds [] = ds
   547 	| dbls ds [i] = ds
   548 	| dbls ds (i::i'::is) = if i=i' then dbls (i::ds) is
   549 				else dbls ds (i'::is)
   550   in dbls [] ds end;
   551 (*> doubles [2,3,4];
   552 val it = [] : int list
   553 > doubles [2,3,3,5,5,7];
   554 val it = [5,3] : int list*)
   555 
   556 fun squfact 0 = 0
   557   | squfact 1 = 1
   558   | squfact n = foldl op* (1, (doubles o divisors) n);
   559 (*> squfact 30;
   560 val it = 1 : int
   561 > squfact 32;
   562 val it = 4 : int
   563 > squfact 60;
   564 val it = 2 : int
   565 > squfact 11;
   566 val it = 1 : int*)
   567 
   568 
   569 fun dest_type (Type(T,[])) = T
   570   | dest_type T = 
   571     (atomtyp T;
   572      error ("... dest_type: not impl. for this type"));
   573 
   574 fun term_of_num ntyp n = Free (str_of_int n, ntyp);
   575 
   576 fun pairT T1 T2 = Type ("*", [T1, T2]);
   577 (*> val t = str2term "(1,2)";
   578 > type_of t = pairT HOLogic.realT HOLogic.realT;
   579 val it = true : bool
   580 *)
   581 fun PairT T1 T2 = ([T1, T2] ---> Type ("*", [T1, T2]));
   582 (*> val t = str2term "(1,2)";
   583 > val Const ("Product_Type.Pair",pT) $ _ $ _ = t;
   584 > pT = PairT HOLogic.realT HOLogic.realT;
   585 val it = true : bool
   586 *)
   587 fun pairt t1 t2 =
   588     Const ("Product_Type.Pair", PairT (type_of t1) (type_of t2)) $ t1 $ t2;
   589 (*> val t = str2term "(1,2)";
   590 > val (t1, t2) = (str2term "1", str2term "2");
   591 > t = pairt t1 t2;
   592 val it = true : bool*)
   593 
   594 
   595 fun num_of_term (t as Free (s,_)) = 
   596     (case int_of_str s of
   597 	 SOME s' => s'
   598        | NONE => error ("num_of_term not for "^ term2str t))
   599   | num_of_term t = error ("num_of_term not for "^term2str t);
   600 
   601 fun mk_factroot op_(*=thy.sqrt*) T fact root = 
   602   Const ("Groups.times_class.times", [T, T] ---> T) $ (term_of_num T fact) $
   603   (Const (op_, T --> T) $ term_of_num T root);
   604 (*
   605 val T =  (type_of o term_of o the) (parse thy "#12::real");
   606 val t = mk_factroot "SqRoot.sqrt" T 2 3;
   607 (cterm_of thy) t;
   608 val it = "#2 * sqrt #3 " : cterm
   609 *)
   610 fun var_op_num v op_ optype ntyp n =
   611   Const (op_, optype) $ v $ 
   612    Free (str_of_int  n, ntyp);
   613 
   614 fun num_op_var v op_ optype ntyp n =
   615   Const (op_,optype) $  
   616    Free (str_of_int n, ntyp) $ v;
   617 
   618 fun num_op_num T1 T2 (op_,Top) n1 n2 = 
   619   Const (op_,Top) $ 
   620   Free (str_of_int n1, T1) $ Free (str_of_int n2, T2);
   621 (*
   622 > val t = num_op_num "Int" 3 4;
   623 > atomty t;
   624 > string_of_cterm ((cterm_of thy) t);
   625 *)
   626 
   627 
   628 
   629 fun const_in str (Const _) = false
   630   | const_in str (Free (s,_)) = if strip_thy s = str then true else false
   631   | const_in str (Bound _) = false
   632   | const_in str (Var _) = false
   633   | const_in str (Abs (_,_,body)) = const_in str body
   634   | const_in str (f$u) = const_in str f orelse const_in str u;
   635 (*
   636 > val t = (term_of o the o (parse thy)) "6 + 5 * sqrt 4 + 3";
   637 > const_in "sqrt" t;
   638 val it = true : bool
   639 > val t = (term_of o the o (parse thy)) "6 + 5 * 4 + 3";
   640 > const_in "sqrt" t;
   641 val it = false : bool
   642 *)
   643 
   644 (*used for calculating built in binary operations in Isabelle2002->Float.ML*)
   645 (*fun calc "Groups.plus_class.plus"  (n1, n2) = n1+n2
   646   | calc "Groups.minus_class.minus"  (n1, n2) = n1-n2
   647   | calc "Groups.times_class.times"  (n1, n2) = n1*n2
   648   | calc "Fields.inverse_class.divide"(n1, n2) = n1 div n2
   649   | calc "Atools.pow"(n1, n2) = power n1 n2
   650   | calc op_ _ = error ("calc: operator = "^op_^" not defined");-----*)
   651 fun calc_equ "less"  (n1, n2) = n1 < n2
   652   | calc_equ "less_eq" (n1, n2) = n1 <= n2
   653   | calc_equ op_ _ = 
   654   error ("calc_equ: operator = "^op_^" not defined");
   655 fun sqrt (n:int) = if n < 0 then 0
   656     (*FIXME ~~~*)  else (trunc o Math.sqrt o Real.fromInt) n;
   657 
   658 fun mk_thmid thmid op_ n1 n2 = 
   659   thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
   660 
   661 fun dest_binop_typ (Type("fun",[range,Type("fun",[arg2,arg1])])) =
   662   (arg1,arg2,range)
   663   | dest_binop_typ _ = error "dest_binop_typ: not binary";
   664 (* -----
   665 > val t = (term_of o the o (parse thy)) "#3^#4";
   666 > val hT = type_of (head_of t);
   667 > dest_binop_typ hT;
   668 val it = ("'a","nat","'a") : typ * typ * typ
   669  ----- *)
   670 
   671 
   672 (** transform binary numeralsstrings **)
   673 (*Makarius 100308, hacked by WN*)
   674 val numbers_to_string =
   675   let
   676     fun dest_num t =
   677       (case try HOLogic.dest_number t of
   678         SOME (T, i) =>
   679           (*if T = @{typ int} orelse T = @{typ real} then WN*)
   680             SOME (Free (signed_string_of_int i, T))
   681           (*else NONE  WN*)
   682       | NONE => NONE);
   683 
   684     fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
   685       | to_str (t as (u1 $ u2)) =
   686           (case dest_num t of
   687             SOME t' => t'
   688           | NONE => to_str u1 $ to_str u2)
   689       | to_str t = perhaps dest_num t;
   690   in to_str end
   691 
   692 (*.make uminus uniform: 
   693    Const ("uminus", _) $ Free ("2", "RealDef.real") --> Free ("-2", _)
   694 to be used immediately before evaluation of numerals; 
   695 see Scripts/calculate.sml .*)
   696 (*2002 fun(*app_num_tr'2 (Const("0",T)) = Free("0",T)
   697   | app_num_tr'2 (Const("1",T)) = Free("1",T)
   698   |*)app_num_tr'2 (t as Const("uminus",_) $ Free(s,T)) = 
   699     (case int_of_str s of SOME i => 
   700 			  if i > 0 then Free("-"^s,T) else Free(s,T)
   701 		       | NONE => t)
   702 (*| app_num_tr'2 (t as Const(s,T)) = t
   703   | app_num_tr'2 (Const("Numeral.number_of",Type ("fun", [_, T])) $ t) = 
   704     Free(NumeralSyntax.dest_bin_str t, T)
   705   | app_num_tr'2 (t as Free(s,T)) = t
   706   | app_num_tr'2 (t as Var(n,T)) = t
   707   | app_num_tr'2 (t as Bound i) = t
   708 *)| app_num_tr'2 (Abs(s,T,body)) = Abs(s,T, app_num_tr'2 body)
   709   | app_num_tr'2 (t1 $ t2) = (app_num_tr'2 t1) $ (app_num_tr'2 t2)
   710   | app_num_tr'2 t = t;
   711 *)
   712 val uminus_to_string =
   713     let
   714 	fun dest_num t =
   715 	    (case t of
   716 		 (Const ("Groups.uminus_class.uminus", _) $ Free (s, T)) => 
   717 		 (case int_of_str s of
   718 		      SOME i => 
   719 		      SOME (Free (signed_string_of_int (~1 * i), T))
   720 		    | NONE => NONE)
   721 	       | _ => NONE);
   722 	    
   723 	fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
   724 	  | to_str (t as (u1 $ u2)) =
   725             (case dest_num t of
   726 		 SOME t' => t'
   727                | NONE => to_str u1 $ to_str u2)
   728 	  | to_str t = perhaps dest_num t;
   729     in to_str end;
   730 
   731 
   732 (*2002 fun num_str thm =
   733   let 
   734     val {sign_ref = sign_ref, der = der, maxidx = maxidx,
   735 	    shyps = shyps, hyps = hyps, (*tpairs = tpairs,*) prop = prop} = 
   736 	rep_thm_G thm;
   737     val prop' = app_num_tr'1 prop;
   738   in assbl_thm sign_ref der maxidx shyps hyps (*tpairs*) prop' end;*)
   739 fun num_str thm =
   740   let val (deriv, 
   741 	   {thy = thy, tags = tags, maxidx = maxidx, shyps = shyps, 
   742 	    hyps = hyps, tpairs = tpairs, prop = prop}) = rep_thm_G thm
   743     val prop' = numbers_to_string prop;
   744   in assbl_thm deriv thy tags maxidx shyps hyps tpairs prop' end;
   745 
   746 fun get_thm' xstring = (*?covers 2009 Thm?!, replaces 2002 fun get_thm :
   747 val it = fn : theory -> xstring -> Thm.thm*)
   748     Thm (xstring, 
   749 	 num_str (Proof_Context.get_thm (thy2ctxt' "Isac") xstring)); 
   750 
   751 (** get types of Free and Abs for parse' **)
   752 (*11.1.00: not used, fix-typed +,*,-,^ instead *)
   753 
   754 val dummyT = Type ("dummy",[]);
   755 val dummyT = TVar (("DUMMY",0),[]);
   756 
   757 (* assumes only 1 type for numerals 
   758    and different identifiers for Const, Free and Abs *)
   759 fun get_types t = 
   760   let
   761     fun get ts  (Const(s,T)) = (s,T)::ts
   762       | get ts  (Free(s,T)) = if is_no s 
   763 				then ("#",T)::ts else (s,T)::ts
   764       | get ts  (Var(n,T)) = ts
   765       | get ts  (Bound i) = ts
   766       | get ts  (Abs(s,T,body)) = get ((s,T)::ts)  body
   767       | get ts  (t1 $ t2) = (get ts  t1) @ (get ts  t2)
   768   in distinct (get [] t) end;
   769 (*
   770 val t = (term_of o the o (parse thy)) "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)";
   771 get_types t;
   772 *)
   773 
   774 (*11.1.00: not used, fix-typed +,*,-,^ instead *)
   775 fun set_types al (Const(s,T)) = 
   776     (case assoc (al,s) of
   777        SOME T' => Const(s,T')
   778      | NONE => (warning ("set_types: no type for "^s); Const(s,dummyT)))
   779   | set_types al (Free(s,T)) = 
   780   if is_no s then
   781     (case assoc (al,"#") of
   782       SOME T' => Free(s,T')
   783     | NONE => (warning ("set_types: no type for numerals"); Free(s,T)))
   784   else (case assoc (al,s) of
   785 	       SOME T' => Free(s,T')
   786 	     | NONE => (warning ("set_types: no type for "^s); Free(s,T)))
   787   | set_types al (Var(n,T)) = Var(n,T)
   788   | set_types al (Bound i) = Bound i
   789   | set_types al (Abs(s,T,body)) = 
   790 		 (case assoc (al,s) of
   791 		    SOME T'  => Abs(s,T', set_types al body)
   792 		  | NONE => (warning ("set_types: no type for "^s);
   793 			     Abs(s,T, set_types al body)))
   794   | set_types al (t1 $ t2) = (set_types al t1) $ (set_types al t2);
   795 (*
   796 val t = (term_of o the o (parse thy)) "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)";
   797 val al = get_types t;
   798 
   799 val t = (term_of o the o (parse thy)) "x = #0 + #-1 * #-4";
   800 atomty t;                         (* 'a *)
   801 val t' = set_types al t;
   802 atomty t';                        (*real*)
   803 (cterm_of thy) t';
   804 val it = "x = #0 + #-1 * #-4" : cterm
   805 
   806 val t = (term_of o the o (parse thy)) 
   807   "#5 * x + x ^^^ #2 = (#2 + x) ^^^ #2";
   808 atomty t;
   809 val t' = set_types al t;
   810 atomty t';
   811 (cterm_of thy) t';
   812 uncaught exception TYPE               (*^^^ is new, NOT in al*)
   813 *)
   814       
   815 
   816 (** from Descript.ML **)
   817 
   818 (** decompose an isa-list to an ML-list 
   819     i.e. [] belong to the meta-language, too **)
   820 
   821 fun is_list ((Const("List.list.Cons",_)) $ _ $ _) = true
   822   | is_list _ = false;
   823 (* val (SOME ct) = parse thy "lll::real list";
   824 > val ty = (#t o rep_cterm) ct;
   825 > is_list ty;
   826 val it = false : bool
   827 > val (SOME ct) = parse thy "[lll]";
   828 > val ty = (#t o rep_cterm) ct;
   829 > is_list ty;
   830 val it = true : bool *)
   831 
   832 
   833 
   834 fun mk_Free (s,T) = Free(s,T);
   835 fun mk_free T s =  Free(s,T);
   836 
   837 (*Special case: one argument cp from Isabelle2002/src/Pure/term.ML*)
   838 fun subst_bound (arg, t) : term = (*WN100908 neglects 'raise Same.SAME'*)
   839   let fun subst (t as Bound i, lev) =
   840             if i<lev then  t    (*var is locally bound*)
   841             else  if i=lev then incr_boundvars lev arg
   842                            else Bound(i-1)  (*loose: change it*)
   843         | subst (Abs(a,T,body), lev) = Abs(a, T,  subst(body,lev+1))
   844         | subst (f$t, lev) =  subst(f,lev)  $  subst(t,lev)
   845         | subst (t,lev) = t
   846   in  subst (t,0)  end;
   847 
   848 (*instantiate let; necessary for ass_up*)
   849 fun inst_abs thy (Const sT) = Const sT  (*TODO.WN100907 drop thy*)
   850   | inst_abs thy (Free sT) = Free sT
   851   | inst_abs thy (Bound n) = Bound n
   852   | inst_abs thy (Var iT) = Var iT
   853   | inst_abs thy (Const ("HOL.Let",T1) $ e $ (Abs (v, T2, b))) = 
   854     let val b' = subst_bound (Free (v, T2), b);
   855     (*fun variant_abs: term.ML*)
   856     in Const ("HOL.Let", T1) $ inst_abs thy e $ (Abs (v, T2, inst_abs thy b')) end
   857   | inst_abs thy (t1 $ t2) = inst_abs thy t1 $ inst_abs thy t2
   858   | inst_abs thy t = t;
   859 (*val scr =    
   860    "Script Make_fun_by_explicit (f_::real) (v_::real) (eqs_::bool list) = \
   861    \ (let h_ = (hd o (filterVar f_)) eqs_;                    \
   862    \      e_1 = hd (dropWhile (ident h_) eqs_);       \
   863    \      vs_ = dropWhile (ident f_) (Vars h_);                \
   864    \      v_1 = hd (dropWhile (ident v_) vs_);                \
   865    \      (s_1::bool list)=(SubProblem(DiffApp_,[univar,equation],[no_met])\
   866    \                          [BOOL e_1, REAL v_1])\
   867    \ in Substitute [(v_1 = (rhs o hd) s_1)] h_)";
   868 > val ttt = (term_of o the o (parse thy)) scr;
   869 > tracing(term2str ttt);
   870 > atomt ttt;
   871 *** -------------
   872 *** Const ( DiffApp.Make'_fun'_by'_explicit)
   873 *** . Free ( f_, )
   874 *** . Free ( v_, )
   875 *** . Free ( eqs_, )
   876 *** . Const ( Let)
   877 *** . . Const ( Fun.op o)
   878 *** . . . Const ( List.hd)
   879 *** . . . Const ( DiffApp.filterVar)
   880 *** . . . . Free ( f_, )
   881 *** . . . Free ( eqs_, )
   882 *** . . Abs( h_,..
   883 *** . . . Const ( Let)
   884 *** . . . . Const ( List.hd)
   885 *** . . . . . Const ( List.dropWhile)
   886 *** . . . . . . Const ( Atools.ident)
   887 *** . . . . . . . Bound 0                     <---- Free ( h_, )
   888 *** . . . . . . Free ( eqs_, )
   889 *** . . . . Abs( e_1,..
   890 *** . . . . . Const ( Let)
   891 *** . . . . . . Const ( List.dropWhile)
   892 *** . . . . . . . Const ( Atools.ident)
   893 *** . . . . . . . . Free ( f_, )
   894 *** . . . . . . . Const ( Tools.Vars)
   895 *** . . . . . . . . Bound 1                       <---- Free ( h_, )
   896 *** . . . . . . Abs( vs_,..
   897 *** . . . . . . . Const ( Let)
   898 *** . . . . . . . . Const ( List.hd)
   899 *** . . . . . . . . . Const ( List.dropWhile)
   900 *** . . . . . . . . . . Const ( Atools.ident)
   901 *** . . . . . . . . . . . Free ( v_, )
   902 *** . . . . . . . . . . Bound 0                   <---- Free ( vs_, )
   903 *** . . . . . . . . Abs( v_1,..
   904 *** . . . . . . . . . Const ( Let)
   905 *** . . . . . . . . . . Const ( Script.SubProblem)
   906 *** . . . . . . . . . . . Const ( Pair)
   907 *** . . . . . . . . . . . . Free ( DiffApp_, )
   908 *** . . . . . . . . . . . . Const ( Pair)
   909 *** . . . . . . . . . . . . . Const ( List.list.Cons)
   910 *** . . . . . . . . . . . . . . Free ( univar, )
   911 *** . . . . . . . . . . . . . . Const ( List.list.Cons)
   912 *** . . . . . . . . . . . . . . . Free ( equation, )
   913 *** . . . . . . . . . . . . . . . Const ( List.list.Nil)
   914 *** . . . . . . . . . . . . . Const ( List.list.Cons)
   915 *** . . . . . . . . . . . . . . Free ( no_met, )
   916 *** . . . . . . . . . . . . . . Const ( List.list.Nil)
   917 *** . . . . . . . . . . . Const ( List.list.Cons)
   918 *** . . . . . . . . . . . . Const ( Script.BOOL)
   919 *** . . . . . . . . . . . . . Bound 2                   <----- Free ( e_1, )
   920 *** . . . . . . . . . . . . Const ( List.list.Cons)
   921 *** . . . . . . . . . . . . . Const ( Script.real_)
   922 *** . . . . . . . . . . . . . . Bound 0                 <----- Free ( v_1, )
   923 *** . . . . . . . . . . . . . Const ( List.list.Nil)
   924 *** . . . . . . . . . . Abs( s_1,..
   925 *** . . . . . . . . . . . Const ( Script.Substitute)
   926 *** . . . . . . . . . . . . Const ( List.list.Cons)
   927 *** . . . . . . . . . . . . . Const ( Pair)
   928 *** . . . . . . . . . . . . . . Bound 1                 <----- Free ( v_1, )
   929 *** . . . . . . . . . . . . . . Const ( Fun.op o)
   930 *** . . . . . . . . . . . . . . . Const ( Tools.rhs)
   931 *** . . . . . . . . . . . . . . . Const ( List.hd)
   932 *** . . . . . . . . . . . . . . . Bound 0               <----- Free ( s_1, )
   933 *** . . . . . . . . . . . . . Const ( List.list.Nil)
   934 *** . . . . . . . . . . . . Bound 4                     <----- Free ( h_, )
   935 
   936 > val ttt' = inst_abs thy ttt;
   937 > tracing(term2str ttt');
   938 Script Make_fun_by_explicit f_ v_ eqs_ =  
   939   ... as above ...
   940 > atomt ttt';
   941 *** -------------
   942 *** Const ( DiffApp.Make'_fun'_by'_explicit)
   943 *** . Free ( f_, )
   944 *** . Free ( v_, )
   945 *** . Free ( eqs_, )
   946 *** . Const ( Let)
   947 *** . . Const ( Fun.op o)
   948 *** . . . Const ( List.hd)
   949 *** . . . Const ( DiffApp.filterVar)
   950 *** . . . . Free ( f_, )
   951 *** . . . Free ( eqs_, )
   952 *** . . Abs( h_,..
   953 *** . . . Const ( Let)
   954 *** . . . . Const ( List.hd)
   955 *** . . . . . Const ( List.dropWhile)
   956 *** . . . . . . Const ( Atools.ident)
   957 *** . . . . . . . Free ( h_, )                <---- Bound 0
   958 *** . . . . . . Free ( eqs_, )
   959 *** . . . . Abs( e_1,..
   960 *** . . . . . Const ( Let)
   961 *** . . . . . . Const ( List.dropWhile)
   962 *** . . . . . . . Const ( Atools.ident)
   963 *** . . . . . . . . Free ( f_, )
   964 *** . . . . . . . Const ( Tools.Vars)
   965 *** . . . . . . . . Free ( h_, )                  <---- Bound 1
   966 *** . . . . . . Abs( vs_,..
   967 *** . . . . . . . Const ( Let)
   968 *** . . . . . . . . Const ( List.hd)
   969 *** . . . . . . . . . Const ( List.dropWhile)
   970 *** . . . . . . . . . . Const ( Atools.ident)
   971 *** . . . . . . . . . . . Free ( v_, )
   972 *** . . . . . . . . . . Free ( vs_, )             <---- Bound 0
   973 *** . . . . . . . . Abs( v_1,..
   974 *** . . . . . . . . . Const ( Let)
   975 *** . . . . . . . . . . Const ( Script.SubProblem)
   976 *** . . . . . . . . . . . Const ( Pair)
   977 *** . . . . . . . . . . . . Free ( DiffApp_, )
   978 *** . . . . . . . . . . . . Const ( Pair)
   979 *** . . . . . . . . . . . . . Const ( List.list.Cons)
   980 *** . . . . . . . . . . . . . . Free ( univar, )
   981 *** . . . . . . . . . . . . . . Const ( List.list.Cons)
   982 *** . . . . . . . . . . . . . . . Free ( equation, )
   983 *** . . . . . . . . . . . . . . . Const ( List.list.Nil)
   984 *** . . . . . . . . . . . . . Const ( List.list.Cons)
   985 *** . . . . . . . . . . . . . . Free ( no_met, )
   986 *** . . . . . . . . . . . . . . Const ( List.list.Nil)
   987 *** . . . . . . . . . . . Const ( List.list.Cons)
   988 *** . . . . . . . . . . . . Const ( Script.BOOL)
   989 *** . . . . . . . . . . . . . Free ( e_1, )             <----- Bound 2
   990 *** . . . . . . . . . . . . Const ( List.list.Cons)
   991 *** . . . . . . . . . . . . . Const ( Script.real_)
   992 *** . . . . . . . . . . . . . . Free ( v_1, )           <----- Bound 0
   993 *** . . . . . . . . . . . . . Const ( List.list.Nil)
   994 *** . . . . . . . . . . Abs( s_1,..
   995 *** . . . . . . . . . . . Const ( Script.Substitute)
   996 *** . . . . . . . . . . . . Const ( List.list.Cons)
   997 *** . . . . . . . . . . . . . Const ( Pair)
   998 *** . . . . . . . . . . . . . . Free ( v_1, )           <----- Bound 1
   999 *** . . . . . . . . . . . . . . Const ( Fun.op o)
  1000 *** . . . . . . . . . . . . . . . Const ( Tools.rhs)
  1001 *** . . . . . . . . . . . . . . . Const ( List.hd)
  1002 *** . . . . . . . . . . . . . . . Free ( s_1, )         <----- Bound 0
  1003 *** . . . . . . . . . . . . . Const ( List.list.Nil)
  1004 *** . . . . . . . . . . . . Free ( h_, )                <----- Bound 4
  1005 
  1006 Note numbering of de Bruijn indexes !
  1007 
  1008 Script Make_fun_by_explicit f_ v_ eqs_ =
  1009  let h_ = (hd o filterVar f_) eqs_; 
  1010      e_1 = hd (dropWhile (ident h_ BOUND_0) eqs_);
  1011      vs_ = dropWhile (ident f_) (Vars h_ BOUND_1);
  1012      v_1 = hd (dropWhile (ident v_) vs_ BOUND_0);
  1013      s_1 =
  1014        SubProblem (DiffApp_, [univar, equation], [no_met])
  1015         [BOOL e_1 BOUND_2, REAL v_1 BOUND_0]
  1016  in Substitute [(v_1 BOUND_1 = (rhs o hd) s_1 BOUND_0)] h_ BOUND_4
  1017 *)
  1018 
  1019 (* for parse and parse_patt: fix all types to real *)
  1020 fun T_a2real (Type (s, [])) = 
  1021     if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT 
  1022     else Type (s, [])
  1023   | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts)
  1024   | T_a2real (TFree (s, srt)) = 
  1025     if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT 
  1026     else TFree (s, srt)
  1027   | T_a2real (TVar ((s, i), srt)) = 
  1028     if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT 
  1029     else TVar ((s, i), srt)
  1030   | T_a2real (TVar (("DUMMY",_), srt)) = HOLogic.realT;
  1031 
  1032 (*FIXME .. fixes the type (+see Typefix.thy*)
  1033 fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T)) 
  1034   | typ_a2real (Free( s, T)) = (Free( s, T_a2real T))
  1035   | typ_a2real (Var( n, T)) = (Var( n, T_a2real T))
  1036   | typ_a2real (Bound i) = (Bound i)
  1037   | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t)
  1038   | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2);
  1039 
  1040 
  1041 (* before 2002 *)
  1042 fun parseold thy str = 
  1043   (let val t = ((*typ_a2real o*) numbers_to_string) 
  1044 		   (Syntax.read_term_global thy str)
  1045    in SOME (cterm_of thy t) end)
  1046     handle _ => NONE;
  1047 (*2002 fun parseN thy str = 
  1048   (let 
  1049      val sgn = sign_of thy;
  1050      val t = ((*typ_a2real o app_num_tr'1 o*) term_of) 
  1051        (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
  1052    in SOME (cterm_of sgn t) end)
  1053      handle _ => NONE;*)
  1054 fun parseN thy str = 
  1055   (let val t = (*(typ_a2real o numbers_to_string)*) 
  1056 	   (Syntax.read_term_global thy str)
  1057    in SOME (cterm_of thy t) end)
  1058     handle _ => NONE;
  1059 (*2002 fun parse thy str = 
  1060   (let 
  1061      val sgn = sign_of thy;
  1062      val t = (typ_a2real o app_num_tr'1 o term_of) 
  1063        (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
  1064    in SOME (cterm_of sgn t) end) (*FIXXXXME 10.8.02: return term !!!*)
  1065      handle _ => NONE;*)
  1066 (*2010 fun parse thy str = 
  1067   (let val t = (typ_a2real o app_num_tr'1) (Syntax.read_term_global thy str)
  1068    in SOME (cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*)
  1069      handle _ => NONE;*)
  1070 fun parse thy str = 
  1071   (let val t = (typ_a2real o numbers_to_string) 
  1072 		   (Syntax.read_term_global thy str)
  1073    in SOME (cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*)
  1074      handle _ => NONE;
  1075 (*
  1076 > val (SOME ct) = parse thy "(-#5)^^^#3"; 
  1077 > atomty (term_of ct);
  1078 *** -------------
  1079 *** Const ( Nat.op ^, ['a, nat] => 'a)
  1080 ***   Const ( uminus, 'a => 'a)
  1081 ***     Free ( #5, 'a)
  1082 ***   Free ( #3, nat)                
  1083 > val (SOME ct) = parse thy "R=R"; 
  1084 > atomty (term_of ct);
  1085 *** -------------
  1086 *** Const ( op =, [real, real] => bool)
  1087 ***   Free ( R, real)
  1088 ***   Free ( R, real)
  1089 
  1090 THIS IS THE OUTPUT FOR VERSION (3) above at typ_a2real !!!!!
  1091 *** -------------
  1092 *** Const ( op =, [RealDef.real, RealDef.real] => bool)
  1093 ***   Free ( R, RealDef.real)
  1094 ***   Free ( R, RealDef.real)                  *)
  1095 
  1096 (*WN110317 parseNEW will replace parse after introduction of ctxt completed*)
  1097 fun parseNEW ctxt str = SOME (Syntax.read_term ctxt str |> numbers_to_string)
  1098       handle _ => NONE;
  1099 
  1100 (* parse term patterns; Var ("v",_), i.e. "?v", are required for instantiation
  1101   WN130613 probably compare to 
  1102   http://www.mail-archive.com/isabelle-dev@mailbroy.informatik.tu-muenchen.de/msg04249.html*)
  1103 fun parse_patt thy str = (thy, str) |>> thy2ctxt 
  1104                                     |-> Proof_Context.read_term_pattern
  1105                                     |> numbers_to_string (*TODO drop*)
  1106                                     |> typ_a2real;       (*TODO drop*)
  1107 
  1108 (*version for testing local to theories*)
  1109 fun str2term_ thy str = (term_of o the o (parse thy)) str;
  1110 (*WN110520
  1111 fun str2term str = (term_of o the o (parse (Thy_Info.get_theory "Isac"))) str;*)
  1112 fun str2term str = parse_patt (Thy_Info.get_theory "Isac") str
  1113 fun strs2terms ss = map str2term ss;
  1114 fun str2termN str = (term_of o the o (parseN (Thy_Info.get_theory "Isac"))) str;
  1115 
  1116 (*+ makes a substitution from the output of Pattern.match +*)
  1117 (*fun mk_subs ((id, _):indexname, t:term) = (Free (id,type_of t), t);*)
  1118 fun mk_subs (subs: ((string * int) * (Term.typ * Term.term)) list) =
  1119 let fun mk_sub ((id, _), (ty, tm)) = (Free (id, ty), tm) in
  1120 map mk_sub subs end;
  1121 
  1122 val atomthm = atomt o #prop o rep_thm;
  1123 
  1124 (*.instantiate #prop thm with bound variables (as Free).*)
  1125 fun inst_bdv [] t = t : term
  1126   | inst_bdv (instl: (term*term) list) t =
  1127       let fun subst (v as Var((s,_),T)) = 
  1128 	      (case Symbol.explode s of
  1129 		   "b"::"d"::"v"::_ => 
  1130 		   if_none (assoc(instl,Free(s,T))) (Free(s,T))
  1131 		 | _ => v)
  1132             | subst (Abs(a,T,body)) = Abs(a, T, subst body)
  1133             | subst (f$t') = subst f $ subst t'
  1134             | subst t = if_none (assoc(instl,t)) t
  1135       in  subst t  end;
  1136 
  1137 
  1138 (*WN050829 caution: is_atom (str2term"q_0/2 * L * x") = true !!!
  1139   use length (vars term) = 1 instead*)
  1140 fun is_atom (Const ("Float.Float",_) $ _) = true
  1141   | is_atom (Const ("ComplexI.I'_'_",_)) = true
  1142   | is_atom (Const ("Groups.times_class.times",_) $ t $ Const ("ComplexI.I'_'_",_)) = is_atom t
  1143   | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ Const ("ComplexI.I'_'_",_)) = is_atom t1
  1144   | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ 
  1145 		   (Const ("Groups.times_class.times",_) $ t2 $ Const ("ComplexI.I'_'_",_))) = 
  1146     is_atom t1 andalso is_atom t2
  1147   | is_atom (Const _) = true
  1148   | is_atom (Free _) = true
  1149   | is_atom (Var _) = true
  1150   | is_atom _ = false;
  1151 (* val t = str2term "q_0/2 * L * x";
  1152 
  1153 
  1154 *)
  1155 (*val t = str2term "Float ((1,2),(0,0))";
  1156 > is_atom t;
  1157 val it = true : bool
  1158 > val t = str2term "Float ((1,2),(0,0)) * I__";
  1159 > is_atom t;
  1160 val it = true : bool
  1161 > val t = str2term "Float ((1,2),(0,0)) + Float ((3,4),(0,0)) * I__";
  1162 > is_atom t;
  1163 val it = true : bool
  1164 > val t = str2term "1 + 2*I__";
  1165 > val Const ("Groups.plus_class.plus",_) $ t1 $ (Const ("Groups.times_class.times",_) $ t2 $ Const ("ComplexI.I'_'_",_)) = t;
  1166 *)
  1167 
  1168 (*.adaption from Isabelle/src/Pure/term.ML; reports if ALL Free's
  1169    have found a substitution (required for evaluating the preconditions
  1170    of _incomplete_ models).*)
  1171 fun subst_atomic_all [] t = (false, (*TODO may be 'true' for some terms ?*)
  1172 			     t : term)
  1173   | subst_atomic_all (instl: (term*term) list) t =
  1174       let fun subst (Abs(a,T,body)) = 
  1175 	      let val (all, body') = subst body
  1176 	      in (all, Abs(a, T, body')) end
  1177             | subst (f$tt) = 
  1178 	      let val (all1, f') = subst f
  1179 		  val (all2, tt') = subst tt
  1180 	      in (all1 andalso all2, f' $ tt') end
  1181             | subst (t as Free _) = 
  1182 	      if is_num t then (true, t) (*numerals cannot be subst*)
  1183 	      else (case assoc(instl,t) of
  1184 					 SOME t' => (true, t')
  1185 				       | NONE => (false, t))
  1186             | subst t = (true, if_none (assoc(instl,t)) t)
  1187       in  subst t  end;
  1188 
  1189 (*.add two terms with a type given.*)
  1190 fun mk_add t1 t2 =
  1191     let val T1 = type_of t1
  1192 	val T2 = type_of t2
  1193     in if T1 <> T2 then raise TYPE ("mk_add gets ",[T1, T2],[t1,t2])
  1194        else (Const ("Groups.plus_class.plus", [T1, T2] ---> T1) $ t1 $ t2)
  1195     end;