src/Tools/isac/Knowledge/EqSystem.thy
author wenzelm
Sat, 12 Jun 2021 14:29:10 +0200
changeset 60296 81b6519da42b
parent 60294 6623f5cdcb19
child 60297 73e7175a7d3f
permissions -rw-r--r--
ML antiquotations for Rule.Thm, with special treatment of symmetric rule;
     1 (* equational systems, minimal -- for use in Biegelinie
     2    author: Walther Neuper
     3    050826,
     4    (c) due to copyright terms
     5 *)
     6 
     7 theory EqSystem imports Integrate Rational Root begin
     8 
     9 consts
    10 
    11   occur_exactly_in :: 
    12    "[real list, real list, 'a] => bool" ("_ from _ occur'_exactly'_in _")
    13 
    14   (*descriptions in the related problems*)
    15   solveForVars       :: "real list => toreall"
    16   solution           :: "bool list => toreall"
    17 
    18   (*the CAS-command, eg. "solveSystem [x+y=1,y=2] [x,y]"*)
    19   solveSystem        :: "[bool list, real list] => bool list"
    20 
    21 axiomatization where
    22 (*stated as axioms, todo: prove as theorems
    23   'bdv' is a constant handled on the meta-level 
    24    specifically as a 'bound variable'            *)
    25 
    26   commute_0_equality:  "(0 = a) = (a = 0)" and
    27 
    28   (*WN0510 see simliar rules 'isolate_' 'separate_' (by RL)
    29     [bdv_1,bdv_2,bdv_3,bdv_4] work also for 2 and 3 bdvs, ugly !*)
    30   separate_bdvs_add:   
    31     "[| [] from [bdv_1,bdv_2,bdv_3,bdv_4] occur_exactly_in a |] 
    32 		      			     ==> (a + b = c) = (b = c + -1*a)" and
    33   separate_bdvs0:
    34     "[| some_of [bdv_1,bdv_2,bdv_3,bdv_4] occur_in b; Not (b=!=0)  |] 
    35 		      			     ==> (a = b) = (a + -1*b = 0)" and
    36   separate_bdvs_add1:  
    37     "[| some_of [bdv_1,bdv_2,bdv_3,bdv_4] occur_in c |] 
    38 		      			     ==> (a = b + c) = (a + -1*c = b)" and
    39   separate_bdvs_add2:
    40     "[| Not (some_of [bdv_1,bdv_2,bdv_3,bdv_4] occur_in a) |] 
    41 		      			     ==> (a + b = c) = (b = -1*a + c)" and
    42   separate_bdvs_mult:  
    43     "[| [] from [bdv_1,bdv_2,bdv_3,bdv_4] occur_exactly_in a; Not (a=!=0) |] 
    44 		      			     ==>(a * b = c) = (b = c / a)" 
    45 axiomatization where (*..if replaced by "and" we get an error in 
    46   ---  rewrite in [EqSystem,normalise,2x2] --- step "--- 3---";*)
    47   order_system_NxN:     "[a,b] = [b,a]"
    48   (*requires rew_ord for termination, eg. ord_simplify_Integral;
    49     works for lists of any length, interestingly !?!*)
    50 
    51 ML \<open>
    52 (** eval functions **)
    53 
    54 (*certain variables of a given list occur _all_ in a term
    55   args: all: ..variables, which are under consideration (eg. the bound vars)
    56         vs:  variables which must be in t, 
    57              and none of the others in all must be in t
    58         t: the term under consideration
    59  *)
    60 fun occur_exactly_in vs all t =
    61     let fun occurs_in' a b = Prog_Expr.occurs_in b a
    62     in foldl and_ (true, map (occurs_in' t) vs)
    63        andalso not (foldl or_ (false, map (occurs_in' t) 
    64                                           (subtract op = vs all)))
    65     end;
    66 
    67 (*("occur_exactly_in", ("EqSystem.occur_exactly_in", 
    68 			eval_occur_exactly_in "#eval_occur_exactly_in_"))*)
    69 fun eval_occur_exactly_in _ "EqSystem.occur_exactly_in"
    70 			  (p as (Const ("EqSystem.occur_exactly_in",_) 
    71 				       $ vs $ all $ t)) _ =
    72     if occur_exactly_in (TermC.isalist2list vs) (TermC.isalist2list all) t
    73     then SOME ((UnparseC.term p) ^ " = True",
    74 	       HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
    75     else SOME ((UnparseC.term p) ^ " = False",
    76 	       HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
    77   | eval_occur_exactly_in _ _ _ _ = NONE;
    78 \<close>
    79 setup \<open>KEStore_Elems.add_calcs
    80   [("occur_exactly_in",
    81 	    ("EqSystem.occur_exactly_in",
    82 	      eval_occur_exactly_in "#eval_occur_exactly_in_"))]\<close>
    83 ML \<open>
    84 (** rewrite order 'ord_simplify_System' **)
    85 
    86 (* order wrt. several linear (i.e. without exponents) variables "c", "c_2",..
    87    which leaves the monomials containing c, c_2,... at the end of an Integral
    88    and puts the c, c_2,... rightmost within a monomial.
    89 
    90    WN050906 this is a quick and dirty adaption of ord_make_polynomial_in,
    91    which was most adequate, because it uses size_of_term*)
    92 (**)
    93 local (*. for simplify_System .*)
    94 (**)
    95 open Term;  (* for type order = EQUAL | LESS | GREATER *)
    96 
    97 fun pr_ord EQUAL = "EQUAL"
    98   | pr_ord LESS  = "LESS"
    99   | pr_ord GREATER = "GREATER";
   100 
   101 fun dest_hd' (Const (a, T)) = (((a, 0), T), 0)
   102   | dest_hd' (Free (ccc, T)) =
   103     (case Symbol.explode ccc of
   104 	"c"::[] => ((("|||||||||||||||||||||", 0), T), 1)(*greatest string WN*)
   105       | "c"::"_"::_ => ((("|||||||||||||||||||||", 0), T), 1)
   106       | _ => (((ccc, 0), T), 1))
   107   | dest_hd' (Var v) = (v, 2)
   108   | dest_hd' (Bound i) = ((("", i), dummyT), 3)
   109   | dest_hd' (Abs (_, T, _)) = ((("", 0), T), 4)
   110   | dest_hd' _ = raise ERROR "dest_hd': uncovered case in fun.def.";
   111 
   112 fun size_of_term' (Free (ccc, _)) =
   113     (case Symbol.explode ccc of (*WN0510 hack for the bound variables*)
   114 	"c"::[] => 1000
   115       | "c"::"_"::is => 1000 * ((TermC.int_of_str o implode) is)
   116       | _ => 1)
   117   | size_of_term' (Abs (_,_,body)) = 1 + size_of_term' body
   118   | size_of_term' (f$t) = size_of_term' f  +  size_of_term' t
   119   | size_of_term' _ = 1;
   120 
   121 fun term_ord' pr thy (Abs (_, T, t), Abs(_, U, u)) =       (* ~ term.ML *)
   122     (case term_ord' pr thy (t, u) of EQUAL => Term_Ord.typ_ord (T, U) | ord => ord)
   123   | term_ord' pr thy (t, u) =
   124     (if pr
   125      then 
   126        let
   127          val (f, ts) = strip_comb t and (g, us) = strip_comb u;
   128          val _ = tracing ("t= f@ts= \"" ^ UnparseC.term_in_thy thy f ^ "\" @ \"[" ^
   129            commas (map (UnparseC.term_in_thy thy) ts) ^ "]\"");
   130          val _ = tracing ("u= g@us= \"" ^ UnparseC.term_in_thy thy g ^ "\" @ \"[" ^
   131            commas (map (UnparseC.term_in_thy thy) us) ^ "]\"");
   132          val _ = tracing ("size_of_term(t,u)= (" ^ string_of_int (size_of_term' t) ^ ", " ^
   133            string_of_int (size_of_term' u) ^ ")");
   134          val _ = tracing ("hd_ord(f,g)      = " ^ ((pr_ord o hd_ord) (f,g)));
   135          val _ = tracing ("terms_ord (ts,us) = " ^(pr_ord o terms_ord str false) (ts,us));
   136          val _=tracing("-------");
   137        in () end
   138      else ();
   139     case int_ord (size_of_term' t, size_of_term' u) of
   140       EQUAL =>
   141         let val (f, ts) = strip_comb t and (g, us) = strip_comb u 
   142         in (case hd_ord (f, g) of 
   143               EQUAL => (terms_ord str pr) (ts, us) 
   144             | ord => ord)
   145         end
   146 	 | ord => ord)
   147 and hd_ord (f, g) =                                        (* ~ term.ML *)
   148   prod_ord (prod_ord Term_Ord.indexname_ord Term_Ord.typ_ord) int_ord (dest_hd' f, dest_hd' g)
   149 and terms_ord _ pr (ts, us) = list_ord (term_ord' pr (ThyC.get_theory "Isac_Knowledge"))(ts, us);
   150 (**)
   151 in
   152 (**)
   153 (*WN0510 for preliminary use in eval_order_system, see case-study mat-eng.tex
   154 fun ord_simplify_System_rev (pr:bool) thy subst tu = 
   155     (term_ord' pr thy (Library.swap tu) = LESS);*)
   156 
   157 (*for the rls's*)
   158 fun ord_simplify_System (pr:bool) thy _(*subst*) tu = 
   159     (term_ord' pr thy tu = LESS);
   160 (**)
   161 end;
   162 (**)
   163 Rewrite_Ord.rew_ord' := overwritel (! Rewrite_Ord.rew_ord',
   164 [("ord_simplify_System", ord_simplify_System false \<^theory>)
   165  ]);
   166 \<close>
   167 ML \<open>
   168 (** rulesets **)
   169 
   170 (*.adapted from 'order_add_mult_in' by just replacing the rew_ord.*)
   171 val order_add_mult_System = 
   172   Rule_Def.Repeat{id = "order_add_mult_System", preconds = [], 
   173       rew_ord = ("ord_simplify_System",
   174 		 ord_simplify_System false @{theory "Integrate"}),
   175       erls = Rule_Set.empty,srls = Rule_Set.Empty, calc = [], errpatts = [],
   176       rules = [Rule.Thm ("mult.commute",ThmC.numerals_to_Free @{thm mult.commute}),
   177 	       (* z * w = w * z *)
   178 	       Rule.Thm ("real_mult_left_commute",ThmC.numerals_to_Free @{thm real_mult_left_commute}),
   179 	       (*z1.0 * (z2.0 * z3.0) = z2.0 * (z1.0 * z3.0)*)
   180 	       Rule.Thm ("mult.assoc",ThmC.numerals_to_Free @{thm mult.assoc}),		
   181 	       (*z1.0 * z2.0 * z3.0 = z1.0 * (z2.0 * z3.0)*)
   182 	       Rule.Thm ("add.commute",ThmC.numerals_to_Free @{thm add.commute}),	
   183 	       (*z + w = w + z*)
   184 	       Rule.Thm ("add.left_commute",ThmC.numerals_to_Free @{thm add.left_commute}),
   185 	       (*x + (y + z) = y + (x + z)*)
   186 	       Rule.Thm ("add.assoc",ThmC.numerals_to_Free @{thm add.assoc})	               
   187 	       (*z1.0 + z2.0 + z3.0 = z1.0 + (z2.0 + z3.0)*)
   188 	       ], 
   189       scr = Rule.Empty_Prog};
   190 \<close>
   191 ML \<open>
   192 (*.adapted from 'norm_Rational' by
   193   #1 using 'ord_simplify_System' in 'order_add_mult_System'
   194   #2 NOT using common_nominator_p                          .*)
   195 val norm_System_noadd_fractions = 
   196   Rule_Def.Repeat {id = "norm_System_noadd_fractions", preconds = [], 
   197        rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord), 
   198        erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
   199        rules = [(*sequence given by operator precedence*)
   200 		Rule.Rls_ discard_minus,
   201 		Rule.Rls_ powers,
   202 		Rule.Rls_ rat_mult_divide,
   203 		Rule.Rls_ expand,
   204 		Rule.Rls_ reduce_0_1_2,
   205 		Rule.Rls_ (*order_add_mult #1*) order_add_mult_System,
   206 		Rule.Rls_ collect_numerals,
   207 		(*Rule.Rls_ add_fractions_p, #2*)
   208 		Rule.Rls_ cancel_p
   209 		],
   210        scr = Rule.Empty_Prog
   211        };
   212 \<close>
   213 ML \<open>
   214 (*.adapted from 'norm_Rational' by
   215   *1* using 'ord_simplify_System' in 'order_add_mult_System'.*)
   216 val norm_System = 
   217   Rule_Def.Repeat {id = "norm_System", preconds = [], 
   218        rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord), 
   219        erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
   220        rules = [(*sequence given by operator precedence*)
   221 		Rule.Rls_ discard_minus,
   222 		Rule.Rls_ powers,
   223 		Rule.Rls_ rat_mult_divide,
   224 		Rule.Rls_ expand,
   225 		Rule.Rls_ reduce_0_1_2,
   226 		Rule.Rls_ (*order_add_mult *1*) order_add_mult_System,
   227 		Rule.Rls_ collect_numerals,
   228 		Rule.Rls_ add_fractions_p,
   229 		Rule.Rls_ cancel_p
   230 		],
   231        scr = Rule.Empty_Prog
   232        };
   233 \<close>
   234 ML \<open>
   235 (*.simplify an equational system BEFORE solving it such that parentheses are
   236    ( ((u0*v0)*w0) + ( ((u1*v1)*w1) * c + ... +((u4*v4)*w4) * c_4 ) )
   237 ATTENTION: works ONLY for bound variables c, c_1, c_2, c_3, c_4 :ATTENTION
   238    This is a copy from 'make_ratpoly_in' with respective reductions:
   239    *0* expand the term, ie. distribute * and / over +
   240    *1* ord_simplify_System instead of termlessI
   241    *2* no add_fractions_p (= common_nominator_p_rls !)
   242    *3* discard_parentheses only for (.*(.*.))
   243    analoguous to simplify_Integral                                       .*)
   244 val simplify_System_parenthesized = 
   245   Rule_Set.Sequence {id = "simplify_System_parenthesized", preconds = []:term list, 
   246        rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
   247       erls = Atools_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
   248       rules = [Rule.Thm ("distrib_right",ThmC.numerals_to_Free @{thm distrib_right}),
   249  	       (*"(?z1.0 + ?z2.0) * ?w = ?z1.0 * ?w + ?z2.0 * ?w"*)
   250 	       Rule.Thm ("add_divide_distrib",ThmC.numerals_to_Free @{thm add_divide_distrib}),
   251  	       (*"(?x + ?y) / ?z = ?x / ?z + ?y / ?z"*)
   252 	       (*^^^^^ *0* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*)
   253 	       Rule.Rls_ norm_Rational_noadd_fractions(**2**),
   254 	       Rule.Rls_ (*order_add_mult_in*) norm_System_noadd_fractions (**1**),
   255 	       \<^rule_thm_sym>\<open>mult.assoc\<close>
   256 	       (*Rule.Rls_ discard_parentheses *3**),
   257 	       Rule.Rls_ collect_bdv, (*from make_polynomial_in WN051031 welldone?*)
   258 	       Rule.Rls_ separate_bdv2,
   259 	       \<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e")
   260 	       ],
   261       scr = Rule.Empty_Prog};      
   262 \<close>
   263 ML \<open>
   264 (*.simplify an equational system AFTER solving it;
   265    This is a copy of 'make_ratpoly_in' with the differences
   266    *1* ord_simplify_System instead of termlessI           .*)
   267 (*TODO.WN051031 ^^^^^^^^^^ should be in EACH rls contained *)
   268 val simplify_System = 
   269   Rule_Set.Sequence {id = "simplify_System", preconds = []:term list, 
   270        rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
   271       erls = Atools_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
   272       rules = [Rule.Rls_ norm_Rational,
   273 	       Rule.Rls_ (*order_add_mult_in*) norm_System (**1**),
   274 	       Rule.Rls_ discard_parentheses,
   275 	       Rule.Rls_ collect_bdv, (*from make_polynomial_in WN051031 welldone?*)
   276 	       Rule.Rls_ separate_bdv2,
   277 	       \<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e")
   278 	       ],
   279       scr = Rule.Empty_Prog};      
   280 (*
   281 val simplify_System = 
   282     Rule_Set.append_rules "simplify_System" simplify_System_parenthesized
   283 	       [\<^rule_thm_sym>\<open>add.assoc\<close>];
   284 *)
   285 \<close>
   286 ML \<open>
   287 val isolate_bdvs = 
   288     Rule_Def.Repeat {id="isolate_bdvs", preconds = [], 
   289 	 rew_ord = ("e_rew_ord", Rewrite_Ord.e_rew_ord), 
   290 	 erls = Rule_Set.append_rules "erls_isolate_bdvs" Rule_Set.empty 
   291 			   [(\<^rule_eval>\<open>occur_exactly_in\<close> (eval_occur_exactly_in "#eval_occur_exactly_in_"))], 
   292 			   srls = Rule_Set.Empty, calc = [], errpatts = [],
   293 	      rules = 
   294              [Rule.Thm ("commute_0_equality", ThmC.numerals_to_Free @{thm commute_0_equality}),
   295 	      Rule.Thm ("separate_bdvs_add", ThmC.numerals_to_Free @{thm separate_bdvs_add}),
   296 	      Rule.Thm ("separate_bdvs_mult", ThmC.numerals_to_Free @{thm separate_bdvs_mult})],
   297 	      scr = Rule.Empty_Prog};
   298 \<close>
   299 ML \<open>
   300 val isolate_bdvs_4x4 = 
   301     Rule_Def.Repeat {id="isolate_bdvs_4x4", preconds = [], 
   302 	 rew_ord = ("e_rew_ord", Rewrite_Ord.e_rew_ord), 
   303 	 erls = Rule_Set.append_rules 
   304 		    "erls_isolate_bdvs_4x4" Rule_Set.empty 
   305 		    [\<^rule_eval>\<open>occur_exactly_in\<close> (eval_occur_exactly_in "#eval_occur_exactly_in_"),
   306 		     \<^rule_eval>\<open>Prog_Expr.ident\<close> (Prog_Expr.eval_ident "#ident_"),
   307 		     \<^rule_eval>\<open>Prog_Expr.some_occur_in\<close> (Prog_Expr.eval_some_occur_in "#some_occur_in_"),
   308          Rule.Thm ("not_true",ThmC.numerals_to_Free @{thm not_true}),
   309 		     Rule.Thm ("not_false",ThmC.numerals_to_Free @{thm not_false})
   310 			    ], 
   311 	 srls = Rule_Set.Empty, calc = [], errpatts = [],
   312 	 rules = [Rule.Thm ("commute_0_equality", ThmC.numerals_to_Free @{thm commute_0_equality}),
   313 		  Rule.Thm ("separate_bdvs0", ThmC.numerals_to_Free @{thm separate_bdvs0}),
   314 		  Rule.Thm ("separate_bdvs_add1", ThmC.numerals_to_Free @{thm separate_bdvs_add1}),
   315 		  Rule.Thm ("separate_bdvs_add1", ThmC.numerals_to_Free @{thm separate_bdvs_add2}),
   316 		  Rule.Thm ("separate_bdvs_mult", ThmC.numerals_to_Free @{thm separate_bdvs_mult})
   317                  ], scr = Rule.Empty_Prog};
   318 
   319 \<close>
   320 ML \<open>
   321 
   322 (*.order the equations in a system such, that a triangular system (if any)
   323    appears as [..c_4 = .., ..., ..., ..c_1 + ..c_2 + ..c_3 ..c_4 = ..].*)
   324 val order_system = 
   325     Rule_Def.Repeat {id="order_system", preconds = [], 
   326 	 rew_ord = ("ord_simplify_System", 
   327 		    ord_simplify_System false \<^theory>), 
   328 	 erls = Rule_Set.Empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
   329 	 rules = [Rule.Thm ("order_system_NxN", ThmC.numerals_to_Free @{thm order_system_NxN})
   330 		  ],
   331 	 scr = Rule.Empty_Prog};
   332 
   333 val prls_triangular = 
   334     Rule_Def.Repeat {id="prls_triangular", preconds = [], 
   335 	 rew_ord = ("e_rew_ord", Rewrite_Ord.e_rew_ord), 
   336 	 erls = Rule_Def.Repeat {id="erls_prls_triangular", preconds = [], 
   337 		     rew_ord = ("e_rew_ord", Rewrite_Ord.e_rew_ord), 
   338 		     erls = Rule_Set.Empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
   339 		     rules = [(*for precond NTH_CONS ...*)
   340 			      \<^rule_eval>\<open>less\<close> (Prog_Expr.eval_equ "#less_"),
   341 			      \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")
   342 			      (*immediately repeated rewrite pushes
   343 					    '+' into precondition !*)
   344 			      ],
   345 		     scr = Rule.Empty_Prog}, 
   346 	 srls = Rule_Set.Empty, calc = [], errpatts = [],
   347 	 rules = [Rule.Thm ("NTH_CONS",ThmC.numerals_to_Free @{thm NTH_CONS}),
   348 		  \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   349 		  Rule.Thm ("NTH_NIL",ThmC.numerals_to_Free @{thm NTH_NIL}),
   350 		  Rule.Thm ("tl_Cons",ThmC.numerals_to_Free @{thm tl_Cons}),
   351 		  Rule.Thm ("tl_Nil",ThmC.numerals_to_Free @{thm tl_Nil}),
   352 		  \<^rule_eval>\<open>occur_exactly_in\<close> (eval_occur_exactly_in "#eval_occur_exactly_in_")
   353 		  ],
   354 	 scr = Rule.Empty_Prog};
   355 \<close>
   356 ML \<open>
   357 
   358 (*WN060914 quickly created for 4x4; 
   359  more similarity to prls_triangular desirable*)
   360 val prls_triangular4 = 
   361     Rule_Def.Repeat {id="prls_triangular4", preconds = [], 
   362 	 rew_ord = ("e_rew_ord", Rewrite_Ord.e_rew_ord), 
   363 	 erls = Rule_Def.Repeat {id="erls_prls_triangular4", preconds = [], 
   364 		     rew_ord = ("e_rew_ord", Rewrite_Ord.e_rew_ord), 
   365 		     erls = Rule_Set.Empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
   366 		     rules = [(*for precond NTH_CONS ...*)
   367 			      \<^rule_eval>\<open>less\<close> (Prog_Expr.eval_equ "#less_"),
   368 			      \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")
   369 			      (*immediately repeated rewrite pushes
   370 					    '+' into precondition !*)
   371 			      ],
   372 		     scr = Rule.Empty_Prog}, 
   373 	 srls = Rule_Set.Empty, calc = [], errpatts = [],
   374 	 rules = [Rule.Thm ("NTH_CONS",ThmC.numerals_to_Free @{thm NTH_CONS}),
   375 		  \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   376 		  Rule.Thm ("NTH_NIL",ThmC.numerals_to_Free @{thm NTH_NIL}),
   377 		  Rule.Thm ("tl_Cons",ThmC.numerals_to_Free @{thm tl_Cons}),
   378 		  Rule.Thm ("tl_Nil",ThmC.numerals_to_Free @{thm tl_Nil}),
   379 		  \<^rule_eval>\<open>occur_exactly_in\<close> (eval_occur_exactly_in "#eval_occur_exactly_in_")
   380 		  ],
   381 	 scr = Rule.Empty_Prog};
   382 \<close>
   383 
   384 rule_set_knowledge
   385   simplify_System_parenthesized = \<open>prep_rls' simplify_System_parenthesized\<close> and
   386   simplify_System = \<open>prep_rls' simplify_System\<close> and
   387   isolate_bdvs = \<open>prep_rls' isolate_bdvs\<close> and
   388   isolate_bdvs_4x4 = \<open>prep_rls' isolate_bdvs_4x4\<close> and 
   389   order_system = \<open>prep_rls' order_system\<close> and 
   390   order_add_mult_System = \<open>prep_rls' order_add_mult_System\<close> and
   391   norm_System_noadd_fractions = \<open>prep_rls' norm_System_noadd_fractions\<close> and
   392   norm_System = \<open>prep_rls' norm_System\<close>
   393 
   394 
   395 section \<open>Problems\<close>
   396 
   397 setup \<open>KEStore_Elems.add_pbts
   398   [(Problem.prep_input @{theory} "pbl_equsys" [] Problem.id_empty
   399       (["system"],
   400         [("#Given" ,["equalities e_s", "solveForVars v_s"]),
   401           ("#Find"  ,["solution ss'''"](*''' is copy-named*))],
   402         Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)], SOME "solveSystem e_s v_s", [])),
   403     (Problem.prep_input @{theory} "pbl_equsys_lin" [] Problem.id_empty
   404       (["LINEAR", "system"],
   405         [("#Given" ,["equalities e_s", "solveForVars v_s"]),
   406           (*TODO.WN050929 check linearity*)
   407           ("#Find"  ,["solution ss'''"])],
   408         Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)], SOME "solveSystem e_s v_s", [])),
   409     (Problem.prep_input @{theory} "pbl_equsys_lin_2x2" [] Problem.id_empty
   410       (["2x2", "LINEAR", "system"],
   411       (*~~~~~~~~~~~~~~~~~~~~~~~~~*)
   412         [("#Given" ,["equalities e_s", "solveForVars v_s"]),
   413           ("#Where"  ,["Length (e_s:: bool list) = 2", "Length v_s = 2"]),
   414           ("#Find"  ,["solution ss'''"])],
   415         Rule_Set.append_rules "prls_2x2_linear_system" Rule_Set.empty 
   416 			    [Rule.Thm ("LENGTH_CONS",ThmC.numerals_to_Free @{thm LENGTH_CONS}),
   417 			      Rule.Thm ("LENGTH_NIL",ThmC.numerals_to_Free @{thm LENGTH_NIL}),
   418 			      \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   419 			      \<^rule_eval>\<open>HOL.eq\<close> (Prog_Expr.eval_equal "#equal_")],
   420         SOME "solveSystem e_s v_s", [])),
   421     (Problem.prep_input @{theory} "pbl_equsys_lin_2x2_tri" [] Problem.id_empty
   422       (["triangular", "2x2", "LINEAR", "system"],
   423         [("#Given" ,["equalities e_s", "solveForVars v_s"]),
   424           ("#Where",
   425             ["(tl v_s) from v_s occur_exactly_in (NTH 1 (e_s::bool list))",
   426               "    v_s  from v_s occur_exactly_in (NTH 2 (e_s::bool list))"]),
   427           ("#Find"  ,["solution ss'''"])],
   428         prls_triangular, SOME "solveSystem e_s v_s", [["EqSystem", "top_down_substitution", "2x2"]])),
   429     (Problem.prep_input @{theory} "pbl_equsys_lin_2x2_norm" [] Problem.id_empty
   430       (["normalise", "2x2", "LINEAR", "system"],
   431         [("#Given" ,["equalities e_s", "solveForVars v_s"]),
   432           ("#Find"  ,["solution ss'''"])],
   433       Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)], 
   434       SOME "solveSystem e_s v_s", 
   435       [["EqSystem", "normalise", "2x2"]])),
   436     (Problem.prep_input @{theory} "pbl_equsys_lin_3x3" [] Problem.id_empty
   437       (["3x3", "LINEAR", "system"],
   438         (*~~~~~~~~~~~~~~~~~~~~~~~~~*)
   439         [("#Given" ,["equalities e_s", "solveForVars v_s"]),
   440           ("#Where"  ,["Length (e_s:: bool list) = 3", "Length v_s = 3"]),
   441           ("#Find"  ,["solution ss'''"])],
   442         Rule_Set.append_rules "prls_3x3_linear_system" Rule_Set.empty 
   443 			    [Rule.Thm ("LENGTH_CONS",ThmC.numerals_to_Free @{thm LENGTH_CONS}),
   444 			      Rule.Thm ("LENGTH_NIL",ThmC.numerals_to_Free @{thm LENGTH_NIL}),
   445 			      \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   446 			      \<^rule_eval>\<open>HOL.eq\<close> (Prog_Expr.eval_equal "#equal_")],
   447         SOME "solveSystem e_s v_s", [])),
   448     (Problem.prep_input @{theory} "pbl_equsys_lin_4x4" [] Problem.id_empty
   449       (["4x4", "LINEAR", "system"],
   450         (*~~~~~~~~~~~~~~~~~~~~~~~~~*)
   451         [("#Given" ,["equalities e_s", "solveForVars v_s"]),
   452           ("#Where"  ,["Length (e_s:: bool list) = 4", "Length v_s = 4"]),
   453           ("#Find"  ,["solution ss'''"])],
   454         Rule_Set.append_rules "prls_4x4_linear_system" Rule_Set.empty 
   455 			    [Rule.Thm ("LENGTH_CONS",ThmC.numerals_to_Free @{thm LENGTH_CONS}),
   456 			      Rule.Thm ("LENGTH_NIL",ThmC.numerals_to_Free @{thm LENGTH_NIL}),
   457 			      \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   458 			      \<^rule_eval>\<open>HOL.eq\<close> (Prog_Expr.eval_equal "#equal_")],
   459         SOME "solveSystem e_s v_s", [])),
   460     (Problem.prep_input @{theory} "pbl_equsys_lin_4x4_tri" [] Problem.id_empty
   461       (["triangular", "4x4", "LINEAR", "system"],
   462         [("#Given" ,["equalities e_s", "solveForVars v_s"]),
   463           ("#Where" , (*accepts missing variables up to diagional form*)
   464             ["(NTH 1 (v_s::real list)) occurs_in (NTH 1 (e_s::bool list))",
   465               "(NTH 2 (v_s::real list)) occurs_in (NTH 2 (e_s::bool list))",
   466               "(NTH 3 (v_s::real list)) occurs_in (NTH 3 (e_s::bool list))",
   467               "(NTH 4 (v_s::real list)) occurs_in (NTH 4 (e_s::bool list))"]),
   468           ("#Find"  ,["solution ss'''"])],
   469       Rule_Set.append_rules "prls_tri_4x4_lin_sys" prls_triangular
   470 	      [\<^rule_eval>\<open>Prog_Expr.occurs_in\<close> (Prog_Expr.eval_occurs_in "")], 
   471       SOME "solveSystem e_s v_s", 
   472       [["EqSystem", "top_down_substitution", "4x4"]])),
   473     (Problem.prep_input @{theory} "pbl_equsys_lin_4x4_norm" [] Problem.id_empty
   474       (["normalise", "4x4", "LINEAR", "system"],
   475         [("#Given" ,["equalities e_s", "solveForVars v_s"]),
   476           (*Length is checked 1 level above*)
   477           ("#Find"  ,["solution ss'''"])],
   478         Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)], 
   479         SOME "solveSystem e_s v_s", 
   480         [["EqSystem", "normalise", "4x4"]]))]\<close>
   481 
   482 ML \<open>
   483 (*this is for NTH only*)
   484 val srls = Rule_Def.Repeat {id="srls_normalise_4x4", 
   485 		preconds = [], 
   486 		rew_ord = ("termlessI",termlessI), 
   487 		erls = Rule_Set.append_rules "erls_in_srls_IntegrierenUnd.." Rule_Set.empty
   488 				  [(*for asm in NTH_CONS ...*)
   489 				   \<^rule_eval>\<open>less\<close> (Prog_Expr.eval_equ "#less_"),
   490 				   (*2nd NTH_CONS pushes n+-1 into asms*)
   491 				   \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")
   492 				   ], 
   493 		srls = Rule_Set.Empty, calc = [], errpatts = [],
   494 		rules = [Rule.Thm ("NTH_CONS",ThmC.numerals_to_Free @{thm NTH_CONS}),
   495 			 \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   496 			 Rule.Thm ("NTH_NIL",ThmC.numerals_to_Free @{thm NTH_NIL})],
   497 		scr = Rule.Empty_Prog};
   498 \<close>
   499 
   500 section \<open>Methods\<close>
   501 
   502 setup \<open>KEStore_Elems.add_mets
   503     [MethodC.prep_input @{theory} "met_eqsys" [] MethodC.id_empty
   504 	    (["EqSystem"], [],
   505 	      {rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], srls = Rule_Set.Empty, prls = Rule_Set.Empty, crls = Rule_Set.Empty,
   506           errpats = [], nrls = Rule_Set.Empty},
   507 	      @{thm refl}),
   508     MethodC.prep_input @{theory} "met_eqsys_topdown" [] MethodC.id_empty
   509       (["EqSystem", "top_down_substitution"], [],
   510         {rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], srls = Rule_Set.Empty, prls = Rule_Set.Empty, crls = Rule_Set.Empty,
   511           errpats = [], nrls = Rule_Set.Empty},
   512        @{thm refl})]
   513 \<close>
   514 
   515 partial_function (tailrec) solve_system :: "bool list => real list => bool list"
   516   where
   517 "solve_system e_s v_s = (
   518   let
   519     e_1 = Take (hd e_s);                                                         
   520     e_1 = (
   521       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''isolate_bdvs'')) #>                   
   522       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System''))
   523       ) e_1;                 
   524     e_2 = Take (hd (tl e_s));                                                    
   525     e_2 = (
   526       (Substitute [e_1]) #>                                                 
   527       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System_parenthesized'' )) #>      
   528       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''isolate_bdvs'' )) #>                   
   529       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System'' ))
   530       ) e_2;                 
   531     e__s = Take [e_1, e_2]                                                       
   532   in
   533     Try (Rewrite_Set ''order_system'' ) e__s)                              "
   534 setup \<open>KEStore_Elems.add_mets
   535     [MethodC.prep_input @{theory} "met_eqsys_topdown_2x2" [] MethodC.id_empty
   536       (["EqSystem", "top_down_substitution", "2x2"],
   537         [("#Given", ["equalities e_s", "solveForVars v_s"]),
   538           ("#Where",
   539             ["(tl v_s) from v_s occur_exactly_in (NTH 1 (e_s::bool list))",
   540               "    v_s  from v_s occur_exactly_in (NTH 2 (e_s::bool list))"]),
   541           ("#Find"  ,["solution ss'''"])],
   542 	      {rew_ord'="ord_simplify_System", rls' = Rule_Set.Empty, calc = [], 
   543 	        srls = Rule_Set.append_rules "srls_top_down_2x2" Rule_Set.empty
   544 				      [Rule.Thm ("hd_thm",ThmC.numerals_to_Free @{thm hd_thm}),
   545 				        Rule.Thm ("tl_Cons",ThmC.numerals_to_Free @{thm tl_Cons}),
   546 				        Rule.Thm ("tl_Nil",ThmC.numerals_to_Free @{thm tl_Nil})], 
   547 	        prls = prls_triangular, crls = Rule_Set.Empty, errpats = [], nrls = Rule_Set.Empty},
   548 	      @{thm solve_system.simps})]
   549 \<close>
   550 setup \<open>KEStore_Elems.add_mets
   551     [MethodC.prep_input @{theory} "met_eqsys_norm" [] MethodC.id_empty
   552 	    (["EqSystem", "normalise"], [],
   553 	      {rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], srls = Rule_Set.Empty, prls = Rule_Set.Empty, crls = Rule_Set.Empty,
   554           errpats = [], nrls = Rule_Set.Empty},
   555 	      @{thm refl})]
   556 \<close>
   557 
   558 partial_function (tailrec) solve_system2 :: "bool list => real list => bool list"
   559   where
   560 "solve_system2 e_s v_s = (
   561   let
   562     e__s = (
   563       (Try (Rewrite_Set ''norm_Rational'' )) #>
   564       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System_parenthesized'' )) #>
   565       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''isolate_bdvs'' )) #>
   566       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System_parenthesized'' )) #>
   567       (Try (Rewrite_Set ''order_system'' ))
   568       ) e_s
   569   in
   570     SubProblem (''EqSystem'', [''LINEAR'', ''system''], [''no_met''])
   571       [BOOL_LIST e__s, REAL_LIST v_s])"
   572 setup \<open>KEStore_Elems.add_mets
   573     [MethodC.prep_input @{theory} "met_eqsys_norm_2x2" [] MethodC.id_empty
   574 	    (["EqSystem", "normalise", "2x2"],
   575 	      [("#Given" ,["equalities e_s", "solveForVars v_s"]),
   576 		      ("#Find"  ,["solution ss'''"])],
   577 	      {rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], 
   578 	        srls = Rule_Set.append_rules "srls_normalise_2x2" Rule_Set.empty
   579 				      [Rule.Thm ("hd_thm",ThmC.numerals_to_Free @{thm hd_thm}),
   580 				        Rule.Thm ("tl_Cons",ThmC.numerals_to_Free @{thm tl_Cons}),
   581 				        Rule.Thm ("tl_Nil",ThmC.numerals_to_Free @{thm tl_Nil})], 
   582 		      prls = Rule_Set.Empty, crls = Rule_Set.Empty, errpats = [], nrls = Rule_Set.Empty},
   583 		    @{thm solve_system2.simps})]
   584 \<close>
   585 
   586 partial_function (tailrec) solve_system3 :: "bool list => real list => bool list"
   587   where
   588 "solve_system3 e_s v_s = (
   589   let
   590     e__s = (
   591       (Try (Rewrite_Set ''norm_Rational'' )) #>
   592       (Repeat (Rewrite ''commute_0_equality'' )) #>
   593       (Try (Rewrite_Set_Inst [(''bdv_1'', NTH 1 v_s), (''bdv_2'', NTH 2 v_s ),
   594         (''bdv_3'', NTH 3 v_s), (''bdv_3'', NTH 4 v_s )] ''simplify_System_parenthesized'' )) #>
   595       (Try (Rewrite_Set_Inst [(''bdv_1'', NTH 1 v_s), (''bdv_2'', NTH 2 v_s ),
   596         (''bdv_3'', NTH 3 v_s), (''bdv_3'', NTH 4 v_s )] ''isolate_bdvs_4x4'' )) #>
   597       (Try (Rewrite_Set_Inst [(''bdv_1'', NTH 1 v_s), (''bdv_2'', NTH 2 v_s ),
   598         (''bdv_3'', NTH 3 v_s), (''bdv_3'', NTH 4 v_s )] ''simplify_System_parenthesized'' )) #>
   599       (Try (Rewrite_Set ''order_system''))
   600       )  e_s
   601   in
   602     SubProblem (''EqSystem'', [''LINEAR'', ''system''], [''no_met''])
   603       [BOOL_LIST e__s, REAL_LIST v_s])"
   604 setup \<open>KEStore_Elems.add_mets
   605     [MethodC.prep_input @{theory} "met_eqsys_norm_4x4" [] MethodC.id_empty
   606 	      (["EqSystem", "normalise", "4x4"],
   607 	       [("#Given" ,["equalities e_s", "solveForVars v_s"]),
   608 	         ("#Find"  ,["solution ss'''"])],
   609 	       {rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], 
   610 	         srls = Rule_Set.append_rules "srls_normalise_4x4" srls
   611 	             [Rule.Thm ("hd_thm",ThmC.numerals_to_Free @{thm hd_thm}),
   612 	               Rule.Thm ("tl_Cons",ThmC.numerals_to_Free @{thm tl_Cons}),
   613 	               Rule.Thm ("tl_Nil",ThmC.numerals_to_Free @{thm tl_Nil})], 
   614 		       prls = Rule_Set.Empty, crls = Rule_Set.Empty, errpats = [], nrls = Rule_Set.Empty},
   615 		     (*STOPPED.WN06? met ["EqSystem", "normalise", "4x4"] #>#>#>#>#>#>#>#>#>#>#>#>#>@*)
   616 		     @{thm solve_system3.simps})]
   617 \<close>
   618 
   619 partial_function (tailrec) solve_system4 :: "bool list => real list => bool list"
   620   where
   621 "solve_system4 e_s v_s = (
   622   let
   623     e_1 = NTH 1 e_s;
   624     e_2 = Take (NTH 2 e_s);
   625     e_2 = (
   626       (Substitute [e_1]) #>
   627       (Try (Rewrite_Set_Inst [(''bdv_1'',NTH 1 v_s), (''bdv_2'',NTH 2 v_s),
   628         (''bdv_3'',NTH 3 v_s), (''bdv_4'',NTH 4 v_s)] ''simplify_System_parenthesized'' )) #>
   629       (Try (Rewrite_Set_Inst [(''bdv_1'',NTH 1 v_s), (''bdv_2'',NTH 2 v_s),
   630         (''bdv_3'',NTH 3 v_s), (''bdv_4'',NTH 4 v_s)] ''isolate_bdvs'' )) #>
   631       (Try (Rewrite_Set_Inst [(''bdv_1'',NTH 1 v_s), (''bdv_2'',NTH 2 v_s),
   632         (''bdv_3'',NTH 3 v_s), (''bdv_4'',NTH 4 v_s)] ''norm_Rational'' ))
   633       ) e_2
   634   in
   635     [e_1, e_2, NTH 3 e_s, NTH 4 e_s])"
   636 setup \<open>KEStore_Elems.add_mets
   637     [MethodC.prep_input @{theory} "met_eqsys_topdown_4x4" [] MethodC.id_empty
   638 	    (["EqSystem", "top_down_substitution", "4x4"],
   639 	      [("#Given" ,["equalities e_s", "solveForVars v_s"]),
   640 	        ("#Where" , (*accepts missing variables up to diagonal form*)
   641             ["(NTH 1 (v_s::real list)) occurs_in (NTH 1 (e_s::bool list))",
   642               "(NTH 2 (v_s::real list)) occurs_in (NTH 2 (e_s::bool list))",
   643               "(NTH 3 (v_s::real list)) occurs_in (NTH 3 (e_s::bool list))",
   644               "(NTH 4 (v_s::real list)) occurs_in (NTH 4 (e_s::bool list))"]),
   645 	        ("#Find", ["solution ss'''"])],
   646 	    {rew_ord'="ord_simplify_System", rls' = Rule_Set.Empty, calc = [], 
   647 	      srls = Rule_Set.append_rules "srls_top_down_4x4" srls [], 
   648 	      prls = Rule_Set.append_rules "prls_tri_4x4_lin_sys" prls_triangular
   649 			      [\<^rule_eval>\<open>Prog_Expr.occurs_in\<close> (Prog_Expr.eval_occurs_in "")], 
   650 	      crls = Rule_Set.Empty, errpats = [], nrls = Rule_Set.Empty},
   651 	    (*FIXXXXME.WN060916: this script works ONLY for exp 7.79 #>#>#>#>#>#>#>#>#>#>*)
   652 	    @{thm solve_system4.simps})]
   653 \<close> ML \<open>
   654 \<close> ML \<open>
   655 \<close> ML \<open>
   656 \<close>
   657 end