src/Tools/isac/ProgLang/termC.sml
author Walther Neuper <neuper@ist.tugraz.at>
Tue, 28 Sep 2010 09:06:56 +0200
branchisac-update-Isa09-2
changeset 38031 460c24a6a6ba
parent 38025 67a110289e4e
child 38034 928cebc9c4aa
permissions -rw-r--r--
tuned error and writeln

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