src/Tools/isac/Knowledge/RatEq.thy
author wneuper <walther.neuper@jku.at>
Fri, 07 May 2021 18:12:51 +0200
changeset 60278 343efa173023
parent 60260 6a3b143d4cf4
child 60286 31efa1b39a20
permissions -rw-r--r--
* WN: simplify const names like "is'_expanded"
neuper@37906
     1
(*.(c) by Richard Lang, 2003 .*)
neuper@37906
     2
(* theory collecting all knowledge for RationalEquations
neuper@37906
     3
   created by: rlang 
neuper@37906
     4
         date: 02.08.12
neuper@37906
     5
   changed by: rlang
neuper@37906
     6
   last change by: rlang
neuper@37906
     7
             date: 02.11.28
neuper@37906
     8
*)
neuper@37906
     9
walther@59817
    10
theory RatEq imports Rational LinEq begin
neuper@37906
    11
wneuper@59472
    12
text \<open>univariate equations over multivariate rational terms:
neuper@42398
    13
  In 2003 this type has been integrated into ISAC's equation solver
neuper@42398
    14
  by Richard Lang; the root for the solver is Equation.thy.
neuper@42398
    15
  The migration Isabelle2002 --> 2011 found that application of theorems like 
neuper@42398
    16
  rat_mult_denominator_right: "[|Not(d=0)|] ==> ((a::real) = c / d) = (a*d = c)"
neuper@42398
    17
  in rule-sets does not transfer "d ~= 0" to the assumptions; see
neuper@42398
    18
  test --- repair NO asms from rls RatEq_eliminate ---.
neuper@42398
    19
  Thus the migration dropped update of Check_elementwise, which would require
neuper@42398
    20
  these assumptions; see 
neuper@42398
    21
  test --- pbl: rational, univariate, equation ---, --- x / (x ^ 2 - 6 * x + 9) - 1 /...
wneuper@59472
    22
\<close>
neuper@42398
    23
wneuper@59526
    24
subsection \<open>consts definition for predicates in specifications\<close>
neuper@37906
    25
consts
walther@60278
    26
  is_ratequation_in :: "[bool, real] => bool" ("_ is'_ratequation'_in _")
neuper@37906
    27
wneuper@59526
    28
subsection \<open>theorems not yet adopted from Isabelle\<close>
neuper@52148
    29
axiomatization where
neuper@37906
    30
   (* FIXME also in Poly.thy def. --> FIXED*)
neuper@37906
    31
   (*real_diff_minus            
neuper@37906
    32
   "a - b = a + (-1) * b"*)
neuper@52148
    33
   real_rat_mult_1:   "a*(b/c) = (a*b)/c" and
neuper@52148
    34
   real_rat_mult_2:   "(a/b)*(c/d) = (a*c)/(b*d)" and
neuper@52148
    35
   real_rat_mult_3:   "(a/b)*c = (a*c)/b" and
walther@60242
    36
   real_rat_pow:      "(a/b) \<up> 2 = a \<up> 2/b \<up> 2" and
neuper@37906
    37
neuper@52148
    38
   rat_double_rat_1:   "[|Not(c=0); Not(d=0)|] ==> (a / (c/d) = (a*d) / c)" and
neuper@37983
    39
   rat_double_rat_2:   "[|Not(b=0);Not(c=0); Not(d=0)|] ==> 
neuper@52148
    40
                                           ((a/b) / (c/d) = (a*d) / (b*c))" and
neuper@52148
    41
   rat_double_rat_3:   "[|Not(b=0);Not(c=0)|] ==> ((a/b) / c = a / (b*c))" and
neuper@37906
    42
neuper@37906
    43
  (* equation to same denominator *)
neuper@37983
    44
  rat_mult_denominator_both:
neuper@52148
    45
   "[|Not(b=0); Not(d=0)|] ==> ((a::real) / b = c / d) = (a*d = c*b)" and
neuper@37983
    46
  rat_mult_denominator_left:
neuper@52148
    47
   "[|Not(d=0)|] ==> ((a::real) = c / d) = (a*d = c)" and
neuper@37983
    48
  rat_mult_denominator_right:
neuper@37906
    49
   "[|Not(b=0)|] ==> ((a::real) / b = c) = (a = c*b)"
neuper@37906
    50
wneuper@59526
    51
subsection \<open>predicates\<close>
wneuper@59472
    52
ML \<open>
neuper@37972
    53
val thy = @{theory};
neuper@37972
    54
neuper@37954
    55
(*-------------------------functions-----------------------*)
neuper@37954
    56
(* is_rateqation_in becomes true, if a bdv is in the denominator of a fraction*)
neuper@37954
    57
