
neuper@37906: (*. (c) by Richard Lang, 2003 .*)
neuper@37906: (* theory collecting all knowledge for LinearEquations
neuper@37906:    created by: rlang 
neuper@37906:          date: 02.10
neuper@37906:    changed by: rlang
neuper@37906:    last change by: rlang
neuper@37906:              date: 02.10.20
neuper@37906: *)
neuper@37906: 
neuper@37950: theory LinEq imports Poly Equation begin
neuper@37906: 
neuper@52148: axiomatization where
wneuper@59370: (*-- normalise --*)
neuper@37906:   (*WN0509 compare PolyEq.all_left "[|Not(b=!=0)|] ==> (a = b) = (a - b = 0)"*)
neuper@52148:   all_left:          "[|Not(b=!=0)|] ==> (a=b) = (a+(-1)*b=0)" and
neuper@52148:   makex1_x:          "a^^^1  = a" and
neuper@52148:   real_assoc_1:      "a+(b+c) = a+b+c" and
neuper@52148:   real_assoc_2:      "a*(b*c) = a*b*c" and
neuper@37906: 
neuper@37906: (*-- solve --*)
neuper@52148:   lin_isolate_add1:  "(a + b*bdv = 0) = (b*bdv = (-1)*a)" and
neuper@52148:   lin_isolate_add2:  "(a +   bdv = 0) = (  bdv = (-1)*a)" and
neuper@37982:   lin_isolate_div:   "[|Not(b=0)|] ==> (b*bdv = c) = (bdv = c / b)"
neuper@37950: 
wneuper@59472: ML \<open>
neuper@37972: val thy = @{theory};
neuper@37972: 
neuper@37950: val LinEq_prls = (*3.10.02:just the following order due to subterm evaluation*)
walther@59852:   Rule_Set.append_rules "LinEq_prls" Rule_Set.empty 
walther@59878: 	     [Rule.Eval ("HOL.eq", Prog_Expr.eval_equal "#equal_"),
walther@59878: 	      Rule.Eval ("Prog_Expr.matches", Prog_Expr.eval_matches ""),
walther@59878: 	      Rule.Eval ("Prog_Expr.lhs"    , Prog_Expr.eval_lhs ""),
walther@59878: 	      Rule.Eval ("Prog_Expr.rhs"    , Prog_Expr.eval_rhs ""),
walther@59878: 	      Rule.Eval ("Poly.has'_degree'_in", eval_has_degree_in ""),
walther@59878:  	      Rule.Eval ("Poly.is'_polyrat'_in", eval_is_polyrat_in ""),
walther@59878: 	      Rule.Eval ("Prog_Expr.occurs'_in", Prog_Expr.eval_occurs_in ""),    
walther@59878: 	      Rule.Eval ("Prog_Expr.ident", Prog_Expr.eval_ident "#ident_"),
walther@59871: 	      Rule.Thm ("not_true",ThmC.numerals_to_Free @{thm not_true}),
walther@59871: 	      Rule.Thm ("not_false",ThmC.numerals_to_Free @{thm not_false}),
walther@59871: 	      Rule.Thm ("and_true",ThmC.numerals_to_Free @{thm and_true}),
walther@59871: 	      Rule.Thm ("and_false",ThmC.numerals_to_Free @{thm and_false}),
walther@59871: 	      Rule.Thm ("or_true",ThmC.numerals_to_Free @{thm or_true}),
walther@59871: 	      Rule.Thm ("or_false",ThmC.numerals_to_Free @{thm or_false})
neuper@37950:               ];
neuper@37950: (* ----- erls ----- *)
neuper@37950: val LinEq_crls = 
walther@59852:    Rule_Set.append_rules "LinEq_crls" poly_crls
walther@59871:    [Rule.Thm  ("real_assoc_1",ThmC.numerals_to_Free @{thm real_assoc_1})
neuper@37950:     (*		
neuper@37950:      Don't use
walther@59878:      Rule.Eval ("Rings.divide_class.divide", Prog_Expr.eval_cancel "#divide_e"),
walther@59878:      Rule.Eval ("Prog_Expr.pow" , (**)eval_binop "#power_"),
neuper@37950:      *)
neuper@37950:     ];
neuper@37950: 
neuper@37950: (* ----- crls ----- *)
neuper@37950: val LinEq_erls = 
walther@59852:    Rule_Set.append_rules "LinEq_erls" Poly_erls
walther@59871:    [Rule.Thm  ("real_assoc_1",ThmC.numerals_to_Free @{thm real_assoc_1})
neuper@37950:     (*		
neuper@37950:      Don't use
walther@59878:      Rule.Eval ("Rings.divide_class.divide", Prog_Expr.eval_cancel "#divide_e"),
walther@59878:      Rule.Eval ("Prog_Expr.pow" , (**)eval_binop "#power_"),
neuper@37950:      *)
neuper@37950:     ];
wneuper@59472: \<close>
wneuper@59472: setup \<open>KEStore_Elems.add_rlss 
wneuper@59472:   [("LinEq_erls", (Context.theory_name @{theory}, LinEq_erls))]\<close>
wneuper@59472: ML \<open>
neuper@37950:     
s1210629013@55444: val LinPoly_simplify = prep_rls'(
walther@59851:   Rule_Def.Repeat {id = "LinPoly_simplify", preconds = [], 
neuper@37950:        rew_ord = ("termlessI",termlessI), 
neuper@37950:        erls = LinEq_erls, 
walther@59851:        srls = Rule_Set.Empty, 
neuper@42451:        calc = [], errpatts = [],
neuper@37950:        rules = [
walther@59871: 		Rule.Thm  ("real_assoc_1",ThmC.numerals_to_Free @{thm real_assoc_1}),
walther@59878: 		Rule.Eval ("Groups.plus_class.plus", (**)eval_binop "#add_"),
walther@59878: 		Rule.Eval ("Groups.minus_class.minus", (**)eval_binop "#sub_"),
walther@59878: 		Rule.Eval ("Groups.times_class.times", (**)eval_binop "#mult_"),
neuper@37950: 		(*  Dont use  
walther@59878: 		 Rule.Eval ("Rings.divide_class.divide", Prog_Expr.eval_cancel "#divide_e"),		
walther@59878: 		 Rule.Eval ("NthRoot.sqrt", eval_sqrt "#sqrt_"),
neuper@37950: 		 *)
walther@59878: 		Rule.Eval ("Prog_Expr.pow" , (**)eval_binop "#power_")
neuper@37950: 		],
walther@59878:        scr = Rule.Empty_Prog});
wneuper@59472: \<close>
wneuper@59472: setup \<open>KEStore_Elems.add_rlss 
wneuper@59472:   [("LinPoly_simplify", (Context.theory_name @{theory}, LinPoly_simplify))]\<close>
wneuper@59472: ML \<open>
neuper@37950: 
neuper@37950: (*isolate the bound variable in an linear equation; 'bdv' is a meta-constant*)
s1210629013@55444: val LinEq_simplify = prep_rls'(
walther@59851: Rule_Def.Repeat {id = "LinEq_simplify", preconds = [],
walther@59857:      rew_ord = ("e_rew_ord", Rewrite_Ord.e_rew_ord),
neuper@37950:      erls = LinEq_erls,
walther@59851:      srls = Rule_Set.Empty,
neuper@42451:      calc = [], errpatts = [],
neuper@37950:      rules = [
walther@59871: 	      Rule.Thm("lin_isolate_add1",ThmC.numerals_to_Free @{thm lin_isolate_add1}), 
neuper@37950: 	      (* a+bx=0 -> bx=-a *)
walther@59871: 	      Rule.Thm("lin_isolate_add2",ThmC.numerals_to_Free @{thm lin_isolate_add2}), 
neuper@37950: 	      (* a+ x=0 ->  x=-a *)
walther@59871: 	      Rule.Thm("lin_isolate_div",ThmC.numerals_to_Free @{thm lin_isolate_div})    
neuper@37950: 	      (*   bx=c -> x=c/b *)  
neuper@37950: 	      ],
walther@59878:      scr = Rule.Empty_Prog});
wneuper@59472: \<close>
wneuper@59472: setup \<open>KEStore_Elems.add_rlss 
wneuper@59472:   [("LinEq_simplify", (Context.theory_name @{theory}, LinEq_simplify))]\<close>
neuper@37950: 
neuper@37950: (*----------------------------- problem types --------------------------------*)
neuper@37950: (* ---------linear----------- *)
wneuper@59472: setup \<open>KEStore_Elems.add_pbts
walther@59973:   [(Problem.prep_input thy "pbl_equ_univ_lin" [] Problem.id_empty
s1210629013@55339:       (["LINEAR", "univariate", "equation"],
s1210629013@55339:         [("#Given" ,["equality e_e", "solveFor v_v"]),
s1210629013@55339:           ("#Where" ,["HOL.False", (*WN0509 just detected: this pbl can never be used?!?*)
s1210629013@55339:               "Not( (lhs e_e) is_polyrat_in v_v)",
s1210629013@55339:               "Not( (rhs e_e) is_polyrat_in v_v)",
s1210629013@55339:               "((lhs e_e) has_degree_in v_v)=1",
s1210629013@55339: 	            "((rhs e_e) has_degree_in v_v)=1"]),
s1210629013@55339:           ("#Find"  ,["solutions v_v'i'"])],
wneuper@59472:         LinEq_prls, SOME "solve (e_e::bool, v_v)", [["LinEq", "solve_lineq_equation"]]))]\<close>
neuper@37950: 
s1210629013@55373: (*-------------- methods------------------------------------------------------*)
wneuper@59472: setup \<open>KEStore_Elems.add_mets
walther@59973:     [Method.prep_input thy "met_eqlin" [] Method.id_empty
s1210629013@55373:       (["LinEq"], [],
walther@59852:         {rew_ord' = "tless_true",rls' = Atools_erls,calc = [], srls = Rule_Set.empty, prls = Rule_Set.empty,
s1210629013@55373:           crls = LinEq_crls, errpats = [], nrls = norm_Poly},
wneuper@59545:         @{thm refl})]
wneuper@59473: \<close>
s1210629013@55373:     (* ansprechen mit ["LinEq","solve_univar_equation"] *)
wneuper@59545: 
wneuper@59504: partial_function (tailrec) solve_linear_equation :: "bool \<Rightarrow> real \<Rightarrow> bool list"
wneuper@59504:   where
walther@59635: "solve_linear_equation e_e v_v = (
walther@59635:   let
walther@59635:     e_e = (
walther@59637:       (Try (Rewrite ''all_left'')) #>
walther@59637:       (Try (Repeat (Rewrite ''makex1_x''))) #>
walther@59637:       (Try (Rewrite_Set ''expand_binoms'')) #>
walther@59637:       (Try (Repeat (Rewrite_Set_Inst [(''bdv'', v_v)] ''make_ratpoly_in''))) #>
walther@59635:       (Try (Repeat (Rewrite_Set ''LinPoly_simplify''))) ) e_e;
walther@59635:     e_e = (
walther@59637:       (Try (Rewrite_Set_Inst [(''bdv'', v_v)] ''LinEq_simplify'')) #>
walther@59635:       (Repeat (Try (Rewrite_Set ''LinPoly_simplify''))) ) e_e
walther@59635:   in
walther@59635:     Or_to_List e_e)"
wneuper@59473: setup \<open>KEStore_Elems.add_mets
walther@59973:     [Method.prep_input thy "met_eq_lin" [] Method.id_empty
s1210629013@55373:       (["LinEq","solve_lineq_equation"],
s1210629013@55373:         [("#Given", ["equality e_e", "solveFor v_v"]),
s1210629013@55373:           ("#Where", ["Not ((lhs e_e) is_polyrat_in v_v)", "((lhs e_e)  has_degree_in v_v) = 1"]),
s1210629013@55373:           ("#Find",  ["solutions v_v'i'"])],
walther@59852:         {rew_ord' = "termlessI", rls' = LinEq_erls, srls = Rule_Set.empty, prls = LinEq_prls, calc = [],
s1210629013@55373:           crls = LinEq_crls, errpats = [], nrls = norm_Poly},
wneuper@59551:         @{thm solve_linear_equation.simps})]
wneuper@59472: \<close>
walther@59970: ML \<open>Method.from_store' @{theory} ["LinEq","solve_lineq_equation"];\<close>
neuper@37950: 
neuper@37906: end
neuper@37906: 




