logicaffeine_verify/
incremental.rs1use crate::ir::VerifyExpr;
6use std::collections::HashMap;
7use std::hash::{Hash, Hasher};
8
9pub type PropertyHash = u64;
11
12#[derive(Debug, Clone)]
14pub struct CachedResult {
15 pub is_safe: bool,
16 pub dependencies: Vec<String>,
17 pub timestamp: u64,
18}
19
20#[derive(Debug, Clone, Default)]
22pub struct VerificationCache {
23 pub entries: HashMap<PropertyHash, CachedResult>,
24}
25
26#[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 pub fn store(&mut self, hash: PropertyHash, result: CachedResult) {
41 self.entries.insert(hash, result);
42 }
43
44 pub fn lookup(&self, hash: PropertyHash) -> Option<&CachedResult> {
46 self.entries.get(&hash)
47 }
48
49 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 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
70pub 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
77pub fn verify_incremental(
79 properties: &[(VerifyExpr, Vec<String>)],
80 changes: &[ChangeEvent],
81 cache: &mut VerificationCache,
82) -> Vec<(PropertyHash, bool)> {
83 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 results.push((hash, cached.is_safe));
94 } else {
95 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}