diff -r 91f6ecbcb778 -r e2b23ba9df13 src/Tools/isac/Scripts/term_G.sml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Tools/isac/Scripts/term_G.sml Thu Aug 12 11:02:32 2010 +0200 @@ -0,0 +1,1344 @@ +(* extends Isabelle/src/Pure/term.ML + (c) Walther Neuper 1999 + +use"Scripts/term_G.sml"; +use"term_G.sml"; +*) + +(* +> cterm_of (sign_of thy) a_term; +val it = "empty" : cterm *) + +(*1003 fun match thy t pat = + (snd (Pattern.match (Sign.tsig_of (sign_of thy)) (pat, t))) + handle _ => []; +fn : Theory.theory -> + Term.term -> Term.term -> (Term.indexname * Term.term) list*) +(*see src/Tools/eqsubst.ML fun clean_match*) +(*1003 fun matches thy tm pa = if match thy tm pa = [] then false else true;*) +fun matches thy tm pa = + (Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true) + handle _ => false + +fun atomtyp t = (*see raw_pp_typ*) + let + fun ato n (Type (s,[])) = + ("\n*** "^indent n^"Type ("^s^",[])") + | ato n (Type (s,Ts)) = + ("\n*** "^indent n^"Type ("^s^",["^ atol (n+1) Ts) + + | ato n (TFree (s,sort)) = + ("\n*** "^indent n^"TFree ("^s^",["^ strs2str' sort) + + | ato n (TVar ((s,i),sort)) = + ("\n*** "^indent n^"TVar (("^s^","^ + string_of_int i ^ strs2str' sort) + and atol n [] = + ("\n*** "^indent n^"]") + | atol n (T::Ts) = (ato n T ^ atol n Ts) +(*in print (ato 0 t ^ "\n") end; TODO TUM10*) +in writeln(ato 0 t) end; + +(*Prog.Tutorial.p.34*) +local + fun pp_pair (x, y) = Pretty.list "(" ")" [x, y] + fun pp_list xs = Pretty.list "[" "]" xs + fun pp_str s = Pretty.str s + fun pp_qstr s = Pretty.quote (pp_str s) + fun pp_int i = pp_str (string_of_int i) + fun pp_sort S = pp_list (map pp_qstr S) + fun pp_constr a args = Pretty.block [pp_str a, Pretty.brk 1, args] +in +fun raw_pp_typ (TVar ((a, i), S)) = + pp_constr "TVar" (pp_pair (pp_pair (pp_qstr a, pp_int i), pp_sort S)) + | raw_pp_typ (TFree (a, S)) = + pp_constr "TFree" (pp_pair (pp_qstr a, pp_sort S)) + | raw_pp_typ (Type (a, tys)) = + pp_constr "Type" (pp_pair (pp_qstr a, pp_list (map raw_pp_typ tys))) +end +(* install +PolyML.addPrettyPrinter + (fn _ => fn _ => ml_pretty o Pretty.to_ML o raw_pp_typ); +de-install +PolyML.addPrettyPrinter + (fn _ => fn _ => ml_pretty o Pretty.to_ML o Proof_Display.pp_typ Pure.thy); +*) + +(* +> val T = (type_of o term_of o the o (parse thy)) "a::[real,int] => nat"; +> atomtyp T; +*** Type (fun,[ +*** Type (RealDef.real,[]) +*** Type (fun,[ +*** Type (IntDef.int,[]) +*** Type (nat,[]) +*** ] +*** ] +*) + +fun atomt t = + let fun ato (Const(a,T)) n = + ("\n*** "^indent n^"Const ("^a^")") + | ato (Free (a,T)) n = + ("\n*** "^indent n^"Free ("^a^", "^")") + | ato (Var ((a,ix),T)) n = + ("\n*** "^indent n^"Var (("^a^", "^(string_of_int ix)^"), "^")") + | ato (Bound ix) n = + ("\n*** "^indent n^"Bound "^(string_of_int ix)) + | ato (Abs(a,T,body)) n = + ("\n*** "^indent n^"Abs("^a^",..")^ato body (n+1) + | ato (f$t') n = (ato f n; ato t' (n+1)) + in writeln("\n*** -------------"^ ato t 0 ^"\n***") end; + +fun term_detail2str t = + let fun ato (Const (a, T)) n = + "\n*** "^indent n^"Const ("^a^", "^string_of_typ T^")" + | ato (Free (a, T)) n = + "\n*** "^indent n^"Free ("^a^", "^string_of_typ T^")" + | ato (Var ((a, ix), T)) n = + "\n*** "^indent n^"Var (("^a^", "^string_of_int ix^"), "^ + string_of_typ T^")" + | ato (Bound ix) n = + "\n*** "^indent n^"Bound "^string_of_int ix + | ato (Abs(a, T, body)) n = + "\n*** "^indent n^"Abs ("^a^", "^ + (string_of_typ T)^",.." + ^ato body (n + 1) + | ato (f $ t') n = ato f n^ato t' (n+1) + in "\n*** "^ato t 0^"\n***" end; +fun atomty t = (writeln o term_detail2str) t; + +fun term_str thy (Const(s,_)) = s + | term_str thy (Free(s,_)) = s + | term_str thy (Var((s,i),_)) = s^(string_of_int i) + | term_str thy (Bound i) = "B."^(string_of_int i) + | term_str thy (Abs(s,_,_)) = s + | term_str thy t = raise error("term_str not for "^term2str t); + +(*.contains the fst argument the second argument (a leave! of term).*) +fun contains_term (Abs(_,_,body)) t = contains_term body t + | contains_term (f $ f') t = + contains_term f t orelse contains_term f' t + | contains_term s t = t = s; +(*.contains the term a VAR(("*",_),_) ?.*) +fun contains_Var (Abs(_,_,body)) = contains_Var body + | contains_Var (f $ f') = contains_Var f orelse contains_Var f' + | contains_Var (Var _) = true + | contains_Var _ = false; +(* contains_Var (str2term "?z = 3") (*true*); + contains_Var (str2term "z = 3") (*false*); + *) + +(*fun int_of_str str = + let val ss = explode str + val str' = case ss of + "("::s => drop_last s | _ => ss + in case BasisLibrary.Int.fromString (implode str') of + SOME i => SOME i + | NONE => NONE end;*) +fun int_of_str str = + let val ss = explode str + val str' = case ss of + "("::s => drop_last s | _ => ss + in (SOME (Thy_Output.integer (implode str'))) handle _ => NONE end; +(* +> int_of_str "123"; +val it = SOME 123 : int option +> int_of_str "(-123)"; +val it = SOME 123 : int option +> int_of_str "#123"; +val it = NONE : int option +> int_of_str "-123"; +val it = SOME ~123 : int option +*) +fun int_of_str' str = + case int_of_str str of + SOME i => i + | NONE => raise TERM ("int_of_string: no int-string",[]); +val str2int = int_of_str'; + +fun is_numeral str = case int_of_str str of + SOME _ => true + | NONE => false; +val is_no = is_numeral; +fun is_num (Free (s,_)) = if is_numeral s then true else false + | is_num _ = false; +(*> +> is_num ((term_of o the o (parse thy)) "#1"); +val it = true : bool +> is_num ((term_of o the o (parse thy)) "#-1"); +val it = true : bool +> is_num ((term_of o the o (parse thy)) "a123"); +val it = false : bool +*) + +(*fun int_of_Free (Free (intstr, _)) = + (case BasisLibrary.Int.fromString intstr of + SOME i => i + | NONE => raise error ("int_of_Free ( "^ intstr ^", _)")) + | int_of_Free t = raise error ("int_of_Free ( "^ term2str t ^" )");*) +fun int_of_Free (Free (intstr, _)) = (Thy_Output.integer intstr + handle _ => raise error ("int_of_Free ( "^ intstr ^", _)")) + | int_of_Free t = raise error ("int_of_Free ( "^ term2str t ^" )"); + +fun vars t = + let + fun scan vs (Const(s,T)) = vs + | scan vs (t as Free(s,T)) = if is_no s then vs else t::vs + | scan vs (t as Var((s,i),T)) = t::vs + | scan vs (Bound i) = vs + | scan vs (Abs(s,T,t)) = scan vs t + | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2) + in (distinct o (scan [])) t end; + +fun is_Free (Free _) = true + | is_Free _ = false; +fun is_fun_id (Const _) = true + | is_fun_id (Free _) = true + | is_fun_id _ = false; +fun is_f_x (f $ x) = is_fun_id f andalso is_Free x + | is_f_x _ = false; +(* is_f_x (str2term "q_0/2 * L * x") (*false*); + is_f_x (str2term "M_b x") (*true*); + *) +fun vars_str t = + let + fun scan vs (Const(s,T)) = vs + | scan vs (t as Free(s,T)) = if is_no s then vs else s::vs + | scan vs (t as Var((s,i),T)) = (s^"_"^(string_of_int i))::vs + | scan vs (Bound i) = vs + | scan vs (Abs(s,T,t)) = scan vs t + | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2) + in (distinct o (scan [])) t end; + +fun ids2str t = + let + fun scan vs (Const(s,T)) = if is_no s then vs else s::vs + | scan vs (t as Free(s,T)) = if is_no s then vs else s::vs + | scan vs (t as Var((s,i),T)) = (s^"_"^(string_of_int i))::vs + | scan vs (Bound i) = vs + | scan vs (Abs(s,T,t)) = scan (s::vs) t + | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2) + in (distinct o (scan [])) t end; +fun is_bdv str = + case explode str of + "b"::"d"::"v"::_ => true + | _ => false; +fun is_bdv_ (Free (s,_)) = is_bdv s + | is_bdv_ _ = false; + +fun free2str (Free (s,_)) = s + | free2str t = raise error ("free2str not for "^ term2str t); +fun free2int (t as Free (s, _)) = ((str2int s) + handle _ => raise error ("free2int: "^term_detail2str t)) + | free2int t = raise error ("free2int: "^term_detail2str t); + +(*27.8.01: unused*) +fun var2free (t as Const(s,T)) = t + | var2free (t as Free(s,T)) = t + | var2free (Var((s,i),T)) = Free(s,T) + | var2free (t as Bound i) = t + | var2free (Abs(s,T,t)) = Abs(s,T,var2free t) + | var2free (t1 $ t2) = (var2free t1) $ (var2free t2); + +(*27.8.01: doesn't find some subterm ???!???*) +(*2010 Logic.varify !!!*) +fun free2var (t as Const(s,T)) = t + | free2var (t as Free(s,T)) = if is_no s then t else Var((s,0),T) + | free2var (t as Var((s,i),T)) = t + | free2var (t as Bound i) = t + | free2var (Abs(s,T,t)) = Abs(s,T,free2var t) + | free2var (t1 $ t2) = (free2var t1) $ (free2var t2); + + +fun mk_listT T = Type ("List.list", [T]); +fun list_const T = + Const("List.list.Cons", [T, mk_listT T] ---> mk_listT T); +(*28.8.01: TODO: get type from head of list: 1 arg less!!!*) +fun list2isalist T [] = Const("List.list.Nil",mk_listT T) + | list2isalist T (t::ts) = (list_const T) $ t $ (list2isalist T ts); +(* +> val tt = (term_of o the o (parse thy)) "R=(R::real)"; +> val TT = type_of tt; +> val ss = list2isalist TT [tt,tt,tt]; +> cterm_of (sign_of thy) ss; +val it = "[R = R, R = R, R = R]" : cterm *) + +fun isapair2pair (Const ("Pair",_) $ a $ b) = (a,b) + | isapair2pair t = + raise error ("isapair2pair called with "^term2str t); + +val listType = Type ("List.list",[Type ("bool",[])]); +fun isalist2list ls = + let + fun get es (Const("List.list.Cons",_) $ t $ ls) = get (t::es) ls + | get es (Const("List.list.Nil",_)) = es + | get _ t = + raise error ("isalist2list applied to NON-list '"^term2str t^"'") + in (rev o (get [])) ls end; +(* +> val il = str2term "[a=b,c=d,e=f]"; +> val l = isalist2list il; +> (writeln o terms2str) l; +["a = b","c = d","e = f"] + +> val il = str2term "ss___::bool list"; +> val l = isalist2list il; +[Free ("ss___", "bool List.list")] +*) + + +(*review Isabelle2009/src/HOL/Tools/hologic.ML*) +val prop = Type ("prop",[]); (* ~/Diss.99/Integers-Isa/tools.sml*) +val bool = Type ("bool",[]); (* 2002 Integ.int *) +val Trueprop = Const("Trueprop",bool-->prop); +fun mk_prop t = Trueprop $ t; +val true_as_term = Const("True",bool); +val false_as_term = Const("False",bool); +val true_as_cterm = cterm_of HOL true_as_term; +val false_as_cterm = cterm_of HOL false_as_term; + +infixr 5 -->; (*2002 /Pure/term.ML *) +infixr --->; (*2002 /Pure/term.ML *) +fun S --> T = Type("fun",[S,T]); (*2002 /Pure/term.ML *) +val op ---> = foldr (op -->); (*2002 /Pure/term.ML *) +fun list_implies ([], B) = B : term (*2002 /term.ML *) + | list_implies (A::AS, B) = Logic.implies $ A $ list_implies(AS,B); + + + +(** substitution **) + +fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) = (* = thm.ML *) + match_bvs(s, t, if x="" orelse y="" then al + else (x,y)::al) + | match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al)) + | match_bvs(_,_,al) = al; +fun ren_inst(insts,prop,pat,obj) = (* = thm.ML *) + let val ren = match_bvs(pat,obj,[]) + fun renAbs(Abs(x,T,b)) = + Abs(case assoc_string(ren,x) of NONE => x + | SOME(y) => y, T, renAbs(b)) + | renAbs(f$t) = renAbs(f) $ renAbs(t) + | renAbs(t) = t + in subst_vars insts (if null(ren) then prop else renAbs(prop)) end; + + + + + + +fun dest_equals' (Const("op =",_) $ t $ u) = (t,u)(* logic.ML: Const("=="*) + | dest_equals' t = raise TERM("dest_equals'", [t]); +val lhs_ = (fst o dest_equals'); +val rhs_ = (snd o dest_equals'); + +fun is_equality (Const("op =",_) $ t $ u) = true (* logic.ML: Const("=="*) + | is_equality _ = false; +fun mk_equality (t,u) = (Const("op =",[type_of t,type_of u]--->bool) $ t $ u); +fun is_expliceq (Const("op =",_) $ (Free _) $ u) = true + | is_expliceq _ = false; +fun strip_trueprop (Const("Trueprop",_) $ t) = t + | strip_trueprop t = t; +(* | strip_trueprop t = raise TERM("strip_trueprop", [t]); +*) + +(*.(A1==>...An==>B) goes to (A1==>...An==>).*) +fun strip_imp_prems' (Const("==>", T) $ A $ t) = + let fun coll_prems As (Const("==>", _) $ A $ t) = + coll_prems (As $ (Logic.implies $ A)) t + | coll_prems As _ = SOME As + in coll_prems (Logic.implies $ A) t end + | strip_imp_prems' _ = NONE; (* logic.ML: term -> term list*) +(* + val thm = real_mult_div_cancel1; + val prop = (#prop o rep_thm) thm; + atomt prop; +*** ------------- +*** Const ( ==>) +*** . Const ( Trueprop) +*** . . Const ( Not) +*** . . . Const ( op =) +*** . . . . Var ((k, 0), ) +*** . . . . Const ( 0) +*** . Const ( Trueprop) +*** . . Const ( op =) *** ............. + val SOME t = strip_imp_prems' ((#prop o rep_thm) thm); + atomt t; +*** ------------- +*** Const ( ==>) +*** . Const ( Trueprop) +*** . . Const ( Not) +*** . . . Const ( op =) +*** . . . . Var ((k, 0), ) +*** . . . . Const ( 0) + + val thm = real_le_anti_sym; + val prop = (#prop o rep_thm) thm; + atomt prop; +*** ------------- +*** Const ( ==>) +*** . Const ( Trueprop) +*** . . Const ( op <=) +*** . . . Var ((z, 0), ) +*** . . . Var ((w, 0), ) +*** . Const ( ==>) +*** . . Const ( Trueprop) +*** . . . Const ( op <=) +*** . . . . Var ((w, 0), ) +*** . . . . Var ((z, 0), ) +*** . . Const ( Trueprop) +*** . . . Const ( op =) +*** ............. + val SOME t = strip_imp_prems' ((#prop o rep_thm) thm); + atomt t; +*** ------------- +*** Const ( ==>) +*** . Const ( Trueprop) +*** . . Const ( op <=) +*** . . . Var ((z, 0), ) +*** . . . Var ((w, 0), ) +*** . Const ( ==>) +*** . . Const ( Trueprop) +*** . . . Const ( op <=) +*** . . . . Var ((w, 0), ) +*** . . . . Var ((z, 0), ) +*) + +(*. (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch.*) +fun ins_concl (Const("==>", T) $ A $ t) B = Logic.implies $ A $ (ins_concl t B) + | ins_concl (Const("==>", T) $ A ) B = Logic.implies $ A $ B + | ins_concl t B = raise TERM("ins_concl", [t, B]); +(* + val thm = real_le_anti_sym; + val prop = (#prop o rep_thm) thm; + val concl = Logic.strip_imp_concl prop; + val SOME prems = strip_imp_prems' prop; + val prop' = ins_concl prems concl; + prop = prop'; + atomt prop; + atomt prop'; +*) + + +fun vperm (Var _, Var _) = true (*2002 Pure/thm.ML *) + | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t) + | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2) + | vperm (t, u) = (t = u); + +(*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*) +fun mem_term (_, []) = false + | mem_term (t, t'::ts) = t aconv t' orelse mem_term(t,ts); +fun subset_term ([], ys) = true + | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term(xs, ys); +fun eq_set_term (xs, ys) = + xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs)); +(*a total, irreflexive ordering on index names*) +fun xless ((a,i), (b,j): indexname) = i insert_aterm(t,vars) + | Abs (_,_,body) => add_term_vars(body,vars) + | f$t => add_term_vars (f, add_term_vars(t, vars)) + | _ => vars; +fun term_vars t = add_term_vars(t,[]); + + +fun var_perm (t, u) = (*2002 Pure/thm.ML *) + vperm (t, u) andalso eq_set_term (term_vars t, term_vars u); + +(*2002 fun decomp_simp, Pure/thm.ML *) +fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs) + andalso not (is_Var lhs); + + +fun str_of_int n = + if n < 0 then "-"^((string_of_int o abs) n) + else string_of_int n; +(* +> str_of_int 1; +val it = "1" : string > str_of_int ~1; +val it = "-1" : string +*) + + +fun power b 0 = 1 + | power b n = + if n>0 then b*(power b (n-1)) + else raise error ("power "^(str_of_int b)^" "^(str_of_int n)); +(* +> power 2 3; +val it = 8 : int +> power ~2 3; +val it = ~8 : int +> power ~3 2; +val it = 9 : int +> power 3 ~2; +*) +fun gcd 0 b = b + | gcd a b = if a < b then gcd (b mod a) a + else gcd (a mod b) b; +fun sign n = if n < 0 then ~1 + else if n = 0 then 0 else 1; +fun sign2 n1 n2 = (sign n1) * (sign n2); + +infix dvd; +fun d dvd n = n mod d = 0; + +fun divisors n = + let fun pdiv ds d n = + if d=n then d::ds + else if d dvd n then pdiv (d::ds) d (n div d) + else pdiv ds (d+1) n + in pdiv [] 2 n end; + +divisors 30; +divisors 32; +divisors 60; +divisors 11; + +fun doubles ds = (* ds is ordered *) + let fun dbls ds [] = ds + | dbls ds [i] = ds + | dbls ds (i::i'::is) = if i=i' then dbls (i::ds) is + else dbls ds (i'::is) + in dbls [] ds end; +(*> doubles [2,3,4]; +val it = [] : int list +> doubles [2,3,3,5,5,7]; +val it = [5,3] : int list*) + +fun squfact 0 = 0 + | squfact 1 = 1 + | squfact n = foldl op* (1, (doubles o divisors) n); +(*> squfact 30; +val it = 1 : int +> squfact 32; +val it = 4 : int +> squfact 60; +val it = 2 : int +> squfact 11; +val it = 1 : int*) + + +fun dest_type (Type(T,[])) = T + | dest_type T = + (atomtyp T; + raise error ("... dest_type: not impl. for this type")); + +fun term_of_num ntyp n = Free (str_of_int n, ntyp); + +fun pairT T1 T2 = Type ("*", [T1, T2]); +(*> val t = str2term "(1,2)"; +> type_of t = pairT HOLogic.realT HOLogic.realT; +val it = true : bool +*) +fun PairT T1 T2 = ([T1, T2] ---> Type ("*", [T1, T2])); +(*> val t = str2term "(1,2)"; +> val Const ("Pair",pT) $ _ $ _ = t; +> pT = PairT HOLogic.realT HOLogic.realT; +val it = true : bool +*) +fun pairt t1 t2 = + Const ("Pair", PairT (type_of t1) (type_of t2)) $ t1 $ t2; +(*> val t = str2term "(1,2)"; +> val (t1, t2) = (str2term "1", str2term "2"); +> t = pairt t1 t2; +val it = true : bool*) + + +fun num_of_term (t as Free (s,_)) = + (case int_of_str s of + SOME s' => s' + | NONE => raise error ("num_of_term not for "^ term2str t)) + | num_of_term t = raise error ("num_of_term not for "^term2str t); + +fun mk_factroot op_(*=thy.sqrt*) T fact root = + Const ("op *", [T, T] ---> T) $ (term_of_num T fact) $ + (Const (op_, T --> T) $ term_of_num T root); +(* +val T = (type_of o term_of o the) (parse thy "#12::real"); +val t = mk_factroot "SqRoot.sqrt" T 2 3; +cterm_of (sign_of thy) t; +val it = "#2 * sqrt #3 " : cterm +*) +fun var_op_num v op_ optype ntyp n = + Const (op_, optype) $ v $ + Free (str_of_int n, ntyp); + +fun num_op_var v op_ optype ntyp n = + Const (op_,optype) $ + Free (str_of_int n, ntyp) $ v; + +fun num_op_num T1 T2 (op_,Top) n1 n2 = + Const (op_,Top) $ + Free (str_of_int n1, T1) $ Free (str_of_int n2, T2); +(* +> val t = num_op_num "Int" 3 4; +> atomty t; +> string_of_cterm (cterm_of (sign_of thy) t); +*) + +fun const_in str (Const _) = false + | const_in str (Free (s,_)) = if strip_thy s = str then true else false + | const_in str (Bound _) = false + | const_in str (Var _) = false + | const_in str (Abs (_,_,body)) = const_in str body + | const_in str (f$u) = const_in str f orelse const_in str u; +(* +> val t = (term_of o the o (parse thy)) "6 + 5 * sqrt 4 + 3"; +> const_in "sqrt" t; +val it = true : bool +> val t = (term_of o the o (parse thy)) "6 + 5 * 4 + 3"; +> const_in "sqrt" t; +val it = false : bool +*) + +(*used for calculating built in binary operations in Isabelle2002->Float.ML*) +(*fun calc "op +" (n1, n2) = n1+n2 + | calc "op -" (n1, n2) = n1-n2 + | calc "op *" (n1, n2) = n1*n2 + | calc "HOL.divide"(n1, n2) = n1 div n2 + | calc "Atools.pow"(n1, n2) = power n1 n2 + | calc op_ _ = raise error ("calc: operator = "^op_^" not defined");-----*) +fun calc_equ "op <" (n1, n2) = n1 < n2 + | calc_equ "op <=" (n1, n2) = n1 <= n2 + | calc_equ op_ _ = + raise error ("calc_equ: operator = "^op_^" not defined"); +fun sqrt (n:int) = if n < 0 then 0 + (*FIXME ~~~*) else (trunc o Math.sqrt o Real.fromInt) n; + +fun mk_thmid thmid op_ n1 n2 = + thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2); + +fun dest_binop_typ (Type("fun",[range,Type("fun",[arg2,arg1])])) = + (arg1,arg2,range) + | dest_binop_typ _ = raise error "dest_binop_typ: not binary"; +(* ----- +> val t = (term_of o the o (parse thy)) "#3^#4"; +> val hT = type_of (head_of t); +> dest_binop_typ hT; +val it = ("'a","nat","'a") : typ * typ * typ + ----- *) + + +(** transform binary numeralsstrings **) +(*Makarius 100308, hacked by WN*) +val numbers_to_string = + let + fun dest_num t = + (case try HOLogic.dest_number t of + SOME (T, i) => + (*if T = @{typ int} orelse T = @{typ real} then WN*) + SOME (Free (signed_string_of_int i, T)) + (*else NONE WN*) + | NONE => NONE); + + fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b) + | to_str (t as (u1 $ u2)) = + (case dest_num t of + SOME t' => t' + | NONE => to_str u1 $ to_str u2) + | to_str t = perhaps dest_num t; + in to_str end + +(*.make uminus uniform: + Const ("uminus", _) $ Free ("2", "RealDef.real") --> Free ("-2", _) +to be used immediately before evaluation of numerals; +see Scripts/calculate.sml .*) +(*2002 fun(*app_num_tr'2 (Const("0",T)) = Free("0",T) + | app_num_tr'2 (Const("1",T)) = Free("1",T) + |*)app_num_tr'2 (t as Const("uminus",_) $ Free(s,T)) = + (case int_of_str s of Some i => + if i > 0 then Free("-"^s,T) else Free(s,T) + | None => t) +(*| app_num_tr'2 (t as Const(s,T)) = t + | app_num_tr'2 (Const("Numeral.number_of",Type ("fun", [_, T])) $ t) = + Free(NumeralSyntax.dest_bin_str t, T) + | app_num_tr'2 (t as Free(s,T)) = t + | app_num_tr'2 (t as Var(n,T)) = t + | app_num_tr'2 (t as Bound i) = t +*)| app_num_tr'2 (Abs(s,T,body)) = Abs(s,T, app_num_tr'2 body) + | app_num_tr'2 (t1 $ t2) = (app_num_tr'2 t1) $ (app_num_tr'2 t2) + | app_num_tr'2 t = t; +*) +val uminus_to_string = + let + fun dest_num t = + (case t of + (Const ("HOL.uminus_class.uminus", _) $ Free (s, T)) => + (case int_of_str s of + SOME i => + SOME (Free (signed_string_of_int (~1 * i), T)) + | NONE => NONE) + | _ => NONE); + + fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b) + | to_str (t as (u1 $ u2)) = + (case dest_num t of + SOME t' => t' + | NONE => to_str u1 $ to_str u2) + | to_str t = perhaps dest_num t; + in to_str end; + + +(*2002 fun num_str thm = + let + val {sign_ref = sign_ref, der = der, maxidx = maxidx, + shyps = shyps, hyps = hyps, (*tpairs = tpairs,*) prop = prop} = + rep_thm_G thm; + val prop' = app_num_tr'1 prop; + in assbl_thm sign_ref der maxidx shyps hyps (*tpairs*) prop' end;*) +fun num_str thm = + let val (deriv, + {thy_ref = thy_ref, tags = tags, maxidx = maxidx, shyps = shyps, + hyps = hyps, tpairs = tpairs, prop = prop}) = rep_thm_G thm + val prop' = numbers_to_string prop; + in assbl_thm deriv thy_ref tags maxidx shyps hyps tpairs prop' end; + +fun get_thm' xstring = (*?covers 2009 Thm?!, replaces 2002 fun get_thm : +val it = fn : Theory.theory -> xstring -> Thm.thm*) + Thm (xstring, + num_str (ProofContext.get_thm (ctxt_Isac"") xstring)); + +(** get types of Free and Abs for parse' **) +(*11.1.00: not used, fix-typed +,*,-,^ instead *) + +val dummyT = Type ("dummy",[]); +val dummyT = TVar (("DUMMY",0),[]); + +(* assumes only 1 type for numerals + and different identifiers for Const, Free and Abs *) +fun get_types t = + let + fun get ts (Const(s,T)) = (s,T)::ts + | get ts (Free(s,T)) = if is_no s + then ("#",T)::ts else (s,T)::ts + | get ts (Var(n,T)) = ts + | get ts (Bound i) = ts + | get ts (Abs(s,T,body)) = get ((s,T)::ts) body + | get ts (t1 $ t2) = (get ts t1) @ (get ts t2) + in distinct (get [] t) end; +(* +val t = (term_of o the o (parse thy)) "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)"; +get_types t; +*) + +(*11.1.00: not used, fix-typed +,*,-,^ instead *) +fun set_types al (Const(s,T)) = + (case assoc (al,s) of + SOME T' => Const(s,T') + | NONE => (warning ("set_types: no type for "^s); Const(s,dummyT))) + | set_types al (Free(s,T)) = + if is_no s then + (case assoc (al,"#") of + SOME T' => Free(s,T') + | NONE => (warning ("set_types: no type for numerals"); Free(s,T))) + else (case assoc (al,s) of + SOME T' => Free(s,T') + | NONE => (warning ("set_types: no type for "^s); Free(s,T))) + | set_types al (Var(n,T)) = Var(n,T) + | set_types al (Bound i) = Bound i + | set_types al (Abs(s,T,body)) = + (case assoc (al,s) of + SOME T' => Abs(s,T', set_types al body) + | NONE => (warning ("set_types: no type for "^s); + Abs(s,T, set_types al body))) + | set_types al (t1 $ t2) = (set_types al t1) $ (set_types al t2); +(* +val t = (term_of o the o (parse thy)) "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)"; +val al = get_types t; + +val t = (term_of o the o (parse thy)) "x = #0 + #-1 * #-4"; +atomty t; (* 'a *) +val t' = set_types al t; +atomty t'; (*real*) +cterm_of (sign_of thy) t'; +val it = "x = #0 + #-1 * #-4" : cterm + +val t = (term_of o the o (parse thy)) + "#5 * x + x ^^^ #2 = (#2 + x) ^^^ #2"; +atomty t; +val t' = set_types al t; +atomty t'; +cterm_of (sign_of thy) t'; +uncaught exception TYPE (*^^^ is new, NOT in al*) +*) + + +(** from Descript.ML **) + +(** decompose an isa-list to an ML-list + i.e. [] belong to the meta-language, too **) + +fun is_list ((Const("List.list.Cons",_)) $ _ $ _) = true + | is_list _ = false; +(* val (SOME ct) = parse thy "lll::real list"; +> val ty = (#t o rep_cterm) ct; +> is_list ty; +val it = false : bool +> val (SOME ct) = parse thy "[lll]"; +> val ty = (#t o rep_cterm) ct; +> is_list ty; +val it = true : bool *) + + + +fun mk_Free (s,T) = Free(s,T); +fun mk_free T s = Free(s,T); + +(*instantiate let; necessary for ass_up*) +fun inst_abs thy (Const sT) = Const sT + | inst_abs thy (Free sT) = Free sT + | inst_abs thy (Bound n) = Bound n + | inst_abs thy (Var iT) = Var iT + | inst_abs thy (Const ("Let",T1) $ e $ (Abs (v,T2,b))) = + let val (v',b') = variant_abs (v,T2,b); (*fun variant_abs: term.ML*) + in Const ("Let",T1) $ inst_abs thy e $ (Abs (v',T2,inst_abs thy b')) end + | inst_abs thy (t1 $ t2) = inst_abs thy t1 $ inst_abs thy t2 + | inst_abs thy t = + (writeln("inst_abs: unchanged t= "^ term2str t); + t); +(*val scr as (Script sc) = Script ((term_of o the o (parse thy)) + "Script Testeq (e_::bool) = \ + \While (contains_root e_) Do \ + \ (let e_ = Try (Repeat (Rewrite rroot_square_inv False e_)); \ + \ e_ = Try (Repeat (Rewrite square_equation_left True e_)) \ + \ in Try (Repeat (Rewrite radd_0 False e_))) "); +ML> atomt sc; +*** Const ( Script.Testeq) +*** . Free ( e_, ) +*** . Const ( Script.While) +*** . . Const ( RatArith.contains'_root) +*** . . . Free ( e_, ) +*** . . Const ( Let) +*** . . . Const ( Script.Try) +*** . . . . Const ( Script.Repeat) +*** . . . . . Const ( Script.Rewrite) +*** . . . . . . Free ( rroot_square_inv, ) +*** . . . . . . Const ( False) +*** . . . . . . Free ( e_, ) +*** . . . Abs( e_,.. +*** . . . . Const ( Let) +*** . . . . . Const ( Script.Try) +*** . . . . . . Const ( Script.Repeat) +*** . . . . . . . Const ( Script.Rewrite) +*** . . . . . . . . Free ( square_equation_left, ) +*** . . . . . . . . Const ( True) +*** . . . . . . . . Bound 0 <-- !!! +*** . . . . . Abs( e_,.. +*** . . . . . . Const ( Script.Try) +*** . . . . . . . Const ( Script.Repeat) +*** . . . . . . . . Const ( Script.Rewrite) +*** . . . . . . . . . Free ( radd_0, ) +*** . . . . . . . . . Const ( False) +*** . . . . . . . . . Bound 0 <-- !!! +val it = () : unit +ML> atomt (inst_abs thy sc); +*** Const ( Script.Testeq) +*** . Free ( e_, ) +*** . Const ( Script.While) +*** . . Const ( RatArith.contains'_root) +*** . . . Free ( e_, ) +*** . . Const ( Let) +*** . . . Const ( Script.Try) +*** . . . . Const ( Script.Repeat) +*** . . . . . Const ( Script.Rewrite) +*** . . . . . . Free ( rroot_square_inv, ) +*** . . . . . . Const ( False) +*** . . . . . . Free ( e_, ) +*** . . . Abs( e_,.. +*** . . . . Const ( Let) +*** . . . . . Const ( Script.Try) +*** . . . . . . Const ( Script.Repeat) +*** . . . . . . . Const ( Script.Rewrite) +*** . . . . . . . . Free ( square_equation_left, ) +*** . . . . . . . . Const ( True) +*** . . . . . . . . Free ( e_, ) <-- !!! +*** . . . . . Abs( e_,.. +*** . . . . . . Const ( Script.Try) +*** . . . . . . . Const ( Script.Repeat) +*** . . . . . . . . Const ( Script.Rewrite) +*** . . . . . . . . . Free ( radd_0, ) +*** . . . . . . . . . Const ( False) +*** . . . . . . . . . Free ( e_, ) <-- ZUFALL vor 5.03!!! +val it = () : unit*) + + + + +fun inst_abs thy (Const sT) = Const sT + | inst_abs thy (Free sT) = Free sT + | inst_abs thy (Bound n) = Bound n + | inst_abs thy (Var iT) = Var iT + | inst_abs thy (Const ("Let",T1) $ e $ (Abs (v,T2,b))) = + let val b' = subst_bound (Free(v,T2),b); + (*fun variant_abs: term.ML*) + in Const ("Let",T1) $ inst_abs thy e $ (Abs (v,T2,inst_abs thy b')) end + | inst_abs thy (t1 $ t2) = inst_abs thy t1 $ inst_abs thy t2 + | inst_abs thy t = + (writeln("inst_abs: unchanged t= "^ term2str t); + t); +(*val scr = + "Script Make_fun_by_explicit (f_::real) (v_::real) (eqs_::bool list) = \ + \ (let h_ = (hd o (filterVar f_)) eqs_; \ + \ e_1 = hd (dropWhile (ident h_) eqs_); \ + \ vs_ = dropWhile (ident f_) (Vars h_); \ + \ v_1 = hd (dropWhile (ident v_) vs_); \ + \ (s_1::bool list)=(SubProblem(DiffApp_,[univar,equation],[no_met])\ + \ [bool_ e_1, real_ v_1])\ + \ in Substitute [(v_1 = (rhs o hd) s_1)] h_)"; +> val ttt = (term_of o the o (parse thy)) scr; +> writeln(term2str ttt); +> atomt ttt; +*** ------------- +*** Const ( DiffApp.Make'_fun'_by'_explicit) +*** . Free ( f_, ) +*** . Free ( v_, ) +*** . Free ( eqs_, ) +*** . Const ( Let) +*** . . Const ( Fun.op o) +*** . . . Const ( List.hd) +*** . . . Const ( DiffApp.filterVar) +*** . . . . Free ( f_, ) +*** . . . Free ( eqs_, ) +*** . . Abs( h_,.. +*** . . . Const ( Let) +*** . . . . Const ( List.hd) +*** . . . . . Const ( List.dropWhile) +*** . . . . . . Const ( Atools.ident) +*** . . . . . . . Bound 0 <---- Free ( h_, ) +*** . . . . . . Free ( eqs_, ) +*** . . . . Abs( e_1,.. +*** . . . . . Const ( Let) +*** . . . . . . Const ( List.dropWhile) +*** . . . . . . . Const ( Atools.ident) +*** . . . . . . . . Free ( f_, ) +*** . . . . . . . Const ( Tools.Vars) +*** . . . . . . . . Bound 1 <---- Free ( h_, ) +*** . . . . . . Abs( vs_,.. +*** . . . . . . . Const ( Let) +*** . . . . . . . . Const ( List.hd) +*** . . . . . . . . . Const ( List.dropWhile) +*** . . . . . . . . . . Const ( Atools.ident) +*** . . . . . . . . . . . Free ( v_, ) +*** . . . . . . . . . . Bound 0 <---- Free ( vs_, ) +*** . . . . . . . . Abs( v_1,.. +*** . . . . . . . . . Const ( Let) +*** . . . . . . . . . . Const ( Script.SubProblem) +*** . . . . . . . . . . . Const ( Pair) +*** . . . . . . . . . . . . Free ( DiffApp_, ) +*** . . . . . . . . . . . . Const ( Pair) +*** . . . . . . . . . . . . . Const ( List.list.Cons) +*** . . . . . . . . . . . . . . Free ( univar, ) +*** . . . . . . . . . . . . . . Const ( List.list.Cons) +*** . . . . . . . . . . . . . . . Free ( equation, ) +*** . . . . . . . . . . . . . . . Const ( List.list.Nil) +*** . . . . . . . . . . . . . Const ( List.list.Cons) +*** . . . . . . . . . . . . . . Free ( no_met, ) +*** . . . . . . . . . . . . . . Const ( List.list.Nil) +*** . . . . . . . . . . . Const ( List.list.Cons) +*** . . . . . . . . . . . . Const ( Script.bool_) +*** . . . . . . . . . . . . . Bound 2 <----- Free ( e_1, ) +*** . . . . . . . . . . . . Const ( List.list.Cons) +*** . . . . . . . . . . . . . Const ( Script.real_) +*** . . . . . . . . . . . . . . Bound 0 <----- Free ( v_1, ) +*** . . . . . . . . . . . . . Const ( List.list.Nil) +*** . . . . . . . . . . Abs( s_1,.. +*** . . . . . . . . . . . Const ( Script.Substitute) +*** . . . . . . . . . . . . Const ( List.list.Cons) +*** . . . . . . . . . . . . . Const ( Pair) +*** . . . . . . . . . . . . . . Bound 1 <----- Free ( v_1, ) +*** . . . . . . . . . . . . . . Const ( Fun.op o) +*** . . . . . . . . . . . . . . . Const ( Tools.rhs) +*** . . . . . . . . . . . . . . . Const ( List.hd) +*** . . . . . . . . . . . . . . . Bound 0 <----- Free ( s_1, ) +*** . . . . . . . . . . . . . Const ( List.list.Nil) +*** . . . . . . . . . . . . Bound 4 <----- Free ( h_, ) + +> val ttt' = inst_abs thy ttt; +> writeln(term2str ttt'); +Script Make_fun_by_explicit f_ v_ eqs_ = + ... as above ... +> atomt ttt'; +*** ------------- +*** Const ( DiffApp.Make'_fun'_by'_explicit) +*** . Free ( f_, ) +*** . Free ( v_, ) +*** . Free ( eqs_, ) +*** . Const ( Let) +*** . . Const ( Fun.op o) +*** . . . Const ( List.hd) +*** . . . Const ( DiffApp.filterVar) +*** . . . . Free ( f_, ) +*** . . . Free ( eqs_, ) +*** . . Abs( h_,.. +*** . . . Const ( Let) +*** . . . . Const ( List.hd) +*** . . . . . Const ( List.dropWhile) +*** . . . . . . Const ( Atools.ident) +*** . . . . . . . Free ( h_, ) <---- Bound 0 +*** . . . . . . Free ( eqs_, ) +*** . . . . Abs( e_1,.. +*** . . . . . Const ( Let) +*** . . . . . . Const ( List.dropWhile) +*** . . . . . . . Const ( Atools.ident) +*** . . . . . . . . Free ( f_, ) +*** . . . . . . . Const ( Tools.Vars) +*** . . . . . . . . Free ( h_, ) <---- Bound 1 +*** . . . . . . Abs( vs_,.. +*** . . . . . . . Const ( Let) +*** . . . . . . . . Const ( List.hd) +*** . . . . . . . . . Const ( List.dropWhile) +*** . . . . . . . . . . Const ( Atools.ident) +*** . . . . . . . . . . . Free ( v_, ) +*** . . . . . . . . . . Free ( vs_, ) <---- Bound 0 +*** . . . . . . . . Abs( v_1,.. +*** . . . . . . . . . Const ( Let) +*** . . . . . . . . . . Const ( Script.SubProblem) +*** . . . . . . . . . . . Const ( Pair) +*** . . . . . . . . . . . . Free ( DiffApp_, ) +*** . . . . . . . . . . . . Const ( Pair) +*** . . . . . . . . . . . . . Const ( List.list.Cons) +*** . . . . . . . . . . . . . . Free ( univar, ) +*** . . . . . . . . . . . . . . Const ( List.list.Cons) +*** . . . . . . . . . . . . . . . Free ( equation, ) +*** . . . . . . . . . . . . . . . Const ( List.list.Nil) +*** . . . . . . . . . . . . . Const ( List.list.Cons) +*** . . . . . . . . . . . . . . Free ( no_met, ) +*** . . . . . . . . . . . . . . Const ( List.list.Nil) +*** . . . . . . . . . . . Const ( List.list.Cons) +*** . . . . . . . . . . . . Const ( Script.bool_) +*** . . . . . . . . . . . . . Free ( e_1, ) <----- Bound 2 +*** . . . . . . . . . . . . Const ( List.list.Cons) +*** . . . . . . . . . . . . . Const ( Script.real_) +*** . . . . . . . . . . . . . . Free ( v_1, ) <----- Bound 0 +*** . . . . . . . . . . . . . Const ( List.list.Nil) +*** . . . . . . . . . . Abs( s_1,.. +*** . . . . . . . . . . . Const ( Script.Substitute) +*** . . . . . . . . . . . . Const ( List.list.Cons) +*** . . . . . . . . . . . . . Const ( Pair) +*** . . . . . . . . . . . . . . Free ( v_1, ) <----- Bound 1 +*** . . . . . . . . . . . . . . Const ( Fun.op o) +*** . . . . . . . . . . . . . . . Const ( Tools.rhs) +*** . . . . . . . . . . . . . . . Const ( List.hd) +*** . . . . . . . . . . . . . . . Free ( s_1, ) <----- Bound 0 +*** . . . . . . . . . . . . . Const ( List.list.Nil) +*** . . . . . . . . . . . . Free ( h_, ) <----- Bound 4 + +Note numbering of de Bruijn indexes ! + +Script Make_fun_by_explicit f_ v_ eqs_ = + let h_ = (hd o filterVar f_) eqs_; + e_1 = hd (dropWhile (ident h_ BOUND_0) eqs_); + vs_ = dropWhile (ident f_) (Vars h_ BOUND_1); + v_1 = hd (dropWhile (ident v_) vs_ BOUND_0); + s_1 = + SubProblem (DiffApp_, [univar, equation], [no_met]) + [bool_ e_1 BOUND_2, real_ v_1 BOUND_0] + in Substitute [(v_1 BOUND_1 = (rhs o hd) s_1 BOUND_0)] h_ BOUND_4 +*) + + +fun T_a2real (Type (s, [])) = + if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else Type (s, []) + | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts) + | T_a2real (TFree (s, srt)) = + if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TFree (s, srt) + | T_a2real (TVar (("DUMMY",_),srt)) = HOLogic.realT; + +(*FIXME .. fixes the type (+see Typefix.thy*) +fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T)) + | typ_a2real (Free( s, T)) = (Free( s, T_a2real T)) + | typ_a2real (Var( n, T)) = (Var( n, T_a2real T)) + | typ_a2real (Bound i) = (Bound i) + | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t) + | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2); +(* +----------------6.8.02--------------------------------------------------- + val str = "1"; + val t = read_cterm (sign_of thy) (str,(TVar(("DUMMY",0),[]))); + atomty (term_of t); +*** ------------- +*** Const ( 1, 'a) + val t = (app_num_tr' o term_of) t; + atomty t; +*** ------------- +*** Const ( 1, 'a) + val t = typ_a2real t; + atomty t; +*** ------------- +*** Const ( 1, real) + + val str = "2"; + val t = read_cterm (sign_of thy) (str,(TVar(("DUMMY",0),[]))); + atomty (term_of t); +*** ------------- +*** Const ( Numeral.number_of, bin => 'a) +*** . Const ( Numeral.bin.Bit, [bin, bool] => bin) +*** . . Const ( Numeral.bin.Bit, [bin, bool] => bin) +*** . . . Const ( Numeral.bin.Pls, bin) +*** . . . Const ( True, bool) +*** . . Const ( False, bool) + val t = (app_num_tr' o term_of) t; + atomty t; +*** ------------- +*** Free ( 2, 'a) + val t = typ_a2real t; + atomty t; +*** ------------- +*** Free ( 2, real) +----------------6.8.02--------------------------------------------------- + + +> val str = "R"; +> val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[])))); +val t = Free ("R","?DUMMY") : term +> val t' = typ_a2real t; +> cterm_of (sign_of thy) t'; +val it = "R::RealDef.real" : cterm + +> val str = "R=R"; +> val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[])))); +> atomty (typ_a2real t); +*** ------------- +*** Const ( op =, [RealDef.real, RealDef.real] => bool) +*** Free ( R, RealDef.real) +*** Free ( R, RealDef.real) +> val t' = typ_a2real t; +> cterm_of (sign_of thy) t'; +val it = "(R::RealDef.real) = R" : cterm + +> val str = "fixed_values [R=R]"; +> val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[])))); +> val t' = typ_a2real t; +> cterm_of (sign_of thy) t'; +val it = "fixed_values [(R::RealDef.real) = R]" : cterm +*) + +(*TODO.WN0609: parse should return a term or a string + (or even more comprehensive datastructure for error-messages) + i.e. in wrapping with SOME term or NONE the latter is not sufficient*) +(*2002 fun parseold thy str = + (let + val sgn = sign_of thy; + val t = ((*typ_a2real o*) app_num_tr'1 o term_of) + (read_cterm sgn (str,(TVar(("DUMMY",0),[])))); + in SOME (cterm_of sgn t) end) + handle _ => NONE;*) + + + +fun parseold thy str = + (let val t = ((*typ_a2real o*) numbers_to_string) + (Syntax.read_term_global thy str) + in SOME (cterm_of thy t) end) + handle _ => NONE; +(*2002 fun parseN thy str = + (let + val sgn = sign_of thy; + val t = ((*typ_a2real o app_num_tr'1 o*) term_of) + (read_cterm sgn (str,(TVar(("DUMMY",0),[])))); + in SOME (cterm_of sgn t) end) + handle _ => NONE;*) +fun parseN thy str = + (let val t = (*(typ_a2real o numbers_to_string)*) + (Syntax.read_term_global thy str) + in SOME (cterm_of thy t) end) + handle _ => NONE; +(*2002 fun parse thy str = + (let + val sgn = sign_of thy; + val t = (typ_a2real o app_num_tr'1 o term_of) + (read_cterm sgn (str,(TVar(("DUMMY",0),[])))); + in SOME (cterm_of sgn t) end) (*FIXXXXME 10.8.02: return term !!!*) + handle _ => NONE;*) +(*2010 fun parse thy str = + (let val t = (typ_a2real o app_num_tr'1) (Syntax.read_term_global thy str) + in SOME (cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*) + handle _ => NONE;*) +fun parse thy str = + (let val t = (typ_a2real o numbers_to_string) + (Syntax.read_term_global thy str) + in SOME (cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*) + handle _ => NONE; + +(* 10.8.02: for this reason we still have ^^^-------------------- + val thy = SqRoot.thy; + val str = "(1::real) ^ (2::nat)"; + val sgn = sign_of thy; + val ct = (read_cterm sgn (str,(TVar(("DUMMY",0),[])))) handle e =>print_exn e; +(*1*)"(1::real) ^ 2"; + atomty (term_of ct); +*** ------------- +*** Const ( Nat.power, [real, nat] => real) +*** . Const ( 1, real) +*** . Const ( Numeral.number_of, bin => nat) +*** . . Const ( Numeral.bin.Bit, [bin, bool] => bin) +*** . . . Const ( Numeral.bin.Bit, [bin, bool] => bin) +*** . . . . Const ( Numeral.bin.Pls, bin) +*** . . . . Const ( True, bool) +*** . . . Const ( False, bool) + val t = ((app_num_tr' o term_of) + (read_cterm sgn (str,(TVar(("DUMMY",0),[])))))handle e => print_exn e; + val ct = (cterm_of sgn t) handle e => print_exn e; +(*2*)"(1::real) ^ (2::nat)"; + atomty (term_of ct); +*** ------------- +*** Const ( Nat.power, [real, nat] => real) +*** . Free ( 1, real) +*** . Free ( 2, nat) (*1*) Const("2",_) (*2*) Free("2",_) + + + val str = "(2::real) ^ (2::nat)"; + val t = (read_cterm sgn (str,(TVar(("DUMMY",0),[])))) handle e => print_exn e; +val t = "(2::real) ^ 2" : cterm + val t = ((app_num_tr' o term_of) + (read_cterm sgn (str,(TVar(("DUMMY",0),[])))))handle e => print_exn e; + val ct = (cterm_of sgn t) handle e => print_exn e; +Variable "2" has two distinct types +real +nat +uncaught exception TYPE + raised at: sign.ML:672.26-673.56 + goals.ML:1100.61 + + + val str = "(3::real) ^ (2::nat)"; + val t = (read_cterm sgn (str,(TVar(("DUMMY",0),[])))) handle e => print_exn e; +val t = "(3::real) ^ 2" : cterm + val t = ((app_num_tr' o term_of) + (read_cterm sgn (str,(TVar(("DUMMY",0),[])))))handle e => print_exn e; + val ct = (cterm_of sgn t) handle e => print_exn e; +val ct = "(3::real) ^ (2::nat)" : cterm + + +Conclusion: The type inference allows different types + for one and the same Numeral.number_of + BUT the type inference doesn't allow + Free ( 2, real) and Free ( 2, nat) within one term +--------------- ~~~~ ~~~ *) +(* +> val (SOME ct) = parse thy "(-#5)^^^#3"; +> atomty (term_of ct); +*** ------------- +*** Const ( Nat.op ^, ['a, nat] => 'a) +*** Const ( uminus, 'a => 'a) +*** Free ( #5, 'a) +*** Free ( #3, nat) +> val (SOME ct) = parse thy "R=R"; +> atomty (term_of ct); +*** ------------- +*** Const ( op =, [real, real] => bool) +*** Free ( R, real) +*** Free ( R, real) + +THIS IS THE OUTPUT FOR VERSION (3) above at typ_a2real !!!!! +*** ------------- +*** Const ( op =, [RealDef.real, RealDef.real] => bool) +*** Free ( R, RealDef.real) +*** Free ( R, RealDef.real) *) + +(*version for testing local to theories*) +fun str2t thy str = (term_of o the o (parse thy )) str; + +fun str2term str = (term_of o the o (parse (assoc_thy "Isac.thy"))) str; +fun str2termN str = (term_of o the o (parseN (assoc_thy "Isac.thy"))) str; +fun strs2terms ss = map str2term ss; + +(*+ makes a substitution from the output of Pattern.match +*) +(*fun mk_subs ((id, _):indexname, t:term) = (Free (id,type_of t), t);*) +fun mk_subs (subs: ((string * int) * (Term.typ * Term.term)) list) = +let fun mk_sub ((id, _), (ty, tm)) = (Free (id, ty), tm) in +map mk_sub subs end; + +val atomthm = atomt o #prop o rep_thm; + +(*.instantiate #prop thm with bound variables (as Free).*) +fun inst_bdv [] t = t : term + | inst_bdv (instl: (term*term) list) t = + let fun subst (v as Var((s,_),T)) = + (case explode s of + "b"::"d"::"v"::_ => + if_none (assoc(instl,Free(s,T))) (Free(s,T)) + | _ => v) + | subst (Abs(a,T,body)) = Abs(a, T, subst body) + | subst (f$t') = subst f $ subst t' + | subst t = if_none (assoc(instl,t)) t + in subst t end; + + +(*WN050829 caution: is_atom (str2term"q_0/2 * L * x") = true !!! + use length (vars term) = 1 instead*) +fun is_atom (Const ("Float.Float",_) $ _) = true + | is_atom (Const ("ComplexI.I'_'_",_)) = true + | is_atom (Const ("op *",_) $ t $ Const ("ComplexI.I'_'_",_)) = is_atom t + | is_atom (Const ("op +",_) $ t1 $ Const ("ComplexI.I'_'_",_)) = is_atom t1 + | is_atom (Const ("op +",_) $ t1 $ + (Const ("op *",_) $ t2 $ Const ("ComplexI.I'_'_",_))) = + is_atom t1 andalso is_atom t2 + | is_atom (Const _) = true + | is_atom (Free _) = true + | is_atom (Var _) = true + | is_atom _ = false; +(* val t = str2term "q_0/2 * L * x"; + + +*) +(*val t = str2term "Float ((1,2),(0,0))"; +> is_atom t; +val it = true : bool +> val t = str2term "Float ((1,2),(0,0)) * I__"; +> is_atom t; +val it = true : bool +> val t = str2term "Float ((1,2),(0,0)) + Float ((3,4),(0,0)) * I__"; +> is_atom t; +val it = true : bool +> val t = str2term "1 + 2*I__"; +> val Const ("op +",_) $ t1 $ (Const ("op *",_) $ t2 $ Const ("ComplexI.I'_'_",_)) = t; +*) + +(*.adaption from Isabelle/src/Pure/term.ML; reports if ALL Free's + have found a substitution (required for evaluating the preconditions + of _incomplete_ models).*) +fun subst_atomic_all [] t = (false, (*TODO may be 'true' for some terms ?*) + t : term) + | subst_atomic_all (instl: (term*term) list) t = + let fun subst (Abs(a,T,body)) = + let val (all, body') = subst body + in (all, Abs(a, T, body')) end + | subst (f$tt) = + let val (all1, f') = subst f + val (all2, tt') = subst tt + in (all1 andalso all2, f' $ tt') end + | subst (t as Free _) = + if is_num t then (true, t) (*numerals cannot be subst*) + else (case assoc(instl,t) of + SOME t' => (true, t') + | NONE => (false, t)) + | subst t = (true, if_none (assoc(instl,t)) t) + in subst t end; + +(*.add two terms with a type given.*) +fun mk_add t1 t2 = + let val T1 = type_of t1 + val T2 = type_of t2 + in if T1 <> T2 then raise TYPE ("mk_add gets ",[T1, T2],[t1,t2]) + else (Const ("op +", [T1, T2] ---> T1) $ t1 $ t2) + end; +