src/Tools/isac/Knowledge/PolyMinus.thy
author wneuper <walther.neuper@jku.at>
Tue, 01 Jun 2021 15:41:23 +0200
changeset 60317 638d02a9a96a
parent 60278 343efa173023
child 60325 a7c0e6ab4883
permissions -rw-r--r--
Test_Some.thy with looping ML<>
     1 (* attempts to perserve binary minus as wanted by Austrian teachers
     2    WN071207
     3    (c) due to copyright terms
     4 *)
     5 
     6 theory PolyMinus imports (*Poly// due to "is_ratpolyexp" in...*) Rational begin
     7 
     8 consts
     9 
    10   (*predicates for conditions in rewriting*)
    11   kleiner     :: "['a, 'a] => bool" 	("_ kleiner _") 
    12   ist_monom  :: "'a => bool"		("_ ist'_monom")
    13 
    14   (*the CAS-command*)
    15   Probe       :: "[bool, bool list] => bool"  
    16 	(*"Probe (3*a+2*b+a = 4*a+2*b) [a=1,b=2]"*)
    17 
    18   (*descriptions for the pbl and met*)
    19   Pruefe      :: "bool => una"
    20   mitWert     :: "bool list => tobooll"
    21   Geprueft    :: "bool => una"
    22 
    23 axiomatization where
    24 
    25   null_minus:            "0 - a = -a" and
    26   vor_minus_mal:         "- a * b = (-a) * b" and
    27 
    28   (*commute with invariant (a.b).c -association*)
    29   tausche_plus:		"[| b ist_monom; a kleiner b  |] ==> 
    30 			 (b + a) = (a + b)" and
    31   tausche_minus:		"[| b ist_monom; a kleiner b  |] ==> 
    32 			 (b - a) = (-a + b)" and
    33   tausche_vor_plus:	"[| b ist_monom; a kleiner b  |] ==> 
    34 			 (- b + a) = (a - b)" and
    35   tausche_vor_minus:	"[| b ist_monom; a kleiner b  |] ==> 
    36 			 (- b - a) = (-a - b)" and
    37 (*Ambiguous input\<^here> produces 3 parse trees -----------------------------\\*)
    38   tausche_plus_plus:	"b kleiner c ==> (a + c + b) = (a + b + c)" and
    39   tausche_plus_minus:	"b kleiner c ==> (a + c - b) = (a - b + c)" and
    40   tausche_minus_plus:	"b kleiner c ==> (a - c + b) = (a + b - c)" and
    41   tausche_minus_minus:	"b kleiner c ==> (a - c - b) = (a - b - c)" and
    42 (*Ambiguous input\<^here> produces 3 parse trees -----------------------------//*)
    43 
    44   (*commute with invariant (a.b).c -association*)
    45   tausche_mal:		"[| b is_atom; a kleiner b  |] ==> 
    46 			 (b * a) = (a * b)" and
    47   tausche_vor_mal:	"[| b is_atom; a kleiner b  |] ==> 
    48 			 (-b * a) = (-a * b)" and
    49   tausche_mal_mal:	"[| c is_atom; b kleiner c  |] ==> 
    50 			 (x * c * b) = (x * b * c)" and
    51   x_quadrat:             "(x * a) * a = x * a \<up> 2" and
    52 
    53 
    54   subtrahiere:               "[| l is_const; m is_const |] ==>  
    55 			     m * v - l * v = (m - l) * v" and
    56   subtrahiere_von_1:         "[| l is_const |] ==>  
    57 			     v - l * v = (1 - l) * v" and
    58   subtrahiere_1:             "[| l is_const; m is_const |] ==>  
    59 			     m * v - v = (m - 1) * v" and
    60 
    61   subtrahiere_x_plus_minus:  "[| l is_const; m is_const |] ==>  
    62 			     (x + m * v) - l * v = x + (m - l) * v" and
    63   subtrahiere_x_plus1_minus: "[| l is_const |] ==>  
    64 			     (x + v) - l * v = x + (1 - l) * v" and
    65   subtrahiere_x_plus_minus1: "[| m is_const |] ==>  
    66 			     (x + m * v) - v = x + (m - 1) * v" and
    67 
    68   subtrahiere_x_minus_plus:  "[| l is_const; m is_const |] ==>  
    69 			     (x - m * v) + l * v = x + (-m + l) * v" and
    70   subtrahiere_x_minus1_plus: "[| l is_const |] ==>  
    71 			     (x - v) + l * v = x + (-1 + l) * v" and
    72   subtrahiere_x_minus_plus1: "[| m is_const |] ==>  
    73 			     (x - m * v) + v = x + (-m + 1) * v" and
    74 
    75   subtrahiere_x_minus_minus: "[| l is_const; m is_const |] ==>  
    76 			     (x - m * v) - l * v = x + (-m - l) * v" and
    77   subtrahiere_x_minus1_minus:"[| l is_const |] ==>  
    78 			     (x - v) - l * v = x + (-1 - l) * v" and
    79   subtrahiere_x_minus_minus1:"[| m is_const |] ==>  
    80 			     (x - m * v) - v = x + (-m - 1) * v" and
    81 
    82 
    83   addiere_vor_minus:         "[| l is_const; m is_const |] ==>  
    84 			     - (l * v) +  m * v = (-l + m) * v" and
    85   addiere_eins_vor_minus:    "[| m is_const |] ==>  
    86 			     -  v +  m * v = (-1 + m) * v" and
    87   subtrahiere_vor_minus:     "[| l is_const; m is_const |] ==>  
    88 			     - (l * v) -  m * v = (-l - m) * v" and
    89   subtrahiere_eins_vor_minus:"[| m is_const |] ==>  
    90 			     -  v -  m * v = (-1 - m) * v" and
    91 
    92 (*Ambiguous input\<^here> produces 3 parse trees -----------------------------\\*)
    93   vorzeichen_minus_weg1:      "l kleiner 0 ==> a + l * b = a - -1*l * b" and
    94   vorzeichen_minus_weg2:      "l kleiner 0 ==> a - l * b = a + -1*l * b" and
    95   vorzeichen_minus_weg3:      "l kleiner 0 ==> k + a - l * b = k + a + -1*l * b" and
    96   vorzeichen_minus_weg4:      "l kleiner 0 ==> k - a - l * b = k - a + -1*l * b" and
    97 (*Ambiguous input\<^here> produces 3 parse trees -----------------------------//*)
    98 
    99   (*klammer_plus_plus = (add.assoc RS sym)*)
   100   klammer_plus_minus:          "a + (b - c) = (a + b) - c" and
   101   klammer_minus_plus:          "a - (b + c) = (a - b) - c" and
   102   klammer_minus_minus:         "a - (b - c) = (a - b) + c" and
   103 
   104   klammer_mult_minus:          "a * (b - c) = a * b - a * c" and
   105   klammer_minus_mult:          "(b - c) * a = b * a - c * a"
   106 
   107 ML \<open>
   108 val thy = @{theory};
   109 
   110 (** eval functions **)
   111 
   112 (*. get the identifier from specific monomials; see fun ist_monom .*)
   113 (*HACK.WN080107*)
   114 fun increase str = 
   115   let
   116     val (s, ss) = 
   117       case Symbol.explode str of
   118         s :: ss => (s, ss)
   119       | _ => raise ERROR "PolyMinus.increase: uncovered case"
   120   in implode ((chr (ord s + 1))::ss) end;
   121 fun identifier (Free (id,_)) = id                            (* 2     ,   a   *)
   122   | identifier (Const ("Groups.times_class.times", _) $ Free (_(*num*), _) $ Free (id, _)) = 
   123     id                                                       (* 2*a   , a*b *)
   124   | identifier (Const ("Groups.times_class.times", _) $                          (* 3*a*b    *)
   125 		     (Const ("Groups.times_class.times", _) $
   126 			    Free (num, _) $ Free _) $ Free (id, _)) = 
   127     if TermC.is_num' num then id
   128     else "|||||||||||||"
   129   | identifier (Const ("Transcendental.powr", _) $ Free (base, _) $ Free (_(*exp*), _)) =
   130     if TermC.is_num' base then "|||||||||||||"                     (* a^2      *)
   131     else (*increase*) base
   132   | identifier (Const ("Groups.times_class.times", _) $ Free (num, _) $          (* 3*a^2    *)
   133 		     (Const ("Transcendental.powr", _) $
   134 			    Free (base, _) $ Free (_(*exp*), _))) = 
   135     if TermC.is_num' num andalso not (TermC.is_num' base) then (*increase*) base
   136     else "|||||||||||||"
   137   | identifier _ = "|||||||||||||"(*the "largest" string*);
   138 
   139 (*("kleiner", ("PolyMinus.kleiner", eval_kleiner ""))*)
   140 (* order "by alphabet" w.r.t. var: num < (var | num*var) > (var*var | ..) *)
   141 fun eval_kleiner _ _ (p as (Const ("PolyMinus.kleiner",_) $ a $ b)) _  =
   142      if TermC.is_num b then
   143 	 if TermC.is_num a then (*123 kleiner 32 = True !!!*)
   144 	     if TermC.num_of_term a < TermC.num_of_term b then 
   145 		 SOME ((UnparseC.term p) ^ " = True",
   146 		       HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
   147 	     else SOME ((UnparseC.term p) ^ " = False",
   148 			HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
   149 	 else (* -1 * -2 kleiner 0 *)
   150 	     SOME ((UnparseC.term p) ^ " = False",
   151 		   HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
   152     else
   153 	if identifier a < identifier b then 
   154 	     SOME ((UnparseC.term p) ^ " = True",
   155 		  HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
   156 	else SOME ((UnparseC.term p) ^ " = False",
   157 		   HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
   158   | eval_kleiner _ _ _ _ =  NONE;
   159 
   160 fun ist_monom (Free _) = true                                                   (* 2,   a   *)
   161   | ist_monom (Const ("Groups.times_class.times", _) $
   162       (Const ("Num.numeral_class.numeral", _) $ _) $ Free _) = true
   163   | ist_monom (Const ("Groups.times_class.times", _) $                          (* 2*a, a*b *)
   164       Free _ $ (Const ("Num.numeral_class.numeral", _) $ _)) = false
   165   | ist_monom (Const ("Groups.times_class.times", _) $                          (* 3*a*b    *)
   166 		  (Const ("Groups.times_class.times", _) $
   167 			  (Const ("Num.numeral_class.numeral", _) $ _) $ Free _) $ Free _) = true
   168   | ist_monom (Const ("Transcendental.powr", _) $                               (* a^2      *)
   169       Free _ $ (Const ("Num.numeral_class.numeral", _) $ _)) = true
   170   | ist_monom (Const ("Groups.times_class.times", _) $                          (* 3*a^2    *)
   171       (Const ("Num.numeral_class.numeral", _) $ _) $
   172 		     (Const ("Transcendental.powr", _) $ Free _ $ Free _)) = true
   173   | ist_monom _ = false;
   174 
   175 (* is this a univariate monomial ? *)
   176 (*("ist_monom", ("PolyMinus.ist_monom", eval_ist_monom ""))*)
   177 fun eval_ist_monom _ _ (p as (Const ("PolyMinus.ist_monom",_) $ a)) _  =
   178     if ist_monom a  then 
   179 	SOME ((UnparseC.term p) ^ " = True",
   180 	      HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
   181     else SOME ((UnparseC.term p) ^ " = False",
   182 	       HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
   183   | eval_ist_monom _ _ _ _ =  NONE;
   184 
   185 
   186 (** rewrite order **)
   187 
   188 (** rulesets **)
   189 
   190 val erls_ordne_alphabetisch =
   191     Rule_Set.append_rules "erls_ordne_alphabetisch" Rule_Set.empty
   192 	       [Rule.Eval ("PolyMinus.kleiner", eval_kleiner ""),
   193 		Rule.Eval ("PolyMinus.ist_monom", eval_ist_monom "")
   194 		];
   195 
   196 val ordne_alphabetisch = 
   197   Rule_Def.Repeat{id = "ordne_alphabetisch", preconds = [], 
   198       rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), srls = Rule_Set.Empty, calc = [], errpatts = [],
   199       erls = erls_ordne_alphabetisch, 
   200       rules = [Rule.Thm ("tausche_plus",ThmC.numerals_to_Free @{thm tausche_plus}),
   201 	       (*"b kleiner a ==> (b + a) = (a + b)"*)
   202 	       Rule.Thm ("tausche_minus",ThmC.numerals_to_Free @{thm tausche_minus}),
   203 	       (*"b kleiner a ==> (b - a) = (-a + b)"*)
   204 	       Rule.Thm ("tausche_vor_plus",ThmC.numerals_to_Free @{thm tausche_vor_plus}),
   205 	       (*"[| b ist_monom; a kleiner b  |] ==> (- b + a) = (a - b)"*)
   206 	       Rule.Thm ("tausche_vor_minus",ThmC.numerals_to_Free @{thm tausche_vor_minus}),
   207 	       (*"[| b ist_monom; a kleiner b  |] ==> (- b - a) = (-a - b)"*)
   208 	       Rule.Thm ("tausche_plus_plus",ThmC.numerals_to_Free @{thm tausche_plus_plus}),
   209 	       (*"c kleiner b ==> (a + c + b) = (a + b + c)"*)
   210 	       Rule.Thm ("tausche_plus_minus",ThmC.numerals_to_Free @{thm tausche_plus_minus}),
   211 	       (*"c kleiner b ==> (a + c - b) = (a - b + c)"*)
   212 	       Rule.Thm ("tausche_minus_plus",ThmC.numerals_to_Free @{thm tausche_minus_plus}),
   213 	       (*"c kleiner b ==> (a - c + b) = (a + b - c)"*)
   214 	       Rule.Thm ("tausche_minus_minus",ThmC.numerals_to_Free @{thm tausche_minus_minus})
   215 	       (*"c kleiner b ==> (a - c - b) = (a - b - c)"*)
   216 	       ], scr = Rule.Empty_Prog};
   217 
   218 val fasse_zusammen = 
   219     Rule_Def.Repeat{id = "fasse_zusammen", preconds = [], 
   220 	rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
   221 	erls = Rule_Set.append_rules "erls_fasse_zusammen" Rule_Set.empty 
   222 			  [Rule.Eval ("Prog_Expr.is_const", Prog_Expr.eval_const "#is_const_")], 
   223 	srls = Rule_Set.Empty, calc = [], errpatts = [],
   224 	rules = 
   225 	[Rule.Thm ("real_num_collect",ThmC.numerals_to_Free @{thm real_num_collect}), 
   226 	 (*"[| l is_const; m is_const |]==>l * n + m * n = (l + m) * n"*)
   227 	 Rule.Thm ("real_num_collect_assoc_r",ThmC.numerals_to_Free @{thm real_num_collect_assoc_r}),
   228 	 (*"[| l is_const; m..|] ==>  (k + m * n) + l * n = k + (l + m)*n"*)
   229 	 Rule.Thm ("real_one_collect",ThmC.numerals_to_Free @{thm real_one_collect}),	
   230 	 (*"m is_const ==> n + m * n = (1 + m) * n"*)
   231 	 Rule.Thm ("real_one_collect_assoc_r",ThmC.numerals_to_Free @{thm real_one_collect_assoc_r}), 
   232 	 (*"m is_const ==> (k + n) + m * n = k + (m + 1) * n"*)
   233 
   234 
   235 	 Rule.Thm ("subtrahiere",ThmC.numerals_to_Free @{thm subtrahiere}),
   236 	 (*"[| l is_const; m is_const |] ==> m * v - l * v = (m - l) * v"*)
   237 	 Rule.Thm ("subtrahiere_von_1",ThmC.numerals_to_Free @{thm subtrahiere_von_1}),
   238 	 (*"[| l is_const |] ==> v - l * v = (1 - l) * v"*)
   239 	 Rule.Thm ("subtrahiere_1",ThmC.numerals_to_Free @{thm subtrahiere_1}),
   240 	 (*"[| l is_const; m is_const |] ==> m * v - v = (m - 1) * v"*)
   241 
   242 	 Rule.Thm ("subtrahiere_x_plus_minus",ThmC.numerals_to_Free @{thm subtrahiere_x_plus_minus}), 
   243 	 (*"[| l is_const; m..|] ==> (k + m * n) - l * n = k + ( m - l) * n"*)
   244 	 Rule.Thm ("subtrahiere_x_plus1_minus",ThmC.numerals_to_Free @{thm subtrahiere_x_plus1_minus}),
   245 	 (*"[| l is_const |] ==> (x + v) - l * v = x + (1 - l) * v"*)
   246 	 Rule.Thm ("subtrahiere_x_plus_minus1",ThmC.numerals_to_Free @{thm subtrahiere_x_plus_minus1}),
   247 	 (*"[| m is_const |] ==> (x + m * v) - v = x + (m - 1) * v"*)
   248 
   249 	 Rule.Thm ("subtrahiere_x_minus_plus",ThmC.numerals_to_Free @{thm subtrahiere_x_minus_plus}), 
   250 	 (*"[| l is_const; m..|] ==> (k - m * n) + l * n = k + (-m + l) * n"*)
   251 	 Rule.Thm ("subtrahiere_x_minus1_plus",ThmC.numerals_to_Free @{thm subtrahiere_x_minus1_plus}),
   252 	 (*"[| l is_const |] ==> (x - v) + l * v = x + (-1 + l) * v"*)
   253 	 Rule.Thm ("subtrahiere_x_minus_plus1",ThmC.numerals_to_Free @{thm subtrahiere_x_minus_plus1}),
   254 	 (*"[| m is_const |] ==> (x - m * v) + v = x + (-m + 1) * v"*)
   255 
   256 	 Rule.Thm ("subtrahiere_x_minus_minus",ThmC.numerals_to_Free @{thm subtrahiere_x_minus_minus}), 
   257 	 (*"[| l is_const; m..|] ==> (k - m * n) - l * n = k + (-m - l) * n"*)
   258 	 Rule.Thm ("subtrahiere_x_minus1_minus",ThmC.numerals_to_Free @{thm subtrahiere_x_minus1_minus}),
   259 	 (*"[| l is_const |] ==> (x - v) - l * v = x + (-1 - l) * v"*)
   260 	 Rule.Thm ("subtrahiere_x_minus_minus1",ThmC.numerals_to_Free @{thm subtrahiere_x_minus_minus1}),
   261 	 (*"[| m is_const |] ==> (x - m * v) - v = x + (-m - 1) * v"*)
   262 	 
   263 	 Rule.Eval ("Groups.plus_class.plus", (**)eval_binop "#add_"),
   264 	 Rule.Eval ("Groups.minus_class.minus", (**)eval_binop "#subtr_"),
   265 	 
   266 	 (*MG: Reihenfolge der folgenden 2 Rule.Thm muss so bleiben, wegen
   267            (a+a)+a --> a + 2*a --> 3*a and not (a+a)+a --> 2*a + a *)
   268 	 Rule.Thm ("real_mult_2_assoc_r",ThmC.numerals_to_Free @{thm real_mult_2_assoc_r}),
   269 	 (*"(k + z1) + z1 = k + 2 * z1"*)
   270 	 Rule.Thm ("sym_real_mult_2",ThmC.numerals_to_Free (@{thm real_mult_2} RS @{thm sym})),
   271 	 (*"z1 + z1 = 2 * z1"*)
   272 
   273 	 Rule.Thm ("addiere_vor_minus",ThmC.numerals_to_Free @{thm addiere_vor_minus}),
   274 	 (*"[| l is_const; m is_const |] ==> -(l * v) +  m * v = (-l + m) *v"*)
   275 	 Rule.Thm ("addiere_eins_vor_minus",ThmC.numerals_to_Free @{thm addiere_eins_vor_minus}),
   276 	 (*"[| m is_const |] ==> -  v +  m * v = (-1 + m) * v"*)
   277 	 Rule.Thm ("subtrahiere_vor_minus",ThmC.numerals_to_Free @{thm subtrahiere_vor_minus}),
   278 	 (*"[| l is_const; m is_const |] ==> -(l * v) -  m * v = (-l - m) *v"*)
   279 	 Rule.Thm ("subtrahiere_eins_vor_minus",ThmC.numerals_to_Free @{thm subtrahiere_eins_vor_minus})
   280 	 (*"[| m is_const |] ==> -  v -  m * v = (-1 - m) * v"*)
   281 	 
   282 	 ], scr = Rule.Empty_Prog};
   283     
   284 val verschoenere = 
   285   Rule_Def.Repeat{id = "verschoenere", preconds = [], 
   286       rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), srls = Rule_Set.Empty, calc = [], errpatts = [],
   287       erls = Rule_Set.append_rules "erls_verschoenere" Rule_Set.empty 
   288 			[Rule.Eval ("PolyMinus.kleiner", eval_kleiner "")], 
   289       rules = [Rule.Thm ("vorzeichen_minus_weg1",ThmC.numerals_to_Free @{thm vorzeichen_minus_weg1}),
   290 	       (*"l kleiner 0 ==> a + l * b = a - -l * b"*)
   291 	       Rule.Thm ("vorzeichen_minus_weg2",ThmC.numerals_to_Free @{thm vorzeichen_minus_weg2}),
   292 	       (*"l kleiner 0 ==> a - l * b = a + -l * b"*)
   293 	       Rule.Thm ("vorzeichen_minus_weg3",ThmC.numerals_to_Free @{thm vorzeichen_minus_weg3}),
   294 	       (*"l kleiner 0 ==> k + a - l * b = k + a + -l * b"*)
   295 	       Rule.Thm ("vorzeichen_minus_weg4",ThmC.numerals_to_Free @{thm vorzeichen_minus_weg4}),
   296 	       (*"l kleiner 0 ==> k - a - l * b = k - a + -l * b"*)
   297 
   298 	       Rule.Eval ("Groups.times_class.times", (**)eval_binop "#mult_"),
   299 
   300 	       Rule.Thm ("mult_zero_left",ThmC.numerals_to_Free @{thm mult_zero_left}),    
   301 	       (*"0 * z = 0"*)
   302 	       Rule.Thm ("mult_1_left",ThmC.numerals_to_Free @{thm mult_1_left}),     
   303 	       (*"1 * z = z"*)
   304 	       Rule.Thm ("add_0_left",ThmC.numerals_to_Free @{thm add_0_left}),
   305 	       (*"0 + z = z"*)
   306 	       Rule.Thm ("null_minus",ThmC.numerals_to_Free @{thm null_minus}),
   307 	       (*"0 - a = -a"*)
   308 	       Rule.Thm ("vor_minus_mal",ThmC.numerals_to_Free @{thm vor_minus_mal})
   309 	       (*"- a * b = (-a) * b"*)
   310 
   311 	       (*Rule.Thm ("",ThmC.numerals_to_Free @{}),*)
   312 	       (**)
   313 	       ], scr = Rule.Empty_Prog} (*end verschoenere*);
   314 
   315 val klammern_aufloesen = 
   316   Rule_Def.Repeat{id = "klammern_aufloesen", preconds = [], 
   317       rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), srls = Rule_Set.Empty, calc = [], errpatts = [], erls = Rule_Set.Empty, 
   318       rules = [Rule.Thm ("sym_add.assoc",
   319                      ThmC.numerals_to_Free (@{thm add.assoc} RS @{thm sym})),
   320 	       (*"a + (b + c) = (a + b) + c"*)
   321 	       Rule.Thm ("klammer_plus_minus",ThmC.numerals_to_Free @{thm klammer_plus_minus}),
   322 	       (*"a + (b - c) = (a + b) - c"*)
   323 	       Rule.Thm ("klammer_minus_plus",ThmC.numerals_to_Free @{thm klammer_minus_plus}),
   324 	       (*"a - (b + c) = (a - b) - c"*)
   325 	       Rule.Thm ("klammer_minus_minus",ThmC.numerals_to_Free @{thm klammer_minus_minus})
   326 	       (*"a - (b - c) = (a - b) + c"*)
   327 	       ], scr = Rule.Empty_Prog};
   328 
   329 val klammern_ausmultiplizieren = 
   330   Rule_Def.Repeat{id = "klammern_ausmultiplizieren", preconds = [], 
   331       rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), srls = Rule_Set.Empty, calc = [], errpatts = [], erls = Rule_Set.Empty, 
   332       rules = [Rule.Thm ("distrib_right" ,ThmC.numerals_to_Free @{thm distrib_right}),
   333 	       (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
   334 	       Rule.Thm ("distrib_left",ThmC.numerals_to_Free @{thm distrib_left}),
   335 	       (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
   336 	       
   337 	       Rule.Thm ("klammer_mult_minus",ThmC.numerals_to_Free @{thm klammer_mult_minus}),
   338 	       (*"a * (b - c) = a * b - a * c"*)
   339 	       Rule.Thm ("klammer_minus_mult",ThmC.numerals_to_Free @{thm klammer_minus_mult})
   340 	       (*"(b - c) * a = b * a - c * a"*)
   341 
   342 	       (*Rule.Thm ("",ThmC.numerals_to_Free @{}),
   343 	       (*""*)*)
   344 	       ], scr = Rule.Empty_Prog};
   345 
   346 val ordne_monome = 
   347   Rule_Def.Repeat{id = "ordne_monome", preconds = [], 
   348       rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), srls = Rule_Set.Empty, calc = [], errpatts = [], 
   349       erls = Rule_Set.append_rules "erls_ordne_monome" Rule_Set.empty
   350 	       [Rule.Eval ("PolyMinus.kleiner", eval_kleiner ""),
   351 		Rule.Eval ("Prog_Expr.is_atom", Prog_Expr.eval_is_atom "")
   352 		], 
   353       rules = [Rule.Thm ("tausche_mal",ThmC.numerals_to_Free @{thm tausche_mal}),
   354 	       (*"[| b is_atom; a kleiner b  |] ==> (b * a) = (a * b)"*)
   355 	       Rule.Thm ("tausche_vor_mal",ThmC.numerals_to_Free @{thm tausche_vor_mal}),
   356 	       (*"[| b is_atom; a kleiner b  |] ==> (-b * a) = (-a * b)"*)
   357 	       Rule.Thm ("tausche_mal_mal",ThmC.numerals_to_Free @{thm tausche_mal_mal}),
   358 	       (*"[| c is_atom; b kleiner c  |] ==> (a * c * b) = (a * b *c)"*)
   359 	       Rule.Thm ("x_quadrat",ThmC.numerals_to_Free @{thm x_quadrat})
   360 	       (*"(x * a) * a = x * a \<up> 2"*)
   361 
   362 	       (*Rule.Thm ("",ThmC.numerals_to_Free @{}),
   363 	       (*""*)*)
   364 	       ], scr = Rule.Empty_Prog};
   365 
   366 
   367 val rls_p_33 = 
   368     Rule_Set.append_rules "rls_p_33" Rule_Set.empty
   369 	       [Rule.Rls_ ordne_alphabetisch,
   370 		Rule.Rls_ fasse_zusammen,
   371 		Rule.Rls_ verschoenere
   372 		];
   373 val rls_p_34 = 
   374     Rule_Set.append_rules "rls_p_34" Rule_Set.empty
   375 	       [Rule.Rls_ klammern_aufloesen,
   376 		Rule.Rls_ ordne_alphabetisch,
   377 		Rule.Rls_ fasse_zusammen,
   378 		Rule.Rls_ verschoenere
   379 		];
   380 val rechnen = 
   381     Rule_Set.append_rules "rechnen" Rule_Set.empty
   382 	       [Rule.Eval ("Groups.times_class.times", (**)eval_binop "#mult_"),
   383 		Rule.Eval ("Groups.plus_class.plus", (**)eval_binop "#add_"),
   384 		Rule.Eval ("Groups.minus_class.minus", (**)eval_binop "#subtr_")
   385 		];
   386 \<close>
   387 setup \<open>KEStore_Elems.add_rlss 
   388   [("ordne_alphabetisch", (Context.theory_name @{theory}, prep_rls' ordne_alphabetisch)), 
   389   ("fasse_zusammen", (Context.theory_name @{theory}, prep_rls' fasse_zusammen)), 
   390   ("verschoenere", (Context.theory_name @{theory}, prep_rls' verschoenere)), 
   391   ("ordne_monome", (Context.theory_name @{theory}, prep_rls' ordne_monome)), 
   392   ("klammern_aufloesen", (Context.theory_name @{theory}, prep_rls' klammern_aufloesen)), 
   393   ("klammern_ausmultiplizieren",
   394     (Context.theory_name @{theory}, prep_rls' klammern_ausmultiplizieren))]\<close>
   395 
   396 (** problems **)
   397 setup \<open>KEStore_Elems.add_pbts
   398   [(Problem.prep_input thy "pbl_vereinf_poly" [] Problem.id_empty
   399       (["polynom", "vereinfachen"], [], Rule_Set.Empty, NONE, [])),
   400     (Problem.prep_input thy "pbl_vereinf_poly_minus" [] Problem.id_empty
   401       (["plus_minus", "polynom", "vereinfachen"],
   402         [("#Given", ["Term t_t"]),
   403           ("#Where", ["t_t is_polyexp",
   404             "Not (matchsub (?a + (?b + ?c)) t_t | " ^
   405             "     matchsub (?a + (?b - ?c)) t_t | " ^
   406             "     matchsub (?a - (?b + ?c)) t_t | " ^
   407             "     matchsub (?a + (?b - ?c)) t_t )",
   408             "Not (matchsub (?a * (?b + ?c)) t_t | " ^
   409             "     matchsub (?a * (?b - ?c)) t_t | " ^
   410             "     matchsub ((?b + ?c) * ?a) t_t | " ^
   411             "     matchsub ((?b - ?c) * ?a) t_t )"]),
   412           ("#Find", ["normalform n_n"])],
   413         Rule_Set.append_rules "prls_pbl_vereinf_poly" Rule_Set.empty 
   414 	        [Rule.Eval ("Poly.is_polyexp", eval_is_polyexp ""),
   415 	          Rule.Eval ("Prog_Expr.matchsub", Prog_Expr.eval_matchsub ""),
   416 	          Rule.Thm ("or_true", ThmC.numerals_to_Free @{thm or_true}),
   417             (*"(?a | True) = True"*)
   418             Rule.Thm ("or_false", ThmC.numerals_to_Free @{thm or_false}),
   419             (*"(?a | False) = ?a"*)
   420             Rule.Thm ("not_true",ThmC.numerals_to_Free @{thm not_true}),
   421             (*"(~ True) = False"*)
   422             Rule.Thm ("not_false",ThmC.numerals_to_Free @{thm not_false})
   423             (*"(~ False) = True"*)], 
   424        SOME "Vereinfache t_t", [["simplification", "for_polynomials", "with_minus"]])),
   425     (Problem.prep_input thy "pbl_vereinf_poly_klammer" [] Problem.id_empty
   426       (["klammer", "polynom", "vereinfachen"],
   427         [("#Given" ,["Term t_t"]),
   428           ("#Where" ,["t_t is_polyexp",
   429             "Not (matchsub (?a * (?b + ?c)) t_t | " ^
   430             "     matchsub (?a * (?b - ?c)) t_t | " ^
   431             "     matchsub ((?b + ?c) * ?a) t_t | " ^
   432             "     matchsub ((?b - ?c) * ?a) t_t )"]),
   433           ("#Find"  ,["normalform n_n"])],
   434         Rule_Set.append_rules "prls_pbl_vereinf_poly_klammer" Rule_Set.empty
   435           [Rule.Eval ("Poly.is_polyexp", eval_is_polyexp ""),
   436 	           Rule.Eval ("Prog_Expr.matchsub", Prog_Expr.eval_matchsub ""),
   437              Rule.Thm ("or_true", ThmC.numerals_to_Free @{thm or_true}),
   438              (*"(?a | True) = True"*)
   439              Rule.Thm ("or_false", ThmC.numerals_to_Free @{thm or_false}),
   440              (*"(?a | False) = ?a"*)
   441              Rule.Thm ("not_true",ThmC.numerals_to_Free @{thm not_true}),
   442              (*"(~ True) = False"*)
   443              Rule.Thm ("not_false",ThmC.numerals_to_Free @{thm not_false})
   444              (*"(~ False) = True"*)], 
   445         SOME "Vereinfache t_t", 
   446         [["simplification", "for_polynomials", "with_parentheses"]])),
   447     (Problem.prep_input thy "pbl_vereinf_poly_klammer_mal" [] Problem.id_empty
   448       (["binom_klammer", "polynom", "vereinfachen"],
   449         [("#Given", ["Term t_t"]),
   450           ("#Where", ["t_t is_polyexp"]),
   451           ("#Find", ["normalform n_n"])],
   452         Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)
   453 			      Rule.Eval ("Poly.is_polyexp", eval_is_polyexp "")], 
   454         SOME "Vereinfache t_t", 
   455         [["simplification", "for_polynomials", "with_parentheses_mult"]])),
   456     (Problem.prep_input thy "pbl_probe" [] Problem.id_empty (["probe"], [], Rule_Set.Empty, NONE, [])),
   457     (Problem.prep_input thy "pbl_probe_poly" [] Problem.id_empty
   458       (["polynom", "probe"],
   459         [("#Given", ["Pruefe e_e", "mitWert w_w"]),
   460           ("#Where", ["e_e is_polyexp"]),
   461           ("#Find", ["Geprueft p_p"])],
   462         Rule_Set.append_rules "prls_pbl_probe_poly" Rule_Set.empty [(*for preds in where_*)
   463 		      Rule.Eval ("Poly.is_polyexp", eval_is_polyexp "")], 
   464         SOME "Probe e_e w_w", 
   465         [["probe", "fuer_polynom"]])),
   466     (Problem.prep_input thy "pbl_probe_bruch" [] Problem.id_empty
   467       (["bruch", "probe"],
   468         [("#Given" ,["Pruefe e_e", "mitWert w_w"]),
   469           ("#Where" ,["e_e is_ratpolyexp"]),
   470           ("#Find"  ,["Geprueft p_p"])],
   471         Rule_Set.append_rules "prls_pbl_probe_bruch" Rule_Set.empty [(*for preds in where_*)
   472 		      Rule.Eval ("Rational.is_ratpolyexp", eval_is_ratpolyexp "")], 
   473         SOME "Probe e_e w_w", [["probe", "fuer_bruch"]]))]\<close>
   474 
   475 (** methods **)
   476 
   477 partial_function (tailrec) simplify :: "real \<Rightarrow> real"
   478   where
   479 "simplify t_t = (
   480   (Repeat(
   481     (Try (Rewrite_Set ''ordne_alphabetisch'')) #>
   482     (Try (Rewrite_Set ''fasse_zusammen'')) #>
   483     (Try (Rewrite_Set ''verschoenere'')))
   484   ) t_t)"
   485 setup \<open>KEStore_Elems.add_mets
   486     [MethodC.prep_input thy "met_simp_poly_minus" [] MethodC.id_empty
   487 	    (["simplification", "for_polynomials", "with_minus"],
   488 	      [("#Given" ,["Term t_t"]),
   489 	        ("#Where" ,["t_t is_polyexp",
   490 	            "Not (matchsub (?a + (?b + ?c)) t_t | " ^
   491 	            "     matchsub (?a + (?b - ?c)) t_t | " ^
   492 	            "     matchsub (?a - (?b + ?c)) t_t | " ^
   493 	            "     matchsub (?a + (?b - ?c)) t_t )"]),
   494 	        ("#Find"  ,["normalform n_n"])],
   495 	      {rew_ord'="tless_true", rls' = Rule_Set.empty, calc = [], srls = Rule_Set.empty,
   496 	        prls = Rule_Set.append_rules "prls_met_simp_poly_minus" Rule_Set.empty 
   497 				      [Rule.Eval ("Poly.is_polyexp", eval_is_polyexp ""),
   498 				        Rule.Eval ("Prog_Expr.matchsub", Prog_Expr.eval_matchsub ""),
   499 				        Rule.Thm ("and_true",ThmC.numerals_to_Free @{thm and_true}),
   500                 (*"(?a & True) = ?a"*)
   501                 Rule.Thm ("and_false",ThmC.numerals_to_Free @{thm and_false}),
   502                 (*"(?a & False) = False"*)
   503                 Rule.Thm ("not_true",ThmC.numerals_to_Free @{thm not_true}),
   504                 (*"(~ True) = False"*)
   505                 Rule.Thm ("not_false",ThmC.numerals_to_Free @{thm not_false})
   506                 (*"(~ False) = True"*)],
   507           crls = Rule_Set.empty, errpats = [], nrls = rls_p_33},
   508           @{thm simplify.simps})]
   509 \<close>
   510 
   511 partial_function (tailrec) simplify2 :: "real \<Rightarrow> real"
   512   where
   513 "simplify2 t_t = (
   514   (Repeat(
   515     (Try (Rewrite_Set ''klammern_aufloesen'')) #>
   516     (Try (Rewrite_Set ''ordne_alphabetisch'')) #>
   517     (Try (Rewrite_Set ''fasse_zusammen'')) #>
   518     (Try (Rewrite_Set ''verschoenere'')))
   519   ) t_t)"
   520 setup \<open>KEStore_Elems.add_mets
   521     [MethodC.prep_input thy "met_simp_poly_parenth" [] MethodC.id_empty
   522 	    (["simplification", "for_polynomials", "with_parentheses"],
   523 	      [("#Given" ,["Term t_t"]),
   524 	        ("#Where" ,["t_t is_polyexp"]),
   525 	        ("#Find"  ,["normalform n_n"])],
   526 	      {rew_ord'="tless_true", rls' = Rule_Set.empty, calc = [], srls = Rule_Set.empty, 
   527 	        prls = Rule_Set.append_rules "simplification_for_polynomials_prls" Rule_Set.empty 
   528 				    [(*for preds in where_*) Rule.Eval("Poly.is_polyexp", eval_is_polyexp"")],
   529 				  crls = Rule_Set.empty, errpats = [], nrls = rls_p_34},
   530 				@{thm simplify2.simps})]
   531 \<close>
   532 
   533 partial_function (tailrec) simplify3 :: "real \<Rightarrow> real"
   534   where
   535 "simplify3 t_t = (
   536   (Repeat(
   537     (Try (Rewrite_Set ''klammern_ausmultiplizieren'')) #>
   538     (Try (Rewrite_Set ''discard_parentheses'')) #>
   539     (Try (Rewrite_Set ''ordne_monome'')) #>
   540     (Try (Rewrite_Set ''klammern_aufloesen'')) #>
   541     (Try (Rewrite_Set ''ordne_alphabetisch'')) #>
   542     (Try (Rewrite_Set ''fasse_zusammen'')) #>
   543     (Try (Rewrite_Set ''verschoenere'')))
   544   ) t_t)"
   545 setup \<open>KEStore_Elems.add_mets
   546     [MethodC.prep_input thy "met_simp_poly_parenth_mult" [] MethodC.id_empty
   547 	    (["simplification", "for_polynomials", "with_parentheses_mult"],
   548 	      [("#Given" ,["Term t_t"]), ("#Where" ,["t_t is_polyexp"]), ("#Find"  ,["normalform n_n"])],
   549 	        {rew_ord'="tless_true", rls' = Rule_Set.empty, calc = [], srls = Rule_Set.empty, 
   550 	          prls = Rule_Set.append_rules "simplification_for_polynomials_prls" Rule_Set.empty 
   551 				      [(*for preds in where_*) Rule.Eval("Poly.is_polyexp", eval_is_polyexp"")],
   552 				    crls = Rule_Set.empty, errpats = [], nrls = rls_p_34},
   553 				  @{thm simplify3.simps})]
   554 \<close>
   555 setup \<open>KEStore_Elems.add_mets
   556     [MethodC.prep_input thy "met_probe" [] MethodC.id_empty
   557 	    (["probe"], [],
   558 	      {rew_ord'="tless_true", rls' = Rule_Set.empty, calc = [], srls = Rule_Set.empty, prls = Rule_Set.Empty, crls = Rule_Set.empty,
   559 	        errpats = [], nrls = Rule_Set.Empty}, 
   560 	      @{thm refl})]
   561 \<close>
   562 
   563 partial_function (tailrec) mache_probe :: "bool \<Rightarrow> bool list \<Rightarrow> bool"
   564   where
   565 "mache_probe e_e w_w = (
   566   let
   567      e_e = Take e_e;
   568      e_e = Substitute w_w e_e
   569   in (
   570     Repeat (
   571       (Try (Repeat (Calculate ''TIMES''))) #>
   572       (Try (Repeat (Calculate ''PLUS'' ))) #>
   573       (Try (Repeat (Calculate ''MINUS''))))
   574     ) e_e)"
   575 setup \<open>KEStore_Elems.add_mets
   576     [MethodC.prep_input thy "met_probe_poly" [] MethodC.id_empty
   577 	    (["probe", "fuer_polynom"],
   578 	      [("#Given" ,["Pruefe e_e", "mitWert w_w"]),
   579 	        ("#Where" ,["e_e is_polyexp"]),
   580 	        ("#Find"  ,["Geprueft p_p"])],
   581 	      {rew_ord'="tless_true", rls' = Rule_Set.empty, calc = [], srls = Rule_Set.empty, 
   582 	        prls = Rule_Set.append_rules "prls_met_probe_bruch" Rule_Set.empty
   583 	            [(*for preds in where_*) Rule.Eval ("Rational.is_ratpolyexp", eval_is_ratpolyexp "")], 
   584 	        crls = Rule_Set.empty, errpats = [], nrls = rechnen}, 
   585 	      @{thm mache_probe.simps})]
   586 \<close>
   587 setup \<open>KEStore_Elems.add_mets
   588     [MethodC.prep_input thy "met_probe_bruch" [] MethodC.id_empty
   589 	    (["probe", "fuer_bruch"],
   590 	      [("#Given" ,["Pruefe e_e", "mitWert w_w"]),
   591 	        ("#Where" ,["e_e is_ratpolyexp"]),
   592 	        ("#Find"  ,["Geprueft p_p"])],
   593 	      {rew_ord'="tless_true", rls' = Rule_Set.empty, calc = [], srls = Rule_Set.empty, 
   594 	        prls = Rule_Set.append_rules "prls_met_probe_bruch" Rule_Set.empty
   595 	            [(*for preds in where_*) Rule.Eval ("Rational.is_ratpolyexp", eval_is_ratpolyexp "")], 
   596 	        crls = Rule_Set.empty, errpats = [], nrls = Rule_Set.Empty}, 
   597 	      @{thm refl})]
   598 \<close> ML \<open>
   599 \<close> ML \<open>
   600 \<close>
   601 
   602 end
   603