oxilean-kernel 0.1.2

OxiLean kernel - The trusted computing base for type checking
Documentation
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! Functions for the Definitional Equality Cache.
//!
//! Provides the core cache operations: construction, lookup, insertion,
//! eviction, statistics, and a higher-order memoizing wrapper.

use crate::proof_cert::hash_expr;
use crate::Expr;

use super::types::{CacheEviction, DefEqCache, DefEqCacheStats, DefEqEntry, DefEqKey};

// ---------------------------------------------------------------------------
// DefEqKey construction
// ---------------------------------------------------------------------------

impl DefEqKey {
    /// Construct a canonical `DefEqKey` for the given expression pair.
    ///
    /// The two hashes are sorted so that `new(a, b) == new(b, a)`, enabling
    /// symmetric reuse of cache entries regardless of argument order.
    pub fn new(lhs: &Expr, rhs: &Expr) -> Self {
        let h_lhs = hash_expr(lhs);
        let h_rhs = hash_expr(rhs);
        if h_lhs <= h_rhs {
            DefEqKey {
                lhs_hash: h_lhs,
                rhs_hash: h_rhs,
            }
        } else {
            DefEqKey {
                lhs_hash: h_rhs,
                rhs_hash: h_lhs,
            }
        }
    }

    /// Construct a `DefEqKey` directly from two hashes (already in canonical order).
    pub fn from_hashes(a: u64, b: u64) -> Self {
        if a <= b {
            DefEqKey {
                lhs_hash: a,
                rhs_hash: b,
            }
        } else {
            DefEqKey {
                lhs_hash: b,
                rhs_hash: a,
            }
        }
    }
}

// ---------------------------------------------------------------------------
// DefEqCache construction and core operations
// ---------------------------------------------------------------------------

impl DefEqCache {
    /// Create a new empty cache with the given capacity and LRU eviction.
    pub fn new(max_size: usize) -> Self {
        DefEqCache {
            hits: 0,
            misses: 0,
            entries: std::collections::HashMap::new(),
            max_size,
            eviction: CacheEviction::LRU,
            clock: 0,
            insertion_order: std::collections::HashMap::new(),
            insert_clock: 0,
        }
    }

    /// Create a new cache with an explicit eviction policy.
    pub fn with_eviction(max_size: usize, eviction: CacheEviction) -> Self {
        DefEqCache {
            eviction,
            ..DefEqCache::new(max_size)
        }
    }

    /// Look up whether `(lhs, rhs)` is in the cache.
    ///
    /// Updates `hits`/`misses` and refreshes the LRU timestamp on a hit.
    pub fn lookup(&mut self, lhs: &Expr, rhs: &Expr) -> Option<bool> {
        let key = DefEqKey::new(lhs, rhs);
        self.clock = self.clock.wrapping_add(1);
        let now = self.clock;
        if let Some(entry) = self.entries.get_mut(&key) {
            entry.checked_count = entry.checked_count.saturating_add(1);
            entry.last_access = now;
            self.hits += 1;
            Some(entry.result)
        } else {
            self.misses += 1;
            None
        }
    }

    /// Insert the result of a definitional equality check into the cache.
    ///
    /// If the cache is full, one entry is evicted according to the policy
    /// before insertion.
    pub fn insert(&mut self, lhs: &Expr, rhs: &Expr, result: bool) {
        let key = DefEqKey::new(lhs, rhs);
        // If entry already exists, update it in-place.
        if let Some(entry) = self.entries.get_mut(&key) {
            entry.result = result;
            return;
        }
        // Evict if at capacity.
        if self.entries.len() >= self.max_size && self.max_size > 0 {
            self.evict_by_policy();
        }
        self.clock = self.clock.wrapping_add(1);
        self.insert_clock = self.insert_clock.wrapping_add(1);
        let now = self.clock;
        let ins = self.insert_clock;
        self.insertion_order.insert(key, ins);
        self.entries.insert(
            key,
            DefEqEntry {
                key,
                result,
                checked_count: 0,
                last_access: now,
            },
        );
    }

    /// Evict one entry according to the configured eviction policy.
    fn evict_by_policy(&mut self) {
        match self.eviction {
            CacheEviction::LRU => self.evict_lru(),
            CacheEviction::LFU => self.evict_lfu(),
            CacheEviction::FIFO => self.evict_fifo(),
        }
    }

