lean-agentic 0.1.0

Core library for Lean-Agentic: hash-consed dependent types with 150x faster equality
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
//! Definitional equality and weak head normal form evaluation
//!
//! Implements conversion checking through normalization with
//! beta, delta, zeta, and iota reductions.

use crate::arena::Arena;
use crate::context::Context;
use crate::environment::Environment;
use crate::term::{TermId, TermKind};
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::RwLock;

/// Fuel for preventing non-termination in reduction
const DEFAULT_FUEL: u32 = 10000;

/// Cache for memoizing WHNF computations
type WhnfCache = Arc<RwLock<HashMap<(TermId, usize), TermId>>>;

/// Conversion checker with WHNF evaluation
pub struct Converter {
    /// Fuel remaining to prevent infinite loops
    fuel: u32,

    /// Cache for WHNF results
    cache: WhnfCache,

    /// Statistics
    stats: ConversionStats,
}

/// Statistics for conversion checking
#[derive(Debug, Default, Clone)]
pub struct ConversionStats {
    /// Number of conversions checked
    pub checks: usize,

    /// Number of successful conversions
    pub successes: usize,

    /// Number of WHNF reductions
    pub reductions: usize,

    /// Cache hits
    pub cache_hits: usize,
}

impl Converter {
    /// Create a new converter with default fuel
    pub fn new() -> Self {
        Self {
            fuel: DEFAULT_FUEL,
            cache: Arc::new(RwLock::new(HashMap::new())),
            stats: ConversionStats::default(),
        }
    }

    /// Create a converter with custom fuel
    pub fn with_fuel(fuel: u32) -> Self {
        Self {
            fuel,
            cache: Arc::new(RwLock::new(HashMap::new())),
            stats: ConversionStats::default(),
        }
    }

    /// Check if two terms are definitionally equal
    pub fn is_def_eq(
        &mut self,
        arena: &mut Arena,
        env: &Environment,
        ctx: &Context,
        t1: TermId,
        t2: TermId,
    ) -> crate::Result<bool> {
        self.stats.checks += 1;

        // Fast path: pointer equality
        if t1 == t2 {
            self.stats.successes += 1;
            return Ok(true);
        }

        // Reduce both to WHNF and compare
        let whnf1 = self.whnf(arena, env, ctx, t1)?;
        let whnf2 = self.whnf(arena, env, ctx, t2)?;

        if whnf1 == whnf2 {
            self.stats.successes += 1;
            return Ok(true);
        }

        // Structural comparison
        let result = self.is_def_eq_whnf(arena, env, ctx, whnf1, whnf2)?;
        if result {
            self.stats.successes += 1;
        }

        Ok(result)
    }

    /// Reduce a term to weak head normal form
    pub fn whnf(
        &mut self,
        arena: &mut Arena,
        env: &Environment,
        ctx: &Context,
        term: TermId,
    ) -> crate::Result<TermId> {
        if self.fuel == 0 {
            return Err(crate::Error::Internal(
                "Out of fuel during normalization".to_string(),
            ));
        }

        // Check cache
        let cache_key = (term, ctx.len());
        {
            let cache = self.cache.read().unwrap();
            if let Some(&cached) = cache.get(&cache_key) {
                self.stats.cache_hits += 1;
                return Ok(cached);
            }
        }

        self.fuel -= 1;
        self.stats.reductions += 1;

        let kind = arena.kind(term).ok_or_else(|| {
            crate::Error::Internal(format!("Invalid term ID: {:?}", term))
        })?.clone();

        let result = match kind {
            // Variables: look up in context for let-bound values
            TermKind::Var(idx) => {
                if let Some(value) = ctx.value_of(idx) {
                    self.whnf(arena, env, ctx, value)?
                } else {
                    term
                }
            }

            // Constants: unfold if reducible
            TermKind::Const(name, _levels) => {
                if let Some(decl) = env.get_decl(name) {
                    if decl.is_reducible() {
                        if let Some(body) = decl.value {
                            // Instantiate universe parameters if needed
                            // For now, just reduce the body
                            self.whnf(arena, env, ctx, body)?
                        } else {
                            term
                        }
                    } else {
                        term
                    }
                } else {
                    term
                }
            }

            // Application: try beta reduction
            TermKind::App(func, arg) => {
                let func_whnf = self.whnf(arena, env, ctx, func)?;

                if let Some(TermKind::Lam(_binder, body)) = arena.kind(func_whnf).cloned() {
                    // Beta reduction: (λx.body) arg ~> body[x := arg]
                    let subst = self.substitute(arena, body, 0, arg)?;
                    self.whnf(arena, env, ctx, subst)?
                } else {
                    // Can't reduce further
                    if func_whnf != func {
                        let new_app = arena.mk_app(func_whnf, arg);
                        self.whnf(arena, env, ctx, new_app)?
                    } else {
                        term
                    }
                }
            }

            // Let expression: zeta reduction
            TermKind::Let(_binder, value, body) => {
                // Substitute value into body
                let subst = self.substitute(arena, body, 0, value)?;
                self.whnf(arena, env, ctx, subst)?
            }

            // Already in WHNF
            TermKind::Sort(_) | TermKind::Pi(_, _) | TermKind::Lam(_, _) => term,

            // Metavariables and literals are values
            TermKind::MVar(_) | TermKind::Lit(_) => term,
        };

        // Cache the result
        {
            let mut cache = self.cache.write().unwrap();
            cache.insert(cache_key, result);
        }

        Ok(result)
    }

