olean-export 0.5.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
use clap::{Parser, Subcommand, ValueEnum};
use indicatif::{HumanBytes, HumanCount, ProgressBar, ProgressDrawTarget, ProgressStyle};
use olean_export::{
    blean::{self, Blean},
    ndjson::{self, Ndjson},
    resolve, search_path, statement, Env, Exporter, NameId, Sink, Summary,
};
use std::{
    cell::Cell,
    cmp::Reverse,
    error::Error,
    fs::File,
    io::{self, BufWriter, Write},
    mem,
    path::PathBuf,
    process::ExitCode,
    rc::Rc,
    thread,
    time::{Duration, Instant},
};

#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

/// Read Lean 4 .olean files directly and emit the lean4export NDJSON stream.
#[derive(Debug, Parser)]
#[command(
    version,
    args_conflicts_with_subcommands = true,
    subcommand_negates_reqs = true,
    after_help = "Run under `lake env` so LEAN_PATH covers your build and the toolchain:\n  lake env olean-export Mathlib -o mathlib.ndjson"
)]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,
    /// Root modules to load, e.g. `Mathlib` or `MyProject.Main`
    #[arg(required = true, value_name = "MODULE")]
    modules: Vec<String>,
    /// Export only these constants and what they depend on (repeatable)
    #[arg(short, long = "const", value_name = "NAME")]
    consts: Vec<String>,
    /// Write to FILE instead of stdout
    #[arg(short, long, value_name = "FILE")]
    output: Option<PathBuf>,
    /// Search DIR before `LEAN_PATH` (repeatable)
    #[arg(short = 'L', long = "search", value_name = "DIR", global = true)]
    search: Vec<PathBuf>,
    /// Decode modules on N threads [default: all cores]
    #[arg(short, long, value_name = "N", global = true)]
    jobs: Option<usize>,
    /// Output format
    #[arg(short, long, value_enum, default_value_t = Format::Ndjson)]
    format: Format,
    /// No progress bars or summary
    #[arg(short, long)]
    quiet: bool,
    /// Show peak resident memory in the progress bars and summary
    #[arg(long)]
    rss: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
enum Format {
    /// lean4export 3.1.0 NDJSON
    Ndjson,
    /// The same records in binary, see docs/BLEAN.md
    Blean,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
enum Shape {
    /// Each declaration as a raw term, with its source lines
    Text,
    Ndjson,
    Blean,
}

#[derive(Debug, Subcommand)]
enum Command {
    /// Summarize one olean: header, imports, constants and where its bytes go
    Inspect {
        /// A module name such as `Init.Prelude`, or a path to an .olean
        #[arg(value_name = "MODULE|FILE")]
        target: String,
        /// List every constant with its kind
        #[arg(short, long)]
        consts: bool,
    },
    /// Convert an export between NDJSON and blean, whichever the input is not
    Convert {
        /// An NDJSON or blean export
        input: PathBuf,
        /// Write to FILE instead of stdout
        #[arg(short, long, value_name = "FILE")]
        output: Option<PathBuf>,
    },
    /// Print what theorems' statements rest on, every proof left out, for review
    Statement {
        /// Root modules to load
        #[arg(required = true, value_name = "MODULE")]
        modules: Vec<String>,
        /// A theorem whose statement to follow (repeatable)
        #[arg(short, long = "const", value_name = "NAME", required = true)]
        consts: Vec<String>,
        /// Leave out what modules named or prefixed by these define, e.g. `Init,Std,Mathlib`
        #[arg(short, long, value_delimiter = ',', value_name = "MODULES")]
        trust: Vec<String>,
        /// Raw terms to read, or the cone itself with theorems as axioms
        #[arg(short, long, value_enum, default_value_t = Shape::Text)]
        format: Shape,
        /// Cut each term after N characters, 0 for no limit
        #[arg(short, long, value_name = "N", default_value_t = 2000)]
        max: usize,
        /// Write to FILE instead of stdout
        #[arg(short, long, value_name = "FILE")]
        output: Option<PathBuf>,
    },
    /// Compare two exports, NDJSON or blean, up to id numbering and record order
    Compare {
        a: PathBuf,
        b: PathBuf,
        /// Show at most N names per list
        #[arg(short = 'n', long, value_name = "N", default_value_t = 20)]
        limit: usize,
    },
}

struct Counting<W> {
    inner: W,
    bytes: Rc<Cell<u64>>,
}

impl<W: Write> Write for Counting<W> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let n = self.inner.write(buf)?;
        self.bytes.set(self.bytes.get() + n as u64);
        Ok(n)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }
}

