cargo-leanfran 0.6.0

cargo leanfran: extract Lean models of Rust functions with Charon and Aeneas, state their obligations and check their proofs
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! `cargo leanfran extract`.

use crate::{check_package, run, tool, Crate, Resolved, Specs, STUB};
use leanfran_build::gen::{self, AeneasDecl, Spec, LEAN_HEADER};
use leanfran_build::lock::{Lock, LockItem, Model, Obligation, LOCK_VERSION};
use leanfran_build::model::{CrateItems, ModelFn};
use leanfran_build::{AENEAS_REV, CHARON_VERSION};
use sha2::{Digest, Sha256};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Write;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

fn charon_path(crate_ident: &str, segs: &[String]) -> String {
    let mut s = crate_ident.to_string();
    for p in segs {
        s.push_str("::");
        s.push_str(p);
    }
    s
}

/// The absolute module path of a type or trait path written in `module`.
fn resolve(items: &CrateItems, module: &[String], segs: &[String]) -> Vec<String> {
    items.resolve_segments(module, segs, 0)
}

/// The Charon pattern of a modelled function (`--start-from`).
fn start_from(crate_ident: &str, items: &CrateItems, f: &ModelFn) -> String {
    let mut fn_path = f.module.clone();
    match (&f.self_path, &f.trait_path) {
        (Some(t), Some(tr)) => {
            let t = charon_path(crate_ident, &resolve(items, &f.module, t));
            let tr = charon_path(crate_ident, &resolve(items, &f.module, tr));
            format!("{{impl {tr} for {t}}}::{}", f.name)
        }
        (Some(t), None) => {
            let mut p = resolve(items, &f.module, t);
            p.push(f.name.clone());
            charon_path(crate_ident, &p)
        }
        _ => {
            fn_path.push(f.name.clone());
            charon_path(crate_ident, &fn_path)
        }
    }
}

/// The Rust path of the type and the trait of a method, from the generated code.
fn rust_paths(items: &CrateItems, f: &ModelFn) -> (Option<String>, Option<String>) {
    let p = |segs: &Option<Vec<String>>| {
        segs.as_ref()
            .map(|s| leanfran_build::module_path_string(&resolve(items, &f.module, s)))
    };
    (p(&f.self_path), p(&f.trait_path))
}

/// The `rs:` extern of `f`, with the Rust types `types` of the type parameters of a generic
/// function.
pub fn rust_extern(items: &CrateItems, f: &ModelFn, types: &[String]) -> String {
    let (s, t) = rust_paths(items, f);
    gen::rust_extern(f, s.as_deref(), t.as_deref(), types)
}

fn hash(text: &str) -> String {
    let d = Sha256::digest(text.as_bytes());
    let hex: String = d.iter().map(|b| format!("{b:02x}")).collect();
    format!("sha256:{hex}")
}

fn write(path: &Path, text: &str, written: &mut BTreeSet<PathBuf>) -> Result<(), String> {
    if let Some(d) = path.parent() {
        fs::create_dir_all(d).map_err(|e| e.to_string())?;
    }
    if fs::read_to_string(path).ok().as_deref() != Some(text) {
        fs::write(path, text).map_err(|e| format!("{}: {e}", path.display()))?;
    }
    written.insert(path.to_path_buf());
    Ok(())
}

/// Removes the files below `dir` not in `live` and the empty directories.
pub fn prune(dir: &Path, live: &BTreeSet<PathBuf>) -> Result<(), String> {
    let Ok(entries) = fs::read_dir(dir) else {
        return Ok(());
    };
    for e in entries.flatten() {
        let p = e.path();
        if p.is_dir() {
            prune(&p, live)?;
            if fs::read_dir(&p)
                .map_err(|e| e.to_string())?
                .next()
                .is_none()
            {
                fs::remove_dir(&p).map_err(|e| e.to_string())?;
            }
        } else if !live.contains(&p) {
            fs::remove_file(&p).map_err(|e| e.to_string())?;
        }
    }
    Ok(())
}

/// A modelled function with its resolved `requires` and obligation specs.
pub type ResolvedFn<'a> = (&'a ModelFn, Option<Resolved>, Vec<(String, Resolved)>);

