test/Tools/isac/Knowledge/polyminus.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 11 Jan 2023 11:38:01 +0100
changeset 60650 06ec8abfd3bc
parent 60594 439f7f3867ec
child 60660 c4b24621077e
permissions -rw-r--r--
eliminate use of Thy_Info 12: TermC partially
     1 (* title:  Knowledge/polyminus.sml
     2    author: Walther Neuper
     3    WN071207,
     4    (c) due to copyright terms
     5 *)
     6 "--------------------------------------------------------";
     7 "--------------------------------------------------------";
     8 "table of contents --------------------------------------";
     9 "--------------------------------------------------------";
    10 "----------- fun identifier --------------------------------------------------------------------";
    11 "----------- fun eval_kleiner, fun kleiner -----------------------------------------------------";
    12 "----------- fun ist_monom ---------------------------------------------------------------------";
    13 "----------- fun eval_ist_monom -------------------------";
    14 "----------- watch order_add_mult  ----------------------";
    15 "----------- build predicate for +- ordering ------------";
    16 "----------- build fasse_zusammen -----------------------";
    17 "----------- build verschoenere -------------------------";
    18 "----------- met simplification for_polynomials with_minu";
    19 "----------- me simplification.for_polynomials.with_minus";
    20 "----------- pbl polynom vereinfachen p.33 --------------";
    21 "----------- met probe fuer_polynom ---------------------";
    22 "----------- pbl polynom probe --------------------------";
    23 "----------- pbl klammer polynom vereinfachen p.34 ------";
    24 "----------- try fun applyTactics -----------------------";
    25 "----------- pbl binom polynom vereinfachen p.39 --------";
    26 "----------- pbl binom polynom vereinfachen: cube -------";
    27 "----------- Refine.refine Vereinfache -------------------------";
    28 "----------- *** Problem.prep_input: syntax error in '#Where' of [v";
    29 "--------------------------------------------------------";
    30 "--------------------------------------------------------";
    31 "--------------------------------------------------------";
    32 
    33 val thy = @{theory "PolyMinus"};
    34 val ctxt = Proof_Context.init_global thy;
    35 
    36 "----------- fun identifier --------------------------------------------------------------------";
    37 "----------- fun identifier --------------------------------------------------------------------";
    38 "----------- fun identifier --------------------------------------------------------------------";
    39 if identifier (TermC.parse_test @{context} "12 ::real") = "12"     then () else error "identifier 1";
    40 if identifier (TermC.parse_test @{context}
    41   "5 * e + 6 * f - 8 * g - 9 - 7 * e - 4 * f + 10 * g ::real") = "|||||||||||||"
    42                                                       then () else error "identifier 1a";
    43 
    44 if identifier (TermC.parse_test @{context} "a ::real") = "a"       then () else error "identifier 2";
    45 if identifier (TermC.parse_test @{context} "3 * a ::real") = "a"   then () else error "identifier 3";
    46 
    47 if identifier (TermC.parse_test @{context} "a \<up> 2 ::real") = "a"   then () else error "identifier 4";
    48 if identifier (TermC.parse_test @{context} "3*a \<up> 2 ::real") = "a" then () else error "identifier 5";
    49 if identifier (TermC.parse_test @{context} "a * b ::real") = "b"   then () else error "identifier 5b";
    50 
    51 (*these are strange (see "specific monomials" in comment to fun.def.)..*)
    52 if identifier (TermC.parse_test @{context} "a*b ::real") = "b"     then () else error "identifier 6";
    53 if identifier (TermC.parse_test @{context} "(3*a*b) ::real") = "b" then () else error "identifier 7";
    54 
    55 
    56 "----------- fun eval_kleiner, fun kleiner -----------------------------------------------------";
    57 "----------- fun eval_kleiner, fun kleiner -----------------------------------------------------";
    58 "----------- fun eval_kleiner, fun kleiner -----------------------------------------------------";
    59 "a" < "b";
    60 "ba" < "ab";
    61 "123" < "a"; (*unused due to ---vvv*)
    62 "12" < "3"; (*true !!!*)
    63 
    64 " a kleiner b ==> (b + a) = (a + b)";
    65 TermC.parse_test @{context} "aaa";
    66 TermC.parse_test @{context} "222 * aaa";
    67 
    68 case eval_kleiner 0 0 (TermC.parse_test @{context} "123 kleiner 32") 0 of
    69     SOME ("123 kleiner 32 = False", _) => ()
    70   | _ => error "polyminus.sml: 12 kleiner 9 = False";
    71 case eval_kleiner 0 0 (TermC.parse_test @{context} "a kleiner b") 0 of
    72     SOME ("a kleiner b = True", _) => ()
    73   | _ => error "polyminus.sml: a kleiner b = True";
    74 case eval_kleiner 0 0 (TermC.parse_test @{context} "(10*g) kleiner f") 0 of
    75     SOME ("10 * g kleiner f = False", _) => ()
    76   | _ => error "polyminus.sml: 10 * g kleiner f = False";
    77 case eval_kleiner 0 0 (TermC.parse_test @{context} "(a \<up> 2) kleiner b") 0 of
    78     SOME ("a \<up> 2 kleiner b = True", _) => ()
    79   | _ => error "polyminus.sml: a \<up> 2 kleiner b = True";
    80 case eval_kleiner 0 0 (TermC.parse_test @{context} "(3*a \<up> 2) kleiner b") 0 of
    81     SOME ("3 * a \<up> 2 kleiner b = True", _) => ()
    82   | _ => error "polyminus.sml: 3 * a \<up> 2 kleiner b = True";
    83 case eval_kleiner 0 0 (TermC.parse_test @{context} "(a*b) kleiner c") 0 of
    84     SOME ("a * b kleiner c = True", _) => ()
    85   | _ => error "polyminus.sml: a * b kleiner b = True";
    86 case eval_kleiner 0 0 (TermC.parse_test @{context} "(3*a*b) kleiner c") 0 of
    87     SOME ("3 * a * b kleiner c = True", _) => ()
    88   | _ => error "polyminus.sml: 3 * a * b kleiner b = True";
    89 
    90 
    91 val t = TermC.parse_test @{context} "12 kleiner 5 * e + 6 * f - 8 * g - 9 - 7 * e - 4 * f + 10 * (g::real)";
    92 val SOME ("12 kleiner 5 * e + 6 * f - 8 * g - 9 - 7 * e - 4 * f + 10 * g = True", _) =
    93            eval_kleiner "aaa" "bbb" t "ccc";
    94 "~~~~~ fun eval_kleiner , args:"; val (_, _, (p as (Const (\<^const_name>\<open>kleiner\<close>,_) $ a $ b)), _) =
    95   ("aaa", "bbb", t, "ccc");
    96     (*if*) TermC.is_num b (*else*);
    97 
    98    	(*if*) identifier a < identifier b  (*else*);
    99 "~~~~~ fun identifier , args:"; val (t) = (a);
   100 (*+*)case a of
   101   Const (\<^const_name>\<open>numeral\<close>, _) $ (Const (\<^const_name>\<open>num.Bit0\<close>, _) $
   102     (Const (\<^const_name>\<open>num.Bit0\<close>, _) $ (Const (\<^const_name>\<open>num.Bit1\<close>, _) $ Const (\<^const_name>\<open>num.One\<close>, _)))) => ()
   103 | _ => error "eval_kleiner CHANGED";                                                 (*isa*)
   104 
   105 
   106 "----------- fun ist_monom ---------------------------------------------------------------------";
   107 "----------- fun ist_monom ---------------------------------------------------------------------";
   108 "----------- fun ist_monom ---------------------------------------------------------------------";
   109 val t = TermC.parse_test @{context} "0 ::real";
   110  if ist_monom t then () else error "ist_monom 1";
   111 
   112 val t = TermC.parse_test @{context} "a";
   113 if ist_monom t then () else error "ist_monom 2";
   114 
   115 val t = TermC.parse_test @{context} "2 * a";
   116 if ist_monom t then () else error "ist_monom 3";
   117 
   118 val t = TermC.parse_test @{context} "2 * a * b";
   119 if ist_monom t then () else error "ist_monom 4";
   120 
   121 val t = TermC.parse_test @{context} "a * b";
   122 if ist_monom t then () else error "ist_monom 5";
   123 
   124 (*not covered before NEW numerals*)
   125 val t = TermC.parse_test @{context} "2 * a \<up> 2 * b";
   126 if ist_monom t then () else error "ist_monom 6";
   127 
   128 (*not covered before NEW numerals*)
   129 val t = TermC.parse_test @{context} "a \<up> 2 * b \<up> 3";
   130 if ist_monom t then () else error "ist_monom 7";
   131 
   132 val t = TermC.parse_test @{context} "a \<up> 2 * 4 * b \<up> 3 * 5";
   133 if ist_monom t then () else error "ist_monom 8";
   134 
   135 
   136 "----------- fun eval_ist_monom ----------------------------------";
   137 "----------- fun eval_ist_monom ----------------------------------";
   138 "----------- fun eval_ist_monom ----------------------------------";
   139 case eval_ist_monom 0 0 (TermC.parse_test @{context} "12 ist_monom") 0 of
   140     SOME ("12 ist_monom = True", _) => ()
   141   | _ => error "polyminus.sml: 12 ist_monom = True";
   142 
   143 case eval_ist_monom 0 0 (TermC.parse_test @{context} "a ist_monom") 0 of
   144     SOME ("a ist_monom = True", _) => ()
   145   | _ => error "polyminus.sml: a ist_monom = True";
   146 
   147 case eval_ist_monom 0 0 (TermC.parse_test @{context} "(3*a) ist_monom") 0 of
   148     SOME ("3 * a ist_monom = True", _) => ()
   149   | _ => error "polyminus.sml: 3 * a ist_monom = True";
   150 
   151 case eval_ist_monom 0 0 (TermC.parse_test @{context} "(a \<up> 2) ist_monom") 0 of 
   152    SOME ("a \<up> 2 ist_monom = True", _) => ()
   153   | _ => error "polyminus.sml: a \<up> 2 ist_monom = True";
   154 
   155 case eval_ist_monom 0 0 (TermC.parse_test @{context} "(3*a \<up> 2) ist_monom") 0 of
   156     SOME ("3 * a \<up> 2 ist_monom = True", _) => ()
   157   | _ => error "polyminus.sml: 3*a \<up> 2 ist_monom = True";
   158 
   159 case eval_ist_monom 0 0 (TermC.parse_test @{context} "(a*b) ist_monom") 0 of
   160     SOME ("a * b ist_monom = True", _) => ()
   161   | _ => error "polyminus.sml: a*b ist_monom = True";
   162 
   163 case eval_ist_monom 0 0 (TermC.parse_test @{context} "(3*a*b) ist_monom") 0 of
   164     SOME ("3 * a * b ist_monom = True", _) => ()
   165   | _ => error "polyminus.sml: 3*a*b ist_monom = True";
   166 
   167 
   168 "----------- watch order_add_mult  -------------------------------";
   169 "----------- watch order_add_mult  -------------------------------";
   170 "----------- watch order_add_mult  -------------------------------";
   171 "----- with these simple variables it works...";
   172 val ctxt = @{context};
   173 val t = TermC.parse_test ctxt "((a + d) + c) + b";
   174 val SOME (t,_) = rewrite_set_ ctxt false order_add_mult t; UnparseC.term t;
   175 if UnparseC.term t = "a + (b + (c + d))" then ()
   176 else error "polyminus.sml 1 watch order_add_mult";
   177 
   178 "----- the same stepwise...";
   179 val od = ord_make_polynomial true
   180 val t = TermC.parse_test @{context} "((a + d) + c) + b";
   181 "((a + d) + c) + b"; 
   182 val SOME (t,_) = rewrite_ ctxt od Rule_Set.empty true @{thm add.commute} t; UnparseC.term t;
   183 "b + ((a + d) + c)";
   184 val SOME (t,_) = rewrite_ ctxt od Rule_Set.empty true @{thm add.commute} t; UnparseC.term t;
   185 "b + (c + (a + d))";
   186 val SOME (t,_) = rewrite_ ctxt od Rule_Set.empty true @{thm add.left_commute} t;UnparseC.term t;
   187 "b + (a + (c + d))";
   188 val SOME (t,_) = rewrite_ ctxt od Rule_Set.empty true @{thm add.left_commute} t;UnparseC.term t;
   189 "a + (b + (c + d))";
   190 if UnparseC.term t = "a + (b + (c + d))" then ()
   191 else error "polyminus.sml 2 watch order_add_mult";
   192 
   193 "----- if parentheses are right, left_commute is (almost) sufficient...";
   194 val t = TermC.parse_test @{context} "a + (d + (c + b))";
   195 "a + (d + (c + b))";
   196 val SOME (t,_) = rewrite_ ctxt od Rule_Set.empty true @{thm add.left_commute} t;UnparseC.term t;
   197 "a + (c + (d + b))";
   198 val SOME (t,_) = rewrite_ ctxt od Rule_Set.empty true @{thm add.commute} t;UnparseC.term t;
   199 "a + (c + (b + d))";
   200 val SOME (t,_) = rewrite_ ctxt od Rule_Set.empty true @{thm add.left_commute} t;UnparseC.term t;
   201 "a + (b + (c + d))";
   202 
   203 "----- but we do not want the parentheses at right; thus: cond.rew.";
   204 "WN0712707 complicated monomials do not yet work ...";
   205 val t = TermC.parse_test @{context} "((5*a + 4*d) + 3*c) + 2*b";
   206 val SOME (t,_) = rewrite_set_ ctxt false order_add_mult t; UnparseC.term t;
   207 if UnparseC.term t = "2 * b + (3 * c + (4 * d + 5 * a))" then ()
   208 else error "polyminus.sml: order_add_mult changed";
   209 
   210 "----- here we see rew_sub going into subterm with ord.rew....";
   211 val od = ord_make_polynomial false;
   212 val t = TermC.parse_test @{context} "b + a + c + d";
   213 val SOME (t,_) = rewrite_ ctxt od Rule_Set.empty false @{thm add.commute} t; UnparseC.term t;
   214 val SOME (t,_) = rewrite_ ctxt od Rule_Set.empty false @{thm add.commute} t; UnparseC.term t;
   215 (*@@@ rew_sub gosub: t = d + (b + a + c)
   216   @@@ rew_sub begin: t = b + a + c*)
   217 
   218 
   219 "----------- build predicate for +- ordering ---------------------";
   220 "----------- build predicate for +- ordering ---------------------";
   221 "----------- build predicate for +- ordering ---------------------";
   222 "a" < "b";
   223 "ba" < "ab";
   224 "123" < "a"; (*unused due to ---vvv*)
   225 "12" < "3"; (*true !!!*)
   226 
   227 " a kleiner b ==> (b + a) = (a + b)";
   228 TermC.parse_test @{context} "aaa";
   229 TermC.parse_test @{context} "222 * aaa";
   230 
   231 case eval_kleiner 0 0 (TermC.parse_test @{context} "123 kleiner 32") 0 of
   232     SOME ("123 kleiner 32 = False", _) => ()
   233   | _ => error "polyminus.sml: 12 kleiner 9 = False";
   234 
   235 case eval_kleiner 0 0 (TermC.parse_test @{context} "a kleiner b") 0 of
   236     SOME ("a kleiner b = True", _) => ()
   237   | _ => error "polyminus.sml: a kleiner b = True";
   238 
   239 case eval_kleiner 0 0 (TermC.parse_test @{context} "(10*g) kleiner f") 0 of
   240     SOME ("10 * g kleiner f = False", _) => ()
   241   | _ => error "polyminus.sml: 10 * g kleiner f = False";
   242 
   243 case eval_kleiner 0 0 (TermC.parse_test @{context} "(a \<up> 2) kleiner b") 0 of
   244     SOME ("a \<up> 2 kleiner b = True", _) => ()
   245   | _ => error "polyminus.sml: a \<up> 2 kleiner b = True";
   246 
   247 case eval_kleiner 0 0 (TermC.parse_test @{context} "(3*a \<up> 2) kleiner b") 0 of
   248     SOME ("3 * a \<up> 2 kleiner b = True", _) => ()
   249   | _ => error "polyminus.sml: 3 * a \<up> 2 kleiner b = True";
   250 
   251 case eval_kleiner 0 0 (TermC.parse_test @{context} "(a*b) kleiner c") 0 of
   252     SOME ("a * b kleiner c = True", _) => ()
   253   | _ => error "polyminus.sml: a * b kleiner b = True";
   254 
   255 case eval_kleiner 0 0 (TermC.parse_test @{context} "(3*a*b) kleiner c") 0 of
   256     SOME ("3 * a * b kleiner c = True", _) => ()
   257   | _ => error "polyminus.sml: 3 * a * b kleiner b = True";
   258 
   259 "======= compare tausche_plus with real_num_collect";
   260 val od = Rewrite_Ord.function_empty;
   261 
   262 val asm_rls = erls_ordne_alphabetisch;
   263 val t = TermC.parse_test @{context} "b + a";
   264 val SOME (t,_) = rewrite_ ctxt od asm_rls false @{thm tausche_plus} t; UnparseC.term t;
   265 if UnparseC.term t = "a + b" then ()
   266 else error "polyminus.sml: ordne_alphabetisch1 b + a";
   267 
   268 val asm_rls = Atools_erls;
   269 val t = TermC.parse_test @{context} "2*a + 3*a";
   270 val SOME (t,_) = rewrite_ ctxt od asm_rls false @{thm real_num_collect} t; UnparseC.term t;
   271 
   272 "======= test rewrite_, rewrite_set_";
   273 (*Rewrite.trace_on := true; ..stopped Test_Isac.thy*)
   274 val asm_rls = erls_ordne_alphabetisch;
   275 val t = TermC.parse_test @{context} "b + a";
   276 val SOME (t,_) = rewrite_set_ ctxt false ordne_alphabetisch t; UnparseC.term t;
   277 if UnparseC.term t = "a + b" then ()
   278 else error "polyminus.sml: ordne_alphabetisch a + b";
   279 
   280 val t = TermC.parse_test @{context} "2*b + a";
   281 val SOME (t,_) = rewrite_set_ ctxt false ordne_alphabetisch t; UnparseC.term t;
   282 if UnparseC.term t = "a + 2 * b" then ()
   283 else error "polyminus.sml: ordne_alphabetisch a + 2 * b";
   284 
   285 val t = TermC.parse_test @{context} "a + c + b";
   286 val SOME (t,_) = rewrite_set_ ctxt false ordne_alphabetisch t; UnparseC.term t;
   287 if UnparseC.term t = "a + b + c" then ()
   288 else error "polyminus.sml: ordne_alphabetisch a + b + c";
   289 
   290 "======= rewrite goes into subterms";
   291 val t = TermC.parse_test @{context} "a + c + b + d ::real";
   292 val SOME (t,_) = rewrite_ ctxt od asm_rls false @{thm tausche_plus_plus} t; UnparseC.term t;
   293 if UnparseC.term t = "a + b + c + d" then ()
   294 else error "polyminus.sml: ordne_alphabetisch1 a + b + c + d";
   295 
   296 val t = TermC.parse_test @{context} "a + c + d + b";
   297 val SOME (t,_) = rewrite_set_ ctxt false ordne_alphabetisch t; UnparseC.term t;
   298 if UnparseC.term t = "a + b + c + d" then ()
   299 else error "polyminus.sml: ordne_alphabetisch2 a + b + c + d";
   300 
   301 "======= here we see rew_sub going into subterm with cond.rew....";
   302 val t = TermC.parse_test @{context} "b + a + c + d";
   303 val SOME (t,_) = rewrite_ ctxt od asm_rls false @{thm tausche_plus} t; UnparseC.term t;
   304 if UnparseC.term t = "a + b + c + d" then ()
   305 else error "polyminus.sml: ordne_alphabetisch3 a + b + c + d";
   306 
   307 "======= compile rls for the most complicated terms";
   308 val t = TermC.parse_test @{context} "5*e + 6*f - 8*g - 9 - 7*e - 4*f + 10*g + 12";
   309 "5 * e + 6 * f - 8 * g - 9 - 7 * e - 4 * f + 10 * g + 12";
   310 val SOME (t,_) = rewrite_set_ ctxt false ordne_alphabetisch t; 
   311 if UnparseC.term t = "- 9 + 12 + 5 * e - 7 * e + 6 * f - 4 * f - 8 * g + 10 * g"
   312 then () else error "polyminus.sml: ordne_alphabetisch finished";
   313 
   314 
   315 
   316 "----------- build fasse_zusammen --------------------------------";
   317 "----------- build fasse_zusammen --------------------------------";
   318 "----------- build fasse_zusammen --------------------------------";
   319 val t = TermC.parse_test @{context} "- 9 + 12 + 5 * e - 7 * e + 6 * f - 4 * f - 8 * g + 10 * g";
   320 val SOME (t,_) = rewrite_set_ ctxt false fasse_zusammen t;
   321 if UnparseC.term t = "3 + - 2 * e + 2 * f + 2 * g" then ()
   322 else error "polyminus.sml: fasse_zusammen finished";
   323 
   324 "----------- build verschoenere ----------------------------------";
   325 "----------- build verschoenere ----------------------------------";
   326 "----------- build verschoenere ----------------------------------";
   327 val t = TermC.parse_test @{context} "3 + - 2 * e + 2 * f + 2 * g";
   328 val SOME (t,_) = rewrite_set_ ctxt false verschoenere t;
   329 if UnparseC.term t = "3 - 2 * e + 2 * f + 2 * g" then ()
   330 else error "polyminus.sml: verschoenere 3 + - 2 * e ...";
   331 
   332 
   333 "----------- met simplification for_polynomials with_minus -------";
   334 "----------- met simplification for_polynomials with_minus -------";
   335 "----------- met simplification for_polynomials with_minus -------";
   336 val str = 
   337 "Program SimplifyScript (t_t::real) =                \
   338 \  (((Try (Rewrite_Set ordne_alphabetisch False)) #>     \
   339 \    (Try (Rewrite_Set fasse_zusammen False)) #>     \
   340 \    (Try (Rewrite_Set verschoenere False))) t_t)"
   341 val sc = (inst_abs o (TermC.parseNEW' ctxt)) str;
   342 TermC.atom_trace_detail @{context} sc;
   343 
   344 "----------- me simplification.for_polynomials.with_minus";
   345 "----------- me simplification.for_polynomials.with_minus";
   346 "----------- me simplification.for_polynomials.with_minus";
   347 val c = [];
   348 val (p,_,f,nxt,_,pt) = 
   349       Test_Code.init_calc @{context} 
   350         [(["Term (5*e + 6*f - 8*g - 9 - 7*e - 4*f + 10*g + 12)",
   351            "normalform N"],
   352 	          ("PolyMinus",["plus_minus", "polynom", "vereinfachen"],
   353 	           ["simplification", "for_polynomials", "with_minus"]))];
   354 val (p,_,f,nxt,_,pt) = me nxt p c pt;
   355 val (p,_,f,nxt,_,pt) = me nxt p c pt;
   356 val (p,_,f,nxt,_,pt) = me nxt p c pt;
   357 val (p,_,f,nxt,_,pt) = me nxt p c pt;
   358 val (p,_,f,nxt,_,pt) = me nxt p c pt;
   359 val (p,_,f,nxt,_,pt) = me nxt p c pt;
   360 
   361 val (p,_,f,nxt,_,pt) = me nxt p c pt;
   362 val (p,_,f,nxt,_,pt) = me nxt p c pt;
   363 val (p,_,f,nxt,_,pt) = me nxt p c pt;
   364 val (p,_,f,nxt,_,pt) = me nxt p c pt;
   365 val (p,_,f,nxt,_,pt) = me nxt p c pt;
   366 if f2str f = "3 - 2 * e + 2 * f + 2 * g" 
   367 then case nxt of End_Proof' => () | _ =>  error "me simplification.for_polynomials.with_minus 1"
   368 else error "polyminus.sml: me simplification.for_polynomials.with_minus 2";
   369 
   370 "----------- pbl polynom vereinfachen p.33 -----------------------";
   371 "----------- pbl polynom vereinfachen p.33 -----------------------";
   372 "----------- pbl polynom vereinfachen p.33 -----------------------";
   373 "----------- 140 c ---";
   374 States.reset ();
   375 CalcTree @{context} [(["Term (5*e + 6*f - 8*g - 9 - 7*e - 4*f + 10*g + 12)",
   376 	    "normalform N"],
   377 	   ("PolyMinus",["plus_minus", "polynom", "vereinfachen"],
   378 	    ["simplification", "for_polynomials", "with_minus"]))];
   379 moveActiveRoot 1;
   380 autoCalculate 1 CompleteCalc;
   381 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   382 if p = ([], Res) andalso 
   383    UnparseC.term (get_obj g_res pt (fst p)) = "3 - 2 * e + 2 * f + 2 * g"
   384 then () else error "polyminus.sml: Vereinfache (3 - 2 * e + 2 * f...";
   385 
   386 "======= 140 d ---";
   387 States.reset ();
   388 CalcTree @{context} [(["Term (-r - 2*s - 3*t + 5 + 4*r + 8*s - 5*t - 2)",
   389 	    "normalform N"],
   390 	   ("PolyMinus",["plus_minus", "polynom", "vereinfachen"],
   391 	    ["simplification", "for_polynomials", "with_minus"]))];
   392 moveActiveRoot 1;
   393 autoCalculate 1 CompleteCalc;
   394 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   395 if p = ([], Res) andalso 
   396    UnparseC.term (get_obj g_res pt (fst p)) = "3 + 3 * r + 6 * s - 8 * t"
   397 then () else error "polyminus.sml: Vereinfache 140 d)";
   398 
   399 "======= 139 c ---";
   400 States.reset ();
   401 CalcTree @{context} [(["Term (3*e - 6*f - 8*e - 4*f + 5*e + 7*f)",
   402 	    "normalform N"],
   403 	   ("PolyMinus",["plus_minus", "polynom", "vereinfachen"],
   404 	    ["simplification", "for_polynomials", "with_minus"]))];
   405 moveActiveRoot 1;
   406 autoCalculate 1 CompleteCalc;
   407 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   408 if p = ([], Res) andalso 
   409    UnparseC.term (get_obj g_res pt (fst p)) = "- (3 * f)"
   410 then () else error "polyminus.sml: Vereinfache 139 c)";
   411 
   412 "======= 139 b ---";
   413 States.reset ();
   414 CalcTree @{context} [(["Term (8*u - 5*v - 5*u + 7*v - 6*u - 3*v)",
   415 	    "normalform N"],
   416 	   ("PolyMinus",["plus_minus", "polynom", "vereinfachen"],
   417 	    ["simplification", "for_polynomials", "with_minus"]))];
   418 moveActiveRoot 1;
   419 autoCalculate 1 CompleteCalc;
   420 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   421 if p = ([], Res) andalso 
   422    UnparseC.term (get_obj g_res pt (fst p)) = "- 3 * u - v"
   423 then () else error "polyminus.sml: Vereinfache 139 b)";
   424 
   425 "======= 138 a ---";
   426 States.reset ();
   427 CalcTree @{context} [(["Term (2*u - 3*v - 6*u + 5*v)",
   428 	    "normalform N"],
   429 	   ("PolyMinus",["plus_minus", "polynom", "vereinfachen"],
   430 	    ["simplification", "for_polynomials", "with_minus"]))];
   431 moveActiveRoot 1;
   432 autoCalculate 1 CompleteCalc;
   433 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   434 if p = ([], Res) andalso 
   435    UnparseC.term (get_obj g_res pt (fst p)) = "- 4 * u + 2 * v"
   436 then () else error "polyminus.sml: Vereinfache 138 a)";
   437 
   438 "----------- met probe fuer_polynom ------------------------------";
   439 "----------- met probe fuer_polynom ------------------------------";
   440 "----------- met probe fuer_polynom ------------------------------";
   441 val str = 
   442 "Program ProbeScript (e_e::bool) (w_s::bool list) =\
   443 \ (let e_e = Take e_e;                             \
   444 \      e_e = Substitute w_s e_e                    \
   445 \ in (Repeat((Try (Repeat (Calculate ''TIMES''))) #>  \
   446 \            (Try (Repeat (Calculate ''PLUS''))) #>  \
   447 \            (Try (Repeat (Calculate ''MINUS''))))) e_e)"
   448 val sc = (inst_abs o (TermC.parseNEW' ctxt)) str;
   449 TermC.atom_trace_detail @{context} sc;
   450 
   451 "----------- pbl polynom probe -----------------------------------";
   452 "----------- pbl polynom probe -----------------------------------";
   453 "----------- pbl polynom probe -----------------------------------";
   454 States.reset ();
   455 CalcTree @{context} [(["Pruefe ((5::int)*e + 6*f - 8*g - 9 - 7*e - 4*f + 10*g + 12 =\
   456 	    \3 - 2 * e + 2 * f + 2 * (g::int))",
   457 	    "mitWert [e = (1::int), f = (2::int), g = (3::int)]",
   458 	    "Geprueft b"],
   459 	   ("PolyMinus",["polynom", "probe"],
   460 	    ["probe", "fuer_polynom"]))];
   461 moveActiveRoot 1;
   462 autoCalculate 1 CompleteCalc;
   463 (* autoCalculate 1 CompleteCalcHead;
   464    autoCalculate 1 (Steps 1);
   465    autoCalculate 1 (Steps 1);
   466    val ((pt,p),_) = States.get_calc 1; UnparseC.term (get_obj g_res pt (fst p));
   467 @@@@@WN081114 gives "??.empty", all "Pruefe" are the same,
   468 although analogies work in interface.sml: FIXME.WN081114 in "Pruefe"*)
   469 val ((pt,p),_) = States.get_calc 1;
   470 if p = ([], Res) andalso UnparseC.term (get_obj g_res pt (fst p)) = "11 = 11"
   471 then () else error "polyminus.sml: Probe 11 = 11";
   472 Test_Tool.show_pt pt;
   473 
   474 
   475 "----------- pbl klammer polynom vereinfachen p.34 ---------------";
   476 "----------- pbl klammer polynom vereinfachen p.34 ---------------";
   477 "----------- pbl klammer polynom vereinfachen p.34 ---------------";
   478 States.reset ();
   479 CalcTree @{context} [(["Term (2*u - 5 - (3 - 4*u) + (8*u + 9))",
   480 	    "normalform N"],
   481 	   ("PolyMinus",["klammer", "polynom", "vereinfachen"],
   482 	    ["simplification", "for_polynomials", "with_parentheses"]))];
   483 moveActiveRoot 1;
   484 autoCalculate 1 CompleteCalc;
   485 val ((pt,p),_) = States.get_calc 1;
   486 if p = ([], Res) andalso 
   487    UnparseC.term (get_obj g_res pt (fst p)) = "1 + 14 * u"
   488 then () else error "polyminus.sml: Vereinfache (2*u - 5 - (3 - ...";
   489 Test_Tool.show_pt pt;
   490 
   491 "======= probe p.34 -----";
   492 States.reset ();
   493 CalcTree @{context} [(["Pruefe (2*u - 5 - (3 - 4*u) + (8*u + 9) = 1 + 14 * (u::int))",
   494 	    "mitWert [u = (2::int)]",
   495 	    "Geprueft b"],
   496 	   ("PolyMinus",["polynom", "probe"],
   497 	    ["probe", "fuer_polynom"]))];
   498 moveActiveRoot 1;
   499 autoCalculate 1 CompleteCalc;
   500 val ((pt,p),_) = States.get_calc 1;
   501 if p = ([], Res) andalso UnparseC.term (get_obj g_res pt (fst p)) = "29 = 29"
   502 then () else error "polyminus.sml: Probe 29 = 29";
   503 Test_Tool.show_pt pt;
   504 
   505 
   506 "----------- try fun applyTactics --------------------------------";
   507 "----------- try fun applyTactics --------------------------------";
   508 "----------- try fun applyTactics --------------------------------";
   509 States.reset ();
   510 CalcTree @{context} [(["Term (5*e + 6*f - 8*g - 9 - 7*e - 4*f + 10*g + 12)",
   511 	    "normalform N"],
   512 	   ("PolyMinus",["plus_minus", "polynom", "vereinfachen"],
   513 	    ["simplification", "for_polynomials", "with_minus"]))];
   514 moveActiveRoot 1;
   515 autoCalculate 1 CompleteCalcHead;
   516 autoCalculate 1 (Steps 1);
   517 autoCalculate 1 (Steps 1);
   518 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   519 "----- 1  \<up> ";
   520 fetchApplicableTactics 1 0 p;
   521 val appltacs = specific_from_prog pt p;
   522 applyTactic 1 p (hd appltacs) (*addiere_x_plus_minus*);
   523 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   524 "----- 2  \<up> ";
   525 (*Rewrite.trace_on := true; ..stopped Test_Isac.thy*)
   526 val asm_rls = erls_ordne_alphabetisch;
   527 val t = TermC.parse_test @{context} "- 9 + 12 + 5 * e - 7 * e + (6 - 4) * f - 8 * g + 10 * g";
   528 val SOME (t',_) = 
   529     rewrite_ ctxt Rewrite_Ord.function_empty asm_rls false @{thm tausche_minus} t;
   530 UnparseC.term t';     "- 9 + 12 + 5 * e - 7 * e + (- 4 + 6) * f - 8 * g + 10 * g";
   531 
   532 val t = TermC.parse_test @{context} "- 9 + 12 + 5 * e - 7 * e + (6 - 4) * f - 8 * g + 10 * g";
   533 val NONE = 
   534     rewrite_ ctxt Rewrite_Ord.function_empty asm_rls false @{thm tausche_minus_plus} t;
   535 
   536 val t = TermC.parse_test @{context} "- 9 + 12 + 5 * e - 7 * e + (6 - 4) * f - 8 * g + 10 * g";
   537 val SOME (t',_) = 
   538     rewrite_set_ ctxt false ordne_alphabetisch t;
   539 UnparseC.term t';     "- 9 + 12 + 5 * e - 7 * e - 8 * g + 10 * g + (- 4 + 6) * f";
   540 
   541 
   542 applyTactic 1 p (hd (specific_from_prog pt p)) (*tausche_minus*);
   543 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   544 "----- 3  \<up> ";
   545 applyTactic 1 p (hd (specific_from_prog pt p)) (**);
   546 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   547 "----- 4  \<up> ";
   548 applyTactic 1 p (hd (specific_from_prog pt p)) (**);
   549 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   550 "----- 5  \<up> ";
   551 applyTactic 1 p (hd (specific_from_prog pt p)) (**);
   552 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   553 "----- 6  \<up> ";
   554 
   555 (*<CALCMESSAGE> failure </CALCMESSAGE>
   556 applyTactic 1 p (hd (specific_from_prog pt p)) (**);
   557 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   558 "----- 7  \<up> ";
   559 *)
   560 autoCalculate 1 CompleteCalc;
   561 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   562 (*independent from failure above: met_simp_poly_minus not confluent:
   563 (([9], Res), - (8 * g) + 10 * g + (3 - 2 * e + 2 * f)),
   564 (([], Res), - (8 * g) + 10 * g + (3 - 2 * e + 2 * f))]
   565 ~~~~~~~~~~~###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*)
   566 val ([], Res) = p;
   567 val "2 * g + (3 - 2 * e + 2 * f)" = get_obj g_res pt (fst p) |> UnparseC.term;
   568 (* ---------^^^--- not quite perfect*)
   569 
   570 
   571 "#############################################################################";
   572 States.reset ();
   573 CalcTree @{context} [(["Term (- (8 * g) + 10 * g + h)",
   574 	    "normalform N"],
   575 	   ("PolyMinus",["plus_minus", "polynom", "vereinfachen"],
   576 	    ["simplification", "for_polynomials", "with_minus"]))];
   577 moveActiveRoot 1;
   578 autoCalculate 1 CompleteCalc;
   579 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   580 if p = ([], Res) andalso UnparseC.term (get_obj g_res pt (fst p)) = "2 * g + h"
   581 then () else error "polyminus.sml: addiere_vor_minus";
   582 
   583 
   584 "#############################################################################";
   585 States.reset ();
   586 CalcTree @{context} [(["Term (- (8 * g) + 10 * g + f)",
   587 	    "normalform N"],
   588 	   ("PolyMinus",["plus_minus", "polynom", "vereinfachen"],
   589 	    ["simplification", "for_polynomials", "with_minus"]))];
   590 moveActiveRoot 1;
   591 autoCalculate 1 CompleteCalc;
   592 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   593 if p = ([], Res) andalso UnparseC.term (get_obj g_res pt (fst p)) = "f + 2 * g"
   594 then () else error "polyminus.sml: tausche_vor_plus";
   595 
   596 "----------- pbl binom polynom vereinfachen p.39 -----------------";
   597 "----------- pbl binom polynom vereinfachen p.39 -----------------";
   598 "----------- pbl binom polynom vereinfachen p.39 -----------------";
   599 val thy = @{theory};
   600 val rls = klammern_ausmultiplizieren;
   601 val t = TermC.parse_test @{context} "(3 * a + 2) * (4 * a - 1::real)";
   602 val SOME (t,_) = rewrite_set_ ctxt false rls t; UnparseC.term t;
   603 "3 * a * (4 * a) - 3 * a * 1 + (2 * (4 * a) - 2 * 1)";
   604 val rls = discard_parentheses;
   605 val SOME (t,_) = rewrite_set_ ctxt false rls t; UnparseC.term t;
   606 "3 * a * 4 * a - 3 * a * 1 + (2 * 4 * a - 2 * 1)";
   607 val rls = ordne_monome;
   608 val SOME (t,_) = rewrite_set_ ctxt false rls t; UnparseC.term t;
   609 "3 * 4 * a * a - 1 * 3 * a + (2 * 4 * a - 1 * 2)";
   610 (*
   611 val t = TermC.parse_test @{context} "3 * a * 4 * a";
   612 val rls = ordne_monome;
   613 val SOME (t,_) = rewrite_set_ ctxt false rls t; UnparseC.term t;
   614 *)
   615 val rls = klammern_aufloesen;
   616 val SOME (t,_) = rewrite_set_ ctxt false rls t; UnparseC.term t;
   617 "3 * 4 * a * a - 1 * 3 * a + 2 * 4 * a - 1 * 2";
   618 val rls = ordne_alphabetisch;
   619 (*TODO: make is_monom more general, a*a=a^2, ...*)
   620 val SOME (t,_) = rewrite_set_ ctxt false rls t; UnparseC.term t;
   621 "3 * 4 * a * a - 1 * 2 - 1 * 3 * a + 2 * 4 * a";
   622 (*STOPPED.WN080104
   623 val rls = fasse_zusammen;
   624 val SOME (t,_) = rewrite_set_ ctxt false rls t; UnparseC.term t;
   625 val rls = verschoenere;
   626 val SOME (t,_) = rewrite_set_ ctxt false rls t; UnparseC.term t;
   627 *)
   628 
   629 (*@@@@@@@*)
   630 States.reset ();
   631 CalcTree @{context} [(["Term ((3*a + 2) * (4*a - 1))",
   632 	    "normalform N"],
   633 	   ("PolyMinus",["binom_klammer", "polynom", "vereinfachen"],
   634 	    ["simplification", "for_polynomials", "with_parentheses_mult"]))];
   635 moveActiveRoot 1;
   636 autoCalculate 1 CompleteCalc;
   637 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   638 
   639 if p = ([], Res) andalso
   640    UnparseC.term (get_obj g_res pt (fst p)) =(*"- 2 + 12 * a \<up> 2 + 5 * a" with OLD numerals*)
   641                                                "- 2 + 5 * a + 12 * a \<up> 2"
   642 then () else error "polyminus.sml: Vereinfache (2*u - 5 - (3 - ...";
   643 
   644 "----------- pbl binom polynom vereinfachen: cube ----------------";
   645 "----------- pbl binom polynom vereinfachen: cube ----------------";
   646 "----------- pbl binom polynom vereinfachen: cube ----------------";
   647 States.reset ();
   648 CalcTree @{context} [(["Term (8*(a - q) + a - 2*q + 3*(a - 2*q))", "normalform N"],
   649 	   ("PolyMinus",["binom_klammer", "polynom", "vereinfachen"],
   650 	    ["simplification", "for_polynomials", "with_parentheses_mult"]))];
   651 moveActiveRoot 1;
   652 autoCalculate 1 CompleteCalc;
   653 val ((pt,p),_) = States.get_calc 1; Test_Tool.show_pt pt;
   654 if p = ([], Res) andalso UnparseC.term (get_obj g_res pt (fst p)) = "12 * a - 16 * q" 
   655 then () else error "pbl binom polynom vereinfachen: cube";
   656 
   657 "----------- Refine.refine Vereinfache ----------------------------------";
   658 "----------- Refine.refine Vereinfache ----------------------------------";
   659 "----------- Refine.refine Vereinfache ----------------------------------";
   660 val fmz = ["Term (8*(a - q) + a - 2*q + 3*(a - 2*(q::real)))", "normalform (N::real)"];
   661 (*default_print_depth 11;*)
   662 val matches = Refine.xxxxx @{context} fmz ["vereinfachen"];
   663 (*default_print_depth 3;*)
   664 
   665 "----- go into details, if it seems not to work -----";
   666 "--- does the predicate evaluate correctly ?";
   667 val t = TermC.parse_patt_test @{theory} 
   668 	    "matchsub (?a * (?b - ?c)) (8 * (a - q) + a - 2 * q + 3 * (a - 2 * (q::real)))";
   669 val ma = eval_matchsub "" "Prog_Expr.matchsub" t ctxt;
   670 case ma of
   671     SOME ("matchsub (?a * (?b - ?c)) (8 * (a - q) + \
   672 	  \a - 2 * q + 3 * (a - 2 * q)) = True", _) => ()
   673   | _ => error "polyminus.sml matchsub (?a * (?b - ?c)...A";
   674 
   675 "--- does the respective where_rls rewrite ?";
   676 val where_rls = Rule_Set.append_rules "prls_pbl_vereinf_poly" Rule_Set.empty 
   677 	     [Eval ("Poly.is_polyexp", eval_is_polyexp ""),
   678 	      Eval ("Prog_Expr.matchsub", eval_matchsub ""),
   679 	      Thm ("or_true",@{thm or_true}),
   680 	      (*"(?a | True) = True"*)
   681 	      Thm ("or_false",@{thm or_false}),
   682 	      (*"(?a | False) = ?a"*)
   683 	      Thm ("not_true", @{thm not_true}),
   684 	      (*"(~ True) = False"*)
   685 	      Thm ("not_false", @{thm not_false})
   686 	      (*"(~ False) = True"*)];
   687 (*Rewrite.trace_on := true; ..stopped Test_Isac.thy*)
   688 val SOME (t', _) = rewrite_set_ ctxt false where_rls t;
   689 
   690 "--- does the respective where_rls rewrite the whole predicate ?";
   691 val t = TermC.parse_patt_test @{theory}
   692 	    "Not (matchsub (?a * (?b + ?c)) (8 * (a - q) + a - 2 * q) | \
   693 	    \     matchsub (?a * (?b - ?c)) (8 * (a - q) + a - 2 * q) | \
   694 	    \     matchsub ((?b + ?c) * ?a) (8 * (a - q) + a - 2 * q) | \
   695 	    \     matchsub ((?b - ?c) * ?a) (8 * (a - q) + a - 2 * q) )";
   696 (*Rewrite.trace_on := true; ..stopped Test_Isac.thy*)
   697 val SOME (t', _) = rewrite_set_ ctxt false where_rls t;
   698 if UnparseC.term t' = "False" then ()
   699 else error "polyminus.sml Not (matchsub (?a * (?b + ?c)) (8 ...";
   700 
   701 "----------- *** Problem.prep_input: syntax error in '#Where' of [v";
   702 "----------- *** Problem.prep_input: syntax error in '#Where' of [v";
   703 "----------- *** Problem.prep_input: syntax error in '#Where' of [v";
   704 (*see test/../termC.sml for details*)
   705 val t = TermC.parse_patt thy "t_t is_polyexp";
   706 val t = TermC.parse_patt thy ("Not (matchsub (?a + (?b + ?c)) t_t | " ^
   707 	                "     matchsub (?a + (?b - ?c)) t_t | " ^
   708 	                "     matchsub (?a - (?b + ?c)) t_t | " ^
   709 	                "     matchsub (?a + (?b - ?c)) t_t )");
   710 (*show_types := true;
   711 if UnparseC.term t = "~ (matchsub ((?a::real) + ((?b::real) + (?c::real))) (t_t::real) |\n   matchsub (?a + (?b - ?c)) t_t |\n   matchsub (?a - (?b + ?c)) t_t | matchsub (?a + (?b - ?c)) t_t)"
   712 then () else error "polyminus.sml type-structure of \"?a :: real\" changed 1";
   713 show_types := false;*)
   714 if UnparseC.term t =
   715   "\<not> (matchsub (?a + (?b + ?c)) t_t \<or>\n        " ^
   716   "matchsub (?a + (?b - ?c)) t_t \<or>\n        " ^
   717   "matchsub (?a - (?b + ?c)) t_t \<or> " ^
   718   "matchsub (?a + (?b - ?c)) t_t)"
   719 then () else error "polyminus.sml type-structure of \"?a :: real\" changed 1";
   720