src/Tools/isac/Knowledge/EqSystem.thy
author wneuper <walther.neuper@jku.at>
Mon, 19 Jul 2021 15:34:54 +0200
changeset 60335 7701598a2182
parent 60331 40eb8aa2b0d6
child 60358 8377b6c37640
permissions -rw-r--r--
ALL const_name replaces (others cannot be replaced)
     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 val thy = @{theory};
    53 
    54 (** eval functions **)
    55 
    56 (*certain variables of a given list occur _all_ in a term
    57   args: all: ..variables, which are under consideration (eg. the bound vars)
    58         vs:  variables which must be in t, 
    59              and none of the others in all must be in t
    60         t: the term under consideration
    61  *)
    62 fun occur_exactly_in vs all t =
    63     let fun occurs_in' a b = Prog_Expr.occurs_in b a
    64     in foldl and_ (true, map (occurs_in' t) vs)
    65        andalso not (foldl or_ (false, map (occurs_in' t) 
    66                                           (subtract op = vs all)))
    67     end;
    68 
    69 (*("occur_exactly_in", ("EqSystem.occur_exactly_in", 
    70                         eval_occur_exactly_in "#eval_occur_exactly_in_") )*)
    71 fun eval_occur_exactly_in _ "EqSystem.occur_exactly_in"
    72 			  (p as (Const (\<^const_name>\<open>EqSystem.occur_exactly_in\<close>,_) 
    73 				       $ vs $ all $ t)) _ =
    74     if occur_exactly_in (TermC.isalist2list vs) (TermC.isalist2list all) t
    75     then SOME ((UnparseC.term p) ^ " = True",
    76 	       HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
    77     else SOME ((UnparseC.term p) ^ " = False",
    78 	       HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
    79   | eval_occur_exactly_in _ _ _ _ = NONE;
    80 \<close>
    81 calculation occur_exactly_in = \<open>eval_occur_exactly_in "#eval_occur_exactly_in_"\<close>
    82 
    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*) (ts, us) = 
   159     (term_ord' pr thy (TermC.numerals_to_Free ts, TermC.numerals_to_Free us) = 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>\<open>mult.commute\<close>,
   177 	       (* z * w = w * z *)
   178 	       \<^rule_thm>\<open>real_mult_left_commute\<close>,
   179 	       (*z1.0 * (z2.0 * z3.0) = z2.0 * (z1.0 * z3.0)*)
   180 	       \<^rule_thm>\<open>mult.assoc\<close>,		
   181 	       (*z1.0 * z2.0 * z3.0 = z1.0 * (z2.0 * z3.0)*)
   182 	       \<^rule_thm>\<open>add.commute\<close>,	
   183 	       (*z + w = w + z*)
   184 	       \<^rule_thm>\<open>add.left_commute\<close>,
   185 	       (*x + (y + z) = y + (x + z)*)
   186 	       \<^rule_thm>\<open>add.assoc\<close>	               
   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>\<open>distrib_right\<close>,
   249  	       (*"(?z1.0 + ?z2.0) * ?w = ?z1.0 * ?w + ?z2.0 * ?w"*)
   250 	       \<^rule_thm>\<open>add_divide_distrib\<close>,
   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>\<open>commute_0_equality\<close>,
   295 	      \<^rule_thm>\<open>separate_bdvs_add\<close>,
   296 	      \<^rule_thm>\<open>separate_bdvs_mult\<close>],
   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>\<open>not_true\<close>,
   309 		     \<^rule_thm>\<open>not_false\<close>
   310 			    ], 
   311 	 srls = Rule_Set.Empty, calc = [], errpatts = [],
   312 	 rules = [\<^rule_thm>\<open>commute_0_equality\<close>,
   313 		  \<^rule_thm>\<open>separate_bdvs0\<close>,
   314 		  \<^rule_thm>\<open>separate_bdvs_add1\<close>,
   315 		  \<^rule_thm>\<open>separate_bdvs_add2\<close>,
   316 		  \<^rule_thm>\<open>separate_bdvs_mult\<close>
   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>\<open>order_system_NxN\<close>
   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             Rule.Eval ("EqSystem.occur_exactly_in", 
   343               eval_occur_exactly_in "#eval_occur_exactly_in_")
   344 			      (*immediately repeated rewrite pushes
   345 					    '+' into precondition !*)
   346 			      ],
   347 		     scr = Rule.Empty_Prog}, 
   348 	 srls = Rule_Set.Empty, calc = [], errpatts = [],
   349 	 rules = [\<^rule_thm>\<open>NTH_CONS\<close>,
   350 		  \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   351 		  \<^rule_thm>\<open>NTH_NIL\<close>,
   352 		  \<^rule_thm>\<open>tl_Cons\<close>,
   353 		  \<^rule_thm>\<open>tl_Nil\<close>,
   354 		  \<^rule_eval>\<open>occur_exactly_in\<close> (eval_occur_exactly_in "#eval_occur_exactly_in_")
   355 		  ],
   356 	 scr = Rule.Empty_Prog};
   357 \<close>
   358 ML \<open>
   359 
   360 (*WN060914 quickly created for 4x4; 
   361  more similarity to prls_triangular desirable*)
   362 val prls_triangular4 = 
   363     Rule_Def.Repeat {id="prls_triangular4", preconds = [], 
   364 	 rew_ord = ("e_rew_ord", Rewrite_Ord.e_rew_ord), 
   365 	 erls = Rule_Def.Repeat {id="erls_prls_triangular4", preconds = [], 
   366 		     rew_ord = ("e_rew_ord", Rewrite_Ord.e_rew_ord), 
   367 		     erls = Rule_Set.Empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
   368 		     rules = [(*for precond NTH_CONS ...*)
   369 			      \<^rule_eval>\<open>less\<close> (Prog_Expr.eval_equ "#less_"),
   370 			      \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")
   371 			      (*immediately repeated rewrite pushes
   372 					    '+' into precondition !*)
   373 			      ],
   374 		     scr = Rule.Empty_Prog}, 
   375 	 srls = Rule_Set.Empty, calc = [], errpatts = [],
   376 	 rules = [\<^rule_thm>\<open>NTH_CONS\<close>,
   377 		  \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   378 		  \<^rule_thm>\<open>NTH_NIL\<close>,
   379 		  \<^rule_thm>\<open>tl_Cons\<close>,
   380 		  \<^rule_thm>\<open>tl_Nil\<close>,
   381 		  \<^rule_eval>\<open>occur_exactly_in\<close> (eval_occur_exactly_in "#eval_occur_exactly_in_")
   382 		  ],
   383 	 scr = Rule.Empty_Prog};
   384 \<close>
   385 
   386 rule_set_knowledge
   387   simplify_System_parenthesized = \<open>prep_rls' simplify_System_parenthesized\<close> and
   388   simplify_System = \<open>prep_rls' simplify_System\<close> and
   389   isolate_bdvs = \<open>prep_rls' isolate_bdvs\<close> and
   390   isolate_bdvs_4x4 = \<open>prep_rls' isolate_bdvs_4x4\<close> and 
   391   order_system = \<open>prep_rls' order_system\<close> and 
   392   order_add_mult_System = \<open>prep_rls' order_add_mult_System\<close> and
   393   norm_System_noadd_fractions = \<open>prep_rls' norm_System_noadd_fractions\<close> and
   394   norm_System = \<open>prep_rls' norm_System\<close>
   395 
   396 
   397 section \<open>Problems\<close>
   398 
   399 problem pbl_equsys : "system" =
   400   \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
   401   CAS: "solveSystem e_s v_s"
   402   Given: "equalities e_s" "solveForVars v_s"
   403   Find: "solution ss'''" (*''' is copy-named*)
   404 
   405 problem pbl_equsys_lin : "LINEAR/system" =
   406   \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
   407   CAS: "solveSystem e_s v_s"
   408   Given: "equalities e_s" "solveForVars v_s"
   409   (*TODO.WN050929 check linearity*)
   410   Find: "solution ss'''"
   411 
   412 problem pbl_equsys_lin_2x2: "2x2/LINEAR/system" =
   413   \<open>Rule_Set.append_rules "prls_2x2_linear_system" Rule_Set.empty 
   414     [\<^rule_thm>\<open>LENGTH_CONS\<close>,
   415       \<^rule_thm>\<open>LENGTH_NIL\<close>,
   416       \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   417       \<^rule_eval>\<open>HOL.eq\<close> (Prog_Expr.eval_equal "#equal_")]\<close>
   418   CAS: "solveSystem e_s v_s"
   419   Given: "equalities e_s" "solveForVars v_s"
   420   Where: "Length (e_s:: bool list) = 2" "Length v_s = 2"
   421   Find: "solution ss'''"
   422 
   423 problem pbl_equsys_lin_2x2_tri : "triangular/2x2/LINEAR/system" =
   424   \<open>prls_triangular\<close>
   425   Method: "EqSystem/top_down_substitution/2x2"
   426   CAS: "solveSystem e_s v_s"
   427   Given: "equalities e_s" "solveForVars v_s"
   428   Where:
   429     "(tl v_s) from v_s occur_exactly_in (NTH 1 (e_s::bool list))"
   430     "    v_s  from v_s occur_exactly_in (NTH 2 (e_s::bool list))"
   431   Find: "solution ss'''"
   432 
   433 problem pbl_equsys_lin_2x2_norm : "normalise/2x2/LINEAR/system" =
   434   \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
   435   Method: "EqSystem/normalise/2x2"
   436   CAS: "solveSystem e_s v_s"
   437   Given: "equalities e_s" "solveForVars v_s"
   438   Find: "solution ss'''"
   439 
   440 problem pbl_equsys_lin_3x3 : "3x3/LINEAR/system" =
   441   \<open>Rule_Set.append_rules "prls_3x3_linear_system" Rule_Set.empty 
   442     [\<^rule_thm>\<open>LENGTH_CONS\<close>,
   443       \<^rule_thm>\<open>LENGTH_NIL\<close>,
   444       \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   445       \<^rule_eval>\<open>HOL.eq\<close> (Prog_Expr.eval_equal "#equal_")]\<close>
   446   CAS: "solveSystem e_s v_s"
   447   Given: "equalities e_s" "solveForVars v_s"
   448   Where: "Length (e_s:: bool list) = 3" "Length v_s = 3"
   449   Find: "solution ss'''"
   450 
   451 problem pbl_equsys_lin_4x4 : "4x4/LINEAR/system" =
   452   \<open>Rule_Set.append_rules "prls_4x4_linear_system" Rule_Set.empty 
   453     [\<^rule_thm>\<open>LENGTH_CONS\<close>,
   454       \<^rule_thm>\<open>LENGTH_NIL\<close>,
   455       \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   456       \<^rule_eval>\<open>HOL.eq\<close> (Prog_Expr.eval_equal "#equal_")]\<close>
   457   CAS: "solveSystem e_s v_s"
   458   Given: "equalities e_s" "solveForVars v_s"
   459   Where: "Length (e_s:: bool list) = 4" "Length v_s = 4"
   460   Find: "solution ss'''"
   461 
   462 problem pbl_equsys_lin_4x4_tri : "triangular/4x4/LINEAR/system" =
   463   \<open>Rule_Set.append_rules "prls_tri_4x4_lin_sys" prls_triangular
   464     [\<^rule_eval>\<open>Prog_Expr.occurs_in\<close> (Prog_Expr.eval_occurs_in "")]\<close>
   465   Method: "EqSystem/top_down_substitution/4x4"
   466   CAS: "solveSystem e_s v_s"
   467   Given: "equalities e_s" "solveForVars v_s"
   468   Where: (*accepts missing variables up to diagional form*)
   469     "(NTH 1 (v_s::real list)) occurs_in (NTH 1 (e_s::bool list))"
   470     "(NTH 2 (v_s::real list)) occurs_in (NTH 2 (e_s::bool list))"
   471     "(NTH 3 (v_s::real list)) occurs_in (NTH 3 (e_s::bool list))"
   472     "(NTH 4 (v_s::real list)) occurs_in (NTH 4 (e_s::bool list))"
   473   Find: "solution ss'''"
   474 
   475 problem pbl_equsys_lin_4x4_norm : "normalise/4x4/LINEAR/system" =
   476   \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
   477   Method: "EqSystem/normalise/4x4"
   478   CAS: "solveSystem e_s v_s"
   479   Given: "equalities e_s" "solveForVars v_s"
   480   (*Length is checked 1 level above*)
   481   Find: "solution ss'''"
   482 
   483 ML \<open>
   484 (*this is for NTH only*)
   485 val srls = Rule_Def.Repeat {id="srls_normalise_4x4", 
   486 		preconds = [], 
   487 		rew_ord = ("termlessI",termlessI), 
   488 		erls = Rule_Set.append_rules "erls_in_srls_IntegrierenUnd.." Rule_Set.empty
   489 				  [(*for asm in NTH_CONS ...*)
   490 				   \<^rule_eval>\<open>less\<close> (Prog_Expr.eval_equ "#less_"),
   491 				   (*2nd NTH_CONS pushes n+-1 into asms*)
   492 				   \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")
   493 				   ], 
   494 		srls = Rule_Set.Empty, calc = [], errpatts = [],
   495 		rules = [\<^rule_thm>\<open>NTH_CONS\<close>,
   496 			 \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   497 			 \<^rule_thm>\<open>NTH_NIL\<close>],
   498 		scr = Rule.Empty_Prog};
   499 \<close>
   500 
   501 section \<open>Methods\<close>
   502 
   503 method met_eqsys : "EqSystem" =
   504   \<open>{rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], srls = Rule_Set.Empty, prls = Rule_Set.Empty, crls = Rule_Set.Empty,
   505     errpats = [], nrls = Rule_Set.Empty}\<close>
   506 
   507 method met_eqsys_topdown : "EqSystem/top_down_substitution" =
   508   \<open>{rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], srls = Rule_Set.Empty, prls = Rule_Set.Empty, crls = Rule_Set.Empty,
   509     errpats = [], nrls = Rule_Set.Empty}\<close>
   510 
   511 partial_function (tailrec) solve_system :: "bool list => real list => bool list"
   512   where
   513 "solve_system e_s v_s = (
   514   let
   515     e_1 = Take (hd e_s);                                                         
   516     e_1 = (
   517       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''isolate_bdvs'')) #>                   
   518       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System''))
   519       ) e_1;                 
   520     e_2 = Take (hd (tl e_s));                                                    
   521     e_2 = (
   522       (Substitute [e_1]) #>                                                 
   523       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System_parenthesized'' )) #>      
   524       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''isolate_bdvs'' )) #>                   
   525       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System'' ))
   526       ) e_2;                 
   527     e__s = Take [e_1, e_2]                                                       
   528   in
   529     Try (Rewrite_Set ''order_system'' ) e__s)                              "
   530 
   531 method met_eqsys_topdown_2x2 : "EqSystem/top_down_substitution/2x2" =
   532   \<open>{rew_ord'="ord_simplify_System", rls' = Rule_Set.Empty, calc = [], 
   533     srls = Rule_Set.append_rules "srls_top_down_2x2" Rule_Set.empty
   534         [\<^rule_thm>\<open>hd_thm\<close>,
   535           \<^rule_thm>\<open>tl_Cons\<close>,
   536           \<^rule_thm>\<open>tl_Nil\<close>], 
   537     prls = prls_triangular, crls = Rule_Set.Empty, errpats = [], nrls = Rule_Set.Empty}\<close>
   538   Program: solve_system.simps
   539   Given: "equalities e_s" "solveForVars v_s"
   540   Where:
   541     "(tl v_s) from v_s occur_exactly_in (NTH 1 (e_s::bool list))"
   542     "    v_s  from v_s occur_exactly_in (NTH 2 (e_s::bool list))"
   543   Find: "solution ss'''"
   544 
   545 method met_eqsys_norm : "EqSystem/normalise" =
   546   \<open>{rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], srls = Rule_Set.Empty, prls = Rule_Set.Empty, crls = Rule_Set.Empty,
   547     errpats = [], nrls = Rule_Set.Empty}\<close>
   548 
   549 partial_function (tailrec) solve_system2 :: "bool list => real list => bool list"
   550   where
   551 "solve_system2 e_s v_s = (
   552   let
   553     e__s = (
   554       (Try (Rewrite_Set ''norm_Rational'' )) #>
   555       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System_parenthesized'' )) #>
   556       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''isolate_bdvs'' )) #>
   557       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System_parenthesized'' )) #>
   558       (Try (Rewrite_Set ''order_system'' ))
   559       ) e_s
   560   in
   561     SubProblem (''EqSystem'', [''LINEAR'', ''system''], [''no_met''])
   562       [BOOL_LIST e__s, REAL_LIST v_s])"
   563 
   564 method met_eqsys_norm_2x2 : "EqSystem/normalise/2x2" =
   565   \<open>{rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], 
   566     srls = Rule_Set.append_rules "srls_normalise_2x2" Rule_Set.empty
   567         [\<^rule_thm>\<open>hd_thm\<close>,
   568           \<^rule_thm>\<open>tl_Cons\<close>,
   569           \<^rule_thm>\<open>tl_Nil\<close>], 
   570     prls = Rule_Set.Empty, crls = Rule_Set.Empty, errpats = [], nrls = Rule_Set.Empty}\<close>
   571   Program: solve_system2.simps
   572   Given: "equalities e_s" "solveForVars v_s"
   573   Find: "solution ss'''"
   574 
   575 partial_function (tailrec) solve_system3 :: "bool list => real list => bool list"
   576   where
   577 "solve_system3 e_s v_s = (
   578   let
   579     e__s = (
   580       (Try (Rewrite_Set ''norm_Rational'' )) #>
   581       (Repeat (Rewrite ''commute_0_equality'' )) #>
   582       (Try (Rewrite_Set_Inst [(''bdv_1'', NTH 1 v_s), (''bdv_2'', NTH 2 v_s ),
   583         (''bdv_3'', NTH 3 v_s), (''bdv_3'', NTH 4 v_s )] ''simplify_System_parenthesized'' )) #>
   584       (Try (Rewrite_Set_Inst [(''bdv_1'', NTH 1 v_s), (''bdv_2'', NTH 2 v_s ),
   585         (''bdv_3'', NTH 3 v_s), (''bdv_3'', NTH 4 v_s )] ''isolate_bdvs_4x4'' )) #>
   586       (Try (Rewrite_Set_Inst [(''bdv_1'', NTH 1 v_s), (''bdv_2'', NTH 2 v_s ),
   587         (''bdv_3'', NTH 3 v_s), (''bdv_3'', NTH 4 v_s )] ''simplify_System_parenthesized'' )) #>
   588       (Try (Rewrite_Set ''order_system''))
   589       )  e_s
   590   in
   591     SubProblem (''EqSystem'', [''LINEAR'', ''system''], [''no_met''])
   592       [BOOL_LIST e__s, REAL_LIST v_s])"
   593 
   594 method met_eqsys_norm_4x4 : "EqSystem/normalise/4x4" =
   595   \<open>{rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [],
   596     srls =
   597       Rule_Set.append_rules "srls_normalise_4x4" srls
   598         [\<^rule_thm>\<open>hd_thm\<close>, \<^rule_thm>\<open>tl_Cons\<close>, \<^rule_thm>\<open>tl_Nil\<close>],
   599     prls = Rule_Set.Empty, crls = Rule_Set.Empty, errpats = [], nrls = Rule_Set.Empty}\<close>
   600   Program: solve_system3.simps
   601   Given: "equalities e_s" "solveForVars v_s"
   602   Find: "solution ss'''"
   603   (*STOPPED.WN06? met ["EqSystem", "normalise", "4x4"] #>#>#>#>#>#>#>#>#>#>#>#>#>@*)
   604 
   605 partial_function (tailrec) solve_system4 :: "bool list => real list => bool list"
   606   where
   607 "solve_system4 e_s v_s = (
   608   let
   609     e_1 = NTH 1 e_s;
   610     e_2 = Take (NTH 2 e_s);
   611     e_2 = (
   612       (Substitute [e_1]) #>
   613       (Try (Rewrite_Set_Inst [(''bdv_1'',NTH 1 v_s), (''bdv_2'',NTH 2 v_s),
   614         (''bdv_3'',NTH 3 v_s), (''bdv_4'',NTH 4 v_s)] ''simplify_System_parenthesized'' )) #>
   615       (Try (Rewrite_Set_Inst [(''bdv_1'',NTH 1 v_s), (''bdv_2'',NTH 2 v_s),
   616         (''bdv_3'',NTH 3 v_s), (''bdv_4'',NTH 4 v_s)] ''isolate_bdvs'' )) #>
   617       (Try (Rewrite_Set_Inst [(''bdv_1'',NTH 1 v_s), (''bdv_2'',NTH 2 v_s),
   618         (''bdv_3'',NTH 3 v_s), (''bdv_4'',NTH 4 v_s)] ''norm_Rational'' ))
   619       ) e_2
   620   in
   621     [e_1, e_2, NTH 3 e_s, NTH 4 e_s])"
   622 
   623 method met_eqsys_topdown_4x4 : "EqSystem/top_down_substitution/4x4" =
   624   \<open>{rew_ord'="ord_simplify_System", rls' = Rule_Set.Empty, calc = [], 
   625     srls = Rule_Set.append_rules "srls_top_down_4x4" srls [], 
   626     prls = Rule_Set.append_rules "prls_tri_4x4_lin_sys" prls_triangular
   627         [\<^rule_eval>\<open>Prog_Expr.occurs_in\<close> (Prog_Expr.eval_occurs_in "")], 
   628     crls = Rule_Set.Empty, errpats = [], nrls = Rule_Set.Empty}\<close>
   629   (*FIXXXXME.WN060916: this script works ONLY for exp 7.79 #>#>#>#>#>#>#>#>#>#>*)
   630   Program: solve_system4.simps
   631   Given: "equalities e_s" "solveForVars v_s"
   632   Where: (*accepts missing variables up to diagonal form*)
   633     "(NTH 1 (v_s::real list)) occurs_in (NTH 1 (e_s::bool list))"
   634     "(NTH 2 (v_s::real list)) occurs_in (NTH 2 (e_s::bool list))"
   635     "(NTH 3 (v_s::real list)) occurs_in (NTH 3 (e_s::bool list))"
   636     "(NTH 4 (v_s::real list)) occurs_in (NTH 4 (e_s::bool list))"
   637   Find: "solution ss'''"
   638 
   639 ML \<open>
   640 \<close> ML \<open>
   641 \<close> ML \<open>
   642 \<close>
   643 end