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
//! `HashConsArena` — deduplicated `Arena<Expr>`.

use crate::arena::{Arena, Idx};
use crate::{BinderInfo, Expr, FVarId, Level, Literal, Name};
use std::collections::HashMap;

use super::key::ExprKey;
use super::stats::HashConsStats;

/// A hash-consing wrapper around `Arena<Expr>`.
///
/// Every `mk_*` method checks a `HashMap<ExprKey, Idx<Expr>>` before
/// allocating. If an equal node already exists in the arena the existing
/// `Idx` is returned (a *hit*). Otherwise the node is allocated and
/// indexed (a *miss*).
///
/// # Structural sharing guarantee
///
/// For any two calls `mk_foo(…args…)` with structurally identical arguments,
/// the returned `Idx<Expr>` values are equal. This means:
///
/// - `O(1)` equality of deduplicated nodes via index comparison
/// - Significantly reduced arena memory for expression-heavy workloads
///   (e.g. type checking with many repeated sub-expressions)
pub struct HashConsArena {
    /// Backing arena; all nodes live here.
    arena: Arena<Expr>,
    /// Cache: expression content → arena index.
    cache: HashMap<ExprKey, Idx<Expr>>,
    /// Hit counter.
    hits: u64,
    /// Miss counter.
    misses: u64,
}

impl HashConsArena {
    /// Creates an empty `HashConsArena`.
    pub fn new() -> Self {
        Self {
            arena: Arena::new(),
            cache: HashMap::new(),
            hits: 0,
            misses: 0,
        }
    }

    /// Creates a `HashConsArena` with pre-allocated capacity for `cap` nodes.
    pub fn with_capacity(cap: usize) -> Self {
        Self {
            arena: Arena::with_capacity(cap),
            cache: HashMap::with_capacity(cap),
            hits: 0,
            misses: 0,
        }
    }

    // -----------------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------------

    /// Core deduplication: look up `key`; allocate `expr` on a miss.
    ///
    /// Takes both `key` (for lookup) and `expr` (for insertion) separately so
    /// the caller can build the key cheaply and only clone the full `Expr`
    /// when a miss occurs.
    fn get_or_insert(&mut self, key: ExprKey, expr: Expr) -> Idx<Expr> {
        if let Some(idx) = self.cache.get(&key) {
            self.hits += 1;
            return idx.clone();
        }
        self.misses += 1;
        let idx = self.arena.alloc(expr);
        self.cache.insert(key, idx.clone());
        idx
    }

    // -----------------------------------------------------------------------
    // Public read API
    // -----------------------------------------------------------------------

    /// Retrieve the `Expr` stored at `idx`.
    pub fn get(&self, idx: Idx<Expr>) -> &Expr {
        self.arena.get(idx)
    }

    /// Returns the number of distinct nodes in the arena.
    pub fn len(&self) -> usize {
        self.arena.len()
    }

    /// Returns `true` when no nodes have been allocated.
    pub fn is_empty(&self) -> bool {
        self.arena.is_empty()
    }

    /// Returns a statistics snapshot.
    pub fn stats(&self) -> HashConsStats {
        HashConsStats {
            hits: self.hits,
            misses: self.misses,
            total: self.hits + self.misses,
            unique_nodes: self.arena.len(),
        }
    }

    /// Borrow the underlying `Arena<Expr>` for read-only access.
    pub fn arena(&self) -> &Arena<Expr> {
        &self.arena
    }

    // -----------------------------------------------------------------------
    // mk_* constructors
    // -----------------------------------------------------------------------

    /// Allocate (or deduplicate) `Expr::Sort(level)`.
    pub fn mk_sort(&mut self, level: Level) -> Idx<Expr> {
        let key = ExprKey::sort(level.clone());
        let expr = Expr::Sort(level);
        self.get_or_insert(key, expr)
    }

    /// Allocate (or deduplicate) `Expr::BVar(idx)`.
    pub fn mk_bvar(&mut self, idx: u32) -> Idx<Expr> {
        let key = ExprKey::bvar(idx);
        let expr = Expr::BVar(idx);
        self.get_or_insert(key, expr)
    }