fn main() -> ExitCode {
    let cli = Cli::parse();
    let result = match &cli.command {
        Some(Command::Inspect { target, consts }) => inspect(&cli, target, *consts),
        Some(Command::Convert { input, output }) => convert(input, output.as_ref()),
        Some(Command::Compare { a, b, limit }) => compare(a, b, *limit),
        Some(Command::Statement {
            modules,
            consts,
            trust,
            format,
            max,
            output,
        }) => statement(&cli, modules, consts, trust, *format, *max, output.as_ref()),
        None => run(&cli),
    };
    match result {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) if broken_pipe(&*e) => ExitCode::SUCCESS,
        Err(e) => {
            eprintln!("error: {e}");
            ExitCode::FAILURE
        }
    }
}

fn broken_pipe(e: &(dyn Error + 'static)) -> bool {
    let io = e
        .downcast_ref::<io::Error>()
        .or_else(|| match e.downcast_ref() {
            Some(olean_export::Error::Io(io)) => Some(io),
            _ => None,
        });
    io.is_some_and(|io| io.kind() == io::ErrorKind::BrokenPipe)
}

/// Peak resident set so far, if the platform reports it.
#[cfg(unix)]
#[allow(unsafe_code)]
fn peak_rss() -> Option<u64> {
    // SAFETY: rusage is plain integers, for which zero is valid, and getrusage only writes it.
    let mut u: libc::rusage = unsafe { mem::zeroed() };
    if unsafe { libc::getrusage(libc::RUSAGE_SELF, &raw mut u) } != 0 {
        return None;
    }
    let n = u64::try_from(u.ru_maxrss).ok()?;
    // bytes on macOS, KiB elsewhere
    Some(if cfg!(target_os = "macos") {
        n
    } else {
        n << 10
    })
}

#[cfg(not(unix))]
const fn peak_rss() -> Option<u64> {
    None
}

fn rss(on: bool) -> String {
    match peak_rss() {
        Some(n) if on => format!(", peak {}", HumanBytes(n)),
        _ => String::new(),
    }
}

fn bar(quiet: bool, len: Option<u64>, template: &str) -> ProgressBar {
    let pb = len.map_or_else(ProgressBar::new_spinner, ProgressBar::new);
    if quiet {
        pb.set_draw_target(ProgressDrawTarget::hidden());
    }
    pb.set_style(
        ProgressStyle::with_template(template)
            .expect("valid template")
            .progress_chars("=> "),
    );
    pb.enable_steady_tick(Duration::from_millis(100));
    pb
}

type Res = Result<(), Box<dyn Error + Send + Sync>>;

fn search(cli: &Cli) -> Result<Vec<PathBuf>, Box<dyn Error + Send + Sync>> {
    let dirs: Vec<PathBuf> = cli.search.iter().cloned().chain(search_path()).collect();
    if dirs.is_empty() {
        return Err("no search path: run under `lake env` or pass -L DIR".into());
    }
    Ok(dirs)
}

fn inspect(cli: &Cli, target: &str, list: bool) -> Res {
    let path = PathBuf::from(target);
    let path = if path.is_file() {
        path
    } else {
        resolve(&search(cli)?, target)?
    };
    let s = Summary::open(&path)?;
    let flag = |on: bool, s: &'static str| if on { s } else { "" };
    let mut out = io::stdout().lock();
    writeln!(
        out,
        "Lean {} ({}){}{}",
        s.header.version,
        s.header.githash,
        flag(s.header.gmp, ", gmp"),
        flag(s.is_module, ", module")
    )?;
    for (p, n) in &s.parts {
        writeln!(out, "  {:>11}  {}", HumanBytes(*n).to_string(), p.display())?;
    }
    writeln!(out, "\nimports ({})", s.imports.len())?;
    for i in &s.imports {
        writeln!(
            out,
            "  {}{}{}{}",
            i.module,
            flag(i.all, " all"),
            flag(!i.exported, " private"),
            flag(i.meta, " meta")
        )?;
    }
    let mut kinds: Vec<(&str, usize)> = Vec::new();
    for d in &s.consts {
        match kinds.iter_mut().find(|k| k.0 == d.kind) {
            Some(k) => k.1 += 1,
            None => kinds.push((d.kind, 1)),
        }
    }
    kinds.sort_by_key(|k| Reverse(k.1));
    let kinds: Vec<String> = kinds.iter().map(|(k, n)| format!("{n} {k}")).collect();
    writeln!(
        out,
        "\nconstants ({}): {}",
        s.consts.len(),
        kinds.join(", ")
    )?;
    if list {
        let mut consts: Vec<_> = s.consts.iter().collect();
        consts.sort_by(|a, b| a.name.cmp(&b.name));
        for d in consts {
            let line = format!("  {:<9} {} {}", d.kind, d.name, d.safety);
            writeln!(out, "{}", line.trim_end())?;
        }
    }
    let mut sections: Vec<_> = s.sections.iter().filter(|x| x.items > 0).collect();
    sections.sort_by_key(|x| Reverse(x.bytes));
    let total = s.bytes().max(1);
    let row = |out: &mut io::StdoutLock<'_>, bytes: u64, items: String, name: &str| {
        #[allow(clippy::cast_precision_loss)]
        let share = 100.0 * bytes as f64 / total as f64;
        let size = HumanBytes(bytes).to_string();
        writeln!(out, "{size:>11}  {share:>5.1}%  {items:>6}  {name}")
    };
    writeln!(
        out,
        "\n{:>11}  {:>6}  {:>6}  section",
        "bytes", "share", "items"
    )?;
    for x in &sections {
        row(&mut out, x.bytes, x.items.to_string(), &x.name)?;
    }
    let reached: u64 = s.sections.iter().map(|x| x.bytes).sum();
    row(
        &mut out,
        total.saturating_sub(reached),
        String::new(),
        "(headers and unreached)",
    )?;
    Ok(())
}

