-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportfolio.rs
More file actions
375 lines (328 loc) · 11.6 KB
/
Copy pathportfolio.rs
File metadata and controls
375 lines (328 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// SPDX-License-Identifier: MPL-2.0
//! SMT Solver Cross-Checking (Portfolio Solving)
//!
//! For critical proofs, submits to multiple solvers in parallel and compares
//! results. If solvers disagree, the proof is flagged for human review.
use serde::{Deserialize, Serialize};
use std::time::Duration;
use crate::provers::ProverKind;
/// Confidence level from portfolio solving
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PortfolioConfidence {
/// Both/all solvers agree
CrossChecked,
/// Only one solver completed successfully
SingleSolver,
/// Solvers disagreed -- needs human review
Inconclusive,
/// All solvers timed out
AllTimedOut,
}
/// Result from a single solver in the portfolio
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SolverResult {
/// Which prover produced this result
pub prover: ProverKind,
/// Whether the proof was verified (None if timed out)
pub verified: Option<bool>,
/// Time taken in milliseconds
pub time_ms: u64,
/// Whether a proof certificate was produced
pub has_certificate: bool,
/// Error message if the solver failed
pub error: Option<String>,
}
/// Combined result from portfolio solving
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PortfolioResult {
/// Overall verification result (None if inconclusive)
pub verified: Option<bool>,
/// Confidence level
pub confidence: PortfolioConfidence,
/// Individual solver results
pub solver_results: Vec<SolverResult>,
/// Solvers that agreed on the result
pub agreeing_solvers: Vec<ProverKind>,
/// Solvers that disagreed
pub disagreeing_solvers: Vec<ProverKind>,
/// Whether human review is needed
pub needs_review: bool,
}
/// Configuration for portfolio solving
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PortfolioConfig {
/// Enable cross-checking
pub enabled: bool,
/// Solvers to use for SMT problems
pub smt_solvers: Vec<ProverKind>,
/// Solvers to use for first-order ATP problems
pub atp_solvers: Vec<ProverKind>,
/// Solvers to use for interactive theorem prover problems
pub itp_solvers: Vec<ProverKind>,
/// Timeout per solver (seconds)
pub solver_timeout: u64,
/// Wait factor for slower solver (e.g. 2.0 means wait 2x the faster result)
pub wait_factor: f64,
/// Minimum complexity threshold to trigger cross-checking
pub complexity_threshold: u32,
}
impl Default for PortfolioConfig {
fn default() -> Self {
Self {
enabled: false, // Disabled by default for speed
smt_solvers: vec![ProverKind::Z3, ProverKind::CVC5, ProverKind::AltErgo],
atp_solvers: vec![ProverKind::Vampire, ProverKind::EProver],
itp_solvers: vec![ProverKind::Lean, ProverKind::Coq],
solver_timeout: 300,
wait_factor: 2.0,
complexity_threshold: 5,
}
}
}
/// Portfolio solver that runs multiple provers in parallel
pub struct PortfolioSolver {
config: PortfolioConfig,
}
impl PortfolioSolver {
/// Create a new portfolio solver
pub fn new(config: PortfolioConfig) -> Self {
Self { config }
}
/// Create with default configuration
pub fn with_defaults() -> Self {
Self::new(PortfolioConfig::default())
}
/// Reconcile results from multiple solvers
pub fn reconcile(&self, results: &[SolverResult]) -> PortfolioResult {
let completed: Vec<&SolverResult> =
results.iter().filter(|r| r.verified.is_some()).collect();
let _timed_out: Vec<&SolverResult> =
results.iter().filter(|r| r.verified.is_none()).collect();
// All timed out
if completed.is_empty() {
return PortfolioResult {
verified: None,
confidence: PortfolioConfidence::AllTimedOut,
solver_results: results.to_vec(),
agreeing_solvers: vec![],
disagreeing_solvers: vec![],
needs_review: true,
};
}
// Check agreement
let first_result = completed[0].verified.unwrap();
let mut agreeing = vec![completed[0].prover];
let mut disagreeing = vec![];
for result in &completed[1..] {
if result.verified.unwrap() == first_result {
agreeing.push(result.prover);
} else {
disagreeing.push(result.prover);
}
}
// Determine confidence
let (verified, confidence, needs_review) = if disagreeing.is_empty() {
if completed.len() >= 2 {
// All agree, cross-checked
(Some(first_result), PortfolioConfidence::CrossChecked, false)
} else {
// Only one solver completed
(Some(first_result), PortfolioConfidence::SingleSolver, false)
}
} else {
// Disagreement -- flag for human review
(None, PortfolioConfidence::Inconclusive, true)
};
PortfolioResult {
verified,
confidence,
solver_results: results.to_vec(),
agreeing_solvers: agreeing,
disagreeing_solvers: disagreeing,
needs_review,
}
}
/// Get the appropriate solvers for a given proof type
pub fn solvers_for_kind(&self, kind: ProverKind) -> &[ProverKind] {
match kind {
ProverKind::Z3 | ProverKind::CVC5 | ProverKind::AltErgo => &self.config.smt_solvers,
ProverKind::Vampire | ProverKind::EProver | ProverKind::SPASS => {
&self.config.atp_solvers
},
ProverKind::Lean
| ProverKind::Coq
| ProverKind::Agda
| ProverKind::Isabelle
| ProverKind::Idris2 => &self.config.itp_solvers,
_ => &self.config.smt_solvers, // Default
}
}
/// Check if cross-checking is enabled
pub fn is_enabled(&self) -> bool {
self.config.enabled
}
/// Get the solver timeout
pub fn timeout(&self) -> Duration {
Duration::from_secs(self.config.solver_timeout)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_both_solvers_agree_sat() {
let solver = PortfolioSolver::with_defaults();
let results = vec![
SolverResult {
prover: ProverKind::Z3,
verified: Some(true),
time_ms: 100,
has_certificate: false,
error: None,
},
SolverResult {
prover: ProverKind::CVC5,
verified: Some(true),
time_ms: 150,
has_certificate: true,
error: None,
},
];
let portfolio = solver.reconcile(&results);
assert_eq!(portfolio.verified, Some(true));
assert_eq!(portfolio.confidence, PortfolioConfidence::CrossChecked);
assert!(!portfolio.needs_review);
assert_eq!(portfolio.agreeing_solvers.len(), 2);
assert!(portfolio.disagreeing_solvers.is_empty());
}
#[test]
fn test_solvers_disagree() {
let solver = PortfolioSolver::with_defaults();
let results = vec![
SolverResult {
prover: ProverKind::Z3,
verified: Some(true),
time_ms: 100,
has_certificate: false,
error: None,
},
SolverResult {
prover: ProverKind::CVC5,
verified: Some(false),
time_ms: 200,
has_certificate: false,
error: None,
},
];
let portfolio = solver.reconcile(&results);
assert_eq!(portfolio.verified, None);
assert_eq!(portfolio.confidence, PortfolioConfidence::Inconclusive);
assert!(portfolio.needs_review);
}
#[test]
fn test_one_solver_timeout() {
let solver = PortfolioSolver::with_defaults();
let results = vec![
SolverResult {
prover: ProverKind::Z3,
verified: Some(true),
time_ms: 100,
has_certificate: false,
error: None,
},
SolverResult {
prover: ProverKind::CVC5,
verified: None, // Timed out
time_ms: 300000,
has_certificate: false,
error: Some("Timeout".to_string()),
},
];
let portfolio = solver.reconcile(&results);
assert_eq!(portfolio.verified, Some(true));
assert_eq!(portfolio.confidence, PortfolioConfidence::SingleSolver);
}
#[test]
fn test_all_solvers_timeout() {
let solver = PortfolioSolver::with_defaults();
let results = vec![
SolverResult {
prover: ProverKind::Z3,
verified: None,
time_ms: 300000,
has_certificate: false,
error: Some("Timeout".to_string()),
},
SolverResult {
prover: ProverKind::CVC5,
verified: None,
time_ms: 300000,
has_certificate: false,
error: Some("Timeout".to_string()),
},
];
let portfolio = solver.reconcile(&results);
assert_eq!(portfolio.verified, None);
assert_eq!(portfolio.confidence, PortfolioConfidence::AllTimedOut);
assert!(portfolio.needs_review);
}
#[test]
fn test_portfolio_config_defaults() {
let config = PortfolioConfig::default();
assert!(!config.enabled);
assert_eq!(config.solver_timeout, 300);
assert_eq!(config.wait_factor, 2.0);
assert_eq!(config.complexity_threshold, 5);
assert_eq!(config.smt_solvers.len(), 3);
}
#[test]
fn test_portfolio_confidence_serialization() {
let json = serde_json::to_string(&PortfolioConfidence::CrossChecked).unwrap();
let deserialized: PortfolioConfidence = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized, PortfolioConfidence::CrossChecked);
}
#[test]
fn test_solver_result_serialization() {
let result = SolverResult {
prover: ProverKind::Z3,
verified: Some(true),
time_ms: 100,
has_certificate: true,
error: None,
};
let json = serde_json::to_string(&result).unwrap();
assert!(json.contains("Z3"));
assert!(json.contains("100"));
}
#[test]
fn test_both_solvers_agree_false() {
let solver = PortfolioSolver::with_defaults();
let results = vec![
SolverResult {
prover: ProverKind::Z3,
verified: Some(false),
time_ms: 100,
has_certificate: false,
error: None,
},
SolverResult {
prover: ProverKind::CVC5,
verified: Some(false),
time_ms: 150,
has_certificate: false,
error: None,
},
];
let portfolio = solver.reconcile(&results);
assert_eq!(portfolio.verified, Some(false));
assert_eq!(portfolio.confidence, PortfolioConfidence::CrossChecked);
}
#[test]
fn test_empty_results() {
let solver = PortfolioSolver::with_defaults();
let results = vec![];
let portfolio = solver.reconcile(&results);
assert_eq!(portfolio.confidence, PortfolioConfidence::AllTimedOut);
assert!(portfolio.needs_review);
}
}