pub fn resolve_all<'a>(
    c: &Crate,
    items: &'a CrateItems,
    specs: &Specs,
) -> Result<Vec<ResolvedFn<'a>>, String> {
    let mut out = Vec::new();
    let mut errors = Vec::new();
    for f in &items.models {
        let requires = match &f.attr.requires {
            Some(r) => match specs.resolve(c, items, f, r) {
                Ok(r) => Some(r),
                Err(e) => {
                    errors.push(e);
                    None
                }
            },
            None => None,
        };
        let mut obls = Vec::new();
        for (label, spec) in f.attr.obligations() {
            match specs.resolve(c, items, f, spec) {
                Ok(r) => obls.push((label, r)),
                Err(e) => errors.push(e),
            }
        }
        out.push((f, requires, obls));
    }
    if errors.is_empty() {
        Ok(out)
    } else {
        Err(errors.join("\n"))
    }
}

/// The spec in an obligation: a generated function by its name from the root, which the
/// namespaces the obligation module opens cannot capture.
fn to_spec(r: &Resolved) -> Spec {
    Spec {
        lean: if r.boolean {
            format!("_root_.{}", r.lean)
        } else {
            r.lean.clone()
        },
        boolean: r.boolean,
        named: Vec::new(),
    }
}

/// One obligation of a label: for a generic function, the one for every type (`generic`) or
/// the one of a type of `instances` (`per_instance`, with the suffix `.<type>`).
pub struct Case {
    pub suffix: String,
    pub parts: gen::GenericParts,
    pub spec: Spec,
    pub requires: Option<Spec>,
}

/// The spec `r` for the Rust types `types` of a polymorphic spec: its instantiation.
pub fn instance_spec(r: &Resolved, types: &[String], at: &str) -> Result<Spec, String> {
    let mut s = to_spec(r);
    if r.instances.is_empty() || !r.boolean {
        return Ok(s);
    }
    let key = if types.len() == 1 {
        types[0].clone()
    } else {
        format!("({})", types.join(","))
    };
    let key = leanfran_build::model::normalize_type(&key);
    match r
        .instances
        .iter()
        .find(|(t, _)| leanfran_build::model::normalize_type(t) == key)
    {
        Some((_, lean)) => {
            s.lean = format!("_root_.{lean}");
            Ok(s)
        }
        None => Err(format!(
            "{at}: the spec {} is not instantiated for `{key}`: add it to its `rust_instances`",
            r.lean
        )),
    }
}

/// The polymorphic spec `r` applied to the type parameters `params` of the model.
fn generic_spec(r: &Resolved, params: &[String]) -> Spec {
    let mut s = to_spec(r);
    if r.boolean && !r.type_params.is_empty() && r.type_params.len() == params.len() {
        s.named = r
            .type_params
            .iter()
            .zip(params)
            .map(|(a, t)| format!("({a} := {t})"))
            .collect();
    }
    s
}

/// The obligations of `f` for the spec `r`.
pub fn cases(
    f: &ModelFn,
    binders: &[gen::Binder],
    requires: Option<&Resolved>,
    r: &Resolved,
    instances_of_crate: &dyn Fn(&str, &str) -> Option<String>,
) -> Result<Vec<Case>, String> {
    let at = format!("{}:{}", f.file, f.line);
    if f.sig.generics.is_empty() {
        return Ok(vec![Case {
            suffix: String::new(),
            parts: gen::GenericParts::default(),
            spec: to_spec(r),
            requires: requires.map(to_spec),
        }]);
    }
    let params: Vec<String> = binders
        .iter()
        .filter(|b| b.implicit && b.ty.starts_with("Type"))
        .map(|b| b.name.clone())
        .collect();
    if params.len() != f.sig.generics.len() {
        return Err(format!(
            "{at}: the model of {} has {} type parameters, the Rust function {}",
            f.rust,
            params.len(),
            f.sig.generics.len()
        ));
    }
    if !f.attr.per_instance() {
        let parts = gen::generic_parts(binders, &gen::lean_params(f))
            .map_err(|e| format!("{at}: {}: {e}", f.rust))?;
        return Ok(vec![Case {
            suffix: String::new(),
            parts,
            spec: generic_spec(r, &params),
            requires: requires.map(|q| generic_spec(q, &params)),
        }]);
    }
    let mut out = Vec::new();
    for inst in &f.attr.instances {
        let types = instance_types(f, inst);
        if types.len() != params.len() {
            return Err(format!(
                "{at}: an instance of {} needs {} types",
                f.rust,
                params.len()
            ));
        }
        let mut insts = Vec::new();
        let values = gen::lean_params(f);
        for b in binders
            .iter()
            .filter(|b| !b.implicit && !values.contains(&b.name))
        {
            let mut words = b.ty.split_whitespace();
            let (Some(head), Some(first)) = (words.next(), words.next()) else {
                continue;
            };
            let Some(i) = params.iter().position(|p| p == first) else {
                continue;
            };
            let rust = &types[i];
            let name = gen::library_instance(head, rust)
                .or_else(|| instances_of_crate(head, rust))
                .ok_or_else(|| {
                    format!(
                        "{at}: no instance of `{head}` for `{rust}` in Aeneas's library or the crate's model"
                    )
                })?;
            insts.push(name);
        }
        out.push(Case {
            suffix: format!(".{}", leanfran_build::model::instance_name(inst)),
            parts: gen::GenericParts {
                binders: String::new(),
                insts,
            },
            spec: instance_spec(r, &types, &at)?,
            requires: requires
                .map(|q| instance_spec(q, &types, &at))
                .transpose()?,
        });
    }
    Ok(out)
}

