| author | wenzelm | 
| Fri, 22 Apr 2011 13:58:13 +0200 | |
| changeset 42455 | 6702c984bf5a | 
| parent 41490 | 0f1e411a1448 | 
| child 42616 | 92715b528e78 | 
| 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: 
32839diff
changeset | 2 | Author: Amine Chaieb, University of Cambridge | 
| 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 3 | Author: Philipp Meyer, TU Muenchen | 
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
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: 
32150diff
changeset | 6 | *) | 
| 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 7 | |
| 32949 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 8 | signature SUM_OF_SQUARES = | 
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 9 | sig | 
| 32949 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
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: 
32839diff
changeset | 11 | val sos_tac: (RealArith.pss_tree -> unit) -> proof_method -> Proof.context -> int -> tactic | 
| 38805 | 12 | val trace: bool Config.T | 
| 13 | val setup: theory -> theory | |
| 32332 | 14 | exception Failure of string; | 
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 15 | end | 
| 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 16 | |
| 41474 | 17 | structure Sum_of_Squares: SUM_OF_SQUARES = | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 18 | struct | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 19 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 20 | val rat_0 = Rat.zero; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 21 | val rat_1 = Rat.one; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 22 | val rat_2 = Rat.two; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 23 | val rat_10 = Rat.rat_of_int 10; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 24 | val rat_1_2 = rat_1 // rat_2; | 
| 33029 | 25 | val max = Integer.max; | 
| 26 | val min = Integer.min; | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 27 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 28 | val denominator_rat = Rat.quotient_of_rat #> snd #> Rat.rat_of_int; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 29 | val numerator_rat = Rat.quotient_of_rat #> fst #> Rat.rat_of_int; | 
| 32839 | 30 | fun int_of_rat a = | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 31 | case Rat.quotient_of_rat a of (i,1) => i | _ => error "int_of_rat: not an int"; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 32 | fun lcm_rat x y = Rat.rat_of_int (Integer.lcm (int_of_rat x) (int_of_rat y)); | 
| 
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 = | |
| 36 | if i = 0 then rat_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) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 38 | in d */ d */ (if i mod 2 = 0 then rat_1 else r) | 
| 
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 = | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 43 | let val (a,b) = Rat.quotient_of_rat (Rat.abs r) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 44 | val d = a div b | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 45 | val s = if r </ rat_0 then (Rat.neg o Rat.rat_of_int) else Rat.rat_of_int | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 46 | val x2 = 2 * (a - (b * d)) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 47 | in s (if x2 >= b then d + 1 else d) end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 48 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 49 | val abs_rat = Rat.abs; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 50 | val pow2 = rat_pow rat_2; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 51 | val pow10 = rat_pow rat_10; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 52 | |
| 38805 | 53 | val (trace, setup_trace) = Attrib.config_bool "sos_trace" (K false); | 
| 54 | val setup = setup_trace; | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 55 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 56 | exception Sanity; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 57 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 58 | exception Unsolvable; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 59 | |
| 32332 | 60 | exception Failure of string; | 
| 61 | ||
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 62 | datatype proof_method = | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 63 | Certificate of RealArith.pss_tree | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 64 | | Prover of (string -> string) | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 65 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 66 | (* 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 | 67 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 68 | local | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 69 | fun normalize y = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 70 | if abs_rat y </ (rat_1 // rat_10) then normalize (rat_10 */ y) - 1 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 71 | else if abs_rat y >=/ rat_1 then normalize (y // rat_10) + 1 | 
| 32839 | 72 | else 0 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 73 | in | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 74 | fun decimalize d x = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 75 | if x =/ rat_0 then "0.0" else | 
| 32839 | 76 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 77 | val y = Rat.abs x | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 78 | val e = normalize y | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 79 | val z = pow10(~ e) */ y +/ rat_1 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 80 | val k = int_of_rat (round_rat(pow10 d */ z)) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 81 | in (if x </ rat_0 then "-0." else "0.") ^ | 
| 40627 
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
 wenzelm parents: 
39027diff
changeset | 82 | implode(tl(raw_explode(string_of_int k))) ^ | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 83 | (if e = 0 then "" else "e"^string_of_int e) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 84 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 85 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 86 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 87 | (* Iterations over numbers, and lists indexed by numbers. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 88 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 89 | fun itern k l f a = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 90 | case l of | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 91 | [] => a | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 92 | | h::t => itern (k + 1) t f (f h k a); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 93 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 94 | fun iter (m,n) f a = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 95 | if n < m then a | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 96 | else iter (m+1,n) f (f m a); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 97 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 98 | (* The main types. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 99 | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 100 | type vector = int* Rat.rat FuncUtil.Intfunc.table; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 101 | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 102 | 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 | 103 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 104 | fun iszero (k,r) = r =/ rat_0; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 105 | |
| 32839 | 106 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 107 | (* Vectors. Conventionally indexed 1..n. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 108 | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 109 | 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 | 110 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 111 | fun dim (v:vector) = fst v; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 112 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 113 | fun vector_const c n = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 114 | if c =/ rat_0 then vector_0 n | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 115 | else (n,fold_rev (fn k => FuncUtil.Intfunc.update (k,c)) (1 upto n) FuncUtil.Intfunc.empty) :vector; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 116 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 117 | val vector_1 = vector_const rat_1; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 118 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 119 | fun vector_cmul c (v:vector) = | 
| 32839 | 120 | let val n = dim v | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 121 | in if c =/ rat_0 then vector_0 n | 
| 39027 | 122 | else (n,FuncUtil.Intfunc.map (fn _ => fn x => c */ x) (snd v)) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 123 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 124 | |
| 39027 | 125 | fun vector_neg (v:vector) = (fst v,FuncUtil.Intfunc.map (K Rat.neg) (snd v)) :vector; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 126 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 127 | fun vector_add (v1:vector) (v2:vector) = | 
| 32839 | 128 | let val m = dim v1 | 
| 129 | val n = dim v2 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 130 | in if m <> n then error "vector_add: incompatible dimensions" | 
| 32839 | 131 | else (n,FuncUtil.Intfunc.combine (curry op +/) (fn x => x =/ rat_0) (snd v1) (snd v2)) :vector | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 132 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 133 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 134 | fun vector_sub v1 v2 = vector_add v1 (vector_neg v2); | 
| 
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 | fun vector_dot (v1:vector) (v2:vector) = | 
| 32839 | 137 | let val m = dim v1 | 
| 138 | val n = dim v2 | |
| 139 | in if m <> n then error "vector_dot: incompatible dimensions" | |
| 140 | else FuncUtil.Intfunc.fold (fn (i,x) => fn a => x +/ a) | |
| 32828 | 141 | (FuncUtil.Intfunc.combine (curry op */) (fn x => x =/ rat_0) (snd v1) (snd v2)) rat_0 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 142 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 143 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 144 | fun vector_of_list l = | 
| 32839 | 145 | let val n = length l | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 146 | in (n,fold_rev2 (curry FuncUtil.Intfunc.update) (1 upto n) l FuncUtil.Intfunc.empty) :vector | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 147 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 148 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 149 | (* Matrices; again rows and columns indexed from 1. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 150 | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 151 | fun matrix_0 (m,n) = ((m,n),FuncUtil.Intpairfunc.empty):matrix; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 152 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 153 | fun dimensions (m:matrix) = fst m; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 154 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 155 | fun matrix_const c (mn as (m,n)) = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 156 | if m <> n then error "matrix_const: needs to be square" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 157 | else if c =/ rat_0 then matrix_0 mn | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 158 | else (mn,fold_rev (fn k => FuncUtil.Intpairfunc.update ((k,k), c)) (1 upto n) FuncUtil.Intpairfunc.empty) :matrix;; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 159 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 160 | val matrix_1 = matrix_const rat_1; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 161 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 162 | fun matrix_cmul c (m:matrix) = | 
| 32839 | 163 | let val (i,j) = dimensions m | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 164 | in if c =/ rat_0 then matrix_0 (i,j) | 
| 39027 | 165 | else ((i,j),FuncUtil.Intpairfunc.map (fn _ => fn x => c */ x) (snd m)) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 166 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 167 | |
| 32839 | 168 | fun matrix_neg (m:matrix) = | 
| 39027 | 169 | (dimensions m, FuncUtil.Intpairfunc.map (K Rat.neg) (snd m)) :matrix; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 170 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 171 | fun matrix_add (m1:matrix) (m2:matrix) = | 
| 32839 | 172 | let val d1 = dimensions m1 | 
| 173 | val d2 = dimensions m2 | |
| 174 | in if d1 <> d2 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 175 | then error "matrix_add: incompatible dimensions" | 
| 32828 | 176 | else (d1,FuncUtil.Intpairfunc.combine (curry op +/) (fn x => x =/ rat_0) (snd m1) (snd m2)) :matrix | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 177 | end;; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 178 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 179 | fun matrix_sub m1 m2 = matrix_add m1 (matrix_neg m2); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 180 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 181 | fun row k (m:matrix) = | 
| 32839 | 182 | let val (i,j) = dimensions m | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 183 | in (j, | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 184 | FuncUtil.Intpairfunc.fold (fn ((i,j), c) => fn a => if i = k then FuncUtil.Intfunc.update (j,c) a else a) (snd m) FuncUtil.Intfunc.empty ) : vector | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 185 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 186 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 187 | fun column k (m:matrix) = | 
| 32839 | 188 | let val (i,j) = dimensions m | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 189 | in (i, | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 190 | FuncUtil.Intpairfunc.fold (fn ((i,j), c) => fn a => if j = k then FuncUtil.Intfunc.update (i,c) a else a) (snd m) FuncUtil.Intfunc.empty) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 191 | : vector | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 192 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 193 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 194 | fun transp (m:matrix) = | 
| 32839 | 195 | let val (i,j) = dimensions m | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 196 | in | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 197 | ((j,i),FuncUtil.Intpairfunc.fold (fn ((i,j), c) => fn a => FuncUtil.Intpairfunc.update ((j,i), c) a) (snd m) FuncUtil.Intpairfunc.empty) :matrix | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 198 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 199 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 200 | fun diagonal (v:vector) = | 
| 32839 | 201 | let val n = dim v | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 202 | in ((n,n),FuncUtil.Intfunc.fold (fn (i, c) => fn a => FuncUtil.Intpairfunc.update ((i,i), c) a) (snd v) FuncUtil.Intpairfunc.empty) : matrix | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 203 | end; | 
| 
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 matrix_of_list l = | 
| 32839 | 206 | let val m = length l | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 207 | in if m = 0 then matrix_0 (0,0) else | 
| 32839 | 208 | let val n = length (hd l) | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 209 | in ((m,n),itern 1 l (fn v => fn i => itern 1 v (fn c => fn j => FuncUtil.Intpairfunc.update ((i,j), c))) FuncUtil.Intpairfunc.empty) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 210 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 211 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 212 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 213 | (* Monomials. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 214 | |
| 32828 | 215 | fun monomial_eval assig m = | 
| 216 | FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => a */ rat_pow (FuncUtil.Ctermfunc.apply assig x) k) | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 217 | m rat_1; | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 218 | val monomial_1 = FuncUtil.Ctermfunc.empty; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 219 | |
| 32828 | 220 | 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 | 221 | |
| 32828 | 222 | val monomial_mul = | 
| 33002 | 223 | FuncUtil.Ctermfunc.combine Integer.add (K false); | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 224 | |
| 32828 | 225 | fun monomial_pow m k = | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 226 | if k = 0 then monomial_1 | 
| 39027 | 227 | else FuncUtil.Ctermfunc.map (fn _ => fn x => k * x) m; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 228 | |
| 32828 | 229 | fun monomial_divides m1 m2 = | 
| 230 | FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => FuncUtil.Ctermfunc.tryapplyd m2 x 0 >= k andalso a) m1 true;; | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 231 | |
| 32828 | 232 | fun monomial_div m1 m2 = | 
| 33002 | 233 | let val m = FuncUtil.Ctermfunc.combine Integer.add | 
| 39027 | 234 | (fn x => x = 0) m1 (FuncUtil.Ctermfunc.map (fn _ => fn x => ~ x) m2) | 
| 32828 | 235 | in if FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => k >= 0 andalso a) m true then m | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 236 | else error "monomial_div: non-divisible" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 237 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 238 | |
| 32839 | 239 | fun monomial_degree x m = | 
| 32828 | 240 | FuncUtil.Ctermfunc.tryapplyd m x 0;; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 241 | |
| 32828 | 242 | fun monomial_lcm m1 m2 = | 
| 243 | fold_rev (fn x => FuncUtil.Ctermfunc.update (x, max (monomial_degree x m1) (monomial_degree x m2))) | |
| 33042 | 244 | (union (is_equal o FuncUtil.cterm_ord) (FuncUtil.Ctermfunc.dom m1) (FuncUtil.Ctermfunc.dom m2)) (FuncUtil.Ctermfunc.empty); | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 245 | |
| 32839 | 246 | fun monomial_multidegree m = | 
| 32828 | 247 | FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => k + a) m 0;; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 248 | |
| 32828 | 249 | fun monomial_variables m = FuncUtil.Ctermfunc.dom m;; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 250 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 251 | (* Polynomials. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 252 | |
| 32828 | 253 | fun eval assig p = | 
| 254 | FuncUtil.Monomialfunc.fold (fn (m, c) => fn a => a +/ c */ monomial_eval assig m) p rat_0; | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 255 | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 256 | val poly_0 = FuncUtil.Monomialfunc.empty; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 257 | |
| 32839 | 258 | fun poly_isconst p = | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 259 | FuncUtil.Monomialfunc.fold (fn (m, c) => fn a => FuncUtil.Ctermfunc.is_empty m andalso a) p true; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 260 | |
| 32828 | 261 | fun poly_var x = FuncUtil.Monomialfunc.onefunc (monomial_var x,rat_1); | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 262 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 263 | fun poly_const c = | 
| 32828 | 264 | if c =/ rat_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 | 265 | |
| 32828 | 266 | fun poly_cmul c p = | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 267 | if c =/ rat_0 then poly_0 | 
| 39027 | 268 | 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 | 269 | |
| 39027 | 270 | fun poly_neg p = FuncUtil.Monomialfunc.map (K Rat.neg) p;; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 271 | |
| 32828 | 272 | fun poly_add p1 p2 = | 
| 273 | FuncUtil.Monomialfunc.combine (curry op +/) (fn x => x =/ rat_0) p1 p2; | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 274 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 275 | 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 | 276 | |
| 32828 | 277 | fun poly_cmmul (c,m) p = | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 278 | if c =/ rat_0 then poly_0 | 
| 32839 | 279 | else if FuncUtil.Ctermfunc.is_empty m | 
| 39027 | 280 | then FuncUtil.Monomialfunc.map (fn _ => fn d => c */ d) p | 
| 32828 | 281 | else FuncUtil.Monomialfunc.fold (fn (m', d) => fn a => (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 | 282 | |
| 32828 | 283 | fun poly_mul p1 p2 = | 
| 284 | 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 | 285 | |
| 32828 | 286 | fun poly_div p1 p2 = | 
| 32839 | 287 | if not(poly_isconst p2) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 288 | then error "poly_div: non-constant" else | 
| 32839 | 289 | let val c = eval FuncUtil.Ctermfunc.empty p2 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 290 | in if c =/ rat_0 then error "poly_div: division by zero" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 291 | else poly_cmul (Rat.inv c) p1 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 292 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 293 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 294 | fun poly_square p = poly_mul p p; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 295 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 296 | fun poly_pow p k = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 297 | if k = 0 then poly_const rat_1 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 298 | else if k = 1 then p | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 299 | else let val q = poly_square(poly_pow p (k div 2)) in | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 300 | if k mod 2 = 1 then poly_mul p q else q end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 301 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 302 | fun poly_exp p1 p2 = | 
| 32839 | 303 | if not(poly_isconst p2) | 
| 304 | then error "poly_exp: not a constant" | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 305 | else poly_pow p1 (int_of_rat (eval FuncUtil.Ctermfunc.empty p2)); | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 306 | |
| 32839 | 307 | fun degree x p = | 
| 32828 | 308 | FuncUtil.Monomialfunc.fold (fn (m,c) => fn a => max (monomial_degree x m) a) p 0; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 309 | |
| 32828 | 310 | fun multidegree p = | 
| 311 | FuncUtil.Monomialfunc.fold (fn (m, c) => 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 | 312 | |
| 32828 | 313 | fun poly_variables p = | 
| 33042 | 314 | sort FuncUtil.cterm_ord (FuncUtil.Monomialfunc.fold_rev (fn (m, c) => 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 | 315 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 316 | (* Order monomials for human presentation. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 317 | |
| 32828 | 318 | val humanorder_varpow = prod_ord FuncUtil.cterm_ord (rev_order o int_ord); | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 319 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 320 | local | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 321 | fun ord (l1,l2) = case (l1,l2) of | 
| 32839 | 322 | (_,[]) => LESS | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 323 | | ([],_) => GREATER | 
| 32839 | 324 | | (h1::t1,h2::t2) => | 
| 325 | (case humanorder_varpow (h1, h2) of | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 326 | LESS => LESS | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 327 | | EQUAL => ord (t1,t2) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 328 | | GREATER => GREATER) | 
| 32839 | 329 | in fun humanorder_monomial m1 m2 = | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 330 | ord (sort humanorder_varpow (FuncUtil.Ctermfunc.dest m1), | 
| 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 331 | sort humanorder_varpow (FuncUtil.Ctermfunc.dest m2)) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 332 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 333 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 334 | (* Conversions to strings. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 335 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 336 | fun string_of_vector min_size max_size (v:vector) = | 
| 32839 | 337 | let val n_raw = dim v | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 338 | in if n_raw = 0 then "[]" else | 
| 32839 | 339 | let | 
| 340 | val n = max min_size (min n_raw max_size) | |
| 341 | val xs = map (Rat.string_of_rat o (fn i => FuncUtil.Intfunc.tryapplyd (snd v) i rat_0)) (1 upto n) | |
| 32830 | 342 | in "[" ^ space_implode ", " xs ^ | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 343 | (if n_raw > max_size then ", ...]" else "]") | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 344 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 345 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 346 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 347 | fun string_of_matrix max_size (m:matrix) = | 
| 32839 | 348 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 349 | val (i_raw,j_raw) = dimensions m | 
| 32839 | 350 | val i = min max_size i_raw | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 351 | val j = min max_size j_raw | 
| 32839 | 352 | val rstr = map (fn k => string_of_vector j j (row k m)) (1 upto i) | 
| 32830 | 353 | in "["^ space_implode ";\n " rstr ^ | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 354 | (if j > max_size then "\n ...]" else "]") | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 355 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 356 | |
| 32839 | 357 | fun string_of_term t = | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 358 | case t of | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 359 |    a$b => "("^(string_of_term a)^" "^(string_of_term b)^")"
 | 
| 32839 | 360 | | Abs x => | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 361 | let val (xn, b) = Term.dest_abs x | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 362 | in "(\\"^xn^"."^(string_of_term b)^")" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 363 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 364 | | Const(s,_) => s | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 365 | | Free (s,_) => s | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 366 | | Var((s,_),_) => s | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 367 | | _ => error "string_of_term"; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 368 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 369 | val string_of_cterm = string_of_term o term_of; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 370 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 371 | fun string_of_varpow x k = | 
| 32839 | 372 | if k = 1 then string_of_cterm x | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 373 | else string_of_cterm x^"^"^string_of_int k; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 374 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 375 | fun string_of_monomial m = | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 376 | if FuncUtil.Ctermfunc.is_empty m then "1" else | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 377 | let val vps = fold_rev (fn (x,k) => fn a => string_of_varpow x k :: a) | 
| 32839 | 378 | (sort humanorder_varpow (FuncUtil.Ctermfunc.dest m)) [] | 
| 32830 | 379 | in space_implode "*" vps | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 380 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 381 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 382 | fun string_of_cmonomial (c,m) = | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 383 | if FuncUtil.Ctermfunc.is_empty m then Rat.string_of_rat c | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 384 | else if c =/ rat_1 then string_of_monomial m | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 385 | else Rat.string_of_rat c ^ "*" ^ string_of_monomial m;; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 386 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 387 | fun string_of_poly p = | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 388 | if FuncUtil.Monomialfunc.is_empty p then "<<0>>" else | 
| 32839 | 389 | let | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 390 | val cms = sort (fn ((m1,_),(m2,_)) => humanorder_monomial m1 m2) (FuncUtil.Monomialfunc.dest p) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 391 | val s = fold (fn (m,c) => fn a => | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 392 | if c </ rat_0 then a ^ " - " ^ string_of_cmonomial(Rat.neg c,m) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 393 | else a ^ " + " ^ string_of_cmonomial(c,m)) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 394 | cms "" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 395 | val s1 = String.substring (s, 0, 3) | 
| 32839 | 396 | val s2 = String.substring (s, 3, String.size s - 3) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 397 | in "<<" ^(if s1 = " + " then s2 else "-"^s2)^">>" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 398 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 399 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 400 | (* Conversion from HOL term. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 401 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 402 | local | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 403 |  val neg_tm = @{cterm "uminus :: real => _"}
 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 404 |  val add_tm = @{cterm "op + :: real => _"}
 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 405 |  val sub_tm = @{cterm "op - :: real => _"}
 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 406 |  val mul_tm = @{cterm "op * :: real => _"}
 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 407 |  val inv_tm = @{cterm "inverse :: real => _"}
 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 408 |  val div_tm = @{cterm "op / :: real => _"}
 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 409 |  val pow_tm = @{cterm "op ^ :: real => _"}
 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 410 |  val zero_tm = @{cterm "0:: real"}
 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 411 | val is_numeral = can (HOLogic.dest_number o term_of) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 412 | fun is_comb t = case t of _$_ => true | _ => false | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 413 | fun poly_of_term tm = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 414 | if tm aconvc zero_tm then poly_0 | 
| 32839 | 415 | else if RealArith.is_ratconst tm | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 416 | then poly_const(RealArith.dest_ratconst tm) | 
| 32839 | 417 | else | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 418 | (let val (lop,r) = Thm.dest_comb tm | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 419 | in if lop aconvc neg_tm then poly_neg(poly_of_term r) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 420 | else if lop aconvc inv_tm then | 
| 32839 | 421 | let val p = poly_of_term r | 
| 422 | in if poly_isconst p | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 423 | then poly_const(Rat.inv (eval FuncUtil.Ctermfunc.empty p)) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 424 | else error "poly_of_term: inverse of non-constant polyomial" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 425 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 426 | else (let val (opr,l) = Thm.dest_comb lop | 
| 32839 | 427 | in | 
| 428 | if opr aconvc pow_tm andalso is_numeral r | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 429 | then poly_pow (poly_of_term l) ((snd o HOLogic.dest_number o term_of) r) | 
| 32839 | 430 | else if opr aconvc add_tm | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 431 | then poly_add (poly_of_term l) (poly_of_term r) | 
| 32839 | 432 | else if opr aconvc sub_tm | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 433 | then poly_sub (poly_of_term l) (poly_of_term r) | 
| 32839 | 434 | else if opr aconvc mul_tm | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 435 | then poly_mul (poly_of_term l) (poly_of_term r) | 
| 32839 | 436 | else if opr aconvc div_tm | 
| 437 | then let | |
| 438 | val p = poly_of_term l | |
| 439 | val q = poly_of_term r | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 440 | in if poly_isconst q then poly_cmul (Rat.inv (eval FuncUtil.Ctermfunc.empty q)) p | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 441 | else error "poly_of_term: division by non-constant polynomial" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 442 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 443 | else poly_var tm | 
| 32839 | 444 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 445 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 446 |          handle CTERM ("dest_comb",_) => poly_var tm)
 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 447 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 448 |    handle CTERM ("dest_comb",_) => poly_var tm)
 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 449 | in | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 450 | val poly_of_term = fn tm => | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 451 |  if type_of (term_of tm) = @{typ real} then poly_of_term tm
 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 452 | else error "poly_of_term: term does not have real type" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 453 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 454 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 455 | (* 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 | 456 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 457 | fun sdpa_of_vector (v:vector) = | 
| 32839 | 458 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 459 | val n = dim v | 
| 32839 | 460 | val strs = map (decimalize 20 o (fn i => FuncUtil.Intfunc.tryapplyd (snd v) i rat_0)) (1 upto n) | 
| 32830 | 461 | in space_implode " " strs ^ "\n" | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 462 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 463 | |
| 32839 | 464 | fun triple_int_ord ((a,b,c),(a',b',c')) = | 
| 465 | prod_ord int_ord (prod_ord int_ord int_ord) | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 466 | ((a,(b,c)),(a',(b',c'))); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 467 | structure Inttriplefunc = FuncFun(type key = int*int*int val ord = triple_int_ord); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 468 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 469 | (* String for block diagonal matrix numbered k. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 470 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 471 | fun sdpa_of_blockdiagonal k m = | 
| 32839 | 472 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 473 | val pfx = string_of_int k ^" " | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 474 | val ents = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 475 | Inttriplefunc.fold (fn ((b,i,j), c) => fn a => if i > j then a else ((b,i,j),c)::a) m [] | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 476 | val entss = sort (triple_int_ord o pairself fst) ents | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 477 | in fold_rev (fn ((b,i,j),c) => fn a => | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 478 | pfx ^ string_of_int b ^ " " ^ string_of_int i ^ " " ^ string_of_int j ^ | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 479 | " " ^ decimalize 20 c ^ "\n" ^ a) entss "" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 480 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 481 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 482 | (* String for a matrix numbered k, in SDPA sparse format. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 483 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 484 | fun sdpa_of_matrix k (m:matrix) = | 
| 32839 | 485 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 486 | val pfx = string_of_int k ^ " 1 " | 
| 32839 | 487 | val ms = FuncUtil.Intpairfunc.fold (fn ((i,j), c) => fn a => if i > j then a else ((i,j),c)::a) (snd m) [] | 
| 488 | val mss = sort ((prod_ord int_ord int_ord) o pairself fst) ms | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 489 | in fold_rev (fn ((i,j),c) => fn a => | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 490 | pfx ^ string_of_int i ^ " " ^ string_of_int j ^ | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 491 | " " ^ decimalize 20 c ^ "\n" ^ a) mss "" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 492 | end;; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 493 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 494 | (* ------------------------------------------------------------------------- *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 495 | (* String in SDPA sparse format for standard SDP problem: *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 496 | (* *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 497 | (* X = v_1 * [M_1] + ... + v_m * [M_m] - [M_0] must be PSD *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 498 | (* Minimize obj_1 * v_1 + ... obj_m * v_m *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 499 | (* ------------------------------------------------------------------------- *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 500 | |
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 501 | fun sdpa_of_problem obj mats = | 
| 32839 | 502 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 503 | val m = length mats - 1 | 
| 32839 | 504 | val (n,_) = dimensions (hd mats) | 
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 505 | in | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 506 | string_of_int m ^ "\n" ^ | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 507 | "1\n" ^ | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 508 | string_of_int n ^ "\n" ^ | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 509 | sdpa_of_vector obj ^ | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 510 | fold_rev2 (fn k => fn m => fn a => sdpa_of_matrix (k - 1) m ^ a) (1 upto length mats) mats "" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 511 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 512 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 513 | fun index_char str chr pos = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 514 | if pos >= String.size str then ~1 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 515 | else if String.sub(str,pos) = chr then pos | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 516 | else index_char str chr (pos + 1); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 517 | fun rat_of_quotient (a,b) = if b = 0 then rat_0 else Rat.rat_of_quotient (a,b); | 
| 32839 | 518 | fun rat_of_string s = | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 519 | let val n = index_char s #"/" 0 in | 
| 33035 | 520 | if n = ~1 then s |> Int.fromString |> the |> Rat.rat_of_int | 
| 32839 | 521 | else | 
| 32949 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 522 | let val SOME numer = Int.fromString(String.substring(s,0,n)) | 
| 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 523 | val SOME den = Int.fromString (String.substring(s,n+1,String.size s - n - 1)) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 524 | in rat_of_quotient(numer, den) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 525 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 526 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 527 | |
| 36692 
54b64d4ad524
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
 haftmann parents: 
36350diff
changeset | 528 | fun isspace x = (x = " "); | 
| 
54b64d4ad524
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
 haftmann parents: 
36350diff
changeset | 529 | 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 | 530 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 531 | (* More parser basics. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 532 | |
| 32828 | 533 | val word = Scan.this_string | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 534 | fun token s = | 
| 32828 | 535 | Scan.repeat ($$ " ") |-- word s --| Scan.repeat ($$ " ") | 
| 536 | val numeral = Scan.one isnum | |
| 33906 | 537 | val decimalint = Scan.repeat1 numeral >> (rat_of_string o implode) | 
| 538 | val decimalfrac = Scan.repeat1 numeral | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 539 | >> (fn s => rat_of_string(implode s) // pow10 (length s)) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 540 | val decimalsig = | 
| 32828 | 541 | decimalint -- Scan.option (Scan.$$ "." |-- decimalfrac) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 542 | >> (fn (h,NONE) => h | (h,SOME x) => h +/ x) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 543 | fun signed prs = | 
| 32839 | 544 | $$ "-" |-- prs >> Rat.neg | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 545 | || $$ "+" |-- prs | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 546 | || prs; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 547 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 548 | fun emptyin def xs = if null xs then (def,xs) else Scan.fail xs | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 549 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 550 | val exponent = ($$ "e" || $$ "E") |-- signed decimalint; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 551 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 552 | val decimal = signed decimalsig -- (emptyin rat_0|| exponent) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 553 | >> (fn (h, x) => h */ pow10 (int_of_rat x)); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 554 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 555 | fun mkparser p s = | 
| 40627 
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
 wenzelm parents: 
39027diff
changeset | 556 | let val (x,rst) = p (raw_explode s) | 
| 32839 | 557 | in if null rst then x | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 558 | else error "mkparser: unparsed input" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 559 | end;; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 560 | |
| 32332 | 561 | (* Parse back csdp output. *) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 562 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 563 | fun ignore inp = ((),[]) | 
| 33067 
a70d5baa53ee
use plain Scan.repeat (NB: Scan.bulk is for cascading sources -- mostly interna use);
 wenzelm parents: 
33042diff
changeset | 564 | fun csdpoutput inp = | 
| 
a70d5baa53ee
use plain Scan.repeat (NB: Scan.bulk is for cascading sources -- mostly interna use);
 wenzelm parents: 
33042diff
changeset | 565 | ((decimal -- Scan.repeat (Scan.$$ " " |-- Scan.option decimal) >> | 
| 
a70d5baa53ee
use plain Scan.repeat (NB: Scan.bulk is for cascading sources -- mostly interna use);
 wenzelm parents: 
33042diff
changeset | 566 | (fn (h,to) => map_filter I ((SOME h)::to))) --| ignore >> vector_of_list) inp | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 567 | val parse_csdpoutput = mkparser csdpoutput | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 568 | |
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 569 | (* Run prover on a problem in linear form. *) | 
| 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 570 | |
| 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 571 | fun run_problem prover obj mats = | 
| 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 572 | parse_csdpoutput (prover (sdpa_of_problem obj mats)) | 
| 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 573 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 574 | (* 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 | 575 | (* 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 | 576 | (* 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 | 577 | (* are extreme numbers in the original problem. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 578 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 579 | (* Version for (int*int) keys *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 580 | local | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 581 | fun max_rat x y = if x </ y then y else x | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 582 | fun common_denominator fld amat acc = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 583 | fld (fn (m,c) => fn a => lcm_rat (denominator_rat c) a) amat acc | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 584 | fun maximal_element fld amat acc = | 
| 32839 | 585 | fld (fn (m,c) => fn maxa => max_rat maxa (abs_rat c)) amat acc | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 586 | fun float_of_rat x = let val (a,b) = Rat.quotient_of_rat x | 
| 41490 
0f1e411a1448
eliminated obsolete LargeInt -- Int is unbounded;
 wenzelm parents: 
41474diff
changeset | 587 | in Real.fromInt a / Real.fromInt b end; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 588 | in | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 589 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 590 | fun pi_scale_then solver (obj:vector) mats = | 
| 32839 | 591 | let | 
| 32828 | 592 | val cd1 = fold_rev (common_denominator FuncUtil.Intpairfunc.fold) mats (rat_1) | 
| 32839 | 593 | val cd2 = common_denominator FuncUtil.Intfunc.fold (snd obj) (rat_1) | 
| 39027 | 594 | val mats' = map (FuncUtil.Intpairfunc.map (fn _ => fn x => cd1 */ x)) mats | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 595 | val obj' = vector_cmul cd2 obj | 
| 32828 | 596 | val max1 = fold_rev (maximal_element FuncUtil.Intpairfunc.fold) mats' (rat_0) | 
| 32839 | 597 | val max2 = maximal_element FuncUtil.Intfunc.fold (snd obj') (rat_0) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 598 | val scal1 = pow2 (20 - trunc(Math.ln (float_of_rat max1) / Math.ln 2.0)) | 
| 32839 | 599 | val scal2 = pow2 (20 - trunc(Math.ln (float_of_rat max2) / Math.ln 2.0)) | 
| 39027 | 600 | val mats'' = map (FuncUtil.Intpairfunc.map (fn _ => fn x => x */ scal1)) mats' | 
| 32839 | 601 | val obj'' = vector_cmul scal2 obj' | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 602 | in solver obj'' mats'' | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 603 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 604 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 605 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 606 | (* 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 | 607 | (* 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 | 608 | (* 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 | 609 | (* are extreme numbers in the original problem. *) | 
| 
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 | (* Version for (int*int*int) keys *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 612 | local | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 613 | fun max_rat x y = if x </ y then y else x | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 614 | fun common_denominator fld amat acc = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 615 | fld (fn (m,c) => fn a => lcm_rat (denominator_rat c) a) amat acc | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 616 | fun maximal_element fld amat acc = | 
| 32839 | 617 | fld (fn (m,c) => fn maxa => max_rat maxa (abs_rat c)) amat acc | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 618 | fun float_of_rat x = let val (a,b) = Rat.quotient_of_rat x | 
| 41490 
0f1e411a1448
eliminated obsolete LargeInt -- Int is unbounded;
 wenzelm parents: 
41474diff
changeset | 619 | in Real.fromInt a / Real.fromInt b end; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 620 | fun int_of_float x = (trunc x handle Overflow => 0 | Domain => 0) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 621 | in | 
| 
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 | fun tri_scale_then solver (obj:vector) mats = | 
| 32839 | 624 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 625 | val cd1 = fold_rev (common_denominator Inttriplefunc.fold) mats (rat_1) | 
| 32839 | 626 | val cd2 = common_denominator FuncUtil.Intfunc.fold (snd obj) (rat_1) | 
| 39027 | 627 | val mats' = map (Inttriplefunc.map (fn _ => fn x => cd1 */ x)) mats | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 628 | val obj' = vector_cmul cd2 obj | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 629 | val max1 = fold_rev (maximal_element Inttriplefunc.fold) mats' (rat_0) | 
| 32839 | 630 | val max2 = maximal_element FuncUtil.Intfunc.fold (snd obj') (rat_0) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 631 | val scal1 = pow2 (20 - int_of_float(Math.ln (float_of_rat max1) / Math.ln 2.0)) | 
| 32839 | 632 | val scal2 = pow2 (20 - int_of_float(Math.ln (float_of_rat max2) / Math.ln 2.0)) | 
| 39027 | 633 | val mats'' = map (Inttriplefunc.map (fn _ => fn x => x */ scal1)) mats' | 
| 32839 | 634 | val obj'' = vector_cmul scal2 obj' | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 635 | in solver obj'' mats'' | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 636 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 637 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 638 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 639 | (* Round a vector to "nice" rationals. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 640 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 641 | fun nice_rational n x = round_rat (n */ x) // n;; | 
| 32839 | 642 | fun nice_vector n ((d,v) : vector) = | 
| 643 | (d, FuncUtil.Intfunc.fold (fn (i,c) => fn a => | |
| 644 | let val y = nice_rational n c | |
| 645 | in if c =/ rat_0 then a | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 646 | else FuncUtil.Intfunc.update (i,y) a end) v FuncUtil.Intfunc.empty):vector | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 647 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 648 | fun dest_ord f x = is_equal (f x); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 649 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 650 | (* Stuff for "equations" ((int*int*int)->num functions). *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 651 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 652 | fun tri_equation_cmul c eq = | 
| 39027 | 653 | if c =/ rat_0 then Inttriplefunc.empty 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 | 654 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 655 | fun tri_equation_add eq1 eq2 = Inttriplefunc.combine (curry op +/) (fn x => x =/ rat_0) eq1 eq2; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 656 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 657 | fun tri_equation_eval assig eq = | 
| 32839 | 658 | let fun value v = Inttriplefunc.apply assig v | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 659 | in Inttriplefunc.fold (fn (v, c) => fn a => a +/ value v */ c) eq rat_0 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 660 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 661 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 662 | (* Eliminate among linear equations: return unconstrained variables and *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 663 | (* assignments for the others in terms of them. We give one pseudo-variable *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 664 | (* "one" that's used for a constant term. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 665 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 666 | local | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 667 | fun extract_first p l = case l of (* FIXME : use find_first instead *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 668 | [] => error "extract_first" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 669 | | h::t => if p h then (h,t) else | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 670 | let val (k,s) = extract_first p t in (k,h::s) end | 
| 32839 | 671 | fun eliminate vars dun eqs = case vars of | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 672 | [] => if forall Inttriplefunc.is_empty eqs then dun | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 673 | else raise Unsolvable | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 674 | | v::vs => | 
| 32839 | 675 | ((let | 
| 676 | val (eq,oeqs) = extract_first (fn e => Inttriplefunc.defined e v) eqs | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 677 | val a = Inttriplefunc.apply eq v | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 678 | val eq' = tri_equation_cmul ((Rat.neg rat_1) // a) (Inttriplefunc.delete_safe v eq) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 679 | fun elim e = | 
| 32839 | 680 | let val b = Inttriplefunc.tryapplyd e v rat_0 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 681 | in if b =/ rat_0 then e else | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 682 | tri_equation_add e (tri_equation_cmul (Rat.neg b // a) eq) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 683 | end | 
| 39027 | 684 | in eliminate vs (Inttriplefunc.update (v,eq') (Inttriplefunc.map (K elim) dun)) (map elim oeqs) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 685 | end) | 
| 32332 | 686 | handle Failure _ => eliminate vs dun eqs) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 687 | in | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 688 | fun tri_eliminate_equations one vars eqs = | 
| 32839 | 689 | let | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 690 | val assig = eliminate vars Inttriplefunc.empty eqs | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 691 | val vs = Inttriplefunc.fold (fn (x, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig [] | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 692 | in (distinct (dest_ord triple_int_ord) vs, assig) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 693 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 694 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 695 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 696 | (* Eliminate all variables, in an essentially arbitrary order. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 697 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 698 | fun tri_eliminate_all_equations one = | 
| 32839 | 699 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 700 | fun choose_variable eq = | 
| 32839 | 701 | let val (v,_) = Inttriplefunc.choose eq | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 702 | in if is_equal (triple_int_ord(v,one)) then | 
| 32839 | 703 | let val eq' = Inttriplefunc.delete_safe v eq | 
| 704 | in if Inttriplefunc.is_empty eq' then error "choose_variable" | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 705 | else fst (Inttriplefunc.choose eq') | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 706 | end | 
| 32839 | 707 | else v | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 708 | end | 
| 32839 | 709 | fun eliminate dun eqs = case eqs of | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 710 | [] => dun | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 711 | | eq::oeqs => | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 712 | if Inttriplefunc.is_empty eq then eliminate dun oeqs else | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 713 | let val v = choose_variable eq | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 714 | val a = Inttriplefunc.apply eq v | 
| 32839 | 715 | val eq' = tri_equation_cmul ((Rat.rat_of_int ~1) // a) | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 716 | (Inttriplefunc.delete_safe v eq) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 717 | fun elim e = | 
| 32839 | 718 | let val b = Inttriplefunc.tryapplyd e v rat_0 | 
| 719 | in if b =/ rat_0 then e | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 720 | else tri_equation_add e (tri_equation_cmul (Rat.neg b // a) eq) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 721 | end | 
| 39027 | 722 | in eliminate (Inttriplefunc.update(v, eq') (Inttriplefunc.map (K elim) dun)) | 
| 32839 | 723 | (map elim oeqs) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 724 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 725 | in fn eqs => | 
| 32839 | 726 | let | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 727 | val assig = eliminate Inttriplefunc.empty eqs | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 728 | val vs = Inttriplefunc.fold (fn (x, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig [] | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 729 | in (distinct (dest_ord triple_int_ord) vs,assig) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 730 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 731 | end; | 
| 32839 | 732 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 733 | (* Solve equations by assigning arbitrary numbers. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 734 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 735 | fun tri_solve_equations one eqs = | 
| 32839 | 736 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 737 | val (vars,assigs) = tri_eliminate_all_equations one eqs | 
| 32839 | 738 | val vfn = fold_rev (fn v => Inttriplefunc.update(v,rat_0)) vars | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 739 | (Inttriplefunc.onefunc(one, Rat.rat_of_int ~1)) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 740 | val ass = | 
| 32839 | 741 | Inttriplefunc.combine (curry op +/) (K false) | 
| 39027 | 742 | (Inttriplefunc.map (K (tri_equation_eval vfn)) assigs) vfn | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 743 | in if forall (fn e => tri_equation_eval ass e =/ rat_0) eqs | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 744 | then Inttriplefunc.delete_safe one ass else raise Sanity | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 745 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 746 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 747 | (* 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 | 748 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 749 | fun tri_epoly_pmul p q acc = | 
| 32828 | 750 | FuncUtil.Monomialfunc.fold (fn (m1, c) => fn a => | 
| 751 | FuncUtil.Monomialfunc.fold (fn (m2,e) => fn b => | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 752 | let val m = monomial_mul m1 m2 | 
| 32839 | 753 | val es = FuncUtil.Monomialfunc.tryapplyd b m Inttriplefunc.empty | 
| 754 | in FuncUtil.Monomialfunc.update (m,tri_equation_add (tri_equation_cmul c e) es) b | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 755 | end) q a) p acc ; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 756 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 757 | (* Usual operations on equation-parametrized poly. *) | 
| 
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 | fun tri_epoly_cmul c l = | 
| 39027 | 760 | if c =/ rat_0 then Inttriplefunc.empty else Inttriplefunc.map (K (tri_equation_cmul c)) l;; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 761 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 762 | val tri_epoly_neg = tri_epoly_cmul (Rat.rat_of_int ~1); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 763 | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 764 | val tri_epoly_add = Inttriplefunc.combine tri_equation_add Inttriplefunc.is_empty; | 
| 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 | fun tri_epoly_sub p q = tri_epoly_add p (tri_epoly_neg q);; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 767 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 768 | (* Stuff for "equations" ((int*int)->num functions). *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 769 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 770 | fun pi_equation_cmul c eq = | 
| 39027 | 771 | if c =/ rat_0 then Inttriplefunc.empty 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 | 772 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 773 | fun pi_equation_add eq1 eq2 = Inttriplefunc.combine (curry op +/) (fn x => x =/ rat_0) eq1 eq2; | 
| 
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 | fun pi_equation_eval assig eq = | 
| 32839 | 776 | let fun value v = Inttriplefunc.apply assig v | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 777 | in Inttriplefunc.fold (fn (v, c) => fn a => a +/ value v */ c) eq rat_0 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 778 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 779 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 780 | (* Eliminate among linear equations: return unconstrained variables and *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 781 | (* assignments for the others in terms of them. We give one pseudo-variable *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 782 | (* "one" that's used for a constant term. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 783 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 784 | local | 
| 32839 | 785 | fun extract_first p l = case l of | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 786 | [] => error "extract_first" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 787 | | h::t => if p h then (h,t) else | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 788 | let val (k,s) = extract_first p t in (k,h::s) end | 
| 32839 | 789 | fun eliminate vars dun eqs = case vars of | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 790 | [] => if forall Inttriplefunc.is_empty eqs then dun | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 791 | else raise Unsolvable | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 792 | | v::vs => | 
| 32839 | 793 | let | 
| 794 | val (eq,oeqs) = extract_first (fn e => Inttriplefunc.defined e v) eqs | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 795 | val a = Inttriplefunc.apply eq v | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 796 | val eq' = pi_equation_cmul ((Rat.neg rat_1) // a) (Inttriplefunc.delete_safe v eq) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 797 | fun elim e = | 
| 32839 | 798 | let val b = Inttriplefunc.tryapplyd e v rat_0 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 799 | in if b =/ rat_0 then e else | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 800 | pi_equation_add e (pi_equation_cmul (Rat.neg b // a) eq) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 801 | end | 
| 39027 | 802 | in eliminate vs (Inttriplefunc.update (v,eq') (Inttriplefunc.map (K elim) dun)) (map elim oeqs) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 803 | end | 
| 32332 | 804 | handle Failure _ => eliminate vs dun eqs | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 805 | in | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 806 | fun pi_eliminate_equations one vars eqs = | 
| 32839 | 807 | let | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 808 | val assig = eliminate vars Inttriplefunc.empty eqs | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 809 | val vs = Inttriplefunc.fold (fn (x, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig [] | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 810 | in (distinct (dest_ord triple_int_ord) vs, assig) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 811 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 812 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 813 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 814 | (* Eliminate all variables, in an essentially arbitrary order. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 815 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 816 | fun pi_eliminate_all_equations one = | 
| 32839 | 817 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 818 | fun choose_variable eq = | 
| 32839 | 819 | let val (v,_) = Inttriplefunc.choose eq | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 820 | in if is_equal (triple_int_ord(v,one)) then | 
| 32839 | 821 | let val eq' = Inttriplefunc.delete_safe v eq | 
| 822 | in if Inttriplefunc.is_empty eq' then error "choose_variable" | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 823 | else fst (Inttriplefunc.choose eq') | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 824 | end | 
| 32839 | 825 | else v | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 826 | end | 
| 32839 | 827 | fun eliminate dun eqs = case eqs of | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 828 | [] => dun | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 829 | | eq::oeqs => | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 830 | if Inttriplefunc.is_empty eq then eliminate dun oeqs else | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 831 | let val v = choose_variable eq | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 832 | val a = Inttriplefunc.apply eq v | 
| 32839 | 833 | val eq' = pi_equation_cmul ((Rat.rat_of_int ~1) // a) | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 834 | (Inttriplefunc.delete_safe v eq) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 835 | fun elim e = | 
| 32839 | 836 | let val b = Inttriplefunc.tryapplyd e v rat_0 | 
| 837 | in if b =/ rat_0 then e | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 838 | else pi_equation_add e (pi_equation_cmul (Rat.neg b // a) eq) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 839 | end | 
| 39027 | 840 | in eliminate (Inttriplefunc.update(v, eq') (Inttriplefunc.map (K elim) dun)) | 
| 32839 | 841 | (map elim oeqs) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 842 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 843 | in fn eqs => | 
| 32839 | 844 | let | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 845 | val assig = eliminate Inttriplefunc.empty eqs | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 846 | val vs = Inttriplefunc.fold (fn (x, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig [] | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 847 | in (distinct (dest_ord triple_int_ord) vs,assig) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 848 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 849 | end; | 
| 32839 | 850 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 851 | (* Solve equations by assigning arbitrary numbers. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 852 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 853 | fun pi_solve_equations one eqs = | 
| 32839 | 854 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 855 | val (vars,assigs) = pi_eliminate_all_equations one eqs | 
| 32839 | 856 | val vfn = fold_rev (fn v => Inttriplefunc.update(v,rat_0)) vars | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 857 | (Inttriplefunc.onefunc(one, Rat.rat_of_int ~1)) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 858 | val ass = | 
| 32839 | 859 | Inttriplefunc.combine (curry op +/) (K false) | 
| 39027 | 860 | (Inttriplefunc.map (K (pi_equation_eval vfn)) assigs) vfn | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 861 | in if forall (fn e => pi_equation_eval ass e =/ rat_0) eqs | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 862 | then Inttriplefunc.delete_safe one ass else raise Sanity | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 863 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 864 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 865 | (* 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 | 866 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 867 | fun pi_epoly_pmul p q acc = | 
| 32828 | 868 | FuncUtil.Monomialfunc.fold (fn (m1, c) => fn a => | 
| 869 | FuncUtil.Monomialfunc.fold (fn (m2,e) => fn b => | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 870 | let val m = monomial_mul m1 m2 | 
| 32839 | 871 | val es = FuncUtil.Monomialfunc.tryapplyd b m Inttriplefunc.empty | 
| 872 | in FuncUtil.Monomialfunc.update (m,pi_equation_add (pi_equation_cmul c e) es) b | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 873 | end) q a) p acc ; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 874 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 875 | (* Usual operations on equation-parametrized poly. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 876 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 877 | fun pi_epoly_cmul c l = | 
| 39027 | 878 | if c =/ rat_0 then Inttriplefunc.empty else Inttriplefunc.map (K (pi_equation_cmul c)) l;; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 879 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 880 | val pi_epoly_neg = pi_epoly_cmul (Rat.rat_of_int ~1); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 881 | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 882 | val pi_epoly_add = Inttriplefunc.combine pi_equation_add Inttriplefunc.is_empty; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 883 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 884 | fun pi_epoly_sub p q = pi_epoly_add p (pi_epoly_neg q);; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 885 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 886 | fun allpairs f l1 l2 = fold_rev (fn x => (curry (op @)) (map (f x) l2)) l1 []; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 887 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 888 | (* 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 | 889 | (* 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 | 890 | (* 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 | 891 | (* vol 45, pp. 363--374, 1978. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 892 | (* *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 893 | (* 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 | 894 | (* 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 | 895 | (* quadratic form that will tend to display constants. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 896 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 897 | (* 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 | 898 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 899 | local | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 900 | fun diagonalize n i m = | 
| 32839 | 901 | if FuncUtil.Intpairfunc.is_empty (snd m) then [] | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 902 | else | 
| 32839 | 903 | let val a11 = FuncUtil.Intpairfunc.tryapplyd (snd m) (i,i) rat_0 | 
| 32332 | 904 | in if a11 </ rat_0 then raise Failure "diagonalize: not PSD" | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 905 | else if a11 =/ rat_0 then | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 906 | if FuncUtil.Intfunc.is_empty (snd (row i m)) then diagonalize n (i + 1) m | 
| 32332 | 907 | else raise Failure "diagonalize: not PSD ___ " | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 908 | else | 
| 32839 | 909 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 910 | val v = row i m | 
| 32839 | 911 | val v' = (fst v, FuncUtil.Intfunc.fold (fn (i, c) => fn a => | 
| 912 | let val y = c // a11 | |
| 913 | in if y = rat_0 then a else FuncUtil.Intfunc.update (i,y) a | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 914 | end) (snd v) FuncUtil.Intfunc.empty) | 
| 32828 | 915 | fun upt0 x y a = if y = rat_0 then a else FuncUtil.Intpairfunc.update (x,y) a | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 916 | val m' = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 917 | ((n,n), | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 918 | iter (i+1,n) (fn j => | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 919 | iter (i+1,n) (fn k => | 
| 32828 | 920 | (upt0 (j,k) (FuncUtil.Intpairfunc.tryapplyd (snd m) (j,k) rat_0 -/ FuncUtil.Intfunc.tryapplyd (snd v) j rat_0 */ FuncUtil.Intfunc.tryapplyd (snd v') k rat_0)))) | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 921 | FuncUtil.Intpairfunc.empty) | 
| 32839 | 922 | in (a11,v')::diagonalize n (i + 1) m' | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 923 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 924 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 925 | in | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 926 | fun diag m = | 
| 32839 | 927 | let | 
| 928 | val nn = dimensions m | |
| 929 | val n = fst nn | |
| 930 | in if snd nn <> n then error "diagonalize: non-square matrix" | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 931 | else diagonalize n 1 m | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 932 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 933 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 934 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 935 | fun gcd_rat a b = Rat.rat_of_int (Integer.gcd (int_of_rat a) (int_of_rat b)); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 936 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 937 | (* Adjust a diagonalization to collect rationals at the start. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 938 | (* FIXME : Potentially polymorphic keys, but here only: integers!! *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 939 | local | 
| 32828 | 940 | fun upd0 x y a = if y =/ rat_0 then a else FuncUtil.Intfunc.update(x,y) a; | 
| 32839 | 941 | fun mapa f (d,v) = | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 942 | (d, FuncUtil.Intfunc.fold (fn (i,c) => fn a => upd0 i (f c) a) v FuncUtil.Intfunc.empty) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 943 | fun adj (c,l) = | 
| 32839 | 944 | let val a = | 
| 945 | FuncUtil.Intfunc.fold (fn (i,c) => fn a => lcm_rat a (denominator_rat c)) | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 946 | (snd l) rat_1 // | 
| 32839 | 947 | FuncUtil.Intfunc.fold (fn (i,c) => fn a => gcd_rat a (numerator_rat c)) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 948 | (snd l) rat_0 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 949 | in ((c // (a */ a)),mapa (fn x => a */ x) l) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 950 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 951 | in | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 952 | fun deration d = if null d then (rat_0,d) else | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 953 | let val d' = map adj d | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 954 | val a = fold (lcm_rat o denominator_rat o fst) d' rat_1 // | 
| 32839 | 955 | fold (gcd_rat o numerator_rat o fst) d' rat_0 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 956 | in ((rat_1 // a),map (fn (c,l) => (a */ c,l)) d') | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 957 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 958 | end; | 
| 32839 | 959 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 960 | (* Enumeration of monomials with given multidegree bound. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 961 | |
| 32839 | 962 | fun enumerate_monomials d vars = | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 963 | if d < 0 then [] | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 964 | else if d = 0 then [FuncUtil.Ctermfunc.empty] | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 965 | else if null vars then [monomial_1] else | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 966 | let val alts = | 
| 33063 | 967 | map_range (fn k => let val oths = enumerate_monomials (d - k) (tl vars) | 
| 968 | in map (fn ks => if k = 0 then ks else FuncUtil.Ctermfunc.update (hd vars, k) ks) oths end) (d + 1) | |
| 32830 | 969 | in flat alts | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 970 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 971 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 972 | (* Enumerate products of distinct input polys with degree <= d. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 973 | (* We ignore any constant input polynomials. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 974 | (* 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 | 975 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 976 | fun enumerate_products d pols = | 
| 32839 | 977 | if d = 0 then [(poly_const rat_1,RealArith.Rational_lt rat_1)] | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 978 | else if d < 0 then [] else | 
| 32839 | 979 | case pols of | 
| 32828 | 980 | [] => [(poly_const rat_1,RealArith.Rational_lt rat_1)] | 
| 32839 | 981 | | (p,b)::ps => | 
| 982 | let val e = multidegree p | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 983 | in if e = 0 then enumerate_products d ps else | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 984 | enumerate_products d ps @ | 
| 32828 | 985 | map (fn (q,c) => (poly_mul p q,RealArith.Product(b,c))) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 986 | (enumerate_products (d - e) ps) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 987 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 988 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 989 | (* 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 | 990 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 991 | fun epoly_of_poly p = | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 992 | FuncUtil.Monomialfunc.fold (fn (m,c) => fn a => FuncUtil.Monomialfunc.update (m, Inttriplefunc.onefunc ((0,0,0), Rat.neg c)) a) p FuncUtil.Monomialfunc.empty; | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 993 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 994 | (* String for block diagonal matrix numbered k. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 995 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 996 | fun sdpa_of_blockdiagonal k m = | 
| 32839 | 997 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 998 | val pfx = string_of_int k ^" " | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 999 | val ents = | 
| 32839 | 1000 | Inttriplefunc.fold | 
| 1001 | (fn ((b,i,j),c) => fn a => if i > j then a else ((b,i,j),c)::a) | |
| 1002 | m [] | |
| 1003 | val entss = sort (triple_int_ord o pairself fst) ents | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1004 | in fold_rev (fn ((b,i,j),c) => fn a => | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1005 | pfx ^ string_of_int b ^ " " ^ string_of_int i ^ " " ^ string_of_int j ^ | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1006 | " " ^ decimalize 20 c ^ "\n" ^ a) entss "" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1007 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1008 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1009 | (* 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 | 1010 | |
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 1011 | fun sdpa_of_blockproblem nblocks blocksizes obj mats = | 
| 32839 | 1012 | let val m = length mats - 1 | 
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 1013 | in | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1014 | string_of_int m ^ "\n" ^ | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1015 | string_of_int nblocks ^ "\n" ^ | 
| 32830 | 1016 | (space_implode " " (map string_of_int blocksizes)) ^ | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1017 | "\n" ^ | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1018 | sdpa_of_vector obj ^ | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1019 | fold_rev2 (fn k => fn m => fn a => sdpa_of_blockdiagonal (k - 1) m ^ a) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1020 | (1 upto length mats) mats "" | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1021 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1022 | |
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 1023 | (* Run prover on a problem in block diagonal form. *) | 
| 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 1024 | |
| 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 1025 | fun run_blockproblem prover nblocks blocksizes obj mats= | 
| 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 1026 | 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: 
32150diff
changeset | 1027 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1028 | (* 3D versions of matrix operations to consider blocks separately. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1029 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1030 | val bmatrix_add = Inttriplefunc.combine (curry op +/) (fn x => x =/ rat_0); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1031 | fun bmatrix_cmul c bm = | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 1032 | if c =/ rat_0 then Inttriplefunc.empty | 
| 39027 | 1033 | 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 | 1034 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1035 | val bmatrix_neg = bmatrix_cmul (Rat.rat_of_int ~1); | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1036 | fun bmatrix_sub m1 m2 = bmatrix_add m1 (bmatrix_neg m2);; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1037 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1038 | (* Smash a block matrix into components. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1039 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1040 | fun blocks blocksizes bm = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1041 | map (fn (bs,b0) => | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1042 | let val m = Inttriplefunc.fold | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 1043 | (fn ((b,i,j),c) => fn a => if b = b0 then FuncUtil.Intpairfunc.update ((i,j),c) a else a) bm FuncUtil.Intpairfunc.empty | 
| 32839 | 1044 | val d = FuncUtil.Intpairfunc.fold (fn ((i,j),c) => fn a => max a (max i j)) m 0 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1045 | in (((bs,bs),m):matrix) end) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1046 | (blocksizes ~~ (1 upto length blocksizes));; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1047 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1048 | (* FIXME : Get rid of this !!!*) | 
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 1049 | local | 
| 32332 | 1050 | fun tryfind_with msg f [] = raise Failure msg | 
| 1051 | | tryfind_with msg f (x::xs) = (f x handle Failure s => tryfind_with s f xs); | |
| 32839 | 1052 | in | 
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 1053 | fun tryfind f = tryfind_with "tryfind" f | 
| 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 1054 | end | 
| 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 1055 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1056 | (* Positiv- and Nullstellensatz. Flag "linf" forces a linear representation. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1057 | |
| 32839 | 1058 | |
| 38805 | 1059 | fun real_positivnullstellensatz_general ctxt prover linf d eqs leqs pol = | 
| 32839 | 1060 | let | 
| 33042 | 1061 | val vars = fold_rev (union (op aconvc) o poly_variables) | 
| 1062 | (pol :: eqs @ map fst leqs) [] | |
| 32839 | 1063 | val monoid = if linf then | 
| 32828 | 1064 | (poly_const rat_1,RealArith.Rational_lt rat_1):: | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1065 | (filter (fn (p,c) => multidegree p <= d) leqs) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1066 | else enumerate_products d leqs | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1067 | val nblocks = length monoid | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1068 | fun mk_idmultiplier k p = | 
| 32839 | 1069 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1070 | val e = d - multidegree p | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1071 | val mons = enumerate_monomials e vars | 
| 32839 | 1072 | val nons = mons ~~ (1 upto length mons) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1073 | in (mons, | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 1074 | fold_rev (fn (m,n) => FuncUtil.Monomialfunc.update(m,Inttriplefunc.onefunc((~k,~n,n),rat_1))) nons FuncUtil.Monomialfunc.empty) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1075 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1076 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1077 | fun mk_sqmultiplier k (p,c) = | 
| 32839 | 1078 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1079 | val e = (d - multidegree p) div 2 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1080 | val mons = enumerate_monomials e vars | 
| 32839 | 1081 | val nons = mons ~~ (1 upto length mons) | 
| 1082 | in (mons, | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1083 | fold_rev (fn (m1,n1) => | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1084 | fold_rev (fn (m2,n2) => fn a => | 
| 32839 | 1085 | let val m = monomial_mul m1 m2 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1086 | in if n1 > n2 then a else | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1087 | let val c = if n1 = n2 then rat_1 else rat_2 | 
| 32839 | 1088 | val e = FuncUtil.Monomialfunc.tryapplyd a m Inttriplefunc.empty | 
| 32828 | 1089 | in FuncUtil.Monomialfunc.update(m, tri_equation_add (Inttriplefunc.onefunc((k,n1,n2), c)) e) a | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1090 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1091 | end) nons) | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 1092 | nons FuncUtil.Monomialfunc.empty) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1093 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1094 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1095 | val (sqmonlist,sqs) = split_list (map2 mk_sqmultiplier (1 upto length monoid) monoid) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1096 | val (idmonlist,ids) = split_list(map2 mk_idmultiplier (1 upto length eqs) eqs) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1097 | val blocksizes = map length sqmonlist | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1098 | val bigsum = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1099 | fold_rev2 (fn p => fn q => fn a => tri_epoly_pmul p q a) eqs ids | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1100 | (fold_rev2 (fn (p,c) => fn s => fn a => tri_epoly_pmul p s a) monoid sqs | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1101 | (epoly_of_poly(poly_neg pol))) | 
| 32828 | 1102 | val eqns = FuncUtil.Monomialfunc.fold (fn (m,e) => fn a => e::a) bigsum [] | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1103 | val (pvs,assig) = tri_eliminate_all_equations (0,0,0) eqns | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1104 | val qvars = (0,0,0)::pvs | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1105 | val allassig = fold_rev (fn v => Inttriplefunc.update(v,(Inttriplefunc.onefunc(v,rat_1)))) pvs assig | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1106 | fun mk_matrix v = | 
| 32839 | 1107 | Inttriplefunc.fold (fn ((b,i,j), ass) => fn m => | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1108 | if b < 0 then m else | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1109 | let val c = Inttriplefunc.tryapplyd ass v rat_0 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1110 | in if c = rat_0 then m else | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1111 | Inttriplefunc.update ((b,j,i), c) (Inttriplefunc.update ((b,i,j), c) m) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1112 | end) | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 1113 | allassig Inttriplefunc.empty | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1114 | val diagents = Inttriplefunc.fold | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1115 | (fn ((b,i,j), e) => fn a => if b > 0 andalso i = j then tri_equation_add e a else a) | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 1116 | allassig Inttriplefunc.empty | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1117 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1118 | val mats = map mk_matrix qvars | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1119 | val obj = (length pvs, | 
| 32828 | 1120 | itern 1 pvs (fn v => fn i => FuncUtil.Intfunc.updatep iszero (i,Inttriplefunc.tryapplyd diagents v rat_0)) | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 1121 | FuncUtil.Intfunc.empty) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1122 | val raw_vec = if null pvs then vector_0 0 | 
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 1123 | else tri_scale_then (run_blockproblem prover nblocks blocksizes) obj mats | 
| 32828 | 1124 | fun int_element (d,v) i = FuncUtil.Intfunc.tryapplyd v i rat_0 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1125 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1126 | fun find_rounding d = | 
| 32839 | 1127 | let | 
| 32949 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 1128 | val _ = | 
| 38805 | 1129 | if Config.get ctxt trace | 
| 32949 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 1130 |       then writeln ("Trying rounding with limit "^Rat.string_of_rat d ^ "\n")
 | 
| 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 1131 | else () | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1132 | val vec = nice_vector d raw_vec | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1133 | val blockmat = iter (1,dim vec) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1134 | (fn i => fn a => bmatrix_add (bmatrix_cmul (int_element vec i) (nth mats i)) a) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1135 | (bmatrix_neg (nth mats 0)) | 
| 32839 | 1136 | val allmats = blocks blocksizes blockmat | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1137 | in (vec,map diag allmats) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1138 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1139 | val (vec,ratdias) = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1140 | if null pvs then find_rounding rat_1 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1141 | else tryfind find_rounding (map Rat.rat_of_int (1 upto 31) @ | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1142 | map pow2 (5 upto 66)) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1143 | val newassigs = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1144 | fold_rev (fn k => Inttriplefunc.update (nth pvs (k - 1), int_element vec k)) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1145 | (1 upto dim vec) (Inttriplefunc.onefunc ((0,0,0), Rat.rat_of_int ~1)) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1146 | val finalassigs = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1147 | Inttriplefunc.fold (fn (v,e) => fn a => Inttriplefunc.update(v, tri_equation_eval newassigs e) a) allassig newassigs | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1148 | fun poly_of_epoly p = | 
| 32828 | 1149 | FuncUtil.Monomialfunc.fold (fn (v,e) => fn a => FuncUtil.Monomialfunc.updatep iszero (v,tri_equation_eval finalassigs e) a) | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 1150 | p FuncUtil.Monomialfunc.empty | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1151 | fun mk_sos mons = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1152 | let fun mk_sq (c,m) = | 
| 32828 | 1153 | (c,fold_rev (fn k=> fn a => FuncUtil.Monomialfunc.updatep iszero (nth mons (k - 1), int_element m k) a) | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 1154 | (1 upto length mons) FuncUtil.Monomialfunc.empty) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1155 | in map mk_sq | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1156 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1157 | val sqs = map2 mk_sos sqmonlist ratdias | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1158 | val cfs = map poly_of_epoly ids | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1159 | val msq = filter (fn (a,b) => not (null b)) (map2 pair monoid sqs) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1160 | fun eval_sq sqs = fold_rev (fn (c,q) => poly_add (poly_cmul c (poly_mul q q))) sqs poly_0 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1161 | val sanity = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1162 | fold_rev (fn ((p,c),s) => poly_add (poly_mul p (eval_sq s))) msq | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1163 | (fold_rev2 (fn p => fn q => poly_add (poly_mul p q)) cfs eqs | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1164 | (poly_neg pol)) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1165 | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 1166 | in if not(FuncUtil.Monomialfunc.is_empty sanity) then raise Sanity else | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1167 | (cfs,map (fn (a,b) => (snd a,b)) msq) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1168 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1169 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1170 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1171 | (* Iterative deepening. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1172 | |
| 32839 | 1173 | fun deepen f n = | 
| 32949 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 1174 |   (writeln ("Searching with depth limit " ^ string_of_int n);
 | 
| 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 1175 |     (f n handle Failure s => (writeln ("failed with message: " ^ s); deepen f (n + 1))));
 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1176 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1177 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1178 | (* 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 | 1179 | |
| 32828 | 1180 | 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 | 1181 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1182 | fun cterm_of_sos (pr,sqs) = if null sqs then pr | 
| 32830 | 1183 | 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 | 1184 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1185 | (* Interface to HOL. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1186 | local | 
| 32828 | 1187 | open Conv | 
| 1188 | val concl = Thm.dest_arg o cprop_of | |
| 35408 | 1189 | fun simple_cterm_ord t u = Term_Ord.fast_term_ord (term_of t, term_of u) = LESS | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1190 | in | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1191 | (* FIXME: Replace tryfind by get_first !! *) | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1192 | fun real_nonlinear_prover proof_method ctxt = | 
| 32839 | 1193 | let | 
| 36753 
5cf4e9128f22
renamed Normalizer to the more specific Semiring_Normalizer
 haftmann parents: 
36751diff
changeset | 1194 |   val {add,mul,neg,pow,sub,main} =  Semiring_Normalizer.semiring_normalizers_ord_wrapper ctxt
 | 
| 
5cf4e9128f22
renamed Normalizer to the more specific Semiring_Normalizer
 haftmann parents: 
36751diff
changeset | 1195 |       (the (Semiring_Normalizer.match ctxt @{cterm "(0::real) + 1"}))
 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1196 | simple_cterm_ord | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1197 | val (real_poly_add_conv,real_poly_mul_conv,real_poly_neg_conv, | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1198 | real_poly_pow_conv,real_poly_sub_conv,real_poly_conv) = (add,mul,neg,pow,sub,main) | 
| 32839 | 1199 | fun mainf cert_choice translator (eqs,les,lts) = | 
| 1200 | let | |
| 32828 | 1201 | val eq0 = map (poly_of_term o Thm.dest_arg1 o concl) eqs | 
| 1202 | val le0 = map (poly_of_term o Thm.dest_arg o concl) les | |
| 1203 | val lt0 = map (poly_of_term o Thm.dest_arg o concl) lts | |
| 33063 | 1204 | val eqp0 = map_index (fn (i, t) => (t,RealArith.Axiom_eq i)) eq0 | 
| 1205 | val lep0 = map_index (fn (i, t) => (t,RealArith.Axiom_le i)) le0 | |
| 1206 | val ltp0 = map_index (fn (i, t) => (t,RealArith.Axiom_lt i)) lt0 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1207 | val (keq,eq) = List.partition (fn (p,_) => multidegree p = 0) eqp0 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1208 | val (klep,lep) = List.partition (fn (p,_) => multidegree p = 0) lep0 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1209 | val (kltp,ltp) = List.partition (fn (p,_) => multidegree p = 0) ltp0 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1210 | fun trivial_axiom (p,ax) = | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1211 | case ax of | 
| 32839 | 1212 | RealArith.Axiom_eq n => if eval FuncUtil.Ctermfunc.empty p <>/ Rat.zero then nth eqs n | 
| 32332 | 1213 | else raise Failure "trivial_axiom: Not a trivial axiom" | 
| 32839 | 1214 | | RealArith.Axiom_le n => if eval FuncUtil.Ctermfunc.empty p </ Rat.zero then nth les n | 
| 32332 | 1215 | else raise Failure "trivial_axiom: Not a trivial axiom" | 
| 32839 | 1216 | | RealArith.Axiom_lt n => if eval FuncUtil.Ctermfunc.empty p <=/ Rat.zero then nth lts n | 
| 32332 | 1217 | else raise Failure "trivial_axiom: Not a trivial axiom" | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1218 | | _ => error "trivial_axiom: Not a trivial axiom" | 
| 32839 | 1219 | in | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1220 | (let val th = tryfind trivial_axiom (keq @ klep @ kltp) | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1221 | in | 
| 36751 
7f1da69cacb3
split of semiring normalization from Groebner theory; moved field_comp_conv to Numeral_Simproces
 haftmann parents: 
36721diff
changeset | 1222 | (fconv_rule (arg_conv (arg1_conv real_poly_conv) then_conv Numeral_Simprocs.field_comp_conv) th, RealArith.Trivial) | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1223 | end) | 
| 32839 | 1224 | handle Failure _ => | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1225 | (let val proof = | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1226 | (case proof_method of Certificate certs => | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1227 | (* choose certificate *) | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1228 | let | 
| 32828 | 1229 | fun chose_cert [] (RealArith.Cert c) = c | 
| 1230 | | chose_cert (RealArith.Left::s) (RealArith.Branch (l, _)) = chose_cert s l | |
| 1231 | | chose_cert (RealArith.Right::s) (RealArith.Branch (_, r)) = chose_cert s r | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1232 | | chose_cert _ _ = error "certificate tree in invalid form" | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1233 | in | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1234 | chose_cert cert_choice certs | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1235 | end | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1236 | | Prover prover => | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1237 | (* call prover *) | 
| 32839 | 1238 | let | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1239 | val pol = fold_rev poly_mul (map fst ltp) (poly_const Rat.one) | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1240 | val leq = lep @ ltp | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1241 | fun tryall d = | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1242 | let val e = multidegree pol | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1243 | val k = if e = 0 then 0 else d div e | 
| 32839 | 1244 | val eq' = map fst eq | 
| 38805 | 1245 | in tryfind (fn i => (d,i,real_positivnullstellensatz_general ctxt prover false d eq' leq | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1246 | (poly_neg(poly_pow pol i)))) | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1247 | (0 upto k) | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1248 | end | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1249 | val (d,i,(cert_ideal,cert_cone)) = deepen tryall 0 | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1250 | val proofs_ideal = | 
| 32828 | 1251 | map2 (fn q => fn (p,ax) => RealArith.Eqmul(q,ax)) cert_ideal eq | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1252 | val proofs_cone = map cterm_of_sos cert_cone | 
| 32828 | 1253 | val proof_ne = if null ltp then RealArith.Rational_lt Rat.one else | 
| 32839 | 1254 | let val p = foldr1 RealArith.Product (map snd ltp) | 
| 32828 | 1255 | in funpow i (fn q => RealArith.Product(p,q)) (RealArith.Rational_lt Rat.one) | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1256 | end | 
| 32839 | 1257 | in | 
| 1258 | foldr1 RealArith.Sum (proof_ne :: proofs_ideal @ proofs_cone) | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1259 | end) | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1260 | in | 
| 32828 | 1261 | (translator (eqs,les,lts) proof, RealArith.Cert proof) | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1262 | end) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1263 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1264 | in mainf end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1265 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1266 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1267 | fun C f x y = f y x; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1268 | (* FIXME : This is very bad!!!*) | 
| 32839 | 1269 | fun subst_conv eqs t = | 
| 1270 | let | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1271 | val t' = fold (Thm.cabs o Thm.lhs_of) eqs t | 
| 36945 | 1272 | in Conv.fconv_rule (Thm.beta_conversion true) (fold (C Thm.combination) eqs (Thm.reflexive t')) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1273 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1274 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1275 | (* A wrapper that tries to substitute away variables first. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1276 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1277 | local | 
| 32828 | 1278 | open Conv | 
| 35408 | 1279 | fun simple_cterm_ord t u = Term_Ord.fast_term_ord (term_of t, term_of u) = LESS | 
| 32828 | 1280 | val concl = Thm.dest_arg o cprop_of | 
| 32839 | 1281 | val shuffle1 = | 
| 36350 | 1282 |    fconv_rule (rewr_conv @{lemma "(a + x == y) == (x == y - (a::real))" by (atomize (full)) (simp add: field_simps) })
 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1283 | val shuffle2 = | 
| 36350 | 1284 |     fconv_rule (rewr_conv @{lemma "(x + a == y) ==  (x == y - (a::real))" by (atomize (full)) (simp add: field_simps)})
 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1285 | fun substitutable_monomial fvs tm = case term_of tm of | 
| 32839 | 1286 |     Free(_,@{typ real}) => if not (member (op aconvc) fvs tm) then (Rat.one,tm)
 | 
| 32332 | 1287 | else raise Failure "substitutable_monomial" | 
| 32839 | 1288 |   | @{term "op * :: real => _"}$c$(t as Free _ ) =>
 | 
| 32828 | 1289 | if RealArith.is_ratconst (Thm.dest_arg1 tm) andalso not (member (op aconvc) fvs (Thm.dest_arg tm)) | 
| 1290 | then (RealArith.dest_ratconst (Thm.dest_arg1 tm),Thm.dest_arg tm) else raise Failure "substitutable_monomial" | |
| 32839 | 1291 |   | @{term "op + :: real => _"}$s$t =>
 | 
| 32828 | 1292 | (substitutable_monomial (Thm.add_cterm_frees (Thm.dest_arg tm) fvs) (Thm.dest_arg1 tm) | 
| 1293 | handle Failure _ => substitutable_monomial (Thm.add_cterm_frees (Thm.dest_arg1 tm) fvs) (Thm.dest_arg tm)) | |
| 32332 | 1294 | | _ => raise Failure "substitutable_monomial" | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1295 | |
| 32839 | 1296 | fun isolate_variable v th = | 
| 32828 | 1297 | let val w = Thm.dest_arg1 (cprop_of th) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1298 | in if v aconvc w then th | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1299 | else case term_of w of | 
| 32839 | 1300 |            @{term "op + :: real => _"}$s$t =>
 | 
| 1301 | if Thm.dest_arg1 w aconvc v then shuffle2 th | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1302 | else isolate_variable v (shuffle1 th) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1303 | | _ => error "isolate variable : This should not happen?" | 
| 32839 | 1304 | end | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1305 | in | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1306 | |
| 32268 
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
 Philipp Meyer parents: 
32150diff
changeset | 1307 | fun real_nonlinear_subst_prover prover ctxt = | 
| 32839 | 1308 | let | 
| 36753 
5cf4e9128f22
renamed Normalizer to the more specific Semiring_Normalizer
 haftmann parents: 
36751diff
changeset | 1309 |   val {add,mul,neg,pow,sub,main} =  Semiring_Normalizer.semiring_normalizers_ord_wrapper ctxt
 | 
| 
5cf4e9128f22
renamed Normalizer to the more specific Semiring_Normalizer
 haftmann parents: 
36751diff
changeset | 1310 |       (the (Semiring_Normalizer.match ctxt @{cterm "(0::real) + 1"}))
 | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1311 | simple_cterm_ord | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1312 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1313 | val (real_poly_add_conv,real_poly_mul_conv,real_poly_neg_conv, | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1314 | real_poly_pow_conv,real_poly_sub_conv,real_poly_conv) = (add,mul,neg,pow,sub,main) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1315 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1316 | fun make_substitution th = | 
| 32839 | 1317 | let | 
| 32828 | 1318 | val (c,v) = substitutable_monomial [] (Thm.dest_arg1(concl th)) | 
| 1319 |     val th1 = Drule.arg_cong_rule (Thm.capply @{cterm "op * :: real => _"} (RealArith.cterm_of_rat (Rat.inv c))) (mk_meta_eq th)
 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1320 | val th2 = fconv_rule (binop_conv real_poly_mul_conv) th1 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1321 | in fconv_rule (arg_conv real_poly_conv) (isolate_variable v th2) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1322 | end | 
| 32839 | 1323 | fun oprconv cv ct = | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1324 | let val g = Thm.dest_fun2 ct | 
| 32839 | 1325 |     in if g aconvc @{cterm "op <= :: real => _"}
 | 
| 1326 |          orelse g aconvc @{cterm "op < :: real => _"}
 | |
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1327 | then arg_conv cv ct else arg1_conv cv ct | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1328 | end | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1329 | fun mainf cert_choice translator = | 
| 32839 | 1330 | let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1331 | fun substfirst(eqs,les,lts) = | 
| 32839 | 1332 | ((let | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1333 | val eth = tryfind make_substitution eqs | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1334 | val modify = fconv_rule (arg_conv (oprconv(subst_conv [eth] then_conv real_poly_conv))) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1335 | in substfirst | 
| 32839 | 1336 | (filter_out (fn t => (Thm.dest_arg1 o Thm.dest_arg o cprop_of) t | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1337 |                                    aconvc @{cterm "0::real"}) (map modify eqs),
 | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1338 | map modify les,map modify lts) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1339 | end) | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1340 | handle Failure _ => real_nonlinear_prover prover ctxt cert_choice translator (rev eqs, rev les, rev lts)) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1341 | in substfirst | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1342 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1343 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1344 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1345 | in mainf | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1346 | end | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1347 | |
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1348 | (* Overall function. *) | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1349 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32332diff
changeset | 1350 | fun real_sos prover ctxt = | 
| 32828 | 1351 | RealArith.gen_prover_real_arith ctxt (real_nonlinear_subst_prover prover ctxt) | 
| 31119 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1352 | end; | 
| 
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
 chaieb parents: diff
changeset | 1353 | |
| 32839 | 1354 | val known_sos_constants = | 
| 1355 |   [@{term "op ==>"}, @{term "Trueprop"},
 | |
| 38795 
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
 haftmann parents: 
38786diff
changeset | 1356 |    @{term HOL.implies}, @{term HOL.conj}, @{term HOL.disj},
 | 
| 32839 | 1357 |    @{term "Not"}, @{term "op = :: bool => _"},
 | 
| 1358 |    @{term "All :: (real => _) => _"}, @{term "Ex :: (real => _) => _"},
 | |
| 1359 |    @{term "op = :: real => _"}, @{term "op < :: real => _"},
 | |
| 1360 |    @{term "op <= :: real => _"},
 | |
| 1361 |    @{term "op + :: real => _"}, @{term "op - :: real => _"},
 | |
| 1362 |    @{term "op * :: real => _"}, @{term "uminus :: real => _"},
 | |
| 31512 | 1363 |    @{term "op / :: real => _"}, @{term "inverse :: real => _"},
 | 
| 32839 | 1364 |    @{term "op ^ :: real => _"}, @{term "abs :: real => _"},
 | 
| 31512 | 1365 |    @{term "min :: real => _"}, @{term "max :: real => _"},
 | 
| 1366 |    @{term "0::real"}, @{term "1::real"}, @{term "number_of :: int => real"},
 | |
| 1367 |    @{term "number_of :: int => nat"},
 | |
| 32839 | 1368 |    @{term "Int.Bit0"}, @{term "Int.Bit1"},
 | 
| 31512 | 1369 |    @{term "Int.Pls"}, @{term "Int.Min"}];
 | 
| 1370 | ||
| 32839 | 1371 | fun check_sos kcts ct = | 
| 31512 | 1372 | let | 
| 1373 | val t = term_of ct | |
| 32839 | 1374 | val _ = if not (null (Term.add_tfrees t []) | 
| 1375 | andalso null (Term.add_tvars t [])) | |
| 31512 | 1376 | then error "SOS: not sos. Additional type varables" else () | 
| 1377 | val fs = Term.add_frees t [] | |
| 32839 | 1378 |   val _ = if exists (fn ((_,T)) => not (T = @{typ "real"})) fs
 | 
| 31512 | 1379 | then error "SOS: not sos. Variables with type not real" else () | 
| 1380 | val vs = Term.add_vars t [] | |
| 32839 | 1381 |   val _ = if exists (fn ((_,T)) => not (T = @{typ "real"})) vs
 | 
| 31512 | 1382 | then error "SOS: not sos. Variables with type not real" else () | 
| 1383 | val ukcs = subtract (fn (t,p) => Const p aconv t) kcts (Term.add_consts t []) | |
| 32839 | 1384 | val _ = if null ukcs then () | 
| 31512 | 1385 |               else error ("SOSO: Unknown constants in Subgoal:" ^ commas (map fst ukcs))
 | 
| 1386 | in () end | |
| 1387 | ||
| 32838 
d9dfd30af9ae
core_sos_tac: SUBPROOF body operates on subgoal 1;
 wenzelm parents: 
32831diff
changeset | 1388 | fun core_sos_tac print_cert prover = SUBPROOF (fn {concl, context, ...} =>
 | 
| 32831 | 1389 | let | 
| 32838 
d9dfd30af9ae
core_sos_tac: SUBPROOF body operates on subgoal 1;
 wenzelm parents: 
32831diff
changeset | 1390 | val _ = check_sos known_sos_constants concl | 
| 
d9dfd30af9ae
core_sos_tac: SUBPROOF body operates on subgoal 1;
 wenzelm parents: 
32831diff
changeset | 1391 | val (ths, certificates) = real_sos prover context (Thm.dest_arg concl) | 
| 
d9dfd30af9ae
core_sos_tac: SUBPROOF body operates on subgoal 1;
 wenzelm parents: 
32831diff
changeset | 1392 | val _ = print_cert certificates | 
| 
d9dfd30af9ae
core_sos_tac: SUBPROOF body operates on subgoal 1;
 wenzelm parents: 
32831diff
changeset | 1393 | in rtac ths 1 end) | 
| 31131 | 1394 | |
| 1395 | fun default_SOME f NONE v = SOME v | |
| 1396 | | default_SOME f (SOME v) _ = SOME v; | |
| 1397 | ||
| 1398 | fun lift_SOME f NONE a = f a | |
| 1399 | | lift_SOME f (SOME a) _ = SOME a; | |
| 1400 | ||
| 1401 | ||
| 1402 | local | |
| 1403 | val is_numeral = can (HOLogic.dest_number o term_of) | |
| 1404 | in | |
| 1405 | fun get_denom b ct = case term_of ct of | |
| 32839 | 1406 |   @{term "op / :: real => _"} $ _ $ _ =>
 | 
| 31131 | 1407 | if is_numeral (Thm.dest_arg ct) then get_denom b (Thm.dest_arg1 ct) | 
| 1408 | else default_SOME (get_denom b) (get_denom b (Thm.dest_arg ct)) (Thm.dest_arg ct, b) | |
| 1409 |  | @{term "op < :: real => _"} $ _ $ _ => lift_SOME (get_denom true) (get_denom true (Thm.dest_arg ct)) (Thm.dest_arg1 ct)
 | |
| 1410 |  | @{term "op <= :: real => _"} $ _ $ _ => lift_SOME (get_denom true) (get_denom true (Thm.dest_arg ct)) (Thm.dest_arg1 ct)
 | |
| 1411 | | _ $ _ => lift_SOME (get_denom b) (get_denom b (Thm.dest_fun ct)) (Thm.dest_arg ct) | |
| 1412 | | _ => NONE | |
| 1413 | end; | |
| 1414 | ||
| 32839 | 1415 | fun elim_one_denom_tac ctxt = | 
| 1416 | CSUBGOAL (fn (P,i) => | |
| 1417 | case get_denom false P of | |
| 31131 | 1418 | NONE => no_tac | 
| 32839 | 1419 | | SOME (d,ord) => | 
| 1420 | let | |
| 1421 |       val ss = simpset_of ctxt addsimps @{thms field_simps}
 | |
| 31131 | 1422 |                addsimps [@{thm nonzero_power_divide}, @{thm power_divide}]
 | 
| 32839 | 1423 | val th = instantiate' [] [SOME d, SOME (Thm.dest_arg P)] | 
| 31131 | 1424 |          (if ord then @{lemma "(d=0 --> P) & (d>0 --> P) & (d<(0::real) --> P) ==> P" by auto}
 | 
| 1425 |           else @{lemma "(d=0 --> P) & (d ~= (0::real) --> P) ==> P" by blast})
 | |
| 32949 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 1426 | in rtac th i THEN Simplifier.asm_full_simp_tac ss i end); | 
| 31131 | 1427 | |
| 1428 | fun elim_denom_tac ctxt i = REPEAT (elim_one_denom_tac ctxt i); | |
| 1429 | ||
| 32949 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 1430 | fun sos_tac print_cert prover ctxt = | 
| 35625 | 1431 | Object_Logic.full_atomize_tac THEN' | 
| 32949 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 1432 | elim_denom_tac ctxt THEN' | 
| 
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
 wenzelm parents: 
32839diff
changeset | 1433 | core_sos_tac print_cert prover ctxt; | 
| 31131 | 1434 | |
| 31512 | 1435 | end; |