test/Tools/isac/Interpret/inform.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Mon, 07 Dec 2015 14:10:59 +0100
changeset 59188 c477d0f79ab9
parent 59123 5127be395ea1
child 59248 5eba5e6d5266
permissions -rw-r--r--
Isabelle2014-->15: closed Thm.thy applied to tests

Note:
# concerns the funs: assbl_thm, cterm_of-->global_cterm_of, make_thm,
prop_of, rep_thm, rep_thm_G, therm_of.
# tests have not been run since start of Isabelle2014-->15.
     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 "-----------------------------------------------------------------";
    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 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 = 2*)
    61  autoCalculate' 1 (Step 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_ptree 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"}) 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"}) 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 
   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 (Step 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  if tac = Rewrite_Set "norm_equation" then ()
   164  else 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 
   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 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 = 2*)
   181  autoCalculate' 1 (Step 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_ptree 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  if tac = Rewrite_Set "Test_simplify" then ()
   194  else 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 
   200 
   201 "--------- appendFormula: on Res + late deriv --------------------";
   202 "--------- appendFormula: on Res + late deriv --------------------";
   203 "--------- appendFormula: on Res + late deriv --------------------";
   204  reset_states ();
   205  CalcTree [(["equality (x+1=(2::real))", "solveFor x","solutions L"], 
   206 	    ("Test", ["sqroot-test","univariate","equation","test"],
   207 	     ["Test","squ-equ-test-subpbl1"]))];
   208  Iterator 1; moveActiveRoot 1;
   209  autoCalculate' 1 CompleteCalcHead;
   210  autoCalculate' 1 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 = 2*)
   211  autoCalculate' 1 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 + -1 * 2 = 0*);
   212 
   213  appendFormula 1 "x = 1" (*|> Future.join*);
   214  val ((pt,p),_) = get_calc 1;
   215  val str = pr_ptree pr_short pt;
   216  writeln str;
   217  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)
   218  then () (*finds 1 step too early: ([3,2], Res) "x = 1" also by script !!!*)
   219  else error "inform.sml: diff.behav.appendFormula: Res + late d 1";
   220 
   221  fetchProposedTactic 1;
   222  val (_,(tac,_,_)::_) = get_calc 1;
   223  if tac = Check_Postcond ["LINEAR", "univariate", "equation", "test"] then ()
   224  else error "inform.sml: diff.behav.appendFormula: Res + late d 2";
   225  autoCalculate' 1 CompleteCalc;
   226  val ((pt,_),_) = get_calc 1;
   227  if "[x = 1]" = term2str (fst (get_obj g_result pt [])) then ()
   228  else error "inform.sml: diff.behav.appendFormula: Res + late d 3";
   229 
   230 
   231 "--------- appendFormula: on Res + late deriv [x = 3 + -2]---///--";
   232 "--------- appendFormula: on Res + late deriv [x = 3 + -2]---///--";
   233 "--------- appendFormula: on Res + late deriv [x = 3 + -2]---///--";
   234  reset_states ();
   235  CalcTree [(["equality (x+1=(2::real))", "solveFor x","solutions L"], 
   236 	    ("Test", ["sqroot-test","univariate","equation","test"],
   237 	     ["Test","squ-equ-test-subpbl1"]))];
   238  Iterator 1; moveActiveRoot 1;
   239  autoCalculate' 1 CompleteCalcHead;
   240  autoCalculate' 1 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 = 2*)
   241  autoCalculate' 1 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 + -1 * 2 = 0*);
   242  appendFormula 1 "[x = 3 + -2*1]" (*|> Future.join*);
   243  val ((pt,p),_) = get_calc 1;
   244  val str = pr_ptree pr_short pt;
   245  writeln str;
   246  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 ()
   247  else error "inform.sml: diff.behav.appendFormula: Res + latEE 1";
   248  autoCalculate' 1 CompleteCalc;
   249  val ((pt,p),_) = get_calc 1;
   250  if "[x = 3 + -2 * 1]" = term2str (fst (get_obj g_result pt [])) then ()
   251  (*       ~~~~~~~~~~ simplify as last step in any script ?!*)
   252  else error "inform.sml: diff.behav.appendFormula: Res + latEE 2";
   253 
   254 
   255 
   256 "--------- replaceFormula: on Res + = ----------------------------";
   257 "--------- replaceFormula: on Res + = ----------------------------";
   258 "--------- replaceFormula: on Res + = ----------------------------";
   259  reset_states ();
   260  CalcTree [(["equality (x+1=(2::real))", "solveFor x","solutions L"], 
   261 	    ("Test", ["sqroot-test","univariate","equation","test"],
   262 	     ["Test","squ-equ-test-subpbl1"]))];
   263  Iterator 1; moveActiveRoot 1;
   264  autoCalculate' 1 CompleteCalcHead;
   265  autoCalculate' 1 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 = 2*)
   266  autoCalculate' 1 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 + -1 * 2 = 0*);
   267  autoCalculate' 1 (Step 1); refFormula 1 (get_pos 1 1);(*-1 + x*);
   268 
   269  replaceFormula 1 "-2 * 1 + (1 + x) = 0"; refFormula 1 (get_pos 1 1);
   270  val ((pt,_),_) = get_calc 1;
   271  val str = pr_ptree pr_short pt;
   272 
   273 (* before AK110725 this was
   274 ".    ----- pblobj -----\n
   275 1.   x + 1 = 2\n
   276 2.   x + 1 + -1 * 2 = 0\n
   277 2.1.   x + 1 + -1 * 2 = 0\n
   278 2.2.   1 + x + -1 * 2 = 0\n
   279 2.3.   1 + (x + -1 * 2) = 0\n
   280 2.4.   1 + (x + -2) = 0\n
   281 2.5.   1 + (x + -2 * 1) = 0\n
   282 2.6.   1 + x + -2 * 1 = 0\n";
   283 *)
   284 if str = 
   285 ".    ----- pblobj -----\n"^
   286 "1.   x + 1 = 2\n"^
   287 "2.   x + 1 + -1 * 2 = 0\n"^
   288 "2.1.   x + 1 + -1 * 2 = 0\n"^
   289 "2.2.   1 + x + -1 * 2 = 0\n"^
   290 "2.3.   1 + (x + -1 * 2) = 0\n"^
   291 "2.4.   1 + (x + -2) = 0\n"^
   292 "2.5.   1 + (x + -2 * 1) = 0\n"^
   293 "2.6.   1 + x + -2 * 1 = 0\n" then()
   294 else error "inform.sml: diff.behav.replaceFormula: on Res += 1";
   295 
   296  autoCalculate' 1 CompleteCalc;
   297  val ((pt,pos as (p,_)),_) = get_calc 1;
   298  if pos = ([],Res) andalso "[x = 1]" = (term2str o fst) (get_obj g_result pt p) then()
   299  else error "inform.sml: diff.behav.replaceFormula: on Res + = 2";
   300 
   301 "--------- replaceFormula: on Res + = 1st Nd ---------------------";
   302 "--------- replaceFormula: on Res + = 1st Nd ---------------------";
   303 "--------- replaceFormula: on Res + = 1st Nd ---------------------";
   304  reset_states ();
   305  CalcTree [(["equality (x+1=(2::real))", "solveFor x","solutions L"], 
   306 	    ("Test", ["sqroot-test","univariate","equation","test"],
   307 	     ["Test","squ-equ-test-subpbl1"]))];
   308  Iterator 1; moveActiveRoot 1;
   309  autoCalculate' 1 CompleteCalcHead;
   310  autoCalculate' 1 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 = 2*)
   311  autoCalculate' 1 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 + -1 * 2 = 0*);
   312 
   313  replaceFormula 1 "x + 1 = 4 + -2"; refFormula 1 (get_pos 1 1);
   314  val ((pt,_),_) = get_calc 1;
   315  val str = pr_ptree pr_short pt;
   316  writeln str;
   317  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 ()
   318  else error "inform.sml: diff.behav.replaceFormula: on Res 1 + = 1";
   319  autoCalculate' 1 CompleteCalc;
   320  val ((pt,pos as (p,_)),_) = get_calc 1;
   321  if pos = ([],Res) andalso "[x = 1]" = (term2str o fst)(get_obj g_result pt p) then()
   322  else error "inform.sml: diff.behav.replaceFormula: on Res + = 2";
   323 
   324 
   325 "--------- replaceFormula: on Frm + = 1st Nd ---------------------";
   326 "--------- replaceFormula: on Frm + = 1st Nd ---------------------";
   327 "--------- replaceFormula: on Frm + = 1st Nd ---------------------";
   328  reset_states ();
   329  CalcTree [(["equality (x+1=(2::real))", "solveFor x","solutions L"], 
   330 	    ("Test", ["sqroot-test","univariate","equation","test"],
   331 	     ["Test","squ-equ-test-subpbl1"]))];
   332  Iterator 1; moveActiveRoot 1;
   333  autoCalculate' 1 CompleteCalcHead;
   334  autoCalculate' 1 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 = 2*)
   335 
   336  replaceFormula 1 "x + 1 = 4 + -2"; refFormula 1 (get_pos 1 1);
   337  val ((pt,_),_) = get_calc 1;
   338  val str = pr_ptree pr_short pt;
   339  writeln str;
   340  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 ()
   341  else error "inform.sml: diff.behav.replaceFormula: on Frm 1 + = 1";
   342  autoCalculate' 1 CompleteCalc;
   343  val ((pt,pos as (p,_)),_) = get_calc 1;
   344  if pos = ([],Res) andalso "[x = 1]" = (term2str o fst)(get_obj g_result pt p) then()
   345  else error "inform.sml: diff.behav.replaceFormula: on Frm 1 + = 2";
   346 
   347 
   348 "--------- replaceFormula: cut calculation -----------------------";
   349 "--------- replaceFormula: cut calculation -----------------------";
   350 "--------- replaceFormula: cut calculation -----------------------";
   351  reset_states ();
   352  CalcTree [(["equality (x+1=(2::real))", "solveFor x","solutions L"], 
   353 	    ("Test", ["sqroot-test","univariate","equation","test"],
   354 	     ["Test","squ-equ-test-subpbl1"]))];
   355  Iterator 1; moveActiveRoot 1;
   356  autoCalculate' 1 CompleteCalc;
   357  moveActiveRoot 1; moveActiveDown 1;
   358  if get_pos 1 1 = ([1], Frm) then ()
   359  else error "inform.sml: diff.behav. cut calculation 1";
   360 
   361  replaceFormula 1 "x + 1 = 4 + -2"; refFormula 1 (get_pos 1 1);
   362  val ((pt,p),_) = get_calc 1;
   363  val str = pr_ptree pr_short pt;
   364  writeln str;
   365  if p = ([1], Res) then ()
   366  else error "inform.sml: diff.behav. cut calculation 2";
   367 
   368 
   369 (* 040307 copied from informtest.sml; ... old version 
   370  "---------------- maximum-example, UC: Modeling / modifyCalcHead -";
   371  "---------------- maximum-example, UC: Modeling / modifyCalcHead -";
   372  "---------------- maximum-example, UC: Modeling / modifyCalcHead -";
   373 
   374  val p = ([],Pbl);
   375  val elems = ["fixedValues [r=Arbfix]","maximum A","valuesFor [a,b]",
   376 	      "relations [A=a*b, (a/2)^^^2 + (b/2)^^^2 = r^^^2]",
   377 	      "relations [A=a*b, (a/2)^^^2 + (b/2)^^^2 = r^^^2]",
   378 	      "relations [A=a*b, a/2=r*sin alpha, b/2=r*cos alpha]",
   379 	      (*^^^ these are the elements for the root-problem (in variants)*)
   380               (*vvv these are elements required for subproblems*)
   381 	      "boundVariable a","boundVariable b","boundVariable alpha",
   382 	      "interval {x::real. 0 <= x & x <= 2*r}",
   383 	      "interval {x::real. 0 <= x & x <= 2*r}",
   384 	      "interval {x::real. 0 <= x & x <= pi}",
   385 	      "errorBound (eps=(0::real))"]
   386  (*specifying is not interesting for this example*)
   387  val spec = ("DiffApp", ["maximum_of","function"], 
   388 	     ["DiffApp","max_by_calculus"]);
   389  (*the empty model with descriptions for user-guidance by Model_Problem*)
   390  val empty_model = [Given ["fixedValues []"],
   391 		    Find ["maximum", "valuesFor"],
   392 		    Relate ["relations []"]];
   393 
   394 
   395  (*!!!!!!!!!!!!!!!!! DON'T USE me FOR FINDING nxt !!!!!!!!!!!!!!!!!!*)
   396  val (p,_,f,nxt,_,pt) = CalcTreeTEST [(elems, spec)];
   397  (*val nxt = ("Model_Problem", ...*)
   398  val pbl = get_obj g_pbl pt (fst p); (writeln o (itms2str_ ctxt)) pbl; 
   399 
   400  val (p,_,f,nxt,_,pt) = me nxt p c pt;
   401  (*nxt = Add_Given "fixedValues [r = Arbfix]"*)
   402  val pbl = get_obj g_pbl pt (fst p); (writeln o (itms2str_ ctxt)) pbl; 
   403 (*[
   404 (0 ,[] ,false ,#Given ,Inc fixedValues [] ,(??.empty, [])),
   405 (0 ,[] ,false ,#Find ,Inc maximum ,(??.empty, [])),
   406 (0 ,[] ,false ,#Find ,Inc valuesFor ,(??.empty, [])),
   407 (0 ,[] ,false ,#Relate ,Inc relations [] ,(??.empty, []))]*)
   408 
   409  (*the empty CalcHead is checked w.r.t the model and re-established as such*)
   410  val (b,pt,ocalhd) = input_icalhd pt (p,"", empty_model, Pbl, e_spec);
   411  val pbl = get_obj g_pbl pt (fst p); (writeln o (itms2str_ ctxt)) pbl; 
   412  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";
   413 
   414  (*there is one input to the model (could be more)*)
   415  val (b,pt,ocalhd) = 
   416      input_icalhd pt (p,"", [Given ["fixedValues [r=Arbfix]"],
   417 			     Find ["maximum", "valuesFor"],
   418 			     Relate ["relations"]], Pbl, e_spec);
   419  val pbl = get_obj g_pbl pt (fst p); (writeln o (itms2str_ ctxt)) pbl; 
   420  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 () 
   421  else error "informtest.sml: diff.behav. max 2";
   422 
   423  (*this input is complete in variant 3, but the ME doesn't recognize FIXXXXME
   424  val (b,pt''''',ocalhd) = 
   425      input_icalhd pt (p,"", [Given ["fixedValues [r=Arbfix]"],
   426 			     Find ["maximum A", "valuesFor [a,b]"],
   427 			     Relate ["relations [A=a*b, a/2=r*sin alpha, \
   428 				     \b/2=r*cos alpha]"]], Pbl, e_spec);
   429  val pbl = get_obj g_pbl pt''''' (fst p); (writeln o (itms2str_ ctxt)) pbl; 
   430  if ocalhd2str ocalhd = ------------^^^^^^^^^^ missing !!!*)
   431 
   432  (*this input is complete in variant 1 (variant 3 does not work yet)*)
   433  val (b,pt''''',ocalhd) = 
   434      input_icalhd pt (p,"", [Given ["fixedValues [r=Arbfix]"],
   435 			     Find ["maximum A", "valuesFor [a,b]"],
   436 			     Relate ["relations [A=a*b, \
   437 				     \(a/2)^^^2 + (b/2)^^^2 = r^^^2]"]], 
   438 		      Pbl, e_spec);
   439  val pbl = get_obj g_pbl pt''''' (fst p); (writeln o (itms2str_ ctxt)) pbl; 
   440 
   441  modifycalcheadOK2xml 111 (bool2str b) ocalhd;
   442 *)
   443 
   444 "--------- syntax error ------------------------------------------";
   445 "--------- syntax error ------------------------------------------";
   446 "--------- syntax error ------------------------------------------";
   447  reset_states ();
   448  CalcTree [(["equality (x+1=(2::real))", "solveFor x","solutions L"], 
   449 	    ("Test", ["sqroot-test","univariate","equation","test"],
   450 	     ["Test","squ-equ-test-subpbl1"]))];
   451  Iterator 1; moveActiveRoot 1;
   452  autoCalculate' 1 CompleteCalcHead;
   453  autoCalculate' 1 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 = 2*)
   454  autoCalculate' 1 (Step 1); refFormula 1 (get_pos 1 1);(*x + 1 + -1 * 2 = 0*);
   455 
   456  appendFormula 1 " x - "; (*<ERROR> syntax error in ' x - ' </ERROR>*)
   457  val ((pt,_),_) = get_calc 1;
   458  val str = pr_ptree pr_short pt;
   459  writeln str;
   460  if str = ".    ----- pblobj -----\n1.   x + 1 = 2\n" then ()
   461  else error "inform.sml: diff.behav.appendFormula: syntax error";
   462 
   463 
   464 "--------- CAS-command on ([],Pbl) -------------------------------";
   465 "--------- CAS-command on ([],Pbl) -------------------------------";
   466 "--------- CAS-command on ([],Pbl) -------------------------------";
   467 val (p,_,f,nxt,_,pt) = 
   468     CalcTreeTEST [([], ("e_domID", ["e_pblID"], ["e_metID"]))];
   469 val ifo = "solve(x+1=2,x)";
   470 val (_,(_,c,(pt,p))) = inform ([],[],(pt,p)) "solve(x+1=2,x)";
   471 show_pt pt;
   472 val nxt = ("Apply_Method",Apply_Method ["Test","squ-equ-test-subpbl1"]);
   473 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   474 if p = ([1], Frm) andalso f2str f = "x + 1 = 2" then ()
   475 else error "inform.sml: diff.behav. CAScmd ([],Pbl)";
   476 
   477 
   478 "--------- CAS-command on ([],Pbl) FE-interface ------------------";
   479 "--------- CAS-command on ([],Pbl) FE-interface ------------------";
   480 "--------- CAS-command on ([],Pbl) FE-interface ------------------";
   481 reset_states ();
   482 CalcTree [([], ("e_domID", ["e_pblID"], ["e_metID"]))];
   483 Iterator 1;
   484 moveActiveRoot 1;
   485 replaceFormula 1 "solve(x+1=2,x)";
   486 autoCalculate' 1 CompleteCalc;
   487 val ((pt,p),_) = get_calc 1;
   488 show_pt pt;
   489 if p = ([], Res) then ()
   490 else error "inform.sml: diff.behav. CAScmd ([],Pbl) FE-interface";
   491 
   492 
   493 "--------- inform [rational,simplification] ----------------------";
   494 "--------- inform [rational,simplification] ----------------------";
   495 "--------- inform [rational,simplification] ----------------------";
   496 reset_states ();
   497 CalcTree [(["Term (a * x / (b * x) + c * x / (d * x) + e / f)", "normalform N"],
   498 	("Rational", ["rational", "simplification"], ["simplification", "of_rationals"]))];
   499 Iterator 1; moveActiveRoot 1;
   500 autoCalculate' 1 CompleteCalcHead;
   501 
   502 "--- (-1) give a preview on the calculation without any input";
   503 (*
   504 autoCalculate' 1 CompleteCalc;
   505 val ((pt, p), _) = get_calc 1;
   506 show_pt pt;
   507 [
   508 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   509 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   510 (([1], Res), a / b + c / d + e / f),                             <--- (1) input arbitrary
   511 (([2], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   512 (([3], Res), (a * (d * f) + b * (c * f) + b * (d * e)) / (b * (d * f))),
   513 (([4], Res), (a * d * f + b * c * f + b * d * e) / (b * d * f)), <--- (2) input next
   514 (([], Res), (a * d * f + b * c * f + b * d * e) / (b * d * f))]  <--- (3) is also final result
   515                                                                           EXAMPLE NOT OPTIMAL
   516 *)
   517 "--- (0) user input as the *first* step does not work, thus impdo at least 1 step";
   518 autoCalculate' 1 (Step 1);
   519 autoCalculate' 1 (Step 1);
   520 val ((pt, p), _) = get_calc 1;
   521 (*show_pt pt;
   522 [
   523 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   524 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   525 (([1], Res), a / b + c / d + e / f)] 
   526 *)
   527 "--- (1) input an arbitrary next formula";
   528 appendFormula 1 "((a * d) + (c * b)) / (b * d) + e / f" (*|> Future.join*);
   529 val ((pt, p), _) = get_calc 1;
   530 (*show_pt pt;
   531 [
   532 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   533 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   534 (([1], Res), a / b + c / d + e / f),
   535 (([2,1], Frm), a / b + c / d + e / f),
   536 (([2,1], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   537 (([2,2], Res), (a * d + c * b) / (b * d) + e / f),
   538 (([2], Res), (a * d + c * b) / (b * d) + e / f)] 
   539 *)
   540 val ((pt,p),_) = get_calc 1;
   541 if p = ([2], Res) andalso (length o children o (get_nd pt)) (fst p) = 2 then ()
   542 else error ("inform.sml: [rational,simplification] 1");
   543 
   544 "--- (2) input the next formula that would be presented by mat-engine";
   545 (* generate a preview:
   546 autoCalculate' 1 (Step 1);
   547 val ((pt, p), _) = get_calc 1;
   548 show_pt pt;
   549 [
   550 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   551 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   552 (([1], Res), a / b + c / d + e / f),
   553 (([2,1], Frm), a / b + c / d + e / f),
   554 (([2,1], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   555 (([2,2], Res), (a * d + c * b) / (b * d) + e / f),
   556 (([2], Res), (a * d + c * b) / (b * d) + e / f),
   557 (([3], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f))]   <--- input this
   558 *)
   559 appendFormula 1 "(b * d * e + b * c * f + a * d * f) / (b * d * f)" (*|> Future.join*);
   560 val ((pt, p), _) = get_calc 1;
   561 (*show_pt pt;
   562 [
   563 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   564 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   565 (([1], Res), a / b + c / d + e / f),
   566 (([2,1], Frm), a / b + c / d + e / f),
   567 (([2,1], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   568 (([2,2], Res), (a * d + c * b) / (b * d) + e / f),
   569 (([2], Res), (a * d + c * b) / (b * d) + e / f),
   570 (([3], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f))] 
   571 *)
   572 if p = ([3], Res) andalso (length o children o (get_nd pt)) (fst p) = 0 then ()
   573 else error ("inform.sml: [rational,simplification] 2");
   574 
   575 "--- (3) input the exact final result";
   576 appendFormula 1 "(b * d * e + b * c * f + a * d * f) / (b * d * f)" (*|> Future.join*);
   577 val ((pt, p), _) = get_calc 1;
   578 (*show_pt pt;
   579 [
   580 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   581 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   582 (([1], Res), a / b + c / d + e / f),
   583 (([2,1], Frm), a / b + c / d + e / f),
   584 (([2,1], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   585 (([2,2], Res), (a * d + c * b) / (b * d) + e / f),
   586 (([2], Res), (a * d + c * b) / (b * d) + e / f),
   587 (([3], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   588 (([4,1], Frm), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   589 (([4,1], Res), (a * (d * f) + b * (c * f) + b * (d * e)) / (b * (d * f))),
   590 (([4,2], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   591 (([4], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f))] 
   592 *)
   593 if p = ([4], Res) andalso (length o children o (get_nd pt)) (fst p) = 2 then ()
   594 else error ("inform.sml: [rational,simplification] 3");
   595 
   596 "--- (4) finish the calculation + check the postcondition (in the future)";
   597 autoCalculate' 1 CompleteCalc;
   598 val ((pt, p), _) = get_calc 1;
   599 val (t, asm) = get_obj g_result pt [];
   600 if term2str t = "(a * d * f + b * c * f + b * d * e) / (b * d * f)" andalso
   601 terms2str asm = "[\"b * d * f ~= 0\",\"d ~= 0\",\"b ~= 0\"," ^
   602   "\"a * x / (b * x) + c * x / (d * x) + e / f is_ratpolyexp\"]"
   603 then () else error "inform [rational,simplification] changed at end";
   604 (*show_pt pt;
   605 [
   606 (([], Frm), Simplify (a * x / (b * x) + c * x / (d * x) + e / f)),
   607 (([1], Frm), a * x / (b * x) + c * x / (d * x) + e / f),
   608 (([1], Res), a / b + c / d + e / f),
   609 (([2,1], Frm), a / b + c / d + e / f),
   610 (([2,1], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   611 (([2,2], Res), (a * d + c * b) / (b * d) + e / f),
   612 (([2], Res), (a * d + c * b) / (b * d) + e / f),
   613 (([3], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   614 (([4,1], Frm), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   615 (([4,1], Res), (a * (d * f) + b * (c * f) + b * (d * e)) / (b * (d * f))),
   616 (([4,2], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   617 (([4], Res), (b * d * e + b * c * f + a * d * f) / (b * d * f)),
   618 (([5], Res), (a * (d * f) + b * (c * f) + b * (d * e)) / (b * (d * f))),
   619 (([6], Res), (a * d * f + b * c * f + b * d * e) / (b * d * f)),
   620 (([], Res), (a * d * f + b * c * f + b * d * e) / (b * d * f))] 
   621 *)
   622 
   623 "--------- Take as 1st tac, start with <NEW> (CAS input) ---------";
   624 "--------- Take as 1st tac, start with <NEW> (CAS input) ---------";
   625 "--------- Take as 1st tac, start with <NEW> (CAS input) ---------";
   626 val t = str2term "Diff (x^^^2 + x + 1, x)";
   627 case t of Const ("Diff.Diff", _) $ _ => ()
   628 	| _ => raise 
   629 	      error "diff.sml behav.changed for CAS Diff (..., x)";
   630 atomty t;
   631 "-----------------------------------------------------------------";
   632 (*1>*)reset_states ();
   633 (*2>*)CalcTree [([], ("e_domID", ["e_pblID"], ["e_metID"]))];
   634 (*3>*)Iterator 1;moveActiveRoot 1;
   635 "----- here the Headline has been finished";
   636 (*4>*)moveActiveFormula 1 ([],Pbl);
   637 (*5>*)replaceFormula 1 "Diff (x^2 + x + 1, x)";
   638 val ((pt,_),_) = get_calc 1;
   639 val PblObj {probl, meth, spec, fmz, env, loc, ...} = get_obj I pt [];
   640 val NONE = env;
   641 val (SOME istate, NONE) = loc;
   642 default_print_depth 5;
   643 writeln"-----------------------------------------------------------";
   644 spec;
   645 writeln (itms2str_ ctxt probl);
   646 writeln (itms2str_ ctxt meth);
   647 writeln (istate2str (fst istate));
   648 
   649 refFormula 1 ([],Pbl) (*--> correct CalcHead*);
   650  (*081016 NOT necessary (but leave it in Java):*)
   651 (*6>*)(*completeCalcHead*)autoCalculate' 1 CompleteCalcHead;
   652 "----- here the CalcHead has been completed --- ONCE MORE ?????";
   653 
   654 (***difference II***)
   655 val ((pt,p),_) = get_calc 1;
   656 (*val p = ([], Pbl)*)
   657 val PblObj {probl, meth, spec, fmz, env, loc, ...} = get_obj I pt [];
   658 val NONE = env;
   659 val (SOME istate, NONE) = loc;
   660 default_print_depth 5; writeln (istate2str (fst istate));  default_print_depth 3;
   661 (*ScrState ([],
   662  [], NONE,
   663  ??.empty, Sundef, false)*)
   664 default_print_depth 5; spec; default_print_depth 3;
   665 (*("Isac",
   666       ["derivative_of", "function"],
   667       ["diff", "differentiate_on_R"]) : spec*)
   668 writeln (itms2str_ ctxt probl);
   669 (*[
   670 (1 ,[1] ,true ,#Given ,Cor functionTerm (x ^^^ 2 + x + 1) ,(f_, [x ^^^ 2 + x + 1])),
   671 (2 ,[1] ,true ,#Given ,Cor differentiateFor x ,(v_, [x])),
   672 (3 ,[1] ,true ,#Find ,Cor derivative f_'_f ,(f_'_f, [f_'_f]))]*)
   673 writeln (itms2str_ ctxt meth);
   674 (*[
   675 (1 ,[1] ,true ,#Given ,Cor functionTerm (x ^^^ 2 + x + 1) ,(f_, [x ^^^ 2 + x + 1])),
   676 (2 ,[1] ,true ,#Given ,Cor differentiateFor x ,(v_, [x])),
   677 (3 ,[1] ,true ,#Find ,Cor derivative f_'_f ,(f_'_f, [f_'_f]))]*)
   678 writeln"-----------------------------------------------------------";
   679 (*7>*)fetchProposedTactic 1 (*--> Apply_Method*);
   680 (*WN081028 fixed <ERROR> helpless </ERROR> by inform returning ...(.,Met)*)
   681 autoCalculate' 1 CompleteCalc;
   682 val ((pt,p),_) = get_calc 1;
   683 val Form res = (#1 o pt_extract) (pt, ([],Res));
   684 show_pt pt;
   685 if p = ([], Res) andalso term2str res = "1 + 2 * x" then ()
   686 else error "diff.sml behav.changed for Diff (x^2 + x + 1, x)";
   687 
   688 "--------- Take as 1st tac, start from exp -----------------------";
   689 "--------- Take as 1st tac, start from exp -----------------------";
   690 "--------- Take as 1st tac, start from exp -----------------------";
   691 (*the following input is copied from BridgeLog Java <==> SML,
   692   omitting unnecessary inputs*)
   693 (*1>*)reset_states ();
   694 (*2>*)CalcTree [(["functionTerm (x^2 + x + 1)", "differentiateFor x", "derivative f_'_f"],("Isac",["derivative_of","function"],["diff","differentiate_on_R"]))];
   695 (*3>*)Iterator 1; moveActiveRoot 1;
   696 
   697 (*6>*)(*completeCalcHead*)autoCalculate' 1 CompleteCalcHead;
   698 (***difference II***)
   699 val ((pt,_),_) = get_calc 1;
   700 val PblObj {probl, meth, spec, fmz, env, loc, ...} = get_obj I pt [];
   701 val NONE = env;
   702 val (SOME istate, NONE) = loc;
   703 default_print_depth 5; writeln (istate2str (fst istate));  default_print_depth 3;
   704 (*ScrState ([],
   705  [], NONE,
   706  ??.empty, Sundef, false)*)
   707 default_print_depth 5; spec; default_print_depth 3;
   708 (*("Isac",
   709       ["derivative_of", "function"],
   710       ["diff", "differentiate_on_R"]) : spec*)
   711 writeln (itms2str_ ctxt probl);
   712 (*[
   713 (1 ,[1] ,true ,#Given ,Cor functionTerm (x ^^^ 2 + x + 1) ,(f_, [x ^^^ 2 + x + 1])),
   714 (2 ,[1] ,true ,#Given ,Cor differentiateFor x ,(v_, [x])),
   715 (3 ,[1] ,true ,#Find ,Cor derivative f_'_f ,(f_'_f, [f_'_f]))]*)
   716 writeln (itms2str_ ctxt meth);
   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"-----------------------------------------------------------";
   722 (*7>*)fetchProposedTactic 1 (*--> Apply_Method*);
   723 autoCalculate' 1 (Step 1);
   724 val ((pt,p),_) = get_calc 1;
   725 val Form res = (#1 o pt_extract) (pt, p);
   726 if term2str res = "d_d x (x ^^^ 2 + x + 1)" then ()
   727 else error "diff.sml Diff (x^2 + x + 1, x) from exp";
   728 
   729 "--------- init_form, start with <NEW> (CAS input) ---------------";
   730 "--------- init_form, start with <NEW> (CAS input) ---------------";
   731 "--------- init_form, start with <NEW> (CAS input) ---------------";
   732 reset_states ();
   733 CalcTree [([], ("e_domID", ["e_pblID"], ["e_metID"]))];
   734 (*[[from sml: > @@@@@begin@@@@@
   735 [[from sml:  1 
   736 [[from sml: <CALCTREE>
   737 [[from sml:    <CALCID> 1 </CALCID>
   738 [[from sml: </CALCTREE>
   739 [[from sml: @@@@@end@@@@@*)
   740 Iterator 1;
   741 (*[[from sml: > @@@@@begin@@@@@
   742 [[from sml:  1 
   743 [[from sml: <ADDUSER>
   744 [[from sml:   <CALCID> 1 </CALCID>
   745 [[from sml:   <USERID> 1 </USERID>
   746 [[from sml: </ADDUSER>
   747 [[from sml: @@@@@end@@@@@*)
   748 moveActiveRoot 1;
   749 (*[[from sml: > @@@@@begin@@@@@
   750 [[from sml:  1 
   751 [[from sml: <CALCITERATOR>
   752 [[from sml:   <CALCID> 1 </CALCID>
   753 [[from sml:   <POSITION>
   754 [[from sml:     <INTLIST>
   755 [[from sml:     </INTLIST>
   756 [[from sml:     <POS> Pbl </POS>
   757 [[from sml:   </POSITION>
   758 [[from sml: </CALCITERATOR>
   759 [[from sml: @@@@@end@@@@@*)
   760 getFormulaeFromTo 1 ([],Pbl) ([],Pbl) 0 false;
   761 (*[[from sml: > @@@@@begin@@@@@                STILL CORRECT
   762 [[from sml:  1 
   763 [[from sml: <GETELEMENTSFROMTO>
   764 [[from sml:   <CALCID> 1 </CALCID>
   765 [[from sml:   <FORMHEADS>
   766 [[from sml:     <CALCFORMULA>
   767 [[from sml:       <POSITION>
   768 [[from sml:         <INTLIST>
   769 [[from sml:         </INTLIST>
   770 [[from sml:         <POS> Pbl </POS>
   771 [[from sml:       </POSITION>
   772 [[from sml:       <FORMULA>
   773 [[from sml:         <MATHML>
   774 [[from sml:           <ISA> ________________________________________________ </ISA>
   775 [[from sml:         </MATHML>
   776 [[from sml: 
   777 [[from sml:       </FORMULA>
   778 [[from sml:     </CALCFORMULA>
   779 [[from sml:   </FORMHEADS>
   780 [[from sml: </GETELEMENTSFROMTO>
   781 [[from sml: @@@@@end@@@@@*)
   782 refFormula 1 ([],Pbl);
   783 (*[[from sml: > @@@@@begin@@@@@                STILL CORRECT
   784 [[from sml:  1 
   785 [[from sml: <REFFORMULA>
   786 [[from sml:   <CALCID> 1 </CALCID>
   787 [[from sml:   <CALCHEAD status = "incorrect">
   788 [[from sml:     <POSITION>
   789 [[from sml:       <INTLIST>
   790 [[from sml:       </INTLIST>
   791 [[from sml:       <POS> Pbl </POS>
   792 [[from sml:     </POSITION>
   793 [[from sml:     <HEAD>
   794 [[from sml:       <MATHML>
   795 [[from sml:         <ISA> Problem (e_domID, [e_pblID]) </ISA>
   796 [[from sml:       </MATHML>
   797 [[from sml:     </HEAD>
   798 [[from sml:     <MODEL>
   799 [[from sml:       <GIVEN>  </GIVEN>
   800 [[from sml:       <WHERE>  </WHERE>
   801 [[from sml:       <FIND>  </FIND>
   802 [[from sml:       <RELATE>  </RELATE>
   803 [[from sml:     </MODEL>
   804 [[from sml:     <BELONGSTO> Pbl </BELONGSTO>
   805 [[from sml:     <SPECIFICATION>
   806 [[from sml:       <THEORYID> e_domID </THEORYID>
   807 [[from sml:       <PROBLEMID>
   808 [[from sml:         <STRINGLIST>
   809 [[from sml:           <STRING> e_pblID </STRING>
   810 [[from sml:         </STRINGLIST>
   811 [[from sml:       </PROBLEMID>
   812 [[from sml:       <METHODID>
   813 [[from sml:         <STRINGLIST>
   814 [[from sml:           <STRING> e_metID </STRING>
   815 [[from sml:         </STRINGLIST>
   816 [[from sml:       </METHODID>
   817 [[from sml:     </SPECIFICATION>
   818 [[from sml:   </CALCHEAD>
   819 [[from sml: </REFFORMULA>
   820 [[from sml: @@@@@end@@@@@*)
   821 moveActiveFormula 1 ([],Pbl);
   822 (*[[from sml: > @@@@@begin@@@@@
   823 [[from sml:  1 
   824 [[from sml: <CALCITERATOR>
   825 [[from sml:   <CALCID> 1 </CALCID>
   826 [[from sml:   <POSITION>
   827 [[from sml:     <INTLIST>
   828 [[from sml:     </INTLIST>
   829 [[from sml:     <POS> Pbl </POS>
   830 [[from sml:   </POSITION>
   831 [[from sml: </CALCITERATOR>
   832 [[from sml: @@@@@end@@@@@*)
   833 replaceFormula 1 "Simplify (1+2)";
   834 (*[[from sml: > @@@@@begin@@@@@
   835 [[from sml:  1 
   836 [[from sml: <REPLACEFORMULA>
   837 [[from sml:   <CALCID> 1 </CALCID>
   838 [[from sml:   <CALCCHANGED>
   839 [[from sml:     <UNCHANGED>
   840 [[from sml:       <INTLIST>
   841 [[from sml:       </INTLIST>
   842 [[from sml:       <POS> Pbl </POS>
   843 [[from sml:     </UNCHANGED>
   844 [[from sml:     <DELETED>
   845 [[from sml:       <INTLIST>
   846 [[from sml:       </INTLIST>
   847 [[from sml:       <POS> Pbl </POS>
   848 [[from sml:     </DELETED>
   849 [[from sml:     <GENERATED>
   850 [[from sml:       <INTLIST>
   851 [[from sml:       </INTLIST>
   852 [[from sml:       <POS> Met </POS>                           DIFFERENCE: Pbl
   853 [[from sml:     </GENERATED>
   854 [[from sml:   </CALCCHANGED>
   855 [[from sml: </REPLACEFORMULA>
   856 [[from sml: @@@@@end@@@@@*)
   857 getFormulaeFromTo 1 ([],Pbl) ([],Pbl) 0 false(*              DIFFERENCE: Pbl*);
   858 (*@@@@@begin@@@@@
   859  1
   860 <GETELEMENTSFROMTO>
   861   <CALCID> 1 </CALCID>
   862   <FORMHEADS>
   863     <CALCFORMULA>
   864       <POSITION>
   865         <INTLIST>
   866         </INTLIST>
   867         <POS> Pbl </POS>
   868       </POSITION>
   869       <FORMULA>
   870         <MATHML>
   871           <ISA> Simplify (1 + 2) </ISA>                      WORKS !!!!!
   872         </MATHML>
   873       </FORMULA>
   874     </CALCFORMULA>
   875   </FORMHEADS>
   876 </GETELEMENTSFROMTO>
   877 @@@@@end@@@@@*)
   878 getFormulaeFromTo 1 ([],Pbl) ([],Met) 0 false;
   879 (*[[from sml: > @@@@@begin@@@@@
   880 [[from sml:  1 
   881 [[from sml: <SYSERROR>
   882 [[from sml:   <CALCID> 1 </CALCID>
   883 [[from sml:   <ERROR> error in getFormulaeFromTo </ERROR>
   884 [[from sml: </SYSERROR>
   885 [[from sml: @@@@@end@@@@@*)
   886 (*step into getFormulaeFromTo --- bug corrected...*)
   887 
   888 "--------- build fun check_err_patt ------------------------------";
   889 "--------- build fun check_err_patt ------------------------------";
   890 "--------- build fun check_err_patt ------------------------------";
   891 val subst = [(str2term "bdv", str2term "x")]: subst;
   892 val rls = norm_Rational
   893 val pat = parse_patt @{theory} "(?a + ?b)/(?b + ?c) = ?a / ?c";
   894 val (res, inf) = (str2term "(2 + 3)/(3 + 4)", str2term "2 / 4");
   895 val (res, inf) = (str2term "(2 + 3)/(3 + 4)", str2term "1 / 2");
   896 
   897 val (res', _, _, rewritten) = (*rewritten: the lhs of the pattern matches in res*)
   898   rew_sub thy 1 [] e_rew_ord e_rls false [] (Trueprop $ pat) res;
   899 if rewritten then NONE else SOME "e_errpatID";
   900 
   901 val norm_res = case rewrite_set_ (Isac()) false rls res' of
   902   NONE => res'
   903 | SOME (norm_res, _) => norm_res
   904 
   905 val norm_inf = case rewrite_set_ (Isac()) false rls inf of
   906   NONE => inf
   907 | SOME (norm_inf, _) => norm_inf;
   908 
   909 res' = inf;
   910 norm_res = norm_inf;
   911 
   912 val pat = parse_patt @{theory} "(?a + ?b)/?a = ?b";
   913 val (res, inf) = (str2term "(2 + 3)/2", str2term "3");
   914 if check_err_patt (res, inf) subst ("errpatID", pat) rls = SOME "errpatID"
   915 then () else error "error patt example1 changed";
   916 
   917 val pat = parse_patt @{theory} "(?a + ?b)/(?a + ?c) = ?b / ?c";
   918 val (res, inf) = (str2term "(2 + 3)/(2 + 4)", str2term "3 / 4");
   919 if check_err_patt (res, inf) subst ("errpatID", pat) rls = SOME "errpatID"
   920 then () else error "error patt example2 changed";
   921 
   922 val pat = parse_patt @{theory} "(?a + ?b)/(?b + ?c) = ?a / ?c";
   923 val (res, inf) = (str2term "(2 + 3)/(3 + 4)", str2term "2 / 4");
   924 if check_err_patt (res, inf) subst ("errpatID", pat) rls = SOME "errpatID"
   925 then () else error "error patt example3 changed";
   926 
   927 val inf =  str2term "1 / 2";
   928 if check_err_patt (res, inf) subst ("errpatID", pat) rls = SOME "errpatID"
   929 then () else error "error patt example3 changed";
   930 
   931 "--------- build fun check_err_patt ?bdv -------------------------";
   932 "--------- build fun check_err_patt ?bdv -------------------------";
   933 "--------- build fun check_err_patt ?bdv -------------------------";
   934 val subst = [(str2term "bdv", str2term "x")]: subst;
   935 val t = str2term "d_d x (x ^^^ 2 + sin (x ^^^ 4))";
   936 val SOME (t, _) = rewrite_set_inst_ thy false subst norm_diff t;
   937 if term2str t = "2 * x + cos (x ^^^ 4) * 4 * x ^^^ 3" then ()
   938 else error "build fun check_err_patt ?bdv changed 1"; 
   939 
   940 val rls = norm_diff
   941 val pat = parse_patt @{theory} "d_d ?bdv (sin ?u) = cos (d_d ?bdv ?u)"; 
   942 val (res, inf) = (str2term "2 * x + d_d x (sin (x ^^^ 4))", str2term "2 * x + cos (4 * x ^^^ 3)");
   943 
   944 val (res', _, _, rewritten) = (*rewritten: the lhs of the pattern matches in res*)
   945   rew_sub thy 1 subst e_rew_ord e_rls false [] (Trueprop $ pat) res;
   946 if term2str res' = "2 * x + cos (d_d x (x ^^^ 4))" andalso rewritten then ()
   947 else error "build fun check_err_patt ?bdv changed 2";
   948 
   949 val norm_res = case rewrite_set_inst_ (Isac()) false subst rls  res' of
   950   NONE => res'
   951 | SOME (norm_res, _) => norm_res;
   952 if term2str norm_res = "2 * x + cos (4 * x ^^^ 3)" then ()
   953 else error "build fun check_err_patt ?bdv changed 3";
   954 
   955 val norm_inf = case rewrite_set_inst_ (Isac()) false subst rls inf of
   956   NONE => inf
   957 | SOME (norm_inf, _) => norm_inf;
   958 if term2str norm_inf = "2 * x + cos (4 * x ^^^ 3)" then ()
   959 else error "build fun check_err_patt ?bdv changed 4";
   960 
   961 res' = inf;
   962 if norm_res = norm_inf then ()
   963 else error "build fun check_err_patt ?bdv changed 5";
   964 
   965 if check_err_patt (res, inf) (subst: subst) ("errpatID": errpatID, pat) rls = SOME "errpatID"
   966 then () else error "error patt example1 changed";
   967 
   968 "--------- build fun check_error_patterns ------------------------";
   969 "--------- build fun check_error_patterns ------------------------";
   970 "--------- build fun check_error_patterns ------------------------";
   971 val (res, inf) =
   972   (str2term "d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))",
   973    str2term "d_d x (x ^^^ 2) + cos (4 * x ^^^ 3)");
   974 val {errpats, nrls = rls, scr = Prog prog, ...} = get_met ["diff", "differentiate_on_R"]
   975 
   976 val env = [(str2term "v_v", str2term "x")];
   977 val errpats =
   978   [e_errpat, (*generalised for testing*)
   979    ("chain-rule-diff-both",
   980      [parse_patt @{theory} "d_d ?bdv (sin ?u) = cos (d_d ?bdv ?u)",
   981       parse_patt @{theory} "d_d ?bdv (cos ?u) = - sin (d_d ?bdv ?u)",
   982       parse_patt @{theory} "d_d ?bdv (?u ^^^ ?n) = ?n * ?u ^^^ (?n - 1)",
   983       parse_patt @{theory} "d_d ?bdv (LogExp.ln ?u) = 1 / ?u",
   984       parse_patt @{theory} "d_d ?bdv (LogExp.ln ?u) = 1 / d_d ?bdv ?u"],
   985      [@{thm diff_sin_chain}, @{thm diff_cos_chain}, @{thm diff_pow_chain}, 
   986       @{thm diff_ln_chain}, @{thm  diff_exp_chain}])]: errpat list;
   987 case check_error_patterns (res, inf) (prog, env) (errpats, rls) of SOME _ => () 
   988 | NONE => error "check_error_patterns broken";
   989 
   990 "--------- embed fun check_error_patterns ------------------------";
   991 "--------- embed fun check_error_patterns ------------------------";
   992 "--------- embed fun check_error_patterns ------------------------";
   993 reset_states ();
   994 CalcTree
   995 [(["functionTerm (x ^ 2 + sin (x ^ 4))", "differentiateFor x", "derivative f_f'"], 
   996   ("Isac", ["derivative_of","function"], ["diff","differentiate_on_R"]))];
   997 Iterator 1;
   998 moveActiveRoot 1;
   999 autoCalculate' 1 CompleteCalcHead;
  1000 autoCalculate' 1 (Step 1);
  1001 autoCalculate' 1 (Step 1);(*([1], Res), d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))*)
  1002 (*autoCalculate' 1 (Step 1);([2], Res), d_d x (x ^^^ 2) + cos (x ^^^ 4) * d_d x (x ^^^ 4)*)
  1003 
  1004 "~~~~~ fun appendFormula, args:"; val (cI, (ifo:cterm')) = (1, "d_d x (x ^ 2) + cos (4 * x ^ 3)");
  1005 val cs = get_calc cI
  1006 val pos as (_,p_) = get_pos cI 1; (*pos = ([1], Res)*)
  1007 val cs' = 
  1008     case step pos cs of
  1009 	    ("ok", cs') => cs';
  1010 
  1011 val (_, _, (pt, ([2], Res))) = cs';
  1012 (*show_pt pt;
  1013   [(([], Frm), Diff (x ^^^ 2 + sin (x ^^^ 4), x)),
  1014    (([1], Frm), d_d x (x ^^^ 2 + sin (x ^^^ 4))),
  1015    (([1], Res), d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))), <<=== input follos: "...+ cos(4.x^3)"
  1016    (([2], Res), d_d x (x ^^^ 2) + cos (x ^^^ 4) * d_d x (x ^^^ 4))] *)
  1017 
  1018 "~~~~~ fun inform, args:"; val ((cs as (_, _, ptp as (pt, pos as (p, p_))): calcstate'), istr) =
  1019   (cs', (encode ifo));
  1020 val 	SOME f_in = parse (assoc_thy "Isac") istr
  1021 val f_in = Thm.term_of f_in
  1022 val pos_pred = lev_back' pos
  1023 			(* f_pred ---"step pos cs"---> f_succ in appendFormula
  1024    TODO.WN120517: one starting point for redesign of pos' *)
  1025 val (f_pred, f_succ) = (get_curr_formula (pt, pos_pred), get_curr_formula (pt, pos));
  1026 	
  1027 term2str f_pred = "d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))";
  1028 term2str f_succ = "d_d x (x ^^^ 2) + cos (x ^^^ 4) * d_d x (x ^^^ 4)";
  1029 
  1030 			f_succ = f_in; (* = false*)
  1031 cas_input f_in; (* = NONE*)
  1032 val msg_calcstate' = compare_step ([], [], (pt, pos_pred)) f_in
  1033 val ("no derivation found", calcstate') = msg_calcstate';
  1034 			                 val pp = par_pblobj pt p
  1035 			                 val {errpats, nrls, scr = Prog prog, ...} = get_met (get_obj g_metID pt pp)
  1036 			                 val ScrState (env, _, _, _, _, _) = get_istate pt pos;
  1037                  case check_error_patterns (f_pred, f_in) (prog, env) (errpats, nrls) of
  1038 			                   SOME errpatID => ("error pattern #" ^ errpatID ^ "#", calcstate')
  1039 			                 | NONE => msg_calcstate';
  1040 
  1041 "~~~~~ from inform return val:"; val () = ();
  1042 case check_error_patterns (f_pred, f_in) (prog, env) (errpats, nrls) of
  1043 			  SOME errpatID => ()
  1044 			| NONE => error "check_error_patterns broken";
  1045 
  1046 "--- final check:";
  1047 case inform cs' (encode ifo) of
  1048   ("error pattern #chain-rule-diff-both#", calcstate') => ()
  1049 | _ => error "inform with (positive) check_error_patterns broken"
  1050 
  1051 "--------- build fun get_fillpats --------------------------------";
  1052 "--------- build fun get_fillpats --------------------------------";
  1053 "--------- build fun get_fillpats --------------------------------";
  1054 (*cause for this test was: wrong thy in Build_Thydata.thy in 
  1055   insert_fillpats ["IsacKnowledge", "Diff", "Theorems", "diff_sin_chain"]*)
  1056 val f_curr = str2term "d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))"
  1057 val errpatID = "chain-rule-diff-both"
  1058   val {errpats, scr = Prog prog, ...} = get_met ["diff", "differentiate_on_R"]
  1059 val env = 
  1060   [(str2term "f_f", str2term "x ^^^ 2 + sin (x ^^^ 4)"),
  1061    (str2term "v_v", str2term "x"),
  1062    (str2term "f_f'", str2term "d_d x (x ^^^ 2 + sin (x ^^^ 4))")]
  1063   val subst = get_bdv_subst prog env
  1064   val errpatthms = errpats
  1065     |> filter ((curry op = errpatID) o (#1: errpat -> errpatID))
  1066     |> map (#3: errpat -> thm list)
  1067     |> flat;
  1068 "~~~~~ fun get_fillpats, args:"; val (subst, form, errpatID, thm) =
  1069   (subst, f_curr, errpatID, hd (*simulate beginning of "map"*) errpatthms);
  1070         val thmDeriv = Thm.get_name_hint thm
  1071         val (part, thyID) = thy_containing_thm thmDeriv
  1072         val theID = [part, thyID, "Theorems", thmID_of_derivation_name thmDeriv]
  1073         val Hthm {fillpats, ...} = get_the theID
  1074         val some = map (get_fillform subst (thm, form) errpatID) fillpats;
  1075 "~~~~~ fun get_fillform, args:";
  1076   val ((subs_opt, subst), (thm, form), errpatID, (fillpatID, pat, erpaID)) =
  1077   (subst, (thm, form), errpatID, hd (*simulate beginning of "map"*) fillpats);
  1078   val (form', _, _, rewritten) =
  1079         rew_sub (Isac()) 1 subst e_rew_ord e_rls false [] (Trueprop $ pat) form;
  1080 "~~~~~ fun rew_sub, args:"; val (thy, i, bdv, tless, rls, put_asm, lrd:lrd list, r, t) = 
  1081   ((Isac()), 1, subst, e_rew_ord, e_rls, false, [], (Trueprop $ pat), form);
  1082      val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop
  1083                        o Logic.strip_imp_concl) r;
  1084 (*/-------------catch the planned exception*)
  1085 (let
  1086      val r' = Envir.subst_term (Pattern.match thy (lhs, t) 
  1087 					      (Vartab.empty, Vartab.empty)) r;
  1088 in 111 end
  1089 ) handle PATTERN (*because only a subterm matches*) => 111;
  1090 case t of t1 $ t2 => 
  1091 	  let val (t2', asm2, lrd, rew2) = rew_sub thy i bdv tless rls put_asm (lrd@[R]) r t2
  1092 	  in 
  1093 	    if rew2 then (t1 $ t2', asm2, lrd, true) else
  1094 	      let val (t1', asm1, lrd, rew1) = rew_sub thy i bdv tless rls put_asm (lrd@[L]) r t1
  1095 	      in if rew1 then (t1' $ t2, asm1, lrd, true) else (t1 $ t2,[], lrd, false) end
  1096 	  end;
  1097 (*catch the planned exception-------------/*)
  1098 
  1099 val t1 $ t2 = t;
  1100 val (t2', asm2, lrd, rew2) = rew_sub thy i bdv tless rls put_asm (lrd@[R]) r t2;
  1101 "~~~~~ fun rew_sub, args:"; val (thy, i, bdv, tless, rls, put_asm, lrd:lrd list, r, t) = 
  1102   (thy, i, bdv, tless, rls, put_asm, (lrd@[R]), r, t2);
  1103      val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r;
  1104      val r' = Envir.subst_term (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r;
  1105      val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'));
  1106      val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r';
  1107 if term2str t' = "cos (x ^^^ 4) * d_d x ?_dummy_1" then ()
  1108   else error "get_fillpats changed 1"
  1109 
  1110 "--------- embed fun find_fillpatterns ---------------------------";
  1111 "--------- embed fun find_fillpatterns ---------------------------";
  1112 "--------- embed fun find_fillpatterns ---------------------------";
  1113 reset_states ();
  1114 CalcTree
  1115 [(["functionTerm (x ^ 2 + sin (x ^ 4))", "differentiateFor x", "derivative f_f'"], 
  1116   ("Isac", ["derivative_of","function"], ["diff","differentiate_on_R"]))];
  1117 Iterator 1;
  1118 moveActiveRoot 1;
  1119 autoCalculate' 1 CompleteCalcHead;
  1120 autoCalculate' 1 (Step 1);
  1121 autoCalculate' 1 (Step 1);(*([1], Res), d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))*)
  1122 appendFormula 1 "d_d x (x ^ 2) + cos (4 * x ^ 3)" (*|> Future.join*);
  1123   (*<CALCMESSAGE> error pattern #chain-rule-diff-both# </CALCMESSAGE>*)
  1124   (*or
  1125     <CALCMESSAGE> no derivation found </CALCMESSAGE>*)
  1126 
  1127 "~~~~~ fun findFillpatterns, args:"; val (cI, errpatID) = (1, "chain-rule-diff-both");
  1128   val ((pt, _), _) = get_calc cI
  1129 				val pos = get_pos cI 1;
  1130 "~~~~~ fun find_fillpatterns, args:"; val ((pt, pos as (p, _)), errpatID) = ((pt, pos), errpatID);
  1131 	    val f_curr = get_curr_formula (pt, pos); (* = "d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))"*)
  1132     val pp = par_pblobj pt p
  1133     val {errpats, scr = Prog prog, ...} = get_met (get_obj g_metID pt pp)
  1134     val ScrState (env, _, _, _, _, _) = get_istate pt pos
  1135     val subst = get_bdv_subst prog env
  1136     val errpatthms = errpats
  1137       |> filter ((curry op = errpatID) o (#1: errpat -> errpatID))
  1138       |> map (#3: errpat -> thm list)
  1139       |> flat;
  1140 
  1141 case map (get_fillpats subst f_curr errpatID) errpatthms |> flat of
  1142   ("fill-d_d-arg", tm, thm, subs_opt) :: _ => if term2str tm = 
  1143     "d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4)) =\nd_d x (x ^^^ 2) + cos (x ^^^ 4) * d_d x ?_dummy_1"
  1144     then () else error "find_fillpatterns changed 1a"
  1145 | _ => error "find_fillpatterns changed 1b"
  1146 
  1147 "~~~~~ fun get_fillpats, args:"; val (subst, form, errpatID, thm) =
  1148   (subst, f_curr, errpatID, hd (*simulate beginning of "map"*) errpatthms);
  1149         val thmDeriv = Thm.get_name_hint thm
  1150         val (part, thyID) = thy_containing_thm thmDeriv
  1151         val theID = [part, thyID, "Theorems", thmID_of_derivation_name thmDeriv]
  1152         val Hthm {fillpats, ...} = get_the theID
  1153         val some = map (get_fillform subst (thm, form) errpatID) fillpats;
  1154 
  1155 case some |> filter is_some |> map the of
  1156   ("fill-d_d-arg", tm, thm, subsopt) :: _ => if term2str tm = 
  1157     "d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4)) =\nd_d x (x ^^^ 2) + cos (x ^^^ 4) * d_d x ?_dummy_1"
  1158     then () else error "find_fillpatterns changed 2a"
  1159 | _ => error "find_fillpatterns changed 2b"
  1160 
  1161 "~~~~~ fun get_fillform, args:";
  1162   val ((subs_opt, subst), (thm, form), errpatID, (fillpatID, pat, erpaID)) =
  1163   (subst, (thm, form), errpatID, hd (*simulate beginning of "map"*) fillpats);
  1164 val (form', _, _, rewritten) =
  1165       rew_sub (Isac()) 1 subst e_rew_ord e_rls false [] (Trueprop $ pat) form;
  1166 
  1167 if term2str form' = "d_d x (x ^^^ 2) + cos (x ^^^ 4) * d_d x ?_dummy_1" then ()
  1168 else error "find_fillpatterns changed 3";
  1169 
  1170 "~~~~~ to findFillpatterns return val:"; val (fillpats) =
  1171   (map (get_fillpats (subs_opt, subst) f_curr errpatID) errpatthms |> flat) (*only from "hd errpatthms"*);
  1172 
  1173 "vvv--- dropped this code WN120730";
  1174 val msg = "fill patterns " ^
  1175   ((map ((apsnd term2str) o quad2pair) fillpats) |> map pair2str_ |> strs2str_);
  1176 msg =
  1177   "fill patterns #fill-d_d-arg#d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))" ^
  1178     " =\nd_d x (x ^^^ 2) + cos (x ^^^ 4) * d_d x ?_dummy_1" ^
  1179   "#fill-both-args#d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))" ^
  1180     " =\nd_d x (x ^^^ 2) + cos ?_dummy_2 * d_d x ?_dummy_3" ^
  1181   "#fill-d_d#d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))" ^
  1182     " =\nd_d x (x ^^^ 2) + cos (x ^^^ 4) * ?_dummy_1 x (x ^^^ 4)" ^
  1183   "#fill-inner-deriv#d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))" ^
  1184     " =\nd_d x (x ^^^ 2) + cos (x ^^^ 4) * ?_dummy_1" ^
  1185   "#fill-all#d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4)) = d_d x (x ^^^ 2) + ?_dummy_1#";
  1186 "^^^--- dropped this code WN120730";
  1187 
  1188 if (map #1 fillpats) =
  1189   ["fill-d_d-arg", "fill-both-args", "fill-d_d", "fill-inner-deriv", "fill-all"]
  1190 then () else error "find_fillpatterns changed 4b";
  1191 
  1192 "--------- build fun is_exactly_equal, inputFillFormula ----------";
  1193 "--------- build fun is_exactly_equal, inputFillFormula ----------";
  1194 "--------- build fun is_exactly_equal, inputFillFormula ----------";
  1195 reset_states ();
  1196 CalcTree
  1197 [(["functionTerm (x ^ 2 + sin (x ^ 4))", "differentiateFor x", "derivative f_f'"], 
  1198   ("Isac", ["derivative_of","function"], ["diff","differentiate_on_R"]))];
  1199 Iterator 1;
  1200 moveActiveRoot 1;
  1201 autoCalculate' 1 CompleteCalcHead;
  1202 autoCalculate' 1 (Step 1);
  1203 autoCalculate' 1 (Step 1);(*([1], Res), d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))*)
  1204 appendFormula 1 "d_d x (x ^ 2) + cos (4 * x ^ 3)" (*|> Future.join*); (*<<<<<<<=========================*)
  1205 (* the check for errpat is maximally liberal (whole term modulo "nrls" from "type met"),
  1206   would recognize "cos (4 * x ^ (4 - 1)) + 2 * x" as well.
  1207   results in <CALCMESSAGE> error pattern #chain-rule-diff-both# </CALCMESSAGE>
  1208   instead of <CALCMESSAGE> no derivation found </CALCMESSAGE> *)
  1209   val ((pt,pos), _) = get_calc 1;
  1210   val p = get_pos 1 1;
  1211   val (Form f, _, asms) = pt_extract (pt, p);
  1212 
  1213   if p = ([1], Res) andalso term2str f = "d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))" andalso
  1214     get_obj g_tac pt (fst p) = Rewrite_Inst (["(bdv, x)"], 
  1215       ("diff_sum", "d_d ?bdv (?u + ?v) = d_d ?bdv ?u + d_d ?bdv ?v"))
  1216   then () else error "embed fun get_fillform changed 1";
  1217 
  1218 findFillpatterns 1 "chain-rule-diff-both"; (*<<<<<<<=================================*)
  1219 (*<CALCMESSAGE> fill patterns #fill-d_d-arg#d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4)) =
  1220   d_d x (x ^^^ 2) + cos (x ^^^ 4) * d_d x ?_dummy_1#fill-both-args#...#...#... *)
  1221   val ((pt,pos),_) = get_calc 1;
  1222   val p = get_pos 1 1;
  1223 
  1224   val (Form f, _, asms) = pt_extract (pt, p);
  1225   if p = ([1], Res) andalso term2str f = "d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))" andalso
  1226     get_obj g_tac pt (fst p) = Rewrite_Inst (["(bdv, x)"], 
  1227       ("diff_sum", "d_d ?bdv (?u + ?v) = d_d ?bdv ?u + d_d ?bdv ?v"))
  1228   then () else error "embed fun get_fillform changed 2";
  1229 
  1230 requestFillformula 1 ("chain-rule-diff-both", "fill-both-args");(*<<<<<<<============*)
  1231   (*<AUTOCALC> ([1], Res) ([2], Res) ([2], Res) </AUTOCALC>*)
  1232   val ((pt,pos),_) = get_calc 1;
  1233   val p = get_pos 1 1;
  1234   val (Form f, _, asms) = pt_extract (pt, p);
  1235   if p = ([1], Res) andalso existpt [2] pt andalso
  1236     term2str f = "d_d x (x ^^^ 2) + d_d x (sin (x ^^^ 4))" andalso
  1237     get_obj g_tac pt (fst p) = Rewrite_Inst (["(bdv, x)"], 
  1238       ("diff_sum", "d_d ?bdv (?u + ?v) = d_d ?bdv ?u + d_d ?bdv ?v"))
  1239   then () else error "embed fun get_fillform changed 3";
  1240 
  1241 (* input a formula which exactly fills the gaps in a "fillformula"
  1242    presented to the learner immediately before by "requestFillformula (errpatID, fillpatID)":
  1243    errpatID: lhs of the respective thm = lhs of fillformula with fillpatID.
  1244    the respective thm is in the ctree ................
  1245 *)
  1246 "~~~~~ fun inputFillFormula, args:"; val (cI, ifo) =
  1247   (1, "d_d x (x ^^^ 2) + cos (x ^^^ 4) * d_d x (x ^^^ 4)");
  1248     val ((pt, _), _) = get_calc cI
  1249     val pos = get_pos cI 1;
  1250 
  1251 "~~~~~ fun is_exactly_equal, args:"; val ((pt, pos as (p, p_)), istr) = ((pt, pos), ifo);
  1252   val SOME ifo = parseNEW (assoc_thy "Isac" |> thy2ctxt) istr
  1253   val p' = lev_on p;
  1254   val tac = get_obj g_tac pt p';
  1255 if tac = Rewrite_Inst (["(bdv, x)"], ("diff_sin_chain", "d_d ?bdv (sin ?u) = cos ?u * d_d ?bdv ?u"))
  1256 then () else error "inputFillFormula changed 10";
  1257   val Appl rew = applicable_in pos pt tac;
  1258   val Rewrite_Inst' (_, _, _, _, _, _, _, (res, _)) = rew;
  1259 
  1260 "~~~~~ to inputFillFormula return val:"; val ("ok", tac) = ("ok", tac);
  1261   val ("ok", (_, c, ptp as (_,p'))) = locatetac tac (pt, pos);
  1262     upd_calc cI (ptp, []);
  1263     upd_ipos cI 1 p';
  1264     autocalculateOK2xml cI pos (if null c then p' else last_elem c) p';
  1265 
  1266 "~~~~~ final check:";
  1267 val ((pt, _),_) = get_calc 1;
  1268 val p = get_pos 1 1;
  1269 val (Form f, _, asms) = pt_extract (pt, p);
  1270 if p = ([2], Res) andalso
  1271   term2str f = "d_d x (x ^^^ 2) + cos (x ^^^ 4) * d_d x (x ^^^ 4)" andalso
  1272   get_obj g_tac pt (fst p) =
  1273     Rewrite_Inst (["(bdv, x)"], ("diff_sin_chain", "d_d ?bdv (sin ?u) = cos ?u * d_d ?bdv ?u"))
  1274 then () else error "inputFillFormula changed 11";
  1275