src/Tools/isac/Scripts/term_G.sml
author Walther Neuper <neuper@ist.tugraz.at>
Fri, 20 Aug 2010 12:25:37 +0200
branchisac-update-Isa09-2
changeset 37934 56f10b13005e
parent 37931 2d12beb7f983
child 37935 27d365c3dd31
permissions -rw-r--r--
finished update ME/calchead.sml + pushed updates over all sml+test

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