    /// Compare two terms in WHNF
    fn is_def_eq_whnf(
        &mut self,
        arena: &mut Arena,
        env: &Environment,
        ctx: &Context,
        t1: TermId,
        t2: TermId,
    ) -> crate::Result<bool> {
        if t1 == t2 {
            return Ok(true);
        }

        let kind1 = arena.kind(t1).ok_or_else(|| {
            crate::Error::Internal(format!("Invalid term ID: {:?}", t1))
        })?.clone();

        let kind2 = arena.kind(t2).ok_or_else(|| {
            crate::Error::Internal(format!("Invalid term ID: {:?}", t2))
        })?.clone();

        match (kind1, kind2) {
            // Sorts
            (TermKind::Sort(l1), TermKind::Sort(l2)) => Ok(l1 == l2),

            // Variables
            (TermKind::Var(i1), TermKind::Var(i2)) => Ok(i1 == i2),

            // Constants
            (TermKind::Const(n1, lvls1), TermKind::Const(n2, lvls2)) => {
                Ok(n1 == n2 && lvls1 == lvls2)
            }

            // Applications
            (TermKind::App(f1, a1), TermKind::App(f2, a2)) => {
                let funcs_eq = self.is_def_eq(arena, env, ctx, f1, f2)?;
                let args_eq = self.is_def_eq(arena, env, ctx, a1, a2)?;
                Ok(funcs_eq && args_eq)
            }

            // Lambda
            (TermKind::Lam(b1, body1), TermKind::Lam(b2, body2)) => {
                // Check binder types
                let types_eq = self.is_def_eq(arena, env, ctx, b1.ty, b2.ty)?;
                if !types_eq {
                    return Ok(false);
                }

                // Check bodies under extended context
                let mut new_ctx = ctx.clone();
                new_ctx.push_var(b1.name, b1.ty);
                self.is_def_eq(arena, env, &new_ctx, body1, body2)
            }

            // Pi types
            (TermKind::Pi(b1, body1), TermKind::Pi(b2, body2)) => {
                // Check binder types
                let types_eq = self.is_def_eq(arena, env, ctx, b1.ty, b2.ty)?;
                if !types_eq {
                    return Ok(false);
                }

                // Check bodies under extended context
                let mut new_ctx = ctx.clone();
                new_ctx.push_var(b1.name, b1.ty);
                self.is_def_eq(arena, env, &new_ctx, body1, body2)
            }

            // Literals
            (TermKind::Lit(l1), TermKind::Lit(l2)) => Ok(l1 == l2),

            // Different constructors
            _ => Ok(false),
        }
    }