    /// Remove the least-recently-used entry.
    pub fn evict_lru(&mut self) {
        let victim = self
            .entries
            .iter()
            .min_by_key(|(_, e)| e.last_access)
            .map(|(k, _)| *k);
        if let Some(k) = victim {
            self.entries.remove(&k);
            self.insertion_order.remove(&k);
        }
    }

    /// Remove the least-frequently-used entry.
    pub fn evict_lfu(&mut self) {
        let victim = self
            .entries
            .iter()
            .min_by_key(|(_, e)| e.checked_count)
            .map(|(k, _)| *k);
        if let Some(k) = victim {
            self.entries.remove(&k);
            self.insertion_order.remove(&k);
        }
    }

    /// Remove the oldest-inserted entry (FIFO).
    pub fn evict_fifo(&mut self) {
        let victim = self
            .insertion_order
            .iter()
            .min_by_key(|(_, &ord)| ord)
            .map(|(k, _)| *k);
        if let Some(k) = victim {
            self.entries.remove(&k);
            self.insertion_order.remove(&k);
        }
    }

    /// Return a snapshot of current cache statistics.
    pub fn stats(&self) -> DefEqCacheStats {
        let total = self.hits + self.misses;
        let hit_rate = if total == 0 {
            0.0
        } else {
            self.hits as f64 / total as f64
        };
        DefEqCacheStats {
            hits: self.hits,
            misses: self.misses,
            hit_rate,
            size: self.entries.len(),
        }
    }

    /// Clear all entries and reset statistics.
    pub fn clear(&mut self) {
        self.entries.clear();
        self.insertion_order.clear();
        self.hits = 0;
        self.misses = 0;
        self.clock = 0;
        self.insert_clock = 0;
    }

    /// Return the number of currently cached entries.
    pub fn size(&self) -> usize {
        self.entries.len()
    }

    /// Return true if the cache has no entries.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

// ---------------------------------------------------------------------------
// Memoizing wrapper
// ---------------------------------------------------------------------------

/// Memoizing wrapper that checks the cache before invoking the checker.
///
/// If `(lhs, rhs)` is already in `cache`, returns the cached result immediately.
/// Otherwise, calls `check()`, stores the result in the cache, and returns it.
///
/// This is the primary way to integrate `DefEqCache` into a definitional
/// equality checker without modifying the checker itself.
///
/// # Example
///
/// ```ignore
/// let result = with_cache(&mut cache, &expr_a, &expr_b, || {
///     expensive_def_eq_check(&expr_a, &expr_b)
/// });
/// ```
pub fn with_cache<F>(cache: &mut DefEqCache, lhs: &Expr, rhs: &Expr, check: F) -> bool
where
    F: FnOnce() -> bool,
{
    if let Some(cached) = cache.lookup(lhs, rhs) {
        return cached;
    }
    let result = check();
    cache.insert(lhs, rhs, result);
    result
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Expr, Level, Name};

    fn prop() -> Expr {
        Expr::Sort(Level::Zero)
    }

    fn type0() -> Expr {
        Expr::Sort(Level::succ(Level::Zero))
    }

    fn const_expr(name: &str) -> Expr {
        Expr::Const(Name::from_str(name), vec![])
    }

    fn bvar(n: u32) -> Expr {
        Expr::BVar(n)
    }

    // --- DefEqKey ---

    #[test]
    fn test_key_symmetric() {
        let a = prop();
        let b = type0();
        let k1 = DefEqKey::new(&a, &b);
        let k2 = DefEqKey::new(&b, &a);
        assert_eq!(k1, k2, "DefEqKey must be symmetric");
    }

    #[test]
    fn test_key_same_expr() {
        let a = prop();
        let k = DefEqKey::new(&a, &a);
        assert_eq!(k.lhs_hash, k.rhs_hash);
    }

    #[test]
    fn test_key_from_hashes_canonical() {
        let k1 = DefEqKey::from_hashes(10, 20);
        let k2 = DefEqKey::from_hashes(20, 10);
        assert_eq!(k1, k2);
        assert_eq!(k1.lhs_hash, 10);
        assert_eq!(k1.rhs_hash, 20);
    }

