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
//! Functions for the reduction statistics module.
//!
//! These helpers operate on `ReductionStats` and provide instrumented
//! wrappers around the WHNF reducer for counting reductions.

use crate::{Environment, Expr, Reducer};

use super::types::{DepthGuard, ReductionKind, ReductionSession, ReductionStats};

// ── Instrumented reduce helpers ───────────────────────────────────────────────

/// Perform a single WHNF step on `expr` and record statistics.
///
/// The caller supplies a mutable `Reducer` and `ReductionStats`.
/// This wrapper:
/// 1. Pushes/pops the depth counter.
/// 2. Records a beta step if the outermost form is `App(Lam, _)`.
/// 3. Records a zeta step if the outermost form is `Let(...)`.
/// 4. Records a delta step if the outermost form is `Const(name, _)` that
///    has a definition in `env`.
///
/// Returns the reduced expression.
pub fn whnf_tracked(
    expr: &Expr,
    env: &Environment,
    reducer: &mut Reducer,
    stats: &mut ReductionStats,
) -> Expr {
    // Push depth before borrowing stats for specific counters
    stats.push_depth();

    // Classify what kind of top-level step will happen
    match expr {
        Expr::App(f, _) => {
            if let Expr::Lam(_, _, _, _) = f.as_ref() {
                stats.increment_beta();
            }
        }
        Expr::Let(_, _, _, _) => stats.increment_zeta(),
        Expr::Const(name, _) if env.find(name).and_then(|ci| ci.value()).is_some() => {
            stats.increment_delta();
        }
        _ => {}
    }

    let result = reducer.whnf_env(expr, env);
    stats.pop_depth();
    result
}

/// Run a full WHNF reduction under stats tracking and return both the
/// reduced expression and a `ReductionStats` delta for this call.
pub fn whnf_with_stats(
    expr: &Expr,
    env: &Environment,
    reducer: &mut Reducer,
    stats: &mut ReductionStats,
) -> (Expr, ReductionStats) {
    let session = ReductionSession::begin(stats);
    let result = whnf_tracked(expr, env, reducer, stats);
    let delta = session.finish(stats);
    (result, delta)
}

/// Apply `f` while tracking depth in `stats`.  Useful for instrumenting
/// recursive calls that don't go through `whnf_tracked`.
pub fn with_depth_tracking<T, F>(stats: &mut ReductionStats, f: F) -> T
where
    F: FnOnce() -> T,
{
    stats.push_depth();
    let result = f();
    stats.pop_depth();
    result
}

/// Record a reduction of the given kind, returning the current total.
pub fn record_reduction(stats: &mut ReductionStats, kind: ReductionKind) -> u64 {
    stats.increment(kind);
    stats.total()
}

/// Format a `ReductionStats` as a human-readable table.
pub fn format_stats(stats: &ReductionStats) -> String {
    format!(
        "┌─────────────────────────────┐\n\
         │      Reduction Statistics   │\n\
         ├──────────────┬──────────────┤\n\
         │ β (beta)     │ {:>12} │\n\
         │ δ (delta)    │ {:>12} │\n\
         │ ζ (zeta)     │ {:>12} │\n\
         │ ι (iota)     │ {:>12} │\n\
         │ η (eta)      │ {:>12} │\n\
         │ level        │ {:>12} │\n\
         ├──────────────┼──────────────┤\n\
         │ Total steps  │ {:>12} │\n\
         │ Max depth    │ {:>12} │\n\
         └──────────────┴──────────────┘",
        stats.beta_count,
        stats.delta_count,
        stats.zeta_count,
        stats.iota_count,
        stats.eta_count,
        stats.level_count,
        stats.total_steps,
        stats.max_depth,
    )
}

