author | wenzelm |
Wed, 01 Jun 2016 15:10:27 +0200 | |
changeset 63205 | 97b721666890 |
parent 63201 | f151704c08e4 |
child 63211 | 0bec0d1d9998 |
permissions | -rw-r--r-- |
41474 | 1 |
(* Title: HOL/Library/Sum_of_Squares/sum_of_squares.ML |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
2 |
Author: Amine Chaieb, University of Cambridge |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
3 |
Author: Philipp Meyer, TU Muenchen |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
4 |
|
32839 | 5 |
A tactic for proving nonlinear inequalities. |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
6 |
*) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
7 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
8 |
signature SUM_OF_SQUARES = |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
9 |
sig |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
10 |
datatype proof_method = Certificate of RealArith.pss_tree | Prover of string -> string |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
11 |
val sos_tac: (RealArith.pss_tree -> unit) -> proof_method -> Proof.context -> int -> tactic |
38805 | 12 |
val trace: bool Config.T |
58631 | 13 |
val debug: bool Config.T |
14 |
val trace_message: Proof.context -> (unit -> string) -> unit |
|
15 |
val debug_message: Proof.context -> (unit -> string) -> unit |
|
32332 | 16 |
exception Failure of string; |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
17 |
end |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
18 |
|
41474 | 19 |
structure Sum_of_Squares: SUM_OF_SQUARES = |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
20 |
struct |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
21 |
|
33029 | 22 |
val max = Integer.max; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
23 |
|
63201 | 24 |
val denominator_rat = Rat.dest #> snd #> Rat.of_int; |
55508 | 25 |
|
32839 | 26 |
fun int_of_rat a = |
63201 | 27 |
(case Rat.dest a of |
55508 | 28 |
(i, 1) => i |
29 |
| _ => error "int_of_rat: not an int"); |
|
30 |
||
31 |
fun lcm_rat x y = |
|
63201 | 32 |
Rat.of_int (Integer.lcm (int_of_rat x) (int_of_rat y)); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
33 |
|
32839 | 34 |
fun rat_pow r i = |
35 |
let fun pow r i = |
|
63205 | 36 |
if i = 0 then @1 else |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
37 |
let val d = pow r (i div 2) |
63205 | 38 |
in d * d * (if i mod 2 = 0 then @1 else r) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
39 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
40 |
in if i < 0 then pow (Rat.inv r) (~ i) else pow r i end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
41 |
|
32839 | 42 |
fun round_rat r = |
55508 | 43 |
let |
63201 | 44 |
val (a,b) = Rat.dest (Rat.abs r) |
55508 | 45 |
val d = a div b |
63205 | 46 |
val s = if r < @0 then (Rat.neg o Rat.of_int) else Rat.of_int |
55508 | 47 |
val x2 = 2 * (a - (b * d)) |
48 |
in s (if x2 >= b then d + 1 else d) end |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
49 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
50 |
val abs_rat = Rat.abs; |
63205 | 51 |
val pow2 = rat_pow @2; |
52 |
val pow10 = rat_pow @10; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
53 |
|
58631 | 54 |
|
42616
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
41490
diff
changeset
|
55 |
val trace = Attrib.setup_config_bool @{binding sos_trace} (K false); |
58631 | 56 |
val debug = Attrib.setup_config_bool @{binding sos_debug} (K false); |
57 |
||
58 |
fun trace_message ctxt msg = |
|
59 |
if Config.get ctxt trace orelse Config.get ctxt debug then tracing (msg ()) else (); |
|
60 |
fun debug_message ctxt msg = if Config.get ctxt debug then tracing (msg ()) else (); |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
61 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
62 |
exception Sanity; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
63 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
64 |
exception Unsolvable; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
65 |
|
32332 | 66 |
exception Failure of string; |
67 |
||
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
68 |
datatype proof_method = |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
69 |
Certificate of RealArith.pss_tree |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
70 |
| Prover of (string -> string) |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
71 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
72 |
(* Turn a rational into a decimal string with d sig digits. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
73 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
74 |
local |
55508 | 75 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
76 |
fun normalize y = |
63205 | 77 |
if abs_rat y < @1/10 then normalize (@10 * y) - 1 |
78 |
else if abs_rat y >= @1 then normalize (y / @10) + 1 |
|
32839 | 79 |
else 0 |
55508 | 80 |
|
81 |
in |
|
82 |
||
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
83 |
fun decimalize d x = |
63205 | 84 |
if x = @0 then "0.0" |
55508 | 85 |
else |
86 |
let |
|
87 |
val y = Rat.abs x |
|
88 |
val e = normalize y |
|
63205 | 89 |
val z = pow10(~ e) * y + @1 |
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
90 |
val k = int_of_rat (round_rat(pow10 d * z)) |
55508 | 91 |
in |
63205 | 92 |
(if x < @0 then "-0." else "0.") ^ |
55508 | 93 |
implode (tl (raw_explode(string_of_int k))) ^ |
94 |
(if e = 0 then "" else "e" ^ string_of_int e) |
|
95 |
end |
|
96 |
||
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
97 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
98 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
99 |
(* Iterations over numbers, and lists indexed by numbers. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
100 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
101 |
fun itern k l f a = |
55508 | 102 |
(case l of |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
103 |
[] => a |
55508 | 104 |
| h::t => itern (k + 1) t f (f h k a)); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
105 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
106 |
fun iter (m,n) f a = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
107 |
if n < m then a |
55508 | 108 |
else iter (m + 1, n) f (f m a); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
109 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
110 |
(* The main types. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
111 |
|
55508 | 112 |
type vector = int * Rat.rat FuncUtil.Intfunc.table; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
113 |
|
55508 | 114 |
type matrix = (int * int) * Rat.rat FuncUtil.Intpairfunc.table; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
115 |
|
63205 | 116 |
fun iszero (_, r) = r = @0; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
117 |
|
32839 | 118 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
119 |
(* Vectors. Conventionally indexed 1..n. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
120 |
|
55508 | 121 |
fun vector_0 n = (n, FuncUtil.Intfunc.empty): vector; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
122 |
|
55508 | 123 |
fun dim (v: vector) = fst v; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
124 |
|
55508 | 125 |
fun vector_cmul c (v: vector) = |
126 |
let val n = dim v in |
|
63205 | 127 |
if c = @0 then vector_0 n |
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
128 |
else (n,FuncUtil.Intfunc.map (fn _ => fn x => c * x) (snd v)) |
55508 | 129 |
end; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
130 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
131 |
fun vector_of_list l = |
55508 | 132 |
let val n = length l in |
58635 | 133 |
(n, fold_rev FuncUtil.Intfunc.update (1 upto n ~~ l) FuncUtil.Intfunc.empty): vector |
55508 | 134 |
end; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
135 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
136 |
(* Matrices; again rows and columns indexed from 1. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
137 |
|
55508 | 138 |
fun dimensions (m: matrix) = fst m; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
139 |
|
55508 | 140 |
fun row k (m: matrix) : vector = |
141 |
let val (_, j) = dimensions m in |
|
142 |
(j, |
|
143 |
FuncUtil.Intpairfunc.fold (fn ((i, j), c) => fn a => |
|
144 |
if i = k then FuncUtil.Intfunc.update (j, c) a else a) (snd m) FuncUtil.Intfunc.empty) |
|
145 |
end; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
146 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
147 |
(* Monomials. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
148 |
|
32828 | 149 |
fun monomial_eval assig m = |
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
150 |
FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => a * rat_pow (FuncUtil.Ctermfunc.apply assig x) k) |
63205 | 151 |
m @1; |
55508 | 152 |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
153 |
val monomial_1 = FuncUtil.Ctermfunc.empty; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
154 |
|
32828 | 155 |
fun monomial_var x = FuncUtil.Ctermfunc.onefunc (x, 1); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
156 |
|
32828 | 157 |
val monomial_mul = |
33002 | 158 |
FuncUtil.Ctermfunc.combine Integer.add (K false); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
159 |
|
32839 | 160 |
fun monomial_multidegree m = |
55508 | 161 |
FuncUtil.Ctermfunc.fold (fn (_, k) => fn a => k + a) m 0; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
162 |
|
55508 | 163 |
fun monomial_variables m = FuncUtil.Ctermfunc.dom m; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
164 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
165 |
(* Polynomials. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
166 |
|
32828 | 167 |
fun eval assig p = |
63205 | 168 |
FuncUtil.Monomialfunc.fold (fn (m, c) => fn a => a + c * monomial_eval assig m) p @0; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
169 |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
170 |
val poly_0 = FuncUtil.Monomialfunc.empty; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
171 |
|
32839 | 172 |
fun poly_isconst p = |
55508 | 173 |
FuncUtil.Monomialfunc.fold (fn (m, _) => fn a => FuncUtil.Ctermfunc.is_empty m andalso a) |
174 |
p true; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
175 |
|
63205 | 176 |
fun poly_var x = FuncUtil.Monomialfunc.onefunc (monomial_var x, @1); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
177 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
178 |
fun poly_const c = |
63205 | 179 |
if c = @0 then poly_0 else FuncUtil.Monomialfunc.onefunc (monomial_1, c); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
180 |
|
32828 | 181 |
fun poly_cmul c p = |
63205 | 182 |
if c = @0 then poly_0 |
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
183 |
else FuncUtil.Monomialfunc.map (fn _ => fn x => c * x) p; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
184 |
|
55508 | 185 |
fun poly_neg p = FuncUtil.Monomialfunc.map (K Rat.neg) p; |
186 |
||
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
187 |
|
32828 | 188 |
fun poly_add p1 p2 = |
63205 | 189 |
FuncUtil.Monomialfunc.combine (curry op +) (fn x => x = @0) p1 p2; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
190 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
191 |
fun poly_sub p1 p2 = poly_add p1 (poly_neg p2); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
192 |
|
32828 | 193 |
fun poly_cmmul (c,m) p = |
63205 | 194 |
if c = @0 then poly_0 |
55508 | 195 |
else |
196 |
if FuncUtil.Ctermfunc.is_empty m |
|
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
197 |
then FuncUtil.Monomialfunc.map (fn _ => fn d => c * d) p |
55508 | 198 |
else |
199 |
FuncUtil.Monomialfunc.fold (fn (m', d) => fn a => |
|
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
200 |
(FuncUtil.Monomialfunc.update (monomial_mul m m', c * d) a)) p poly_0; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
201 |
|
32828 | 202 |
fun poly_mul p1 p2 = |
203 |
FuncUtil.Monomialfunc.fold (fn (m, c) => fn a => poly_add (poly_cmmul (c,m) p2) a) p1 poly_0; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
204 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
205 |
fun poly_square p = poly_mul p p; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
206 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
207 |
fun poly_pow p k = |
63205 | 208 |
if k = 0 then poly_const @1 |
55508 | 209 |
else if k = 1 then p |
210 |
else |
|
211 |
let val q = poly_square(poly_pow p (k div 2)) |
|
212 |
in if k mod 2 = 1 then poly_mul p q else q end; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
213 |
|
32828 | 214 |
fun multidegree p = |
44453 | 215 |
FuncUtil.Monomialfunc.fold (fn (m, _) => fn a => max (monomial_multidegree m) a) p 0; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
216 |
|
32828 | 217 |
fun poly_variables p = |
55508 | 218 |
sort FuncUtil.cterm_ord |
219 |
(FuncUtil.Monomialfunc.fold_rev |
|
220 |
(fn (m, _) => union (is_equal o FuncUtil.cterm_ord) (monomial_variables m)) p []); |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
221 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
222 |
(* Conversion from HOL term. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
223 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
224 |
local |
55508 | 225 |
val neg_tm = @{cterm "uminus :: real => _"} |
226 |
val add_tm = @{cterm "op + :: real => _"} |
|
227 |
val sub_tm = @{cterm "op - :: real => _"} |
|
228 |
val mul_tm = @{cterm "op * :: real => _"} |
|
229 |
val inv_tm = @{cterm "inverse :: real => _"} |
|
230 |
val div_tm = @{cterm "op / :: real => _"} |
|
231 |
val pow_tm = @{cterm "op ^ :: real => _"} |
|
232 |
val zero_tm = @{cterm "0:: real"} |
|
59582 | 233 |
val is_numeral = can (HOLogic.dest_number o Thm.term_of) |
55508 | 234 |
fun poly_of_term tm = |
235 |
if tm aconvc zero_tm then poly_0 |
|
236 |
else |
|
237 |
if RealArith.is_ratconst tm |
|
238 |
then poly_const(RealArith.dest_ratconst tm) |
|
239 |
else |
|
240 |
(let |
|
241 |
val (lop, r) = Thm.dest_comb tm |
|
242 |
in |
|
243 |
if lop aconvc neg_tm then poly_neg(poly_of_term r) |
|
244 |
else if lop aconvc inv_tm then |
|
245 |
let val p = poly_of_term r in |
|
246 |
if poly_isconst p |
|
247 |
then poly_const(Rat.inv (eval FuncUtil.Ctermfunc.empty p)) |
|
248 |
else error "poly_of_term: inverse of non-constant polyomial" |
|
249 |
end |
|
250 |
else |
|
251 |
(let |
|
252 |
val (opr,l) = Thm.dest_comb lop |
|
253 |
in |
|
254 |
if opr aconvc pow_tm andalso is_numeral r |
|
59582 | 255 |
then poly_pow (poly_of_term l) ((snd o HOLogic.dest_number o Thm.term_of) r) |
55508 | 256 |
else if opr aconvc add_tm |
257 |
then poly_add (poly_of_term l) (poly_of_term r) |
|
258 |
else if opr aconvc sub_tm |
|
259 |
then poly_sub (poly_of_term l) (poly_of_term r) |
|
260 |
else if opr aconvc mul_tm |
|
261 |
then poly_mul (poly_of_term l) (poly_of_term r) |
|
262 |
else if opr aconvc div_tm |
|
263 |
then |
|
264 |
let |
|
32839 | 265 |
val p = poly_of_term l |
266 |
val q = poly_of_term r |
|
55508 | 267 |
in |
268 |
if poly_isconst q |
|
269 |
then poly_cmul (Rat.inv (eval FuncUtil.Ctermfunc.empty q)) p |
|
270 |
else error "poly_of_term: division by non-constant polynomial" |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
271 |
end |
55508 | 272 |
else poly_var tm |
273 |
end handle CTERM ("dest_comb",_) => poly_var tm) |
|
274 |
end handle CTERM ("dest_comb",_) => poly_var tm) |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
275 |
in |
55508 | 276 |
val poly_of_term = fn tm => |
59582 | 277 |
if type_of (Thm.term_of tm) = @{typ real} |
55508 | 278 |
then poly_of_term tm |
279 |
else error "poly_of_term: term does not have real type" |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
280 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
281 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
282 |
(* String of vector (just a list of space-separated numbers). *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
283 |
|
55508 | 284 |
fun sdpa_of_vector (v: vector) = |
285 |
let |
|
286 |
val n = dim v |
|
287 |
val strs = |
|
63205 | 288 |
map (decimalize 20 o (fn i => FuncUtil.Intfunc.tryapplyd (snd v) i @0)) (1 upto n) |
55508 | 289 |
in space_implode " " strs ^ "\n" end; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
290 |
|
55508 | 291 |
fun triple_int_ord ((a, b, c), (a', b', c')) = |
292 |
prod_ord int_ord (prod_ord int_ord int_ord) ((a, (b, c)), (a', (b', c'))); |
|
293 |
structure Inttriplefunc = FuncFun(type key = int * int * int val ord = triple_int_ord); |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
294 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
295 |
fun index_char str chr pos = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
296 |
if pos >= String.size str then ~1 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
297 |
else if String.sub(str,pos) = chr then pos |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
298 |
else index_char str chr (pos + 1); |
55508 | 299 |
|
300 |
fun rat_of_quotient (a,b) = |
|
63205 | 301 |
if b = 0 then @0 else Rat.make (a, b); |
55508 | 302 |
|
32839 | 303 |
fun rat_of_string s = |
55508 | 304 |
let val n = index_char s #"/" 0 in |
63201 | 305 |
if n = ~1 then s |> Int.fromString |> the |> Rat.of_int |
55508 | 306 |
else |
307 |
let |
|
308 |
val SOME numer = Int.fromString(String.substring(s,0,n)) |
|
309 |
val SOME den = Int.fromString (String.substring(s,n+1,String.size s - n - 1)) |
|
310 |
in rat_of_quotient(numer, den) end |
|
311 |
end; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
312 |
|
53975
22ee3fb9d596
backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents:
53972
diff
changeset
|
313 |
|
55508 | 314 |
fun isnum x = member (op =) ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] x; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
315 |
|
53975
22ee3fb9d596
backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents:
53972
diff
changeset
|
316 |
(* More parser basics. *) |
22ee3fb9d596
backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents:
53972
diff
changeset
|
317 |
(* FIXME improper use of parser combinators ahead *) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
318 |
|
55508 | 319 |
val numeral = Scan.one isnum |
320 |
val decimalint = Scan.repeat1 numeral >> (rat_of_string o implode) |
|
321 |
val decimalfrac = Scan.repeat1 numeral |
|
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
322 |
>> (fn s => rat_of_string(implode s) / pow10 (length s)) |
55508 | 323 |
val decimalsig = |
324 |
decimalint -- Scan.option (Scan.$$ "." |-- decimalfrac) |
|
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
325 |
>> (fn (h,NONE) => h | (h,SOME x) => h + x) |
55508 | 326 |
fun signed prs = |
327 |
$$ "-" |-- prs >> Rat.neg |
|
328 |
|| $$ "+" |-- prs |
|
329 |
|| prs; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
330 |
|
55508 | 331 |
fun emptyin def xs = if null xs then (def, xs) else Scan.fail xs |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
332 |
|
55508 | 333 |
val exponent = ($$ "e" || $$ "E") |-- signed decimalint; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
334 |
|
63205 | 335 |
val decimal = signed decimalsig -- (emptyin @0|| exponent) |
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
336 |
>> (fn (h, x) => h * pow10 (int_of_rat x)); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
337 |
|
55508 | 338 |
fun mkparser p s = |
53975
22ee3fb9d596
backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents:
53972
diff
changeset
|
339 |
let val (x,rst) = p (raw_explode s) |
55508 | 340 |
in if null rst then x else error "mkparser: unparsed input" end; |
53975
22ee3fb9d596
backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents:
53972
diff
changeset
|
341 |
|
32332 | 342 |
(* Parse back csdp output. *) |
53975
22ee3fb9d596
backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents:
53972
diff
changeset
|
343 |
(* FIXME improper use of parser combinators ahead *) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
344 |
|
55508 | 345 |
fun ignore _ = ((),[]) |
346 |
fun csdpoutput inp = |
|
347 |
((decimal -- Scan.repeat (Scan.$$ " " |-- Scan.option decimal) >> |
|
33067
a70d5baa53ee
use plain Scan.repeat (NB: Scan.bulk is for cascading sources -- mostly interna use);
wenzelm
parents:
33042
diff
changeset
|
348 |
(fn (h,to) => map_filter I ((SOME h)::to))) --| ignore >> vector_of_list) inp |
55508 | 349 |
val parse_csdpoutput = mkparser csdpoutput |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
350 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
351 |
(* Try some apparently sensible scaling first. Note that this is purely to *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
352 |
(* get a cleaner translation to floating-point, and doesn't affect any of *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
353 |
(* the results, in principle. In practice it seems a lot better when there *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
354 |
(* are extreme numbers in the original problem. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
355 |
|
55508 | 356 |
(* Version for (int*int*int) keys *) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
357 |
local |
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
358 |
fun max_rat x y = if x < y then y else x |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
359 |
fun common_denominator fld amat acc = |
55508 | 360 |
fld (fn (_,c) => fn a => lcm_rat (denominator_rat c) a) amat acc |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
361 |
fun maximal_element fld amat acc = |
44453 | 362 |
fld (fn (_,c) => fn maxa => max_rat maxa (abs_rat c)) amat acc |
55508 | 363 |
fun float_of_rat x = |
63201 | 364 |
let val (a,b) = Rat.dest x |
55508 | 365 |
in Real.fromInt a / Real.fromInt b end; |
366 |
fun int_of_float x = (trunc x handle Overflow => 0 | Domain => 0) |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
367 |
in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
368 |
|
55508 | 369 |
fun tri_scale_then solver (obj:vector) mats = |
370 |
let |
|
63205 | 371 |
val cd1 = fold_rev (common_denominator Inttriplefunc.fold) mats @1 |
372 |
val cd2 = common_denominator FuncUtil.Intfunc.fold (snd obj) @1 |
|
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
373 |
val mats' = map (Inttriplefunc.map (fn _ => fn x => cd1 * x)) mats |
55508 | 374 |
val obj' = vector_cmul cd2 obj |
63205 | 375 |
val max1 = fold_rev (maximal_element Inttriplefunc.fold) mats' @0 |
376 |
val max2 = maximal_element FuncUtil.Intfunc.fold (snd obj') @0 |
|
55508 | 377 |
val scal1 = pow2 (20 - int_of_float(Math.ln (float_of_rat max1) / Math.ln 2.0)) |
378 |
val scal2 = pow2 (20 - int_of_float(Math.ln (float_of_rat max2) / Math.ln 2.0)) |
|
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
379 |
val mats'' = map (Inttriplefunc.map (fn _ => fn x => x * scal1)) mats' |
55508 | 380 |
val obj'' = vector_cmul scal2 obj' |
381 |
in solver obj'' mats'' end |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
382 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
383 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
384 |
(* Round a vector to "nice" rationals. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
385 |
|
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
386 |
fun nice_rational n x = round_rat (n * x) / n; |
32839 | 387 |
fun nice_vector n ((d,v) : vector) = |
55508 | 388 |
(d, FuncUtil.Intfunc.fold (fn (i,c) => fn a => |
389 |
let val y = nice_rational n c in |
|
63205 | 390 |
if c = @0 then a |
55508 | 391 |
else FuncUtil.Intfunc.update (i,y) a |
392 |
end) v FuncUtil.Intfunc.empty): vector |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
393 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
394 |
fun dest_ord f x = is_equal (f x); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
395 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
396 |
(* Stuff for "equations" ((int*int*int)->num functions). *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
397 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
398 |
fun tri_equation_cmul c eq = |
63205 | 399 |
if c = @0 then Inttriplefunc.empty |
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
400 |
else Inttriplefunc.map (fn _ => fn d => c * d) eq; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
401 |
|
55508 | 402 |
fun tri_equation_add eq1 eq2 = |
63205 | 403 |
Inttriplefunc.combine (curry op +) (fn x => x = @0) eq1 eq2; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
404 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
405 |
fun tri_equation_eval assig eq = |
55508 | 406 |
let |
407 |
fun value v = Inttriplefunc.apply assig v |
|
63205 | 408 |
in Inttriplefunc.fold (fn (v, c) => fn a => a + value v * c) eq @0 end; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
409 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
410 |
(* Eliminate all variables, in an essentially arbitrary order. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
411 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
412 |
fun tri_eliminate_all_equations one = |
55508 | 413 |
let |
414 |
fun choose_variable eq = |
|
415 |
let val (v,_) = Inttriplefunc.choose eq |
|
416 |
in |
|
417 |
if is_equal (triple_int_ord(v,one)) then |
|
418 |
let |
|
419 |
val eq' = Inttriplefunc.delete_safe v eq |
|
420 |
in |
|
421 |
if Inttriplefunc.is_empty eq' then error "choose_variable" |
|
422 |
else fst (Inttriplefunc.choose eq') |
|
423 |
end |
|
424 |
else v |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
425 |
end |
55508 | 426 |
|
427 |
fun eliminate dun eqs = |
|
428 |
(case eqs of |
|
429 |
[] => dun |
|
430 |
| eq :: oeqs => |
|
431 |
if Inttriplefunc.is_empty eq then eliminate dun oeqs |
|
432 |
else |
|
433 |
let |
|
434 |
val v = choose_variable eq |
|
435 |
val a = Inttriplefunc.apply eq v |
|
436 |
val eq' = |
|
63201 | 437 |
tri_equation_cmul ((Rat.of_int ~1) / a) (Inttriplefunc.delete_safe v eq) |
55508 | 438 |
fun elim e = |
63205 | 439 |
let val b = Inttriplefunc.tryapplyd e v @0 in |
440 |
if b = @0 then e |
|
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
441 |
else tri_equation_add e (tri_equation_cmul (Rat.neg b / a) eq) |
55508 | 442 |
end |
443 |
in |
|
444 |
eliminate (Inttriplefunc.update(v, eq') (Inttriplefunc.map (K elim) dun)) |
|
445 |
(map elim oeqs) |
|
446 |
end) |
|
447 |
in |
|
448 |
fn eqs => |
|
449 |
let |
|
450 |
val assig = eliminate Inttriplefunc.empty eqs |
|
451 |
val vs = Inttriplefunc.fold (fn (_, f) => fn a => |
|
452 |
remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig [] |
|
453 |
in (distinct (dest_ord triple_int_ord) vs,assig) end |
|
454 |
end; |
|
32839 | 455 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
456 |
(* Multiply equation-parametrized poly by regular poly and add accumulator. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
457 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
458 |
fun tri_epoly_pmul p q acc = |
55508 | 459 |
FuncUtil.Monomialfunc.fold (fn (m1, c) => fn a => |
460 |
FuncUtil.Monomialfunc.fold (fn (m2, e) => fn b => |
|
461 |
let |
|
462 |
val m = monomial_mul m1 m2 |
|
463 |
val es = FuncUtil.Monomialfunc.tryapplyd b m Inttriplefunc.empty |
|
464 |
in |
|
465 |
FuncUtil.Monomialfunc.update (m,tri_equation_add (tri_equation_cmul c e) es) b |
|
466 |
end) q a) p acc; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
467 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
468 |
(* Hence produce the "relevant" monomials: those whose squares lie in the *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
469 |
(* Newton polytope of the monomials in the input. (This is enough according *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
470 |
(* to Reznik: "Extremal PSD forms with few terms", Duke Math. Journal, *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
471 |
(* vol 45, pp. 363--374, 1978. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
472 |
(* *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
473 |
(* These are ordered in sort of decreasing degree. In particular the *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
474 |
(* constant monomial is last; this gives an order in diagonalization of the *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
475 |
(* quadratic form that will tend to display constants. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
476 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
477 |
(* Diagonalize (Cholesky/LDU) the matrix corresponding to a quadratic form. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
478 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
479 |
local |
55508 | 480 |
fun diagonalize n i m = |
481 |
if FuncUtil.Intpairfunc.is_empty (snd m) then [] |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
482 |
else |
55508 | 483 |
let |
63205 | 484 |
val a11 = FuncUtil.Intpairfunc.tryapplyd (snd m) (i,i) @0 |
55508 | 485 |
in |
63205 | 486 |
if a11 < @0 then raise Failure "diagonalize: not PSD" |
487 |
else if a11 = @0 then |
|
55508 | 488 |
if FuncUtil.Intfunc.is_empty (snd (row i m)) |
489 |
then diagonalize n (i + 1) m |
|
490 |
else raise Failure "diagonalize: not PSD ___ " |
|
491 |
else |
|
492 |
let |
|
493 |
val v = row i m |
|
494 |
val v' = |
|
495 |
(fst v, FuncUtil.Intfunc.fold (fn (i, c) => fn a => |
|
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
496 |
let val y = c / a11 |
63205 | 497 |
in if y = @0 then a else FuncUtil.Intfunc.update (i,y) a |
55508 | 498 |
end) (snd v) FuncUtil.Intfunc.empty) |
499 |
fun upt0 x y a = |
|
63205 | 500 |
if y = @0 then a |
55508 | 501 |
else FuncUtil.Intpairfunc.update (x,y) a |
502 |
val m' = |
|
503 |
((n, n), |
|
504 |
iter (i + 1, n) (fn j => |
|
505 |
iter (i + 1, n) (fn k => |
|
506 |
(upt0 (j, k) |
|
63205 | 507 |
(FuncUtil.Intpairfunc.tryapplyd (snd m) (j, k) @0 - |
508 |
FuncUtil.Intfunc.tryapplyd (snd v) j @0 * |
|
509 |
FuncUtil.Intfunc.tryapplyd (snd v') k @0)))) |
|
55508 | 510 |
FuncUtil.Intpairfunc.empty) |
511 |
in (a11, v') :: diagonalize n (i + 1) m' end |
|
512 |
end |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
513 |
in |
55508 | 514 |
fun diag m = |
515 |
let |
|
516 |
val nn = dimensions m |
|
517 |
val n = fst nn |
|
518 |
in |
|
519 |
if snd nn <> n then error "diagonalize: non-square matrix" |
|
520 |
else diagonalize n 1 m |
|
521 |
end |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
522 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
523 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
524 |
(* Enumeration of monomials with given multidegree bound. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
525 |
|
32839 | 526 |
fun enumerate_monomials d vars = |
55508 | 527 |
if d < 0 then [] |
528 |
else if d = 0 then [FuncUtil.Ctermfunc.empty] |
|
529 |
else if null vars then [monomial_1] |
|
530 |
else |
|
531 |
let val alts = |
|
532 |
map_range (fn k => |
|
533 |
let |
|
534 |
val oths = enumerate_monomials (d - k) (tl vars) |
|
535 |
in map (fn ks => if k = 0 then ks else FuncUtil.Ctermfunc.update (hd vars, k) ks) oths end) |
|
536 |
(d + 1) |
|
537 |
in flat alts end; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
538 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
539 |
(* Enumerate products of distinct input polys with degree <= d. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
540 |
(* We ignore any constant input polynomials. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
541 |
(* Give the output polynomial and a record of how it was derived. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
542 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
543 |
fun enumerate_products d pols = |
63205 | 544 |
if d = 0 then [(poly_const @1, RealArith.Rational_lt @1)] |
55508 | 545 |
else if d < 0 then [] |
546 |
else |
|
547 |
(case pols of |
|
63205 | 548 |
[] => [(poly_const @1, RealArith.Rational_lt @1)] |
55508 | 549 |
| (p, b) :: ps => |
550 |
let val e = multidegree p in |
|
551 |
if e = 0 then enumerate_products d ps |
|
552 |
else |
|
553 |
enumerate_products d ps @ |
|
554 |
map (fn (q, c) => (poly_mul p q, RealArith.Product (b, c))) |
|
555 |
(enumerate_products (d - e) ps) |
|
556 |
end) |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
557 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
558 |
(* Convert regular polynomial. Note that we treat (0,0,0) as -1. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
559 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
560 |
fun epoly_of_poly p = |
55508 | 561 |
FuncUtil.Monomialfunc.fold (fn (m, c) => fn a => |
562 |
FuncUtil.Monomialfunc.update (m, Inttriplefunc.onefunc ((0, 0, 0), Rat.neg c)) a) |
|
563 |
p FuncUtil.Monomialfunc.empty; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
564 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
565 |
(* String for block diagonal matrix numbered k. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
566 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
567 |
fun sdpa_of_blockdiagonal k m = |
55508 | 568 |
let |
569 |
val pfx = string_of_int k ^" " |
|
570 |
val ents = |
|
571 |
Inttriplefunc.fold |
|
572 |
(fn ((b, i, j), c) => fn a => if i > j then a else ((b, i, j), c) :: a) |
|
573 |
m [] |
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58635
diff
changeset
|
574 |
val entss = sort (triple_int_ord o apply2 fst) ents |
55508 | 575 |
in |
576 |
fold_rev (fn ((b,i,j),c) => fn a => |
|
577 |
pfx ^ string_of_int b ^ " " ^ string_of_int i ^ " " ^ string_of_int j ^ |
|
578 |
" " ^ decimalize 20 c ^ "\n" ^ a) entss "" |
|
579 |
end; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
580 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
581 |
(* SDPA for problem using block diagonal (i.e. multiple SDPs) *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
582 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
583 |
fun sdpa_of_blockproblem nblocks blocksizes obj mats = |
55508 | 584 |
let val m = length mats - 1 |
585 |
in |
|
586 |
string_of_int m ^ "\n" ^ |
|
587 |
string_of_int nblocks ^ "\n" ^ |
|
588 |
(space_implode " " (map string_of_int blocksizes)) ^ |
|
589 |
"\n" ^ |
|
590 |
sdpa_of_vector obj ^ |
|
58635 | 591 |
fold_rev (fn (k, m) => fn a => sdpa_of_blockdiagonal (k - 1) m ^ a) |
592 |
(1 upto length mats ~~ mats) "" |
|
55508 | 593 |
end; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
594 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
595 |
(* Run prover on a problem in block diagonal form. *) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
596 |
|
55508 | 597 |
fun run_blockproblem prover nblocks blocksizes obj mats = |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
598 |
parse_csdpoutput (prover (sdpa_of_blockproblem nblocks blocksizes obj mats)) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
599 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
600 |
(* 3D versions of matrix operations to consider blocks separately. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
601 |
|
63205 | 602 |
val bmatrix_add = Inttriplefunc.combine (curry op +) (fn x => x = @0); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
603 |
fun bmatrix_cmul c bm = |
63205 | 604 |
if c = @0 then Inttriplefunc.empty |
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
60867
diff
changeset
|
605 |
else Inttriplefunc.map (fn _ => fn x => c * x) bm; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
606 |
|
63201 | 607 |
val bmatrix_neg = bmatrix_cmul (Rat.of_int ~1); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
608 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
609 |
(* Smash a block matrix into components. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
610 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
611 |
fun blocks blocksizes bm = |
55508 | 612 |
map (fn (bs, b0) => |
613 |
let |
|
614 |
val m = |
|
615 |
Inttriplefunc.fold |
|
616 |
(fn ((b, i, j), c) => fn a => |
|
617 |
if b = b0 then FuncUtil.Intpairfunc.update ((i, j), c) a else a) |
|
618 |
bm FuncUtil.Intpairfunc.empty |
|
619 |
val _ = FuncUtil.Intpairfunc.fold (fn ((i, j), _) => fn a => max a (max i j)) m 0 |
|
620 |
in (((bs, bs), m): matrix) end) |
|
621 |
(blocksizes ~~ (1 upto length blocksizes)); |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
622 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
623 |
(* FIXME : Get rid of this !!!*) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
624 |
local |
44453 | 625 |
fun tryfind_with msg _ [] = raise Failure msg |
626 |
| tryfind_with _ f (x::xs) = (f x handle Failure s => tryfind_with s f xs); |
|
32839 | 627 |
in |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
628 |
fun tryfind f = tryfind_with "tryfind" f |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
629 |
end |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
630 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
631 |
(* Positiv- and Nullstellensatz. Flag "linf" forces a linear representation. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
632 |
|
38805 | 633 |
fun real_positivnullstellensatz_general ctxt prover linf d eqs leqs pol = |
32839 | 634 |
let |
55508 | 635 |
val vars = |
636 |
fold_rev (union (op aconvc) o poly_variables) |
|
637 |
(pol :: eqs @ map fst leqs) [] |
|
638 |
val monoid = |
|
639 |
if linf then |
|
63205 | 640 |
(poly_const @1, RealArith.Rational_lt @1):: |
55508 | 641 |
(filter (fn (p,_) => multidegree p <= d) leqs) |
642 |
else enumerate_products d leqs |
|
643 |
val nblocks = length monoid |
|
644 |
fun mk_idmultiplier k p = |
|
645 |
let |
|
646 |
val e = d - multidegree p |
|
647 |
val mons = enumerate_monomials e vars |
|
648 |
val nons = mons ~~ (1 upto length mons) |
|
649 |
in |
|
650 |
(mons, |
|
651 |
fold_rev (fn (m, n) => |
|
63205 | 652 |
FuncUtil.Monomialfunc.update (m, Inttriplefunc.onefunc ((~k, ~n, n), @1))) |
55508 | 653 |
nons FuncUtil.Monomialfunc.empty) |
654 |
end |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
655 |
|
55508 | 656 |
fun mk_sqmultiplier k (p,_) = |
657 |
let |
|
658 |
val e = (d - multidegree p) div 2 |
|
659 |
val mons = enumerate_monomials e vars |
|
660 |
val nons = mons ~~ (1 upto length mons) |
|
661 |
in |
|
662 |
(mons, |
|
663 |
fold_rev (fn (m1, n1) => |
|
664 |
fold_rev (fn (m2, n2) => fn a => |
|
665 |
let val m = monomial_mul m1 m2 in |
|
666 |
if n1 > n2 then a |
|
667 |
else |
|
668 |
let |
|
63205 | 669 |
val c = if n1 = n2 then @1 else @2 |
55508 | 670 |
val e = FuncUtil.Monomialfunc.tryapplyd a m Inttriplefunc.empty |
671 |
in |
|
672 |
FuncUtil.Monomialfunc.update |
|
673 |
(m, tri_equation_add (Inttriplefunc.onefunc ((k, n1, n2), c)) e) a |
|
674 |
end |
|
675 |
end) nons) nons FuncUtil.Monomialfunc.empty) |
|
676 |
end |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
677 |
|
55508 | 678 |
val (sqmonlist,sqs) = split_list (map2 mk_sqmultiplier (1 upto length monoid) monoid) |
679 |
val (_(*idmonlist*),ids) = split_list (map2 mk_idmultiplier (1 upto length eqs) eqs) |
|
680 |
val blocksizes = map length sqmonlist |
|
681 |
val bigsum = |
|
58635 | 682 |
fold_rev (fn (p, q) => fn a => tri_epoly_pmul p q a) (eqs ~~ ids) |
683 |
(fold_rev (fn ((p, _), s) => fn a => tri_epoly_pmul p s a) (monoid ~~ sqs) |
|
684 |
(epoly_of_poly (poly_neg pol))) |
|
55508 | 685 |
val eqns = FuncUtil.Monomialfunc.fold (fn (_, e) => fn a => e :: a) bigsum [] |
686 |
val (pvs, assig) = tri_eliminate_all_equations (0, 0, 0) eqns |
|
687 |
val qvars = (0, 0, 0) :: pvs |
|
688 |
val allassig = |
|
63205 | 689 |
fold_rev (fn v => Inttriplefunc.update (v, (Inttriplefunc.onefunc (v, @1)))) pvs assig |
55508 | 690 |
fun mk_matrix v = |
691 |
Inttriplefunc.fold (fn ((b, i, j), ass) => fn m => |
|
692 |
if b < 0 then m |
|
693 |
else |
|
63205 | 694 |
let val c = Inttriplefunc.tryapplyd ass v @0 in |
695 |
if c = @0 then m |
|
55508 | 696 |
else Inttriplefunc.update ((b, j, i), c) (Inttriplefunc.update ((b, i, j), c) m) |
697 |
end) |
|
698 |
allassig Inttriplefunc.empty |
|
699 |
val diagents = |
|
700 |
Inttriplefunc.fold |
|
701 |
(fn ((b, i, j), e) => fn a => if b > 0 andalso i = j then tri_equation_add e a else a) |
|
702 |
allassig Inttriplefunc.empty |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
703 |
|
55508 | 704 |
val mats = map mk_matrix qvars |
705 |
val obj = |
|
706 |
(length pvs, |
|
707 |
itern 1 pvs (fn v => fn i => |
|
63205 | 708 |
FuncUtil.Intfunc.updatep iszero (i,Inttriplefunc.tryapplyd diagents v @0)) |
55508 | 709 |
FuncUtil.Intfunc.empty) |
710 |
val raw_vec = |
|
711 |
if null pvs then vector_0 0 |
|
712 |
else tri_scale_then (run_blockproblem prover nblocks blocksizes) obj mats |
|
63205 | 713 |
fun int_element (_, v) i = FuncUtil.Intfunc.tryapplyd v i @0 |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
714 |
|
55508 | 715 |
fun find_rounding d = |
716 |
let |
|
717 |
val _ = |
|
58631 | 718 |
debug_message ctxt (fn () => "Trying rounding with limit "^Rat.string_of_rat d ^ "\n") |
55508 | 719 |
val vec = nice_vector d raw_vec |
720 |
val blockmat = |
|
721 |
iter (1, dim vec) |
|
722 |
(fn i => fn a => bmatrix_add (bmatrix_cmul (int_element vec i) (nth mats i)) a) |
|
723 |
(bmatrix_neg (nth mats 0)) |
|
724 |
val allmats = blocks blocksizes blockmat |
|
725 |
in (vec, map diag allmats) end |
|
726 |
val (vec, ratdias) = |
|
63205 | 727 |
if null pvs then find_rounding @1 |
63201 | 728 |
else tryfind find_rounding (map Rat.of_int (1 upto 31) @ map pow2 (5 upto 66)) |
55508 | 729 |
val newassigs = |
730 |
fold_rev (fn k => Inttriplefunc.update (nth pvs (k - 1), int_element vec k)) |
|
63201 | 731 |
(1 upto dim vec) (Inttriplefunc.onefunc ((0, 0, 0), Rat.of_int ~1)) |
55508 | 732 |
val finalassigs = |
733 |
Inttriplefunc.fold (fn (v, e) => fn a => |
|
734 |
Inttriplefunc.update (v, tri_equation_eval newassigs e) a) allassig newassigs |
|
735 |
fun poly_of_epoly p = |
|
736 |
FuncUtil.Monomialfunc.fold (fn (v, e) => fn a => |
|
737 |
FuncUtil.Monomialfunc.updatep iszero (v, tri_equation_eval finalassigs e) a) |
|
738 |
p FuncUtil.Monomialfunc.empty |
|
739 |
fun mk_sos mons = |
|
740 |
let |
|
741 |
fun mk_sq (c, m) = |
|
742 |
(c, fold_rev (fn k => fn a => |
|
743 |
FuncUtil.Monomialfunc.updatep iszero (nth mons (k - 1), int_element m k) a) |
|
744 |
(1 upto length mons) FuncUtil.Monomialfunc.empty) |
|
745 |
in map mk_sq end |
|
746 |
val sqs = map2 mk_sos sqmonlist ratdias |
|
747 |
val cfs = map poly_of_epoly ids |
|
748 |
val msq = filter (fn (_, b) => not (null b)) (map2 pair monoid sqs) |
|
749 |
fun eval_sq sqs = fold_rev (fn (c, q) => poly_add (poly_cmul c (poly_mul q q))) sqs poly_0 |
|
750 |
val sanity = |
|
751 |
fold_rev (fn ((p, _), s) => poly_add (poly_mul p (eval_sq s))) msq |
|
58635 | 752 |
(fold_rev (fn (p, q) => poly_add (poly_mul p q)) (cfs ~~ eqs) (poly_neg pol)) |
55508 | 753 |
in |
754 |
if not(FuncUtil.Monomialfunc.is_empty sanity) then raise Sanity |
|
755 |
else (cfs, map (fn (a, b) => (snd a, b)) msq) |
|
756 |
end |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
757 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
758 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
759 |
(* Iterative deepening. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
760 |
|
58631 | 761 |
fun deepen ctxt f n = |
762 |
(trace_message ctxt (fn () => "Searching with depth limit " ^ string_of_int n); |
|
763 |
(f n handle Failure s => |
|
764 |
(trace_message ctxt (fn () => "failed with message: " ^ s); deepen ctxt f (n + 1)))); |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
765 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
766 |
|
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
767 |
(* Map back polynomials and their composites to a positivstellensatz. *) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
768 |
|
55508 | 769 |
fun cterm_of_sqterm (c, p) = RealArith.Product (RealArith.Rational_lt c, RealArith.Square p); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
770 |
|
55508 | 771 |
fun cterm_of_sos (pr,sqs) = |
772 |
if null sqs then pr |
|
773 |
else RealArith.Product (pr, foldr1 RealArith.Sum (map cterm_of_sqterm sqs)); |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
774 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
775 |
(* Interface to HOL. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
776 |
local |
32828 | 777 |
open Conv |
59582 | 778 |
val concl = Thm.dest_arg o Thm.cprop_of |
779 |
fun simple_cterm_ord t u = Term_Ord.fast_term_ord (Thm.term_of t, Thm.term_of u) = LESS |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
780 |
in |
55508 | 781 |
(* FIXME: Replace tryfind by get_first !! *) |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
782 |
fun real_nonlinear_prover proof_method ctxt = |
32839 | 783 |
let |
55508 | 784 |
val {add = _, mul = _, neg = _, pow = _, sub = _, main = real_poly_conv} = |
785 |
Semiring_Normalizer.semiring_normalizers_ord_wrapper ctxt |
|
786 |
(the (Semiring_Normalizer.match ctxt @{cterm "(0::real) + 1"})) |
|
787 |
simple_cterm_ord |
|
788 |
fun mainf cert_choice translator (eqs, les, lts) = |
|
789 |
let |
|
790 |
val eq0 = map (poly_of_term o Thm.dest_arg1 o concl) eqs |
|
791 |
val le0 = map (poly_of_term o Thm.dest_arg o concl) les |
|
792 |
val lt0 = map (poly_of_term o Thm.dest_arg o concl) lts |
|
793 |
val eqp0 = map_index (fn (i, t) => (t,RealArith.Axiom_eq i)) eq0 |
|
794 |
val lep0 = map_index (fn (i, t) => (t,RealArith.Axiom_le i)) le0 |
|
795 |
val ltp0 = map_index (fn (i, t) => (t,RealArith.Axiom_lt i)) lt0 |
|
796 |
val (keq,eq) = List.partition (fn (p, _) => multidegree p = 0) eqp0 |
|
797 |
val (klep,lep) = List.partition (fn (p, _) => multidegree p = 0) lep0 |
|
798 |
val (kltp,ltp) = List.partition (fn (p, _) => multidegree p = 0) ltp0 |
|
799 |
fun trivial_axiom (p, ax) = |
|
800 |
(case ax of |
|
801 |
RealArith.Axiom_eq n => |
|
63205 | 802 |
if eval FuncUtil.Ctermfunc.empty p <> @0 then nth eqs n |
55508 | 803 |
else raise Failure "trivial_axiom: Not a trivial axiom" |
804 |
| RealArith.Axiom_le n => |
|
63205 | 805 |
if eval FuncUtil.Ctermfunc.empty p < @0 then nth les n |
55508 | 806 |
else raise Failure "trivial_axiom: Not a trivial axiom" |
807 |
| RealArith.Axiom_lt n => |
|
63205 | 808 |
if eval FuncUtil.Ctermfunc.empty p <= @0 then nth lts n |
55508 | 809 |
else raise Failure "trivial_axiom: Not a trivial axiom" |
810 |
| _ => error "trivial_axiom: Not a trivial axiom") |
|
811 |
in |
|
812 |
let val th = tryfind trivial_axiom (keq @ klep @ kltp) in |
|
813 |
(fconv_rule (arg_conv (arg1_conv (real_poly_conv ctxt)) |
|
814 |
then_conv Numeral_Simprocs.field_comp_conv ctxt) th, |
|
815 |
RealArith.Trivial) |
|
816 |
end handle Failure _ => |
|
817 |
let |
|
818 |
val proof = |
|
819 |
(case proof_method of |
|
820 |
Certificate certs => |
|
821 |
(* choose certificate *) |
|
822 |
let |
|
823 |
fun chose_cert [] (RealArith.Cert c) = c |
|
824 |
| chose_cert (RealArith.Left::s) (RealArith.Branch (l, _)) = chose_cert s l |
|
825 |
| chose_cert (RealArith.Right::s) (RealArith.Branch (_, r)) = chose_cert s r |
|
826 |
| chose_cert _ _ = error "certificate tree in invalid form" |
|
827 |
in |
|
828 |
chose_cert cert_choice certs |
|
829 |
end |
|
830 |
| Prover prover => |
|
831 |
(* call prover *) |
|
832 |
let |
|
63205 | 833 |
val pol = fold_rev poly_mul (map fst ltp) (poly_const @1) |
55508 | 834 |
val leq = lep @ ltp |
835 |
fun tryall d = |
|
836 |
let |
|
837 |
val e = multidegree pol |
|
838 |
val k = if e = 0 then 0 else d div e |
|
839 |
val eq' = map fst eq |
|
840 |
in |
|
841 |
tryfind (fn i => |
|
842 |
(d, i, real_positivnullstellensatz_general ctxt prover false d eq' leq |
|
843 |
(poly_neg(poly_pow pol i)))) |
|
844 |
(0 upto k) |
|
845 |
end |
|
58631 | 846 |
val (_,i,(cert_ideal,cert_cone)) = deepen ctxt tryall 0 |
55508 | 847 |
val proofs_ideal = |
848 |
map2 (fn q => fn (_,ax) => RealArith.Eqmul(q,ax)) cert_ideal eq |
|
849 |
val proofs_cone = map cterm_of_sos cert_cone |
|
850 |
val proof_ne = |
|
63205 | 851 |
if null ltp then RealArith.Rational_lt @1 |
55508 | 852 |
else |
853 |
let val p = foldr1 RealArith.Product (map snd ltp) in |
|
854 |
funpow i (fn q => RealArith.Product (p, q)) |
|
63205 | 855 |
(RealArith.Rational_lt @1) |
55508 | 856 |
end |
857 |
in |
|
858 |
foldr1 RealArith.Sum (proof_ne :: proofs_ideal @ proofs_cone) |
|
859 |
end) |
|
860 |
in |
|
861 |
(translator (eqs,les,lts) proof, RealArith.Cert proof) |
|
862 |
end |
|
863 |
end |
|
864 |
in mainf end |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
865 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
866 |
|
55508 | 867 |
(* FIXME : This is very bad!!!*) |
32839 | 868 |
fun subst_conv eqs t = |
55508 | 869 |
let |
870 |
val t' = fold (Thm.lambda o Thm.lhs_of) eqs t |
|
871 |
in |
|
60329
e85ed7a36b2f
eliminated odd C combinator -- Isabelle/ML usually has canonical argument order;
wenzelm
parents:
59582
diff
changeset
|
872 |
Conv.fconv_rule (Thm.beta_conversion true) |
e85ed7a36b2f
eliminated odd C combinator -- Isabelle/ML usually has canonical argument order;
wenzelm
parents:
59582
diff
changeset
|
873 |
(fold (fn a => fn b => Thm.combination b a) eqs (Thm.reflexive t')) |
55508 | 874 |
end |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
875 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
876 |
(* A wrapper that tries to substitute away variables first. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
877 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
878 |
local |
55508 | 879 |
open Conv |
59582 | 880 |
fun simple_cterm_ord t u = Term_Ord.fast_term_ord (Thm.term_of t, Thm.term_of u) = LESS |
881 |
val concl = Thm.dest_arg o Thm.cprop_of |
|
55508 | 882 |
val shuffle1 = |
883 |
fconv_rule (rewr_conv @{lemma "(a + x == y) == (x == y - (a::real))" |
|
884 |
by (atomize (full)) (simp add: field_simps)}) |
|
885 |
val shuffle2 = |
|
886 |
fconv_rule (rewr_conv @{lemma "(x + a == y) == (x == y - (a::real))" |
|
887 |
by (atomize (full)) (simp add: field_simps)}) |
|
888 |
fun substitutable_monomial fvs tm = |
|
59582 | 889 |
(case Thm.term_of tm of |
55508 | 890 |
Free (_, @{typ real}) => |
63205 | 891 |
if not (member (op aconvc) fvs tm) then (@1, tm) |
55508 | 892 |
else raise Failure "substitutable_monomial" |
893 |
| @{term "op * :: real => _"} $ _ $ (Free _) => |
|
894 |
if RealArith.is_ratconst (Thm.dest_arg1 tm) andalso |
|
895 |
not (member (op aconvc) fvs (Thm.dest_arg tm)) |
|
896 |
then (RealArith.dest_ratconst (Thm.dest_arg1 tm), Thm.dest_arg tm) |
|
897 |
else raise Failure "substitutable_monomial" |
|
898 |
| @{term "op + :: real => _"}$_$_ => |
|
60818 | 899 |
(substitutable_monomial (Drule.cterm_add_frees (Thm.dest_arg tm) fvs) (Thm.dest_arg1 tm) |
55508 | 900 |
handle Failure _ => |
60818 | 901 |
substitutable_monomial (Drule.cterm_add_frees (Thm.dest_arg1 tm) fvs) (Thm.dest_arg tm)) |
55508 | 902 |
| _ => raise Failure "substitutable_monomial") |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
903 |
|
32839 | 904 |
fun isolate_variable v th = |
55508 | 905 |
let |
59582 | 906 |
val w = Thm.dest_arg1 (Thm.cprop_of th) |
55508 | 907 |
in |
908 |
if v aconvc w then th |
|
909 |
else |
|
59582 | 910 |
(case Thm.term_of w of |
55508 | 911 |
@{term "op + :: real => _"} $ _ $ _ => |
912 |
if Thm.dest_arg1 w aconvc v then shuffle2 th |
|
913 |
else isolate_variable v (shuffle1 th) |
|
914 |
| _ => error "isolate variable : This should not happen?") |
|
32839 | 915 |
end |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
916 |
in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
917 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
918 |
fun real_nonlinear_subst_prover prover ctxt = |
55508 | 919 |
let |
920 |
val {add = _, mul = real_poly_mul_conv, neg = _, pow = _, sub = _, main = real_poly_conv} = |
|
44453 | 921 |
Semiring_Normalizer.semiring_normalizers_ord_wrapper ctxt |
55508 | 922 |
(the (Semiring_Normalizer.match ctxt @{cterm "(0::real) + 1"})) |
923 |
simple_cterm_ord |
|
924 |
||
925 |
fun make_substitution th = |
|
926 |
let |
|
927 |
val (c,v) = substitutable_monomial [] (Thm.dest_arg1(concl th)) |
|
928 |
val th1 = |
|
929 |
Drule.arg_cong_rule |
|
930 |
(Thm.apply @{cterm "op * :: real => _"} (RealArith.cterm_of_rat (Rat.inv c))) |
|
931 |
(mk_meta_eq th) |
|
932 |
val th2 = fconv_rule (binop_conv (real_poly_mul_conv ctxt)) th1 |
|
933 |
in fconv_rule (arg_conv (real_poly_conv ctxt)) (isolate_variable v th2) end |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
934 |
|
55508 | 935 |
fun oprconv cv ct = |
936 |
let val g = Thm.dest_fun2 ct in |
|
937 |
if g aconvc @{cterm "op <= :: real => _"} orelse g aconvc @{cterm "op < :: real => _"} |
|
938 |
then arg_conv cv ct else arg1_conv cv ct |
|
939 |
end |
|
940 |
fun mainf cert_choice translator = |
|
941 |
let |
|
942 |
fun substfirst (eqs, les, lts) = |
|
943 |
(let |
|
944 |
val eth = tryfind make_substitution eqs |
|
945 |
val modify = |
|
946 |
fconv_rule (arg_conv (oprconv(subst_conv [eth] then_conv (real_poly_conv ctxt)))) |
|
947 |
in |
|
948 |
substfirst |
|
949 |
(filter_out |
|
59582 | 950 |
(fn t => (Thm.dest_arg1 o Thm.dest_arg o Thm.cprop_of) t aconvc @{cterm "0::real"}) |
55508 | 951 |
(map modify eqs), |
952 |
map modify les, |
|
953 |
map modify lts) |
|
954 |
end handle Failure _ => |
|
955 |
real_nonlinear_prover prover ctxt cert_choice translator (rev eqs, rev les, rev lts)) |
|
956 |
in substfirst end |
|
957 |
in mainf end |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
958 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
959 |
(* Overall function. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
960 |
|
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
961 |
fun real_sos prover ctxt = |
32828 | 962 |
RealArith.gen_prover_real_arith ctxt (real_nonlinear_subst_prover prover ctxt) |
55508 | 963 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
964 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
965 |
|
32839 | 966 |
val known_sos_constants = |
967 |
[@{term "op ==>"}, @{term "Trueprop"}, |
|
56625
54505623a8ef
sos accepts False, returns apply command
paulson <lp15@cam.ac.uk>
parents:
56536
diff
changeset
|
968 |
@{term HOL.False}, @{term HOL.implies}, @{term HOL.conj}, @{term HOL.disj}, |
32839 | 969 |
@{term "Not"}, @{term "op = :: bool => _"}, |
970 |
@{term "All :: (real => _) => _"}, @{term "Ex :: (real => _) => _"}, |
|
971 |
@{term "op = :: real => _"}, @{term "op < :: real => _"}, |
|
972 |
@{term "op <= :: real => _"}, |
|
973 |
@{term "op + :: real => _"}, @{term "op - :: real => _"}, |
|
974 |
@{term "op * :: real => _"}, @{term "uminus :: real => _"}, |
|
31512 | 975 |
@{term "op / :: real => _"}, @{term "inverse :: real => _"}, |
32839 | 976 |
@{term "op ^ :: real => _"}, @{term "abs :: real => _"}, |
31512 | 977 |
@{term "min :: real => _"}, @{term "max :: real => _"}, |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46497
diff
changeset
|
978 |
@{term "0::real"}, @{term "1::real"}, |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46497
diff
changeset
|
979 |
@{term "numeral :: num => nat"}, |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46497
diff
changeset
|
980 |
@{term "numeral :: num => real"}, |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46497
diff
changeset
|
981 |
@{term "Num.Bit0"}, @{term "Num.Bit1"}, @{term "Num.One"}]; |
31512 | 982 |
|
32839 | 983 |
fun check_sos kcts ct = |
55508 | 984 |
let |
59582 | 985 |
val t = Thm.term_of ct |
55508 | 986 |
val _ = |
987 |
if not (null (Term.add_tfrees t []) andalso null (Term.add_tvars t [])) |
|
988 |
then error "SOS: not sos. Additional type varables" |
|
989 |
else () |
|
990 |
val fs = Term.add_frees t [] |
|
991 |
val _ = |
|
992 |
if exists (fn ((_,T)) => not (T = @{typ "real"})) fs |
|
993 |
then error "SOS: not sos. Variables with type not real" |
|
994 |
else () |
|
995 |
val vs = Term.add_vars t [] |
|
996 |
val _ = |
|
997 |
if exists (fn ((_,T)) => not (T = @{typ "real"})) vs |
|
998 |
then error "SOS: not sos. Variables with type not real" |
|
999 |
else () |
|
1000 |
val ukcs = subtract (fn (t,p) => Const p aconv t) kcts (Term.add_consts t []) |
|
1001 |
val _ = |
|
1002 |
if null ukcs then () |
|
1003 |
else error ("SOSO: Unknown constants in Subgoal:" ^ commas (map fst ukcs)) |
|
1004 |
in () end |
|
31512 | 1005 |
|
60752 | 1006 |
fun core_sos_tac print_cert prover = SUBPROOF (fn {concl, context = ctxt, ...} => |
32831 | 1007 |
let |
32838
d9dfd30af9ae
core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents:
32831
diff
changeset
|
1008 |
val _ = check_sos known_sos_constants concl |
60752 | 1009 |
val (th, certificates) = real_sos prover ctxt (Thm.dest_arg concl) |
32838
d9dfd30af9ae
core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents:
32831
diff
changeset
|
1010 |
val _ = print_cert certificates |
60752 | 1011 |
in resolve_tac ctxt [th] 1 end); |
31131 | 1012 |
|
44453 | 1013 |
fun default_SOME _ NONE v = SOME v |
1014 |
| default_SOME _ (SOME v) _ = SOME v; |
|
31131 | 1015 |
|
1016 |
fun lift_SOME f NONE a = f a |
|
44453 | 1017 |
| lift_SOME _ (SOME a) _ = SOME a; |
31131 | 1018 |
|
1019 |
||
1020 |
local |
|
59582 | 1021 |
val is_numeral = can (HOLogic.dest_number o Thm.term_of) |
31131 | 1022 |
in |
55508 | 1023 |
fun get_denom b ct = |
59582 | 1024 |
(case Thm.term_of ct of |
55508 | 1025 |
@{term "op / :: real => _"} $ _ $ _ => |
1026 |
if is_numeral (Thm.dest_arg ct) |
|
1027 |
then get_denom b (Thm.dest_arg1 ct) |
|
1028 |
else default_SOME (get_denom b) (get_denom b (Thm.dest_arg ct)) (Thm.dest_arg ct, b) |
|
1029 |
| @{term "op < :: real => _"} $ _ $ _ => |
|
1030 |
lift_SOME (get_denom true) (get_denom true (Thm.dest_arg ct)) (Thm.dest_arg1 ct) |
|
1031 |
| @{term "op <= :: real => _"} $ _ $ _ => |
|
1032 |
lift_SOME (get_denom true) (get_denom true (Thm.dest_arg ct)) (Thm.dest_arg1 ct) |
|
1033 |
| _ $ _ => lift_SOME (get_denom b) (get_denom b (Thm.dest_fun ct)) (Thm.dest_arg ct) |
|
1034 |
| _ => NONE) |
|
31131 | 1035 |
end; |
1036 |
||
55508 | 1037 |
fun elim_one_denom_tac ctxt = CSUBGOAL (fn (P, i) => |
1038 |
(case get_denom false P of |
|
1039 |
NONE => no_tac |
|
1040 |
| SOME (d, ord) => |
|
1041 |
let |
|
1042 |
val simp_ctxt = |
|
1043 |
ctxt addsimps @{thms field_simps} |
|
60867 | 1044 |
addsimps [@{thm power_divide}] |
55508 | 1045 |
val th = |
60801 | 1046 |
Thm.instantiate' [] [SOME d, SOME (Thm.dest_arg P)] |
55508 | 1047 |
(if ord then @{lemma "(d=0 --> P) & (d>0 --> P) & (d<(0::real) --> P) ==> P" by auto} |
1048 |
else @{lemma "(d=0 --> P) & (d ~= (0::real) --> P) ==> P" by blast}) |
|
60752 | 1049 |
in resolve_tac ctxt [th] i THEN Simplifier.asm_full_simp_tac simp_ctxt i end)); |
31131 | 1050 |
|
1051 |
fun elim_denom_tac ctxt i = REPEAT (elim_one_denom_tac ctxt i); |
|
1052 |
||
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1053 |
fun sos_tac print_cert prover ctxt = |
56536 | 1054 |
(* The SOS prover breaks if mult_nonneg_nonneg is in the simpset *) |
57889 | 1055 |
let val ctxt' = Context_Position.set_visible false ctxt delsimps @{thms mult_nonneg_nonneg} |
56536 | 1056 |
in Object_Logic.full_atomize_tac ctxt' THEN' |
1057 |
elim_denom_tac ctxt' THEN' |
|
1058 |
core_sos_tac print_cert prover ctxt' |
|
1059 |
end; |
|
31131 | 1060 |
|
31512 | 1061 |
end; |