    /// Allocate (or deduplicate) `Expr::FVar(id)`.
    pub fn mk_fvar(&mut self, id: FVarId) -> Idx<Expr> {
        let key = ExprKey::fvar(id);
        let expr = Expr::FVar(id);
        self.get_or_insert(key, expr)
    }

    /// Allocate (or deduplicate) `Expr::Const(name, levels)`.
    pub fn mk_const(&mut self, name: Name, levels: Vec<Level>) -> Idx<Expr> {
        let key = ExprKey::const_(name.clone(), levels.clone());
        let expr = Expr::Const(name, levels);
        self.get_or_insert(key, expr)
    }

    /// Allocate (or deduplicate) `Expr::App(f_expr, a_expr)`.
    ///
    /// `f_expr` and `a_expr` are cloned once to build the `ExprKey`; no
    /// extra clones occur on a cache hit.
    pub fn mk_app(&mut self, f_expr: Expr, a_expr: Expr) -> Idx<Expr> {
        let key = ExprKey::app(f_expr.clone(), a_expr.clone());
        let expr = Expr::App(Box::new(f_expr), Box::new(a_expr));
        self.get_or_insert(key, expr)
    }

    /// Allocate (or deduplicate) `Expr::Lam(bi, name, dom, body)`.
    pub fn mk_lam(&mut self, bi: BinderInfo, name: Name, dom: Expr, body: Expr) -> Idx<Expr> {
        let key = ExprKey::lam(bi, name.clone(), dom.clone(), body.clone());
        let expr = Expr::Lam(bi, name, Box::new(dom), Box::new(body));
        self.get_or_insert(key, expr)
    }

    /// Allocate (or deduplicate) `Expr::Pi(bi, name, dom, cod)`.
    pub fn mk_pi(&mut self, bi: BinderInfo, name: Name, dom: Expr, cod: Expr) -> Idx<Expr> {
        let key = ExprKey::pi(bi, name.clone(), dom.clone(), cod.clone());
        let expr = Expr::Pi(bi, name, Box::new(dom), Box::new(cod));
        self.get_or_insert(key, expr)
    }

    /// Allocate (or deduplicate) `Expr::Let(name, ty, val, body)`.
    pub fn mk_let(&mut self, name: Name, ty: Expr, val: Expr, body: Expr) -> Idx<Expr> {
        let key = ExprKey::let_(name.clone(), ty.clone(), val.clone(), body.clone());
        let expr = Expr::Let(name, Box::new(ty), Box::new(val), Box::new(body));
        self.get_or_insert(key, expr)
    }

    /// Allocate (or deduplicate) `Expr::Lit(lit)`.
    pub fn mk_lit(&mut self, lit: Literal) -> Idx<Expr> {
        let key = ExprKey::lit(lit.clone());
        let expr = Expr::Lit(lit);
        self.get_or_insert(key, expr)
    }

    /// Allocate (or deduplicate) `Expr::Proj(name, field_idx, struct_expr)`.
    pub fn mk_proj(&mut self, name: Name, field_idx: u32, struct_expr: Expr) -> Idx<Expr> {
        let key = ExprKey::proj(name.clone(), field_idx, struct_expr.clone());
        let expr = Expr::Proj(name, field_idx, Box::new(struct_expr));
        self.get_or_insert(key, expr)
    }
}