    #[test]
    fn test_key_distinct_exprs() {
        let a = prop();
        let b = type0();
        let ka = DefEqKey::new(&a, &a);
        let kb = DefEqKey::new(&b, &b);
        let kab = DefEqKey::new(&a, &b);
        // All three should be distinct (modulo hash collision, which is unlikely).
        assert_ne!(ka, kb);
        assert_ne!(ka, kab);
    }

    // --- DefEqCache construction ---

    #[test]
    fn test_new_cache_empty() {
        let cache = DefEqCache::new(128);
        assert!(cache.is_empty());
        assert_eq!(cache.hits, 0);
        assert_eq!(cache.misses, 0);
    }

    #[test]
    fn test_new_cache_zero_capacity() {
        // A zero-capacity cache should not panic.
        let cache = DefEqCache::new(0);
        assert_eq!(cache.max_size, 0);
    }

    // --- lookup / insert ---

    #[test]
    fn test_miss_on_empty() {
        let mut cache = DefEqCache::new(64);
        let result = cache.lookup(&prop(), &type0());
        assert!(result.is_none());
        assert_eq!(cache.misses, 1);
        assert_eq!(cache.hits, 0);
    }

    #[test]
    fn test_hit_after_insert() {
        let mut cache = DefEqCache::new(64);
        let a = prop();
        let b = type0();
        cache.insert(&a, &b, true);
        let result = cache.lookup(&a, &b);
        assert_eq!(result, Some(true));
        assert_eq!(cache.hits, 1);
    }

    #[test]
    fn test_symmetric_hit() {
        let mut cache = DefEqCache::new(64);
        let a = prop();
        let b = type0();
        cache.insert(&a, &b, false);
        // Lookup with reversed arguments should also hit.
        let result = cache.lookup(&b, &a);
        assert_eq!(result, Some(false));
    }

    #[test]
    fn test_insert_same_entry_twice() {
        let mut cache = DefEqCache::new(64);
        let a = prop();
        let b = type0();
        cache.insert(&a, &b, true);
        cache.insert(&a, &b, false); // overwrite
        let result = cache.lookup(&a, &b);
        assert_eq!(result, Some(false));
    }

    #[test]
    fn test_size_tracking() {
        let mut cache = DefEqCache::new(64);
        assert_eq!(cache.size(), 0);
        cache.insert(&prop(), &type0(), true);
        assert_eq!(cache.size(), 1);
        cache.insert(&bvar(0), &bvar(1), false);
        assert_eq!(cache.size(), 2);
    }

    // --- clear ---

    #[test]
    fn test_clear_resets_everything() {
        let mut cache = DefEqCache::new(64);
        cache.insert(&prop(), &type0(), true);
        let _ = cache.lookup(&prop(), &type0());
        cache.clear();
        assert!(cache.is_empty());
        assert_eq!(cache.hits, 0);
        assert_eq!(cache.misses, 0);
    }

    // --- stats ---

    #[test]
    fn test_stats_zero_queries() {
        let cache = DefEqCache::new(64);
        let s = cache.stats();
        assert_eq!(s.hits, 0);
        assert_eq!(s.misses, 0);
        assert_eq!(s.hit_rate, 0.0);
        assert_eq!(s.size, 0);
    }

    #[test]
    fn test_stats_hit_rate() {
        let mut cache = DefEqCache::new(64);
        let a = prop();
        let b = type0();
        cache.insert(&a, &b, true);
        let _ = cache.lookup(&a, &b); // hit
        let _ = cache.lookup(&bvar(0), &bvar(1)); // miss
        let s = cache.stats();
        assert_eq!(s.hits, 1);
        assert_eq!(s.misses, 1);
        assert!((s.hit_rate - 0.5).abs() < 1e-9);
    }

    // --- eviction ---

    #[test]
    fn test_lru_eviction_capacity_respected() {
        let mut cache = DefEqCache::new(2);
        cache.insert(&prop(), &type0(), true);
        cache.insert(&bvar(0), &bvar(1), false);
        // Access first entry to make it recently used.
        let _ = cache.lookup(&prop(), &type0());
        // Insert a third entry — should evict the LRU (bvar(0)/bvar(1)).
        cache.insert(&const_expr("Nat"), &const_expr("Int"), true);
        assert_eq!(cache.size(), 2, "cache should not exceed max_size");
    }

