src/Tools/isac/Knowledge/EqSystem.thy
author Walther Neuper <walther.neuper@jku.at>
Mon, 07 Dec 2020 17:39:21 +0100
changeset 60121 e6cd6dd07d7a
parent 60023 113997e55e71
child 60154 2ab0d1523731
permissions -rw-r--r--
step 2 of integration: interrupted

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