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