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