author | wenzelm |
Wed, 08 Oct 2014 10:15:04 +0200 | |
changeset 58629 | a6a6cd499d4e |
parent 58418 | a04b242a7a01 |
child 58630 | 71cdb885b3bb |
permissions | -rw-r--r-- |
41474 | 1 |
(* Title: HOL/Library/Sum_of_Squares/sos_wrapper.ML |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
2 |
Author: Philipp Meyer, TU Muenchen |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
3 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
4 |
Added functionality for sums of squares, e.g. calling a remote prover. |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
5 |
*) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
6 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
7 |
signature SOS_WRAPPER = |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
8 |
sig |
32332 | 9 |
datatype prover_result = Success | Failure | Error |
38805 | 10 |
val dest_dir: string Config.T |
11 |
val prover_name: string Config.T |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
12 |
end |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
13 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
14 |
structure SOS_Wrapper: SOS_WRAPPER = |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
15 |
struct |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
16 |
|
32332 | 17 |
datatype prover_result = Success | Failure | Error |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
18 |
|
32332 | 19 |
fun str_of_result Success = "Success" |
20 |
| str_of_result Failure = "Failure" |
|
21 |
| str_of_result Error = "Error" |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
22 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
23 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
24 |
(*** calling provers ***) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
25 |
|
42616
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
41950
diff
changeset
|
26 |
val dest_dir = Attrib.setup_config_string @{binding sos_dest_dir} (K "") |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
27 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
28 |
fun filename dir name = |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
29 |
let |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
30 |
val probfile = Path.basic (name ^ serial_string ()) |
32332 | 31 |
val dir_path = Path.explode dir |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
32 |
in |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
33 |
if dir = "" then |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
34 |
File.tmp_path probfile |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
35 |
else if File.exists dir_path then |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
36 |
Path.append dir_path probfile |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
37 |
else error ("No such directory: " ^ dir) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
38 |
end |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
39 |
|
41950
134131d519c0
clarified ISABELLE_CSDP setting (formerly CSDP_EXE);
wenzelm
parents:
41474
diff
changeset
|
40 |
fun run_solver ctxt name exe find_failure input = |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
41 |
let |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
42 |
val _ = warning ("Calling solver: " ^ name) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
43 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
44 |
(* create input file *) |
38805 | 45 |
val dir = Config.get ctxt dest_dir |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
46 |
val input_file = filename dir "sos_in" |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
47 |
val _ = File.write input_file input |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
48 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
49 |
(* call solver *) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
50 |
val output_file = filename dir "sos_out" |
35010
d6e492cea6e4
renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
wenzelm
parents:
32949
diff
changeset
|
51 |
val (output, rv) = |
43850
7f2cbc713344
moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents:
42616
diff
changeset
|
52 |
Isabelle_System.bash_output |
41950
134131d519c0
clarified ISABELLE_CSDP setting (formerly CSDP_EXE);
wenzelm
parents:
41474
diff
changeset
|
53 |
(if File.exists exe then |
134131d519c0
clarified ISABELLE_CSDP setting (formerly CSDP_EXE);
wenzelm
parents:
41474
diff
changeset
|
54 |
space_implode " " (map File.shell_path [exe, input_file, output_file]) |
134131d519c0
clarified ISABELLE_CSDP setting (formerly CSDP_EXE);
wenzelm
parents:
41474
diff
changeset
|
55 |
else error ("Bad executable: " ^ File.platform_path exe)) |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
56 |
|
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
57 |
(* read and analyze output *) |
32332 | 58 |
val (res, res_msg) = find_failure rv |
59 |
val result = if File.exists output_file then File.read output_file else "" |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
60 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
61 |
(* remove temporary files *) |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
62 |
val _ = |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
63 |
if dir = "" then |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
64 |
(File.rm input_file; if File.exists output_file then File.rm output_file else ()) |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
65 |
else () |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
66 |
|
38805 | 67 |
val _ = |
41474 | 68 |
if Config.get ctxt Sum_of_Squares.trace |
38805 | 69 |
then writeln ("Solver output:\n" ^ output) |
70 |
else () |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
71 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
72 |
val _ = warning (str_of_result res ^ ": " ^ res_msg) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
73 |
in |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
74 |
(case res of |
32332 | 75 |
Success => result |
41474 | 76 |
| Failure => raise Sum_of_Squares.Failure res_msg |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
77 |
| Error => error ("Prover failed: " ^ res_msg)) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
78 |
end |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
79 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
80 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
81 |
(*** various provers ***) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
82 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
83 |
(* local csdp client *) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
84 |
|
32332 | 85 |
fun find_csdp_failure rv = |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
86 |
case rv of |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
87 |
0 => (Success, "SDP solved") |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
88 |
| 1 => (Failure, "SDP is primal infeasible") |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
89 |
| 2 => (Failure, "SDP is dual infeasible") |
32332 | 90 |
| 3 => (Success, "SDP solved with reduced accuracy") |
91 |
| 4 => (Failure, "Maximum iterations reached") |
|
92 |
| 5 => (Failure, "Stuck at edge of primal feasibility") |
|
93 |
| 6 => (Failure, "Stuck at edge of dual infeasibility") |
|
94 |
| 7 => (Failure, "Lack of progress") |
|
95 |
| 8 => (Failure, "X, Z, or O was singular") |
|
96 |
| 9 => (Failure, "Detected NaN or Inf values") |
|
97 |
| _ => (Error, "return code is " ^ string_of_int rv) |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
98 |
|
41950
134131d519c0
clarified ISABELLE_CSDP setting (formerly CSDP_EXE);
wenzelm
parents:
41474
diff
changeset
|
99 |
val csdp = (Path.explode "$ISABELLE_CSDP", find_csdp_failure) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
100 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
101 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
102 |
(* remote neos server *) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
103 |
|
32332 | 104 |
fun find_neos_failure rv = |
105 |
case rv of |
|
106 |
20 => (Error, "error submitting job") |
|
32362 | 107 |
| 21 => (Error, "interrupt") |
32332 | 108 |
| _ => find_csdp_failure rv |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
109 |
|
41950
134131d519c0
clarified ISABELLE_CSDP setting (formerly CSDP_EXE);
wenzelm
parents:
41474
diff
changeset
|
110 |
val neos_csdp = (Path.explode "$ISABELLE_SUM_OF_SQUARES/neos_csdp_client", find_neos_failure) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
111 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
112 |
|
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
113 |
(* named provers *) |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
114 |
|
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
115 |
fun prover "remote_csdp" = neos_csdp |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
116 |
| prover "csdp" = csdp |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
117 |
| prover name = error ("Unknown prover: " ^ name) |
32363
a0ea6cd47724
SOS: function to set default prover; output channel changed
Philipp Meyer
parents:
32362
diff
changeset
|
118 |
|
42616
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
41950
diff
changeset
|
119 |
val prover_name = Attrib.setup_config_string @{binding sos_prover_name} (K "remote_csdp") |
32363
a0ea6cd47724
SOS: function to set default prover; output channel changed
Philipp Meyer
parents:
32362
diff
changeset
|
120 |
|
38805 | 121 |
fun call_solver ctxt opt_name = |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
122 |
let |
38805 | 123 |
val name = the_default (Config.get ctxt prover_name) opt_name |
41950
134131d519c0
clarified ISABELLE_CSDP setting (formerly CSDP_EXE);
wenzelm
parents:
41474
diff
changeset
|
124 |
val (exe, find_failure) = prover name |
134131d519c0
clarified ISABELLE_CSDP setting (formerly CSDP_EXE);
wenzelm
parents:
41474
diff
changeset
|
125 |
in run_solver ctxt name exe find_failure end |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
126 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
127 |
|
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32363
diff
changeset
|
128 |
(* certificate output *) |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32363
diff
changeset
|
129 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
130 |
fun output_line cert = |
56625
54505623a8ef
sos accepts False, returns apply command
paulson <lp15@cam.ac.uk>
parents:
52697
diff
changeset
|
131 |
"To repeat this proof with a certificate use this command:\n" ^ |
54505623a8ef
sos accepts False, returns apply command
paulson <lp15@cam.ac.uk>
parents:
52697
diff
changeset
|
132 |
Active.sendback_markup [] ("apply (sos_cert \"" ^ cert ^ "\")") |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
133 |
|
58629 | 134 |
val print_cert = warning o output_line o Positivstellensatz_Tools.print_cert |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32363
diff
changeset
|
135 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
136 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
137 |
(* method setup *) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
138 |
|
41474 | 139 |
fun sos_solver print method = SIMPLE_METHOD' o Sum_of_Squares.sos_tac print method |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32363
diff
changeset
|
140 |
|
58418 | 141 |
val _ = Theory.setup |
142 |
(Method.setup @{binding sos} |
|
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
35010
diff
changeset
|
143 |
(Scan.lift (Scan.option Parse.xname) |
38805 | 144 |
>> (fn opt_name => fn ctxt => |
145 |
sos_solver print_cert |
|
41474 | 146 |
(Sum_of_Squares.Prover (call_solver ctxt opt_name)) ctxt)) |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
147 |
"prove universal problems over the reals using sums of squares" #> |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
148 |
Method.setup @{binding sos_cert} |
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
35010
diff
changeset
|
149 |
(Scan.lift Parse.string |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
150 |
>> (fn cert => fn ctxt => |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
151 |
sos_solver ignore |
58629 | 152 |
(Sum_of_Squares.Certificate (Positivstellensatz_Tools.read_cert ctxt cert)) ctxt)) |
58418 | 153 |
"prove universal problems over the reals using sums of squares with certificates") |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
154 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
155 |
end |