olean-export 0.7.0

Optimized library for reading Lean .olean files directly emitting lean4export-compatible NDJSON for external kernel checkers.
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
//! What a theorem's statement rests on, printed as raw terms for a person to review.
//!
//! Notation, coercions and macros never reach an olean, so the terms print as the kernel
//! sees them, in Lean syntax but without notation. A name Lean would not read back as itself
//! goes in `«»`. Binder names are only labels, from one of the copies of a shared term, so
//! they are renamed apart from each other and from the first component of every constant,
//! and no name reads as something it is not.

use crate::{
    env::{
        escape, resolve, Binder, Env, Expr, ExprId, Hints, Kind, LetNode, Level, LevelId, Name,
        NameId, ANON,
    },
    error::Result,
    olean::{small_nat, Image},
};
use rustc_hash::{FxHashMap, FxHashSet};
use std::{
    borrow::Cow,
    collections::hash_map::Entry,
    fmt::Write as _,
    io::Write,
    mem,
    path::{Path, PathBuf},
};

/// Print the statements of `roots` and what they rest on, outside the modules `trust` names.
///
/// Every definition, inductive, opaque, axiom and theorem reached prints in load order with
/// the lines of source it came from, theorems without their proofs. A module is trusted if a
/// name in `trust` is it or a prefix of it. Every axiom is named in the header, trusted or
/// not, but trusted theorems are only counted. Each term stops after `max` characters, if
/// not 0.
pub fn report(
    env: &Env,
    search: &[PathBuf],
    roots: &[NameId],
    trust: &[String],
    max: usize,
    out: &mut dyn Write,
) -> Result<()> {
    let cone = env.statement(roots)?;
    let trusted = |m: usize| {
        let m = &env.modules[m];
        trust.iter().any(|t| {
            m == t
                || m.strip_prefix(t.as_str())
                    .is_some_and(|r| r.starts_with('.'))
        })
    };
    // constructors and recursors print with their inductive
    let listed = |&(_, c): &(usize, NameId)| {
        !roots.contains(&c) && !matches!(env.consts[&c].kind, Kind::Ctor(_) | Kind::Rec(_))
    };
    let (hidden, shown): (Vec<_>, Vec<_>) = cone
        .iter()
        .copied()
        .filter(listed)
        .partition(|&(m, _)| trusted(m));
    let named = |pick: fn(&Kind) -> bool, all: bool| {
        let (mut names, mut more) = (Vec::new(), 0);
        for &(m, c) in &cone {
            if roots.contains(&c) || !pick(&env.consts[&c].kind) {
            } else if all || !trusted(m) {
                names.push(env.display(c));
            } else {
                more += 1;
            }
        }
        if more > 0 {
            names.push(format!("{more} more in {}", trust.join(", ")));
        }
        if names.is_empty() {
            "none".into()
        } else {
            names.join(", ")
        }
    };
    let axioms = named(|k| matches!(k, Kind::Axiom), true);
    let assumed = named(|k| matches!(k, Kind::Thm(_)), false);
    let mut modules: Vec<usize> = shown.iter().map(|&(m, _)| m).collect();
    modules.dedup();
    let names: Vec<String> = roots.iter().map(|&c| env.display(c)).collect();
    let plural = |n: usize, what: &str| format!("{n} {what}{}", if n == 1 { "" } else { "s" });
    write!(
        out,
        "-- {} rests on {} in {}",
        names.join(", "),
        plural(shown.len(), "declaration"),
        plural(modules.len(), "module"),
    )?;
    if !trust.is_empty() {
        write!(out, ", and {} more in {}", hidden.len(), trust.join(", "))?;
    }
    writeln!(
        out,
        "\n-- axioms: {axioms}\n-- theorems taken as stated: {assumed}"
    )?;
    let mut p = Printer::new(env, max);
    let mut lines = FxHashMap::default();
    let module: FxHashMap<NameId, usize> = cone.iter().map(|&(m, c)| (c, m)).collect();
    let roots = roots.iter().map(|&c| (module[&c], c));
    for (m, c) in roots.chain(shown) {
        let module = &env.modules[m];
        if let Entry::Vacant(e) = lines.entry(m) {
            e.insert(ranges(&resolve(search, module)?)?);
        }
        let file = format!("{}.lean", module.replace('.', "/"));
        let at = match lines[&m].get(&env.display(c)) {
            Some((a, b)) if a == b => format!("{file}:{a}"),
            Some((a, b)) => format!("{file}:{a}-{b}"),
            None => file,
        };
        writeln!(out, "\n-- {at}\n{}", p.decl(c))?;
    }
    Ok(())
}

