A constrained Lean 4 safety DSL
Author safety functions as state machines, guarded transitions, boolean and fixed-point dataflow — and prove safety properties (invariants, safe-state reachability) with the Lean kernel.
Lean 4
DjiniousSafeCertifiable codeDjiniousSafe is a toolchain for building safety functions to IEC 61508 SIL 4: specify them formally in Lean 4, prove their safety properties with the Lean kernel, and generate no_std, panic-free Rust — with a per-build, machine-checked certificate that the shipped code equals the spec, and complete traceability from requirements to theorems.

What it is
A Lean-to-Rust safety code generation pipeline. It validates requirements, exports Lean-checked IR, generates restricted Rust, performs equivalence and back-to-back gates, and writes an audit evidence bundle for accepted builds. Failed builds are fail-closed.
Author safety functions as state machines, guarded transitions, boolean and fixed-point dataflow — and prove safety properties (invariants, safe-state reachability) with the Lean kernel.
Lean 4Two independently designed generators, one in Rust, one in Lean, from the same specification. Their outputs must be structurally identical — any divergence fails the build.
sgen-a · sgen-bEvery build re-parses the emitted Rust text and the Lean kernel checks a fresh certificate that it is semantically equal to the spec. What is proved is what ships.
Kernel certificateZero heap, panic-free, no unsafe, constant-bounded loops, saturating arithmetic — ready for the Ferrocene qualified compiler.
#![no_std]Requirement → Lean theorem → Rust symbol → test vector. Hash-pinned requirement text turns change impact into a build error.
Trace matrixExplicit widths, scaling and saturation — plus kernel-checked overflow-freedom and exactness obligations on every constant.
Q-formatInside the product
Every capture below is the running product.
01 · Fail-closed everywhere
A build runs its gates in a fixed order and every gate appends a row to the evidence. Proofs, generator equivalence, certificate, trace matrix and back-to-back tests are all hard gates: the first failure aborts the build, with the error recorded as the failed row’s detail — and no later gate runs.
02 · Generators are never trusted
The Rust text is produced twice, independently: sgen-a lowers the exported IR in Rust; sgen-b prints directly from the elaborated safety function in Lean. The equiv gate requires AST identity, the reprint gate byte-identity — a defect must occur identically in both toolchains to pass.
03 · Reference safety functions
The voter example carries nine reference safety functions from requirement to certified code: a 2oo3 voter, a latching trip, a rate limiter, a threshold monitor with hysteresis, a gain block, a statistics block, a dot-ratio block, a pressure interlock and a flow gauge with an IEEE-754 f32 boundary — each shipped with a kernel-checked equivalence certificate.

04 · On-target replay
Lean-side reference vectors — boundary cross-products, 8 × 32-step LCG sequences and, for float-boundary functions, f32 probes including NaN, ±inf, ±0 and rounding ties — are replayed against the compiled code on the host. When a target is configured, the same vectors replay under QEMU on thumbv7em-none-eabihf, with the SHA-256 of each replayed ELF pinned in the evidence.
AI & agents
djsafe serve wraps the existing pipeline without duplicating build behaviour: local CLI commands remain the source of truth, and the service exposes those same operations over an agent-ready Provider API and an MCP endpoint.
Capabilities, identity and an OpenAPI document, with build and clean as idempotent operations.
The same operations as MCP tools, so an agent can discover and invoke them.
Discover capabilities before invoking operations, keep tokens out of logs and summaries, report failing gates verbatim enough to diagnose the build, and ask before deploying.
Capabilities
One CLI, djsafe, orchestrates a workspace of small, single-purpose crates and a Lean package.
Lean safety semantics, specs and export code: the DSL, interval overflow-freedom, the machine-accuracy bridge, the Lean-side generator and the back-to-back vector export.
Requirements model parsing and hashing — per-requirement SHA-256 over whitespace-normalised text.
The typed safety IR exported from Lean and hash-pinned on both sides.
Rust generator A, lowering the exported IR.
The restricted-Rust parser and printer behind the byte-canonicality law.
The generator equivalence gate: AST identity or the build fails.
Certificate generation: a Lean term rebuilt from the parsed artifact, checked by the kernel.
Trace matrix generation in JSON, CSV and HTML, rejecting orphan requirements, unknown references and untraced functions by name.
CLI, pipeline orchestration, HTTP service and remote client commands — build and clean locally or on the deployed service.
An AI-agent skill for operating the deployed service.
Trust
No single component is trusted for a claim that is not cross-checked through a structurally different path. Every load-bearing claim can be re-derived by an assessor without trusting the pipeline’s own report.
Every semantic claim reduces to kernel-checked proofs. An axiom audit shows the certificates depend only on Lean’s standard axioms — no sorry, no native_decide, no custom axioms.
Lean 4.15.0Re-pin a spec with your own third hash, re-run a certificate through the kernel, re-run the whole build and diff the evidence, replay an on-target binary by hand, and round-trip a shipped artifact through the parser and printer.
The generated-code builds can be pointed at Ferrocene through the [toolchain] configuration; the compiler’s identity is recorded in every evidence bundle. Identity is not qualification, and the evidence says so.
FerroceneRequirements validity remains a human review. No claim concerns timing, WCET, stack bounds or memory footprint. QEMU validates cross-compiled machine code and target libcore, not physical silicon.
In the digital thread
DjiniousSafe does its part of the engineering loop and passes its evidence along — and it works just as well on its own.
DjiniousSafeCertifiable codeOn its own, DjiniousSafe is a command-line toolchain: point djsafe at a project of requirements and Lean specifications, and it returns certified embedded Rust with its evidence — or the gate that refused it.
Use cases
2 worked cases.
Safety-critical embedded controlNine safety functions, from YAML requirement to certified embedded RustThe voter reference project takes nine safety functions — voting, latching, slew limiting, trip monitoring, interlocks and a flow gauge with a floating-point boundary — through all thirteen gates, and replays every one on an emulated Cortex-M4F.
Embedded code generationModel → Rust firmwareModel-based design to deployable code, the Rust-first way — an autopilot designed as block diagrams, code-generated to #![no_std] Rust, proven equivalent to the model, cross-compiled to a Cortex-M target, and attested for provenance.Book a demo
See a safety function go from requirement to certified embedded Rust — and watch the build refuse when it should.
Along the thread
The AI systems engineer: it takes the assignment and carries the whole project through a traceable System Ledger.
Components wired into a complete system and closed through GPU physics in NVIDIA Isaac Sim.
Multi-domain system models, digital replicas and a mathematical toolbox, driven by AI.
Graph automation any stage can call on: build a process, run it, watch it and audit it.