/// Compute a percentage breakdown of reduction kinds.
///
/// Returns a `Vec<(ReductionKind, f64)>` sorted by frequency descending.
/// Returns an empty vec if `stats.total()` is zero.
pub fn reduction_breakdown(stats: &ReductionStats) -> Vec<(ReductionKind, f64)> {
    let total = stats.total();
    if total == 0 {
        return Vec::new();
    }
    let mut entries = vec![
        (ReductionKind::Beta, stats.beta_count),
        (ReductionKind::Delta, stats.delta_count),
        (ReductionKind::Zeta, stats.zeta_count),
        (ReductionKind::Iota, stats.iota_count),
        (ReductionKind::Eta, stats.eta_count),
        (ReductionKind::Level, stats.level_count),
    ];
    entries.sort_by_key(|b| std::cmp::Reverse(b.1));
    entries
        .into_iter()
        .map(|(kind, count)| (kind, count as f64 / total as f64 * 100.0))
        .collect()
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::reduction_stats::types::{ReductionKind, ReductionStats};

    // ── Basic counting ──────────────────────────────────────────────────────

    #[test]
    fn test_increment_beta() {
        let mut s = ReductionStats::new();
        s.increment_beta();
        assert_eq!(s.beta_count, 1);
        assert_eq!(s.total(), 1);
    }

    #[test]
    fn test_increment_delta() {
        let mut s = ReductionStats::new();
        s.increment_delta();
        assert_eq!(s.delta_count, 1);
        assert_eq!(s.total(), 1);
    }

    #[test]
    fn test_increment_zeta() {
        let mut s = ReductionStats::new();
        s.increment_zeta();
        assert_eq!(s.zeta_count, 1);
        assert_eq!(s.total(), 1);
    }

    #[test]
    fn test_increment_iota() {
        let mut s = ReductionStats::new();
        s.increment_iota();
        assert_eq!(s.iota_count, 1);
        assert_eq!(s.total(), 1);
    }

    #[test]
    fn test_increment_eta() {
        let mut s = ReductionStats::new();
        s.increment_eta();
        assert_eq!(s.eta_count, 1);
        assert_eq!(s.total(), 1);
    }

    #[test]
    fn test_increment_level() {
        let mut s = ReductionStats::new();
        s.increment_level();
        assert_eq!(s.level_count, 1);
        assert_eq!(s.total(), 1);
    }

    #[test]
    fn test_increment_by_kind() {
        let mut s = ReductionStats::new();
        s.increment(ReductionKind::Beta);
        s.increment(ReductionKind::Delta);
        s.increment(ReductionKind::Zeta);
        s.increment(ReductionKind::Iota);
        s.increment(ReductionKind::Eta);
        s.increment(ReductionKind::Level);
        assert_eq!(s.total(), 6);
        assert_eq!(s.count_for(ReductionKind::Beta), 1);
        assert_eq!(s.count_for(ReductionKind::Delta), 1);
        assert_eq!(s.count_for(ReductionKind::Zeta), 1);
        assert_eq!(s.count_for(ReductionKind::Iota), 1);
        assert_eq!(s.count_for(ReductionKind::Eta), 1);
        assert_eq!(s.count_for(ReductionKind::Level), 1);
    }

    #[test]
    fn test_total_is_sum() {
        let mut s = ReductionStats::new();
        for _ in 0..5 {
            s.increment_beta();
        }
        for _ in 0..3 {
            s.increment_delta();
        }
        assert_eq!(s.total(), 8);
        assert_eq!(s.beta_count + s.delta_count, s.total_steps);
    }

    // ── Depth tracking ──────────────────────────────────────────────────────

    #[test]
    fn test_push_pop_depth() {
        let mut s = ReductionStats::new();
        assert_eq!(s.current_depth(), 0);
        s.push_depth();
        assert_eq!(s.current_depth(), 1);
        s.push_depth();
        assert_eq!(s.current_depth(), 2);
        assert_eq!(s.max_depth, 2);
        s.pop_depth();
        assert_eq!(s.current_depth(), 1);
        s.pop_depth();
        assert_eq!(s.current_depth(), 0);
        assert_eq!(s.max_depth, 2); // max_depth doesn't decrease
    }

    #[test]
    fn test_pop_depth_saturates() {
        let mut s = ReductionStats::new();
        s.pop_depth(); // should not underflow
        assert_eq!(s.current_depth(), 0);
    }

    #[test]
    fn test_depth_guard() {
        let mut s = ReductionStats::new();
        // DepthGuard borrows `s` exclusively — test its effect after it drops
        {
            let _g = DepthGuard::new(&mut s);
            // Can't read s.current_depth() here due to exclusive borrow —
            // the guard is verified by observing max_depth after it drops.
        }
        // After drop: depth is back to 0; max_depth records the high-water mark
        assert_eq!(s.current_depth(), 0);
        assert_eq!(s.max_depth, 1, "DepthGuard should have pushed to depth 1");
    }

    #[test]
    fn test_nested_depth_guards() {
        let mut s = ReductionStats::new();
        // Test depth guard effect by using nested scopes and checking after drop
        {
            // Push manually to verify intermediate states
            s.push_depth(); // depth 1
            assert_eq!(s.current_depth(), 1);
            {
                s.push_depth(); // depth 2
                assert_eq!(s.current_depth(), 2);
                s.pop_depth(); // depth 1
            }
            assert_eq!(s.current_depth(), 1);
            s.pop_depth(); // depth 0
        }
        assert_eq!(s.current_depth(), 0);
        assert_eq!(s.max_depth, 2);
    }

    // ── Merge ───────────────────────────────────────────────────────────────

    #[test]
    fn test_merge() {
        let mut a = ReductionStats::new();
        a.increment_beta();
        a.increment_beta();
        a.push_depth();
        a.push_depth();

        let mut b = ReductionStats::new();
        b.increment_delta();
        b.push_depth();
        b.push_depth();
        b.push_depth();

        a.merge(&b);
        assert_eq!(a.beta_count, 2);
        assert_eq!(a.delta_count, 1);
        assert_eq!(a.total(), 3);
        assert_eq!(a.max_depth, 3); // max of a.max_depth=2 and b.max_depth=3
    }

    // ── Reset ───────────────────────────────────────────────────────────────

    #[test]
    fn test_reset() {
        let mut s = ReductionStats::new();
        s.increment_beta();
        s.increment_delta();
        s.push_depth();
        s.reset();
        assert_eq!(s.total(), 0);
        assert_eq!(s.max_depth, 0);
        assert_eq!(s.current_depth(), 0);
    }

    // ── Snapshot / delta ────────────────────────────────────────────────────

    #[test]
    fn test_snapshot_and_delta() {
        let mut s = ReductionStats::new();
        s.increment_beta();
        let snap = s.snapshot();
        s.increment_beta();
        s.increment_delta();
        let delta = s.delta_from(&snap);
        assert_eq!(delta.beta_count, 1);
        assert_eq!(delta.delta_count, 1);
        assert_eq!(delta.total(), 2);
    }

    #[test]
    fn test_session() {
        let mut s = ReductionStats::new();
        let session = ReductionSession::begin(&s);
        s.increment_beta();
        s.increment_iota();
        let delta = session.finish(&s);
        assert_eq!(delta.total(), 2);
        assert_eq!(delta.beta_count, 1);
        assert_eq!(delta.iota_count, 1);
    }

    // ── record_reduction ────────────────────────────────────────────────────

    #[test]
    fn test_record_reduction() {
        let mut s = ReductionStats::new();
        let total = record_reduction(&mut s, ReductionKind::Beta);
        assert_eq!(total, 1);
        let total2 = record_reduction(&mut s, ReductionKind::Iota);
        assert_eq!(total2, 2);
    }

    // ── breakdown ───────────────────────────────────────────────────────────

    #[test]
    fn test_breakdown_empty() {
        let s = ReductionStats::new();
        assert!(reduction_breakdown(&s).is_empty());
    }

    #[test]
    fn test_breakdown_percentages_sum_to_100() {
        let mut s = ReductionStats::new();
        for _ in 0..30 {
            s.increment_beta();
        }
        for _ in 0..70 {
            s.increment_delta();
        }
        let breakdown = reduction_breakdown(&s);
        let sum: f64 = breakdown.iter().map(|(_, pct)| pct).sum();
        assert!((sum - 100.0).abs() < 0.001);
    }

    #[test]
    fn test_breakdown_sorted_descending() {
        let mut s = ReductionStats::new();
        s.increment_iota();
        s.increment_iota();
        s.increment_iota();
        s.increment_beta();
        let breakdown = reduction_breakdown(&s);
        assert_eq!(breakdown[0].0, ReductionKind::Iota);
        assert_eq!(breakdown[1].0, ReductionKind::Beta);
    }

    // ── is_empty ────────────────────────────────────────────────────────────

    #[test]
    fn test_is_empty() {
        let s = ReductionStats::new();
        assert!(s.is_empty());
        let mut s2 = ReductionStats::new();
        s2.increment_beta();
        assert!(!s2.is_empty());
    }

    // ── Display ─────────────────────────────────────────────────────────────

    #[test]
    fn test_display_contains_totals() {
        let mut s = ReductionStats::new();
        s.increment_beta();
        s.increment_delta();
        let text = format!("{}", s);
        assert!(text.contains("total"));
        assert!(text.contains("2") || text.contains("beta") || text.contains("delta"));
    }

    #[test]
    fn test_format_stats_contains_sections() {
        let s = ReductionStats::new();
        let table = format_stats(&s);
        assert!(table.contains("beta"));
        assert!(table.contains("delta"));
        assert!(table.contains("Total"));
        assert!(table.contains("Max depth"));
    }
}