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