fun is_rateqation_in t v = 
wneuper@59526
    58
  let 
wneuper@59526
    59
   	fun finddivide (t as (_ $ _ $ _ $ _)) _ = raise TERM ("is_rateqation_in", [t]) 
neuper@37954
    60
	    (* at the moment there is no term like this, but ....*)
walther@59603
    61
	  | finddivide (Const ("Rings.divide_class.divide",_) $ _ $ b) v = Prog_Expr.occurs_in v b
wneuper@59526
    62
	  | finddivide (_ $ t1 $ t2) v = finddivide t1 v orelse finddivide t2 v
wneuper@59526
    63
	  | finddivide (_ $ t1) v = finddivide t1 v
neuper@37954
    64
	  | finddivide _ _ = false;
wneuper@59526
    65
  in
wneuper@59526
    66
	  finddivide t v
wneuper@59526
    67
  end;
wneuper@59526
    68
\<close>
wneuper@59526
    69
wneuper@59526
    70
subsection \<open>evaluations functions\<close>
wneuper@59526
    71
ML \<open>
walther@60278
    72
fun eval_is_ratequation_in _ _ (p as (Const ("RatEq.is_ratequation_in",_) $ t $ v)) _ =
wneuper@59526
    73
    if is_rateqation_in t v
walther@59868
    74
    then SOME ((UnparseC.term p) ^ " = True", HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
walther@59868
    75
    else SOME ((UnparseC.term p) ^ " = True", HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
neuper@38015
    76
  | eval_is_ratequation_in _ _ _ _ = ((*tracing"### nichts matcht";*) NONE);
wneuper@59526
    77
\<close>
wneuper@59526
    78
setup \<open>KEStore_Elems.add_calcs
wneuper@59526
    79
  [("is_ratequation_in", ("RatEq.is_ratequation_in", eval_is_ratequation_in ""))]\<close>
neuper@37954
    80
wneuper@59526
    81
subsection \<open>rule-sets\<close>
wneuper@59526
    82
ML \<open>
neuper@37954
    83
val RatEq_prls = (*15.10.02:just the following order due to subterm evaluation*)
walther@59852
    84
  Rule_Set.append_rules "RatEq_prls" Rule_Set.empty 
walther@59878
    85
	     [Rule.Eval ("Prog_Expr.ident", Prog_Expr.eval_ident "#ident_"),
walther@59878
    86
	      Rule.Eval ("Prog_Expr.matches", Prog_Expr.eval_matches ""),
walther@59878
    87
	      Rule.Eval ("Prog_Expr.lhs", Prog_Expr.eval_lhs ""),
walther@59878
    88
	      Rule.Eval ("Prog_Expr.rhs", Prog_Expr.eval_rhs ""),
walther@60278
    89
	      Rule.Eval ("RatEq.is_ratequation_in", eval_is_ratequation_in ""),
walther@59878
    90
	      Rule.Eval ("HOL.eq", Prog_Expr.eval_equal "#equal_"),
walther@59871
    91
	      Rule.Thm ("not_true",ThmC.numerals_to_Free @{thm not_true}),
walther@59871
    92
	      Rule.Thm ("not_false",ThmC.numerals_to_Free @{thm not_false}),
walther@59871
    93
	      Rule.Thm ("and_true",ThmC.numerals_to_Free @{thm and_true}),
walther@59871
    94
	      Rule.Thm ("and_false",ThmC.numerals_to_Free @{thm and_false}),
walther@59871
    95
	      Rule.Thm ("or_true",ThmC.numerals_to_Free @{thm or_true}),
walther@59871
    96
	      Rule.Thm ("or_false",ThmC.numerals_to_Free @{thm or_false})
neuper@37954
    97
	      ];
neuper@37954
    98
walther@59603
    99
\<close> ML \<open>
walther@59852
   100
(*rls = Rule_Set.merge erls Poly_erls *)
neuper@37954
   101
val rateq_erls = 
walther@59852
   102
  Rule_Set.keep_unique_rules "rateq_erls"                             (*WN: ein Hack*)
walther@59852
   103
	  (Rule_Set.merge "is_ratequation_in" calculate_Rational
walther@59852
   104
		  (Rule_Set.append_rules "is_ratequation_in"
walther@59878
   105
			  Poly_erls [(*Rule.Eval ("Rings.divide_class.divide", Prog_Expr.eval_cancel "#divide_e"),*)
walther@60278
   106
			    Rule.Eval ("RatEq.is_ratequation_in", eval_is_ratequation_in "")]))
walther@59871
   107
 [Rule.Thm ("and_commute",ThmC.numerals_to_Free @{thm and_commute}), (*WN: ein Hack*)
walther@59871
   108
	Rule.Thm ("or_commute",ThmC.numerals_to_Free @{thm or_commute})];  (*WN: ein Hack*)
neuper@37954
   109
wneuper@59526
   110
\<close> ML \<open>
neuper@37954
   111
val RatEq_crls = 
walther@59852
   112
  Rule_Set.keep_unique_rules "RatEq_crls"                              (*WN: ein Hack*)
walther@59852
   113
	  (Rule_Set.merge "is_ratequation_in" calculate_Rational
walther@59852
   114
		  (Rule_Set.append_rules "is_ratequation_in"
walther@59878
   115
			  Poly_erls [(*Rule.Eval ("Rings.divide_class.divide", Prog_Expr.eval_cancel "#divide_e"),*)
walther@60278
   116
			    Rule.Eval ("RatEq.is_ratequation_in", eval_is_ratequation_in "")]))
walther@59871
   117
 [Rule.Thm ("and_commute",ThmC.numerals_to_Free @{thm and_commute}), (*WN: ein Hack*)
walther@59871
   118
	Rule.Thm ("or_commute",ThmC.numerals_to_Free @{thm or_commute})];  (*WN: ein Hack*)
neuper@37954
   119
s1210629013@55444
   120
val RatEq_eliminate = prep_rls'(
walther@59851
   121
  Rule_Def.Repeat
wneuper@59526
   122
    {id = "RatEq_eliminate", preconds = [], rew_ord = ("termlessI", termlessI), erls = rateq_erls,
walther@59851
   123
     srls = Rule_Set.Empty, calc = [], errpatts = [],
wneuper@59526
   124
     rules = [
walther@59871
   125
	     Rule.Thm("rat_mult_denominator_both",ThmC.numerals_to_Free @{thm rat_mult_denominator_both}), 
neuper@37954
   126
	     (* a/b=c/d -> ad=cb *)
walther@59871
   127
	     Rule.Thm("rat_mult_denominator_left",ThmC.numerals_to_Free @{thm rat_mult_denominator_left}), 
neuper@37954
   128
	     (* a  =c/d -> ad=c  *)
walther@59871
   129
	     Rule.Thm("rat_mult_denominator_right",ThmC.numerals_to_Free @{thm rat_mult_denominator_right})
neuper@37954
   130
	     (* a/b=c   ->  a=cb *)
wneuper@59526
   131
	     ],
walther@59878
   132
    scr = Rule.Empty_Prog});
neuper@37954
   133
wneuper@59526
   134
\<close> ML \<open>
s1210629013@55444
   135
val RatEq_simplify = prep_rls'(
walther@59851
   136
  Rule_Def.Repeat
wneuper@59526
   137
    {id = "RatEq_simplify", preconds = [], rew_ord = ("termlessI", termlessI), erls = rateq_erls,
walther@59851
   138
     srls = Rule_Set.Empty, calc = [], errpatts = [],
wneuper@59526
   139
     rules = [
walther@59871
   140
	     Rule.Thm("real_rat_mult_1",ThmC.numerals_to_Free @{thm real_rat_mult_1}),
neuper@37954
   141
	     (*a*(b/c) = (a*b)/c*)
walther@59871
   142
	     Rule.Thm("real_rat_mult_2",ThmC.numerals_to_Free @{thm real_rat_mult_2}),
neuper@37954
   143
	     (*(a/b)*(c/d) = (a*c)/(b*d)*)
walther@59871
   144
       Rule.Thm("real_rat_mult_3",ThmC.numerals_to_Free @{thm real_rat_mult_3}),
wneuper@59526
   145
       (* (a/b)*c = (a*c)/b*)
walther@59871
   146
	     Rule.Thm("real_rat_pow",ThmC.numerals_to_Free @{thm real_rat_pow}),
walther@60260
   147
	     (*(a/b) \<up> 2 = a \<up> 2/b \<up> 2*)
walther@59871
   148
	     Rule.Thm("real_diff_minus",ThmC.numerals_to_Free @{thm real_diff_minus}),
neuper@37954
   149
	     (* a - b = a + (-1) * b *)
walther@59871
   150
       Rule.Thm("rat_double_rat_1",ThmC.numerals_to_Free @{thm rat_double_rat_1}),
wneuper@59526
   151
       (* (a / (c/d) = (a*d) / c) *)
walther@59871
   152
       Rule.Thm("rat_double_rat_2",ThmC.numerals_to_Free @{thm rat_double_rat_2}), 
wneuper@59526
   153
       (* ((a/b) / (c/d) = (a*d) / (b*c)) *)
walther@59871
   154
       Rule.Thm("rat_double_rat_3",ThmC.numerals_to_Free @{thm rat_double_rat_3}) 
wneuper@59526
   155
       (* ((a/b) / c = a / (b*c) ) *)],
walther@59878
   156
     scr = Rule.Empty_Prog});
