src/Tools/isac/ProgLang/termC.sml
author Walther Neuper <neuper@ist.tugraz.at>
Tue, 28 Sep 2010 07:28:10 +0200
branchisac-update-Isa09-2
changeset 38025 67a110289e4e
child 38031 460c24a6a6ba
permissions -rw-r--r--
repaired fun uminus_to_string, fun rewrite_terms_

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