test/Tools/isac/Interpret/error-pattern.sml
author wneuper <Walther.Neuper@jku.at>
Thu, 08 Dec 2022 17:55:45 +0100
changeset 60611 a25716353782
parent 60608 5dabcc1c9235
child 60630 8ab7dc3d4d6d
permissions -rw-r--r--
make Minisubplb/400-start-meth-subpbl.sml from Thy_Info
     1 (* Title: "Interpret/error-pattern.sml"
     2    Author: Walther Neuper 060225,
     3    (c) due to copyright terms 
     4 
     5 Strange ERROR "Undefined fact: "all_left""
     6 *)
     7 
     8 "-----------------------------------------------------------------";
     9 "table of contents -----------------------------------------------";
    10 "-----------------------------------------------------------------";
    11 "appendForm with miniscript with mini-subpbl:";
    12 "--------- appendFormula: on Res + equ_nrls ----------------------";
    13 "--------- appendFormula: on Frm + equ_nrls ----------------------";
    14 "--------- appendFormula: on Res + NO deriv ----------------------";
    15 "--------- appendFormula: on Res + late deriv [x = 3 + -2]---///--";
    16 "replaceForm with miniscript with mini-subpbl:";
    17 "--------- replaceFormula: on Res + = ----------------------------";
    18 "--------- replaceFormula: on Res + = 1st Nd ---------------------";
    19 "--------- replaceFormula: on Frm + = 1st Nd ---------------------";
    20 "--------- replaceFormula: cut calculation -----------------------";
    21 "--------- replaceFormula: cut calculation -----------------------";
    22 (* 040307 copied from informtest.sml ... old versions
    23 "--------- maximum-example, UC: Modeling / modifyCalcHead --------";*)
    24 "--------- syntax error ------------------------------------------";
    25 "CAS-command:";
    26 (*"--------- CAS-command on ([],Pbl) -------------------------------";*)
    27 (*"--------- CAS-command on ([],Pbl) FE-interface ------------------";*)
    28 "--------- locate_input_term [rational,simplification] ----------------------";
    29 (*"--------- Take as 1st tac, start with <NEW> (CAS input) ---------";*)
    30 "--------- Take as 1st tac, start from exp -----------------------";
    31 (*"--------- implicit_take, start with <NEW> (CAS input) ---------------";*)
    32 "--------- build fun check_for' ------------------------------";
    33 "--------- build fun check_for' ?bdv -------------------------";
    34 "--------- build fun check_for ------------------------";
    35 "--------- embed fun check_for ------------------------";
    36 "--------- embed fun find_fill_patterns ---------------------------";
    37 "--------- build fun is_exactly_equal, inputFillFormula ----------";
    38 "--------- fun appl_adds -----------------------------------------";
    39 "--------- fun concat_deriv --------------------------------------";
    40 "--------- handle an input formula -------------------------------";
    41 "--------- fun dropwhile' ----------------------------------------";
    42 "-----------------------------------------------------------------";
    43 "-----------------------------------------------------------------";
    44 "-----------------------------------------------------------------";
    45 
    46 
    47 "--------- appendFormula: on Res + equ_nrls ----------------------";
    48 "--------- appendFormula: on Res + equ_nrls ----------------------";
    49 "--------- appendFormula: on Res + equ_nrls ----------------------";
    50  val Prog sc = (#program o MethodC.from_store ctxt) ["Test", "squ-equ-test-subpbl1"];
    51  (writeln o UnparseC.term) sc;
    52  val Prog sc = (#program o MethodC.from_store ctxt) ["Test", "solve_linear"];
    53  (writeln o UnparseC.term) sc;
    54 
    55  States.reset ();
    56  CalcTree @{context} [(["equality (x+1=(2::real))", "solveFor x", "solutions L"], 
    57 	    ("Test", ["sqroot-test", "univariate", "equation", "test"],
    58 	     ["Test", "squ-equ-test-subpbl1"]))];
    59  Iterator 1; moveActiveRoot 1;
    60  autoCalculate 1 CompleteCalcHead;
    61  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 = 2*)
    62  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 + - 1 * 2 = 0*);
    63 
    64  appendFormula 1 "- 2 * 1 + (1 + x) = 0" (*|> Future.join*); refFormula 1 (States.get_pos 1 1);
    65  val ((pt,_),_) = States.get_calc 1;
    66  val str = pr_ctree ctxt pr_short pt;
    67 if str =
    68 (".    ----- pblobj -----\n" ^
    69 "1.   x + 1 = 2\n" ^
    70 "2.   x + 1 + - 1 * 2 = 0\n" ^
    71 "2.1.   x + 1 + - 1 * 2 = 0\n" ^
    72 "2.2.   1 + x + - 1 * 2 = 0\n" ^
    73 "2.3.   1 + (x + - 1 * 2) = 0\n" ^
    74 "2.4.   1 + (x + - 2) = 0\n" ^
    75 "2.5.   1 + (x + - 2 * 1) = 0\n" ^
    76 "2.6.   1 + x + - 2 * 1 = 0\n" ) then ()
    77 else error "inform.sml: diff.behav.appendFormula: on Res + equ 1";
    78 
    79  moveDown 1 ([ ],Pbl); refFormula 1 ([1],Frm); (*x + 1 = 2*)
    80  moveDown 1 ([1],Frm); refFormula 1 ([1],Res); (*x + 1 + - 1 * 2 = 0*)
    81 
    82  (*the seven steps of detailed derivation*)
    83  moveDown 1 ([1  ],Res); refFormula 1 ([2,1],Frm); 
    84  moveDown 1 ([2,1],Frm); refFormula 1 ([2,1],Res);
    85  moveDown 1 ([2,1],Res); refFormula 1 ([2,2],Res);
    86  moveDown 1 ([2,2],Res); refFormula 1 ([2,3],Res); 
    87  moveDown 1 ([2,3],Res); refFormula 1 ([2,4],Res);
    88  moveDown 1 ([2,4],Res); refFormula 1 ([2,5],Res);
    89  moveDown 1 ([2,5],Res); refFormula 1 ([2,6],Res);
    90  val ((pt,_),_) = States.get_calc 1;
    91  if "- 2 * 1 + (1 + x) = 0" = UnparseC.term (fst (get_obj g_result pt [2,6])) then()
    92  else error "inform.sml: diff.behav.appendFormula: on Res + equ 2";
    93 
    94  fetchProposedTactic 1; (*takes Iterator 1 _1_*)
    95 (* <ERROR> error in kernel </ERROR> ALREADY IN 2009-2*)
    96 (*========== inhibit exn WN1130701 broken at Isabelle2002 --> 2009-2 =============
    97  val (_,(tac,_,_)::_) = States.get_calc 1;
    98  if tac = Rewrite_Set "Test_simplify" then ()
    99  else error "inform.sml: diff.behav.appendFormula: on Res + equ 3";
   100 ============ inhibit exn WN1130701 broken at Isabelle2002 --> 2009-2 ============*)
   101 
   102  autoCalculate 1 CompleteCalc;
   103  val ((pt,_),_) = States.get_calc 1;
   104  if "[x = 1]" = UnparseC.term (fst (get_obj g_result pt [])) then ()
   105  else error "inform.sml: diff.behav.appendFormula: on Res + equ 4";
   106  (* autoCalculate 1 CompleteCalc;
   107    val ((pt,p),_) = States.get_calc 1;
   108    (writeln o istates2str) (get_obj g_loc pt [ ]);  
   109    (writeln o istates2str) (get_obj g_loc pt [1]);  
   110    (writeln o istates2str) (get_obj g_loc pt [2]);  
   111    (writeln o istates2str) (get_obj g_loc pt [3]);  
   112    (writeln o istates2str) (get_obj g_loc pt [3,1]);  
   113    (writeln o istates2str) (get_obj g_loc pt [3,2]);  
   114    (writeln o istates2str) (get_obj g_loc pt [4]);  
   115 
   116    *)
   117 "----------------------------------------------------------";
   118 
   119  val fod = Derive.do_one (Proof_Context.init_global @{theory "Isac_Knowledge"}) Atools_erls 
   120 		       ((#rules o Rule_Set.rep) Test_simplify)
   121 		       (sqrt_right false) NONE 
   122 		       (TermC.parse_test @{context} "x + 1 + - 1 * 2 = 0");
   123  (writeln o (Derive.trtas2str ctxt)) fod;
   124 
   125  val ifod = Derive.do_one (Proof_Context.init_global @{theory "Isac_Knowledge"}) Atools_erls 
   126 		       ((#rules o Rule_Set.rep) Test_simplify)
   127 		       (sqrt_right false) NONE 
   128 		       (TermC.parse_test @{context} "- 2 * 1 + (1 + x) = 0");
   129  (writeln o (Derive.trtas2str ctxt)) ifod;
   130  fun equal (_,_,(t1, _)) (_,_,(t2, _)) = t1 = t2;
   131  val (fod', rifod') = dropwhile' equal (rev fod) (rev ifod);
   132  val der = fod' @ (map Derive.rev_deriv' rifod');
   133  (writeln o (Derive.trtas2str ctxt)) der;
   134  "----------------------------------------------------------";
   135 DEconstrCalcTree 1;
   136 
   137 "--------- appendFormula: on Frm + equ_nrls ----------------------";
   138 "--------- appendFormula: on Frm + equ_nrls ----------------------";
   139 "--------- appendFormula: on Frm + equ_nrls ----------------------";
   140  States.reset ();
   141  CalcTree @{context} [(["equality (x+1=(2::real))", "solveFor x", "solutions L"], 
   142 	    ("Test", ["sqroot-test", "univariate", "equation", "test"],
   143 	     ["Test", "squ-equ-test-subpbl1"]))];
   144  Iterator 1; moveActiveRoot 1;
   145  autoCalculate 1 CompleteCalcHead;
   146  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1) (*x + 1 = 2*);
   147  appendFormula 1 "2+ - 1 + x = 2" (*|> Future.join*); refFormula 1 (States.get_pos 1 1);
   148 
   149  moveDown 1 ([],Pbl); refFormula 1 ([1],Frm) (*x + 1 = 2*);
   150 
   151  moveDown 1 ([1  ],Frm); refFormula 1 ([1,1],Frm); 
   152  moveDown 1 ([1,1],Frm); refFormula 1 ([1,1],Res); 
   153  moveDown 1 ([1,1],Res); refFormula 1 ([1,2],Res); 
   154  moveDown 1 ([1,2],Res); refFormula 1 ([1,3],Res); 
   155  moveDown 1 ([1,3],Res); refFormula 1 ([1,4],Res); 
   156  moveDown 1 ([1,4],Res); refFormula 1 ([1,5],Res); 
   157  moveDown 1 ([1,5],Res); refFormula 1 ([1,6],Res); 
   158  val ((pt,_),_) = States.get_calc 1;
   159  if "2 + - 1 + x = 2" = UnparseC.term (fst (get_obj g_result pt [1,6])) then()
   160  else error "inform.sml: diff.behav.appendFormula: on Frm + equ 1";
   161 
   162  fetchProposedTactic 1; (*takes Iterator 1 _1_*)
   163  val (_,(tac,_,_)::_) = States.get_calc 1;
   164  case tac of Rewrite_Set "norm_equation" => ()
   165  | _ => error "inform.sml: diff.behav.appendFormula: on Frm + equ 2";
   166  autoCalculate 1 CompleteCalc;
   167  val ((pt,_),_) = States.get_calc 1;
   168  if "[x = 1]" = UnparseC.term (fst (get_obj g_result pt [])) then ()
   169  else error "inform.sml: diff.behav.appendFormula: on Frm + equ 3";
   170 DEconstrCalcTree 1;
   171 
   172 "--------- appendFormula: on Res + NO deriv ----------------------";
   173 "--------- appendFormula: on Res + NO deriv ----------------------";
   174 "--------- appendFormula: on Res + NO deriv ----------------------";
   175  States.reset ();
   176  CalcTree @{context} [(["equality (x+1=(2::real))", "solveFor x", "solutions L"], 
   177 	    ("Test", ["sqroot-test", "univariate", "equation", "test"],
   178 	     ["Test", "squ-equ-test-subpbl1"]))];
   179  Iterator 1; moveActiveRoot 1;
   180  autoCalculate 1 CompleteCalcHead;
   181  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 = 2*)
   182  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 + - 1 * 2 = 0*);
   183 
   184  appendFormula 1 "x = 2" (*|> Future.join*);
   185  val ((pt,p),_) = States.get_calc 1;
   186  val str = pr_ctree ctxt pr_short pt;
   187  writeln str;
   188  if str = ".    ----- pblobj -----\n1.   x + 1 = 2\n" andalso p = ([1], Res)
   189  then ()
   190  else error "inform.sml: diff.behav.appendFormula: Res + NOder 1";
   191 
   192  fetchProposedTactic 1;
   193  val (_,(tac,_,_)::_) = States.get_calc 1;
   194  case tac of Rewrite_Set "Test_simplify" => ()
   195  | _ => error "inform.sml: diff.behav.appendFormula: Res + NOder 2";
   196  autoCalculate 1 CompleteCalc;
   197  val ((pt,_),_) = States.get_calc 1;
   198  if "[x = 1]" = UnparseC.term (fst (get_obj g_result pt [])) then ()
   199  else error "inform.sml: diff.behav.appendFormula: on Frm + equ 3";
   200 DEconstrCalcTree 1;
   201 
   202 "--------- appendFormula: on Res + late deriv --------------------";
   203 "--------- appendFormula: on Res + late deriv --------------------";
   204 "--------- appendFormula: on Res + late deriv --------------------";
   205 (*cp with "fun me" to test/../lucas-interpreter.sml:
   206  re-build: fun locate_input_term ---------------------------------------------------"; 
   207 *)
   208  States.reset ();
   209  CalcTree @{context} [(["equality (x+1=(2::real))", "solveFor x", "solutions L"], 
   210 	    ("Test", ["sqroot-test", "univariate", "equation", "test"],
   211 	     ["Test", "squ-equ-test-subpbl1"]))];
   212  Iterator 1; moveActiveRoot 1;
   213  autoCalculate 1 CompleteCalcHead;
   214  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 = 2*)
   215  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 + - 1 * 2 = 0*);
   216 
   217  appendFormula 1 "x = 1" (*|> Future.join*);
   218  val ((pt,p),_) = States.get_calc 1;
   219  val str = pr_ctree ctxt pr_short pt;
   220  writeln str;
   221  if str = ".    ----- pblobj -----\n1.   x + 1 = 2\n2.   x + 1 + - 1 * 2 = 0\n3.    ----- pblobj -----\n3.1.   - 1 + x = 0\n3.2.   x = 0 + - 1 * - 1\n3.2.1.   x = 0 + - 1 * - 1\n3.2.2.   x = 0 + 1\n" andalso p = ([3,2], Res)
   222  then () (*finds 1 step too early: ([3,2], Res) "x = 1" also by script !!!*)
   223  else error "inform.sml: diff.behav.appendFormula: Res + late d 1";
   224 
   225  fetchProposedTactic 1;
   226  val (_,(tac,_,_)::_) = States.get_calc 1;
   227  case tac of Check_Postcond ["LINEAR", "univariate", "equation", "test"] => ()
   228  | _ => error "inform.sml: diff.behav.appendFormula: Res + late d 2";
   229  autoCalculate 1 CompleteCalc;
   230  val ((pt,_),_) = States.get_calc 1;
   231  if "[x = 1]" = UnparseC.term (fst (get_obj g_result pt [])) then ()
   232  else error "inform.sml: diff.behav.appendFormula: Res + late d 3";
   233 DEconstrCalcTree 1;
   234 
   235 "--------- appendFormula: on Res + late deriv [x = 3 + - 2]---///--";
   236 "--------- appendFormula: on Res + late deriv [x = 3 + - 2]---///--";
   237 "--------- appendFormula: on Res + late deriv [x = 3 + - 2]---///--";
   238  States.reset ();
   239  CalcTree @{context} [(["equality (x+1=(2::real))", "solveFor x", "solutions L"], 
   240 	    ("Test", ["sqroot-test", "univariate", "equation", "test"],
   241 	     ["Test", "squ-equ-test-subpbl1"]))];
   242  Iterator 1; moveActiveRoot 1;
   243  autoCalculate 1 CompleteCalcHead;
   244  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 = 2*)
   245  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 + - 1 * 2 = 0*);
   246  appendFormula 1 "[x = 3 + -2*1]" (*|> Future.join*);
   247  val ((pt,p),_) = States.get_calc 1;
   248  val str = pr_ctree ctxt pr_short pt;
   249  writeln str;
   250  if str=".    ----- pblobj -----\n1.   x + 1 = 2\n2.   x + 1 + - 1 * 2 = 0\n3.    ----- pblobj -----\n3.1.   - 1 + x = 0\n3.2.   x = 0 + - 1 * - 1\n4.   [x = 1]\n4.1.   [x = 1]\n4.2.   [x = - 2 + 3]\n4.3.   [x = 3 + - 2]\n" then ()
   251  else error "inform.sml: diff.behav.appendFormula: Res + latEE 1";
   252  autoCalculate 1 CompleteCalc;
   253  val ((pt,p),_) = States.get_calc 1;
   254  if "[x = 3 + - 2 * 1]" = UnparseC.term (fst (get_obj g_result pt [])) then ()
   255  (*       ~~~~~~~~~~ simplify as last step in any script ?!*)
   256  else error "inform.sml: diff.behav.appendFormula: Res + latEE 2";
   257 DEconstrCalcTree 1;
   258 
   259 "--------- replaceFormula: on Res + = ----------------------------";
   260 "--------- replaceFormula: on Res + = ----------------------------";
   261 "--------- replaceFormula: on Res + = ----------------------------";
   262  States.reset ();
   263  CalcTree @{context} [(["equality (x+1=(2::real))", "solveFor x", "solutions L"], 
   264 	    ("Test", ["sqroot-test", "univariate", "equation", "test"],
   265 	     ["Test", "squ-equ-test-subpbl1"]))];
   266  Iterator 1; moveActiveRoot 1;
   267  autoCalculate 1 CompleteCalcHead;
   268  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 = 2*)
   269  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 + - 1 * 2 = 0*);
   270  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*- 1 + x*);
   271 
   272  replaceFormula 1 "-2 * 1 + (1 + x) = 0"; refFormula 1 (States.get_pos 1 1);
   273  val ((pt,_),_) = States.get_calc 1;
   274  val str = pr_ctree ctxt pr_short pt;
   275 
   276 (* before AK110725 this was
   277 ".    ----- pblobj -----\n
   278 1.   x + 1 = 2\n
   279 2.   x + 1 + - 1 * 2 = 0\n
   280 2.1.   x + 1 + - 1 * 2 = 0\n
   281 2.2.   1 + x + - 1 * 2 = 0\n
   282 2.3.   1 + (x + - 1 * 2) = 0\n
   283 2.4.   1 + (x + -2) = 0\n
   284 2.5.   1 + (x + -2 * 1) = 0\n
   285 2.6.   1 + x + -2 * 1 = 0\n";
   286 *)
   287 if str =
   288 ".    ----- pblobj -----\n"^
   289 "1.   x + 1 = 2\n"^
   290 "2.   x + 1 + - 1 * 2 = 0\n"^
   291 "2.1.   x + 1 + - 1 * 2 = 0\n"^
   292 "2.2.   1 + x + - 1 * 2 = 0\n"^
   293 "2.3.   1 + (x + - 1 * 2) = 0\n"^
   294 "2.4.   1 + (x + - 2) = 0\n"^
   295 "2.5.   1 + (x + - 2 * 1) = 0\n"^
   296 "2.6.   1 + x + - 2 * 1 = 0\n" then()
   297 else error "inform.sml: diff.behav.replaceFormula: on Res += 1";
   298 
   299  autoCalculate 1 CompleteCalc;
   300  val ((pt,pos as (p,_)),_) = States.get_calc 1;
   301  if pos = ([],Res) andalso "[x = 1]" = (UnparseC.term o fst) (get_obj g_result pt p) then()
   302  else error "inform.sml: diff.behav.replaceFormula: on Res + = 2";
   303 DEconstrCalcTree 1;
   304 
   305 "--------- replaceFormula: on Res + = 1st Nd ---------------------";
   306 "--------- replaceFormula: on Res + = 1st Nd ---------------------";
   307 "--------- replaceFormula: on Res + = 1st Nd ---------------------";
   308  States.reset ();
   309  CalcTree @{context} [(["equality (x+1=(2::real))", "solveFor x", "solutions L"], 
   310 	    ("Test", ["sqroot-test", "univariate", "equation", "test"],
   311 	     ["Test", "squ-equ-test-subpbl1"]))];
   312  Iterator 1; moveActiveRoot 1;
   313  autoCalculate 1 CompleteCalcHead;
   314  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 = 2*)
   315  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 + - 1 * 2 = 0*);
   316 
   317  replaceFormula 1 "x + 1 = 4 + -2"; refFormula 1 (States.get_pos 1 1);
   318  val ((pt,_),_) = States.get_calc 1;
   319  val str = pr_ctree ctxt pr_short pt;
   320  writeln str;
   321  if str= ".    ----- pblobj -----\n1.   x + 1 = 2\n1.1.   x + 1 = 2\n1.2.   1 + x = 2\n1.3.   1 + x = - 2 + 4\n1.4.   x + 1 = - 2 + 4\n" then ()
   322  else error "inform.sml: diff.behav.replaceFormula: on Res 1 + = 1";
   323  autoCalculate 1 CompleteCalc;
   324  val ((pt,pos as (p,_)),_) = States.get_calc 1;
   325  if pos = ([],Res) andalso "[x = 1]" = (UnparseC.term o fst)(get_obj g_result pt p) then()
   326  else error "inform.sml: diff.behav.replaceFormula: on Res + = 2";
   327 DEconstrCalcTree 1;
   328 
   329 "--------- replaceFormula: on Frm + = 1st Nd ---------------------";
   330 "--------- replaceFormula: on Frm + = 1st Nd ---------------------";
   331 "--------- replaceFormula: on Frm + = 1st Nd ---------------------";
   332  States.reset (); 
   333  CalcTree @{context} [(["equality (x+1=(2::real))", "solveFor x", "solutions L"], 
   334 	    ("Test", ["sqroot-test", "univariate", "equation", "test"],
   335 	     ["Test", "squ-equ-test-subpbl1"]))];
   336  Iterator 1; moveActiveRoot 1;
   337  autoCalculate 1 CompleteCalcHead;
   338  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 = 2*)
   339 
   340  replaceFormula 1 "x + 1 = 4 + -2"; refFormula 1 (States.get_pos 1 1);
   341  val ((pt,_),_) = States.get_calc 1;
   342  val str = pr_ctree ctxt pr_short pt;
   343  writeln str;
   344  if str= ".    ----- pblobj -----\n1.   x + 1 = 2\n1.1.   x + 1 = 2\n1.2.   1 + x = 2\n1.3.   1 + x = - 2 + 4\n1.4.   x + 1 = - 2 + 4\n" then ()
   345  else error "inform.sml: diff.behav.replaceFormula: on Frm 1 + = 1";
   346  autoCalculate 1 CompleteCalc;
   347  val ((pt,pos as (p,_)),_) = States.get_calc 1;
   348  if pos = ([],Res) andalso "[x = 1]" = (UnparseC.term o fst)(get_obj g_result pt p) then()
   349  else error "inform.sml: diff.behav.replaceFormula: on Frm 1 + = 2";
   350 DEconstrCalcTree 1;
   351 
   352 "--------- replaceFormula: cut calculation -----------------------";
   353 "--------- replaceFormula: cut calculation -----------------------";
   354 "--------- replaceFormula: cut calculation -----------------------";
   355  States.reset ();
   356  CalcTree @{context} [(["equality (x+1=(2::real))", "solveFor x", "solutions L"], 
   357 	    ("Test", ["sqroot-test", "univariate", "equation", "test"],
   358 	     ["Test", "squ-equ-test-subpbl1"]))];
   359  Iterator 1; moveActiveRoot 1;
   360  autoCalculate 1 CompleteCalc;
   361  moveActiveRoot 1; moveActiveDown 1;
   362  if States.get_pos 1 1 = ([1], Frm) then ()
   363  else error "inform.sml: diff.behav. cut calculation 1";
   364 
   365  replaceFormula 1 "x + 1 = 4 + -2"; refFormula 1 (States.get_pos 1 1);
   366  val ((pt,p),_) = States.get_calc 1;
   367  val str = pr_ctree ctxt pr_short pt;
   368  writeln str;
   369  if p = ([1], Res) then ()
   370  else error "inform.sml: diff.behav. cut calculation 2";
   371 
   372 
   373 (* 040307 copied from informtest.sml; ... old version 
   374  "---------------- maximum-example, UC: Modeling / modifyCalcHead -";
   375  "---------------- maximum-example, UC: Modeling / modifyCalcHead -";
   376  "---------------- maximum-example, UC: Modeling / modifyCalcHead -";
   377 
   378  val p = ([],Pbl);
   379  val elems = ["fixedValues [r=Arbfix]", "maximum A", "valuesFor [a,b]",
   380 	      "relations [A=a*b, (a/2) \<up> 2 + (b/2) \<up> 2 = r \<up> 2]",
   381 	      "relations [A=a*b, (a/2) \<up> 2 + (b/2) \<up> 2 = r \<up> 2]",
   382         "relations [A=a*b, a/2=r*sin alpha, b/2=r*cos (alpha::real)]",
   383 	      (* \<up>  these are the elements for the root-problem (in variants)*)
   384               (*vvv these are elements required for subproblems*)
   385 	      "boundVariable a", "boundVariable b", "boundVariable alpha",
   386 	      "interval {x::real. 0 <= x & x <= 2*r}",
   387 	      "interval {x::real. 0 <= x & x <= 2*r}",
   388 	      "interval {x::real. 0 <= x & x <= pi}",
   389 	      "errorBound (eps=(0::real))"]
   390  (*specifying is not interesting for this example*)
   391  val spec = ("Diff_App", ["maximum_of", "function"], 
   392 	     ["Diff_App", "max_by_calculus"]);
   393  (*the empty model with descriptions for user-guidance by Model_Problem*)
   394  val empty_model = [Given ["fixedValues []"],
   395 		    Find ["maximum", "valuesFor"],
   396 		    Relate ["relations []"]];
   397 
   398 
   399  (*!!!!!!!!!!!!!!!!! DON'T USE me FOR FINDING nxt !!!!!!!!!!!!!!!!!!*)
   400  val (p,_,f,nxt,_,pt) = Test_Code.init_calc @{context} [(elems, spec)];
   401  (*val nxt = ("Model_Problem", ...*)
   402  val pbl = get_obj g_pbl pt (fst p); (writeln o (I_Model.to_string ctxt)) pbl; 
   403 
   404  val (p,_,f,nxt,_,pt) = me nxt p c pt;
   405  (*nxt = Add_Given "fixedValues [r = Arbfix]"*)
   406  val pbl = get_obj g_pbl pt (fst p); (writeln o (I_Model.to_string ctxt)) pbl; 
   407 (*[
   408 (0 ,[] ,false ,#Given ,Inc fixedValues [] ,(??.empty, [])),
   409 (0 ,[] ,false ,#Find ,Inc maximum ,(??.empty, [])),
   410 (0 ,[] ,false ,#Find ,Inc valuesFor ,(??.empty, [])),
   411 (0 ,[] ,false ,#Relate ,Inc relations [] ,(??.empty, []))]*)
   412 
   413  (*the empty CalcHead is checked w.r.t the model and re-established as such*)
   414  val (b,pt,ocalhd) = input_icalhd pt (p,"", empty_model, Pbl, References.empty);
   415  val pbl = get_obj g_pbl pt (fst p); (writeln o (I_Model.to_string ctxt)) pbl; 
   416  if ocalhd2str ocalhd = "(Pbl, ??.empty, [\n(0 ,[] ,false ,#Given ,Inc fixedValues [] ,(??.empty, [])),\n(0 ,[] ,false ,#Find ,Inc maximum ,(??.empty, [])),\n(0 ,[] ,false ,#Find ,Inc valuesFor ,(??.empty, [])),\n(0 ,[] ,false ,#Relate ,Inc relations [] ,(??.empty, []))], [], \n(\"ThyC.id_empty\", [\"e_pblID\"], [\"e_metID\"]) )" then () else error "informtest.sml: diff.behav. max 1";
   417 
   418  (*there is one input to the model (could be more)*)
   419  val (b,pt,ocalhd) = 
   420      input_icalhd pt (p,"", [Given ["fixedValues [r=Arbfix]"],
   421 			     Find ["maximum", "valuesFor"],
   422 			     Relate ["relations"]], Pbl, References.empty);
   423  val pbl = get_obj g_pbl pt (fst p); (writeln o (I_Model.to_string ctxt)) pbl; 
   424  if ocalhd2str ocalhd = "(Pbl, ??.empty, [\n(1 ,[1,2,3] ,true ,#Given ,Cor fixedValues [r = Arbfix] ,(fix_, [[r = Arbfix]])),\n(0 ,[] ,false ,#Find ,Inc maximum ,(??.empty, [])),\n(0 ,[] ,false ,#Find ,Inc valuesFor ,(??.empty, [])),\n(0 ,[] ,false ,#Relate ,Inc relations [] ,(??.empty, []))], [], \n(\"ThyC.id_empty\", [\"e_pblID\"], [\"e_metID\"]) )" then () 
   425  else error "informtest.sml: diff.behav. max 2";
   426 
   427  (*this input is complete in variant 3, but the ME doesn't recognize FIXXXXME
   428  val (b,pt''''',ocalhd) = 
   429      input_icalhd pt (p,"", [Given ["fixedValues [r=Arbfix]"],
   430 			     Find ["maximum A", "valuesFor [a,b]"],
   431 			     Relate ["relations [A=a*b, a/2=r*sin alpha, \
   432 				     \b/2=r*cos alpha]"]], Pbl, References.empty);
   433  val pbl = get_obj g_pbl pt''''' (fst p); (writeln o (I_Model.to_string ctxt)) pbl; 
   434  if ocalhd2str ocalhd = ------------ \<up> \<up> \<up> ^ missing !!!*)
   435 
   436  (*this input is complete in variant 1 (variant 3 does not work yet)*)
   437  val (b,pt''''',ocalhd) = 
   438      input_icalhd pt (p,"", [Given ["fixedValues [r=Arbfix]"],
   439 			     Find ["maximum A", "valuesFor [a,b]"],
   440 			     Relate ["relations [A=a*b, \
   441 				     \(a/2) \<up> 2 + (b/2) \<up> 2 = r \<up> 2]"]], 
   442 		      Pbl, References.empty);
   443  val pbl = get_obj g_pbl pt''''' (fst p); (writeln o (I_Model.to_string ctxt)) pbl; 
   444 
   445  modifycalcheadOK2xml 111 (bool2str b) ocalhd;
   446 *)
   447 DEconstrCalcTree 1;
   448 
   449 "--------- syntax error ------------------------------------------";
   450 "--------- syntax error ------------------------------------------";
   451 "--------- syntax error ------------------------------------------";
   452  States.reset ();
   453  CalcTree @{context} [(["equality (x+1=(2::real))", "solveFor x", "solutions L"], 
   454 	    ("Test", ["sqroot-test", "univariate", "equation", "test"],
   455 	     ["Test", "squ-equ-test-subpbl1"]))];
   456  Iterator 1; moveActiveRoot 1;
   457  autoCalculate 1 CompleteCalcHead;
   458  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 = 2*)
   459  autoCalculate 1 (Steps 1); refFormula 1 (States.get_pos 1 1);(*x + 1 + - 1 * 2 = 0*);
   460 
   461  appendFormula 1 " x - "; (*<ERROR> syntax error in ' x - ' </ERROR>*)
   462  val ((pt,_),_) = States.get_calc 1;
   463  val str = pr_ctree ctxt pr_short pt;
   464  writeln str;
   465  if str = ".    ----- pblobj -----\n1.   x + 1 = 2\n" then ()
   466  else error "inform.sml: diff.behav.appendFormula: syntax error";
   467 DEconstrCalcTree 1;
   468 
   469 (*POSTPONE CORRECTION AT END OF TRANSITION TO Isabelle/PIDE/Isac (~ Cas_Cmd)
   470 "--------- CAS-command on ([],Pbl) -------------------------------";
   471 "--------- CAS-command on ([],Pbl) -------------------------------";
   472 "--------- CAS-command on ([],Pbl) -------------------------------";
   473 val (p,_,f,nxt,_,pt) = 
   474     Test_Code.init_calc @{context} [([], References.empty)];
   475 val ifo = "solve(x+1=2,x)";
   476 val (_,(_,c,(pt,p))) = Step_Solve.by_term (pt,p) "solve(x+1=2,x)";
   477 ( *
   478   This trick           \<up> \<up> \<up> micked input of \<up> \<up> \<up> \<up>  \<up> ^^ in the front-end.
   479   The trick worked in changeset fbaff8cf0179, it does not work in 59c5dd27d589 anymore
   480   (TODO hg bisect ?) and raises the ERROR Undefined fact: "xfoldr_Nil".
   481 
   482   Compare tests "CAS-command" in test/../inssort.sml etc.
   483   ---------------------------------------------------------------------------------------------
   484 Test_Tool.show_pt pt;
   485 val nxt = (Apply_Method ["Test", "squ-equ-test-subpbl1"]);
   486 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   487 if p = ([1], Frm) andalso f2str f = "x + 1 = 2" then ()
   488 else error "inform.sml: diff.behav. CAScmd ([],Pbl)";
   489 DEconstrCalcTree 1;
   490 -----------------------------------------------------------------------------------------------*)
   491 
   492 (*POSTPONE CORRECTION AT END OF TRANSITION TO Isabelle/PIDE/Isac (~ Cas_Cmd)
   493 "--------- CAS-command on ([],Pbl) FE-interface ------------------";
   494 "--------- CAS-command on ([],Pbl) FE-interface ------------------";
   495 "--------- CAS-command on ([],Pbl) FE-interface ------------------";
   496 States.reset ();
   497 CalcTree @{context} [([], References.empty)];
   498 Iterator 1;
   499 moveActiveRoot 1;
   500 replaceFormula 1 "solve(x+1=2,x)";
   501 autoCalculate 1 CompleteCalc;
   502 val ((pt,p),_) = States.get_calc 1;
   503 Test_Tool.show_pt pt;
   504 if p = ([], Res) then ()
   505 else error "inform.sml: diff.behav. CAScmd ([],Pbl) FE-interface";
   506 DEconstrCalcTree 1;
   507 -----------------------------------------------------------------------------------------------*)
   508 
   509 "--------- inform [rational,simplification] ----------------------";
   510 "--------- inform [rational,simplification] ----------------------";
   511 "--------- inform [rational,simplification] ----------------------";
   512 States.reset ();
   513 CalcTree @{context} [(["Term (a * x / (b * x) + c * x / (d * x) + e / f)", "normalform N"],
   514 	("Rational", ["rational", "simplification"], ["simplification", "of_rationals"]))];
   515 Iterator 1; moveActiveRoot 1;
   516 autoCalculate 1 CompleteCalcHead;
   517 
   518 "--- (- 1) give a preview on the calculation without any input";
   519 (*
   520 autoCalculate 1 CompleteCalc;
   521 val ((pt, p), _) = States.get_calc 1;
   522 Test_Tool.show_pt pt;
   523 [
   524 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   525 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   526 (([1], Res), a / b + c / d + e / f),                             <--- (1) input arbitrary
   527 (([2], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   528 (([3], Res), (a * (d * f) + b * (c * f) + b * (d * e)) / (b * (d * f))),
   529 (([4], Res), (a * d * f + b * c * f + b * d * e) / (b * d * f)), <--- (2) input next
   530 (([], Res), (a * d * f + b * c * f + b * d * e) / (b * d * f))]  <--- (3) is also final result
   531                                                                           EXAMPLE NOT OPTIMAL
   532 *)
   533 "--- (0) user input as the *first* step does not work, thus impdo at least 1 step";
   534 autoCalculate 1 (Steps 1);
   535 autoCalculate 1 (Steps 1);
   536 val ((pt, p), _) = States.get_calc 1;
   537 (*Test_Tool.show_pt pt;
   538 [
   539 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   540 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   541 (([1], Res), a / b + c / d + e / f)] 
   542 *)
   543 "--- (1) input an arbitrary next formula";
   544 appendFormula 1 "((a * d) + (c * b)) / (b * d) + e / f" (*|> Future.join*);
   545 val ((pt, p), _) = States.get_calc 1;
   546 (*Test_Tool.show_pt pt;
   547 [
   548 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   549 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   550 (([1], Res), a / b + c / d + e / f),
   551 (([2,1], Frm), a / b + c / d + e / f),
   552 (([2,1], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   553 (([2,2], Res), (a * d + c * b) / (b * d) + e / f),
   554 (([2], Res), (a * d + c * b) / (b * d) + e / f)] 
   555 *)
   556 val ((pt,p),_) = States.get_calc 1;
   557 if p = ([2], Res) andalso (length o children o (get_nd pt)) (fst p) = 2 then ()
   558 else error ("inform.sml: [rational,simplification] 1");
   559 
   560 "--- (2) input the next formula that would be presented by mat-engine";
   561 (* generate a preview:
   562 autoCalculate 1 (Steps 1);
   563 val ((pt, p), _) = States.get_calc 1;
   564 Test_Tool.show_pt pt;
   565 [
   566 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   567 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   568 (([1], Res), a / b + c / d + e / f),
   569 (([2,1], Frm), a / b + c / d + e / f),
   570 (([2,1], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   571 (([2,2], Res), (a * d + c * b) / (b * d) + e / f),
   572 (([2], Res), (a * d + c * b) / (b * d) + e / f),
   573 (([3], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f))]   <--- input this
   574 *)
   575 appendFormula 1 "(b * d * e + b * c * f + a * d * f) / (b * d * f)" (*|> Future.join*);
   576 val ((pt, p), _) = States.get_calc 1;
   577 (*Test_Tool.show_pt pt;
   578 [
   579 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   580 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   581 (([1], Res), a / b + c / d + e / f),
   582 (([2,1], Frm), a / b + c / d + e / f),
   583 (([2,1], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   584 (([2,2], Res), (a * d + c * b) / (b * d) + e / f),
   585 (([2], Res), (a * d + c * b) / (b * d) + e / f),
   586 (([3], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f))] 
   587 *)
   588 if p = ([3], Res) andalso (length o children o (get_nd pt)) (fst p) = 0 then ()
   589 else error ("inform.sml: [rational,simplification] 2");
   590 
   591 "--- (3) input the exact final result";
   592 appendFormula 1 "(b * d * e + b * c * f + a * d * f) / (b * d * f)" (*|> Future.join*);
   593 val ((pt, p), _) = States.get_calc 1;
   594 (*Test_Tool.show_pt pt;
   595 [
   596 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   597 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   598 (([1], Res), a / b + c / d + e / f),
   599 (([2,1], Frm), a / b + c / d + e / f),
   600 (([2,1], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   601 (([2,2], Res), (a * d + c * b) / (b * d) + e / f),
   602 (([2], Res), (a * d + c * b) / (b * d) + e / f),
   603 (([3], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   604 (([4,1], Frm), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   605 (([4,1], Res), (a * (d * f) + b * (c * f) + b * (d * e)) / (b * (d * f))),
   606 (([4,2], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   607 (([4], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f))] 
   608 *)
   609 if p = ([4], Res) andalso (length o children o (get_nd pt)) (fst p) = 2 then ()
   610 else error ("inform.sml: [rational,simplification] 3");
   611 
   612 "--- (4) finish the calculation + check the postcondition (in the future)";
   613 autoCalculate 1 CompleteCalc;
   614 val ((pt, p), _) = States.get_calc 1;
   615 val (t, asm) = get_obj g_result pt [];
   616 if UnparseC.term t = "(a * d * f + b * c * f + b * d * e) / (b * d * f)" andalso
   617 UnparseC.terms asm =(*"[\"b * d * f \<noteq> 0\",\"d \<noteq> 0\",\"b \<noteq> 0\",\"a * x / (b * x) + c * x / (d * x) + e / f is_ratpolyexp\"]"*)
   618                     "[]" (*..found broken in child of 33913fe24685, error covered by  CAS-command *)
   619 then () else error "inform [rational,simplification] changed at end";
   620 (*Test_Tool.show_pt pt;
   621 [
   622 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   623 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   624 (([1], Res), a / b + c / d + e / f),
   625 (([2,1], Frm), a / b + c / d + e / f),
   626 (([2,1], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   627 (([2,2], Res), (a * d + c * b) / (b * d) + e / f),
   628 (([2], Res), (a * d + c * b) / (b * d) + e / f),
   629 (([3], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   630 (([4,1], Frm), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   631 (([4,1], Res), (a * (d * f) + b * (c * f) + b * (d * e)) / (b * (d * f))),
   632 (([4,2], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   633 (([4], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   634 (([5], Res), (a * (d * f) + b * (c * f) + b * (d * e)) / (b * (d * f))),
   635 (([6], Res), (a * d * f + b * c * f + b * d * e) / (b * d * f)),
   636 (([], Res), (a * d * f + b * c * f + b * d * e) / (b * d * f))] 
   637 *)
   638 DEconstrCalcTree 1;
   639 
   640 (*POSTPONE CORRECTION AT END OF TRANSITION TO Isabelle/PIDE/Isac (~ Cas_Cmd)
   641 "--------- Take as 1st tac, start with <NEW> (CAS input) ---------";
   642 "--------- Take as 1st tac, start with <NEW> (CAS input) ---------";
   643 "--------- Take as 1st tac, start with <NEW> (CAS input) ---------";
   644 val t = TermC.parse_test @{context} "Diff (x \<up> 2 + x + 1, x)";
   645 case t of Const (\<^const_name>\<open>Diff\<close>, _) $ _ => ()
   646 	| _ => raise 
   647 	      error "diff.sml behav.changed for CAS Diff (..., x)";
   648 TermC.atomty t;
   649 "-----------------------------------------------------------------";
   650 (*1>*)States.reset ();
   651 (*2>*)CalcTree @{context} [([], References.empty)];
   652 (*3>*)Iterator 1;moveActiveRoot 1;
   653 "----- here the Headline has been finished";
   654 (*4>*)moveActiveFormula 1 ([],Pbl);
   655 (*5>*)replaceFormula 1 "Diff (x \<up> 2 + x + 1, x)";
   656 val ((pt,_),_) = States.get_calc 1;
   657 val PblObj {probl, meth, spec, fmz, loc, ...} = get_obj I pt [];
   658 val (SOME istate, NONE) = loc;
   659 (*default_print_depth 5;*)
   660 writeln"-----------------------------------------------------------";
   661 spec;
   662 writeln (I_Model.to_string ctxt probl);
   663 writeln (I_Model.to_string ctxt meth);
   664 writeln (Istate.string_of (fst istate));
   665 
   666 refFormula 1 ([],Pbl) (*--> correct CalcHead*);
   667  (*081016 NOT necessary (but leave it in Java):*)
   668 (*6>*)(*completeCalcHead*)autoCalculate 1 CompleteCalcHead;
   669 "----- here the CalcHead has been completed --- ONCE MORE ?????";
   670 
   671 (***difference II***)
   672 val ((pt,p),_) = States.get_calc 1;
   673 (*val p = ([], Pbl)*)
   674 val PblObj {probl, meth, spec, fmz, loc, ...} = get_obj I pt [];
   675 val (SOME istate, NONE) = loc;
   676 (*default_print_depth 5;*) writeln (Istate.string_of (fst istate));  (*default_print_depth 3;*)
   677 (*Pstate ([],
   678  [], NONE,
   679  ??.empty, Sundef, false)*)
   680 (*default_print_depth 5;*) spec; (*default_print_depth 3;*)
   681 (*("Isac_Knowledge",
   682       ["derivative_of", "function"],
   683       ["diff", "differentiate_on_R"]) : spec*)
   684 writeln (I_Model.to_string ctxt probl);
   685 (*[
   686 (1 ,[1] ,true ,#Given ,Cor functionTerm (x \<up> 2 + x + 1) ,(f_, [x \<up> 2 + x + 1])),
   687 (2 ,[1] ,true ,#Given ,Cor differentiateFor x ,(v_, [x])),
   688 (3 ,[1] ,true ,#Find ,Cor derivative f_'_f ,(f_'_f, [f_'_f]))]*)
   689 writeln (I_Model.to_string ctxt meth);
   690 (*[
   691 (1 ,[1] ,true ,#Given ,Cor functionTerm (x \<up> 2 + x + 1) ,(f_, [x \<up> 2 + x + 1])),
   692 (2 ,[1] ,true ,#Given ,Cor differentiateFor x ,(v_, [x])),
   693 (3 ,[1] ,true ,#Find ,Cor derivative f_'_f ,(f_'_f, [f_'_f]))]*)
   694 writeln"-----------------------------------------------------------";
   695 (*7>*)fetchProposedTactic 1 (*--> Apply_Method*);
   696 (*WN081028 fixed <ERROR> helpless </ERROR> by inform returning ...(.,Met)*)
   697 autoCalculate 1 CompleteCalc;
   698 val ((pt,p),_) = States.get_calc 1;
   699 val Form res = (#1 o ME_Misc.pt_extract) (pt, ([],Res));
   700 Test_Tool.show_pt pt;
   701 
   702 if p = ([], Res) andalso UnparseC.term res = "1 + 2 * x" then ()
   703 else error "diff.sml behav.changed for Diff (x \<up> 2 + x + 1, x)";
   704 DEconstrCalcTree 1;
   705 -----------------------------------------------------------------------------------------------*)
   706 
   707 "--------- Take as 1st tac, start from exp -----------------------";
   708 "--------- Take as 1st tac, start from exp -----------------------";
   709 "--------- Take as 1st tac, start from exp -----------------------";
   710 (*the following input is copied from BridgeLog Java <==> SML,
   711   omitting unnecessary inputs*)
   712 (*1>*)States.reset ();
   713 (*2>*)CalcTree @{context} [(["functionTerm (x \<up> 2 + x + 1)", "differentiateFor x", "derivative f_'_f"],("Isac_Knowledge",["derivative_of", "function"],["diff", "differentiate_on_R"]))];
   714 (*3>*)Iterator 1; moveActiveRoot 1;
   715 
   716 (*6>*)(*completeCalcHead*)autoCalculate 1 CompleteCalcHead;
   717 (***difference II***)
   718 val ((pt,_),_) = States.get_calc 1;
   719 val PblObj {probl, meth, spec, fmz, loc, ...} = get_obj I pt [];
   720 val (SOME istate, NONE) = loc;
   721 (*default_print_depth 5;*) writeln (Istate.string_of (fst istate));  (*default_print_depth 3;*)
   722 (*Pstate ([],
   723  [], NONE,
   724  ??.empty, Sundef, false)*)
   725 (*default_print_depth 5;*) spec; (*default_print_depth 3;*)
   726 (*("Isac_Knowledge",
   727       ["derivative_of", "function"],
   728       ["diff", "differentiate_on_R"]) : spec*)
   729 writeln (I_Model.to_string ctxt probl);
   730 (*[
   731 (1 ,[1] ,true ,#Given ,Cor functionTerm (x \<up> 2 + x + 1) ,(f_, [x \<up> 2 + x + 1])),
   732 (2 ,[1] ,true ,#Given ,Cor differentiateFor x ,(v_, [x])),
   733 (3 ,[1] ,true ,#Find ,Cor derivative f_'_f ,(f_'_f, [f_'_f]))]*)
   734 writeln (I_Model.to_string ctxt meth);
   735 (*[
   736 (1 ,[1] ,true ,#Given ,Cor functionTerm (x \<up> 2 + x + 1) ,(f_, [x \<up> 2 + x + 1])),
   737 (2 ,[1] ,true ,#Given ,Cor differentiateFor x ,(v_, [x])),
   738 (3 ,[1] ,true ,#Find ,Cor derivative f_'_f ,(f_'_f, [f_'_f]))]*)
   739 writeln"-----------------------------------------------------------";
   740 (*7>*)fetchProposedTactic 1 (*--> Apply_Method*);
   741 autoCalculate 1 (Steps 1);
   742 val ((pt,p),_) = States.get_calc 1;
   743 val Form res = (#1 o (ME_Misc.pt_extract ctxt)) (pt, p);
   744 if UnparseC.term res = "d_d x (x \<up> 2 + x + 1)" then ()
   745 else error "diff.sml Diff (x \<up> 2 + x + 1, x) from exp";
   746 DEconstrCalcTree 1;
   747 
   748 (*POSTPONE CORRECTION AT END OF TRANSITION TO Isabelle/PIDE/Isac (~ Cas_Cmd)
   749 "--------- implicit_take, start with <NEW> (CAS input) ---------------";
   750 "--------- implicit_take, start with <NEW> (CAS input) ---------------";
   751 "--------- implicit_take, start with <NEW> (CAS input) ---------------";
   752 States.reset ();
   753 CalcTree @{context} [([], References.empty)];
   754 (*[[from sml: > @@@@@begin@@@@@
   755 [[from sml:  1 
   756 [[from sml: <CALCTREE>
   757 [[from sml:    <CALCID> 1 </CALCID>
   758 [[from sml: </CALCTREE>
   759 [[from sml: @@@@@end@@@@@*)
   760 Iterator 1;
   761 (*[[from sml: > @@@@@begin@@@@@
   762 [[from sml:  1 
   763 [[from sml: <ADDUSER>
   764 [[from sml:   <CALCID> 1 </CALCID>
   765 [[from sml:   <USERID> 1 </USERID>
   766 [[from sml: </ADDUSER>
   767 [[from sml: @@@@@end@@@@@*)
   768 moveActiveRoot 1;
   769 (*[[from sml: > @@@@@begin@@@@@
   770 [[from sml:  1 
   771 [[from sml: <CALCITERATOR>
   772 [[from sml:   <CALCID> 1 </CALCID>
   773 [[from sml:   <POSITION>
   774 [[from sml:     <INTLIST>
   775 [[from sml:     </INTLIST>
   776 [[from sml:     <POS> Pbl </POS>
   777 [[from sml:   </POSITION>
   778 [[from sml: </CALCITERATOR>
   779 [[from sml: @@@@@end@@@@@*)
   780 getFormulaeFromTo 1 ([],Pbl) ([],Pbl) 0 false;
   781 (*[[from sml: > @@@@@begin@@@@@                STILL CORRECT
   782 [[from sml:  1 
   783 [[from sml: <GETELEMENTSFROMTO>
   784 [[from sml:   <CALCID> 1 </CALCID>
   785 [[from sml:   <FORMHEADS>
   786 [[from sml:     <CALCFORMULA>
   787 [[from sml:       <POSITION>
   788 [[from sml:         <INTLIST>
   789 [[from sml:         </INTLIST>
   790 [[from sml:         <POS> Pbl </POS>
   791 [[from sml:       </POSITION>
   792 [[from sml:       <FORMULA>
   793 [[from sml:         <MATHML>
   794 [[from sml:           <ISA> ________________________________________________ </ISA>
   795 [[from sml:         </MATHML>
   796 [[from sml: 
   797 [[from sml:       </FORMULA>
   798 [[from sml:     </CALCFORMULA>
   799 [[from sml:   </FORMHEADS>
   800 [[from sml: </GETELEMENTSFROMTO>
   801 [[from sml: @@@@@end@@@@@*)
   802 refFormula 1 ([],Pbl);
   803 (*[[from sml: > @@@@@begin@@@@@                STILL CORRECT
   804 [[from sml:  1 
   805 [[from sml: <REFFORMULA>
   806 [[from sml:   <CALCID> 1 </CALCID>
   807 [[from sml:   <CALCHEAD status = "incorrect">
   808 [[from sml:     <POSITION>
   809 [[from sml:       <INTLIST>
   810 [[from sml:       </INTLIST>
   811 [[from sml:       <POS> Pbl </POS>
   812 [[from sml:     </POSITION>
   813 [[from sml:     <HEAD>
   814 [[from sml:       <MATHML>
   815 [[from sml:         <ISA> Problem (ThyC.id_empty, [e_pblID]) </ISA>
   816 [[from sml:       </MATHML>
   817 [[from sml:     </HEAD>
   818 [[from sml:     <MODEL>
   819 [[from sml:       <GIVEN>  </GIVEN>
   820 [[from sml:       <WHERE>  </WHERE>
   821 [[from sml:       <FIND>  </FIND>
   822 [[from sml:       <RELATE>  </RELATE>
   823 [[from sml:     </MODEL>
   824 [[from sml:     <BELONGSTO> Pbl </BELONGSTO>
   825 [[from sml:     <SPECIFICATION>
   826 [[from sml:       <THEORYID> ThyC.id_empty </THEORYID>
   827 [[from sml:       <PROBLEMID>
   828 [[from sml:         <STRINGLIST>
   829 [[from sml:           <STRING> Problem.id_empty </STRING>
   830 [[from sml:         </STRINGLIST>
   831 [[from sml:       </PROBLEMID>
   832 [[from sml:       <METHODID>
   833 [[from sml:         <STRINGLIST>
   834 [[from sml:           <STRING> e_metID </STRING>
   835 [[from sml:         </STRINGLIST>
   836 [[from sml:       </METHODID>
   837 [[from sml:     </SPECIFICATION>
   838 [[from sml:   </CALCHEAD>
   839 [[from sml: </REFFORMULA>
   840 [[from sml: @@@@@end@@@@@*)
   841 moveActiveFormula 1 ([],Pbl);
   842 (*[[from sml: > @@@@@begin@@@@@
   843 [[from sml:  1 
   844 [[from sml: <CALCITERATOR>
   845 [[from sml:   <CALCID> 1 </CALCID>
   846 [[from sml:   <POSITION>
   847 [[from sml:     <INTLIST>
   848 [[from sml:     </INTLIST>
   849 [[from sml:     <POS> Pbl </POS>
   850 [[from sml:   </POSITION>
   851 [[from sml: </CALCITERATOR>
   852 [[from sml: @@@@@end@@@@@*)
   853 replaceFormula 1 "Simplify (1+2)";
   854 (*[[from sml: > @@@@@begin@@@@@
   855 [[from sml:  1 
   856 [[from sml: <REPLACEFORMULA>
   857 [[from sml:   <CALCID> 1 </CALCID>
   858 [[from sml:   <CALCCHANGED>
   859 [[from sml:     <UNCHANGED>
   860 [[from sml:       <INTLIST>
   861 [[from sml:       </INTLIST>
   862 [[from sml:       <POS> Pbl </POS>
   863 [[from sml:     </UNCHANGED>
   864 [[from sml:     <DELETED>
   865 [[from sml:       <INTLIST>
   866 [[from sml:       </INTLIST>
   867 [[from sml:       <POS> Pbl </POS>
   868 [[from sml:     </DELETED>
   869 [[from sml:     <GENERATED>
   870 [[from sml:       <INTLIST>
   871 [[from sml:       </INTLIST>
   872 [[from sml:       <POS> Met </POS>                           DIFFERENCE: Pbl
   873 [[from sml:     </GENERATED>
   874 [[from sml:   </CALCCHANGED>
   875 [[from sml: </REPLACEFORMULA>
   876 [[from sml: @@@@@end@@@@@*)
   877 getFormulaeFromTo 1 ([],Pbl) ([],Pbl) 0 false(*              DIFFERENCE: Pbl*);
   878 (*@@@@@begin@@@@@
   879  1
   880 <GETELEMENTSFROMTO>
   881   <CALCID> 1 </CALCID>
   882   <FORMHEADS>
   883     <CALCFORMULA>
   884       <POSITION>
   885         <INTLIST>
   886         </INTLIST>
   887         <POS> Pbl </POS>
   888       </POSITION>
   889       <FORMULA>
   890         <MATHML>
   891           <ISA> Simplify (1 + 2) </ISA>                      WORKS !!!!!
   892         </MATHML>
   893       </FORMULA>
   894     </CALCFORMULA>
   895   </FORMHEADS>
   896 </GETELEMENTSFROMTO>
   897 @@@@@end@@@@@*)
   898 getFormulaeFromTo 1 ([],Pbl) ([],Met) 0 false;
   899 (*[[from sml: > @@@@@begin@@@@@
   900 [[from sml:  1 
   901 [[from sml: <SYSERROR>
   902 [[from sml:   <CALCID> 1 </CALCID>
   903 [[from sml:   <ERROR> error in getFormulaeFromTo </ERROR>
   904 [[from sml: </SYSERROR>
   905 [[from sml: @@@@@end@@@@@*)
   906 (*step into getFormulaeFromTo --- bug corrected...*)
   907 -----------------------------------------------------------------------------------------------*)
   908 
   909 "--------- build fun check_for' ------------------------------";
   910 "--------- build fun check_for' ------------------------------";
   911 "--------- build fun check_for' ------------------------------";
   912 val subst = [(TermC.parse_test @{context} "bdv", TermC.parse_test @{context} "x")]: subst;
   913 val rls = norm_Rational
   914 val pat = TermC.parse_patt @{theory} "(?a + ?b)/(?b + ?c) = ?a / ?c";
   915 val (res, inf) = (TermC.parse_test @{context} "(2 + 3)/(3 + 4)", TermC.parse_test @{context} "2 / 4");
   916 val (res, inf) = (TermC.parse_test @{context} "(2 + 3)/(3 + 4)", TermC.parse_test @{context} "1 / 2");
   917 
   918 val (res', _, _, rewritten) = (*rewritten: the lhs of the pattern TermC.matches in res*)
   919   rew_sub ctxt 1 [] Rewrite_Ord.function_empty Rule_Set.empty false [] (HOLogic.Trueprop $ pat) res;
   920 if rewritten then NONE else SOME "e_errpatID";
   921 
   922 val norm_res = case rewrite_set_ ctxt false rls res' of
   923   NONE => res'
   924 | SOME (norm_res, _) => norm_res
   925 
   926 val norm_inf = case rewrite_set_ ctxt false rls inf of
   927   NONE => inf
   928 | SOME (norm_inf, _) => norm_inf;
   929 
   930 res' = inf;
   931 norm_res = norm_inf;
   932 
   933 val pat = TermC.parse_patt @{theory} "(?a + ?b)/?a = ?b";
   934 val (res, inf) = (TermC.parse_test @{context} "(2 + 3)/2", TermC.parse_test @{context} "3");
   935 if check_for' ctxt (res, inf) subst ("errpatID", pat) rls = SOME "errpatID"
   936 then () else error "error patt example1 changed";
   937 
   938 val pat = TermC.parse_patt @{theory} "(?a + ?b)/(?a + ?c) = ?b / ?c";
   939 val (res, inf) = (TermC.parse_test @{context} "(2 + 3)/(2 + 4)", TermC.parse_test @{context} "3 / 4");
   940 if check_for' ctxt (res, inf) subst ("errpatID", pat) rls = SOME "errpatID"
   941 then () else error "error patt example2 changed";
   942 
   943 val pat = TermC.parse_patt @{theory} "(?a + ?b)/(?b + ?c) = ?a / ?c";
   944 val (res, inf) = (TermC.parse_test @{context} "(2 + 3)/(3 + 4)", TermC.parse_test @{context} "2 / 4");
   945 if check_for' ctxt (res, inf) subst ("errpatID", pat) rls = SOME "errpatID"
   946 then () else error "error patt example3 changed";
   947 
   948 val inf =  TermC.parse_test @{context} "1 / 2";
   949 if check_for' ctxt (res, inf) subst ("errpatID", pat) rls = SOME "errpatID"
   950 then () else error "error patt example3 changed";
   951 
   952 "--------- build fun check_for' ?bdv -------------------------";
   953 "--------- build fun check_for' ?bdv -------------------------";
   954 "--------- build fun check_for' ?bdv -------------------------";
   955 val ctxt = Proof_Context.init_global @{theory}
   956 val subst = [(TermC.parse_test @{context} "bdv", TermC.parse_test @{context} "x")]: subst;
   957 val t = TermC.parse_test @{context} "d_d x (x \<up> 2 + sin (x \<up> 4))";
   958 val SOME (t, _) = rewrite_set_inst_ ctxt false subst norm_diff t;
   959 if UnparseC.term t = "2 * x + cos (x \<up> 4) * 4 * x \<up> 3" then ()
   960 else error "build fun check_for' ?bdv changed 1"; 
   961 
   962 val rls = norm_diff
   963 val pat = TermC.parse_patt @{theory} "d_d ?bdv (sin ?u) = cos (d_d ?bdv ?u)"; 
   964 val (res, inf) = (TermC.parse_test @{context} "2 * x + d_d x (sin (x \<up> 4))", TermC.parse_test @{context} "2 * x + cos (4 * x \<up> 3)");
   965 
   966 val (res', _, _, rewritten) = (*rewritten: the lhs of the pattern TermC.matches in res*)
   967   rew_sub ctxt 1 subst Rewrite_Ord.function_empty Rule_Set.empty false [] (HOLogic.Trueprop $ pat) res;
   968 if UnparseC.term res' = "2 * x + cos (d_d x (x \<up> 4))" andalso rewritten then ()
   969 else error "build fun check_for' ?bdv changed 2";
   970 
   971 val norm_res = case rewrite_set_inst_ ctxt false subst rls  res' of
   972   NONE => res'
   973 | SOME (norm_res, _) => norm_res;
   974 if UnparseC.term norm_res = "2 * x + cos (4 * x \<up> 3)" then ()
   975 else error "build fun check_for' ?bdv changed 3";
   976 
   977 val norm_inf = case rewrite_set_inst_ ctxt false subst rls inf of
   978   NONE => inf
   979 | SOME (norm_inf, _) => norm_inf;
   980 if UnparseC.term norm_inf = "2 * x + cos (4 * x \<up> 3)" then ()
   981 else error "build fun check_for' ?bdv changed 4";
   982 
   983 res' = inf;
   984 if norm_res = norm_inf then ()
   985 else error "build fun check_for' ?bdv changed 5";
   986 
   987 if Error_Pattern.check_for' ctxt (res, inf) (subst: subst) ("errpatID": Error_Pattern.id, pat) rls = SOME "errpatID"
   988 then () else error "error patt example1 changed";
   989 
   990 "--------- build fun check_for ------------------------";
   991 "--------- build fun check_for ------------------------";
   992 "--------- build fun check_for ------------------------";
   993 val (res, inf) =
   994   (TermC.parse_test @{context} "d_d x (x \<up> 2) + d_d x (sin (x \<up>  4))",
   995    TermC.parse_test @{context} "d_d x (x \<up> 2) + cos (4 * x \<up> 3)");
   996 val {errpats, rew_rls = rls, program = Prog prog, ...} = MethodC.from_store ctxt ["diff", "differentiate_on_R"]
   997 
   998 val env = [(TermC.parse_test @{context} "v_v", TermC.parse_test @{context} "x")];
   999 val errpats =
  1000   [Error_Pattern.empty, (*generalised for testing*)
  1001    ("chain-rule-diff-both",
  1002      [TermC.parse_patt @{theory} "d_d ?bdv (sin ?u) = cos (d_d ?bdv ?u)",
  1003       TermC.parse_patt @{theory} "d_d ?bdv (cos ?u) = - sin (d_d ?bdv ?u)",
  1004       TermC.parse_patt @{theory} "d_d ?bdv (?u \<up> ?n) = ?n * ?u \<up> (?n - 1)",
  1005       TermC.parse_patt @{theory} "d_d ?bdv (LogExp.ln ?u) = 1 / ?u",
  1006       TermC.parse_patt @{theory} "d_d ?bdv (LogExp.ln ?u) = 1 / d_d ?bdv ?u"],
  1007      [@{thm diff_sin_chain}, @{thm diff_cos_chain}, @{thm diff_pow_chain}, 
  1008       @{thm diff_ln_chain}, @{thm  diff_exp_chain}])];
  1009 case Error_Pattern.check_for ctxt (res, inf) (prog, env) (errpats, rls) of SOME _ => () 
  1010 | NONE => error "Error_Pattern.check_for broken";
  1011 DEconstrCalcTree 1;
  1012 
  1013 "--------- embed fun check_for ------------------------";
  1014 "--------- embed fun check_for ------------------------";
  1015 "--------- embed fun check_for ------------------------";
  1016 States.reset ();     
  1017 CalcTree @{context}
  1018 [(["functionTerm (x \<up> 2 + sin (x \<up> 4))", "differentiateFor x", "derivative f_f'"], 
  1019   ("Isac_Knowledge", ["derivative_of", "function"], ["diff", "differentiate_on_R"]))];
  1020 Iterator 1;
  1021 moveActiveRoot 1;
  1022 autoCalculate 1 CompleteCalcHead;
  1023 autoCalculate 1 (Steps 1);
  1024 autoCalculate 1 (Steps 1);(*([1], Res), d_d x (x \<up> 2) + d_d x (sin (x \<up> 4))*)
  1025 (*autoCalculate 1 (Steps 1);([2], Res), d_d x (x \<up> 2) + cos (x \<up> 4) * d_d x (x \<up> \<up>  4)*)
  1026 
  1027 "~~~~~ fun appendFormula , args:"; val (cI, (ifo:TermC.as_string)) = (1, "d_d x (x \<up> 2) + cos (4 * x \<up> 3)");
  1028 "~~~~~ fun appendFormula' , args:"; val (cI, (ifo: TermC.as_string)) = (cI, ifo);
  1029     val cs = States.get_calc cI
  1030     val pos = States.get_pos cI 1;
  1031 (*+*)if pos = ([1], Res) then () else error "inform with (positive) Error_Pattern.check_for broken 1";
  1032     val ("ok", cs' as (_, _, ptp)) = (*case*) Step.do_next pos cs (*of*);
  1033       (*case*) Step_Solve.by_term ptp (encode ifo) (*of*); (*ERROR WAS: "no derivation found"*)
  1034 "~~~~~ fun Step_Solve.by_term , args:"; val (((*next_*)cs as (_, _, (pt, pos as (p, _))): Calc.state_post), istr)
  1035   = (cs', (encode ifo));
  1036     val ctxt = get_ctxt pt pos (*see TODO.thy*)
  1037     val SOME f_in = (*case*) TermC.parseNEW ctxt istr (*of*);
  1038         val pos_pred = lev_back' pos
  1039     	  val f_pred = Ctree.get_curr_formula (pt, pos_pred);
  1040         (*if*) f_pred = f_in; (*else*)
  1041           val NONE = (*case*) CAS_Cmd.input f_in (*of*);
  1042        (*old* )val (_, _, metID) = get_obj g_spec pt (par_pblobj pt p)
  1043        (*old*)val {program = prog, ...} = MethodC.from_store ctxt metID
  1044        (*old*)val istate = get_istate_LI pt pos
  1045        (*old*)val ctxt = get_ctxt pt pos
  1046        ( *old*)
  1047        val LI.Not_Derivable =
  1048              (*case*) LI.locate_input_term (pt, pos) f_in (*of*);
  1049             		  val pp = Ctree.par_pblobj pt p
  1050             		  val (errpats, rew_rls, prog) = case MethodC.from_store ctxt (Ctree.get_obj Ctree.g_metID pt pp) of
  1051               		    {errpats, rew_rls, program = Rule.Prog prog, ...} => (errpats, rew_rls, prog)
  1052               		  | _ => error "inform: uncovered case of MethodC.from_store ctxt"
  1053 ;
  1054 (*+*)if Error_Pattern.s_to_string errpats = "[(\"chain-rule-diff-both\",\n[\"d_d ?bdv (sin ?u) = cos (d_d ?bdv ?u)\", \"d_d ?bdv (cos ?u) = - sin (d_d ?bdv ?u)\", \"d_d ?bdv (?u \<up> ?n) = ?n * ?u \<up> (?n - 1)\", \"d_d ?bdv (LogExp.ln ?u) = 1 / ?u\", \"d_d ?bdv (LogExp.ln ?u) = 1 / d_d ?bdv ?u\"],\n[\"d_d ?bdv (sin ?u) = cos ?u * d_d ?bdv ?u\", \"d_d ?bdv (cos ?u) = - sin ?u * d_d ?bdv ?u\", \"d_d ?bdv (?u \<up> ?n) = ?n * ?u \<up> (?n - 1) * d_d ?bdv ?u\", \"d_d ?bdv (LogExp.ln ?u) = d_d ?bdv ?u / ?u\", \"d_d ?bdv (exp ?u) = exp ?u * d_d ?x ?u\"]]"
  1055 (*+*)then () else error "inform with (positive) Error_Pattern.check_for broken 3";
  1056 
  1057             		  val {env, ...} = Ctree.get_istate_LI pt pos |> Istate.the_pstate
  1058 ;
  1059 (*+*)if UnparseC.term f_pred = "d_d x (x \<up> 2) + d_d x (sin (x \<up> 4))" andalso
  1060 (*+*)   UnparseC.term f_in = "d_d x (x \<up> 2) + cos (4 * x \<up> 3)"
  1061 (*+*)then () else error "inform with (positive) Error_Pattern.check_for broken 2";
  1062 
  1063              val SOME "chain-rule-diff-both" = (*case*) Error_Pattern.check_for ctxt (f_pred, f_in) (prog, env) (errpats, rew_rls) (*of*);
  1064 
  1065 "--- final check:";
  1066 (*+*)val (_, _, ptp') = cs';
  1067 case Step_Solve.by_term ptp' (encode ifo) of
  1068   ("error pattern #chain-rule-diff-both#", calcstate') => ()
  1069 | _ => error "inform with (positive) Error_Pattern.check_for broken"
  1070 
  1071 
  1072 "--------- embed fun find_fill_patterns ---------------------------";
  1073 "--------- embed fun find_fill_patterns ---------------------------";
  1074 "--------- embed fun find_fill_patterns ---------------------------";
  1075 States.reset ();
  1076 CalcTree @{context}
  1077 [(["functionTerm (x \<up> 2 + sin (x \<up> 4))", "differentiateFor x", "derivative f_f'"], 
  1078   ("Isac_Knowledge", ["derivative_of", "function"], ["diff", "differentiate_on_R"]))];
  1079 Iterator 1;
  1080 moveActiveRoot 1;
  1081 autoCalculate 1 CompleteCalcHead;
  1082 autoCalculate 1 (Steps 1);
  1083 autoCalculate 1 (Steps 1);(*([1], Res), d_d x (x \<up> 2) + d_d x (sin (x \<up> 4))*)
  1084 appendFormula 1 "d_d x (x \<up> 2) + cos (4 * x \<up> 3)" (*|> Future.join*);
  1085   (*<CALCMESSAGE> error pattern #chain-rule-diff-both# </CALCMESSAGE>*)
  1086   (*or
  1087     <CALCMESSAGE> no derivation found </CALCMESSAGE>*)
  1088 
  1089 "~~~~~ fun findFillpatterns, args:"; val (cI, errpatID) = (1, "chain-rule-diff-both");
  1090   val ((pt, _), _) = States.get_calc cI
  1091 				val pos = States.get_pos cI 1;
  1092 "~~~~~ fun find_fill_patterns , args:"; val ((pt, pos as (p, _)), errpatID) = ((pt, pos), errpatID);
  1093     val f_curr = Ctree.get_curr_formula (pt, pos);
  1094     val pp = Ctree.par_pblobj pt p
  1095     val (errpats, prog) = case MethodC.from_store ctxt (Ctree.get_obj Ctree.g_metID pt pp) of
  1096       {errpats, program = Rule.Prog prog, ...} => (errpats, prog)
  1097     | _ => error "find_fill_patterns: uncovered case of MethodC.from_store ctxt"
  1098     val {env, ...} = Ctree.get_istate_LI pt pos |> Istate.the_pstate
  1099     val subst = Subst.for_bdv ctxt prog env
  1100     val errpatthms = errpats
  1101       |> filter ((curry op = errpatID) o (#1: Error_Pattern.T -> Error_Pattern.id))
  1102       |> map (#3: Error_Pattern.T -> thm list)
  1103       |> flat;
  1104 
  1105 case map (Error_Pattern.fill_from_store ctxt subst f_curr errpatID) errpatthms |> flat of
  1106   ("fill-d_d-arg", tm, thm, subs_opt) :: _ => if UnparseC.term tm = 
  1107     "d_d x (x \<up> 2) + d_d x (sin (x \<up> 4)) =\nd_d x (x \<up> 2) + cos (x \<up> 4) * d_d x ?_dummy_1"
  1108     then () else error "find_fill_patterns changed 1a"
  1109 | _ => error "find_fill_patterns changed 1b"
  1110 
  1111 "~~~~~ fun fill_from_store, args:"; val (ctxt, subst, form, errpatID, thm) =
  1112   (ctxt, subst, f_curr, errpatID, hd (*simulate beginning of "map"*) errpatthms);
  1113         val thmDeriv = Thm.get_name_hint thm
  1114         val (part, thyID) = Thy_Read.thy_containing_thm thmDeriv
  1115         val theID = [part, thyID, "Theorems", ThmC.cut_id thmDeriv]
  1116         val Thy_Write.Hthm {fillpats, ...} = Thy_Read.from_store ctxt theID
  1117         val some = map (Error_Pattern.fill_form ctxt subst (thm, form) errpatID) fillpats;
  1118 
  1119 case some |> filter is_some |> map the of
  1120   ("fill-d_d-arg", tm, thm, subsopt) :: _ => if UnparseC.term tm = 
  1121     "d_d x (x \<up> 2) + d_d x (sin (x \<up> 4)) =\nd_d x (x \<up> 2) + cos (x \<up> 4) * d_d x ?_dummy_1"
  1122     then () else error "find_fill_patterns changed 2a"
  1123 | _ => error "find_fill_patterns changed 2b"
  1124 
  1125 "~~~~~ fun fill_form, args:";
  1126   val ((subs_opt, subst), (thm, form), errpatID, (fillpatID, pat, erpaID)) =
  1127   (subst, (thm, form), errpatID, hd (*simulate beginning of "map"*) fillpats);
  1128 val (form', _, _, rewritten) =
  1129       rew_sub ctxt 1 subst Rewrite_Ord.function_empty Rule_Set.empty false [] (HOLogic.Trueprop $ pat) form;
  1130 
  1131 if UnparseC.term form' = "d_d x (x \<up> 2) + cos (x \<up> 4) * d_d x ?_dummy_1" then ()
  1132 else error "find_fill_patterns changed 3";
  1133 
  1134 "~~~~~ to findFillpatterns return val:"; val (fillpats) =
  1135   (map (Error_Pattern.fill_from_store ctxt (subs_opt, subst) f_curr errpatID) errpatthms |> flat) (*only from "hd errpatthms"*);
  1136 
  1137 "vvv--- dropped this code WN120730";
  1138 val msg = "fill patterns " ^
  1139   ((map ((apsnd UnparseC.term) o quad2pair) fillpats) |> map pair2str_ |> strs2str_);
  1140 msg =
  1141   "fill patterns #fill-d_d-arg#d_d x (x \<up> 2) + d_d x (sin (x \<up> 4))" ^
  1142     " =\nd_d x (x \<up> 2) + cos (x \<up> 4) * d_d x ?_dummy_1" ^
  1143   "#fill-both-args#d_d x (x \<up> 2) + d_d x (sin (x \<up> 4))" ^
  1144     " =\nd_d x (x \<up> 2) + cos ?_dummy_2 * d_d x ?_dummy_3" ^
  1145   "#fill-d_d#d_d x (x \<up> 2) + d_d x (sin (x \<up> 4))" ^
  1146     " =\nd_d x (x \<up> 2) + cos (x \<up> 4) * ?_dummy_1 x (x \<up> 4)" ^
  1147   "#fill-inner-deriv#d_d x (x \<up> 2) + d_d x (sin (x \<up> 4))" ^
  1148     " =\nd_d x (x \<up> 2) + cos (x \<up> 4) * ?_dummy_1" ^
  1149   "#fill-all#d_d x (x \<up> 2) + d_d x (sin (x \<up> 4)) = d_d x (x \<up> 2) + ?_dummy_1#";
  1150 " \<up> --- dropped this code WN120730";
  1151 
  1152 if (map #1 fillpats) =
  1153   ["fill-d_d-arg", "fill-both-args", "fill-d_d", "fill-inner-deriv", "fill-all"]
  1154 then () else error "find_fill_patterns changed 4b";
  1155 DEconstrCalcTree 1;
  1156 
  1157 "--------- build fun is_exactly_equal, inputFillFormula ----------";
  1158 "--------- build fun is_exactly_equal, inputFillFormula ----------";
  1159 "--------- build fun is_exactly_equal, inputFillFormula ----------";
  1160 States.reset ();
  1161 CalcTree @{context}
  1162 [(["functionTerm (x \<up> 2 + sin (x \<up> 4))", "differentiateFor x", "derivative f_f'"], 
  1163   ("Isac_Knowledge", ["derivative_of", "function"], ["diff", "differentiate_on_R"]))];
  1164 Iterator 1;
  1165 moveActiveRoot 1;
  1166 autoCalculate 1 CompleteCalcHead;
  1167 autoCalculate 1 (Steps 1);
  1168 autoCalculate 1 (Steps 1);(*([1], Res), d_d x (x \<up> 2) + d_d x (sin (x \<up> 4))*)
  1169 appendFormula 1 "d_d x (x \<up> 2) + cos (4 * x \<up> 3)" (*|> Future.join*); (*<<<<<<<=========================*)
  1170 (* the check for errpat is maximally liberal (whole term modulo "rew_rls" from "type met"),
  1171   would recognize "cos (4 * x \<up> (4 - 1)) + 2 * x" as well.
  1172   results in <CALCMESSAGE> error pattern #chain-rule-diff-both# </CALCMESSAGE>
  1173   instead of <CALCMESSAGE> no derivation found </CALCMESSAGE> *)
  1174   val ((pt,pos), _) = States.get_calc 1;
  1175   val p = States.get_pos 1 1;
  1176   val (Form f, _, asms) = ME_Misc.pt_extract ctxt (pt, p);
  1177 
  1178   if p = ([1], Res) andalso UnparseC.term f = "d_d x (x \<up> 2) + d_d x (sin (x \<up> 4))" then
  1179     case get_obj g_tac pt (fst p) of Rewrite_Inst (["(''bdv'', x)"], 
  1180       ("diff_sum", thm)) =>
  1181       if (UnparseC.term o Thm.prop_of) thm = "d_d ?bdv (?u + ?v) = d_d ?bdv ?u + d_d ?bdv ?v" then ()
  1182       else error "embed fun fill_form changed 11"
  1183     | _ => error "embed fun fill_form changed 12"
  1184   else error "embed fun fill_form changed 13";
  1185 
  1186 findFillpatterns 1 "chain-rule-diff-both"; (*<<<<<<<=================================*)
  1187 (*<CALCMESSAGE> fill patterns #fill-d_d-arg#d_d x (x \<up> 2) + d_d x (sin (x \<up>  4)) =
  1188   d_d x (x \<up> 2) + cos (x \<up> 4) * d_d x ?_dummy_1#fill-both-args#...#...#... *)
  1189   val ((pt,pos),_) = States.get_calc 1;
  1190   val p = States.get_pos 1 1;
  1191 
  1192   val (Form f, _, asms) = ME_Misc.pt_extract ctxt (pt, p);
  1193   if p = ([1], Res) andalso UnparseC.term f = "d_d x (x \<up> 2) + d_d x (sin (x \<up> 4))" then
  1194     case get_obj g_tac pt (fst p) of Rewrite_Inst (["(''bdv'', x)"], 
  1195       ("diff_sum", thm)) =>
  1196       if (UnparseC.term o Thm.prop_of) thm = "d_d ?bdv (?u + ?v) = d_d ?bdv ?u + d_d ?bdv ?v" then ()
  1197       else error "embed fun fill_form changed 21"
  1198     | _ => error "embed fun fill_form changed 22"
  1199   else error "embed fun fill_form changed 23";
  1200 
  1201 requestFillformula 1 ("chain-rule-diff-both", "fill-both-args");(*<<<<<<<============*)
  1202   (*<AUTOCALC> ([1], Res) ([2], Res) ([2], Res) </AUTOCALC>*)
  1203   val ((pt,pos),_) = States.get_calc 1;
  1204   val p = States.get_pos 1 1;
  1205   val (Form f, _, asms) = ME_Misc.pt_extract ctxt (pt, p);
  1206   if p = ([1], Res) andalso existpt [2] pt
  1207     andalso UnparseC.term f = "d_d x (x \<up> 2) + d_d x (sin (x \<up> 4))"
  1208   then case get_obj g_tac pt (fst p) of Rewrite_Inst (["(''bdv'', x)"], 
  1209       ("diff_sum", thm)) =>
  1210       if (UnparseC.term o Thm.prop_of) thm = "d_d ?bdv (?u + ?v) = d_d ?bdv ?u + d_d ?bdv ?v" then ()
  1211       else error "embed fun fill_form changed 31"
  1212     | _ => error "embed fun fill_form changed 32"
  1213   else error "embed fun fill_form changed 33";
  1214 
  1215 (* input a formula which exactly fills the gaps in a "fillformula"
  1216    presented to the learner immediately before by "requestFillformula (errpatID, fillpatID)":
  1217    errpatID: lhs of the respective thm = lhs of fillformula with fillpatID.
  1218    the respective thm is in the ctree ................
  1219 *)
  1220 "~~~~~ fun inputFillFormula, args:"; val (cI, ifo) =
  1221   (1, "d_d x (x \<up> 2) + cos (x \<up> 4) * d_d x (x \<up> 4)");
  1222     val ((pt, _), _) = States.get_calc cI
  1223     val pos = States.get_pos cI 1;
  1224 
  1225 "~~~~~ fun is_exactly_equal, args:"; val ((pt, pos as (p, p_)), istr) = ((pt, pos), ifo);
  1226   val SOME ifo = parseNEW (ThyC.get_theory "Isac_Knowledge" |> Proof_Context.init_global) istr
  1227   val p' = lev_on p;
  1228   val tac = get_obj g_tac pt p';
  1229 val Rewrite_Inst ([bbb as "(''bdv'', x)"], ("diff_sin_chain", ttt)) = tac;
  1230 if (UnparseC.term o Thm.prop_of) ttt = "d_d ?bdv (sin ?u) = cos ?u * d_d ?bdv ?u" then ()
  1231 else error "inputFillFormula changed 10";
  1232   val Applicable.Yes rew = Step.check tac (pt, pos);
  1233   val Rewrite_Inst' (_, _, _, _, _, _, _, (res, _)) = rew;
  1234 
  1235 "~~~~~ to inputFillFormula return val:"; val ("ok", tac) = ("ok", tac);
  1236   val ("ok", (_, c, ptp as (_,p'))) = Step.by_tactic tac (pt, pos);
  1237     States.upd_calc cI (ptp, []);
  1238     States.upd_ipos cI 1 p';
  1239     autocalculateOK2xml cI pos (if null c then p' else last_elem c) p';
  1240 
  1241 "~~~~~ final check:";
  1242 val ((pt, _),_) = States.get_calc 1;
  1243 val p = States.get_pos 1 1;
  1244 val (Form f, _, asms) = ME_Misc.pt_extract ctxt (pt, p);
  1245   if p = ([2], Res) andalso UnparseC.term f = "d_d x (x \<up> 2) + cos (x \<up> 4) * d_d x (x \<up> 4)"
  1246   then case get_obj g_tac pt (fst p) of Rewrite_Inst (["(''bdv'', x)"], 
  1247       ("diff_sin_chain", thm)) =>
  1248       if (UnparseC.term o Thm.prop_of) thm = "d_d ?bdv (sin ?u) = cos ?u * d_d ?bdv ?u" then ()
  1249       else error "inputFillFormula changed 111"
  1250     | _ => error "inputFillFormula changed 112"
  1251   else error "inputFillFormula changed 113";
  1252 
  1253 "--------- fun appl_adds -----------------------------------------";
  1254 "--------- fun appl_adds -----------------------------------------";
  1255 "--------- fun appl_adds -----------------------------------------";
  1256 (* val (dI, oris, model, pbt, selct::ss) = 
  1257        (dI, pors, probl, model, map itms2fstr probl);
  1258    ...vvv
  1259    *)
  1260 (* val (dI, oris, model, pbt, (selct::ss))=
  1261        (#1 (References.select_input ospec spec), oris, []:I_Model.T,
  1262 	((#model o Problem.from_store) (#2 (References.select_input ospec spec))),(imodel2fstr imodel));
  1263    val iii = appl_adds dI oris model pbt (selct::ss); 
  1264    tracing(I_Model.to_string thy iii);
  1265 
  1266  val itm = add_single' dI oris model pbt selct;
  1267  val model = I_Model.add itm model;
  1268 
  1269  val _::selct::ss = (selct::ss);
  1270  val itm = add_single' dI oris model pbt selct;
  1271  val model = I_Model.add itm model;
  1272 
  1273  val _::selct::ss = (selct::ss);
  1274  val itm = add_single' dI oris model pbt selct;
  1275  val model = I_Model.add itm model;
  1276  tracing(I_Model.to_string thy model);
  1277 
  1278  val _::selct::ss = (selct::ss);
  1279  val itm = add_single' dI oris model pbt selct;
  1280  val model = I_Model.add itm model;
  1281    *)
  1282 "--------- fun concat_deriv --------------------------------------";
  1283 "--------- fun concat_deriv --------------------------------------";
  1284 "--------- fun concat_deriv --------------------------------------";
  1285 (*
  1286  val ({rew_ord, asm_rls, rules,...}, fo, ifo) = 
  1287      (Rule_Set.rep Test_simplify, TermC.parse_test @{context} "x+1+ - 1*2=0", TermC.parse_test @{context} "-2*1+(x+1)=0");
  1288  (tracing o Derive.trtas2str) fod';
  1289 > ["
  1290 (x + 1 + - 1 * 2 = 0, Thm ("radd_commute", "?m + ?n = ?n + ?m"), (- 1 * 2 + (x + 1) = 0, []))", "
  1291 (- 1 * 2 + (x + 1) = 0, Thm ("radd_commute", "?m + ?n = ?n + ?m"), (- 1 * 2 + (1 + x) = 0, []))", "
  1292 (- 1 * 2 + (1 + x) = 0, Thm ("radd_left_commute", "?x + (?y + ?z) = ?y + (?x + ?z)"), (1 + (- 1 * 2 + x) = 0, []))", "
  1293 (1 + (- 1 * 2 + x) = 0, Thm ("#mult_Float ((~1,0), (0,0)) __ ((2,0), (0,0))", "- 1 * 2 = -2"), (1 + (-2 + x) = 0, []))"]
  1294 val it = () : unit
  1295  (tracing o Derive.trtas2str) (map Derive.rev_deriv' rifod');
  1296 > ["
  1297 (1 + (-2 + x) = 0, Thm ("sym_#mult_Float ((~2,0), (0,0)) __ ((1,0), (0,0))", "-2 = -2 * 1"), (1 + (-2 * 1 + x) = 0, []))", "
  1298 (1 + (-2 * 1 + x) = 0, Thm ("sym_radd_left_commute", "?y + (?x + ?z) = ?x + (?y + ?z)"), (-2 * 1 + (1 + x) = 0, []))", "
  1299 (-2 * 1 + (1 + x) = 0, Thm ("sym_radd_commute", "?n + ?m = ?m + ?n"), (-2 * 1 + (x + 1) = 0, []))"]
  1300 val it = () : unit
  1301 *)
  1302 "--------- handle an input formula -------------------------------";
  1303 "--------- handle an input formula -------------------------------";
  1304 "--------- handle an input formula -------------------------------";
  1305 (*
  1306 Untersuchung zur Formeleingabe (appendFormula, replaceFormla) zu einer Anregung von Alan Krempler:
  1307 Welche RICHTIGEN Formeln koennen NICHT abgeleitet werden, 
  1308 wenn Abteilungen nur auf gleichem Level gesucht werden ?
  1309 WN.040216 
  1310 
  1311 Beispiele zum Equationsolver von Richard Lang aus /src/sml/kbtest/rlang.sml
  1312 
  1313 ------------------------------------------------------------------------------
  1314 "Schalk I s.87 Bsp 52a ((5*x)/(x - 2) - x/(x+2)=4)";
  1315 ------------------------------------------------------------------------------
  1316 1. "5 * x / (x - 2) - x / (x + 2) = 4"
  1317 ...
  1318 4. "12 * x + 4 * x \<up> 2 = 4 * (-4 + x \<up> 2)",Subproblem["normalise", "poly"..
  1319 ...
  1320 4.3. "16 + 12 * x = 0", Subproblem["degree_1", "polynomial", "univariate"..
  1321 ...
  1322 4.3.3. "[x = -4 / 3]")), Check_elementwise "Assumptions"
  1323 ...
  1324 "[x = -4 / 3]"
  1325 ------------------------------------------------------------------------------
  1326 (1)..(6): keine 'richtige' Eingabe kann abgeleitet werden, die einen Summanden auf die andere Seite verschiebt [Ableitung ware in 4.3.n]
  1327 
  1328 (4.1)..(4.3): keine 'richtige' Eingabe kann abgeleitet werden, die einen Summanden auf die andere Seite verschiebt [Ableitung ware in 4.3.n]
  1329 ------------------------------------------------------------------------------
  1330 
  1331 
  1332 ------------------------------------------------------------------------------
  1333 "Schalk I s.87 Bsp 55b (x/(x \<up> 2 - 6*x+9) - 1/(x \<up> 2 - 3*x) =1/x)";
  1334 ------------------------------------------------------------------------------
  1335 1. "x / (x \<up> 2 - 6 * x + 9) - 1 / (x \<up> 2 - 3 * x) = 1 / x"
  1336 ...
  1337 4. "(3 + (- 1 * x + x \<up> 2)) * x = 1 * (9 * x + (x \<up> 3 + -6 * x \<up> 2))"
  1338                          Subproblem["normalise", "polynomial", "univariate"..
  1339 ...
  1340 4.4. "-6 * x + 5 * x \<up> 2 = 0", Subproblem["bdv_only", "degree_2", "poly"..
  1341 ...
  1342 4.4.4. "[x = 0, x = 6 / 5]", Check_elementwise "Assumptions"
  1343 4.4.5. "[x = 0, x = 6 / 5]"
  1344 ...
  1345 5. "[x = 0, x = 6 / 5]", Check_elementwise "Assumptions"
  1346    "[x = 6 / 5]"
  1347 ------------------------------------------------------------------------------
  1348 (1)..(4): keine 'richtige' Eingabe kann abgeleitet werden, die einen Summanden auf die andere Seite schiebt [Ableitung waere in 4.4.x]
  1349 
  1350 (4.1)..(4.4.5): keine 'richtige' Eingabe kann abgeleitet werden, die dem Ergebnis "[x = 6 / 5]" aequivalent ist [Ableitung waere in 5.]
  1351 ------------------------------------------------------------------------------
  1352 
  1353 
  1354 ------------------------------------------------------------------------------
  1355 "Schalk II s.56 Bsp 73b (sqrt(x+1)+sqrt(4*x+4)=sqrt(9*x+9))";
  1356 ------------------------------------------------------------------------------
  1357 1. "sqrt (x + 1) + sqrt (4 * x + 4) = sqrt (9 * x + 9)"
  1358 ...
  1359 6. "13 + 13 * x + -2 * sqrt ((4 + 4 * x) * (9 + 9 * x)) = 1 + x"
  1360                              Subproblem["sq", "rootX", "univariate", "equation"]
  1361 ...
  1362 6.6. "144 + 288 * x + 144 * x \<up> 2 = 144 + x \<up> 2 + 288 * x + 143 * x \<up> 2"
  1363                 Subproblem["normalise", "polynomial", "univariate", "equation"]
  1364 ...6.6.3 "0 = 0"    Subproblem["degree_0", "polynomial", "univariate", "equation"]
  1365 ...                                       Or_to_List
  1366 6.6.3.2 "UniversalList"
  1367 ------------------------------------------------------------------------------
  1368 (1)..(6): keine 'richtige' Eingabe kann abgeleitet werden, die eine der Wurzeln auf die andere Seite verschieb [Ableitung ware in 6.6.n]
  1369 
  1370 (6.1)..(6.3): keine 'richtige' Eingabe kann abgeleitet werden, die einen Summanden auf die andere Seite verschiebt [Ableitung ware in 6.6.n]
  1371 ------------------------------------------------------------------------------
  1372 *)
  1373 (*sh. comments auf 498*)
  1374 "--------- fun dropwhile' ----------------------------------------";
  1375 "--------- fun dropwhile' ----------------------------------------";
  1376 "--------- fun dropwhile' ----------------------------------------";
  1377 (*
  1378  fun equal a b = a=b;
  1379  val foder = [0,1,2,3,4,5]; val ifoder = [11,12,3,4,5];
  1380  val r_foder = rev foder;  val r_ifoder = rev ifoder;
  1381  dropwhile' equal r_foder r_ifoder;
  1382 > vval it = ([0, 1, 2, 3], [3, 12, 11]) : int list * int list
  1383 
  1384  val foder = [3,4,5]; val ifoder = [11,12,3,4,5];
  1385  val r_foder = rev foder;  val r_ifoder = rev ifoder;
  1386  dropwhile' equal r_foder r_ifoder;
  1387 > val it = ([3], [3, 12, 11]) : int list * int list
  1388 
  1389  val foder = [5]; val ifoder = [11,12,3,4,5];
  1390  val r_foder = rev foder;  val r_ifoder = rev ifoder;
  1391  dropwhile' equal r_foder r_ifoder;
  1392 > val it = ([5], [5, 4, 3, 12, 11]) : int list * int list
  1393 
  1394  val foder = [10,11,12,13,14,15]; val ifoder = [11,12,3,4,5];
  1395  val r_foder = rev foder;  val r_ifoder = rev ifoder;
  1396  dropwhile' equal r_foder r_ifoder;
  1397 > *** dropwhile': did not start with equal elements*)