    #[test]
    fn test_lfu_eviction() {
        let mut cache = DefEqCache::with_eviction(2, CacheEviction::LFU);
        cache.insert(&prop(), &type0(), true);
        cache.insert(&bvar(0), &bvar(1), false);
        // Access first entry many times.
        for _ in 0..5 {
            let _ = cache.lookup(&prop(), &type0());
        }
        // Insert third entry — should evict the less-frequently-used bvar pair.
        cache.insert(&const_expr("Nat"), &const_expr("Int"), true);
        assert_eq!(cache.size(), 2);
    }

    #[test]
    fn test_fifo_eviction() {
        let mut cache = DefEqCache::with_eviction(2, CacheEviction::FIFO);
        cache.insert(&prop(), &type0(), true); // inserted first
        cache.insert(&bvar(0), &bvar(1), false); // inserted second
                                                 // Access first entry to make it recently used (should NOT affect FIFO).
        let _ = cache.lookup(&prop(), &type0());
        // Third insertion should evict the first-inserted (prop/type0).
        cache.insert(&const_expr("Nat"), &const_expr("Int"), true);
        assert_eq!(cache.size(), 2);
        // After FIFO eviction of the first entry, its lookup should miss.
        let result = cache.lookup(&prop(), &type0());
        assert!(result.is_none(), "FIFO should have evicted the first entry");
    }

    #[test]
    fn test_evict_lru_explicit() {
        let mut cache = DefEqCache::new(4);
        cache.insert(&prop(), &type0(), true);
        cache.insert(&bvar(0), &bvar(1), false);
        // Access second entry to make it recently used.
        let _ = cache.lookup(&bvar(0), &bvar(1));
        cache.evict_lru();
        assert_eq!(cache.size(), 1);
        // The LRU (prop/type0, never accessed after insert) should be gone.
        assert!(cache.lookup(&prop(), &type0()).is_none());
    }

    // --- with_cache ---

    #[test]
    fn test_with_cache_miss_calls_check() {
        let mut cache = DefEqCache::new(64);
        let a = prop();
        let b = type0();
        let mut called = false;
        let result = with_cache(&mut cache, &a, &b, || {
            called = true;
            true
        });
        assert!(called, "checker should have been invoked on a miss");
        assert!(result);
    }

    #[test]
    fn test_with_cache_hit_skips_check() {
        let mut cache = DefEqCache::new(64);
        let a = prop();
        let b = type0();
        cache.insert(&a, &b, false);
        let mut called = false;
        let result = with_cache(&mut cache, &a, &b, || {
            called = true;
            true // would return a different value if called
        });
        assert!(!called, "checker must not be invoked on a cache hit");
        assert!(!result, "cached value (false) must be returned");
    }

    #[test]
    fn test_with_cache_stores_result() {
        let mut cache = DefEqCache::new(64);
        let a = prop();
        let b = type0();
        let _ = with_cache(&mut cache, &a, &b, || true);
        // Second call should be a cache hit.
        let result = with_cache(&mut cache, &a, &b, || false);
        assert!(result, "second call should return the cached true");
        assert_eq!(cache.hits, 1);
    }

    #[test]
    fn test_with_cache_symmetric_hit() {
        let mut cache = DefEqCache::new(64);
        let a = prop();
        let b = type0();
        let _ = with_cache(&mut cache, &a, &b, || true);
        // Reverse argument order.
        let mut called = false;
        let result = with_cache(&mut cache, &b, &a, || {
            called = true;
            false
        });
        assert!(!called);
        assert!(result);
    }

    // --- CacheEviction display ---

    #[test]
    fn test_eviction_display() {
        assert_eq!(format!("{}", CacheEviction::LRU), "LRU");
        assert_eq!(format!("{}", CacheEviction::LFU), "LFU");
        assert_eq!(format!("{}", CacheEviction::FIFO), "FIFO");
    }

    // --- DefEqCacheStats display ---

    #[test]
    fn test_stats_display() {
        let cache = DefEqCache::new(64);
        let s = cache.stats();
        let text = format!("{}", s);
        assert!(text.contains("DefEqCacheStats"));
    }
}