src/Tools/isac/Knowledge/LinEq.thy
author Walther Neuper <wneuper@ist.tugraz.at>
Sat, 22 Jun 2019 14:34:06 +0200
changeset 59551 6ea6d9c377a0
parent 59545 4035ec339062
child 59603 30cd47104ad7
permissions -rw-r--r--
funpack: remove code unnecessary after switch to partial_function, partially
neuper@37906
     1
(*. (c) by Richard Lang, 2003 .*)
neuper@37906
     2
(* theory collecting all knowledge for LinearEquations
neuper@37906
     3
   created by: rlang 
neuper@37906
     4
         date: 02.10
neuper@37906
     5
   changed by: rlang
neuper@37906
     6
   last change by: rlang
neuper@37906
     7
             date: 02.10.20
neuper@37906
     8
*)
neuper@37906
     9
neuper@37950
    10
theory LinEq imports Poly Equation begin
neuper@37906
    11
neuper@52148
    12
axiomatization where
wneuper@59370
    13
(*-- normalise --*)
neuper@37906
    14
  (*WN0509 compare PolyEq.all_left "[|Not(b=!=0)|] ==> (a = b) = (a - b = 0)"*)
neuper@52148
    15
  all_left:          "[|Not(b=!=0)|] ==> (a=b) = (a+(-1)*b=0)" and
neuper@52148
    16
  makex1_x:          "a^^^1  = a" and
neuper@52148
    17
  real_assoc_1:      "a+(b+c) = a+b+c" and
neuper@52148
    18
  real_assoc_2:      "a*(b*c) = a*b*c" and
neuper@37906
    19
neuper@37906
    20
(*-- solve --*)
neuper@52148
    21
  lin_isolate_add1:  "(a + b*bdv = 0) = (b*bdv = (-1)*a)" and
neuper@52148
    22
  lin_isolate_add2:  "(a +   bdv = 0) = (  bdv = (-1)*a)" and
neuper@37982
    23
  lin_isolate_div:   "[|Not(b=0)|] ==> (b*bdv = c) = (bdv = c / b)"
neuper@37950
    24
wneuper@59472
    25
ML \<open>
neuper@37972
    26
val thy = @{theory};
neuper@37972
    27
neuper@37950
    28
val LinEq_prls = (*3.10.02:just the following order due to subterm evaluation*)
wneuper@59416
    29
  Rule.append_rls "LinEq_prls" Rule.e_rls 
wneuper@59416
    30
	     [Rule.Calc ("HOL.eq",eval_equal "#equal_"),
wneuper@59491
    31
	      Rule.Calc ("Tools.matches", Tools.eval_matches ""),
wneuper@59491
    32
	      Rule.Calc ("Tools.lhs"    , Tools.eval_lhs ""),
wneuper@59491
    33
	      Rule.Calc ("Tools.rhs"    , Tools.eval_rhs ""),
wneuper@59416
    34
	      Rule.Calc ("Poly.has'_degree'_in",eval_has_degree_in ""),
wneuper@59416
    35
 	      Rule.Calc ("Poly.is'_polyrat'_in",eval_is_polyrat_in ""),
wneuper@59416
    36
	      Rule.Calc ("Atools.occurs'_in",eval_occurs_in ""),    
wneuper@59416
    37
	      Rule.Calc ("Atools.ident",eval_ident "#ident_"),
wneuper@59416
    38
	      Rule.Thm ("not_true",TermC.num_str @{thm not_true}),
wneuper@59416
    39
	      Rule.Thm ("not_false",TermC.num_str @{thm not_false}),
wneuper@59416
    40
	      Rule.Thm ("and_true",TermC.num_str @{thm and_true}),
wneuper@59416
    41
	      Rule.Thm ("and_false",TermC.num_str @{thm and_false}),
wneuper@59416
    42
	      Rule.Thm ("or_true",TermC.num_str @{thm or_true}),
wneuper@59416
    43
	      Rule.Thm ("or_false",TermC.num_str @{thm or_false})
neuper@37950
    44
              ];
neuper@37950
    45
(* ----- erls ----- *)
neuper@37950
    46
val LinEq_crls = 
wneuper@59416
    47
   Rule.append_rls "LinEq_crls" poly_crls
wneuper@59416
    48
   [Rule.Thm  ("real_assoc_1",TermC.num_str @{thm real_assoc_1})
neuper@37950
    49
    (*		
neuper@37950
    50
     Don't use
wneuper@59416
    51
     Rule.Calc ("Rings.divide_class.divide", eval_cancel "#divide_e"),
wneuper@59416
    52
     Rule.Calc ("Atools.pow" ,eval_binop "#power_"),
neuper@37950
    53
     *)
neuper@37950
    54
    ];
