author | haftmann |
Tue, 09 Nov 2010 14:02:12 +0100 | |
changeset 40464 | e1db06cf6254 |
parent 38805 | b09d8603f865 |
permissions | -rw-r--r-- |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
1 |
(* Title: HOL/Library/Sum_Of_Squares/sos_wrapper.ML |
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 |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
10 |
val setup: theory -> theory |
38805 | 11 |
val dest_dir: string Config.T |
12 |
val prover_name: string Config.T |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
13 |
end |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
14 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
15 |
structure SOS_Wrapper: SOS_WRAPPER = |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
16 |
struct |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
17 |
|
32332 | 18 |
datatype prover_result = Success | Failure | Error |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
19 |
|
32332 | 20 |
fun str_of_result Success = "Success" |
21 |
| str_of_result Failure = "Failure" |
|
22 |
| str_of_result Error = "Error" |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
23 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
24 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
25 |
(*** calling provers ***) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
26 |
|
38805 | 27 |
val (dest_dir, setup_dest_dir) = Attrib.config_string "sos_dest_dir" (K "") |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
28 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
29 |
fun filename dir name = |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
30 |
let |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
31 |
val probfile = Path.basic (name ^ serial_string ()) |
32332 | 32 |
val dir_path = Path.explode dir |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
33 |
in |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
34 |
if dir = "" then |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
35 |
File.tmp_path probfile |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
36 |
else if File.exists dir_path then |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
37 |
Path.append dir_path probfile |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
38 |
else error ("No such directory: " ^ dir) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
39 |
end |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
40 |
|
38805 | 41 |
fun run_solver ctxt name cmd find_failure input = |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
42 |
let |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
43 |
val _ = warning ("Calling solver: " ^ name) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
44 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
45 |
(* create input file *) |
38805 | 46 |
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
|
47 |
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
|
48 |
val _ = File.write input_file input |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
49 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
50 |
(* call solver *) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
51 |
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
|
52 |
val (output, rv) = |
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
|
53 |
bash_output |
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
|
54 |
(if File.exists cmd then |
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
|
55 |
space_implode " " |
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
|
56 |
[File.shell_path cmd, File.shell_path input_file, File.shell_path output_file] |
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
|
57 |
else error ("Bad executable: " ^ File.platform_path cmd)) |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
58 |
|
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
59 |
(* read and analyze output *) |
32332 | 60 |
val (res, res_msg) = find_failure rv |
61 |
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
|
62 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
63 |
(* remove temporary files *) |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
64 |
val _ = |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
65 |
if dir = "" then |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
66 |
(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
|
67 |
else () |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
68 |
|
38805 | 69 |
val _ = |
70 |
if Config.get ctxt Sum_Of_Squares.trace |
|
71 |
then writeln ("Solver output:\n" ^ output) |
|
72 |
else () |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
73 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
74 |
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
|
75 |
in |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
76 |
(case res of |
32332 | 77 |
Success => result |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
78 |
| Failure => raise Sum_Of_Squares.Failure res_msg |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
79 |
| Error => error ("Prover failed: " ^ res_msg)) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
80 |
end |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
81 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
82 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
83 |
(*** various provers ***) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
84 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
85 |
(* local csdp client *) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
86 |
|
32332 | 87 |
fun find_csdp_failure rv = |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
88 |
case rv of |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
89 |
0 => (Success, "SDP solved") |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
90 |
| 1 => (Failure, "SDP is primal infeasible") |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
91 |
| 2 => (Failure, "SDP is dual infeasible") |
32332 | 92 |
| 3 => (Success, "SDP solved with reduced accuracy") |
93 |
| 4 => (Failure, "Maximum iterations reached") |
|
94 |
| 5 => (Failure, "Stuck at edge of primal feasibility") |
|
95 |
| 6 => (Failure, "Stuck at edge of dual infeasibility") |
|
96 |
| 7 => (Failure, "Lack of progress") |
|
97 |
| 8 => (Failure, "X, Z, or O was singular") |
|
98 |
| 9 => (Failure, "Detected NaN or Inf values") |
|
99 |
| _ => (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
|
100 |
|
32332 | 101 |
val csdp = ("$CSDP_EXE", find_csdp_failure) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
102 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
103 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
104 |
(* remote neos server *) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
105 |
|
32332 | 106 |
fun find_neos_failure rv = |
107 |
case rv of |
|
108 |
20 => (Error, "error submitting job") |
|
32362 | 109 |
| 21 => (Error, "interrupt") |
32332 | 110 |
| _ => find_csdp_failure rv |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
111 |
|
32332 | 112 |
val neos_csdp = ("$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
|
113 |
|
32949
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 |
(* named provers *) |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
116 |
|
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
117 |
fun prover "remote_csdp" = neos_csdp |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
118 |
| prover "csdp" = csdp |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
119 |
| prover name = error ("Unknown prover: " ^ name) |
32363
a0ea6cd47724
SOS: function to set default prover; output channel changed
Philipp Meyer
parents:
32362
diff
changeset
|
120 |
|
38805 | 121 |
val (prover_name, setup_prover_name) = Attrib.config_string "sos_prover_name" (K "remote_csdp") |
32363
a0ea6cd47724
SOS: function to set default prover; output channel changed
Philipp Meyer
parents:
32362
diff
changeset
|
122 |
|
38805 | 123 |
fun call_solver ctxt opt_name = |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
124 |
let |
38805 | 125 |
val name = the_default (Config.get ctxt prover_name) opt_name |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
126 |
val (cmd, find_failure) = prover name |
38805 | 127 |
in run_solver ctxt name (Path.explode cmd) find_failure end |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
128 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
129 |
|
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32363
diff
changeset
|
130 |
(* certificate output *) |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32363
diff
changeset
|
131 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
132 |
fun output_line cert = |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
133 |
"To repeat this proof with a certifiate use this command:\n" ^ |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
134 |
Markup.markup Markup.sendback ("by (sos_cert \"" ^ cert ^ "\")") |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
135 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
136 |
val print_cert = warning o output_line o PositivstellensatzTools.pss_tree_to_cert |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32363
diff
changeset
|
137 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
138 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
139 |
(* method setup *) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
140 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
141 |
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
|
142 |
|
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32363
diff
changeset
|
143 |
val setup = |
38805 | 144 |
setup_dest_dir #> |
145 |
setup_prover_name #> |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
146 |
Method.setup @{binding sos} |
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
35010
diff
changeset
|
147 |
(Scan.lift (Scan.option Parse.xname) |
38805 | 148 |
>> (fn opt_name => fn ctxt => |
149 |
sos_solver print_cert |
|
150 |
(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
|
151 |
"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
|
152 |
Method.setup @{binding sos_cert} |
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
35010
diff
changeset
|
153 |
(Scan.lift Parse.string |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
154 |
>> (fn cert => fn ctxt => |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
155 |
sos_solver ignore |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
156 |
(Sum_Of_Squares.Certificate (PositivstellensatzTools.cert_to_pss_tree ctxt cert)) ctxt)) |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32944
diff
changeset
|
157 |
"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
|
158 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff
changeset
|
159 |
end |