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