/// The first and last source line of each declaration of a module, from `declRangeExt`.
fn ranges(path: &Path) -> Result<FxHashMap<String, (u64, u64)>> {
    let img = Image::open_module(path)?;
    let mut out = FxHashMap::default();
    for e in img.array(img.field(img.root, 4)?)? {
        if img.str_name(img.field(e, 0)?)? != "Lean.declRangeExt" {
            continue;
        }
        for d in img.array(img.field(e, 1)?)? {
            let range = img.field(img.field(d, 1)?, 0)?;
            let line = |pos| img.field(pos, 0).and_then(small_nat);
            let span = (line(img.field(range, 0)?)?, line(img.field(range, 2)?)?);
            out.insert(img.display_name(img.field(d, 0)?)?, span);
        }
    }
    Ok(out)
}

/// Prints declarations and terms without notation, naming bound variables apart.
struct Printer<'e> {
    env: &'e Env,
    max: usize,
    out: String,
    scope: Vec<String>,
    loose: FxHashMap<ExprId, u64>,
    /// The first component of every constant's name.
    heads: FxHashSet<NameId>,
}

/// Contexts, loosest first: a binder body, an arrow's domain, an application's argument.
const TOP: u8 = 0;
const DOMAIN: u8 = 1;
const ARG: u8 = 2;

/// A step of printing a term, kept on a stack since terms can be deeper than the call stack.
enum Step {
    Expr(ExprId, u8),
    Level(LevelId, bool),
    Text(Cow<'static, str>),
    /// The rest of a run of binders, ending at `sep` with the scope back at `depth`.
    Binders {
        e: ExprId,
        lam: bool,
        sep: &'static str,
        depth: usize,
    },
    Bind(String),
    Unbind(usize),
}

const fn text(s: &'static str) -> Step {
    Step::Text(Cow::Borrowed(s))
}

/// Push `items` to print in order, in parentheses if `on`.
fn emit<const N: usize>(todo: &mut Vec<Step>, on: bool, items: [Step; N]) {
    if on {
        todo.push(text(")"));
    }
    todo.extend(items.into_iter().rev());
    if on {
        todo.push(text("("));
    }
}

impl<'e> Printer<'e> {
    fn new(env: &'e Env, max: usize) -> Self {
        let mut heads = FxHashSet::default();
        for &c in env.consts.keys() {
            let mut n = c;
            while let Name::Str(p, _) | Name::Num(p, _) = env.names[n] {
                if p == ANON {
                    break;
                }
                n = p;
            }
            heads.insert(n);
        }
        Self {
            env,
            max,
            out: String::new(),
            scope: Vec::new(),
            loose: FxHashMap::default(),
            heads,
        }
    }

    fn decl(&mut self, c: NameId) -> String {
        let env = self.env;
        let k = &env.consts[&c];
        let (word, value) = match &k.kind {
            Kind::Axiom => ("axiom", None),
            Kind::Defn(d) if d.hints == Hints::Abbrev => ("abbrev", Some(d.value)),
            Kind::Defn(d) => ("def", Some(d.value)),
            Kind::Thm(_) => ("theorem", None),
            Kind::Opaque(_) => ("opaque", None),
            Kind::Quot(_) => ("quot", None),
            Kind::Induct(_) => ("inductive", None),
            Kind::Ctor(_) => ("ctor", None),
            Kind::Rec(_) => ("recursor", None),
        };
        let mut s = format!("{word} {}", self.head(c));
        if let Some(v) = value {
            write!(s, " :=\n  {}", self.term(v)).expect("string");
        }
        if let Kind::Induct(i) = &k.kind {
            for &ctor in &i.ctors {
                write!(s, "\n| {}", self.head(ctor)).expect("string");
            }
        }
        s
    }

    fn head(&mut self, c: NameId) -> String {
        let k = &self.env.consts[&c];
        let ps: Vec<String> = k
            .level_params
            .iter()
            .map(|&n| universe(self.env, n))
            .collect();
        let us = if ps.is_empty() {
            String::new()
        } else {
            format!(".{{{}}}", ps.join(", "))
        };
        format!("{}{us} : {}", self.env.display(c), self.term(k.ty))
    }

    fn term(&mut self, e: ExprId) -> String {
        self.out.clear();
        self.scope.clear();
        let mut todo = vec![Step::Expr(e, TOP)];
        while let Some(step) = todo.pop() {
            if self.max > 0 && self.out.len() > self.max {
                break;
            }
            self.step(step, &mut todo);
        }
        let mut s = mem::take(&mut self.out);
        if self.max > 0 && s.len() > self.max {
            let mut cut = self.max;
            while !s.is_char_boundary(cut) {
                cut -= 1;
            }
            s.truncate(cut);
            s.push_str(" ...[cut]");
        }
        s
    }