wneuper@59472
   157
\<close>
wneuper@59526
   158
setup \<open>KEStore_Elems.add_rlss [("rateq_erls", (Context.theory_name @{theory}, rateq_erls))]\<close>
wneuper@59526
   159
setup \<open>KEStore_Elems.add_rlss [("RatEq_eliminate", (Context.theory_name @{theory}, RatEq_eliminate))]\<close>
wneuper@59526
   160
setup \<open>KEStore_Elems.add_rlss [("RatEq_simplify", (Context.theory_name @{theory}, RatEq_simplify))]\<close>
wneuper@59526
   161
wneuper@59526
   162
subsection \<open>problems\<close>
wneuper@59472
   163
setup \<open>KEStore_Elems.add_pbts
walther@59973
   164
  [(Problem.prep_input thy "pbl_equ_univ_rat" [] Problem.id_empty
walther@59997
   165
    (["rational", "univariate", "equation"],
walther@59997
   166
      [("#Given", ["equality e_e", "solveFor v_v"]),
s1210629013@55339
   167
        ("#Where", ["(e_e::bool) is_ratequation_in (v_v::real)"]),
s1210629013@55339
   168
        ("#Find", ["solutions v_v'i'"])],
walther@59997
   169
      RatEq_prls, SOME "solve (e_e::bool, v_v)", [["RatEq", "solve_rat_equation"]]))]\<close>
