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