Skip to content

sir: certified lowering from Vars to Stack - #27

Open
Eduardogbg wants to merge 13 commits into
sir/machine-languagesfrom
sir/lowering
Open

sir: certified lowering from Vars to Stack#27
Eduardogbg wants to merge 13 commits into
sir/machine-languagesfrom
sir/lowering

Conversation

@Eduardogbg

@Eduardogbg Eduardogbg commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

this PR defines and verifies the lowering from Vars to Stack. this lowering requires an additional external witness (from plank's rust compiler), namely the stack schedule. it lifts the knowledge of a valid stack schedule into a proven lowering between the languages.

we check a stack schedule is correct by symbolically executing both a Vars and a Stack program against opaque variable names. as long as both executions match, meaning that they fire the same ops against their respective operands, the schedule checks.

  • StackSchedule.check validates a whole function's schedule; StackSchedule.Block.check checks one block.
  • the schedules we currently handle: reordering independent statements, renaming across block edges (arity-checked), exchange/flippedOp, leftover stack at a halting block, loops. we don't yet accept effectful ops such as icall
  • a program certificate pairs an init schedule with an optional main; the lowering theorem is schedule.check = .ok () → Equiv schedule.vars schedule.stack (ProgramSchedule.equiv; StackSchedule.equiv is the one-function corollary)
  • rejections are typed: a failing schedule reports its reason (useBeforeDefinition, boundaryArityMismatch, ...)
  • a Scheduler is Accepted when every schedule it emits checks and schedules exactly its input (Scheduler.Accepted.equiv, Scheduler.Accepted.schedules_input)
  • defined a spillAll scheduler, which loads every operand from its memory slot and stores every result back so nothing stays on the stack between statements, and proved the checker accepts everything it emits (spillAll_accepted)
  • check Examples/Corpus.lean to see the equivalence discharged for real scheduler output (straight-line, diamond, loop) and for a two-function program with main
  • Examples/Lowering.lean has accepted and rejected schedules, each rejection proving its specific error

@Eduardogbg
Eduardogbg force-pushed the sir/machine-languages branch from d37a264 to 1275cc5 Compare August 13, 2026 04:42
@Eduardogbg
Eduardogbg force-pushed the sir/machine-languages branch from 1275cc5 to 014009d Compare August 13, 2026 07:25
@Eduardogbg
Eduardogbg force-pushed the sir/lowering branch 2 times, most recently from 7dbe434 to 48dc4a7 Compare August 13, 2026 08:37
@Eduardogbg
Eduardogbg force-pushed the sir/machine-languages branch from 014009d to e759e2f Compare August 13, 2026 08:37
@Eduardogbg
Eduardogbg force-pushed the sir/lowering branch 4 times, most recently from 1e1cddd to 4e7c326 Compare August 18, 2026 06:48
@Eduardogbg
Eduardogbg marked this pull request as ready for review August 18, 2026 20:28
Eduardogbg and others added 2 commits August 19, 2026 18:38
StackSchedule stays the per-function certificate; ProgramSchedule pairs an
init certificate with an optional main and lifts the equivalence to whole
programs by relocating each certified function's evaluations.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Convert nested match/if expressions in StackSchedule checker functions to use
do-notation with throw, pure, and pattern matching syntax. This reduces
verbosity and makes the code more readable.

Functions updated:
- StackSchedule.Block.checkFinalStack
- StackSchedule.Block.check
- StackSchedule.checkBlocks
- StackSchedule.checkEdge
- StackSchedule.checkBlockEdges
- StackSchedule.checkEdges
- StackSchedule.check
- ProgramSchedule.check

The proofs should still work as the do-notation desugars to the same
structure that split can operate on.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Eduardogbg and others added 2 commits August 22, 2026 00:54
Convert functions in Stack.Spec to use do-notation for cleaner, less verbose code:

- sourceFetch: use pattern matching with | syntax for list destructuring
- decode: extract instruction decoding to do block, match on instruction
- control: add outer do block for consistency
- resume: use | syntax for Except matching

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Restructure decode function to use nested match on instruction type,
which is more maintainable and easier to extend with new instruction types.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant