Now deals with division
authorchaieb
Wed May 13 17:13:33 2009 +0100 (2009-05-13)
changeset 31131d9752181691a
parent 31130 94cb206f8f6a
child 31141 570eaf57cd4d
child 31146 bc47e6fb24de
child 32410 624bd2ea7c1e
Now deals with division
src/HOL/Library/Sum_Of_Squares.thy
src/HOL/Library/sum_of_squares.ML
     1.1 --- a/src/HOL/Library/Sum_Of_Squares.thy	Tue May 12 21:39:19 2009 +0200
     1.2 +++ b/src/HOL/Library/Sum_Of_Squares.thy	Wed May 13 17:13:33 2009 +0100
     1.3 @@ -9,30 +9,20 @@
     1.4    uses "positivstellensatz.ML" "sum_of_squares.ML"
     1.5    begin
     1.6  
     1.7 -method_setup sos = {* 
     1.8 -let 
     1.9 - fun strip_all ct = 
    1.10 -  case term_of ct of 
    1.11 -   Const("all",_) $ Abs (xn,xT,p) => 
    1.12 -    let val (a,(v,t')) = (apsnd (Thm.dest_abs (SOME xn)) o Thm.dest_comb) ct
    1.13 -    in apfst (cons v) (strip_all t')
    1.14 -    end
    1.15 - | _ => ([],ct)
    1.16 +(* Note: 
    1.17 +
    1.18 +In order to use the method sos, install CSDP (https://projects.coin-or.org/Csdp/) and put the executable csdp on your path. 
    1.19 +
    1.20 +*)
    1.21  
    1.22 - fun core_sos_conv ctxt t = Drule.arg_cong_rule @{cterm Trueprop} (Sos.real_sos ctxt (Thm.dest_arg t) RS @{thm Eq_TrueI})
    1.23 - fun core_sos_tac ctxt = CSUBGOAL (fn (ct, i) => 
    1.24 -   let val (avs, p) = strip_all ct
    1.25 -       val th = standard (fold_rev forall_intr avs (Sos.real_sos ctxt (Thm.dest_arg p)))
    1.26 -   in rtac th i end) (* CONVERSION o core_sos_conv *)
    1.27 -in Scan.succeed (SIMPLE_METHOD' o core_sos_tac)
    1.28 -end
    1.29  
    1.30 -*} "Prove universal problems over the reals using sums of squares"
    1.31 +method_setup sos = {* Scan.succeed (SIMPLE_METHOD' o Sos.sos_tac) *} 
    1.32 +  "Prove universal problems over the reals using sums of squares"
    1.33  
    1.34 -text{* Tests -- commented since they work only when csdp is installed *}
    1.35 +text{* Tests -- commented since they work only when csdp is installed -- see above *}
    1.36  
    1.37  (*
    1.38 -lemma "(3::real) * x + 7 * a < 4 & 3 < 2 * x --> a < 0" by sos
    1.39 +lemma "(3::real) * x + 7 * a < 4 & 3 < 2 * x \<Longrightarrow> a < 0" by sos
    1.40  
    1.41  lemma "a1 >= 0 & a2 >= 0 \<and> (a1 * a1 + a2 * a2 = b1 * b1 + b2 * b2 + 2) \<and> (a1 * b1 + a2 * b2 = 0) --> a1 * a2 - b1 * b2 >= (0::real)" by sos
    1.42  
    1.43 @@ -69,8 +59,8 @@
    1.44  (* ------------------------------------------------------------------------- *)
    1.45  (*
    1.46  lemma "2 <= (x::real) & x <= 4 & 2 <= y & y <= 4 & 2 <= z & z <= 4 --> 12 <= 2 * (x * z + x * y + y * z) - (x * x + y * y + z * z)" by sos
    1.47 +
    1.48  *)
    1.49 -
    1.50  (* ------------------------------------------------------------------------- *)
    1.51  (* Inequality from sci.math (see "Leon-Sotelo, por favor").                  *)
    1.52  (* ------------------------------------------------------------------------- *)
    1.53 @@ -110,5 +100,20 @@
    1.54  *)
    1.55  (*
    1.56  lemma "((x::real) - y - 2 * x^4 = 0) & 0 <= x & x <= 2 & 0 <= y & y <= 3 --> y^2 - 7 * y - 12 * x + 17 >= 0" by sos *) (* Too hard?*)
    1.57 +(*
    1.58 +lemma "(0::real) <= x --> (1 + x + x^2)/(1 + x^2) <= 1 + x"
    1.59 +apply sos
    1.60 +done
    1.61 +
    1.62 +lemma "(0::real) <= x --> 1 - x <= 1 / (1 + x + x^2)"
    1.63 +apply sos
    1.64 +done
    1.65 +
    1.66 +lemma "(x::real) <= 1 / 2 --> - x - 2 * x^2 <= - x / (1 - x)"
    1.67 +apply sos
    1.68 +done 
    1.69 +
    1.70 +lemma "4*r^2 = p^2 - 4*q & r >= (0::real) & x^2 + p*x + q = 0 --> 2*(x::real) = - p + 2*r | 2*x = -p - 2*r" by sos
    1.71 +*)
    1.72  
    1.73  end
     2.1 --- a/src/HOL/Library/sum_of_squares.ML	Tue May 12 21:39:19 2009 +0200
     2.2 +++ b/src/HOL/Library/sum_of_squares.ML	Wed May 13 17:13:33 2009 +0100
     2.3 @@ -1609,4 +1609,57 @@
     2.4  fun real_sos ctxt t = gen_prover_real_arith ctxt (real_nonlinear_subst_prover ctxt) t;
     2.5  end;
     2.6  
     2.7 +(* A tactic *)
     2.8 +fun strip_all ct = 
     2.9 + case term_of ct of 
    2.10 +  Const("all",_) $ Abs (xn,xT,p) => 
    2.11 +   let val (a,(v,t')) = (apsnd (Thm.dest_abs (SOME xn)) o Thm.dest_comb) ct
    2.12 +   in apfst (cons v) (strip_all t')
    2.13 +   end
    2.14 +| _ => ([],ct)
    2.15 +
    2.16 +fun core_sos_conv ctxt t = Drule.arg_cong_rule @{cterm Trueprop} (real_sos ctxt (Thm.dest_arg t) RS @{thm Eq_TrueI})
    2.17 +fun core_sos_tac ctxt = CSUBGOAL (fn (ct, i) => 
    2.18 +  let val (avs, p) = strip_all ct
    2.19 +      val th = standard (fold_rev forall_intr avs (real_sos ctxt (Thm.dest_arg p)))
    2.20 +  in rtac th i end);
    2.21 +
    2.22 +fun default_SOME f NONE v = SOME v
    2.23 +  | default_SOME f (SOME v) _ = SOME v;
    2.24 +
    2.25 +fun lift_SOME f NONE a = f a
    2.26 +  | lift_SOME f (SOME a) _ = SOME a;
    2.27 +
    2.28 +
    2.29 +local
    2.30 + val is_numeral = can (HOLogic.dest_number o term_of)
    2.31 +in
    2.32 +fun get_denom b ct = case term_of ct of
    2.33 +  @{term "op / :: real => _"} $ _ $ _ => 
    2.34 +     if is_numeral (Thm.dest_arg ct) then get_denom b (Thm.dest_arg1 ct)
    2.35 +     else default_SOME (get_denom b) (get_denom b (Thm.dest_arg ct))   (Thm.dest_arg ct, b)
    2.36 + | @{term "op < :: real => _"} $ _ $ _ => lift_SOME (get_denom true) (get_denom true (Thm.dest_arg ct)) (Thm.dest_arg1 ct)
    2.37 + | @{term "op <= :: real => _"} $ _ $ _ => lift_SOME (get_denom true) (get_denom true (Thm.dest_arg ct)) (Thm.dest_arg1 ct)
    2.38 + | _ $ _ => lift_SOME (get_denom b) (get_denom b (Thm.dest_fun ct)) (Thm.dest_arg ct)
    2.39 + | _ => NONE
    2.40 +end;
    2.41 +
    2.42 +fun elim_one_denom_tac ctxt = 
    2.43 +CSUBGOAL (fn (P,i) => 
    2.44 + case get_denom false P of 
    2.45 +   NONE => no_tac
    2.46 + | SOME (d,ord) => 
    2.47 +     let 
    2.48 +      val ss = simpset_of (ProofContext.theory_of ctxt) addsimps @{thms field_simps} 
    2.49 +               addsimps [@{thm nonzero_power_divide}, @{thm power_divide}]
    2.50 +      val th = instantiate' [] [SOME d, SOME (Thm.dest_arg P)] 
    2.51 +         (if ord then @{lemma "(d=0 --> P) & (d>0 --> P) & (d<(0::real) --> P) ==> P" by auto}
    2.52 +          else @{lemma "(d=0 --> P) & (d ~= (0::real) --> P) ==> P" by blast})
    2.53 +     in (rtac th i THEN Simplifier.asm_full_simp_tac ss i) end);
    2.54 +
    2.55 +fun elim_denom_tac ctxt i = REPEAT (elim_one_denom_tac ctxt i);
    2.56 +
    2.57 +fun sos_tac ctxt = ObjectLogic.full_atomize_tac THEN' elim_denom_tac ctxt THEN' core_sos_tac ctxt
    2.58 +
    2.59 +
    2.60  end;
    2.61 \ No newline at end of file