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