src/Tools/isac/ProgLang/term.sml
branchisac-update-Isa09-2
changeset 37947 22235e4dbe5f
parent 37938 f6164be9280d
child 37976 98868effcfc8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Tools/isac/ProgLang/term.sml	Wed Aug 25 16:20:07 2010 +0200
     1.3 @@ -0,0 +1,1343 @@
     1.4 +(* extends Isabelle/src/Pure/term.ML
     1.5 +   (c) Walther Neuper 1999
     1.6 +
     1.7 +use"ProgLang/term.sml";
     1.8 +use"term.sml";
     1.9 +*)
    1.10 +
    1.11 +(*
    1.12 +> (cterm_of thy) a_term;
    1.13 +val it = "empty" : cterm        *)
    1.14 +
    1.15 +(*2003 fun match thy t pat =
    1.16 +    (snd (Pattern.match (Sign.tsig_of (sign_of thy)) (pat, t)))
    1.17 +    handle _ => [];
    1.18 +fn : theory ->
    1.19 +     Term.term -> Term.term -> (Term.indexname * Term.term) list*)
    1.20 +(*see src/Tools/eqsubst.ML fun clean_match*)
    1.21 +(*2003 fun matches thy tm pa = if match thy tm pa = [] then false else true;*)
    1.22 +fun matches thy tm pa = 
    1.23 +    (Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true)
    1.24 +    handle _ => false
    1.25 +
    1.26 +fun atomtyp t = (*see raw_pp_typ*)
    1.27 +  let
    1.28 +    fun ato n (Type (s,[])) = 
    1.29 +      ("\n*** "^indent n^"Type ("^s^",[])")
    1.30 +      | ato n (Type (s,Ts)) =
    1.31 +      ("\n*** "^indent n^"Type ("^s^",["^ atol (n+1) Ts)
    1.32 +
    1.33 +      | ato n (TFree (s,sort)) =
    1.34 +      ("\n*** "^indent n^"TFree ("^s^",["^ strs2str' sort)
    1.35 +
    1.36 +      | ato n (TVar ((s,i),sort)) =
    1.37 +      ("\n*** "^indent n^"TVar (("^s^","^ 
    1.38 +       string_of_int i ^ strs2str' sort)
    1.39 +    and atol n [] = 
    1.40 +      ("\n*** "^indent n^"]")
    1.41 +      | atol n (T::Ts) = (ato n T ^ atol n Ts)
    1.42 +(*in print (ato 0 t ^ "\n") end;  TODO TUM10*)
    1.43 +in writeln(ato 0 t) end;
    1.44 +
    1.45 +(*Prog.Tutorial.p.34*)
    1.46 +local
    1.47 +   fun pp_pair (x, y) = Pretty.list "(" ")" [x, y]
    1.48 +   fun pp_list xs = Pretty.list "[" "]" xs
    1.49 +   fun pp_str s   = Pretty.str s
    1.50 +   fun pp_qstr s = Pretty.quote (pp_str s)
    1.51 +   fun pp_int i   = pp_str (string_of_int i)
    1.52 +   fun pp_sort S = pp_list (map pp_qstr S)
    1.53 +   fun pp_constr a args = Pretty.block [pp_str a, Pretty.brk 1, args]
    1.54 +in
    1.55 +fun raw_pp_typ (TVar ((a, i), S)) =
    1.56 +       pp_constr "TVar" (pp_pair (pp_pair (pp_qstr a, pp_int i), pp_sort S))
    1.57 +   | raw_pp_typ (TFree (a, S)) =
    1.58 +       pp_constr "TFree" (pp_pair (pp_qstr a, pp_sort S))
    1.59 +   | raw_pp_typ (Type (a, tys)) =
    1.60 +       pp_constr "Type" (pp_pair (pp_qstr a, pp_list (map raw_pp_typ tys)))
    1.61 +end
    1.62 +(* install
    1.63 +PolyML.addPrettyPrinter
    1.64 +  (fn _ => fn _ => ml_pretty o Pretty.to_ML o raw_pp_typ);
    1.65 +de-install
    1.66 +PolyML.addPrettyPrinter
    1.67 +  (fn _ => fn _ => ml_pretty o Pretty.to_ML o Proof_Display.pp_typ Pure.thy);
    1.68 +*)
    1.69 +
    1.70 +(*
    1.71 +> val T = (type_of o term_of o the o (parse thy)) "a::[real,int] => nat";
    1.72 +> atomtyp T;
    1.73 +*** Type (fun,[
    1.74 +***   Type (RealDef.real,[])
    1.75 +***   Type (fun,[
    1.76 +***     Type (IntDef.int,[])
    1.77 +***     Type (nat,[])
    1.78 +***     ]
    1.79 +***   ]
    1.80 +*)
    1.81 +
    1.82 +fun atomt t =
    1.83 +    let fun ato (Const(a,T))     n = 
    1.84 +	("\n*** "^indent n^"Const ("^a^")")
    1.85 +	  | ato (Free (a,T))     n =  
    1.86 +	("\n*** "^indent n^"Free ("^a^", "^")")
    1.87 +	  | ato (Var ((a,ix),T)) n =
    1.88 +	("\n*** "^indent n^"Var (("^a^", "^(string_of_int ix)^"), "^")")
    1.89 +	  | ato (Bound ix)       n = 
    1.90 +	("\n*** "^indent n^"Bound "^(string_of_int ix))
    1.91 +	  | ato (Abs(a,T,body))  n = 
    1.92 +	("\n*** "^indent n^"Abs("^a^",..")^ato body (n+1)
    1.93 +	  | ato (f$t')           n = (ato f n; ato t' (n+1))
    1.94 +    in writeln("\n*** -------------"^ ato t 0 ^"\n***") end;
    1.95 +
    1.96 +fun term_detail2str t =
    1.97 +    let fun ato (Const (a, T))     n = 
    1.98 +	    "\n*** "^indent n^"Const ("^a^", "^string_of_typ T^")"
    1.99 +	  | ato (Free (a, T))     n =  
   1.100 +	    "\n*** "^indent n^"Free ("^a^", "^string_of_typ T^")"
   1.101 +	  | ato (Var ((a, ix), T)) n =
   1.102 +	    "\n*** "^indent n^"Var (("^a^", "^string_of_int ix^"), "^
   1.103 +	    string_of_typ T^")"
   1.104 +	  | ato (Bound ix)       n = 
   1.105 +	    "\n*** "^indent n^"Bound "^string_of_int ix
   1.106 +	  | ato (Abs(a, T, body))  n = 
   1.107 +	    "\n*** "^indent n^"Abs ("^a^", "^
   1.108 +	       (string_of_typ T)^",.."
   1.109 +	    ^ato body (n + 1)
   1.110 +	  | ato (f $ t')           n = ato f n^ato t' (n+1)
   1.111 +    in "\n*** "^ato t 0^"\n***" end;
   1.112 +fun atomty t = (writeln o term_detail2str) t;
   1.113 +
   1.114 +fun term_str thy (Const(s,_)) = s
   1.115 +  | term_str thy (Free(s,_)) = s
   1.116 +  | term_str thy (Var((s,i),_)) = s^(string_of_int i)
   1.117 +  | term_str thy (Bound i) = "B."^(string_of_int i)
   1.118 +  | term_str thy (Abs(s,_,_)) = s
   1.119 +  | term_str thy t = raise error("term_str not for "^term2str t);
   1.120 +
   1.121 +(*.contains the fst argument the second argument (a leave! of term).*)
   1.122 +fun contains_term (Abs(_,_,body)) t = contains_term body t 
   1.123 +  | contains_term (f $ f') t = 
   1.124 +    contains_term f t orelse contains_term f' t
   1.125 +  | contains_term s t = t = s;
   1.126 +(*.contains the term a VAR(("*",_),_) ?.*)
   1.127 +fun contains_Var (Abs(_,_,body)) = contains_Var body
   1.128 +  | contains_Var (f $ f') = contains_Var f orelse contains_Var f'
   1.129 +  | contains_Var (Var _) = true
   1.130 +  | contains_Var _ = false;
   1.131 +(* contains_Var (str2term "?z = 3") (*true*);
   1.132 +   contains_Var (str2term "z = 3")  (*false*);
   1.133 +   *)
   1.134 +
   1.135 +(*fun int_of_str str =
   1.136 +    let val ss = explode str
   1.137 +	val str' = case ss of
   1.138 +	   "("::s => drop_last s | _ => ss
   1.139 +    in case BasisLibrary.Int.fromString (implode str') of
   1.140 +	     SOME i => SOME i
   1.141 +	   | NONE => NONE end;*)
   1.142 +fun int_of_str str =
   1.143 +    let val ss = explode str
   1.144 +	val str' = case ss of
   1.145 +	   "("::s => drop_last s | _ => ss
   1.146 +    in (SOME (Thy_Output.integer (implode str'))) handle _ => NONE end;
   1.147 +(*
   1.148 +> int_of_str "123";
   1.149 +val it = SOME 123 : int option
   1.150 +> int_of_str "(-123)";
   1.151 +val it = SOME 123 : int option
   1.152 +> int_of_str "#123";
   1.153 +val it = NONE : int option
   1.154 +> int_of_str "-123";
   1.155 +val it = SOME ~123 : int option
   1.156 +*)
   1.157 +fun int_of_str' str = 
   1.158 +    case int_of_str str of
   1.159 +	SOME i => i
   1.160 +      | NONE => raise TERM ("int_of_string: no int-string",[]);
   1.161 +val str2int = int_of_str';
   1.162 +    
   1.163 +fun is_numeral str = case int_of_str str of
   1.164 +			 SOME _ => true
   1.165 +		       | NONE => false;
   1.166 +val is_no = is_numeral;
   1.167 +fun is_num (Free (s,_)) = if is_numeral s then true else false
   1.168 +  | is_num _ = false;
   1.169 +(*>
   1.170 +> is_num ((term_of o the o (parse thy)) "#1");
   1.171 +val it = true : bool
   1.172 +> is_num ((term_of o the o (parse thy)) "#-1");
   1.173 +val it = true : bool
   1.174 +> is_num ((term_of o the o (parse thy)) "a123");
   1.175 +val it = false : bool
   1.176 +*)
   1.177 +
   1.178 +(*fun int_of_Free (Free (intstr, _)) =
   1.179 +    (case BasisLibrary.Int.fromString intstr of
   1.180 +	     SOME i => i
   1.181 +	   | NONE => raise error ("int_of_Free ( "^ intstr ^", _)"))
   1.182 +  | int_of_Free t = raise error ("int_of_Free ( "^ term2str t ^" )");*)
   1.183 +fun int_of_Free (Free (intstr, _)) = (Thy_Output.integer intstr
   1.184 +    handle _ => raise error ("int_of_Free ( "^ intstr ^", _)"))
   1.185 +  | int_of_Free t = raise error ("int_of_Free ( "^ term2str t ^" )");
   1.186 +
   1.187 +fun vars t =
   1.188 +  let
   1.189 +    fun scan vs (Const(s,T)) = vs
   1.190 +      | scan vs (t as Free(s,T)) = if is_no s then vs else t::vs
   1.191 +      | scan vs (t as Var((s,i),T)) = t::vs
   1.192 +      | scan vs (Bound i) = vs 
   1.193 +      | scan vs (Abs(s,T,t)) = scan vs t
   1.194 +      | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
   1.195 +  in (distinct o (scan [])) t end;
   1.196 +
   1.197 +fun is_Free (Free _) = true
   1.198 +  | is_Free _ = false;
   1.199 +fun is_fun_id (Const _) = true
   1.200 +  | is_fun_id (Free _) = true
   1.201 +  | is_fun_id _ = false;
   1.202 +fun is_f_x (f $ x) = is_fun_id f andalso is_Free x
   1.203 +  | is_f_x _ = false;
   1.204 +(* is_f_x (str2term "q_0/2 * L * x") (*false*);
   1.205 +   is_f_x (str2term "M_b x") (*true*);
   1.206 +  *)
   1.207 +fun vars_str t =
   1.208 +  let
   1.209 +    fun scan vs (Const(s,T)) = vs
   1.210 +      | scan vs (t as Free(s,T)) = if is_no s then vs else s::vs
   1.211 +      | scan vs (t as Var((s,i),T)) = (s^"_"^(string_of_int i))::vs
   1.212 +      | scan vs (Bound i) = vs 
   1.213 +      | scan vs (Abs(s,T,t)) = scan vs t
   1.214 +      | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
   1.215 +  in (distinct o (scan [])) t end;
   1.216 +
   1.217 +fun ids2str t =
   1.218 +  let
   1.219 +    fun scan vs (Const(s,T)) = if is_no s then vs else s::vs
   1.220 +      | scan vs (t as Free(s,T)) = if is_no s then vs else s::vs
   1.221 +      | scan vs (t as Var((s,i),T)) = (s^"_"^(string_of_int i))::vs
   1.222 +      | scan vs (Bound i) = vs 
   1.223 +      | scan vs (Abs(s,T,t)) = scan (s::vs) t
   1.224 +      | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
   1.225 +  in (distinct o (scan [])) t end;
   1.226 +fun is_bdv str =
   1.227 +    case explode str of
   1.228 +	"b"::"d"::"v"::_ => true
   1.229 +      | _ => false;
   1.230 +fun is_bdv_ (Free (s,_)) = is_bdv s
   1.231 +  | is_bdv_ _ = false;
   1.232 +
   1.233 +fun free2str (Free (s,_)) = s
   1.234 +  | free2str t = raise error ("free2str not for "^ term2str t);
   1.235 +fun free2int (t as Free (s, _)) = ((str2int s)
   1.236 +    handle _ => raise error ("free2int: "^term_detail2str t))
   1.237 +  | free2int t = raise error ("free2int: "^term_detail2str t);
   1.238 +
   1.239 +(*27.8.01: unused*)
   1.240 +fun var2free (t as Const(s,T)) = t
   1.241 +  | var2free (t as Free(s,T)) = t
   1.242 +  | var2free (Var((s,i),T)) = Free(s,T)
   1.243 +  | var2free (t as Bound i) = t 
   1.244 +  | var2free (Abs(s,T,t)) = Abs(s,T,var2free t)
   1.245 +  | var2free (t1 $ t2) = (var2free t1) $ (var2free t2);
   1.246 +  
   1.247 +(*27.8.01: doesn't find some subterm ???!???*)
   1.248 +(*2010 Logic.varify !!!*)
   1.249 +fun free2var (t as Const(s,T)) = t
   1.250 +  | free2var (t as Free(s,T)) = if is_no s then t else Var((s,0),T)
   1.251 +  | free2var (t as Var((s,i),T)) = t
   1.252 +  | free2var (t as Bound i) = t 
   1.253 +  | free2var (Abs(s,T,t)) = Abs(s,T,free2var t)
   1.254 +  | free2var (t1 $ t2) = (free2var t1) $ (free2var t2);
   1.255 +  
   1.256 +
   1.257 +fun mk_listT T = Type ("List.list", [T]);
   1.258 +fun list_const T = 
   1.259 +  Const("List.list.Cons", [T, mk_listT T] ---> mk_listT T);
   1.260 +(*28.8.01: TODO: get type from head of list: 1 arg less!!!*)
   1.261 +fun list2isalist T [] = Const("List.list.Nil",mk_listT T)
   1.262 +  | list2isalist T (t::ts) = (list_const T) $ t $ (list2isalist T ts);
   1.263 +(*
   1.264 +> val tt = (term_of o the o (parse thy)) "R=(R::real)";
   1.265 +> val TT = type_of tt;
   1.266 +> val ss = list2isalist TT [tt,tt,tt];
   1.267 +> (cterm_of thy) ss;
   1.268 +val it = "[R = R, R = R, R = R]" : cterm  *)
   1.269 +
   1.270 +fun isapair2pair (Const ("Pair",_) $ a $ b) = (a,b)
   1.271 +  | isapair2pair t = 
   1.272 +    raise error ("isapair2pair called with "^term2str t);
   1.273 +
   1.274 +val listType = Type ("List.list",[Type ("bool",[])]);
   1.275 +fun isalist2list ls =
   1.276 +  let
   1.277 +    fun get es (Const("List.list.Cons",_) $ t $ ls) = get (t::es) ls
   1.278 +      | get es (Const("List.list.Nil",_)) = es
   1.279 +      | get _ t = 
   1.280 +	raise error ("isalist2list applied to NON-list '"^term2str t^"'")
   1.281 +  in (rev o (get [])) ls end;
   1.282 +(*      
   1.283 +> val il = str2term "[a=b,c=d,e=f]";
   1.284 +> val l = isalist2list il;
   1.285 +> (writeln o terms2str) l;
   1.286 +["a = b","c = d","e = f"]
   1.287 +
   1.288 +> val il = str2term "ss___::bool list";
   1.289 +> val l = isalist2list il;
   1.290 +[Free ("ss___", "bool List.list")]
   1.291 +*)
   1.292 +
   1.293 +
   1.294 +(*review Isabelle2009/src/HOL/Tools/hologic.ML*)
   1.295 +val prop = Type ("prop",[]);     (* ~/Diss.99/Integers-Isa/tools.sml*)
   1.296 +val bool = Type ("bool",[]);     (* 2002 Integ.int *)
   1.297 +val Trueprop = Const("Trueprop",bool-->prop);
   1.298 +fun mk_prop t = Trueprop $ t;
   1.299 +val true_as_term = Const("True",bool);
   1.300 +val false_as_term = Const("False",bool);
   1.301 +val true_as_cterm = cterm_of (theory "HOL") true_as_term;
   1.302 +val false_as_cterm = cterm_of (theory "HOL") false_as_term;
   1.303 +
   1.304 +infixr 5 -->;                    (*2002 /Pure/term.ML *)
   1.305 +infixr --->;			 (*2002 /Pure/term.ML *)
   1.306 +fun S --> T = Type("fun",[S,T]); (*2002 /Pure/term.ML *)
   1.307 +val op ---> = foldr (op -->);    (*2002 /Pure/term.ML *)
   1.308 +fun list_implies ([], B) = B : term (*2002 /term.ML *)
   1.309 +  | list_implies (A::AS, B) = Logic.implies $ A $ list_implies(AS,B);
   1.310 +
   1.311 +
   1.312 +
   1.313 +(** substitution **)
   1.314 +
   1.315 +fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) =      (* = thm.ML *)
   1.316 +      match_bvs(s, t, if x="" orelse y="" then al
   1.317 +                                          else (x,y)::al)
   1.318 +  | match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al))
   1.319 +  | match_bvs(_,_,al) = al;
   1.320 +fun ren_inst(insts,prop,pat,obj) =              (* = thm.ML *)
   1.321 +  let val ren = match_bvs(pat,obj,[])
   1.322 +      fun renAbs(Abs(x,T,b)) =
   1.323 +            Abs(case assoc_string(ren,x) of NONE => x 
   1.324 +	  | SOME(y) => y, T, renAbs(b))
   1.325 +        | renAbs(f$t) = renAbs(f) $ renAbs(t)
   1.326 +        | renAbs(t) = t
   1.327 +  in subst_vars insts (if null(ren) then prop else renAbs(prop)) end;
   1.328 +
   1.329 +
   1.330 +
   1.331 +
   1.332 +
   1.333 +
   1.334 +fun dest_equals' (Const("op =",_) $ t $ u)  =  (t,u)(* logic.ML: Const("=="*)
   1.335 +  | dest_equals' t = raise TERM("dest_equals'", [t]);
   1.336 +val lhs_ = (fst o dest_equals');
   1.337 +val rhs_ = (snd o dest_equals');
   1.338 +
   1.339 +fun is_equality (Const("op =",_) $ t $ u)  =  true  (* logic.ML: Const("=="*)
   1.340 +  | is_equality _ = false;
   1.341 +fun mk_equality (t,u) = (Const("op =",[type_of t,type_of u]--->bool) $ t $ u); 
   1.342 +fun is_expliceq (Const("op =",_) $ (Free _) $ u)  =  true
   1.343 +  | is_expliceq _ = false;
   1.344 +fun strip_trueprop (Const("Trueprop",_) $ t) = t
   1.345 +  | strip_trueprop t = t;
   1.346 +(*  | strip_trueprop t = raise TERM("strip_trueprop", [t]);
   1.347 +*)
   1.348 +
   1.349 +(*.(A1==>...An==>B) goes to (A1==>...An==>).*)
   1.350 +fun strip_imp_prems' (Const("==>", T) $ A $ t) = 
   1.351 +    let fun coll_prems As (Const("==>", _) $ A $ t) = 
   1.352 +	    coll_prems (As $ (Logic.implies $ A)) t
   1.353 +	  | coll_prems As _ = SOME As
   1.354 +    in coll_prems (Logic.implies $ A) t end
   1.355 +  | strip_imp_prems' _ = NONE;  (* logic.ML: term -> term list*)
   1.356 +(*
   1.357 +  val thm = real_mult_div_cancel1;
   1.358 +  val prop = (#prop o rep_thm) thm;
   1.359 +  atomt prop;
   1.360 +*** -------------
   1.361 +*** Const ( ==>)
   1.362 +*** . Const ( Trueprop)
   1.363 +*** . . Const ( Not)
   1.364 +*** . . . Const ( op =)
   1.365 +*** . . . . Var ((k, 0), )
   1.366 +*** . . . . Const ( 0)
   1.367 +*** . Const ( Trueprop)
   1.368 +*** . . Const ( op =)                                                          *** .............
   1.369 +  val SOME t = strip_imp_prems' ((#prop o rep_thm) thm);
   1.370 +  atomt t;
   1.371 +*** -------------
   1.372 +*** Const ( ==>)
   1.373 +*** . Const ( Trueprop)
   1.374 +*** . . Const ( Not)
   1.375 +*** . . . Const ( op =)
   1.376 +*** . . . . Var ((k, 0), )
   1.377 +*** . . . . Const ( 0)
   1.378 +
   1.379 +  val thm = real_le_anti_sym;
   1.380 +  val prop = (#prop o rep_thm) thm;
   1.381 +  atomt prop;
   1.382 +*** -------------
   1.383 +*** Const ( ==>)
   1.384 +*** . Const ( Trueprop)
   1.385 +*** . . Const ( op <=)
   1.386 +*** . . . Var ((z, 0), )
   1.387 +*** . . . Var ((w, 0), )
   1.388 +*** . Const ( ==>)
   1.389 +*** . . Const ( Trueprop)
   1.390 +*** . . . Const ( op <=)
   1.391 +*** . . . . Var ((w, 0), )
   1.392 +*** . . . . Var ((z, 0), )
   1.393 +*** . . Const ( Trueprop)
   1.394 +*** . . . Const ( op =)
   1.395 +*** .............
   1.396 +  val SOME t = strip_imp_prems' ((#prop o rep_thm) thm);
   1.397 +  atomt t;
   1.398 +*** -------------
   1.399 +*** Const ( ==>)
   1.400 +*** . Const ( Trueprop)
   1.401 +*** . . Const ( op <=)
   1.402 +*** . . . Var ((z, 0), )
   1.403 +*** . . . Var ((w, 0), )
   1.404 +*** . Const ( ==>)
   1.405 +*** . . Const ( Trueprop)
   1.406 +*** . . . Const ( op <=)
   1.407 +*** . . . . Var ((w, 0), )
   1.408 +*** . . . . Var ((z, 0), )
   1.409 +*)
   1.410 +
   1.411 +(*. (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch.*)
   1.412 +fun ins_concl (Const("==>", T) $ A $ t) B = Logic.implies $ A $ (ins_concl t B)
   1.413 +  | ins_concl (Const("==>", T) $ A    ) B = Logic.implies $ A $ B
   1.414 +  | ins_concl t B =  raise TERM("ins_concl", [t, B]);
   1.415 +(*
   1.416 +  val thm = real_le_anti_sym;
   1.417 +  val prop = (#prop o rep_thm) thm;
   1.418 +  val concl = Logic.strip_imp_concl prop;
   1.419 +  val SOME prems = strip_imp_prems' prop;
   1.420 +  val prop' = ins_concl prems concl;
   1.421 +  prop = prop';
   1.422 +  atomt prop;
   1.423 +  atomt prop';
   1.424 +*)
   1.425 +
   1.426 +
   1.427 +fun vperm (Var _, Var _) = true  (*2002 Pure/thm.ML *)
   1.428 +  | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
   1.429 +  | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
   1.430 +  | vperm (t, u) = (t = u);
   1.431 +
   1.432 +(*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*)
   1.433 +fun mem_term (_, []) = false
   1.434 +  | mem_term (t, t'::ts) = t aconv t' orelse mem_term(t,ts);
   1.435 +fun subset_term ([], ys) = true
   1.436 +  | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term(xs, ys);
   1.437 +fun eq_set_term (xs, ys) =
   1.438 +    xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
   1.439 +(*a total, irreflexive ordering on index names*)
   1.440 +fun xless ((a,i), (b,j): indexname) = i<j  orelse  (i=j andalso a<b);
   1.441 +(*a partial ordering (not reflexive) for atomic terms*)
   1.442 +fun atless (Const (a,_), Const (b,_))  =  a<b
   1.443 +  | atless (Free (a,_), Free (b,_)) =  a<b
   1.444 +  | atless (Var(v,_), Var(w,_))  =  xless(v,w)
   1.445 +  | atless (Bound i, Bound j)  =   i<j
   1.446 +  | atless _  =  false;
   1.447 +(*insert atomic term into partially sorted list, suppressing duplicates (?)*)
   1.448 +fun insert_aterm (t,us) =
   1.449 +  let fun inserta [] = [t]
   1.450 +        | inserta (us as u::us') =
   1.451 +              if atless(t,u) then t::us
   1.452 +              else if t=u then us (*duplicate*)
   1.453 +              else u :: inserta(us')
   1.454 +  in  inserta us  end;
   1.455 +
   1.456 +(*Accumulates the Vars in the term, suppressing duplicates*)
   1.457 +fun add_term_vars (t, vars: term list) = case t of
   1.458 +    Var   _ => insert_aterm(t,vars)
   1.459 +  | Abs (_,_,body) => add_term_vars(body,vars)
   1.460 +  | f$t =>  add_term_vars (f, add_term_vars(t, vars))
   1.461 +  | _ => vars;
   1.462 +fun term_vars t = add_term_vars(t,[]);
   1.463 +
   1.464 +
   1.465 +fun var_perm (t, u) = (*2002 Pure/thm.ML *)
   1.466 +  vperm (t, u) andalso eq_set_term (term_vars t, term_vars u);
   1.467 +    
   1.468 +(*2002 fun decomp_simp, Pure/thm.ML *)
   1.469 +fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs)
   1.470 +    andalso not (is_Var lhs);
   1.471 +
   1.472 +
   1.473 +fun str_of_int n = 
   1.474 +  if n < 0 then "-"^((string_of_int o abs) n)
   1.475 +  else string_of_int n;
   1.476 +(*
   1.477 +> str_of_int 1;
   1.478 +val it = "1" : string                                                          > str_of_int ~1;
   1.479 +val it = "-1" : string
   1.480 +*)
   1.481 +
   1.482 +
   1.483 +fun power b 0 = 1
   1.484 +  | power b n = 
   1.485 +  if n>0 then b*(power b (n-1))
   1.486 +  else raise error ("power "^(str_of_int b)^" "^(str_of_int n));
   1.487 +(*
   1.488 +> power 2 3;
   1.489 +val it = 8 : int
   1.490 +> power ~2 3;
   1.491 +val it = ~8 : int
   1.492 +> power ~3 2;
   1.493 +val it = 9 : int
   1.494 +> power 3 ~2;
   1.495 +*)
   1.496 +fun gcd 0 b = b
   1.497 +  | gcd a b = if a < b then gcd (b mod a) a
   1.498 +	      else gcd (a mod b) b;
   1.499 +fun sign n = if n < 0 then ~1
   1.500 +	     else if n = 0 then 0 else 1;
   1.501 +fun sign2 n1 n2 = (sign n1) * (sign n2);
   1.502 +
   1.503 +infix dvd;
   1.504 +fun d dvd n = n mod d = 0;
   1.505 +
   1.506 +fun divisors n =
   1.507 +  let fun pdiv ds d n = 
   1.508 +    if d=n then d::ds
   1.509 +    else if d dvd n then pdiv (d::ds) d (n div d)
   1.510 +	 else pdiv ds (d+1) n
   1.511 +  in pdiv [] 2 n end;
   1.512 +
   1.513 +divisors 30;
   1.514 +divisors 32;
   1.515 +divisors 60;
   1.516 +divisors 11;
   1.517 +
   1.518 +fun doubles ds = (* ds is ordered *)
   1.519 +  let fun dbls ds [] = ds
   1.520 +	| dbls ds [i] = ds
   1.521 +	| dbls ds (i::i'::is) = if i=i' then dbls (i::ds) is
   1.522 +				else dbls ds (i'::is)
   1.523 +  in dbls [] ds end;
   1.524 +(*> doubles [2,3,4];
   1.525 +val it = [] : int list
   1.526 +> doubles [2,3,3,5,5,7];
   1.527 +val it = [5,3] : int list*)
   1.528 +
   1.529 +fun squfact 0 = 0
   1.530 +  | squfact 1 = 1
   1.531 +  | squfact n = foldl op* (1, (doubles o divisors) n);
   1.532 +(*> squfact 30;
   1.533 +val it = 1 : int
   1.534 +> squfact 32;
   1.535 +val it = 4 : int
   1.536 +> squfact 60;
   1.537 +val it = 2 : int
   1.538 +> squfact 11;
   1.539 +val it = 1 : int*)
   1.540 +
   1.541 +
   1.542 +fun dest_type (Type(T,[])) = T
   1.543 +  | dest_type T = 
   1.544 +    (atomtyp T;
   1.545 +     raise error ("... dest_type: not impl. for this type"));
   1.546 +
   1.547 +fun term_of_num ntyp n = Free (str_of_int n, ntyp);
   1.548 +
   1.549 +fun pairT T1 T2 = Type ("*", [T1, T2]);
   1.550 +(*> val t = str2term "(1,2)";
   1.551 +> type_of t = pairT HOLogic.realT HOLogic.realT;
   1.552 +val it = true : bool
   1.553 +*)
   1.554 +fun PairT T1 T2 = ([T1, T2] ---> Type ("*", [T1, T2]));
   1.555 +(*> val t = str2term "(1,2)";
   1.556 +> val Const ("Pair",pT) $ _ $ _ = t;
   1.557 +> pT = PairT HOLogic.realT HOLogic.realT;
   1.558 +val it = true : bool
   1.559 +*)
   1.560 +fun pairt t1 t2 =
   1.561 +    Const ("Pair", PairT (type_of t1) (type_of t2)) $ t1 $ t2;
   1.562 +(*> val t = str2term "(1,2)";
   1.563 +> val (t1, t2) = (str2term "1", str2term "2");
   1.564 +> t = pairt t1 t2;
   1.565 +val it = true : bool*)
   1.566 +
   1.567 +
   1.568 +fun num_of_term (t as Free (s,_)) = 
   1.569 +    (case int_of_str s of
   1.570 +	 SOME s' => s'
   1.571 +       | NONE => raise error ("num_of_term not for "^ term2str t))
   1.572 +  | num_of_term t = raise error ("num_of_term not for "^term2str t);
   1.573 +
   1.574 +fun mk_factroot op_(*=thy.sqrt*) T fact root = 
   1.575 +  Const ("op *", [T, T] ---> T) $ (term_of_num T fact) $
   1.576 +  (Const (op_, T --> T) $ term_of_num T root);
   1.577 +(*
   1.578 +val T =  (type_of o term_of o the) (parse thy "#12::real");
   1.579 +val t = mk_factroot "SqRoot.sqrt" T 2 3;
   1.580 +(cterm_of thy) t;
   1.581 +val it = "#2 * sqrt #3 " : cterm
   1.582 +*)
   1.583 +fun var_op_num v op_ optype ntyp n =
   1.584 +  Const (op_, optype) $ v $ 
   1.585 +   Free (str_of_int  n, ntyp);
   1.586 +
   1.587 +fun num_op_var v op_ optype ntyp n =
   1.588 +  Const (op_,optype) $  
   1.589 +   Free (str_of_int n, ntyp) $ v;
   1.590 +
   1.591 +fun num_op_num T1 T2 (op_,Top) n1 n2 = 
   1.592 +  Const (op_,Top) $ 
   1.593 +  Free (str_of_int n1, T1) $ Free (str_of_int n2, T2);
   1.594 +(*
   1.595 +> val t = num_op_num "Int" 3 4;
   1.596 +> atomty t;
   1.597 +> string_of_cterm ((cterm_of thy) t);
   1.598 +*)
   1.599 +
   1.600 +fun const_in str (Const _) = false
   1.601 +  | const_in str (Free (s,_)) = if strip_thy s = str then true else false
   1.602 +  | const_in str (Bound _) = false
   1.603 +  | const_in str (Var _) = false
   1.604 +  | const_in str (Abs (_,_,body)) = const_in str body
   1.605 +  | const_in str (f$u) = const_in str f orelse const_in str u;
   1.606 +(*
   1.607 +> val t = (term_of o the o (parse thy)) "6 + 5 * sqrt 4 + 3";
   1.608 +> const_in "sqrt" t;
   1.609 +val it = true : bool
   1.610 +> val t = (term_of o the o (parse thy)) "6 + 5 * 4 + 3";
   1.611 +> const_in "sqrt" t;
   1.612 +val it = false : bool
   1.613 +*)
   1.614 +
   1.615 +(*used for calculating built in binary operations in Isabelle2002->Float.ML*)
   1.616 +(*fun calc "op +"  (n1, n2) = n1+n2
   1.617 +  | calc "op -"  (n1, n2) = n1-n2
   1.618 +  | calc "op *"  (n1, n2) = n1*n2
   1.619 +  | calc "HOL.divide"(n1, n2) = n1 div n2
   1.620 +  | calc "Atools.pow"(n1, n2) = power n1 n2
   1.621 +  | calc op_ _ = raise error ("calc: operator = "^op_^" not defined");-----*)
   1.622 +fun calc_equ "op <"  (n1, n2) = n1 < n2
   1.623 +  | calc_equ "op <=" (n1, n2) = n1 <= n2
   1.624 +  | calc_equ op_ _ = 
   1.625 +  raise error ("calc_equ: operator = "^op_^" not defined");
   1.626 +fun sqrt (n:int) = if n < 0 then 0
   1.627 +    (*FIXME ~~~*)  else (trunc o Math.sqrt o Real.fromInt) n;
   1.628 +
   1.629 +fun mk_thmid thmid op_ n1 n2 = 
   1.630 +  thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
   1.631 +
   1.632 +fun dest_binop_typ (Type("fun",[range,Type("fun",[arg2,arg1])])) =
   1.633 +  (arg1,arg2,range)
   1.634 +  | dest_binop_typ _ = raise error "dest_binop_typ: not binary";
   1.635 +(* -----
   1.636 +> val t = (term_of o the o (parse thy)) "#3^#4";
   1.637 +> val hT = type_of (head_of t);
   1.638 +> dest_binop_typ hT;
   1.639 +val it = ("'a","nat","'a") : typ * typ * typ
   1.640 + ----- *)
   1.641 +
   1.642 +
   1.643 +(** transform binary numeralsstrings **)
   1.644 +(*Makarius 100308, hacked by WN*)
   1.645 +val numbers_to_string =
   1.646 +  let
   1.647 +    fun dest_num t =
   1.648 +      (case try HOLogic.dest_number t of
   1.649 +        SOME (T, i) =>
   1.650 +          (*if T = @{typ int} orelse T = @{typ real} then WN*)
   1.651 +            SOME (Free (signed_string_of_int i, T))
   1.652 +          (*else NONE  WN*)
   1.653 +      | NONE => NONE);
   1.654 +
   1.655 +    fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
   1.656 +      | to_str (t as (u1 $ u2)) =
   1.657 +          (case dest_num t of
   1.658 +            SOME t' => t'
   1.659 +          | NONE => to_str u1 $ to_str u2)
   1.660 +      | to_str t = perhaps dest_num t;
   1.661 +  in to_str end
   1.662 +
   1.663 +(*.make uminus uniform: 
   1.664 +   Const ("uminus", _) $ Free ("2", "RealDef.real") --> Free ("-2", _)
   1.665 +to be used immediately before evaluation of numerals; 
   1.666 +see Scripts/calculate.sml .*)
   1.667 +(*2002 fun(*app_num_tr'2 (Const("0",T)) = Free("0",T)
   1.668 +  | app_num_tr'2 (Const("1",T)) = Free("1",T)
   1.669 +  |*)app_num_tr'2 (t as Const("uminus",_) $ Free(s,T)) = 
   1.670 +    (case int_of_str s of SOME i => 
   1.671 +			  if i > 0 then Free("-"^s,T) else Free(s,T)
   1.672 +		       | NONE => t)
   1.673 +(*| app_num_tr'2 (t as Const(s,T)) = t
   1.674 +  | app_num_tr'2 (Const("Numeral.number_of",Type ("fun", [_, T])) $ t) = 
   1.675 +    Free(NumeralSyntax.dest_bin_str t, T)
   1.676 +  | app_num_tr'2 (t as Free(s,T)) = t
   1.677 +  | app_num_tr'2 (t as Var(n,T)) = t
   1.678 +  | app_num_tr'2 (t as Bound i) = t
   1.679 +*)| app_num_tr'2 (Abs(s,T,body)) = Abs(s,T, app_num_tr'2 body)
   1.680 +  | app_num_tr'2 (t1 $ t2) = (app_num_tr'2 t1) $ (app_num_tr'2 t2)
   1.681 +  | app_num_tr'2 t = t;
   1.682 +*)
   1.683 +val uminus_to_string =
   1.684 +    let
   1.685 +	fun dest_num t =
   1.686 +	    (case t of
   1.687 +		 (Const ("HOL.uminus_class.uminus", _) $ Free (s, T)) => 
   1.688 +		 (case int_of_str s of
   1.689 +		      SOME i => 
   1.690 +		      SOME (Free (signed_string_of_int (~1 * i), T))
   1.691 +		    | NONE => NONE)
   1.692 +	       | _ => NONE);
   1.693 +	    
   1.694 +	fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
   1.695 +	  | to_str (t as (u1 $ u2)) =
   1.696 +            (case dest_num t of
   1.697 +		 SOME t' => t'
   1.698 +               | NONE => to_str u1 $ to_str u2)
   1.699 +	  | to_str t = perhaps dest_num t;
   1.700 +    in to_str end;
   1.701 +
   1.702 +
   1.703 +(*2002 fun num_str thm =
   1.704 +  let 
   1.705 +    val {sign_ref = sign_ref, der = der, maxidx = maxidx,
   1.706 +	    shyps = shyps, hyps = hyps, (*tpairs = tpairs,*) prop = prop} = 
   1.707 +	rep_thm_G thm;
   1.708 +    val prop' = app_num_tr'1 prop;
   1.709 +  in assbl_thm sign_ref der maxidx shyps hyps (*tpairs*) prop' end;*)
   1.710 +fun num_str thm =
   1.711 +  let val (deriv, 
   1.712 +	   {thy_ref = thy_ref, tags = tags, maxidx = maxidx, shyps = shyps, 
   1.713 +	    hyps = hyps, tpairs = tpairs, prop = prop}) = rep_thm_G thm
   1.714 +    val prop' = numbers_to_string prop;
   1.715 +  in assbl_thm deriv thy_ref tags maxidx shyps hyps tpairs prop' end;
   1.716 +
   1.717 +fun get_thm' xstring = (*?covers 2009 Thm?!, replaces 2002 fun get_thm :
   1.718 +val it = fn : theory -> xstring -> Thm.thm*)
   1.719 +    Thm (xstring, 
   1.720 +	 num_str (ProofContext.get_thm (thy2ctxt' "Isac") xstring)); 
   1.721 +
   1.722 +(** get types of Free and Abs for parse' **)
   1.723 +(*11.1.00: not used, fix-typed +,*,-,^ instead *)
   1.724 +
   1.725 +val dummyT = Type ("dummy",[]);
   1.726 +val dummyT = TVar (("DUMMY",0),[]);
   1.727 +
   1.728 +(* assumes only 1 type for numerals 
   1.729 +   and different identifiers for Const, Free and Abs *)
   1.730 +fun get_types t = 
   1.731 +  let
   1.732 +    fun get ts  (Const(s,T)) = (s,T)::ts
   1.733 +      | get ts  (Free(s,T)) = if is_no s 
   1.734 +				then ("#",T)::ts else (s,T)::ts
   1.735 +      | get ts  (Var(n,T)) = ts
   1.736 +      | get ts  (Bound i) = ts
   1.737 +      | get ts  (Abs(s,T,body)) = get ((s,T)::ts)  body
   1.738 +      | get ts  (t1 $ t2) = (get ts  t1) @ (get ts  t2)
   1.739 +  in distinct (get [] t) end;
   1.740 +(*
   1.741 +val t = (term_of o the o (parse thy)) "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)";
   1.742 +get_types t;
   1.743 +*)
   1.744 +
   1.745 +(*11.1.00: not used, fix-typed +,*,-,^ instead *)
   1.746 +fun set_types al (Const(s,T)) = 
   1.747 +    (case assoc (al,s) of
   1.748 +       SOME T' => Const(s,T')
   1.749 +     | NONE => (warning ("set_types: no type for "^s); Const(s,dummyT)))
   1.750 +  | set_types al (Free(s,T)) = 
   1.751 +  if is_no s then
   1.752 +    (case assoc (al,"#") of
   1.753 +      SOME T' => Free(s,T')
   1.754 +    | NONE => (warning ("set_types: no type for numerals"); Free(s,T)))
   1.755 +  else (case assoc (al,s) of
   1.756 +	       SOME T' => Free(s,T')
   1.757 +	     | NONE => (warning ("set_types: no type for "^s); Free(s,T)))
   1.758 +  | set_types al (Var(n,T)) = Var(n,T)
   1.759 +  | set_types al (Bound i) = Bound i
   1.760 +  | set_types al (Abs(s,T,body)) = 
   1.761 +		 (case assoc (al,s) of
   1.762 +		    SOME T'  => Abs(s,T', set_types al body)
   1.763 +		  | NONE => (warning ("set_types: no type for "^s);
   1.764 +			     Abs(s,T, set_types al body)))
   1.765 +  | set_types al (t1 $ t2) = (set_types al t1) $ (set_types al t2);
   1.766 +(*
   1.767 +val t = (term_of o the o (parse thy)) "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)";
   1.768 +val al = get_types t;
   1.769 +
   1.770 +val t = (term_of o the o (parse thy)) "x = #0 + #-1 * #-4";
   1.771 +atomty t;                         (* 'a *)
   1.772 +val t' = set_types al t;
   1.773 +atomty t';                        (*real*)
   1.774 +(cterm_of thy) t';
   1.775 +val it = "x = #0 + #-1 * #-4" : cterm
   1.776 +
   1.777 +val t = (term_of o the o (parse thy)) 
   1.778 +  "#5 * x + x ^^^ #2 = (#2 + x) ^^^ #2";
   1.779 +atomty t;
   1.780 +val t' = set_types al t;
   1.781 +atomty t';
   1.782 +(cterm_of thy) t';
   1.783 +uncaught exception TYPE               (*^^^ is new, NOT in al*)
   1.784 +*)
   1.785 +      
   1.786 +
   1.787 +(** from Descript.ML **)
   1.788 +
   1.789 +(** decompose an isa-list to an ML-list 
   1.790 +    i.e. [] belong to the meta-language, too **)
   1.791 +
   1.792 +fun is_list ((Const("List.list.Cons",_)) $ _ $ _) = true
   1.793 +  | is_list _ = false;
   1.794 +(* val (SOME ct) = parse thy "lll::real list";
   1.795 +> val ty = (#t o rep_cterm) ct;
   1.796 +> is_list ty;
   1.797 +val it = false : bool
   1.798 +> val (SOME ct) = parse thy "[lll]";
   1.799 +> val ty = (#t o rep_cterm) ct;
   1.800 +> is_list ty;
   1.801 +val it = true : bool *)
   1.802 +
   1.803 +
   1.804 +
   1.805 +fun mk_Free (s,T) = Free(s,T);
   1.806 +fun mk_free T s =  Free(s,T);
   1.807 +
   1.808 +(*instantiate let; necessary for ass_up*)
   1.809 +fun inst_abs thy (Const sT) = Const sT
   1.810 +  | inst_abs thy (Free sT) = Free sT
   1.811 +  | inst_abs thy (Bound n) = Bound n
   1.812 +  | inst_abs thy (Var iT) = Var iT
   1.813 +  | inst_abs thy (Const ("Let",T1) $ e $ (Abs (v,T2,b))) = 
   1.814 +  let val (v',b') = variant_abs (v,T2,b);     (*fun variant_abs: term.ML*)
   1.815 +  in Const ("Let",T1) $ inst_abs thy e $ (Abs (v',T2,inst_abs thy b')) end
   1.816 +  | inst_abs thy (t1 $ t2) = inst_abs thy t1 $ inst_abs thy t2
   1.817 +  | inst_abs thy t = 
   1.818 +    (writeln("inst_abs: unchanged t= "^ term2str t);
   1.819 +     t);
   1.820 +(*val scr as (Script sc) = Script ((term_of o the o (parse thy))
   1.821 + "Script Testeq (e_::bool) =                                        \
   1.822 +   \While (contains_root e_) Do                                     \
   1.823 +   \ (let e_ = Try (Repeat (Rewrite rroot_square_inv False e_));    \
   1.824 +   \      e_ = Try (Repeat (Rewrite square_equation_left True e_)) \
   1.825 +   \   in Try (Repeat (Rewrite radd_0 False e_)))                 ");
   1.826 +ML> atomt sc;
   1.827 +*** Const ( Script.Testeq)
   1.828 +*** . Free ( e_, )
   1.829 +*** . Const ( Script.While)
   1.830 +*** . . Const ( RatArith.contains'_root)
   1.831 +*** . . . Free ( e_, )
   1.832 +*** . . Const ( Let)
   1.833 +*** . . . Const ( Script.Try)
   1.834 +*** . . . . Const ( Script.Repeat)
   1.835 +*** . . . . . Const ( Script.Rewrite)
   1.836 +*** . . . . . . Free ( rroot_square_inv, )
   1.837 +*** . . . . . . Const ( False)
   1.838 +*** . . . . . . Free ( e_, )
   1.839 +*** . . . Abs( e_,..
   1.840 +*** . . . . Const ( Let)
   1.841 +*** . . . . . Const ( Script.Try)
   1.842 +*** . . . . . . Const ( Script.Repeat)
   1.843 +*** . . . . . . . Const ( Script.Rewrite)
   1.844 +*** . . . . . . . . Free ( square_equation_left, )
   1.845 +*** . . . . . . . . Const ( True)
   1.846 +*** . . . . . . . . Bound 0                            <-- !!!
   1.847 +*** . . . . . Abs( e_,..
   1.848 +*** . . . . . . Const ( Script.Try)
   1.849 +*** . . . . . . . Const ( Script.Repeat)
   1.850 +*** . . . . . . . . Const ( Script.Rewrite)
   1.851 +*** . . . . . . . . . Free ( radd_0, )
   1.852 +*** . . . . . . . . . Const ( False)
   1.853 +*** . . . . . . . . . Bound 0                          <-- !!!
   1.854 +val it = () : unit
   1.855 +ML> atomt (inst_abs thy sc);
   1.856 +*** Const ( Script.Testeq)
   1.857 +*** . Free ( e_, )
   1.858 +*** . Const ( Script.While)
   1.859 +*** . . Const ( RatArith.contains'_root)
   1.860 +*** . . . Free ( e_, )
   1.861 +*** . . Const ( Let)
   1.862 +*** . . . Const ( Script.Try)
   1.863 +*** . . . . Const ( Script.Repeat)
   1.864 +*** . . . . . Const ( Script.Rewrite)
   1.865 +*** . . . . . . Free ( rroot_square_inv, )
   1.866 +*** . . . . . . Const ( False)
   1.867 +*** . . . . . . Free ( e_, )
   1.868 +*** . . . Abs( e_,..
   1.869 +*** . . . . Const ( Let)
   1.870 +*** . . . . . Const ( Script.Try)
   1.871 +*** . . . . . . Const ( Script.Repeat)
   1.872 +*** . . . . . . . Const ( Script.Rewrite)
   1.873 +*** . . . . . . . . Free ( square_equation_left, )
   1.874 +*** . . . . . . . . Const ( True)
   1.875 +*** . . . . . . . . Free ( e_, )                        <-- !!!
   1.876 +*** . . . . . Abs( e_,..
   1.877 +*** . . . . . . Const ( Script.Try)
   1.878 +*** . . . . . . . Const ( Script.Repeat)
   1.879 +*** . . . . . . . . Const ( Script.Rewrite)
   1.880 +*** . . . . . . . . . Free ( radd_0, )
   1.881 +*** . . . . . . . . . Const ( False)
   1.882 +*** . . . . . . . . . Free ( e_, )                      <-- ZUFALL vor 5.03!!!
   1.883 +val it = () : unit*)
   1.884 +
   1.885 +
   1.886 +
   1.887 +
   1.888 +fun inst_abs thy (Const sT) = Const sT
   1.889 +  | inst_abs thy (Free sT) = Free sT
   1.890 +  | inst_abs thy (Bound n) = Bound n
   1.891 +  | inst_abs thy (Var iT) = Var iT
   1.892 +  | inst_abs thy (Const ("Let",T1) $ e $ (Abs (v,T2,b))) = 
   1.893 +  let val b' = subst_bound (Free(v,T2),b);
   1.894 +  (*fun variant_abs: term.ML*)
   1.895 +  in Const ("Let",T1) $ inst_abs thy e $ (Abs (v,T2,inst_abs thy b')) end
   1.896 +  | inst_abs thy (t1 $ t2) = inst_abs thy t1 $ inst_abs thy t2
   1.897 +  | inst_abs thy t = 
   1.898 +    (writeln("inst_abs: unchanged t= "^ term2str t);
   1.899 +     t);
   1.900 +(*val scr =    
   1.901 +   "Script Make_fun_by_explicit (f_::real) (v_::real) (eqs_::bool list) = \
   1.902 +   \ (let h_ = (hd o (filterVar f_)) eqs_;                    \
   1.903 +   \      e_1 = hd (dropWhile (ident h_) eqs_);       \
   1.904 +   \      vs_ = dropWhile (ident f_) (Vars h_);                \
   1.905 +   \      v_1 = hd (dropWhile (ident v_) vs_);                \
   1.906 +   \      (s_1::bool list)=(SubProblem(DiffApp_,[univar,equation],[no_met])\
   1.907 +   \                          [bool_ e_1, real_ v_1])\
   1.908 +   \ in Substitute [(v_1 = (rhs o hd) s_1)] h_)";
   1.909 +> val ttt = (term_of o the o (parse thy)) scr;
   1.910 +> writeln(term2str ttt);
   1.911 +> atomt ttt;
   1.912 +*** -------------
   1.913 +*** Const ( DiffApp.Make'_fun'_by'_explicit)
   1.914 +*** . Free ( f_, )
   1.915 +*** . Free ( v_, )
   1.916 +*** . Free ( eqs_, )
   1.917 +*** . Const ( Let)
   1.918 +*** . . Const ( Fun.op o)
   1.919 +*** . . . Const ( List.hd)
   1.920 +*** . . . Const ( DiffApp.filterVar)
   1.921 +*** . . . . Free ( f_, )
   1.922 +*** . . . Free ( eqs_, )
   1.923 +*** . . Abs( h_,..
   1.924 +*** . . . Const ( Let)
   1.925 +*** . . . . Const ( List.hd)
   1.926 +*** . . . . . Const ( List.dropWhile)
   1.927 +*** . . . . . . Const ( Atools.ident)
   1.928 +*** . . . . . . . Bound 0                     <---- Free ( h_, )
   1.929 +*** . . . . . . Free ( eqs_, )
   1.930 +*** . . . . Abs( e_1,..
   1.931 +*** . . . . . Const ( Let)
   1.932 +*** . . . . . . Const ( List.dropWhile)
   1.933 +*** . . . . . . . Const ( Atools.ident)
   1.934 +*** . . . . . . . . Free ( f_, )
   1.935 +*** . . . . . . . Const ( Tools.Vars)
   1.936 +*** . . . . . . . . Bound 1                       <---- Free ( h_, )
   1.937 +*** . . . . . . Abs( vs_,..
   1.938 +*** . . . . . . . Const ( Let)
   1.939 +*** . . . . . . . . Const ( List.hd)
   1.940 +*** . . . . . . . . . Const ( List.dropWhile)
   1.941 +*** . . . . . . . . . . Const ( Atools.ident)
   1.942 +*** . . . . . . . . . . . Free ( v_, )
   1.943 +*** . . . . . . . . . . Bound 0                   <---- Free ( vs_, )
   1.944 +*** . . . . . . . . Abs( v_1,..
   1.945 +*** . . . . . . . . . Const ( Let)
   1.946 +*** . . . . . . . . . . Const ( Script.SubProblem)
   1.947 +*** . . . . . . . . . . . Const ( Pair)
   1.948 +*** . . . . . . . . . . . . Free ( DiffApp_, )
   1.949 +*** . . . . . . . . . . . . Const ( Pair)
   1.950 +*** . . . . . . . . . . . . . Const ( List.list.Cons)
   1.951 +*** . . . . . . . . . . . . . . Free ( univar, )
   1.952 +*** . . . . . . . . . . . . . . Const ( List.list.Cons)
   1.953 +*** . . . . . . . . . . . . . . . Free ( equation, )
   1.954 +*** . . . . . . . . . . . . . . . Const ( List.list.Nil)
   1.955 +*** . . . . . . . . . . . . . Const ( List.list.Cons)
   1.956 +*** . . . . . . . . . . . . . . Free ( no_met, )
   1.957 +*** . . . . . . . . . . . . . . Const ( List.list.Nil)
   1.958 +*** . . . . . . . . . . . Const ( List.list.Cons)
   1.959 +*** . . . . . . . . . . . . Const ( Script.bool_)
   1.960 +*** . . . . . . . . . . . . . Bound 2                   <----- Free ( e_1, )
   1.961 +*** . . . . . . . . . . . . Const ( List.list.Cons)
   1.962 +*** . . . . . . . . . . . . . Const ( Script.real_)
   1.963 +*** . . . . . . . . . . . . . . Bound 0                 <----- Free ( v_1, )
   1.964 +*** . . . . . . . . . . . . . Const ( List.list.Nil)
   1.965 +*** . . . . . . . . . . Abs( s_1,..
   1.966 +*** . . . . . . . . . . . Const ( Script.Substitute)
   1.967 +*** . . . . . . . . . . . . Const ( List.list.Cons)
   1.968 +*** . . . . . . . . . . . . . Const ( Pair)
   1.969 +*** . . . . . . . . . . . . . . Bound 1                 <----- Free ( v_1, )
   1.970 +*** . . . . . . . . . . . . . . Const ( Fun.op o)
   1.971 +*** . . . . . . . . . . . . . . . Const ( Tools.rhs)
   1.972 +*** . . . . . . . . . . . . . . . Const ( List.hd)
   1.973 +*** . . . . . . . . . . . . . . . Bound 0               <----- Free ( s_1, )
   1.974 +*** . . . . . . . . . . . . . Const ( List.list.Nil)
   1.975 +*** . . . . . . . . . . . . Bound 4                     <----- Free ( h_, )
   1.976 +
   1.977 +> val ttt' = inst_abs thy ttt;
   1.978 +> writeln(term2str ttt');
   1.979 +Script Make_fun_by_explicit f_ v_ eqs_ =  
   1.980 +  ... as above ...
   1.981 +> atomt ttt';
   1.982 +*** -------------
   1.983 +*** Const ( DiffApp.Make'_fun'_by'_explicit)
   1.984 +*** . Free ( f_, )
   1.985 +*** . Free ( v_, )
   1.986 +*** . Free ( eqs_, )
   1.987 +*** . Const ( Let)
   1.988 +*** . . Const ( Fun.op o)
   1.989 +*** . . . Const ( List.hd)
   1.990 +*** . . . Const ( DiffApp.filterVar)
   1.991 +*** . . . . Free ( f_, )
   1.992 +*** . . . Free ( eqs_, )
   1.993 +*** . . Abs( h_,..
   1.994 +*** . . . Const ( Let)
   1.995 +*** . . . . Const ( List.hd)
   1.996 +*** . . . . . Const ( List.dropWhile)
   1.997 +*** . . . . . . Const ( Atools.ident)
   1.998 +*** . . . . . . . Free ( h_, )                <---- Bound 0
   1.999 +*** . . . . . . Free ( eqs_, )
  1.1000 +*** . . . . Abs( e_1,..
  1.1001 +*** . . . . . Const ( Let)
  1.1002 +*** . . . . . . Const ( List.dropWhile)
  1.1003 +*** . . . . . . . Const ( Atools.ident)
  1.1004 +*** . . . . . . . . Free ( f_, )
  1.1005 +*** . . . . . . . Const ( Tools.Vars)
  1.1006 +*** . . . . . . . . Free ( h_, )                  <---- Bound 1
  1.1007 +*** . . . . . . Abs( vs_,..
  1.1008 +*** . . . . . . . Const ( Let)
  1.1009 +*** . . . . . . . . Const ( List.hd)
  1.1010 +*** . . . . . . . . . Const ( List.dropWhile)
  1.1011 +*** . . . . . . . . . . Const ( Atools.ident)
  1.1012 +*** . . . . . . . . . . . Free ( v_, )
  1.1013 +*** . . . . . . . . . . Free ( vs_, )             <---- Bound 0
  1.1014 +*** . . . . . . . . Abs( v_1,..
  1.1015 +*** . . . . . . . . . Const ( Let)
  1.1016 +*** . . . . . . . . . . Const ( Script.SubProblem)
  1.1017 +*** . . . . . . . . . . . Const ( Pair)
  1.1018 +*** . . . . . . . . . . . . Free ( DiffApp_, )
  1.1019 +*** . . . . . . . . . . . . Const ( Pair)
  1.1020 +*** . . . . . . . . . . . . . Const ( List.list.Cons)
  1.1021 +*** . . . . . . . . . . . . . . Free ( univar, )
  1.1022 +*** . . . . . . . . . . . . . . Const ( List.list.Cons)
  1.1023 +*** . . . . . . . . . . . . . . . Free ( equation, )
  1.1024 +*** . . . . . . . . . . . . . . . Const ( List.list.Nil)
  1.1025 +*** . . . . . . . . . . . . . Const ( List.list.Cons)
  1.1026 +*** . . . . . . . . . . . . . . Free ( no_met, )
  1.1027 +*** . . . . . . . . . . . . . . Const ( List.list.Nil)
  1.1028 +*** . . . . . . . . . . . Const ( List.list.Cons)
  1.1029 +*** . . . . . . . . . . . . Const ( Script.bool_)
  1.1030 +*** . . . . . . . . . . . . . Free ( e_1, )             <----- Bound 2
  1.1031 +*** . . . . . . . . . . . . Const ( List.list.Cons)
  1.1032 +*** . . . . . . . . . . . . . Const ( Script.real_)
  1.1033 +*** . . . . . . . . . . . . . . Free ( v_1, )           <----- Bound 0
  1.1034 +*** . . . . . . . . . . . . . Const ( List.list.Nil)
  1.1035 +*** . . . . . . . . . . Abs( s_1,..
  1.1036 +*** . . . . . . . . . . . Const ( Script.Substitute)
  1.1037 +*** . . . . . . . . . . . . Const ( List.list.Cons)
  1.1038 +*** . . . . . . . . . . . . . Const ( Pair)
  1.1039 +*** . . . . . . . . . . . . . . Free ( v_1, )           <----- Bound 1
  1.1040 +*** . . . . . . . . . . . . . . Const ( Fun.op o)
  1.1041 +*** . . . . . . . . . . . . . . . Const ( Tools.rhs)
  1.1042 +*** . . . . . . . . . . . . . . . Const ( List.hd)
  1.1043 +*** . . . . . . . . . . . . . . . Free ( s_1, )         <----- Bound 0
  1.1044 +*** . . . . . . . . . . . . . Const ( List.list.Nil)
  1.1045 +*** . . . . . . . . . . . . Free ( h_, )                <----- Bound 4
  1.1046 +
  1.1047 +Note numbering of de Bruijn indexes !
  1.1048 +
  1.1049 +Script Make_fun_by_explicit f_ v_ eqs_ =
  1.1050 + let h_ = (hd o filterVar f_) eqs_; 
  1.1051 +     e_1 = hd (dropWhile (ident h_ BOUND_0) eqs_);
  1.1052 +     vs_ = dropWhile (ident f_) (Vars h_ BOUND_1);
  1.1053 +     v_1 = hd (dropWhile (ident v_) vs_ BOUND_0);
  1.1054 +     s_1 =
  1.1055 +       SubProblem (DiffApp_, [univar, equation], [no_met])
  1.1056 +        [bool_ e_1 BOUND_2, real_ v_1 BOUND_0]
  1.1057 + in Substitute [(v_1 BOUND_1 = (rhs o hd) s_1 BOUND_0)] h_ BOUND_4
  1.1058 +*)
  1.1059 +
  1.1060 +
  1.1061 +fun T_a2real (Type (s, [])) = 
  1.1062 +    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else Type (s, [])
  1.1063 +  | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts)
  1.1064 +  | T_a2real (TFree (s, srt)) = 
  1.1065 +    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TFree (s, srt)
  1.1066 +  | T_a2real (TVar (("DUMMY",_),srt)) = HOLogic.realT;
  1.1067 +
  1.1068 +(*FIXME .. fixes the type (+see Typefix.thy*)
  1.1069 +fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T)) 
  1.1070 +  | typ_a2real (Free( s, T)) = (Free( s, T_a2real T))
  1.1071 +  | typ_a2real (Var( n, T)) = (Var( n, T_a2real T))
  1.1072 +  | typ_a2real (Bound i) = (Bound i)
  1.1073 +  | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t)
  1.1074 +  | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2);
  1.1075 +(*
  1.1076 +----------------6.8.02---------------------------------------------------
  1.1077 + val str = "1";
  1.1078 + val t = read_cterm (sign_of thy) (str,(TVar(("DUMMY",0),[])));
  1.1079 + atomty (term_of t);
  1.1080 +*** -------------
  1.1081 +*** Const ( 1, 'a)
  1.1082 + val t = (app_num_tr' o term_of) t;
  1.1083 + atomty t;
  1.1084 +*** ------------- 
  1.1085 +*** Const ( 1, 'a)                                                              
  1.1086 + val t = typ_a2real t;
  1.1087 + atomty t;
  1.1088 +*** -------------   
  1.1089 +*** Const ( 1, real)                                                            
  1.1090 +
  1.1091 + val str = "2";
  1.1092 + val t = read_cterm (sign_of thy) (str,(TVar(("DUMMY",0),[])));
  1.1093 + atomty (term_of t);
  1.1094 +*** -------------
  1.1095 +*** Const ( Numeral.number_of, bin => 'a)
  1.1096 +*** . Const ( Numeral.bin.Bit, [bin, bool] => bin)
  1.1097 +*** . . Const ( Numeral.bin.Bit, [bin, bool] => bin)
  1.1098 +*** . . . Const ( Numeral.bin.Pls, bin)
  1.1099 +*** . . . Const ( True, bool)
  1.1100 +*** . . Const ( False, bool)
  1.1101 + val t = (app_num_tr' o term_of) t;
  1.1102 + atomty t;
  1.1103 +*** -------------
  1.1104 +*** Free ( 2, 'a)
  1.1105 + val t = typ_a2real t;
  1.1106 + atomty t;
  1.1107 +*** -------------
  1.1108 +*** Free ( 2, real)
  1.1109 +----------------6.8.02---------------------------------------------------
  1.1110 +
  1.1111 +
  1.1112 +> val str = "R";
  1.1113 +> val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[]))));
  1.1114 +val t = Free ("R","?DUMMY") : term
  1.1115 +> val t' = typ_a2real t;
  1.1116 +> (cterm_of thy) t';
  1.1117 +val it = "R::RealDef.real" : cterm
  1.1118 +
  1.1119 +> val str = "R=R";
  1.1120 +> val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[]))));
  1.1121 +> atomty (typ_a2real t);
  1.1122 +*** -------------
  1.1123 +*** Const ( op =, [RealDef.real, RealDef.real] => bool)
  1.1124 +***   Free ( R, RealDef.real)
  1.1125 +***   Free ( R, RealDef.real)
  1.1126 +> val t' = typ_a2real t;
  1.1127 +> (cterm_of thy) t';
  1.1128 +val it = "(R::RealDef.real) = R" : cterm
  1.1129 +
  1.1130 +> val str = "fixed_values [R=R]";
  1.1131 +> val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[]))));
  1.1132 +> val t' = typ_a2real t;
  1.1133 +> (cterm_of thy) t';
  1.1134 +val it = "fixed_values [(R::RealDef.real) = R]" : cterm
  1.1135 +*)
  1.1136 +
  1.1137 +(*TODO.WN0609: parse should return a term or a string 
  1.1138 +	     (or even more comprehensive datastructure for error-messages)
  1.1139 + i.e. in wrapping with SOME term or NONE the latter is not sufficient*)
  1.1140 +(*2002 fun parseold thy str = 
  1.1141 +  (let 
  1.1142 +     val sgn = sign_of thy;
  1.1143 +     val t = ((*typ_a2real o*) app_num_tr'1 o term_of) 
  1.1144 +       (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
  1.1145 +   in SOME (cterm_of sgn t) end)
  1.1146 +     handle _ => NONE;*)
  1.1147 +
  1.1148 +
  1.1149 +
  1.1150 +fun parseold thy str = 
  1.1151 +  (let val t = ((*typ_a2real o*) numbers_to_string) 
  1.1152 +		   (Syntax.read_term_global thy str)
  1.1153 +   in SOME (cterm_of thy t) end)
  1.1154 +    handle _ => NONE;
  1.1155 +(*2002 fun parseN thy str = 
  1.1156 +  (let 
  1.1157 +     val sgn = sign_of thy;
  1.1158 +     val t = ((*typ_a2real o app_num_tr'1 o*) term_of) 
  1.1159 +       (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
  1.1160 +   in SOME (cterm_of sgn t) end)
  1.1161 +     handle _ => NONE;*)
  1.1162 +fun parseN thy str = 
  1.1163 +  (let val t = (*(typ_a2real o numbers_to_string)*) 
  1.1164 +	   (Syntax.read_term_global thy str)
  1.1165 +   in SOME (cterm_of thy t) end)
  1.1166 +    handle _ => NONE;
  1.1167 +(*2002 fun parse thy str = 
  1.1168 +  (let 
  1.1169 +     val sgn = sign_of thy;
  1.1170 +     val t = (typ_a2real o app_num_tr'1 o term_of) 
  1.1171 +       (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
  1.1172 +   in SOME (cterm_of sgn t) end) (*FIXXXXME 10.8.02: return term !!!*)
  1.1173 +     handle _ => NONE;*)
  1.1174 +(*2010 fun parse thy str = 
  1.1175 +  (let val t = (typ_a2real o app_num_tr'1) (Syntax.read_term_global thy str)
  1.1176 +   in SOME (cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*)
  1.1177 +     handle _ => NONE;*)
  1.1178 +fun parse thy str = 
  1.1179 +  (let val t = (typ_a2real o numbers_to_string) 
  1.1180 +		   (Syntax.read_term_global thy str)
  1.1181 +   in SOME (cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*)
  1.1182 +     handle _ => NONE;
  1.1183 +
  1.1184 +(* 10.8.02: for this reason we still have ^^^--------------------
  1.1185 + val thy = SqRoot.thy;
  1.1186 + val str = "(1::real) ^ (2::nat)";
  1.1187 + val sgn = sign_of thy;
  1.1188 + val ct = (read_cterm sgn (str,(TVar(("DUMMY",0),[])))) handle e =>print_exn e;
  1.1189 +(*1*)"(1::real) ^ 2"; 
  1.1190 + atomty (term_of ct);
  1.1191 +*** -------------
  1.1192 +*** Const ( Nat.power, [real, nat] => real)
  1.1193 +*** . Const ( 1, real)
  1.1194 +*** . Const ( Numeral.number_of, bin => nat)
  1.1195 +*** . . Const ( Numeral.bin.Bit, [bin, bool] => bin)
  1.1196 +*** . . . Const ( Numeral.bin.Bit, [bin, bool] => bin)
  1.1197 +*** . . . . Const ( Numeral.bin.Pls, bin)
  1.1198 +*** . . . . Const ( True, bool)
  1.1199 +*** . . . Const ( False, bool)
  1.1200 + val t = ((app_num_tr' o term_of) 
  1.1201 +	 (read_cterm sgn (str,(TVar(("DUMMY",0),[])))))handle e => print_exn e;
  1.1202 + val ct = (cterm_of sgn t) handle e => print_exn e;
  1.1203 +(*2*)"(1::real) ^ (2::nat)";
  1.1204 + atomty (term_of ct);
  1.1205 +*** -------------
  1.1206 +*** Const ( Nat.power, [real, nat] => real)
  1.1207 +*** . Free ( 1, real)
  1.1208 +*** . Free ( 2, nat)                                                            (*1*) Const("2",_) (*2*) Free("2",_)
  1.1209 +
  1.1210 +
  1.1211 + val str = "(2::real) ^ (2::nat)";
  1.1212 + val t = (read_cterm sgn (str,(TVar(("DUMMY",0),[])))) handle e => print_exn e;
  1.1213 +val t = "(2::real) ^ 2" : cterm
  1.1214 + val t = ((app_num_tr' o term_of) 
  1.1215 +	 (read_cterm sgn (str,(TVar(("DUMMY",0),[])))))handle e => print_exn e;
  1.1216 + val ct = (cterm_of sgn t) handle e => print_exn e;
  1.1217 +Variable "2" has two distinct types
  1.1218 +real
  1.1219 +nat
  1.1220 +uncaught exception TYPE
  1.1221 +  raised at: sign.ML:672.26-673.56
  1.1222 +             goals.ML:1100.61
  1.1223 +
  1.1224 +
  1.1225 + val str = "(3::real) ^ (2::nat)";
  1.1226 + val t = (read_cterm sgn (str,(TVar(("DUMMY",0),[])))) handle e => print_exn e;
  1.1227 +val t = "(3::real) ^ 2" : cterm
  1.1228 + val t = ((app_num_tr' o term_of) 
  1.1229 +	 (read_cterm sgn (str,(TVar(("DUMMY",0),[])))))handle e => print_exn e;
  1.1230 + val ct = (cterm_of sgn t) handle e => print_exn e;
  1.1231 +val ct = "(3::real) ^ (2::nat)" : cterm
  1.1232 +
  1.1233 +
  1.1234 +Conclusion: The type inference allows different types 
  1.1235 +            for one and the same  Numeral.number_of 
  1.1236 +        BUT the type inference doesn't allow 
  1.1237 +	    Free ( 2, real) and Free ( 2, nat) within one term
  1.1238 +---------------       ~~~~                ~~~                  *)
  1.1239 +(*
  1.1240 +> val (SOME ct) = parse thy "(-#5)^^^#3"; 
  1.1241 +> atomty (term_of ct);
  1.1242 +*** -------------
  1.1243 +*** Const ( Nat.op ^, ['a, nat] => 'a)
  1.1244 +***   Const ( uminus, 'a => 'a)
  1.1245 +***     Free ( #5, 'a)
  1.1246 +***   Free ( #3, nat)                
  1.1247 +> val (SOME ct) = parse thy "R=R"; 
  1.1248 +> atomty (term_of ct);
  1.1249 +*** -------------
  1.1250 +*** Const ( op =, [real, real] => bool)
  1.1251 +***   Free ( R, real)
  1.1252 +***   Free ( R, real)
  1.1253 +
  1.1254 +THIS IS THE OUTPUT FOR VERSION (3) above at typ_a2real !!!!!
  1.1255 +*** -------------
  1.1256 +*** Const ( op =, [RealDef.real, RealDef.real] => bool)
  1.1257 +***   Free ( R, RealDef.real)
  1.1258 +***   Free ( R, RealDef.real)                  *)
  1.1259 +
  1.1260 +(*version for testing local to theories*)
  1.1261 +fun str2term_ thy str = (term_of o the o (parse thy)) str;
  1.1262 +fun str2term str = (term_of o the o (parse (theory "Isac"))) str;
  1.1263 +fun strs2terms ss = map str2term ss;
  1.1264 +fun str2termN str = (term_of o the o (parseN (theory "Isac"))) str;
  1.1265 +
  1.1266 +(*+ makes a substitution from the output of Pattern.match +*)
  1.1267 +(*fun mk_subs ((id, _):indexname, t:term) = (Free (id,type_of t), t);*)
  1.1268 +fun mk_subs (subs: ((string * int) * (Term.typ * Term.term)) list) =
  1.1269 +let fun mk_sub ((id, _), (ty, tm)) = (Free (id, ty), tm) in
  1.1270 +map mk_sub subs end;
  1.1271 +
  1.1272 +val atomthm = atomt o #prop o rep_thm;
  1.1273 +
  1.1274 +(*.instantiate #prop thm with bound variables (as Free).*)
  1.1275 +fun inst_bdv [] t = t : term
  1.1276 +  | inst_bdv (instl: (term*term) list) t =
  1.1277 +      let fun subst (v as Var((s,_),T)) = 
  1.1278 +	      (case explode s of
  1.1279 +		   "b"::"d"::"v"::_ => 
  1.1280 +		   if_none (assoc(instl,Free(s,T))) (Free(s,T))
  1.1281 +		 | _ => v)
  1.1282 +            | subst (Abs(a,T,body)) = Abs(a, T, subst body)
  1.1283 +            | subst (f$t') = subst f $ subst t'
  1.1284 +            | subst t = if_none (assoc(instl,t)) t
  1.1285 +      in  subst t  end;
  1.1286 +
  1.1287 +
  1.1288 +(*WN050829 caution: is_atom (str2term"q_0/2 * L * x") = true !!!
  1.1289 +  use length (vars term) = 1 instead*)
  1.1290 +fun is_atom (Const ("Float.Float",_) $ _) = true
  1.1291 +  | is_atom (Const ("ComplexI.I'_'_",_)) = true
  1.1292 +  | is_atom (Const ("op *",_) $ t $ Const ("ComplexI.I'_'_",_)) = is_atom t
  1.1293 +  | is_atom (Const ("op +",_) $ t1 $ Const ("ComplexI.I'_'_",_)) = is_atom t1
  1.1294 +  | is_atom (Const ("op +",_) $ t1 $ 
  1.1295 +		   (Const ("op *",_) $ t2 $ Const ("ComplexI.I'_'_",_))) = 
  1.1296 +    is_atom t1 andalso is_atom t2
  1.1297 +  | is_atom (Const _) = true
  1.1298 +  | is_atom (Free _) = true
  1.1299 +  | is_atom (Var _) = true
  1.1300 +  | is_atom _ = false;
  1.1301 +(* val t = str2term "q_0/2 * L * x";
  1.1302 +
  1.1303 +
  1.1304 +*)
  1.1305 +(*val t = str2term "Float ((1,2),(0,0))";
  1.1306 +> is_atom t;
  1.1307 +val it = true : bool
  1.1308 +> val t = str2term "Float ((1,2),(0,0)) * I__";
  1.1309 +> is_atom t;
  1.1310 +val it = true : bool
  1.1311 +> val t = str2term "Float ((1,2),(0,0)) + Float ((3,4),(0,0)) * I__";
  1.1312 +> is_atom t;
  1.1313 +val it = true : bool
  1.1314 +> val t = str2term "1 + 2*I__";
  1.1315 +> val Const ("op +",_) $ t1 $ (Const ("op *",_) $ t2 $ Const ("ComplexI.I'_'_",_)) = t;
  1.1316 +*)
  1.1317 +
  1.1318 +(*.adaption from Isabelle/src/Pure/term.ML; reports if ALL Free's
  1.1319 +   have found a substitution (required for evaluating the preconditions
  1.1320 +   of _incomplete_ models).*)
  1.1321 +fun subst_atomic_all [] t = (false, (*TODO may be 'true' for some terms ?*)
  1.1322 +			     t : term)
  1.1323 +  | subst_atomic_all (instl: (term*term) list) t =
  1.1324 +      let fun subst (Abs(a,T,body)) = 
  1.1325 +	      let val (all, body') = subst body
  1.1326 +	      in (all, Abs(a, T, body')) end
  1.1327 +            | subst (f$tt) = 
  1.1328 +	      let val (all1, f') = subst f
  1.1329 +		  val (all2, tt') = subst tt
  1.1330 +	      in (all1 andalso all2, f' $ tt') end
  1.1331 +            | subst (t as Free _) = 
  1.1332 +	      if is_num t then (true, t) (*numerals cannot be subst*)
  1.1333 +	      else (case assoc(instl,t) of
  1.1334 +					 SOME t' => (true, t')
  1.1335 +				       | NONE => (false, t))
  1.1336 +            | subst t = (true, if_none (assoc(instl,t)) t)
  1.1337 +      in  subst t  end;
  1.1338 +
  1.1339 +(*.add two terms with a type given.*)
  1.1340 +fun mk_add t1 t2 =
  1.1341 +    let val T1 = type_of t1
  1.1342 +	val T2 = type_of t2
  1.1343 +    in if T1 <> T2 then raise TYPE ("mk_add gets ",[T1, T2],[t1,t2])
  1.1344 +       else (Const ("op +", [T1, T2] ---> T1) $ t1 $ t2)
  1.1345 +    end;
  1.1346 +