src/Tools/isac/ProgLang/termC.sml
author Walther Neuper <neuper@ist.tugraz.at>
Mon, 20 Feb 2012 18:18:03 +0100
changeset 42376 9e59542132b3
parent 42360 2c8de368c64c
child 42426 fc042a668d7a
permissions -rw-r--r--
Jan finished his work

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