impl Default for HashConsArena {
    fn default() -> Self {
        Self::new()
    }
}

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

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

    // ---- mk_sort -----------------------------------------------------------

    #[test]
    fn test_mk_sort_deduplicates() {
        let mut hc = HashConsArena::new();
        let i1 = hc.mk_sort(Level::Zero);
        let i2 = hc.mk_sort(Level::Zero);
        assert_eq!(i1, i2);
        assert_eq!(hc.len(), 1);
    }

    #[test]
    fn test_mk_sort_distinct_levels() {
        let mut hc = HashConsArena::new();
        let i0 = hc.mk_sort(Level::Zero);
        let i1 = hc.mk_sort(Level::succ(Level::Zero));
        assert_ne!(i0, i1);
        assert_eq!(hc.len(), 2);
    }

    // ---- mk_bvar -----------------------------------------------------------

    #[test]
    fn test_mk_bvar_deduplicates() {
        let mut hc = HashConsArena::new();
        let i1 = hc.mk_bvar(0);
        let i2 = hc.mk_bvar(0);
        assert_eq!(i1, i2);
    }

    #[test]
    fn test_mk_bvar_distinct_indices() {
        let mut hc = HashConsArena::new();
        let i0 = hc.mk_bvar(0);
        let i1 = hc.mk_bvar(1);
        assert_ne!(i0, i1);
    }

    // ---- mk_fvar -----------------------------------------------------------

    #[test]
    fn test_mk_fvar_deduplicates() {
        let mut hc = HashConsArena::new();
        let id = FVarId::new(7);
        let i1 = hc.mk_fvar(id);
        let i2 = hc.mk_fvar(id);
        assert_eq!(i1, i2);
    }

    // ---- mk_const ----------------------------------------------------------

    #[test]
    fn test_mk_const_deduplicates() {
        let mut hc = HashConsArena::new();
        let name = Name::str("Nat");
        let i1 = hc.mk_const(name.clone(), vec![]);
        let i2 = hc.mk_const(name, vec![]);
        assert_eq!(i1, i2);
    }

    #[test]
    fn test_mk_const_different_names_distinct() {
        let mut hc = HashConsArena::new();
        let i1 = hc.mk_const(Name::str("Nat"), vec![]);
        let i2 = hc.mk_const(Name::str("Int"), vec![]);
        assert_ne!(i1, i2);
    }

    // ---- mk_app ------------------------------------------------------------

    #[test]
    fn test_mk_app_deduplicates() {
        let mut hc = HashConsArena::new();
        let f = Expr::BVar(0);
        let a = Expr::BVar(1);
        let i1 = hc.mk_app(f.clone(), a.clone());
        let i2 = hc.mk_app(f, a);
        assert_eq!(i1, i2);
    }

    #[test]
    fn test_mk_app_different_args_distinct() {
        let mut hc = HashConsArena::new();
        let i1 = hc.mk_app(Expr::BVar(0), Expr::BVar(1));
        let i2 = hc.mk_app(Expr::BVar(0), Expr::BVar(2));
        assert_ne!(i1, i2);
    }

    // ---- mk_lam ------------------------------------------------------------

    #[test]
    fn test_mk_lam_deduplicates() {
        let mut hc = HashConsArena::new();
        let name = Name::str("x");
        let dom = Expr::Sort(Level::Zero);
        let body = Expr::BVar(0);
        let i1 = hc.mk_lam(BinderInfo::Default, name.clone(), dom.clone(), body.clone());
        let i2 = hc.mk_lam(BinderInfo::Default, name, dom, body);
        assert_eq!(i1, i2);
    }

    // ---- mk_pi -------------------------------------------------------------

    #[test]
    fn test_mk_pi_deduplicates() {
        let mut hc = HashConsArena::new();
        let name = Name::str("A");
        let dom = Expr::Sort(Level::Zero);
        let cod = Expr::BVar(0);
        let i1 = hc.mk_pi(BinderInfo::Default, name.clone(), dom.clone(), cod.clone());
        let i2 = hc.mk_pi(BinderInfo::Default, name, dom, cod);
        assert_eq!(i1, i2);
    }

    // ---- mk_let ------------------------------------------------------------

    #[test]
    fn test_mk_let_deduplicates() {
        let mut hc = HashConsArena::new();
        let name = Name::str("n");
        let ty = Expr::Sort(Level::Zero);
        let val = Expr::BVar(0);
        let body = Expr::BVar(1);
        let i1 = hc.mk_let(name.clone(), ty.clone(), val.clone(), body.clone());
        let i2 = hc.mk_let(name, ty, val, body);
        assert_eq!(i1, i2);
    }

    // ---- mk_lit / mk_proj --------------------------------------------------

    #[test]
    fn test_mk_lit_nat_deduplicates() {
        let mut hc = HashConsArena::new();
        let i1 = hc.mk_lit(Literal::Nat(42));
        let i2 = hc.mk_lit(Literal::Nat(42));
        assert_eq!(i1, i2);
    }

    #[test]
    fn test_mk_lit_str_deduplicates() {
        let mut hc = HashConsArena::new();
        let i1 = hc.mk_lit(Literal::Str("hello".to_string()));
        let i2 = hc.mk_lit(Literal::Str("hello".to_string()));
        assert_eq!(i1, i2);
    }

    #[test]
    fn test_mk_proj_deduplicates() {
        let mut hc = HashConsArena::new();
        let name = Name::str("fst");
        let se = Expr::BVar(0);
        let i1 = hc.mk_proj(name.clone(), 0, se.clone());
        let i2 = hc.mk_proj(name, 0, se);
        assert_eq!(i1, i2);
    }

    // ---- stats -------------------------------------------------------------

    #[test]
    fn test_stats_hit_miss_tracking() {
        let mut hc = HashConsArena::new();
        hc.mk_sort(Level::Zero); // miss
        hc.mk_sort(Level::Zero); // hit
        hc.mk_sort(Level::Zero); // hit
        hc.mk_bvar(0); // miss
        let s = hc.stats();
        assert_eq!(s.misses, 2);
        assert_eq!(s.hits, 2);
        assert_eq!(s.total, 4);
        assert_eq!(s.unique_nodes, 2);
    }

    #[test]
    fn test_stats_dedup_ratio() {
        let mut hc = HashConsArena::new();
        hc.mk_bvar(0); // miss
        hc.mk_bvar(0); // hit
        hc.mk_bvar(0); // hit
        hc.mk_bvar(0); // hit
        let s = hc.stats();
        // dedup_ratio = misses / total = 1/4 = 0.25
        assert!((s.dedup_ratio() - 0.25).abs() < 1e-10);
    }

    #[test]
    fn test_get_returns_correct_expr() {
        let mut hc = HashConsArena::new();
        let idx = hc.mk_bvar(5);
        assert_eq!(hc.get(idx), &Expr::BVar(5));
    }

    #[test]
    fn test_is_empty_initially() {
        let hc = HashConsArena::new();
        assert!(hc.is_empty());
    }

    #[test]
    fn test_with_capacity_works() {
        let mut hc = HashConsArena::with_capacity(64);
        hc.mk_sort(Level::Zero);
        assert_eq!(hc.len(), 1);
    }

    #[test]
    fn test_default_is_empty() {
        let hc = HashConsArena::default();
        assert!(hc.is_empty());
    }

    #[test]
    fn test_cross_variant_no_collision() {
        let mut hc = HashConsArena::new();
        // BVar(0) and Sort(Zero) must NOT collide even though both are "zero-ish"
        let bv = hc.mk_bvar(0);
        let sv = hc.mk_sort(Level::Zero);
        assert_ne!(bv, sv);
    }

    #[test]
    fn test_binder_info_distinguishes_lam() {
        let mut hc = HashConsArena::new();
        let name = Name::str("x");
        let dom = Expr::Sort(Level::Zero);
        let body = Expr::BVar(0);
        let explicit = hc.mk_lam(BinderInfo::Default, name.clone(), dom.clone(), body.clone());
        let implicit = hc.mk_lam(BinderInfo::Implicit, name, dom, body);
        assert_ne!(explicit, implicit);
    }

    #[test]
    fn test_complex_expression_sharing() {
        let mut hc = HashConsArena::new();
        // Build `id : Π (A : Type 0), A → A` structurally, twice.
        let prop = Expr::Sort(Level::Zero);
        let bv0 = Expr::BVar(0);
        // A → A  =  Π (_ : A), A
        let arr = Expr::Pi(
            BinderInfo::Default,
            Name::Anonymous,
            Box::new(bv0.clone()),
            Box::new(bv0.clone()),
        );
        let id_type_1 = hc.mk_pi(
            BinderInfo::Default,
            Name::str("A"),
            prop.clone(),
            arr.clone(),
        );
        let id_type_2 = hc.mk_pi(
            BinderInfo::Default,
            Name::str("A"),
            prop.clone(),
            arr.clone(),
        );
        assert_eq!(
            id_type_1, id_type_2,
            "identical Pi types must be deduplicated"
        );
        assert_eq!(hc.len(), 1, "only one node in the arena");
    }
}