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