src/Tools/isac/Knowledge/EqSystem.thy
author wenzelm
Sun, 20 Jun 2021 16:26:18 +0200
changeset 60306 51ec2e101e9f
parent 60303 815b0dc8b589
child 60312 35f7b2f61797
permissions -rw-r--r--
Isar command 'problem' as combination of KEStore_Elems.add_pbts + Problem.prep_import, without change of semantics;
     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>\<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 			      (*immediately repeated rewrite pushes
   343 					    '+' into precondition !*)
   344 			      ],
   345 		     scr = Rule.Empty_Prog}, 
   346 	 srls = Rule_Set.Empty, calc = [], errpatts = [],
   347 	 rules = [\<^rule_thm>\<open>NTH_CONS\<close>,
   348 		  \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   349 		  \<^rule_thm>\<open>NTH_NIL\<close>,
   350 		  \<^rule_thm>\<open>tl_Cons\<close>,
   351 		  \<^rule_thm>\<open>tl_Nil\<close>,
   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>\<open>NTH_CONS\<close>,
   375 		  \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   376 		  \<^rule_thm>\<open>NTH_NIL\<close>,
   377 		  \<^rule_thm>\<open>tl_Cons\<close>,
   378 		  \<^rule_thm>\<open>tl_Nil\<close>,
   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 problem pbl_equsys : "system" =
   398   \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
   399   CAS: "solveSystem e_s v_s"
   400   Given: "equalities e_s" "solveForVars v_s"
   401   Find: "solution ss'''" (*''' is copy-named*)
   402 
   403 problem pbl_equsys_lin : "LINEAR/system" =
   404   \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
   405   CAS: "solveSystem e_s v_s"
   406   Given: "equalities e_s" "solveForVars v_s"
   407   (*TODO.WN050929 check linearity*)
   408   Find: "solution ss'''"
   409 
   410 problem pbl_equsys_lin_2x2: "2x2/LINEAR/system" =
   411   \<open>Rule_Set.append_rules "prls_2x2_linear_system" Rule_Set.empty 
   412     [\<^rule_thm>\<open>LENGTH_CONS\<close>,
   413       \<^rule_thm>\<open>LENGTH_NIL\<close>,
   414       \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   415       \<^rule_eval>\<open>HOL.eq\<close> (Prog_Expr.eval_equal "#equal_")]\<close>
   416   CAS: "solveSystem e_s v_s"
   417   Given: "equalities e_s" "solveForVars v_s"
   418   Where: "Length (e_s:: bool list) = 2" "Length v_s = 2"
   419   Find: "solution ss'''"
   420 
   421 problem pbl_equsys_lin_2x2_tri : "triangular/2x2/LINEAR/system" =
   422   \<open>prls_triangular\<close>
   423   Method: "EqSystem/top_down_substitution/2x2"
   424   CAS: "solveSystem e_s v_s"
   425   Given: "equalities e_s" "solveForVars v_s"
   426   Where:
   427     "(tl v_s) from v_s occur_exactly_in (NTH 1 (e_s::bool list))"
   428     "    v_s  from v_s occur_exactly_in (NTH 2 (e_s::bool list))"
   429   Find: "solution ss'''"
   430 
   431 problem pbl_equsys_lin_2x2_norm : "normalise/2x2/LINEAR/system" =
   432   \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
   433   Method: "EqSystem/normalise/2x2"
   434   CAS: "solveSystem e_s v_s"
   435   Given: "equalities e_s" "solveForVars v_s"
   436   Find: "solution ss'''"
   437 
   438 problem pbl_equsys_lin_3x3 : "3x3/LINEAR/system" =
   439   \<open>Rule_Set.append_rules "prls_3x3_linear_system" Rule_Set.empty 
   440     [\<^rule_thm>\<open>LENGTH_CONS\<close>,
   441       \<^rule_thm>\<open>LENGTH_NIL\<close>,
   442       \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   443       \<^rule_eval>\<open>HOL.eq\<close> (Prog_Expr.eval_equal "#equal_")]\<close>
   444   CAS: "solveSystem e_s v_s"
   445   Given: "equalities e_s" "solveForVars v_s"
   446   Where: "Length (e_s:: bool list) = 3" "Length v_s = 3"
   447   Find: "solution ss'''"
   448 
   449 problem pbl_equsys_lin_4x4 : "4x4/LINEAR/system" =
   450   \<open>Rule_Set.append_rules "prls_4x4_linear_system" Rule_Set.empty 
   451     [\<^rule_thm>\<open>LENGTH_CONS\<close>,
   452       \<^rule_thm>\<open>LENGTH_NIL\<close>,
   453       \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   454       \<^rule_eval>\<open>HOL.eq\<close> (Prog_Expr.eval_equal "#equal_")]\<close>
   455   CAS: "solveSystem e_s v_s"
   456   Given: "equalities e_s" "solveForVars v_s"
   457   Where: "Length (e_s:: bool list) = 4" "Length v_s = 4"
   458   Find: "solution ss'''"
   459 
   460 problem pbl_equsys_lin_4x4_tri : "triangular/4x4/LINEAR/system" =
   461   \<open>Rule_Set.append_rules "prls_tri_4x4_lin_sys" prls_triangular
   462     [\<^rule_eval>\<open>Prog_Expr.occurs_in\<close> (Prog_Expr.eval_occurs_in "")]\<close>
   463   Method: "EqSystem/top_down_substitution/4x4"
   464   CAS: "solveSystem e_s v_s"
   465   Given: "equalities e_s" "solveForVars v_s"
   466   Where: (*accepts missing variables up to diagional form*)
   467     "(NTH 1 (v_s::real list)) occurs_in (NTH 1 (e_s::bool list))"
   468     "(NTH 2 (v_s::real list)) occurs_in (NTH 2 (e_s::bool list))"
   469     "(NTH 3 (v_s::real list)) occurs_in (NTH 3 (e_s::bool list))"
   470     "(NTH 4 (v_s::real list)) occurs_in (NTH 4 (e_s::bool list))"
   471   Find: "solution ss'''"
   472 
   473 problem pbl_equsys_lin_4x4_norm : "normalise/4x4/LINEAR/system" =
   474   \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
   475   Method: "EqSystem/normalise/4x4"
   476   CAS: "solveSystem e_s v_s"
   477   Given: "equalities e_s" "solveForVars v_s"
   478   (*Length is checked 1 level above*)
   479   Find: "solution ss'''"
   480 
   481 ML \<open>
   482 (*this is for NTH only*)
   483 val srls = Rule_Def.Repeat {id="srls_normalise_4x4", 
   484 		preconds = [], 
   485 		rew_ord = ("termlessI",termlessI), 
   486 		erls = Rule_Set.append_rules "erls_in_srls_IntegrierenUnd.." Rule_Set.empty
   487 				  [(*for asm in NTH_CONS ...*)
   488 				   \<^rule_eval>\<open>less\<close> (Prog_Expr.eval_equ "#less_"),
   489 				   (*2nd NTH_CONS pushes n+-1 into asms*)
   490 				   \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")
   491 				   ], 
   492 		srls = Rule_Set.Empty, calc = [], errpatts = [],
   493 		rules = [\<^rule_thm>\<open>NTH_CONS\<close>,
   494 			 \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   495 			 \<^rule_thm>\<open>NTH_NIL\<close>],
   496 		scr = Rule.Empty_Prog};
   497 \<close>
   498 
   499 section \<open>Methods\<close>
   500 
   501 method met_eqsys : "EqSystem" =
   502   \<open>{rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], srls = Rule_Set.Empty, prls = Rule_Set.Empty, crls = Rule_Set.Empty,
   503     errpats = [], nrls = Rule_Set.Empty}\<close>
   504 
   505 method met_eqsys_topdown : "EqSystem/top_down_substitution" =
   506   \<open>{rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], srls = Rule_Set.Empty, prls = Rule_Set.Empty, crls = Rule_Set.Empty,
   507     errpats = [], nrls = Rule_Set.Empty}\<close>
   508 
   509 partial_function (tailrec) solve_system :: "bool list => real list => bool list"
   510   where
   511 "solve_system e_s v_s = (
   512   let
   513     e_1 = Take (hd e_s);                                                         
   514     e_1 = (
   515       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''isolate_bdvs'')) #>                   
   516       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System''))
   517       ) e_1;                 
   518     e_2 = Take (hd (tl e_s));                                                    
   519     e_2 = (
   520       (Substitute [e_1]) #>                                                 
   521       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System_parenthesized'' )) #>      
   522       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''isolate_bdvs'' )) #>                   
   523       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System'' ))
   524       ) e_2;                 
   525     e__s = Take [e_1, e_2]                                                       
   526   in
   527     Try (Rewrite_Set ''order_system'' ) e__s)                              "
   528 
   529 method met_eqsys_topdown_2x2 : "EqSystem/top_down_substitution/2x2" =
   530   \<open>{rew_ord'="ord_simplify_System", rls' = Rule_Set.Empty, calc = [], 
   531     srls = Rule_Set.append_rules "srls_top_down_2x2" Rule_Set.empty
   532         [\<^rule_thm>\<open>hd_thm\<close>,
   533           \<^rule_thm>\<open>tl_Cons\<close>,
   534           \<^rule_thm>\<open>tl_Nil\<close>], 
   535     prls = prls_triangular, crls = Rule_Set.Empty, errpats = [], nrls = Rule_Set.Empty}\<close>
   536   Program: solve_system.simps
   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 
   543 method met_eqsys_norm : "EqSystem/normalise" =
   544   \<open>{rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], srls = Rule_Set.Empty, prls = Rule_Set.Empty, crls = Rule_Set.Empty,
   545     errpats = [], nrls = Rule_Set.Empty}\<close>
   546 
   547 partial_function (tailrec) solve_system2 :: "bool list => real list => bool list"
   548   where
   549 "solve_system2 e_s v_s = (
   550   let
   551     e__s = (
   552       (Try (Rewrite_Set ''norm_Rational'' )) #>
   553       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System_parenthesized'' )) #>
   554       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''isolate_bdvs'' )) #>
   555       (Try (Rewrite_Set_Inst [(''bdv_1'', hd v_s), (''bdv_2'', hd (tl v_s))] ''simplify_System_parenthesized'' )) #>
   556       (Try (Rewrite_Set ''order_system'' ))
   557       ) e_s
   558   in
   559     SubProblem (''EqSystem'', [''LINEAR'', ''system''], [''no_met''])
   560       [BOOL_LIST e__s, REAL_LIST v_s])"
   561 
   562 method met_eqsys_norm_2x2 : "EqSystem/normalise/2x2" =
   563   \<open>{rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [], 
   564     srls = Rule_Set.append_rules "srls_normalise_2x2" Rule_Set.empty
   565         [\<^rule_thm>\<open>hd_thm\<close>,
   566           \<^rule_thm>\<open>tl_Cons\<close>,
   567           \<^rule_thm>\<open>tl_Nil\<close>], 
   568     prls = Rule_Set.Empty, crls = Rule_Set.Empty, errpats = [], nrls = Rule_Set.Empty}\<close>
   569   Program: solve_system2.simps
   570   Given: "equalities e_s" "solveForVars v_s"
   571   Find: "solution ss'''"
   572 
   573 partial_function (tailrec) solve_system3 :: "bool list => real list => bool list"
   574   where
   575 "solve_system3 e_s v_s = (
   576   let
   577     e__s = (
   578       (Try (Rewrite_Set ''norm_Rational'' )) #>
   579       (Repeat (Rewrite ''commute_0_equality'' )) #>
   580       (Try (Rewrite_Set_Inst [(''bdv_1'', NTH 1 v_s), (''bdv_2'', NTH 2 v_s ),
   581         (''bdv_3'', NTH 3 v_s), (''bdv_3'', NTH 4 v_s )] ''simplify_System_parenthesized'' )) #>
   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 )] ''isolate_bdvs_4x4'' )) #>
   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 )] ''simplify_System_parenthesized'' )) #>
   586       (Try (Rewrite_Set ''order_system''))
   587       )  e_s
   588   in
   589     SubProblem (''EqSystem'', [''LINEAR'', ''system''], [''no_met''])
   590       [BOOL_LIST e__s, REAL_LIST v_s])"
   591 
   592 method met_eqsys_norm_4x4 : "EqSystem/normalise/4x4" =
   593   \<open>{rew_ord'="tless_true", rls' = Rule_Set.Empty, calc = [],
   594     srls =
   595       Rule_Set.append_rules "srls_normalise_4x4" srls
   596         [\<^rule_thm>\<open>hd_thm\<close>, \<^rule_thm>\<open>tl_Cons\<close>, \<^rule_thm>\<open>tl_Nil\<close>],
   597     prls = Rule_Set.Empty, crls = Rule_Set.Empty, errpats = [], nrls = Rule_Set.Empty}\<close>
   598   Program: solve_system3.simps
   599   Given: "equalities e_s" "solveForVars v_s"
   600   Find: "solution ss'''"
   601   (*STOPPED.WN06? met ["EqSystem", "normalise", "4x4"] #>#>#>#>#>#>#>#>#>#>#>#>#>@*)
   602 
   603 partial_function (tailrec) solve_system4 :: "bool list => real list => bool list"
   604   where
   605 "solve_system4 e_s v_s = (
   606   let
   607     e_1 = NTH 1 e_s;
   608     e_2 = Take (NTH 2 e_s);
   609     e_2 = (
   610       (Substitute [e_1]) #>
   611       (Try (Rewrite_Set_Inst [(''bdv_1'',NTH 1 v_s), (''bdv_2'',NTH 2 v_s),
   612         (''bdv_3'',NTH 3 v_s), (''bdv_4'',NTH 4 v_s)] ''simplify_System_parenthesized'' )) #>
   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)] ''isolate_bdvs'' )) #>
   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)] ''norm_Rational'' ))
   617       ) e_2
   618   in
   619     [e_1, e_2, NTH 3 e_s, NTH 4 e_s])"
   620 
   621 method met_eqsys_topdown_4x4 : "EqSystem/top_down_substitution/4x4" =
   622   \<open>{rew_ord'="ord_simplify_System", rls' = Rule_Set.Empty, calc = [], 
   623     srls = Rule_Set.append_rules "srls_top_down_4x4" srls [], 
   624     prls = Rule_Set.append_rules "prls_tri_4x4_lin_sys" prls_triangular
   625         [\<^rule_eval>\<open>Prog_Expr.occurs_in\<close> (Prog_Expr.eval_occurs_in "")], 
   626     crls = Rule_Set.Empty, errpats = [], nrls = Rule_Set.Empty}\<close>
   627   (*FIXXXXME.WN060916: this script works ONLY for exp 7.79 #>#>#>#>#>#>#>#>#>#>*)
   628   Program: solve_system4.simps
   629   Given: "equalities e_s" "solveForVars v_s"
   630   Where: (*accepts missing variables up to diagonal form*)
   631     "(NTH 1 (v_s::real list)) occurs_in (NTH 1 (e_s::bool list))"
   632     "(NTH 2 (v_s::real list)) occurs_in (NTH 2 (e_s::bool list))"
   633     "(NTH 3 (v_s::real list)) occurs_in (NTH 3 (e_s::bool list))"
   634     "(NTH 4 (v_s::real list)) occurs_in (NTH 4 (e_s::bool list))"
   635   Find: "solution ss'''"
   636 
   637 ML \<open>
   638 \<close> ML \<open>
   639 \<close> ML \<open>
   640 \<close>
   641 end