Skip to main content

inlean/
lib.rs

1//! Inline Lean 4 in Rust.
2//!
3//! [`lean!`] takes Lean code written in a Rust source file, has Lean check it, proofs
4//! included, and expands to Rust translated from what Lean's compiler produced for the
5//! functions marked `@[export name]`. Nothing of Lean is linked into the program.
6//!
7//! ```ignore
8//! inlean::lean! {
9//!     @[export midpoint]
10//!     def midpoint (lo hi : UInt32) : UInt32 := lo + (hi - lo) / 2
11//!
12//!     theorem midpoint_ge (lo hi : UInt32) (h : lo <= hi) : lo <= midpoint lo hi := by
13//!       grind [midpoint]
14//!
15//!     theorem midpoint_le (lo hi : UInt32) (h : lo <= hi) : midpoint lo hi <= hi := by
16//!       grind [midpoint]
17//! }
18//!
19//! assert_eq!(midpoint(4_000_000_000, 4_000_000_002), 4_000_000_001);
20//! ```
21//!
22//! With `(lo + hi) / 2` in place of the body, the sum can wrap around, the first theorem is
23//! false, and the crate does not compile.
24//!
25//! A Rust type of the crate takes part at one of three levels, each named in a table of the
26//! crate's `Cargo.toml`: converted to a Lean type at each crossing (a [`Primitive`], under
27//! `[package.metadata.inlean.types]`), carried unconverted for a type the block declares
28//! `opaque` (`[package.metadata.inlean.opaque]`), or carried for a structure or inductive type
29//! of the block whose Lean definition is its model (`[package.metadata.inlean.foreign]`). An
30//! `@[extern]` declaration of a block runs a Rust function that the crate names under
31//! `[package.metadata.inlean.trusted]`, and is trusted, not checked, to compute what the
32//! declaration says. The [guide] describes each, and [Choosing a level][levels] says which
33//! to use.
34//!
35//! A block that writes `import Inlean` can also use Rust's own `Vec`, `VecDeque`, `HashMap`
36//! and `HashSet`: the collection crosses unconverted, its operations run the standard
37//! library's code, and proofs use its model in Lean. Nothing is named in `Cargo.toml`; see
38//! [Collections][collections] in the guide.
39//!
40//! [guide]: https://github.com/shergin/inlean/blob/main/docs/guide/README.md
41//! [levels]: https://github.com/shergin/inlean/blob/main/docs/guide/choosing-a-level.md
42//! [collections]: https://github.com/shergin/inlean/blob/main/docs/guide/collections.md
43
44pub use inlean_macros::{lean, lean_file};
45pub use inlean_runtime::{Generic, Int, Nat, ParseIntError, ParseNatError, Pass, Primitive, TooLarge};
46
47/// The runtime that generated code calls. Not a public API.
48#[doc(hidden)]
49pub use inlean_runtime as __rt;