neuper@37950
    55
neuper@37950
    56
(* ----- crls ----- *)
neuper@37950
    57
val LinEq_erls = 
wneuper@59416
    58
   Rule.append_rls "LinEq_erls" Poly_erls
wneuper@59416
    59
   [Rule.Thm  ("real_assoc_1",TermC.num_str @{thm real_assoc_1})
neuper@37950
    60
    (*		
neuper@37950
    61
     Don't use
wneuper@59416
    62
     Rule.Calc ("Rings.divide_class.divide", eval_cancel "#divide_e"),
wneuper@59416
    63
     Rule.Calc ("Atools.pow" ,eval_binop "#power_"),
neuper@37950
    64
     *)
neuper@37950
    65
    ];
wneuper@59472
    66
\<close>
wneuper@59472
    67
setup \<open>KEStore_Elems.add_rlss 
wneuper@59472
    68
  [("LinEq_erls", (Context.theory_name @{theory}, LinEq_erls))]\<close>
wneuper@59472
    69
ML \<open>
neuper@37950
    70
    
s1210629013@55444
    71
val LinPoly_simplify = prep_rls'(
wneuper@59416
    72
  Rule.Rls {id = "LinPoly_simplify", preconds = [], 
neuper@37950
    73
       rew_ord = ("termlessI",termlessI), 
neuper@37950
    74
       erls = LinEq_erls, 
wneuper@59416
    75
       srls = Rule.Erls, 
neuper@42451
    76
       calc = [], errpatts = [],
neuper@37950
    77
       rules = [
wneuper@59416
    78
		Rule.Thm  ("real_assoc_1",TermC.num_str @{thm real_assoc_1}),
wneuper@59416
    79
		Rule.Calc ("Groups.plus_class.plus",eval_binop "#add_"),
wneuper@59416
    80
		Rule.Calc ("Groups.minus_class.minus",eval_binop "#sub_"),
wneuper@59416
    81
		Rule.Calc ("Groups.times_class.times",eval_binop "#mult_"),
neuper@37950
    82
		(*  Dont use  
wneuper@59416
    83
		 Rule.Calc ("Rings.divide_class.divide", eval_cancel "#divide_e"),		
wneuper@59416
    84
		 Rule.Calc ("NthRoot.sqrt",eval_sqrt "#sqrt_"),
neuper@37950
    85
		 *)
wneuper@59416
    86
		Rule.Calc ("Atools.pow" ,eval_binop "#power_")
neuper@37950
    87
		],
wneuper@59416
    88
       scr = Rule.EmptyScr});
wneuper@59472
    89
\<close>
wneuper@59472
    90
setup \<open>KEStore_Elems.add_rlss 
wneuper@59472
    91
  [("LinPoly_simplify", (Context.theory_name @{theory}, LinPoly_simplify))]\<close>
wneuper@59472
    92
ML \<open>
neuper@37950
    93
neuper@37950
    94
(*isolate the bound variable in an linear equation; 'bdv' is a meta-constant*)
s1210629013@55444
    95
val LinEq_simplify = prep_rls'(
wneuper@59416
    96
Rule.Rls {id = "LinEq_simplify", preconds = [],
wneuper@59416
    97
     rew_ord = ("e_rew_ord", Rule.e_rew_ord),
neuper@37950
    98
     erls = LinEq_erls,
wneuper@59416
    99
     srls = Rule.Erls,
neuper@42451
   100
     calc = [], errpatts = [],
neuper@37950
   101
     rules = [
wneuper@59416
   102
	      Rule.Thm("lin_isolate_add1",TermC.num_str @{thm lin_isolate_add1}), 
neuper@37950
   103
	      (* a+bx=0 -> bx=-a *)
wneuper@59416
   104
	      Rule.Thm("lin_isolate_add2",TermC.num_str @{thm lin_isolate_add2}), 
neuper@37950
   105
	      (* a+ x=0 ->  x=-a *)
wneuper@59416
   106
	      Rule.Thm("lin_isolate_div",TermC.num_str @{thm lin_isolate_div})    
neuper@37950
   107
	      (*   bx=c -> x=c/b *)  
neuper@37950
   108
	      ],
wneuper@59416
   109
     scr = Rule.EmptyScr});
wneuper@59472
   110
\<close>
wneuper@59472
   111
setup \<open>KEStore_Elems.add_rlss 
wneuper@59472
   112
  [("LinEq_simplify", (Context.theory_name @{theory}, LinEq_simplify))]\<close>
neuper@37950
   113