    /// Substitute a term in another term
    /// subst(term, idx, replacement) replaces variable #idx with replacement
    pub fn substitute(
        &mut self,
        arena: &mut Arena,
        term: TermId,
        idx: u32,
        replacement: TermId,
    ) -> crate::Result<TermId> {
        let kind = arena.kind(term).ok_or_else(|| {
            crate::Error::Internal(format!("Invalid term ID: {:?}", term))
        })?.clone();

        let result = match kind {
            TermKind::Var(i) => {
                if i == idx {
                    replacement
                } else {
                    term
                }
            }

            TermKind::App(func, arg) => {
                let new_func = self.substitute(arena, func, idx, replacement)?;
                let new_arg = self.substitute(arena, arg, idx, replacement)?;
                if new_func == func && new_arg == arg {
                    term
                } else {
                    arena.mk_app(new_func, new_arg)
                }
            }

            TermKind::Lam(binder, body) => {
                let old_ty = binder.ty;
                let new_ty = self.substitute(arena, binder.ty, idx, replacement)?;
                let new_body = self.substitute(arena, body, idx + 1, replacement)?;
                if new_ty == old_ty && new_body == body {
                    term
                } else {
                    let new_binder = crate::term::Binder { ty: new_ty, ..binder };
                    arena.mk_lam(new_binder, new_body)
                }
            }

            TermKind::Pi(binder, body) => {
                let old_ty = binder.ty;
                let new_ty = self.substitute(arena, binder.ty, idx, replacement)?;
                let new_body = self.substitute(arena, body, idx + 1, replacement)?;
                if new_ty == old_ty && new_body == body {
                    term
                } else {
                    let new_binder = crate::term::Binder { ty: new_ty, ..binder };
                    arena.mk_pi(new_binder, new_body)
                }
            }

            TermKind::Let(binder, value, body) => {
                let old_ty = binder.ty;
                let new_ty = self.substitute(arena, binder.ty, idx, replacement)?;
                let new_val = self.substitute(arena, value, idx, replacement)?;
                let new_body = self.substitute(arena, body, idx + 1, replacement)?;
                if new_ty == old_ty && new_val == value && new_body == body {
                    term
                } else {
                    let new_binder = crate::term::Binder { ty: new_ty, ..binder };
                    arena.mk_let(new_binder, new_val, new_body)
                }
            }

            // No free variables in these
            TermKind::Sort(_) | TermKind::Const(_, _) | TermKind::Lit(_) | TermKind::MVar(_) => term,
        };

        Ok(result)
    }

    /// Get conversion statistics
    pub fn stats(&self) -> &ConversionStats {
        &self.stats
    }

    /// Clear the WHNF cache
    pub fn clear_cache(&self) {
        let mut cache = self.cache.write().unwrap();
        cache.clear();
    }

    /// Reset fuel to default
    pub fn reset_fuel(&mut self) {
        self.fuel = DEFAULT_FUEL;
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::symbol::SymbolId;
    use crate::term::Binder;

    #[test]
    fn test_simple_conversion() {
        let mut arena = Arena::new();
        let env = Environment::new();
        let ctx = Context::new();
        let mut conv = Converter::new();

        let var0 = arena.mk_var(0);
        let var0_2 = arena.mk_var(0);

        assert!(conv.is_def_eq(&mut arena, &env, &ctx, var0, var0_2).unwrap());
    }

    #[test]
    fn test_beta_reduction() {
        let mut arena = Arena::new();
        let env = Environment::new();
        let ctx = Context::new();
        let mut conv = Converter::new();

        // (λx. x) y should reduce to y
        let x = arena.mk_var(0);
        let binder = Binder::new(SymbolId::new(0), TermId::new(0));
        let lam = arena.mk_lam(binder, x);
        let y = arena.mk_var(1);
        let app = arena.mk_app(lam, y);

        let result = conv.whnf(&mut arena, &env, &ctx, app).unwrap();

        // After beta reduction, should get y (but with adjusted indices)
        // This is a simplified test
        assert_ne!(result, app); // Should have reduced
    }

    #[test]
    fn test_fuel_exhaustion() {
        let mut arena = Arena::new();
        let env = Environment::new();
        let ctx = Context::new();
        let mut conv = Converter::with_fuel(1);

        let var = arena.mk_var(0);

        // This should work with minimal fuel
        assert!(conv.whnf(&mut arena, &env, &ctx, var).is_ok());
    }
}