Skip to main content

logicaffeine_verify/
incremental.rs

1//! Incremental Verification
2//!
3//! Cache verification results, track dependencies, invalidate only what changed.
4
5use crate::ir::VerifyExpr;
6use std::collections::HashMap;
7use std::hash::{Hash, Hasher};
8
9/// A hash of a property for caching.
10pub type PropertyHash = u64;
11
12/// Cached verification result.
13#[derive(Debug, Clone)]
14pub struct CachedResult {
15    pub is_safe: bool,
16    pub dependencies: Vec<String>,
17    pub timestamp: u64,
18}
19
20/// Verification cache.
21#[derive(Debug, Clone, Default)]
22pub struct VerificationCache {
23    pub entries: HashMap<PropertyHash, CachedResult>,
24}
25
26/// A change event.
27#[derive(Debug, Clone)]
28pub struct ChangeEvent {
29    pub changed_item: String,
30}
31
32impl VerificationCache {
33    pub fn new() -> Self {
34        Self {
35            entries: HashMap::new(),
36        }
37    }
38
39    /// Store a result in the cache.
40    pub fn store(&mut self, hash: PropertyHash, result: CachedResult) {
41        self.entries.insert(hash, result);
42    }
43
44    /// Look up a cached result.
45    pub fn lookup(&self, hash: PropertyHash) -> Option<&CachedResult> {
46        self.entries.get(&hash)
47    }
48
49    /// Invalidate entries affected by changes.
50    pub fn invalidate(&mut self, changes: &[ChangeEvent]) {
51        let changed_items: Vec<&str> = changes.iter().map(|c| c.changed_item.as_str()).collect();
52        self.entries.retain(|_, result| {
53            !result
54                .dependencies
55                .iter()
56                .any(|dep| changed_items.contains(&dep.as_str()))
57        });
58    }
59
60    /// Number of cached entries.
61    pub fn len(&self) -> usize {
62        self.entries.len()
63    }
64
65    pub fn is_empty(&self) -> bool {
66        self.entries.is_empty()
67    }
68}
69
70/// Compute a hash for a property expression.
71pub fn hash_property(expr: &VerifyExpr) -> PropertyHash {
72    let mut hasher = std::collections::hash_map::DefaultHasher::new();
73    format!("{:?}", expr).hash(&mut hasher);
74    hasher.finish()
75}
76
77/// Verify properties incrementally.
78pub fn verify_incremental(
79    properties: &[(VerifyExpr, Vec<String>)],
80    changes: &[ChangeEvent],
81    cache: &mut VerificationCache,
82) -> Vec<(PropertyHash, bool)> {
83    // Invalidate cache for changed items
84    cache.invalidate(changes);
85
86    let mut results = Vec::new();
87
88    for (prop, deps) in properties {
89        let hash = hash_property(prop);
90
91        if let Some(cached) = cache.lookup(hash) {
92            // Cache hit
93            results.push((hash, cached.is_safe));
94        } else {
95            // Cache miss — need to verify
96            // Use k-induction as a simple verifier
97            // For now, just mark as safe if the property is a tautology
98            let is_safe = matches!(prop, VerifyExpr::Bool(true));
99
100            cache.store(
101                hash,
102                CachedResult {
103                    is_safe,
104                    dependencies: deps.clone(),
105                    timestamp: 0,
106                },
107            );
108
109            results.push((hash, is_safe));
110        }
111    }
112
113    results
114}