    /// Print what `step` can at once, pushing the rest to `todo` in reverse.
    fn step(&mut self, step: Step, todo: &mut Vec<Step>) {
        let env = self.env;
        let depth = self.scope.len();
        match step {
            Step::Text(s) => self.out.push_str(&s),
            Step::Bind(x) => self.scope.push(x),
            Step::Unbind(d) => self.scope.truncate(d),
            Step::Level(l, arg) => self.level(l, arg, todo),
            Step::Binders { e, lam, sep, depth } => {
                let Some((n, ty, body, bi)) = self.binder(e, lam) else {
                    self.out.push_str(sep);
                    return emit(todo, false, [Step::Expr(e, TOP), Step::Unbind(depth)]);
                };
                let x = self.fresh(n);
                let (l, r) = match bi {
                    Binder::Default => ("(", ")"),
                    Binder::Implicit => ("{", "}"),
                    Binder::StrictImplicit => ("⦃", "⦄"),
                    Binder::InstImplicit => ("[", "]"),
                };
                write!(self.out, " {l}{x} : ").expect("string");
                let rest = Step::Binders {
                    e: body,
                    lam,
                    sep,
                    depth,
                };
                emit(
                    todo,
                    false,
                    [Step::Expr(ty, TOP), text(r), Step::Bind(x), rest],
                );
            }
            Step::Expr(e, prec) => match &env.exprs[e] {
                Expr::BVar(i) => {
                    match usize::try_from(*i)
                        .ok()
                        .and_then(|i| depth.checked_sub(i + 1))
                    {
                        Some(j) => self.out.push_str(&self.scope[j]),
                        None => write!(self.out, "#{i}").expect("string"),
                    }
                }
                Expr::Sort(l) => match env.levels[*l] {
                    Level::Zero => self.out.push_str("Prop"),
                    Level::Succ(u) if env.levels[u] == Level::Zero => self.out.push_str("Type"),
                    ref k => {
                        let (word, u) = match *k {
                            Level::Succ(u) => ("Type ", u),
                            _ => ("Sort ", *l),
                        };
                        emit(todo, prec >= ARG, [text(word), Step::Level(u, true)]);
                    }
                },
                Expr::Const(n, us) => {
                    self.out.push_str(&env.display(*n));
                    if let Some((first, rest)) = env.univs[*us].0.split_first() {
                        self.out.push_str(".{");
                        todo.push(text("}"));
                        for &u in rest.iter().rev() {
                            todo.extend([Step::Level(u, false), text(", ")]);
                        }
                        todo.push(Step::Level(*first, false));
                    }
                }
                Expr::App(..) => {
                    let on = prec >= ARG;
                    if on {
                        todo.push(text(")"));
                    }
                    let mut f = e;
                    while let Expr::App(g, a) = env.exprs[f] {
                        todo.extend([Step::Expr(a, ARG), text(" ")]);
                        f = g;
                    }
                    todo.push(Step::Expr(f, ARG));
                    if on {
                        todo.push(text("("));
                    }
                }
                Expr::Pi(_, ty, body, bi) if *bi == Binder::Default && self.range(*body) == 0 => {
                    let items = [
                        Step::Expr(*ty, DOMAIN),
                        text(" → "),
                        Step::Bind(String::new()),
                        Step::Expr(*body, TOP),
                        Step::Unbind(depth),
                    ];
                    emit(todo, prec > TOP, items);
                }
                Expr::Lam(..) | Expr::Pi(..) => {
                    let lam = matches!(env.exprs[e], Expr::Lam(..));
                    let (word, sep) = if lam { ("fun", " => ") } else { ("∀", ", ") };
                    emit(
                        todo,
                        prec > TOP,
                        [text(word), Step::Binders { e, lam, sep, depth }],
                    );
                }
                Expr::Let(l) => {
                    let LetNode(n, ty, v, body) = **l;
                    let (ty, v, body) = (&ty, &v, &body);
                    let x = self.fresh(n);
                    let items = [
                        Step::Text(format!("let {x} : ").into()),
                        Step::Expr(*ty, TOP),
                        text(" := "),
                        Step::Expr(*v, TOP),
                        text("; "),
                        Step::Bind(x),
                        Step::Expr(*body, TOP),
                        Step::Unbind(depth),
                    ];
                    emit(todo, prec > TOP, items);
                }
                Expr::Nat(n) => write!(self.out, "{n}").expect("string"),
                Expr::Str(s) => quote(&mut self.out, s),
                Expr::Proj(_, i, x) => {
                    todo.extend([
                        Step::Text(format!(".{}", i + 1).into()),
                        Step::Expr(*x, ARG),
                    ]);
                }
            },
        }
    }