fn run(cli: &Cli) -> Res {
    let start = Instant::now();
    let search = search(cli)?;
    let roots: Vec<&str> = cli.modules.iter().map(String::as_str).collect();

    let pb = bar(
        cli.quiet,
        None,
        "{spinner:.cyan} decoding [{elapsed}] {msg}",
    );
    let jobs = jobs(cli);
    pb.set_message(format!("finding modules, {jobs} threads"));
    let tick = |modules: usize, consts: usize| {
        pb.set_message(format!(
            "{} modules, {} constants{}",
            HumanCount(modules as u64),
            HumanCount(consts as u64),
            rss(cli.rss)
        ));
    };
    let env = if cli.consts.is_empty() {
        Env::load_with(&search, &roots, jobs, &tick)?
    } else {
        let consts: Vec<&str> = cli.consts.iter().map(String::as_str).collect();
        Env::load_cone(&search, &roots, &consts, jobs, &tick)?
    };
    pb.finish_and_clear();
    let loaded = start.elapsed();

    let targets = if cli.consts.is_empty() {
        env.roots().collect()
    } else {
        cli.consts
            .iter()
            .map(|n| {
                env.find_name(n)
                    .ok_or_else(|| format!("unknown constant {n}"))
            })
            .collect::<Result<Vec<_>, _>>()?
    };

    let sink: Box<dyn Write> = match &cli.output {
        Some(path) => Box::new(File::create(path)?),
        None => Box::new(io::stdout().lock()),
    };
    let bytes = Rc::new(Cell::new(0));
    let out = BufWriter::with_capacity(
        1 << 20,
        Counting {
            inner: sink,
            bytes: Rc::clone(&bytes),
        },
    );
    let pb = bar(
        cli.quiet,
        Some(targets.len() as u64),
        "{spinner:.cyan} emitting [{elapsed}] [{bar:30.cyan/blue}] {human_pos}/{human_len} {msg}",
    );
    let tick = |i: usize| {
        pb.set_position(i as u64);
        pb.set_message(format!("{}{}", HumanBytes(bytes.get()), rss(cli.rss)));
    };
    let counts = match cli.format {
        Format::Ndjson => export(Exporter::new(&env, out), &targets, tick)?,
        Format::Blean => export(Exporter::with_sink(&env, Blean::new(out)), &targets, tick)?,
    };
    pb.finish_and_clear();

    if !cli.quiet {
        let ms = start.elapsed().saturating_sub(loaded).as_millis().max(1);
        let rate = u64::try_from(u128::from(bytes.get()) * 1000 / ms).unwrap_or(u64::MAX);
        eprintln!(
            "{} modules, {} constants decoded in {:.2}s\n{} of {} ({} names, {} levels, {} exprs) in {:.2}s total, {}/s{}",
            HumanCount(env.modules.len() as u64),
            HumanCount(env.consts.len() as u64),
            loaded.as_secs_f64(),
            HumanBytes(bytes.get()),
            match cli.format {
                Format::Ndjson => "NDJSON",
                Format::Blean => "blean",
            },
            HumanCount(counts.names.into()),
            HumanCount(counts.levels.into()),
            HumanCount(counts.exprs.into()),
            start.elapsed().as_secs_f64(),
            HumanBytes(rate),
            rss(cli.rss),
        );
    }
    Ok(())
}

