1 (* Title: tests for MathEngine/mathengine-stateless.sml
2 Author: Walther Neuper 2000, 2006, 2020
3 (c) due to copyright terms
5 theory Test_Some imports Build_Thydata begin
6 ML {* Know_Store.set_ref_thy @{theory};
7 fun autoCalculate' cI auto = autoCalculate cI auto |> Future.join *}
8 ML_file "$ISABELLE_ISAC_TEST/Tools/isac/Interpret/mathengine.sml"
10 "--------------------------------------------------------";
11 "table of contents --------------------------------------";
12 "--------------------------------------------------------";
13 "----------- change to TermC.parse ctxt -----------------------";
14 "----------- tryrefine ----------------------------------";
15 "----------- search for Or_to_List ----------------------";
16 "----------- check thy in Test_Code.init_calc @{context} ------------------";
17 "----------- identified error in fun getTactic, ThmC.string_of_thm ---------------";
18 "----------- improved fun getTactic for interSteps and numeral calculations --";
19 "--------------------------------------------------------";
20 "--------------------------------------------------------";
21 "--------------------------------------------------------";
23 "----------- change to TermC.parse ctxt -----------------------";
24 "----------- change to TermC.parse ctxt -----------------------";
25 "----------- change to TermC.parse ctxt -----------------------";
26 "===== start calculation: from problem description (fmz) to origin";
27 val fmz = ["realTestGiven (((1+2)*4/3) \<up> 2)", "realTestFind s"];
28 val (thyID, pblID, metID) =
29 ("Test", ["calculate", "test"], ["Test", "test_calculate"]);
30 (*======= Isabelle2013- 2 --> Isabelle2014: unclear, why this test ever run =====================*)
33 "----------- tryrefine ----------------------------------";
34 "----------- tryrefine ----------------------------------";
35 "----------- tryrefine ----------------------------------";
37 CalcTree @{context} [(["equality (x/(x^2 - 6*x+9) - 1/(x^2 - 3*x) =1/x)",
38 "solveFor x", "solutions L"],
39 ("RatEq",["univariate", "equation"],["no_met"]))];
41 moveActiveRoot 1; autoCalculate 1 CompleteCalc;
43 refineProblem 1 ([1],Res) "pbl_equ_univ"
44 (*gives "pbl_equ_univ_rat" correct*);
46 refineProblem 1 ([1],Res) (Ptool.pblID2guh @{context} ["LINEAR", "univariate", "equation"])
47 (*gives "pbl_equ_univ_lin" incorrect*);
49 "--------- search for Or_to_List ------------------------";
50 "--------- search for Or_to_List ------------------------";
51 "--------- search for Or_to_List ------------------------";
52 val fmz = ["equality (x \<up> 2 + 4*x + 5 = (2::real))", "solveFor x", "solutions L"];
53 val (dI',pI',mI') = ("Isac_Knowledge", ["univariate", "equation"], ["no_met"])
54 val (p,_,f,nxt,_,pt) = Test_Code.init_calc @{context} [(fmz, (dI',pI',mI'))];
55 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
56 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
57 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
58 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
59 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
60 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
61 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
62 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
63 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
64 val (p,_,f,nxt,_,pt) = me nxt p [] pt; (*nxt = ("Subproblem"*)
65 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
66 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
67 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
68 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
69 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
70 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
71 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
72 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
73 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
74 val (p''''',_,f,nxt''''',_,pt''''') = me nxt p [] pt;
75 "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ continue AFTER previous step 'me' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
76 "~~~~~ fun me, args:"; val (tac, (p:pos'), (_:NEW(*remove*)), (pt:ctree)) =
77 (nxt''''', p''''', [], pt''''');
78 val ("ok", (_, _, ptp)) = Step.by_tactic tac (pt,p)
80 "~~~~~ fun Step.do_next, args:"; val (((ip as (_,p_)):pos'), ((ptp as (pt,p), tacis):Calc.state_pre)) =
81 (p, ((pt, e_pos'),[]));
82 val pIopt = get_pblID (pt,ip);
83 ip = ([],Res); (*false*)
85 member op = [Pbl,Met] p_; (*false*)
86 "~~~~~ fun do_next, args:"; val (ptp as (pt, pos as (p,p_))) = (pt,ip);
87 MethodC.id_empty = get_obj g_metID pt (par_pblobj pt p); (*false*)
88 val thy' = get_obj g_domID pt (par_pblobj pt p);
89 val ((ist, ctxt), sc) = resume_prog (p,p_) pt;
91 val Next_Step (istate, ctxt, tac) = LI.find_next_step sc (pt,pos) ist ctxt; (*WAS Empty_Tac_: tac_*)
92 case tac of Or_to_List' _ => ()
93 | _ => error "Or_to_List broken ?"
96 "----------- check thy in Test_Code.init_calc @{context} ------------------";
97 "----------- check thy in Test_Code.init_calc @{context} ------------------";
98 "----------- check thy in Test_Code.init_calc @{context} ------------------";
99 "WN1109 with Inverse_Z_Transform.thy when starting tests with Test_Code.init_calc @{context} \n" ^
100 "we got the message: ME_Isa: thy 'Build_Inverse_Z_Transform' not in system \n" ^
101 "Below there are the steps which found out the reason: \n" ^
102 "store_pbt mistakenly stored that theory.";
103 val ctxt = Proof_Context.init_global @{theory Isac_Knowledge};
104 val SOME t = parseNEW ctxt "filterExpression (X = 3 / (z - 1/4 + - 1/8 * (1/(z::real))))";
105 val SOME t = parseNEW ctxt "stepResponse (x[n::real]::bool)";
107 val fmz = ["filterExpression (X = 3 / (z - 1/4 + - 1/8 * (1/(z::real))))", "boundVariable z",
108 "stepResponse (x[n::real]::bool)"];
109 val (dI,pI,mI) = ("Isac_Knowledge", ["Inverse", "Z_Transform", "SignalProcessing"],
110 ["SignalProcessing", "Z_Transform", "Inverse"]);
112 val (p,_,fb,nxt,_,pt) = Test_Code.init_calc @{context} [(fmz, (dI,pI,mI))];
113 (*WAS ME_Isa: thy 'Build_Inverse_Z_Transform' not in system*)
115 "~~~~~ fun Test_Code.init_calc @{context} , args:"; val [(fmz, sp):Formalise.T] = [(fmz, (dI,pI,mI))];
116 (*val ((pt, p), tacis) =*)
117 Step_Specify.nxt_specify_init_calc ctxt (fmz, sp);
118 "~~~~~ fun nxt_specify_init_calc , args:"; val (fmz, (dI, pI, mI)) = (fmz, sp);
119 (*val (_, hdl, (dI, pI, mI), pors, pctxt) =*)
120 Step_Specify.initialise ctxt (fmz, (dI, pI, mI));
121 "~~~~~ fun initialise , args:"; val (fmz, (dI, pI, mI)) = (fmz, (dI, pI, mI));
122 val thy = ThyC.get_theory dI
123 val ctxt = Proof_Context.init_global thy;
124 (*if*) mI = ["no_met"](*else*);
125 val (pors, pctxt) = Problem.from_store ctxt pI |> #model |> O_Model.init_PIDE thy fmz;
126 val {cas, model, thy=thy',...} = Problem.from_store ctxt pI; (*take dI from _refined_ pbl*)
127 "tracing bottom up"; Context.theory_name thy' = "Build_Inverse_Z_Transform"; (*WAS true*)
128 val dI = Context.theory_name (ThyC.parent_of thy thy');
129 "tracing bottom up"; dI = "Build_Inverse_Z_Transform"; (*WAS true*)
131 val hdl = pblterm dI pI;
132 val (pt, _) = cappend_problem e_ctree [] (Istate.empty, pctxt) (fmz, (dI, pI, mI))
133 (pors, (dI, pI, mI), hdl, ContextC.empty)
135 "~~~~~ fun Step_Specify.by_tactic_input, args:"; val ((tac as Model_Problem), (pt, pos as (p,p_))) = (Model_Problem, (pt, ([], Pbl)));
136 val PblObj {origin = (oris, ospec, _), probl, spec, ctxt, ...} = get_obj I pt p;
137 "tracing bottom up"; #1(ospec) = "Build_Inverse_Z_Transform"; (*WAS true*)
139 "~~~~~ fun References.select_input, args:"; val ((odI, opI, omI), (dI, pI, mI)) = (ospec, spec);
140 dI = ThyC.id_empty; (*true*)
141 odI = "Build_Inverse_Z_Transform"; (*true*)
142 dI = ThyC.id_empty; (*true*)
143 "~~~~~ after fun References.select_input:";
144 val (dI, pI, mI) = References.select_input ospec spec;
145 "tracing bottom up"; dI = "Build_Inverse_Z_Transform"; (*WAS true*)
146 val thy = ThyC.get_theory dI; (*caused ME_Isa: thy 'Build_Inverse_Z_Transform' not in system*)
148 "----------- identified error in fun getTactic, ThmC.string_of_thm ---------------";
149 "----------- identified error in fun getTactic, ThmC.string_of_thm ---------------";
150 "----------- identified error in fun getTactic, ThmC.string_of_thm ---------------";
152 CalcTree @{context} [(["equality (x+1=(2::real))", "solveFor x", "solutions L"],
153 ("Test",["sqroot-test", "univariate", "equation", "test"],["Test", "squ-equ-test-subpbl1"]))];
155 autoCalculate 1 CompleteCalcHead;
156 autoCalculate 1 (Steps 1);
157 autoCalculate 1 (Steps 1);
158 autoCalculate 1 (Steps 1);
159 getTactic 1 ([1],Frm); (*<RULESET> norm_equation </RULESET>*)
160 interSteps 1 ([1],Res);
161 "~~~~~ fun interSteps, args:"; val (cI, ip) = (1, ([1],Res));
162 val ((pt,p), tacis) = States.get_calc cI;
163 (not o is_interpos) ip = false;
164 val ip' = lev_pred' pt ip;
165 "~~~~~ fun go, args:"; val (pt, (pos as (p,p_):pos')) = (pt, ip);
166 (* \<up> \<up> \<up> not in test/../ *)
168 val cn = children nd;
170 (is_rewset o (get_obj g_tac nd)) [(*root of nd*)] = true;
171 "~~~~~ fun detailrls, args:"; val (pt, (pos as (p,p_):pos')) = (pt, pos);
172 (* \<up> \<up> \<up> only once in test/../solve.sml*)
173 val t = get_obj g_form pt p
174 val tac = get_obj g_tac pt p
175 val ctxt = get_ctxt pt pos
176 val rls = ((get_rls ctxt) o rls_of) tac
177 val ctxt = get_ctxt pt pos
178 (*rls = Rule_Set.Repeat {calc = [], asm_rls = ...*)
179 val is = Istate.init_detail ctxt tac t
180 (*TODO.WN060602 Pstate (["(t_, Problem (Isac,[equation,univar]))"]
181 is wrong for simpl, but working ?!? *)
182 val tac_ = Apply_Method' (MethodC.id_empty(*WN0402: see Step.add !?!*), SOME t, is, ctxt)
183 val pos' = ((lev_on o lev_dn) p, Frm)
184 val thy = ThyC.get_theory "Isac_Knowledge"
185 val (_,_,_,pt') = (*implicit Take*)Step.add tac_ (is, ctxt) (pt, pos');
186 (*val (_,_,(pt'',_)) = *)complete_solve CompleteSubpbl [] (pt',pos');
187 (* \<up> \<up> \<up> \<up> ^^ in test/../mathengine.sml*)
188 (* in pt'': tac = Rewrite ("rnorm_equation_add", "Test.rnorm_equation_add")},
189 \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> *)
190 "~~~~~ fun complete_solve , args:"; val (auto, c, (ptp as (_, p as (_,p_)): ctree * pos')) =
191 (CompleteSubpbl, [], (pt',pos'));
192 p = ([], Res) = false;
193 member op = [Pbl,Met] p_ = false;
194 val (_, (_, c', ptp')) = Step_Solve.do_next ptp;
195 (* in pt': tac = Rewrite ("rnorm_equation_add", "Test.rnorm_equation_add")},
196 \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> *)
197 "~~~~~ fun do_next , args:"; val ((ptp as (pt, pos as (p,p_)))) = (ptp);
198 MethodC.id_empty = get_obj g_metID pt (par_pblobj pt p) = false;
199 val thy' = get_obj g_domID pt (par_pblobj pt p);
200 val ((ist, ctxt), sc) = resume_prog (p,p_) pt;
201 val Next_Step (is, ctxt, tac_) = LI.find_next_step sc (pt,pos) ist ctxt;
202 (*tac_ = Rewrite' (..., ("rnorm_equation_add", "Test.rnorm_equation_add"), ...) !!!!!!!!!!!!!!!!*)
204 (*----- outcommented during cleanup of args in lucas-interpreter.sml ------------------------\\* )
205 "~~~~~ fun find_next_step , args:"; val (thy, (ptp as (pt, pos as (p, _)):ctree * pos'),
206 (sc as Prog (h $ body)), (Pstate (ist as (E,l,rls,a,v,s,b)), ctxt)) = ((thy',prog_rls), (pt,pos), sc, is);
208 go_scan_up thy ptp sc ist true (*--> Accept_Tac (Rewrite' (.., (.., "Test.rnorm_equation_add"), ...)
209 BUT WE FIND IN THE CODE ABOVE IN find_next_step:*)
211 (*----------------------------------------------------------------------------------------------*)
212 if ThmC.string_of_thm @{thm rnorm_equation_add} = "\<not> ?b =!= 0 \<Longrightarrow> (?a = ?b) = (?a + - 1 * ?b = 0)"
213 then () else error "ThmC.string_of_thm changed";
214 if ThmC.string_of_thm @{thm rnorm_equation_add} = "\<not> ?b =!= 0 \<Longrightarrow> (?a = ?b) = (?a + - 1 * ?b = 0)"
215 then () else error "string_of_thm changed";
216 (*----------------------------------------------------------------------------------------------*)
218 (*SEARCH FOR THE ERROR WENT DOWN TO THE END OF THIS TEST, AND THEN UP TO HERE AGAIN*)
219 "~~~~~ fun begin_end_prog, args:"; val (tac_, is, (pt, pos as (p,p_))) = (tac_, is, ptp);
222 (p, Met) => ((lev_on o lev_dn) p, Frm)(*begin script*)
223 | (p, Res) => (lev_on p,Res) (*somewhere in script*)
225 Step.add tac_ is pos pt;
226 (* tac = Rewrite ("rnorm_equation_add", "Test.rnorm_equation_add")},
227 \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> *)
228 "~~~~~ fun add, args:"; val (thy, (Rewrite' (thy',ord',rls',pa,thm',f,(f',asm))),
229 (is, ctxt), (p,p_), pt) = ((ThyC.get_theory "Isac_Knowledge"), tac_, is, pos, pt);
230 val (pt,c) = cappend_atomic pt p (is, ContextC.insert_assumptions asm ctxt) f
231 (Rewrite thm') (f',asm) Complete;
232 (* in pt: tac = Rewrite ("rnorm_equation_add", "Test.rnorm_equation_add")},
233 \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> *)
234 "~~~~~ fun cappend_atomic, args:"; val (pt, p, ist_res, f, r, f', s) =
235 (pt, p, (is, ContextC.insert_assumptions asm ctxt), f, (Rewrite thm'), (f',asm), Complete);
236 existpt p pt andalso Tactic.is_empty (get_obj g_tac pt p) = false;
237 apfst (append_atomic p ist_res f r f' s) (cut_tree pt (p,Frm));
238 apfst : ('a -> 'b) -> 'a * 'c -> 'b * 'c;
239 (append_atomic p ist_res f r f' s) : ctree -> ctree;
241 (* HERE THE ERROR OCCURED IN THE FIRST APPROACH
242 getTactic 1 ([1,1],Frm); <ERROR> syserror in getTactic </ERROR> <<<<<=========================*)
243 "~~~~~ fun getTactic, args:"; val (cI, (p:pos')) = (1, ([1,1],Frm));
244 val ((pt,_),_) = States.get_calc cI
245 val (form, tac, asms) = ME_Misc.pt_extract (pt, p)
247 "~~~~~ fun gettacticOK2xml, args:"; val ((cI:calcID), tac) = (cI, ta);
249 "~~~~~ fun tac2xml, args:"; val (j, (Rewrite thm')) = (i, tac);
250 "~~~~~ fun thm'2xml, args:"; val (j, ((ID, form) : thm'')) = ((j+i), thm');
251 ID = "rnorm_equation_add";
252 @{thm rnorm_equation_add};
253 (UnparseC.term o Thm.prop_of) form = "~ ?b =!= 0 ==> (?a = ?b) = (?a + - 1 * ?b = 0)"
254 (*?!? should be "\<not> ?b =!= 0 \<Longrightarrow> (?a = ?b) = (?a + - 1 * ?b = 0)"*)
255 (*thmstr2xml (j+i) form;
256 ERROR Undeclared constant: "Test.rnorm_equation_add" \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> \<up> ^*)
258 Test_Tool.show_pt pt;
260 (([], Frm), solve (x + 1 = 2, x)),
261 (([1], Frm), x + 1 = 2),
262 (([1,1], Frm), x + 1 = 2),
263 (([1,1], Res), x + 1 + - 1 * 2 = 0),
264 (([1], Res), x + 1 + - 1 * 2 = 0),
265 (([2], Res), - 1 + x = 0)]
267 pt; --> tac = Rewrite ("rnorm_equation_add", "Test.rnorm_equation_add")*)
268 ( *----- outcommented during cleanup of args in lucas-interpreter.sml ------------------------//*)
270 "----------- improved fun getTactic for interSteps and numeral calculations --";
271 "----------- improved fun getTactic for interSteps and numeral calculations --";
272 "----------- improved fun getTactic for interSteps and numeral calculations --";
273 val ctxt = Proof_Context.init_global @{theory Test};
274 States.reset (); val cI = 1;
275 CalcTree @{context} [(["equality (x+1=(2::real))", "solveFor x", "solutions L"],
276 ("Test",["sqroot-test", "univariate", "equation", "test"],["Test", "squ-equ-test-subpbl1"]))];
278 autoCalculate 1 CompleteCalcHead;
279 autoCalculate 1 (Steps 1);
281 val cs = States.get_calc cI (* we improve from the calcstate here [*] *);
282 val pos as (_,p_) = States.get_pos cI 1 (* we improve from the calcstate here [*] *);
284 appendFormula 1 "x + 4 + -3 = 2" (*|> Future.join*);
285 interSteps 1 ([1],Res); (* already done by appendFormula, now showed to frontend *)
286 getTactic 1 ([1,1], Res);
287 (*<ID> sym_#add_Float ((~3,0), (0,0)) __ ((4,0), (0,0)) </ID> <<<<<================== to improve
288 <ISA> 1 + x = -3 + (4 + x) </ISA>*)
290 val ((pt''',p'''), tacis''') = States.get_calc cI;
291 Test_Tool.show_pt pt'''
293 (([], Frm), solve (x + 1 = 2, x)),
294 (([1], Frm), x + 1 = 2),
295 (([1,1], Frm), x + 1 = 2),
296 (([1,1], Res), 1 + x = 2),
297 (([1,2], Res), -3 + (4 + x) = 2),
298 (([1,3], Res), -3 + (x + 4) = 2),
299 (([1,4], Res), x + 4 + -3 = 2),
300 (([1], Res), x + 4 + -3 = 2)]*)
302 "~~~~~ fun appendFormula', args:"; val (cI, ifo) = (1, "x + 4 + -3 = 2");
303 (* val cs = States.get_calc cI (* we improve from the calcstate here [*] *);
304 val pos as (_,p_) = States.get_pos cI 1 (* we improve from the calcstate here [*] *);*)
305 val ("ok", cs' as (_, _, ptp')) = Step.do_next pos cs;
306 (*val ("ok", (_, c, ptp as (_,p))) = *)Step_Solve.by_term ptp' (encode ifo);
307 "~~~~~ fun Step_Solve.by_term , args:"; val (cs as (_, _, ptp as (pt, pos as (p, p_))), istr) =
309 val SOME f_in = TermC.parseNEW (get_ctxt pt pos) istr
310 val f_succ = get_curr_formula (pt, pos);
311 f_succ = f_in = false;
312 val NONE = CAS_Cmd.input f_in
313 val pos_pred = lev_back' pos
314 (* f_pred ---"step pos cs"---> f_succ in appendFormula *)
315 val f_pred = get_curr_formula (pt, pos_pred);
316 (*val msg_calcstate' = *)compare_step ([], [], (pt, pos_pred)) f_in (*<<=====*);
317 "~~~~~ fun compare_step, args:"; val (((tacis, c, ptp as (pt, pos as (p,p_)))), ifo) =
318 (([], [], (pt, pos_pred)), f_in);
321 Frm => get_obj g_form pt p
322 | Res => (fst o (get_obj g_result pt)) p
323 | _ => TermC.empty (*on PblObj is fo <> ifo*);
324 val {rew_rls, ...} = MethodC.from_store ctxt (get_obj g_metID pt (par_pblobj pt p))
325 val {rew_ord, asm_rls, rules, ...} = Rule_Set.rep rew_rls;
326 (*val (found, der) = *)Derive.steps ctxt rew_ord asm_rls rules fo ifo; (*<---------------*)
327 "~~~~~ fun concat_deriv, args:"; val (rew_ord, asm_rls, rules, fo, ifo) =
328 (rew_ord, asm_rls, rules, fo, ifo);
329 fun derivat ([]:(term * rule * (term * term list)) list) = TermC.empty
330 | derivat dt = (#1 o #3 o last_elem) dt
331 fun equal (_,_,(t1, _)) (_,_,(t2, _)) = t1=t2
332 (*val fod = *)Derive.do_one ctxt asm_rls rules (snd rew_ord) NONE fo;
333 (*val ifod = *)Derive.do_one ctxt asm_rls rules (snd rew_ord) NONE ifo;