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