fn module_of(prefix: &str, ident: &str, module: &[String]) -> String {
    gen::module_of(prefix, ident, module)
}

pub fn extract(c: &Crate) -> Result<(), String> {
    check_package(c)?;
    let b = c.builder();
    let items = b.scan_items().map_err(|e| e.to_string())?;
    if !items.errors.is_empty() {
        return Err(items.errors.join("\n"));
    }
    let ident = c.ident();
    let lean_dir = c.lean_dir();
    if items.models.is_empty() && items.opaque.is_empty() {
        return Err("the crate has no #[leanfran::model] or #[leanfran::opaque] function".into());
    }
    let untyped: Vec<String> = items
        .models
        .iter()
        .filter(|f| !f.sig.generics.is_empty() && f.attr.instances.is_empty())
        .map(|f| {
            format!(
                "{}:{}: {} is generic: give the types of its tests with `instances = [..]`",
                f.file, f.line, f.rust
            )
        })
        .collect();
    if !untyped.is_empty() {
        return Err(untyped.join("\n"));
    }

    let llbc_dir = c.target_dir.join("leanfran");
    fs::create_dir_all(&llbc_dir).map_err(|e| e.to_string())?;
    let llbc = llbc_dir.join(format!("{ident}.llbc"));
    let mut charon = Command::new(tool("CHARON", "charon"));
    charon
        .args(["cargo", "--preset=aeneas"])
        .arg(format!("--dest-file={}", llbc.display()));
    for f in &items.models {
        charon
            .arg("--start-from")
            .arg(start_from(&ident, &items, f));
    }
    for o in &items.opaque {
        let (start, opaque) = match &o.self_path {
            Some(t) => {
                let mut segs = resolve(&items, &o.module, t);
                segs.push(o.name.clone());
                let start = charon_path(&ident, &segs);
                let mut glob = o.module.clone();
                glob.extend(["_".to_string(), o.name.clone()]);
                let module_prefix = std::iter::once("crate")
                    .chain(o.module.iter().map(String::as_str))
                    .map(|s| format!("{s}::"))
                    .collect::<String>();
                let clashes: Vec<&String> = items
                    .hashes
                    .keys()
                    .filter(|u| *u != &o.unit)
                    .filter(|u| {
                        u.strip_prefix(&module_prefix).is_some_and(|rest| {
                            rest.split_once("::")
                                .is_some_and(|(owner, name)| !owner.is_empty() && name == o.name)
                        })
                    })
                    .collect();
                if !clashes.is_empty() {
                    return Err(format!(
                        "{}: #[leanfran::opaque] method {}: Charon matches the method of an impl block by its module and name only, and {} has the same: rename one",
                        o.file,
                        o.unit,
                        clashes.iter().map(|c| c.as_str()).collect::<Vec<_>>().join(", ")
                    ));
                }
                (start, charon_path(&ident, &glob))
            }
            None => {
                let mut segs = o.module.clone();
                segs.push(o.name.clone());
                let p = charon_path(&ident, &segs);
                (p.clone(), p)
            }
        };
        charon.arg("--start-from").arg(start);
        charon.arg("--opaque").arg(opaque);
    }
    charon.env("LEANFRAN_EXTRACTING", "1").current_dir(&c.dir);
    println!(
        "charon cargo --preset=aeneas ({} functions, {} opaque)",
        items.models.len(),
        items.opaque.len()
    );
    run(&mut charon, "charon")?;
    let charon_version = run(
        Command::new(tool("CHARON", "charon")).arg("version"),
        "charon version",
    )?;
    let charon_version = charon_version
        .split_whitespace()
        .next()
        .unwrap_or("")
        .to_string();
    if charon_version != CHARON_VERSION {
        return Err(format!(
            "charon is {charon_version}; this leanfran needs the Charon {CHARON_VERSION} of Aeneas {AENEAS_REV}"
        ));
    }

    let sub = format!("LeanRustGen/Model/{ident}");
    let model_dir = lean_dir.join(&sub);
    if model_dir.exists() {
        fs::remove_dir_all(&model_dir).map_err(|e| e.to_string())?;
    }
    println!("aeneas -backend lean");
    let out = run(
        Command::new(tool("AENEAS", "aeneas"))
            .args(["-backend", "lean"])
            .arg(&llbc)
            .arg("-dest")
            .arg(&lean_dir)
            .args([
                "-subdir",
                &sub,
                "-split-files",
                "-namespace",
                &ident,
                "-loops-to-rec",
            ]),
        "aeneas",
    )?;
    if out.contains("[Error]") {
        return Err(format!("aeneas reported errors:\n{out}"));
    }
    for (template, shim, user, import) in [
        (
            "FunsExternal_Template.lean",
            "FunsExternal.lean",
            "External.lean",
            "External",
        ),
        (
            "TypesExternal_Template.lean",
            "TypesExternal.lean",
            "ExternalTypes.lean",
            "ExternalTypes",
        ),
    ] {
        let t = model_dir.join(template);
        if !t.is_file() {
            continue;
        }
        fs::write(
            model_dir.join(shim),
            format!("{LEAN_HEADER}module\npublic import {import}\n"),
        )
        .map_err(|e| e.to_string())?;
        let u = lean_dir.join(user);
        let current = fs::read_to_string(&u).unwrap_or_default();
        if current.is_empty() || current == STUB {
            fs::copy(&t, &u).map_err(|e| e.to_string())?;
            println!("wrote lean/{user} from {sub}/{template}: replace its axioms by definitions");
        } else {
            println!("note: {sub}/{template} lists the declarations lean/{user} must define");
        }
    }

    let read = |f: &str| fs::read_to_string(model_dir.join(f)).unwrap_or_default();
    let mut decls: Vec<AeneasDecl> = gen::parse_aeneas(&read("Funs.lean"));
    decls.extend(gen::parse_aeneas(&read("Types.lean")));
    let externals: Vec<AeneasDecl> = gen::parse_aeneas(&read("FunsExternal_Template.lean"));

    let mut errors = Vec::new();
    let mut aliases: BTreeMap<Vec<String>, String> = BTreeMap::new();
    let mut externs: BTreeMap<Vec<String>, String> = BTreeMap::new();
    let mut models: BTreeMap<String, Model> = BTreeMap::new();
    let mut binders: BTreeMap<String, Vec<gen::Binder>> = BTreeMap::new();
    for f in &items.models {
        let unit_of = |d: &AeneasDecl| d.rust.as_deref().and_then(|r| gen::aeneas_unit(r, &ident));
        let decl = decls
            .iter()
            .find(|d| d.loop_index.is_none() && unit_of(d).as_deref() == Some(f.rust.as_str()));
        let Some(decl) = decl else {
            let opaque = externals
                .iter()
                .any(|d| unit_of(d).as_deref() == Some(f.rust.as_str()));
            errors.push(if opaque {
                format!("{}:{}: {} is modelled and opaque", f.file, f.line, f.rust)
            } else {
                format!(
                    "{}:{}: Aeneas did not translate {} (see the output of charon and aeneas)",
                    f.file, f.line, f.rust
                )
            });
            continue;
        };
        let lean = format!("{ident}.{}", decl.lean);
        match gen::loop_aliases(f, &decls, &decl.lean) {
            Ok(list) => {
                let text = aliases.entry(f.module.clone()).or_default();
                for (labels, target) in list {
                    let labels: Vec<String> = labels.iter().map(|l| gen::lean_ident(l)).collect();
                    let _ = writeln!(
                        text,
                        "@[reducible] def {lean}.{} := @{ident}.{target}",
                        labels.join(".")
                    );
                }
            }
            Err(e) => errors.push(e),
        }
        let mut model = Model {
            rust: f.rust.clone(),
            lean: lean.clone(),
            instances: f
                .attr
                .instances
                .iter()
                .map(leanfran_build::model::type_text)
                .collect(),
            items: Vec::new(),
            obligation: Vec::new(),
        };
        let mut units = gen::reachable_units(&decls, &decl.lean, &ident);
        units.insert(f.rust.clone());
        let mut seen = BTreeSet::new();
        for u in units {
            let unit = if items.hashes.contains_key(&u) {
                u
            } else if let Some((module, rest)) = u.split_once("::<") {
                let ty = rest.split(" as ").next().unwrap_or("");
                format!("{module}::{ty}")
            } else {
                continue;
            };
            if let Some(h) = items.hashes.get(&unit) {
                if seen.insert(unit.clone()) {
                    model.items.push(LockItem {
                        rust: unit,
                        hash: h.clone(),
                    });
                }
            }
        }
        if f.sig.generics.is_empty() {
            let ext = externs.entry(f.module.clone()).or_default();
            let _ = writeln!(
                ext,
                "@[extern \"{}\"]\ndef {lean}.rs : type_of% @{lean} := @{lean}\n",
                rust_extern(&items, f, &[])
            );
        }
        binders.insert(f.rust.clone(), gen::decl_binders(&decl.body));
        models.insert(f.rust.clone(), model);
    }
    if !errors.is_empty() {
        return Err(errors.join("\n"));
    }

    let mut written = BTreeSet::new();
    let funs = gen::model_module(&ident);
    let open = "open Aeneas Aeneas.Std Result\n";
    for (module, text) in &aliases {
        if text.is_empty() {
            continue;
        }
        let m = module_of("Alias", &ident, module);
        write(
            &lean_dir.join(gen::module_path(&m)),
            &format!("{LEAN_HEADER}import {funs}\n{open}\n{text}"),
            &mut written,
        )?;
    }
    for (module, text) in &externs {
        let m = module_of("Extern", &ident, module);
        write(
            &lean_dir.join(gen::module_path(&m)),
            &format!("{LEAN_HEADER}import {funs}\n{open}\n{text}"),
            &mut written,
        )?;
    }
    for d in ["Alias", "Extern"] {
        prune(&lean_dir.join("LeanRustGen").join(d).join(&ident), &written)?;
    }

    let (blocks, _) = b.scan().map_err(|e| e.to_string())?;
    b.generate_cache(&blocks, |_| true)
        .map_err(|e| e.to_string())?;
    let specs = Specs::new(c)?;
    let resolved = resolve_all(c, &items, &specs)?;
    let all_blocks: Vec<String> = specs
        .blocks
        .iter()
        .filter(|b| {
            !b.imports
                .iter()
                .any(|i| i.starts_with("LeanRustGen.Obligation") || i.starts_with("Proofs"))
        })
        .map(|b| b.lean_module(&ident))
        .collect();
    let block_namespaces: Vec<String> = specs
        .blocks
        .iter()
        .filter(|b| all_blocks.contains(&b.lean_module(&ident)))
        .map(|b| b.name.clone())
        .collect();

    let old = Lock::read(&b.lock_path())?;
    let mut lock = Lock {
        version: LOCK_VERSION,
        aeneas: AENEAS_REV.to_string(),
        charon: charon_version,
        model: Vec::new(),
    };
    let funs_text = read("Funs.lean");
    let crate_instance = |head: &str, rust: &str| {
        gen::crate_instance(&funs_text, head, rust).map(|n| format!("{ident}.{n}"))
    };
    let mut obligations: BTreeMap<Vec<String>, (BTreeSet<String>, String)> = BTreeMap::new();
    let mut uses_laws = false;
    for (f, requires, obls) in &resolved {
        let Some(mut model) = models.remove(&f.rust) else {
            continue;
        };
        let lean = model.lean.clone();
        let entry = obligations.entry(f.module.clone()).or_default();
        if let Some(i) = requires.as_ref().and_then(|r| r.import.as_ref()) {
            entry.0.insert(i.clone());
        }
        let model_binders = binders.get(&f.rust).cloned().unwrap_or_default();
        for (base, r) in obls {
            if let Some(i) = &r.import {
                entry.0.insert(i.clone());
            }
            for case in cases(f, &model_binders, requires.as_ref(), r, &crate_instance)? {
                if !case.parts.binders.is_empty() {
                    uses_laws = true;
                }
                let label = format!("{base}{}", case.suffix);
                let stmt = gen::obligation_statement(
                    f,
                    &lean,
                    &label,
                    &case.spec,
                    case.requires.as_ref(),
                    &case.parts,
                );
                let lean_label: Vec<String> = label.split('.').map(gen::lean_ident).collect();
                let _ = writeln!(
                    entry.1,
                    "/-- `{label}` of `{}`. -/\ndef {lean}.obligation.{} : Prop :=\n  {stmt}\n",
                    f.rust,
                    lean_label.join(".")
                );
                let h = hash(&format!("{stmt}\n{}", r.key));
                let previous = old.model(&f.rust).and_then(|m| {
                    m.obligation
                        .iter()
                        .find(|o| o.label == label && o.hash == h)
                });
                model.obligation.push(Obligation {
                    label: label.clone(),
                    spec: case.spec.lean.trim_start_matches("_root_.").to_string(),
                    spec_block: r.block.clone(),
                    spec_hash: r.key.clone(),
                    hash: h,
                    status: previous
                        .map(|o| o.status.clone())
                        .unwrap_or("unchecked".into()),
                    axioms: previous.map(|o| o.axioms.clone()).unwrap_or_default(),
                    proof: previous.map(|o| o.proof.clone()).unwrap_or_default(),
                });
            }
        }
        lock.model.push(model);
    }
    let laws = lean_dir.join(gen::module_path(gen::AENEAS_MODEL_MODULE));
    if uses_laws {
        write(&laws, leanfran_build::AENEAS_MODEL_LEAN, &mut written)?;
    } else if laws.is_file() {
        fs::remove_file(&laws).map_err(|e| e.to_string())?;
    }

    for (module, (imports, text)) in &obligations {
        let m = module_of("Obligation", &ident, module);
        let mut head = format!("{LEAN_HEADER}import {funs}\n");
        if aliases.get(module).is_some_and(|t| !t.is_empty()) {
            let _ = writeln!(head, "import {}", module_of("Alias", &ident, module));
        }
        if uses_laws {
            let _ = writeln!(head, "import {}", gen::AENEAS_MODEL_MODULE);
        }
        let mut all: BTreeSet<String> = imports.clone();
        all.extend(all_blocks.iter().cloned());
        for i in &all {
            let _ = writeln!(head, "import {i}");
        }
        head.push_str(open);
        let mut ns = vec![ident.clone()];
        ns.extend(module.iter().map(|m| gen::lean_ident(m)));
        let _ = writeln!(head, "open {}", ns.join("."));
        if !block_namespaces.is_empty() {
            let _ = writeln!(head, "open {}", block_namespaces.join(" "));
        }
        write(
            &lean_dir.join(gen::module_path(&m)),
            &format!("{head}\n{text}"),
            &mut written,
        )?;
    }
    prune(
        &lean_dir.join("LeanRustGen").join("Obligation").join(&ident),
        &written,
    )?;
    fs::write(b.lock_path(), lock.to_toml()).map_err(|e| e.to_string())?;
    let n: usize = lock.model.iter().map(|m| m.obligation.len()).sum();
    println!(
        "extracted {} functions, {n} obligations: lean/LeanRustGen/{{Model,Alias,Obligation,Extern}}/, leanfran.lock",
        lock.model.len()
    );
    Ok(())
}