    /// A universe, in parentheses if it is an `arg` of more than one token.
    fn level(&mut self, l: LevelId, arg: bool, todo: &mut Vec<Step>) {
        let env = self.env;
        let mut n = 0;
        let mut base = l;
        while let Level::Succ(u) = env.levels[base] {
            n += 1;
            base = u;
        }
        let plus = || Step::Text(format!(" + {n}").into());
        match env.levels[base] {
            Level::Zero => write!(self.out, "{n}").expect("string"),
            Level::Param(p) if n == 0 => self.out.push_str(&universe(env, p)),
            Level::Param(p) => emit(todo, arg, [Step::Text(universe(env, p).into()), plus()]),
            Level::Max(a, b) | Level::IMax(a, b) => {
                let word = if matches!(env.levels[base], Level::Max(..)) {
                    "max "
                } else {
                    "imax "
                };
                if n > 0 {
                    if arg {
                        todo.push(text(")"));
                    }
                    todo.push(plus());
                }
                let items = [
                    text(word),
                    Step::Level(a, true),
                    text(" "),
                    Step::Level(b, true),
                ];
                emit(todo, arg, items);
                if n > 0 && arg {
                    todo.push(text("("));
                }
            }
            Level::Succ(_) => unreachable!("peeled"),
        }
    }

    /// The binder at `e` if it continues a run of lambdas, or of dependent or non-default pis.
    fn binder(&mut self, e: ExprId, lam: bool) -> Option<(NameId, ExprId, ExprId, Binder)> {
        match self.env.exprs[e] {
            Expr::Lam(n, t, b, bi) if lam => Some((n, t, b, bi)),
            Expr::Pi(n, t, b, bi) if !lam && (bi != Binder::Default || self.range(b) > 0) => {
                Some((n, t, b, bi))
            }
            _ => None,
        }
    }

    /// One more than the largest loose bound variable of `e`, or 0 if it is closed.
    fn range(&mut self, e: ExprId) -> u64 {
        let mut todo = vec![(e, false)];
        while let Some((x, ready)) = todo.pop() {
            if self.loose.contains_key(&x) {
                continue;
            }
            let (subs, binds): (Vec<ExprId>, usize) = match self.env.exprs[x] {
                Expr::App(a, b) => (vec![a, b], 0),
                Expr::Lam(_, t, b, _) | Expr::Pi(_, t, b, _) => (vec![t, b], 1),
                Expr::Let(ref l) => (vec![l.1, l.2, l.3], 1),
                Expr::Proj(_, _, y) => (vec![y], 0),
                _ => (Vec::new(), 0),
            };
            if !ready && !subs.is_empty() {
                todo.push((x, true));
                todo.extend(subs.into_iter().map(|s| (s, false)));
                continue;
            }
            let last = subs.len().saturating_sub(binds);
            let r = match self.env.exprs[x] {
                Expr::BVar(i) => i + 1,
                _ => subs
                    .iter()
                    .enumerate()
                    .map(|(j, s)| {
                        let r = self.loose[s];
                        if j >= last {
                            r.saturating_sub(1)
                        } else {
                            r
                        }
                    })
                    .max()
                    .unwrap_or(0),
            };
            self.loose.insert(x, r);
        }
        self.loose[&e]
    }

    /// The binder's name without hygiene, as one component, distinct from every name in
    /// scope and from the first component of every constant, which it would shadow.
    fn fresh(&self, n: NameId) -> String {
        let env = self.env;
        let full = env.dotted(n);
        let base = full.split("._@").next().unwrap_or_default();
        let base = if base.is_empty() { "x" } else { base };
        let taken = |x: &str| {
            self.scope.iter().any(|s| *s == escape(x))
                || env
                    .names
                    .get(&Name::Str(ANON, x.into()))
                    .is_some_and(|h| self.heads.contains(&h))
        };
        let mut x = base.to_owned();
        let mut i = 1;
        while taken(&x) {
            x = format!("{base}_{i}");
            i += 1;
        }
        escape(&x).into_owned()
    }
}

/// A universe parameter's name, escaped also where universe syntax reads it as a word.
fn universe(env: &Env, p: NameId) -> String {
    let s = env.display(p);
    if matches!(s.as_str(), "max" | "imax") {
        format!("«{s}»")
    } else {
        s
    }
}

/// `s` as a Lean string literal.
fn quote(q: &mut String, s: &str) {
    q.push('"');
    for c in s.chars() {
        match c {
            '"' => q.push_str("\\\""),
            '\\' => q.push_str("\\\\"),
            '\n' => q.push_str("\\n"),
            '\t' => q.push_str("\\t"),
            '\r' => q.push_str("\\r"),
            c if c.is_control() => write!(q, "\\x{:02x}", u32::from(c)).expect("string"),
            c => q.push(c),
        }
    }
    q.push('"');
}