src/Tools/isac/Knowledge/RatEq.thy
author Mathias Lehnfeld <s1210629013@students.fh-hagenberg.at>
Mon, 27 Jan 2014 22:26:51 +0100
changeset 55363 d78bc1342183
parent 55359 73dc85c025ab
child 55373 4f3f530f3cf6
permissions -rw-r--r--
ad 967c8a1eb6b1 (7): removed all code concerned with 'ptyps = Unsynchronized.ref'
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
s1210629013@55339
    10
theory RatEq imports Rational Equation begin
neuper@37906
    11
neuper@42398
    12
text {* 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 /...
neuper@42398
    22
*}
neuper@42398
    23
neuper@37906
    24
consts
neuper@37906
    25
neuper@37906
    26
  is'_ratequation'_in :: "[bool, real] => bool" ("_ is'_ratequation'_in _")
neuper@37906
    27
neuper@37906
    28
  (*----------------------scripts-----------------------*)
neuper@37906
    29
  Solve'_rat'_equation
neuper@37954
    30
             :: "[bool,real, 
neuper@37954
    31
		   bool list] => bool list"
neuper@37954
    32
               ("((Script Solve'_rat'_equation (_ _ =))// 
neuper@37954
    33
                  (_))" 9)
neuper@37906
    34
neuper@52148
    35
axiomatization where
neuper@37906
    36
   (* FIXME also in Poly.thy def. --> FIXED*)
neuper@37906
    37
   (*real_diff_minus            
neuper@37906
    38
   "a - b = a + (-1) * b"*)
neuper@52148
    39
   real_rat_mult_1:   "a*(b/c) = (a*b)/c" and
neuper@52148
    40
   real_rat_mult_2:   "(a/b)*(c/d) = (a*c)/(b*d)" and
neuper@52148
    41
   real_rat_mult_3:   "(a/b)*c = (a*c)/b" and
neuper@52148
    42
   real_rat_pow:      "(a/b)^^^2 = a^^^2/b^^^2" and
neuper@37906
    43
neuper@52148
    44
   rat_double_rat_1:   "[|Not(c=0); Not(d=0)|] ==> (a / (c/d) = (a*d) / c)" and
neuper@37983
    45
   rat_double_rat_2:   "[|Not(b=0);Not(c=0); Not(d=0)|] ==> 
neuper@52148
    46
                                           ((a/b) / (c/d) = (a*d) / (b*c))" and
neuper@52148
    47
   rat_double_rat_3:   "[|Not(b=0);Not(c=0)|] ==> ((a/b) / c = a / (b*c))" and
neuper@37906
    48
neuper@37906
    49
  (* equation to same denominator *)
neuper@37983
    50
  rat_mult_denominator_both:
neuper@52148
    51
   "[|Not(b=0); Not(d=0)|] ==> ((a::real) / b = c / d) = (a*d = c*b)" and
neuper@37983
    52
  rat_mult_denominator_left:
neuper@52148
    53
   "[|Not(d=0)|] ==> ((a::real) = c / d) = (a*d = c)" and
neuper@37983
    54
  rat_mult_denominator_right:
neuper@37906
    55
   "[|Not(b=0)|] ==> ((a::real) / b = c) = (a = c*b)"
neuper@37906
    56
neuper@37954
    57