neuper@37954
   170
wneuper@59526
   171
subsection \<open>methods\<close>
wneuper@59472
   172
setup \<open>KEStore_Elems.add_mets
walther@60154
   173
    [MethodC.prep_input thy "met_rateq" [] MethodC.id_empty
s1210629013@55373
   174
      (["RatEq"], [],
walther@59852
   175
        {rew_ord'="tless_true",rls'=Atools_erls,calc = [], srls = Rule_Set.empty, prls=Rule_Set.empty,
wneuper@59545
   176
          crls=RatEq_crls, errpats = [], nrls = norm_Rational}, @{thm refl})]\<close>
wneuper@59545
   177
wneuper@59504
   178
partial_function (tailrec) solve_rational_equ :: "bool \<Rightarrow> real \<Rightarrow> bool list"
wneuper@59504
   179
  where
wneuper@59504
   180
"solve_rational_equ e_e v_v =
walther@59635
   181
  (let
walther@59635
   182
    e_e = (
walther@59637
   183
      (Repeat (Try (Rewrite_Set ''RatEq_simplify''))) #>
walther@59637
   184
      (Repeat (Try (Rewrite_Set ''norm_Rational''))) #>
walther@59637
   185
      (Repeat (Try (Rewrite_Set ''add_fractions_p''))) #>
walther@59635
   186
      (Repeat (Try (Rewrite_Set ''RatEq_eliminate''))) ) e_e;
walther@59635
   187
    L_L = SubProblem (''RatEq'', [''univariate'', ''equation''], [''no_met'']) [BOOL e_e, REAL v_v]
walther@59635
   188
  in
walther@59635
   189
    Check_elementwise L_L {(v_v::real). Assumptions})"
wneuper@59473
   190
setup \<open>KEStore_Elems.add_mets
walther@60154
   191
    [MethodC.prep_input thy "met_rat_eq" [] MethodC.id_empty
s1210629013@55373
   192
      (["RatEq", "solve_rat_equation"],
walther@59997
   193
        [("#Given" ,["equality e_e", "solveFor v_v"]),
s1210629013@55373
   194
          ("#Where" ,["(e_e::bool) is_ratequation_in (v_v::real)"]),
s1210629013@55373
   195
          ("#Find"  ,["solutions v_v'i'"])],
walther@59852
   196
        {rew_ord'="termlessI", rls'=rateq_erls, srls=Rule_Set.empty, prls=RatEq_prls, calc=[],
s1210629013@55373
   197
          crls=RatEq_crls, errpats = [], nrls = norm_Rational},
wneuper@59551
   198
        @{thm solve_rational_equ.simps})]
wneuper@59472
   199
\<close>
wneuper@59526
   200
ML \<open>
wneuper@59526
   201
\<close> ML \<open>
walther@59817
   202
\<close> ML \<open>
wneuper@59526
   203
\<close> 
neuper@37906
   204
end