| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
*) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
open NumCompat |
| 18 |
open Sos |
| 19 |
open Sos_types |
| 20 |
open Sos_lib |
| 21 |
module Mc = Micromega |
| 22 |
module C2Ml = Mutils.CoqToCaml |
| 23 |
|
| 24 |
type micromega_polys = (Micromega.q Mc.pol * Mc.op1) list |
| 25 |
type csdp_certificate = S of Sos_types.positivstellensatz option | F of string |
| 26 |
type provername = string * int option |
| 27 |
|
| 28 |
let flags = [Open_append; Open_binary; Open_creat] |
| 29 |
let chan = open_out_gen flags 0o666 "trace" |
| 30 |
|
| 31 |
module M = struct |
| 32 |
open Mc |
| 33 |
|
| 34 |
let rec expr_to_term = function |
| 35 |
| PEc z -> Const (C2Ml.q_to_num z) |
| 36 |
| PEX v -> Var ("x" ^ string_of_int (C2Ml.index v)) |
| 37 |
| PEmul (p1, p2) -> |
| 38 |
let p1 = expr_to_term p1 in |
| 39 |
let p2 = expr_to_term p2 in |
| 40 |
let res = Mul (p1, p2) in |
| 41 |
res |
| 42 |
| PEadd (p1, p2) -> Add (expr_to_term p1, expr_to_term p2) |
| 43 |
| PEsub (p1, p2) -> Sub (expr_to_term p1, expr_to_term p2) |
| 44 |
| PEpow (p, n) -> Pow (expr_to_term p, C2Ml.n n) |
| 45 |
| PEopp p -> Opp (expr_to_term p) |
| 46 |
end |
| 47 |
|
| 48 |
open M |
| 49 |
|
| 50 |
let partition_expr l = |
| 51 |
let rec f i = function |
| 52 |
| [] -> ([], [], []) |
| 53 |
| (e, k) :: l -> ( |
| 54 |
let eq, ge, neq = f (i + 1) l in |
| 55 |
match k with |
| 56 |
| Mc.Equal -> ((e, i) :: eq, ge, neq) |
| 57 |
| Mc.NonStrict -> (eq, (e, Axiom_le i) :: ge, neq) |
| 58 |
| Mc.Strict -> |
| 59 |
|
| 60 |
(eq, (e, Axiom_lt i) :: ge, (e, Axiom_lt i) :: neq) |
| 61 |
| Mc.NonEqual -> (eq, ge, (e, Axiom_eq i) :: neq) ) |
| 62 |
|
| 63 |
in |
| 64 |
f 0 l |
| 65 |
|
| 66 |
let rec sets_of_list l = |
| 67 |
match l with |
| 68 |
| [] -> [[]] |
| 69 |
| e :: l -> |
| 70 |
let s = sets_of_list l in |
| 71 |
s @ List.map (fun s0 -> e :: s0) s |
| 72 |
|
| 73 |
|
| 74 |
let real_nonlinear_prover d l = |
| 75 |
let l = List.map (fun (e, op) -> (Mc.denorm e, op)) l in |
| 76 |
try |
| 77 |
let eq, ge, neq = partition_expr l in |
| 78 |
let rec elim_const = function |
| 79 |
| [] -> [] |
| 80 |
| (x, y) :: l -> |
| 81 |
let p = poly_of_term (expr_to_term x) in |
| 82 |
if poly_isconst p then elim_const l else (p, y) :: elim_const l |
| 83 |
in |
| 84 |
let eq = elim_const eq in |
| 85 |
let peq = List.map fst eq in |
| 86 |
let pge = |
| 87 |
List.map (fun (e, psatz) -> (poly_of_term (expr_to_term e), psatz)) ge |
| 88 |
in |
| 89 |
let monoids = |
| 90 |
List.map |
| 91 |
(fun m -> |
| 92 |
( List.fold_right |
| 93 |
(fun (p, kd) y -> |
| 94 |
let p = poly_of_term (expr_to_term p) in |
| 95 |
match kd with |
| 96 |
| Axiom_lt i -> poly_mul p y |
| 97 |
| Axiom_eq i -> poly_mul (poly_pow p 2) y |
| 98 |
| _ -> failwith "monoids") |
| 99 |
m (poly_const Q.one) |
| 100 |
, List.map snd m )) |
| 101 |
(sets_of_list neq) |
| 102 |
in |
| 103 |
let cert_ideal, cert_cone, monoid = |
| 104 |
deepen_until d |
| 105 |
(fun d -> |
| 106 |
tryfind |
| 107 |
(fun m -> |
| 108 |
let ci, cc = |
| 109 |
real_positivnullstellensatz_general false d peq pge |
| 110 |
(poly_neg (fst m)) |
| 111 |
in |
| 112 |
(ci, cc, snd m)) |
| 113 |
monoids) |
| 114 |
0 |
| 115 |
in |
| 116 |
let proofs_ideal = |
| 117 |
List.map2 |
| 118 |
(fun q i -> Eqmul (term_of_poly q, Axiom_eq i)) |
| 119 |
cert_ideal (List.map snd eq) |
| 120 |
in |
| 121 |
let proofs_cone = List.map term_of_sos cert_cone in |
| 122 |
let proof_ne = |
| 123 |
let neq, lt = |
| 124 |
List.partition (function Axiom_eq _ -> true | _ -> false) monoid |
| 125 |
in |
| 126 |
let sq = |
| 127 |
match |
| 128 |
List.map (function Axiom_eq i -> i | _ -> failwith "error") neq |
| 129 |
with |
| 130 |
| [] -> Rational_lt Q.one |
| 131 |
| l -> Monoid l |
| 132 |
in |
| 133 |
List.fold_right (fun x y -> Product (x, y)) lt sq |
| 134 |
in |
| 135 |
let proof = |
| 136 |
end_itlist |
| 137 |
(fun s t -> Sum (s, t)) |
| 138 |
((proof_ne :: proofs_ideal) @ proofs_cone) |
| 139 |
in |
| 140 |
S (Some proof) |
| 141 |
with |
| 142 |
| Sos_lib.TooDeep -> S None |
| 143 |
| any -> F (Printexc.to_string any) |
| 144 |
|
| 145 |
|
| 146 |
let pure_sos l = |
| 147 |
let l = List.map (fun (e, o) -> (Mc.denorm e, o)) l in |
| 148 |
|
| 149 |
|
| 150 |
try |
| 151 |
let l = List.combine l (CList.interval 0 (List.length l - 1)) in |
| 152 |
let lt, i = |
| 153 |
try List.find (fun (x, _) -> snd x = Mc.Strict) l |
| 154 |
with Not_found -> List.hd l |
| 155 |
in |
| 156 |
let plt = poly_neg (poly_of_term (expr_to_term (fst lt))) in |
| 157 |
let n, polys = sumofsquares plt in |
| 158 |
|
| 159 |
let pos = |
| 160 |
Product |
| 161 |
( Rational_lt n |
| 162 |
, List.fold_right |
| 163 |
(fun (c, p) rst -> |
| 164 |
Sum (Product (Rational_lt c, Square (term_of_poly p)), rst)) |
| 165 |
polys (Rational_lt Q.zero) ) |
| 166 |
in |
| 167 |
let proof = Sum (Axiom_lt i, pos) in |
| 168 |
|
| 169 |
|
| 170 |
S (Some proof) |
| 171 |
with |
| 172 |
| any -> |
| 173 |
S None |
| 174 |
|
| 175 |
let run_prover prover pb = |
| 176 |
match prover with |
| 177 |
| "real_nonlinear_prover", Some d -> real_nonlinear_prover d pb |
| 178 |
| "pure_sos", None -> pure_sos pb |
| 179 |
| prover, _ -> |
| 180 |
Printf.printf "unknown prover: %s\n" prover; |
| 181 |
exit 1 |
| 182 |
|
| 183 |
let main () = |
| 184 |
try |
| 185 |
let (prover, poly) = (input_value stdin : provername * micromega_polys) in |
| 186 |
let cert = run_prover prover poly in |
| 187 |
|
| 188 |
|
| 189 |
output_value stdout (cert : csdp_certificate); |
| 190 |
flush stdout; |
| 191 |
Marshal.to_channel chan (cert : csdp_certificate) []; |
| 192 |
flush chan; |
| 193 |
exit 0 |
| 194 |
with any -> |
| 195 |
Printf.fprintf chan "error %s" (Printexc.to_string any); |
| 196 |
exit 1 |
| 197 |
|
| 198 |
;; |
| 199 |
let _ = main () in |
| 200 |
() |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|