1use crate::equivalence::Trace;
6use crate::ir::VerifyExpr;
7use crate::kinduction::{self, KInductionResult, SignalDecl};
8
9#[derive(Debug, Clone, PartialEq)]
11pub enum Strategy {
12 Bmc(u32),
13 KInduction(u32),
14 Ic3,
15 Interpolation(u32),
16 LivenessToSafety,
17 Portfolio {
18 strategies: Vec<Strategy>,
19 timeout_each_ms: u64,
20 },
21}
22
23#[derive(Debug)]
25pub enum VerificationResult {
26 Safe {
27 strategy_used: Strategy,
28 },
29 Unsafe {
30 trace: Trace,
31 strategy_used: Strategy,
32 },
33 Unknown,
34}
35
36pub fn select_strategy(property: &VerifyExpr, _signals: &[SignalDecl]) -> Strategy {
38 if contains_liveness_pattern(property) {
40 return Strategy::LivenessToSafety;
41 }
42
43 if is_small_property(property) {
45 Strategy::KInduction(10)
46 } else {
47 Strategy::Ic3
48 }
49}
50
51pub fn verify_auto(
53 init: &VerifyExpr,
54 transition: &VerifyExpr,
55 property: &VerifyExpr,
56 signals: &[SignalDecl],
57) -> VerificationResult {
58 let strategy = select_strategy(property, signals);
59
60 match &strategy {
61 Strategy::KInduction(max_k) => {
62 let result = kinduction::k_induction(init, transition, property, signals, *max_k);
63 match result {
64 KInductionResult::Proven { .. } => VerificationResult::Safe {
65 strategy_used: strategy,
66 },
67 KInductionResult::Counterexample { trace, .. } => VerificationResult::Unsafe {
68 trace,
69 strategy_used: strategy,
70 },
71 _ => {
72 try_ic3(init, transition, property)
74 }
75 }
76 }
77 Strategy::Ic3 => try_ic3(init, transition, property),
78 Strategy::LivenessToSafety => {
79 let result = crate::liveness::check_liveness(init, transition, &[], property, 10);
80 match result {
81 crate::liveness::LivenessResult::Live => VerificationResult::Safe {
82 strategy_used: strategy,
83 },
84 crate::liveness::LivenessResult::NotLive { trace, .. } => {
85 VerificationResult::Unsafe {
86 trace,
87 strategy_used: strategy,
88 }
89 }
90 _ => VerificationResult::Unknown,
91 }
92 }
93 Strategy::Interpolation(bound) => {
94 let result = crate::interpolation::itp_model_check(init, transition, property, *bound);
95 match result {
96 crate::interpolation::InterpolationResult::Safe
97 | crate::interpolation::InterpolationResult::Fixpoint { .. } => {
98 VerificationResult::Safe {
99 strategy_used: strategy,
100 }
101 }
102 crate::interpolation::InterpolationResult::Unsafe { trace } => {
103 VerificationResult::Unsafe {
104 trace,
105 strategy_used: strategy,
106 }
107 }
108 _ => VerificationResult::Unknown,
109 }
110 }
111 Strategy::Bmc(bound) => {
112 let result = kinduction::k_induction(init, transition, property, signals, *bound);
113 match result {
114 KInductionResult::Counterexample { trace, .. } => VerificationResult::Unsafe {
115 trace,
116 strategy_used: strategy,
117 },
118 KInductionResult::Proven { .. } => VerificationResult::Safe {
119 strategy_used: strategy,
120 },
121 _ => VerificationResult::Unknown,
122 }
123 }
124 Strategy::Portfolio { strategies, .. } => {
125 for s in strategies {
127 let result = verify_with_strategy(init, transition, property, signals, s);
128 match &result {
129 VerificationResult::Safe { .. } | VerificationResult::Unsafe { .. } => {
130 return result
131 }
132 VerificationResult::Unknown => continue,
133 }
134 }
135 VerificationResult::Unknown
136 }
137 }
138}
139
140fn try_ic3(
141 init: &VerifyExpr,
142 transition: &VerifyExpr,
143 property: &VerifyExpr,
144) -> VerificationResult {
145 let result = crate::ic3::ic3(init, transition, property, 20);
146 match result {
147 crate::ic3::Ic3Result::Safe { .. } => VerificationResult::Safe {
148 strategy_used: Strategy::Ic3,
149 },
150 crate::ic3::Ic3Result::Unsafe { trace } => VerificationResult::Unsafe {
151 trace,
152 strategy_used: Strategy::Ic3,
153 },
154 _ => VerificationResult::Unknown,
155 }
156}
157
158fn verify_with_strategy(
159 init: &VerifyExpr,
160 transition: &VerifyExpr,
161 property: &VerifyExpr,
162 signals: &[SignalDecl],
163 strategy: &Strategy,
164) -> VerificationResult {
165 match strategy {
166 Strategy::KInduction(k) => {
167 let result = kinduction::k_induction(init, transition, property, signals, *k);
168 match result {
169 KInductionResult::Proven { .. } => VerificationResult::Safe {
170 strategy_used: strategy.clone(),
171 },
172 KInductionResult::Counterexample { trace, .. } => VerificationResult::Unsafe {
173 trace,
174 strategy_used: strategy.clone(),
175 },
176 _ => VerificationResult::Unknown,
177 }
178 }
179 Strategy::Ic3 => try_ic3(init, transition, property),
180 _ => VerificationResult::Unknown,
181 }
182}
183
184fn contains_liveness_pattern(_expr: &VerifyExpr) -> bool {
185 false
190}
191
192fn is_small_property(expr: &VerifyExpr) -> bool {
193 expr_size(expr) < 20
194}
195
196fn expr_size(expr: &VerifyExpr) -> usize {
197 match expr {
198 VerifyExpr::Bool(_) | VerifyExpr::Int(_) | VerifyExpr::Var(_) => 1,
199 VerifyExpr::BitVecConst { .. } => 1,
200 VerifyExpr::Binary { left, right, .. } => 1 + expr_size(left) + expr_size(right),
201 VerifyExpr::Not(inner) => 1 + expr_size(inner),
202 VerifyExpr::Iff(l, r) => 1 + expr_size(l) + expr_size(r),
203 VerifyExpr::ForAll { body, .. } | VerifyExpr::Exists { body, .. } => 1 + expr_size(body),
204 VerifyExpr::Apply { args, .. } | VerifyExpr::ApplyInt { args, .. } => {
205 1 + args.iter().map(expr_size).sum::<usize>()
206 }
207 VerifyExpr::BitVecBinary { left, right, .. } => 1 + expr_size(left) + expr_size(right),
208 VerifyExpr::BitVecExtract { operand, .. } => 1 + expr_size(operand),
209 VerifyExpr::BitVecConcat(l, r) => 1 + expr_size(l) + expr_size(r),
210 VerifyExpr::Select { array, index } => 1 + expr_size(array) + expr_size(index),
211 VerifyExpr::Store {
212 array,
213 index,
214 value,
215 } => 1 + expr_size(array) + expr_size(index) + expr_size(value),
216 VerifyExpr::AtState { state, expr } => 1 + expr_size(state) + expr_size(expr),
217 VerifyExpr::Transition { from, to } => 1 + expr_size(from) + expr_size(to),
218 }
219}