ML {*
neuper@37972
    58
val thy = @{theory};
neuper@37972
    59
neuper@37954
    60
(*-------------------------functions-----------------------*)
neuper@37954
    61
(* is_rateqation_in becomes true, if a bdv is in the denominator of a fraction*)
neuper@37954
    62
fun is_rateqation_in t v = 
neuper@37954
    63
    let 
neuper@37954
    64
	fun coeff_in c v = member op = (vars c) v;
neuper@38031
    65
   	fun finddivide (_ $ _ $ _ $ _) v = error("is_rateqation_in:")
neuper@37954
    66
	    (* at the moment there is no term like this, but ....*)
neuper@48789
    67
	  | finddivide (t as (Const ("Fields.inverse_class.divide",_) $ _ $ b)) v = coeff_in b v
neuper@37954
    68
	  | finddivide (_ $ t1 $ t2) v = (finddivide t1 v) 
neuper@37954
    69
                                         orelse (finddivide t2 v)
neuper@37954
    70
	  | finddivide (_ $ t1) v = (finddivide t1 v)
neuper@37954
    71
	  | finddivide _ _ = false;
neuper@37954
    72
     in
neuper@37954
    73
	finddivide t v
neuper@37954
    74
    end;
neuper@37954
    75
    
neuper@37954
    76
fun eval_is_ratequation_in _ _ 
neuper@37954
    77
       (p as (Const ("RatEq.is'_ratequation'_in",_) $ t $ v)) _  =
neuper@37954
    78
    if is_rateqation_in t v then 
neuper@37954
    79
	SOME ((term2str p) ^ " = True",
neuper@48760
    80
	      Trueprop $ (mk_equality (p, @{term True})))
neuper@37954
    81
    else SOME ((term2str p) ^ " = True",
neuper@48760
    82
	       Trueprop $ (mk_equality (p, @{term False})))
neuper@38015
    83
  | eval_is_ratequation_in _ _ _ _ = ((*tracing"### nichts matcht";*) NONE);
neuper@37954
    84
neuper@37954
    85
(*-------------------------rulse-----------------------*)
neuper@37954
    86
val RatEq_prls = (*15.10.02:just the following order due to subterm evaluation*)
neuper@37954
    87
  append_rls "RatEq_prls" e_rls 
neuper@37954
    88
	     [Calc ("Atools.ident",eval_ident "#ident_"),
neuper@37954
    89
	      Calc ("Tools.matches",eval_matches ""),
neuper@37954
    90
	      Calc ("Tools.lhs"    ,eval_lhs ""),
neuper@37954
    91
	      Calc ("Tools.rhs"    ,eval_rhs ""),
neuper@37954
    92
	      Calc ("RatEq.is'_ratequation'_in",eval_is_ratequation_in ""),
neuper@41922
    93
	      Calc ("HOL.eq",eval_equal "#equal_"),
neuper@37969
    94
	      Thm ("not_true",num_str @{thm not_true}),
neuper@37969
    95
	      Thm ("not_false",num_str @{thm not_false}),
neuper@37969
    96
	      Thm ("and_true",num_str @{thm and_true}),
neuper@37969
    97
	      Thm ("and_false",num_str @{thm and_false}),
neuper@37969
    98
	      Thm ("or_true",num_str @{thm or_true}),
neuper@37969
    99
	      Thm ("or_false",num_str @{thm or_false})
neuper@37954
   100
	      ];
neuper@37954
   101
neuper@37954
   102
neuper@37954
   103
(*rls = merge_rls erls Poly_erls *)
neuper@37954
   104
val rateq_erls = 
neuper@37954
   105
    remove_rls "rateq_erls"                                   (*WN: ein Hack*)
neuper@37954
   106
	(merge_rls "is_ratequation_in" calculate_Rational
neuper@37954
   107
		   (append_rls "is_ratequation_in"
neuper@37954
   108
			Poly_erls
neuper@48789
   109
			[(*Calc ("Fields.inverse_class.divide", eval_cancel "#divide_e"),*)
neuper@37954
   110
			 Calc ("RatEq.is'_ratequation'_in",
neuper@37954
   111
			       eval_is_ratequation_in "")
neuper@37954
   112
neuper@37954
   113
			 ]))
neuper@37969
   114
	[Thm ("and_commute",num_str @{thm and_commute}), (*WN: ein Hack*)
neuper@37969
   115
	 Thm ("or_commute",num_str @{thm or_commute})    (*WN: ein Hack*)
neuper@37954
   116
	 ];
neuper@52125
   117
*}
neuper@52125
   118
setup {* KEStore_Elems.add_rlss [("rateq_erls", (Context.theory_name @{theory}, rateq_erls))] *}
neuper@52125
   119