neuper@37950
   114
(*----------------------------- problem types --------------------------------*)
neuper@37950
   115
(* ---------linear----------- *)
wneuper@59472
   116
setup \<open>KEStore_Elems.add_pbts
wneuper@59406
   117
  [(Specify.prep_pbt thy "pbl_equ_univ_lin" [] Celem.e_pblID
s1210629013@55339
   118
      (["LINEAR", "univariate", "equation"],
s1210629013@55339
   119
        [("#Given" ,["equality e_e", "solveFor v_v"]),
s1210629013@55339
   120
          ("#Where" ,["HOL.False", (*WN0509 just detected: this pbl can never be used?!?*)
s1210629013@55339
   121
              "Not( (lhs e_e) is_polyrat_in v_v)",
s1210629013@55339
   122
              "Not( (rhs e_e) is_polyrat_in v_v)",
s1210629013@55339
   123
              "((lhs e_e) has_degree_in v_v)=1",
s1210629013@55339
   124
	            "((rhs e_e) has_degree_in v_v)=1"]),
s1210629013@55339
   125
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59472
   126
        LinEq_prls, SOME "solve (e_e::bool, v_v)", [["LinEq", "solve_lineq_equation"]]))]\<close>
neuper@37950
   127
s1210629013@55373
   128
(*-------------- methods------------------------------------------------------*)
wneuper@59472
   129
setup \<open>KEStore_Elems.add_mets
wneuper@59473
   130
    [Specify.prep_met thy "met_eqlin" [] Celem.e_metID
s1210629013@55373
   131
      (["LinEq"], [],
wneuper@59416
   132
        {rew_ord' = "tless_true",rls' = Atools_erls,calc = [], srls = Rule.e_rls, prls = Rule.e_rls,
s1210629013@55373
   133
          crls = LinEq_crls, errpats = [], nrls = norm_Poly},
wneuper@59545
   134
        @{thm refl})]
wneuper@59473
   135
\<close>
s1210629013@55373
   136
    (* ansprechen mit ["LinEq","solve_univar_equation"] *)
wneuper@59545
   137
wneuper@59504
   138
partial_function (tailrec) solve_linear_equation :: "bool \<Rightarrow> real \<Rightarrow> bool list"
wneuper@59504
   139
  where
wneuper@59504
   140
"solve_linear_equation e_e v_v =
wneuper@59504
   141
(let e_e =((Try         (Rewrite     ''all_left''            False)) @@
wneuper@59504
   142
           (Try (Repeat (Rewrite     ''makex1_x''            False))) @@
wneuper@59504
   143
           (Try         (Rewrite_Set ''expand_binoms''       False)) @@
wneuper@59504
   144
           (Try (Repeat (Rewrite_Set_Inst [(''bdv'', v_v)]
wneuper@59504
   145
                                     ''make_ratpoly_in''     False))) @@
wneuper@59504
   146
           (Try (Repeat (Rewrite_Set ''LinPoly_simplify''    False))))e_e;
wneuper@59504
   147
     e_e = ((Try (Rewrite_Set_Inst [(''bdv'', v_v::real)]
wneuper@59504
   148
                                     ''LinEq_simplify'' True)) @@
wneuper@59504
   149
            (Repeat(Try (Rewrite_Set ''LinPoly_simplify''    False)))) e_e
wneuper@59504
   150
 in Or_to_List e_e)"
wneuper@59473
   151
setup \<open>KEStore_Elems.add_mets
wneuper@59473
   152
    [Specify.prep_met thy "met_eq_lin" [] Celem.e_metID
s1210629013@55373
   153
      (["LinEq","solve_lineq_equation"],
s1210629013@55373
   154
        [("#Given", ["equality e_e", "solveFor v_v"]),
s1210629013@55373
   155
          ("#Where", ["Not ((lhs e_e) is_polyrat_in v_v)", "((lhs e_e)  has_degree_in v_v) = 1"]),
s1210629013@55373
   156
          ("#Find",  ["solutions v_v'i'"])],
wneuper@59416
   157
        {rew_ord' = "termlessI", rls' = LinEq_erls, srls = Rule.e_rls, prls = LinEq_prls, calc = [],
s1210629013@55373
   158
          crls = LinEq_crls, errpats = [], nrls = norm_Poly},
wneuper@59551
   159
        @{thm solve_linear_equation.simps})]
wneuper@59472
   160
\<close>
wneuper@59472
   161
ML \<open>Specify.get_met' @{theory} ["LinEq","solve_lineq_equation"];\<close>
neuper@37950
   162
neuper@37906
   163
end
neuper@37906
   164