Warning Although there is a Lean theorem about the Flapjack compiler, it might not mean anything. Flapjack is very young, and nobody has examined it carefully. Distributed “as is” under the BSD-3-Clause license.
Flapjack is an in-progress Lean 4 port of the formally verified Pancake compiler.
- A compiler correctness theorem from Pancake to a vibe-ported RISC-V semantics has been proven, under explicit assumptions: Lean theorem, corresponding original CakeML theorem.
- The vibe-ported RISC-V semantics differs from the authoritative Sail RISC-V
model. flapjack-riscv-check
compares the two models. It proves that they run in lockstep, but only on
the 37 instructions Flapjack's backend emits and only under restrictions:
no hint encodings, aligned accesses to plain RAM, and a Sail configuration
fixed to match the L3 model. Outside those restrictions the two models
behave differently. The compiler correctness theorem has not been carried
over to Sail. See
docs/SOUNDNESS.mditem 2.
Important
Review the RISC-V semantics before using Flapjack. The correctness
theorem is about Flapjack's own L3-derived RISC-V model
(Flapjack.RiscV.L3), not about the official RISC-V specification. Check
that this model, and the conditions under which it agrees with Sail, fit
your hardware and intended use. Flapjack's main branch tries to follow
the original Pancake compiler's choices about RISC-V semantics. Where L3 and
Sail differ, main keeps the L3 behaviour instead of changing the model to
match Sail.
- An experimental MIPS32 backend targets the Ziren
zkVM. Its correctness theorem is stated against Ziren's own executable Lean model of
MIPS32r2 (
ZirenDet.Isa, a Lake dependency), not against L3 or Sail; seedocs/MIPS32.mdanddocs/SOUNDNESS.mditem 9. - An experimental ARMv8 (AArch64) backend follows CakeML's
arm8target. Its source-to-assembly output is byte-for-byte parity-tested against the original Pancake compiler. It provesencoderCorrect arm8Targetfor a closed ARMv8 machine model covering the emitted instruction subset and instantiates the Pancake-to-machine-code theorem there, and it also proves the L3 obligationarm8EncoderCorrectL3 : encoderCorrect arm8TargetL3over CakeML'sarm8_state/NextStateARM8model, withpanToTargetCompileSemanticsArm8L3instantiating the Pancake-to-target theorem at that configuration; seedocs/ARM8.mdanddocs/SOUNDNESS.mditem 10. - An x86-64 backend follows CakeML's
x64target. Its source-to-assembly output is byte-for-byte parity-tested against the original Pancake compiler. The machine model is a closed model for the emitted instruction subset, not a port of CakeML's full L3 x86-64 semantics; seedocs/X64.md. - Other backends, including 32-bit ARM and 32-bit x86, have not yet been ported to Lean.
- An experimental CakeML front end and
cakeexecutable compile CakeML source to RISC-V. The ports are bounded, and full output parity and end-to-end correctness remain unproved; see parity testing.
The Lean specifications and their implications still require independent review. See
docs/SOUNDNESS.md for limitations and
docs/PARITY-TESTING.md for reference comparisons.
Install the pinned Lean toolchain and build the library:
lake buildRun the executable regression suite, including the CakeML-derived RISC-V byte goldens:
lake testCompile CakeML source to RISC-V assembly with the experimental frontend:
lake exe cake --target=riscv < program.cml > program.riscv.SFor output comparisons, pass --target=riscv to both compilers. Flapjack's
cake defaults to RISC-V; the pinned upstream cake defaults to x86-64,
which this executable does not support. The tested output comparisons do not
establish equivalence for all inputs.
Compile a Pancake source file through the source-facing RV64I path:
lake exe flapjack-compile program.pnk > program.riscv.SThe compiler also reads source from standard input:
printf 'fun 1 main() { return 7; }\n' | lake exe flapjack-compileThe default output (also selected by --assembly or --pancake) follows the
original Pancake/CakeML RISC-V assembly artifact boundary: runtime data and
bitmap framing, cml_main startup, cake_main, linked code-section labels,
.byte payloads, and cake_codebuffer_* markers. It is not an ELF file. For
the historical raw byte artifact, use:
lake exe flapjack-compile --hex program.pnk > program.riscv.hexFor the MIPS32 (Ziren) target, pass --target=mips32 first; the output modes are the
same, and the assembly frame is a mipsel GNU assembler file
(see docs/MIPS32.md):
lake exe flapjack-compile --target=mips32 program.pnk > program.mips32.SFor the x86-64 target, pass --target=x64; the output is a GNU x86-64
assembler file in CakeML's x64_export frame (see docs/X64.md):
lake exe flapjack-compile --target=x64 program.pnk > program.x64.SThe original reference compiler can be run locally with:
cakeml/developers/bin/cake --pancake --target=riscv < program.pnk > program.cake.SUse the parity tests, docs/PARITY-TESTING.md, and
docs/SOUNDNESS.md when interpreting comparisons between
the two outputs. For randomized cross-checking, the deterministic
differential fuzzer scripts/parity-difffuzz.py compares full artifacts
(sections, bytes, frame, acceptance) against cake and files every unknown
mismatch as a bead; see the differential-fuzzing section of
docs/PARITY-TESTING.md.
For the parser API and its current limitations, see
Flapjack/Parser/README.md.
When porting a definition or theorem, generate the local CakeML/HOL source
index with python3 scripts/index-hol.py. It records declaration locations,
theory dependencies, and the CakeML commit used; see
docs/HOL-INDEX.md. The generated .hol-index/ directory
is gitignored.
The evolving HOL-to-Lean source layout is recorded in
docs/HOL-LAYOUT.md.
The RISC-V compiler port and its correctness proof
are still in progress. GitHub issues
track work and claims; PLAN.md gives the staged direction.
docs/SOUNDNESS.md describes limitations, external
assumptions, and out-of-scope gaps.
Start with the porting and verification rules in AGENTS.md.
Work bottom-up from a desired theorem through its HOL dependencies: port small
definitions and supporting lemmas first. Use @[hol ...] tags and
scripts/check-hol-refs.py --mapping to locate existing work. Run
python3 scripts/next-hol-port.py --file cakeml/pancake/FILE.sml to list
untagged candidates in a script; --goal HOL_NAME limits the list to earlier
declarations, and --kind Theorem includes theorem candidates. Check the
source, Lean analogues, and issue claims before choosing a target: tags are
navigation aids, not evidence of equivalence or of a missing port.
Flapjack was started with the support of zkSecurity and the Ethereum Foundation.