ML {*
neuper@37954
   120
neuper@37954
   121
val RatEq_crls = 
neuper@37954
   122
    remove_rls "RatEq_crls"                                   (*WN: ein Hack*)
neuper@37954
   123
	(merge_rls "is_ratequation_in" calculate_Rational
neuper@37954
   124
		   (append_rls "is_ratequation_in"
neuper@37954
   125
			Poly_erls
neuper@48789
   126
			[(*Calc ("Fields.inverse_class.divide", eval_cancel "#divide_e"),*)
neuper@37954
   127
			 Calc ("RatEq.is'_ratequation'_in",
neuper@37954
   128
			       eval_is_ratequation_in "")
neuper@37954
   129
			 ]))
neuper@37969
   130
	[Thm ("and_commute",num_str @{thm and_commute}), (*WN: ein Hack*)
neuper@37969
   131
	 Thm ("or_commute",num_str @{thm or_commute})    (*WN: ein Hack*)
neuper@37954
   132
	 ];
neuper@37954
   133
neuper@37954
   134
val RatEq_eliminate = prep_rls(
neuper@37954
   135
  Rls {id = "RatEq_eliminate", preconds = [],
neuper@37954
   136
       rew_ord = ("termlessI", termlessI), erls = rateq_erls, srls = Erls, 
neuper@42451
   137
       calc = [], errpatts = [],
neuper@37954
   138
       rules = [
neuper@37969
   139
	    Thm("rat_mult_denominator_both",num_str @{thm rat_mult_denominator_both}), 
neuper@37954
   140
	     (* a/b=c/d -> ad=cb *)
neuper@37969
   141
	    Thm("rat_mult_denominator_left",num_str @{thm rat_mult_denominator_left}), 
neuper@37954
   142
	     (* a  =c/d -> ad=c  *)
neuper@37969
   143
	    Thm("rat_mult_denominator_right",num_str @{thm rat_mult_denominator_right})
neuper@37954
   144
	     (* a/b=c   ->  a=cb *)
neuper@37954
   145
	    ],
neuper@48763
   146
    scr = Prog ((term_of o the o (parse thy)) "empty_script")
neuper@37954
   147
    }:rls);
neuper@52125
   148
*}
neuper@52125
   149
setup {* KEStore_Elems.add_rlss [("RatEq_eliminate",
neuper@52125
   150
  (Context.theory_name @{theory}, RatEq_eliminate))] *}
neuper@52125
   151
ML {*
neuper@37954
   152
neuper@37954
   153
val RatEq_simplify = prep_rls(
neuper@37954
   154
  Rls {id = "RatEq_simplify", preconds = [], rew_ord = ("termlessI", termlessI),
neuper@42451
   155
      erls = rateq_erls, srls = Erls, calc = [], errpatts = [],
neuper@37954
   156
    rules = [
neuper@37969
   157
	     Thm("real_rat_mult_1",num_str @{thm real_rat_mult_1}),
neuper@37954
   158
	     (*a*(b/c) = (a*b)/c*)
neuper@37969
   159
	     Thm("real_rat_mult_2",num_str @{thm real_rat_mult_2}),
neuper@37954
   160
	     (*(a/b)*(c/d) = (a*c)/(b*d)*)
neuper@37969
   161
             Thm("real_rat_mult_3",num_str @{thm real_rat_mult_3}),
neuper@37954
   162
             (* (a/b)*c = (a*c)/b*)
neuper@37969
   163
	     Thm("real_rat_pow",num_str @{thm real_rat_pow}),
neuper@37954
   164
	     (*(a/b)^^^2 = a^^^2/b^^^2*)
neuper@37969
   165
	     Thm("real_diff_minus",num_str @{thm real_diff_minus}),
neuper@37954
   166
	     (* a - b = a + (-1) * b *)
neuper@37969
   167
             Thm("rat_double_rat_1",num_str @{thm rat_double_rat_1}),
neuper@37954
   168
             (* (a / (c/d) = (a*d) / c) *)
neuper@37969
   169
             Thm("rat_double_rat_2",num_str @{thm rat_double_rat_2}), 
neuper@37954
   170
             (* ((a/b) / (c/d) = (a*d) / (b*c)) *)
neuper@37969
   171
             Thm("rat_double_rat_3",num_str @{thm rat_double_rat_3}) 
neuper@37954
   172
             (* ((a/b) / c = a / (b*c) ) *)
neuper@37954
   173
	     ],
neuper@48763
   174
    scr = Prog ((term_of o the o (parse thy)) "empty_script")
neuper@37954
   175
    }:rls);
neuper@52125
   176
*}
neuper@52125
   177