fn export<S: Sink>(
    mut ex: Exporter<'_, S>,
    targets: &[NameId],
    tick: impl Fn(usize),
) -> Result<olean_export::Counts, Box<dyn Error + Send + Sync>> {
    ex.meta()?;
    for (i, &c) in targets.iter().enumerate() {
        ex.constant(c)?;
        if i % 256 == 0 {
            tick(i);
        }
    }
    let counts = ex.counts();
    ex.finish()?;
    Ok(counts)
}

fn jobs(cli: &Cli) -> usize {
    cli.jobs
        .unwrap_or_else(|| thread::available_parallelism().map_or(1, usize::from))
}

fn statement(
    cli: &Cli,
    modules: &[String],
    consts: &[String],
    trust: &[String],
    format: Shape,
    max: usize,
    output: Option<&PathBuf>,
) -> Res {
    let search = search(cli)?;
    let roots: Vec<&str> = modules.iter().map(String::as_str).collect();
    let env = Env::load_statements(&search, &roots, jobs(cli), &|_, _| {})?;
    let targets = consts
        .iter()
        .map(|n| {
            env.find_name(n)
                .ok_or_else(|| format!("unknown constant {n}"))
        })
        .collect::<Result<Vec<_>, _>>()?;
    let sink: Box<dyn Write + Send> = match output {
        Some(path) => Box::new(File::create(path)?),
        None => Box::new(io::stdout()),
    };
    let mut out = BufWriter::with_capacity(1 << 20, sink);
    match format {
        Shape::Text => statement::report(&env, &search, &targets, trust, max, &mut out)?,
        // exactly what the text report lists, which already includes everything it rests on
        Shape::Ndjson | Shape::Blean => {
            let cone: Vec<NameId> = env
                .statement(&targets)?
                .into_iter()
                .map(|(_, c)| c)
                .collect();
            if format == Shape::Ndjson {
                export(
                    Exporter::new(&env, out.by_ref()).statements(),
                    &cone,
                    |_| {},
                )?;
            } else {
                let sink = Blean::new(out.by_ref());
                export(Exporter::with_sink(&env, sink).statements(), &cone, |_| {})?;
            }
        }
    }
    out.flush()?;
    Ok(())
}

fn mmap(path: &PathBuf) -> io::Result<memmap2::Mmap> {
    let file = File::open(path)?;
    // SAFETY: the export is only read, and is not expected to change while it is.
    #[allow(unsafe_code)]
    unsafe {
        memmap2::Mmap::map(&file)
    }
}

fn compare(a: &PathBuf, b: &PathBuf, limit: usize) -> Res {
    let d = olean_export::compare(&mmap(a)?, &mmap(b)?)?;
    let mut out = io::stdout().lock();
    let mut list = |what: &str, names: &[String]| -> io::Result<()> {
        for n in names.iter().take(limit) {
            writeln!(out, "{what}: {n}")?;
        }
        if names.len() > limit {
            writeln!(out, "{what}: ... {} more", names.len() - limit)?;
        }
        Ok(())
    };
    list(&format!("only in {}", a.display()), &d.only_a)?;
    list(&format!("only in {}", b.display()), &d.only_b)?;
    let differ: Vec<String> = d.differ.iter().map(|(n, w)| format!("{n} ({w})")).collect();
    list("differs", &differ)?;
    list(&format!("duplicate in {}", a.display()), &d.dups.0)?;
    list(&format!("duplicate in {}", b.display()), &d.dups.1)?;
    writeln!(
        out,
        "{} / {} declarations: {} only in the first, {} only in the second, {} differ, {} duplicate",
        d.decls.0,
        d.decls.1,
        d.only_a.len(),
        d.only_b.len(),
        d.differ.len(),
        d.dups.0.len() + d.dups.1.len()
    )?;
    if d.is_empty() {
        Ok(())
    } else {
        Err("the exports differ".into())
    }
}

fn convert(input: &PathBuf, output: Option<&PathBuf>) -> Res {
    let map = mmap(input)?;
    let sink: Box<dyn Write> = match output {
        Some(path) => Box::new(File::create(path)?),
        None => Box::new(io::stdout().lock()),
    };
    let out = BufWriter::with_capacity(1 << 20, sink);
    if blean::sniff(&map) {
        blean::read(&map, Ndjson::new(out))?;
    } else {
        ndjson::read(&map[..], Blean::new(out))?;
    }
    Ok(())
}