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