setup {* KEStore_Elems.add_rlss [("RatEq_simplify",
neuper@52125
   178
  (Context.theory_name @{theory}, RatEq_simplify))] *}
neuper@52125
   179
ML {*
neuper@37954
   180
(*-------------------------Problem-----------------------*)
neuper@37954
   181
(*
neuper@37954
   182
(get_pbt ["rational","univariate","equation"]);
neuper@37954
   183
show_ptyps(); 
neuper@37954
   184
*)
neuper@37988
   185
*}
s1210629013@55359
   186
setup {* KEStore_Elems.add_pbts
s1210629013@55339
   187
  [(prep_pbt thy "pbl_equ_univ_rat" [] e_pblID
s1210629013@55339
   188
    (["rational","univariate","equation"],
s1210629013@55339
   189
      [("#Given", ["equality e_e","solveFor v_v"]),
s1210629013@55339
   190
        ("#Where", ["(e_e::bool) is_ratequation_in (v_v::real)"]),
s1210629013@55339
   191
        ("#Find", ["solutions v_v'i'"])],
s1210629013@55339
   192
      RatEq_prls, SOME "solve (e_e::bool, v_v)", [["RatEq","solve_rat_equation"]]))] *}
neuper@37954
   193
neuper@37988
   194
ML {*
neuper@37954
   195
(*-------------------------methods-----------------------*)
neuper@37954
   196
store_met
neuper@37972
   197
 (prep_met thy "met_rateq" [] e_metID
neuper@37954
   198
 (["RatEq"],
neuper@37954
   199
   [],
neuper@37954
   200
   {rew_ord'="tless_true",rls'=Atools_erls,calc = [], srls = e_rls, prls=e_rls,
neuper@42425
   201
    crls=RatEq_crls, errpats = [], nrls = norm_Rational}, "empty_script"));
neuper@37988
   202
*}
neuper@37988
   203
neuper@37988
   204
ML {*
neuper@37954
   205
store_met
neuper@37972
   206
 (prep_met thy "met_rat_eq" [] e_metID
neuper@42394
   207
 (["RatEq", "solve_rat_equation"],
neuper@37981
   208
   [("#Given" ,["equality e_e","solveFor v_v"]),
neuper@37982
   209
   ("#Where" ,["(e_e::bool) is_ratequation_in (v_v::real)"]),
neuper@38012
   210
   ("#Find"  ,["solutions v_v'i'"])
neuper@37954
   211
  ],
neuper@37954
   212
   {rew_ord'="termlessI",
neuper@37954
   213
    rls'=rateq_erls,
neuper@37954
   214
    srls=e_rls,
neuper@37954
   215
    prls=RatEq_prls,
neuper@37954
   216
    calc=[],
neuper@42425
   217
    crls=RatEq_crls, errpats = [], nrls = norm_Rational},
neuper@37982
   218
   "Script Solve_rat_equation  (e_e::bool) (v_v::real) =                   " ^
neuper@37981
   219
    "(let e_e = ((Repeat(Try (Rewrite_Set RatEq_simplify      True))) @@  " ^
neuper@37954
   220
    "           (Repeat(Try (Rewrite_Set norm_Rational      False))) @@  " ^
neuper@52105
   221
    "           (Repeat(Try (Rewrite_Set add_fractions_p False))) @@  " ^
neuper@37988
   222
    "           (Repeat(Try (Rewrite_Set RatEq_eliminate     True)))) e_e;" ^
neuper@37988
   223
    " (L_L::bool list) = (SubProblem (RatEq',[univariate,equation], [no_met])" ^
neuper@37988
   224
    "                    [BOOL e_e, REAL v_v])                     " ^
neuper@42268
   225
    " in Check_elementwise L_L {(v_v::real). Assumptions})"
neuper@37954
   226
   ));
neuper@37988
   227
*}
neuper@37954
   228
s1210629013@52145
   229
setup {* KEStore_Elems.add_calcs
s1210629013@52145
   230
  [("is_ratequation_in", ("RatEq.is_ratequation_in", eval_is_ratequation_in ""))] *}
neuper@37906
   231
neuper@37906
   232
end