Package 'leanknit'

Title: A 'knitr' Engine for Lean 4 Proof Chunks
Description: Provides a 'knitr' language engine that compiles and checks Lean 4 code chunks against a 'lake' project, capturing proof output (including the result of '#print axioms') into rendered documents. Modeled on the 'duckknit' engine. Because a failing Lean check fails the render, a generated document cannot claim a proof that does not actually check. Also provides install_lean() to provision a Lean toolchain through 'elan', in the spirit of torch::install_torch().
Authors: Sounkou Mahamane Toure [aut, cre]
Maintainer: Sounkou Mahamane Toure <[email protected]>
License: GPL (>= 3)
Version: 0.0.0.9000
Built: 2026-06-11 00:21:36 UTC
Source: https://github.com/sounkou-bioinfo/leanknit

Help Index


The lean knitr engine

Description

Registered as knitr::knit_engines$get("lean") on package load. Not normally called directly.

Usage

eng_lean(options)

Arguments

options

A knitr chunk options list.

Value

Rendered chunk output (from knitr::engine_output()).


Locate the lake binary

Description

Resolution order: the leanknit.lake option, then lake on PATH, then the default elan install location ⁠~/.elan/bin/lake⁠.

Usage

find_lake()

Value

Absolute path to lake.


Install the Lean 4 toolchain

Description

Downloads and runs the official elan installer, which provisions elan, lake, and a Lean toolchain under ⁠~/.elan⁠. This mirrors the convenience of torch::install_torch(): the heavy external toolchain is fetched on demand rather than vendored.

Usage

install_lean(toolchain = "stable", modify_path = FALSE, quiet = FALSE)

Arguments

toolchain

Lean toolchain channel or version to install as default, e.g. "stable" (default) or "leanprover/lean4:v4.30.0".

modify_path

If TRUE, let the installer modify shell profiles to add ⁠~/.elan/bin⁠ to PATH. Defaults to FALSE (non-invasive).

quiet

Suppress installer output.

Details

After installation lake lives at ⁠~/.elan/bin/lake⁠, which find_lake() discovers automatically. To use lake from an interactive shell, add ⁠~/.elan/bin⁠ to your PATH (the installer can do this for you).

Value

The path to the elan bin directory, invisibly.


Report the leanknit / Lean toolchain situation

Description

Prints the resolved lake path and version, the configured project, and any active sessions. Useful in a document setup chunk to confirm the engine can run before relying on it.

Usage

lean_sitrep()

Value

A list with lake, version, project, and sessions, invisibly.


Report the installed Lean version

Description

Report the installed Lean version

Usage

lean_version()

Value

The lean --version string, or NA if Lean is not available.


Is a usable Lean toolchain available?

Description

Is a usable Lean toolchain available?

Usage

leanknit_available()

Value

TRUE if find_lake() succeeds, otherwise FALSE.


A disposable copy of the bundled example lake project

Description

Copies the small Lean project shipped under inst/lean to a fresh temporary directory and returns its path. Used by the vignette and tests so that build artifacts never land in the installed package. Point leanknit.project at the returned path.

Usage

leanknit_example_project()

Value

Path to a writable copy of the example project.


Compile a Lean source string against a lake project

Description

Writes code to a temporary .lean file inside project (so import resolves) and runs ⁠lake env lean⁠ on it.

Usage

leanknit_exec(project, code, timeout = 120000L)

Arguments

project

Path to the lake project.

code

Lean source as a single string.

timeout

Timeout in milliseconds.

Value

A list with stdout, stderr, and integer status.


Get a leanknit session, creating it if absent

Description

Get a leanknit session, creating it if absent

Usage

leanknit_get_session(name)

Arguments

name

Session name.

Value

The session environment, invisibly.


List active leanknit sessions

Description

List active leanknit sessions

Usage

leanknit_list_sessions()

Value

A data frame of session names, accumulated chunk counts, and which session is active.


Start (or reset) a leanknit session

Description

Start (or reset) a leanknit session

Usage

leanknit_start_session(name)

Arguments

name

Session name.

Value

The session environment, invisibly.