Skip to main content

Crate inlean

Crate inlean 

Source
Expand description

Inline Lean 4 in Rust.

lean! takes Lean code written in a Rust source file, has Lean check it, proofs included, and expands to Rust translated from what Lean’s compiler produced for the functions marked @[export name]. Nothing of Lean is linked into the program.

ⓘ
inlean::lean! {
    @[export midpoint]
    def midpoint (lo hi : UInt32) : UInt32 := lo + (hi - lo) / 2

    theorem midpoint_ge (lo hi : UInt32) (h : lo <= hi) : lo <= midpoint lo hi := by
      grind [midpoint]

    theorem midpoint_le (lo hi : UInt32) (h : lo <= hi) : midpoint lo hi <= hi := by
      grind [midpoint]
}

assert_eq!(midpoint(4_000_000_000, 4_000_000_002), 4_000_000_001);

With (lo + hi) / 2 in place of the body, the sum can wrap around, the first theorem is false, and the crate does not compile.

A Rust type of the crate takes part at one of three levels, each named in a table of the crate’s Cargo.toml: converted to a Lean type at each crossing (a Primitive, under [package.metadata.inlean.types]), carried unconverted for a type the block declares opaque ([package.metadata.inlean.opaque]), or carried for a structure or inductive type of the block whose Lean definition is its model ([package.metadata.inlean.foreign]). An @[extern] declaration of a block runs a Rust function that the crate names under [package.metadata.inlean.trusted], and is trusted, not checked, to compute what the declaration says. The guide describes each, and Choosing a level says which to use.

A block that writes import Inlean can also use Rust’s own Vec, VecDeque, HashMap and HashSet: the collection crosses unconverted, its operations run the standard library’s code, and proofs use its model in Lean. Nothing is named in Cargo.toml; see Collections in the guide.

Macros§

lean
Lean code, checked by Lean when the crate is compiled and translated to Rust.
lean_file
Like lean!, for Lean code kept in a file: lean_file!("src/proofs.lean").

Structs§

Generic
A Lean value of a type that a declaration bound to Rust is generic in.
Int
An integer of any size: Lean’s Int.
Nat
A natural number of any size: Lean’s Nat.
ParseIntError
A string is not an integer in decimal digits.
ParseNatError
A string is not a natural number in decimal digits.
TooLarge
A Nat does not fit the integer type it was converted to.

Traits§

Pass
An argument of type T for a function written in Lean: the value itself, which moves into Lean, or a reference to it. A reference to a value that Lean converts, such as a String, is copied. A reference to a value that Lean holds as it is, of an opaque or a foreign type or a collection of import Inlean, is lent for the call: Lean reads the caller’s value, and clones it only where it needs one of its own.
Primitive
A primitive type of your own: a Rust type of your choice in place of the one inlean gives a Lean type.