1 (* tools which depend on Script.thy and thus are not in term.sml
2 (c) Walther Neuper 2000
4 use"ProgLang/scrtools.sml";
10 (Const(_,Type("fun",[Type("List.list",
11 [Type ("real",[])]),_]))) = true
13 (Const(_,Type("fun",[Type("List.list",
14 [Type ("real",[])]),_])) $ t) = true
15 | is_reall_dsc _ = false;
17 (Const(_,Type("fun",[Type("List.list",
18 [Type ("bool",[])]),_]))) = true
20 (Const(_,Type("fun",[Type("List.list",
21 [Type ("bool",[])]),_])) $ t) = true
22 | is_booll_dsc _ = false;
24 > val t = (term_of o the o (parse thy)) "relations";
25 > atomtyp (type_of t);
30 *** Type (Tools.una,[])
38 fun is_list_dsc (Const(_,Type("fun",[Type("List.list",_),_]))) = true
39 | is_list_dsc (Const(_,Type("fun",[Type("List.list",_),_])) $ t) = true
40 (*WN:8.5.03: ??? ~~~~ ???*)
41 | is_list_dsc _ = false;
43 > val t = str2term "someList";
47 > val t = (term_of o the o (parse thy))
48 "additional_relations [a=b,c=(d::real)]";
51 > is_list_dsc (head_of t);
54 > val t = (term_of o the o (parse thy))"max_relation (A=#2*a*b-a^^^#2)";
57 > is_list_dsc (head_of t);
59 > val t = (term_of o the o (parse thy)) "testdscforlist";
60 > is_list_dsc (head_of t);
65 fun is_unl (Const(_,Type("fun",[_,Type("Tools.unl",_)]))) = true
68 > val t = str2term "someList"; is_unl t;
70 > val t = (term_of o the o (parse thy)) "maximum";
75 fun is_dsc (Const(_,Type("fun",[_,Type("Tools.nam",_)]))) = true
76 | is_dsc (Const(_,Type("fun",[_,Type("Tools.una",_)]))) = true
77 | is_dsc (Const(_,Type("fun",[_,Type("Tools.unl",_)]))) = true
78 | is_dsc (Const(_,Type("fun",[_,Type("Tools.str",_)]))) = true
79 | is_dsc (Const(_,Type("fun",[_,Type("Tools.toreal",_)]))) = true
80 | is_dsc (Const(_,Type("fun",[_,Type("Tools.toreall",_)])))= true
81 | is_dsc (Const(_,Type("fun",[_,Type("Tools.tobooll",_)])))= true
82 | is_dsc (Const(_,Type("fun",[_,Type("Tools.unknow",_)])))= true
83 | is_dsc (Const(_,Type("fun",[_,Type("Tools.cpy",_)])))= true
86 (case (range_type o type_of) term of
87 Type("Tools.nam",_) => true
88 | Type("Tools.una",_) => true
89 | Type("Tools.unl",_) => true
90 | Type("Tools.str",_) => true
91 | Type("Tools.toreal",_) => true
92 | Type("Tools.toreall",_) => true
93 | Type("Tools.tobooll",_) => true
94 | Type("Tools.unknow",_) => true
95 | Type("Tools.cpy",_) => true
97 handle Match => false;
101 val t as t1 $ t2 = str2term "antiDerivativeName M_b";
102 val Const (_, Type ("fun", [Type ("fun", _), Type ("Tools.una",[])])) $ _ = t;
105 > val t = (term_of o the o (parse thy)) "maximum";
108 > val t = (term_of o the o (parse thy)) "testdscforlist";
112 > val t = (head_of o term_of o the o (parse thy)) "maximum A";
115 > val t = (head_of o term_of o the o (parse thy))
116 "fixedValues [R=(R::real)]";
122 (*make the term 'Subproblem (domID, pblID)' to a formula for frontend;
123 needs to be here after def. Subproblem in Script.thy*)
124 val t as (subpbl_t $ (pair_t $ Free (domID,_) $ pblID)) =
125 (term_of o the o (parse @{theory Script}))
126 "Subproblem (Isac,[equation,univar])";
127 val t as (pbl_t $ _) =
128 (term_of o the o (parse @{theory Script}))
129 "Problem (Isac,[equation,univar])";
130 val Free (_, ID_type) = (term_of o the o (parse @{theory Script})) "x::ID";
133 fun subpbl domID pblID =
134 subpbl_t $ (pair_t $ Free (domID,ID_type) $
135 (((list2isalist ID_type) o (map (mk_free ID_type))) pblID));
136 (*> subpbl "Isac" ["equation","univar"] = t;
137 val it = true : bool *)
140 fun pblterm (domID:domID) (pblID:pblID) =
141 pbl_t $ (pair_t $ Free (domID,ID_type) $
142 (((list2isalist ID_type) o (map (mk_free ID_type))) pblID));
145 (**.construct scr-env from scr(created automatically) and Rewrite_Set.**)
147 fun one_scr_arg (Const _ $ arg $ _) = arg
148 | one_scr_arg t = raise error ("one_scr_arg: called by "^(term2str t));
149 fun two_scr_arg (Const _ $ a1 $ a2 $ _) = (a1, a2)
150 | two_scr_arg t = raise error ("two_scr_arg: called by "^(term2str t));
153 (**.generate calc from a script.**)
155 (*.instantiate a stactic or scriptexpr, and ev. attach (curried) argument
158 v current value, is attached to curried stactics
159 stac stactic to be instantiated
161 not (a = NONE) /\ (v = e_term) /\ (stac curried, i.e. without last arg.)
162 this ........................ is the initialization for assy with l=[],
164 (a) curried: then (a = SOME _), or
165 (b) not curried: then the values of the initialization are not used
167 datatype stacexpr = STac of term | Expr of term
168 fun rep_stacexpr (STac t ) = t
169 | rep_stacexpr (Expr t) =
170 raise error ("rep_stacexpr called with t= "^(term2str t));
172 type env = (term * term) list;
174 (*update environment; t <> empty if coming from listexpr*)
175 fun upd_env (env:env) (v,t) =
176 let val env' = if t = e_term then env else overwrite (env,(v,t));
177 (*val _= writeln("### upd_env: = "^(subst2str env'));*)
180 (*.substitute the scripts environment in a leaf of the scripts parse-tree
181 and attach the curried argument of a tactic, if any.
182 a leaf is either a tactic or an 'exp' in 'let v = expr'
183 where 'exp' does not contain a tactic.
184 CAUTION: (1) currying with @@ requires 2 patterns for each tactic
185 (2) the non-curried version must return NONE for a
186 (3) non-matching patterns become an Expr by fall-through.
187 WN060906 quick and dirty fix: due to (2) a is returned, too.*)
188 fun subst_stacexpr E a v (t as (Const ("Script.Rewrite",_) $ _ $ _ $ _ ))=
189 (NONE, STac (subst_atomic E t))
191 | subst_stacexpr E a v (t as (Const ("Script.Rewrite",_) $ _ $ _ ))=
192 (a, (*in these cases we hope, that a = SOME _*)
193 STac (case a of SOME a' => (subst_atomic E (t $ a'))
194 | NONE => ((subst_atomic E t) $ v)))
196 | subst_stacexpr E a v
197 (t as (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ $ _ )) =
198 (NONE, STac (subst_atomic E t))
200 | subst_stacexpr E a v (t as (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _))=
201 (a, STac (case a of SOME a' => subst_atomic E (t $ a')
202 | NONE => ((subst_atomic E t) $ v)))
204 | subst_stacexpr E a v (t as (Const ("Script.Rewrite'_Set",_) $ _ $ _ $ _ ))=
205 (NONE, STac (subst_atomic E t))
207 | subst_stacexpr E a v (t as (Const ("Script.Rewrite'_Set",_) $ _ $ _ )) =
208 (a, STac (case a of SOME a' => subst_atomic E (t $ a')
209 | NONE => ((subst_atomic E t) $ v)))
211 | subst_stacexpr E a v
212 (t as (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ $ _ )) =
213 (NONE, STac (subst_atomic E t))
215 | subst_stacexpr E a v
216 (t as (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ )) =
217 (a, STac (case a of SOME a' => subst_atomic E (t $ a')
218 | NONE => ((subst_atomic E t) $ v)))
220 | subst_stacexpr E a v (t as (Const ("Script.Calculate",_) $ _ $ _ )) =
221 (NONE, STac (subst_atomic E t))
223 | subst_stacexpr E a v (t as (Const ("Script.Calculate",_) $ _ )) =
224 (a, STac (case a of SOME a' => subst_atomic E (t $ a')
225 | NONE => ((subst_atomic E t) $ v)))
227 | subst_stacexpr E a v
228 (t as (Const("Script.Check'_elementwise",_) $ _ $ _ )) =
229 (NONE, STac (subst_atomic E t))
231 | subst_stacexpr E a v (t as (Const("Script.Check'_elementwise",_) $ _ )) =
232 (a, STac (case a of SOME a' => subst_atomic E (t $ a')
233 | NONE => ((subst_atomic E t) $ v)))
235 | subst_stacexpr E a v (t as (Const("Script.Or'_to'_List",_) $ _ )) =
236 (NONE, STac (subst_atomic E t))
238 | subst_stacexpr E a v (t as (Const("Script.Or'_to'_List",_))) = (*t $ v*)
239 (a, STac (case a of SOME a' => subst_atomic E (t $ a')
240 | NONE => ((subst_atomic E t) $ v)))
242 | subst_stacexpr E a v (t as (Const ("Script.SubProblem",_) $ _ $ _ )) =
243 (NONE, STac (subst_atomic E t))
245 | subst_stacexpr E a v (t as (Const ("Script.Take",_) $ _ )) =
246 (NONE, STac (subst_atomic E t))
248 | subst_stacexpr E a v (t as (Const ("Script.Substitute",_) $ _ $ _ )) =
249 (NONE, STac (subst_atomic E t))
251 | subst_stacexpr E a v (t as (Const ("Script.Substitute",_) $ _ )) =
252 (a, STac (case a of SOME a' => subst_atomic E (t $ a')
253 | NONE => ((subst_atomic E t) $ v)))
255 (*now all tactics are matched out and this leaf must be without a tactic*)
256 | subst_stacexpr E a v t =
257 (a, Expr (subst_atomic (case a of SOME a => upd_env E (a,v)
259 (*> val t = str2term "SubProblem(Test_, [linear, univariate, equation, test], [Test, solve_linear]) [bool_ e_, real_ v_]";
260 > subst_stacexpr [] NONE e_term t;*)
263 fun stacpbls (h $ body) =
265 fun scan ts (Const ("Let",_) $ e $ (Abs (v,T,b))) =
266 (scan ts e) @ (scan ts b)
267 | scan ts (Const ("If",_) $ c $ e1 $ e2) = (scan ts e1) @ (scan ts e2)
268 | scan ts (Const ("Script.While",_) $ c $ e $ _) = scan ts e
269 | scan ts (Const ("Script.While",_) $ c $ e) = scan ts e
270 | scan ts (Const ("Script.Repeat",_) $ e $ _) = scan ts e
271 | scan ts (Const ("Script.Repeat",_) $ e) = scan ts e
272 | scan ts (Const ("Script.Try",_) $ e $ _) = scan ts e
273 | scan ts (Const ("Script.Try",_) $ e) = scan ts e
274 | scan ts (Const ("Script.Or",_) $e1 $ e2 $ _) =
275 (scan ts e1) @ (scan ts e2)
276 | scan ts (Const ("Script.Or",_) $e1 $ e2) =
277 (scan ts e1) @ (scan ts e2)
278 | scan ts (Const ("Script.Seq",_) $e1 $ e2 $ _) =
279 (scan ts e1) @ (scan ts e2)
280 | scan ts (Const ("Script.Seq",_) $e1 $ e2) =
281 (scan ts e1) @ (scan ts e2)
282 | scan ts t = case subst_stacexpr [] NONE e_term t of
283 (_, STac _) => [t] | (_, Expr _) => []
284 in (distinct o (scan [])) body end;
285 (*sc = Solve_root_equation ...
286 > val ts = stacpbls sc;
287 > writeln (terms2str thy ts);
288 ["Rewrite square_equation_left True e_",
289 "Rewrite_Set SqRoot_simplify False e_",
290 "Rewrite_Set rearrange_assoc False e_",
291 "Rewrite_Set isolate_root False e_",
292 "Rewrite_Set norm_equation False e_",
293 "Rewrite_Set_Inst [(bdv, v_)] isolate_bdv False e_"]
298 fun is_calc (Const ("Script.Calculate",_) $ _) = true
299 | is_calc (Const ("Script.Calculate",_) $ _ $ _) = true
301 fun op_of_calc (Const ("Script.Calculate",_) $ Free (op_,_)) = op_
302 | op_of_calc (Const ("Script.Calculate",_) $ Free (op_,_) $ _) = op_
303 | op_of_calc t = raise error ("op_of_calc called with"^term2str t);
305 val Script sc = (#scr o rep_rls) Test_simplify;
306 val stacs = stacpbls sc;
308 val calcs = filter is_calc stacs;
309 val ids = map op_of_calc calcs;
310 map (curry assoc1 (!calclist')) ids;
312 (((map (curry assoc1 (!calclist'))) o (map op_of_calc) o
313 (filter is_calc) o stacpbls) sc):calc list;
316 (**.for automatic creation of scripts from rls.**)
317 (* naming of identifiers in scripts ???...
318 ((inst_abs @{theory}) o term_of o the o (parse @{theory})) "(t::'z) = t";
319 ((inst_abs @{theory}) o term_of o (the:cterm option -> cterm) o
320 (parse @{theory})) "(t't::'z) = t't";
321 ((inst_abs @{theory}) o term_of o the o (parse @{theory})) "(t_t::'z) = t_t";
322 (* not accepted !!!...*)
323 ((inst_abs @{theory}) o term_of o the o (parse @{theory})) "(t_::'z) = t_";
324 ((inst_abs @{theory}) o term_of o (the:cterm option -> cterm) o
325 (parse @{theory})) "(_t::'z) = _t";
327 ((inst_abs @{theory}) o term_of o the o (parse @{theory}))
328 "Script Stepwise (t::'z) =\
330 \ ((Try (Repeat (Rewrite real_diff_minus False))) @@ \
331 \ (Try (Repeat (Rewrite real_add_commute False))) @@ \
332 \ (Try (Repeat (Rewrite real_mult_commute False)))) \
334 val ScrStep $ _ $ _ = (*'z not affected by parse: 'a --> real*)
335 ((inst_abs @{theory}) o term_of o the o (parse @{theory}))
336 "Script Stepwise (t::'z) =\
338 \ ((Try (Repeat (Rewrite real_diff_minus False))) @@ \
339 \ (Try (Repeat (Rewrite real_add_commute False))) @@ \
340 \ (Try (Repeat (Rewrite real_mult_commute False)))) \
342 (*WN060605 script-arg (t_::'z) and "Free (t_, 'a)" at end of body
343 are inconsistent !!!*)
344 val ScrStep_inst $ Term $ Bdv $ _=(*'z not affected by parse: 'a --> real*)
345 ((inst_abs @{theory}) o term_of o the o (parse @{theory}))
346 "Script Stepwise_inst (t::'z) (v::real) =\
348 \ ((Try (Repeat (Rewrite_Inst [(bdv,v)] real_diff_minus False))) @@ \
349 \ (Try (Repeat (Rewrite_Inst [(bdv,v)] real_add_commute False))) @@\
350 \ (Try (Repeat (Rewrite_Inst [(bdv,v)] real_mult_commute False)))) \
353 ((inst_abs @{theory}) o term_of o the o (parseN @{theory}))
354 "Repeat (Rewrite real_diff_minus False t)";
356 ((inst_abs @{theory}) o term_of o the o (parseN @{theory}))
357 "Try (Rewrite real_diff_minus False t)";
359 ((inst_abs @{theory}) o term_of o the o (parseN @{theory}))
362 ((inst_abs @{theory}) o term_of o the o (parseN @{theory}))
364 val Rew $ (Free (_,IDtype)) $ _ $ t =
365 ((inst_abs @{theory}) o term_of o the o (parseN @{theory}))
366 "Rewrite real_diff_minus False t";
367 val Rew_Inst $ Subs $ _ $ _ =
368 ((inst_abs @{theory}) o term_of o the o (parseN @{theory}))
369 "Rewrite_Inst [(bdv,v)] real_diff_minus False";
370 val Rew_Set $ _ $ _ =
371 ((inst_abs @{theory}) o term_of o the o (parseN @{theory}))
372 "Rewrite_Set real_diff_minus False";
373 val Rew_Set_Inst $ _ $ _ $ _ =
374 ((inst_abs @{theory}) o term_of o the o (parseN @{theory}))
375 "Rewrite_Set_Inst [(bdv,v)] real_diff_minus False";
376 val SEq $ _ $ _ $ _ =
377 ((inst_abs @{theory}) o term_of o the o (parseN @{theory}))
378 " ((Try (Repeat (Rewrite real_diff_minus False))) @@ \
379 \ (Try (Repeat (Rewrite real_add_commute False))) @@ \
380 \ (Try (Repeat (Rewrite real_mult_commute False)))) t";
382 fun rule2stac _ (Thm (thmID, _)) =
383 Try $ (Repeat $ (Rew $ Free (thmID, IDtype) $ HOLogic.false_const))
384 | rule2stac calc (Calc (c, _)) =
385 Try $ (Repeat $ (Cal $ Free (assoc_calc (calc ,c), IDtype)))
386 | rule2stac calc (Cal1 (c, _)) =
387 Try $ (Repeat $ (Ca1 $ Free (assoc_calc (calc ,c), IDtype)))
388 | rule2stac _ (Rls_ rls) =
389 Try $ (Rew_Set $ Free (id_rls rls, IDtype) $ HOLogic.false_const);
390 (*val t = rule2stac [] (Thm ("real_diff_minus", num_str real_diff_minus));
392 val t = rule2stac calclist (Calc ("op +", eval_binop "#add_"));
394 val t = rule2stac [] (Rls_ rearrange_assoc);
397 fun rule2stac_inst _ (Thm (thmID, _)) =
398 Try $ (Repeat $ (Rew_Inst $ Subs $ Free (thmID, IDtype) $
399 HOLogic.false_const))
400 | rule2stac_inst calc (Calc (c, _)) =
401 Try $ (Repeat $ (Cal $ Free (assoc_calc (calc ,c), IDtype)))
402 | rule2stac_inst calc (Cal1 (c, _)) =
403 Try $ (Repeat $ (Cal $ Free (assoc_calc (calc ,c), IDtype)))
404 | rule2stac_inst _ (Rls_ rls) =
405 Try $ (Rew_Set_Inst $ Subs $ Free (id_rls rls, IDtype) $
406 HOLogic.false_const);
407 (*val t = rule2stac_inst [] (Thm ("real_diff_minus", num_str real_diff_minus));
409 val t = rule2stac_inst calclist (Calc ("op +", eval_binop "#add_"));
411 val t = rule2stac_inst [] (Rls_ rearrange_assoc);
415 (*for appropriate nesting take stacs in _reverse_ order*)
416 fun @@@ sts [s] = SEq $ s $ sts
417 | @@@ sts (s::ss) = @@@ (SEq $ s $ sts) ss;
419 | @@ [s1, s2] = SEq $ s1 $ s2 (*---------vvv--*)
421 let val s3::s2::ss = rev stacs
422 in @@@ (SEq $ s2 $ s3) ss end;
424 val rules = (#rules o rep_rls) isolate_root;
425 val rs = map (rule2stac calclist) rules;
427 atomt tt; writeln (term2str tt);
430 val contains_bdv = (not o null o (filter is_bdv) o ids2str o #prop o rep_thm);
432 (*.does a rule contain a 'bdv'; descend recursively into Rls_.*)
433 fun contain_bdv [] = false
434 | contain_bdv (Thm (_, thm)::rs) =
435 if (not o contains_bdv) thm
438 | contain_bdv (Calc _ ::rs) = contain_bdv rs
439 | contain_bdv (Cal1 _ ::rs) = contain_bdv rs
440 | contain_bdv (Rls_ rls ::rs) =
441 contain_bdv (get_rules rls) orelse contain_bdv rs
442 | contain_bdv (r::_) =
443 raise error ("contain_bdv called with ["^(id_rule r)^",...]");
445 fun rules2scr_Rls calc rules = (*WN100816 t_ -> t_t like "Script Stepwise..*)
447 then ScrStep_inst $ Term $ Bdv $
448 (Repeat $ (((@@ o (map (rule2stac_inst calc))) rules) $ e_term))
449 else ScrStep $ Term $
450 (Repeat $ (((@@ o (map (rule2stac calc))) rules) $ e_term));
451 (* val (calc, rules) = (!calclist', rules);
453 fun rules2scr_Seq calc rules = (*WN100816 t_ -> t_t like "Script Stepwise..*)
455 then ScrStep_inst $ Term $ Bdv $
456 (((@@ o (map (rule2stac_inst calc))) rules) $ e_term)
457 else ScrStep $ Term $
458 (((@@ o (map (rule2stac calc))) rules) $ e_term);
460 (*.prepare the input for an rls for use:
461 # generate a script for stepwise execution of the rls
462 # filter the operators for Calc out of the script
463 !!!use this function in ruleset' := !!! .*)
464 fun prep_rls Erls = raise error "prep_rls not impl. for Erls"
465 | prep_rls (Rls {id,preconds,rew_ord,erls,srls,calc,rules,...}) =
466 let val sc = (rules2scr_Rls (!calclist') rules)
467 in Rls {id=id,preconds=preconds,rew_ord=rew_ord,erls=erls,
469 calc = (*FIXXXME.040207 use also for met*)
470 ((map (curry assoc1 (!calclist'))) o (map op_of_calc) o
471 (filter is_calc) o stacpbls) sc,
474 (* val (Seq {id,preconds,rew_ord,erls,srls,calc,rules,...}) = add_new_c;
476 | prep_rls (Seq {id,preconds,rew_ord,erls,srls,calc,rules,...}) =
477 let val sc = (rules2scr_Seq (!calclist') rules)
478 in Seq {id=id,preconds=preconds,rew_ord=rew_ord,erls=erls,
480 calc = ((map (curry assoc1 (!calclist'))) o (map op_of_calc) o
481 (filter is_calc) o stacpbls) sc,
484 | prep_rls (Rrls {id,...}) =
485 raise error ("prep_rls not required for Rrls \""^id^"\"");
487 val Script sc = (#scr o rep_rls o prep_rls) isolate_root;
488 (writeln o term2str) sc;
489 val Script sc = (#scr o rep_rls o prep_rls) isolate_bdv;
490 (writeln o term2str) sc;