/// The Rust types of the type parameters of `f` in the instance `inst`.
pub fn instance_types(f: &ModelFn, inst: &syn::Type) -> Vec<String> {
    match inst {
        syn::Type::Tuple(t) if f.sig.generics.len() != 1 => t
            .elems
            .iter()
            .map(leanfran_build::model::type_text)
            .collect(),
        t => vec![leanfran_build::model::type_text(t)],
    }
}

/// The trait structures of Aeneas for the Rust traits of the law classes.
fn trait_struct(rust: &str) -> Option<&'static str> {
    Some(match rust {
        "PartialEq" => "core.cmp.PartialEq",
        "Eq" => "core.cmp.Eq",
        "PartialOrd" => "core.cmp.PartialOrd",
        "Ord" => "core.cmp.Ord",
        "Clone" => "core.clone.Clone",
        "Copy" => "core.marker.Copy",
        "Default" => "core.default.Default",
        _ => return None,
    })
}

/// `true` when every bound of `f` holds for `types` by an instance of Aeneas's library that
/// leanfran's `AeneasModel` proves to be a model: a generic proof applies to these types.
pub fn modelled_by_library(f: &ModelFn, types: &[String]) -> bool {
    f.sig.generics.iter().zip(types).all(|(p, t)| {
        p.bounds.iter().all(|b| {
            trait_struct(b).is_some_and(|s| leanfran_build::gen::library_instance(s, t).is_some())
        })
    })
}