Skip to content

Add function preconditions to Core#444

Merged
shigoel merged 54 commits intomainfrom
josh/partial-func
Feb 23, 2026
Merged

Add function preconditions to Core#444
shigoel merged 54 commits intomainfrom
josh/partial-func

Conversation

@joscoh
Copy link
Contributor

@joscoh joscoh commented Feb 18, 2026

Description of changes: Adds support for function preconditions to Core. This is done via a transformation precondElim that is run before typechecking (to ensure that the generated assertions are typechecked).

Structure:
Strata/DL/Lambda/Preconditions.lean generates well-formedness conditions for Lambda expressions
Strata/Transform/PrecondElim.lean contains the transformation that
1. adds assertions at function call sites to verify that preconditions hold
2. checks the well-formedness of procedure and function contracts by ensuring that any preconditions of functions used in the contract hold
3. removes preconditions from functions, making everything total

Tests
StrataTest/DL/Lambda/PreconditionsTests.lean contains tests for generating well-formedness conditions for Lambda expressions (i.e. preconditions in called functions)
StrataTest/Transform/PrecondElim.lean contains tests of the precondElim transformation
StrataTest/Languages/Core/Examples/FunctionPreconditions.lean contains tests that involve the end-to-end verification of functions (including function decl statements) with preconditions

Other smaller changes include adding syntax for preconditions to the Core grammar, adding metadata for these preconditions, and adding a precondition that the denominator is nonzero to Int.div and Int.mod (updating the corresponding proof obligations in tests).

Note that ADT destructors do NOT by default have a precondition that they are called on the correct constructor (e.g. that List..head is called on something satisfying List..isCons). This will happen in a future PR, and we will include both checked and unchecked versions.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Josh Cohen added 27 commits February 4, 2026 13:45
…handling

Now produces definition-time and call-time wf assumptions
Preserves implications in exprs to avoid losing information
Handles calls in quantifiers and lambdas correctly
Some more test cases
Don't generate VCs directly, instead transform into asserts with extra
procedures
Includes tests
Reverts many prior changes
Need to duplicate Factory datatype parts (for now) because of type aliases
Fix issue with indices in Translate.lean
Add test case in PrecondElim.lean
Need to thread Factory through to keep track of func preconditions
Factory generated on the fly, not all at once
Correctly handle function decl statement scopes
Improve comments and formatting
@joscoh joscoh marked this pull request as ready for review February 18, 2026 21:13
@joscoh joscoh requested a review from a team as a code owner February 18, 2026 21:13
shigoel
shigoel previously approved these changes Feb 20, 2026
Copy link
Contributor

@shigoel shigoel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@shigoel shigoel enabled auto-merge February 20, 2026 16:18
@joscoh joscoh dismissed MikaelMayer’s stale review February 20, 2026 16:19

Discussed separately, Mikael is unavailable at the moment

aqjune-aws
aqjune-aws previously approved these changes Feb 20, 2026
@shigoel shigoel added this pull request to the merge queue Feb 23, 2026
Merged via the queue into main with commit cb2411d Feb 23, 2026
15 checks passed
@shigoel shigoel deleted the josh/partial-func branch February 23, 2026 23:14
fabiomadge added a commit that referenced this pull request Feb 24, 2026
Merged commits: f6ff88b, a6e5f93, 64fd6d6, 6f67113, 9dccebf,
a0716de, 1221d67, d1c7ddd, cb2411d

Conflict resolutions:
- IntBoolFactory.lean: took HEAD (has full function set + DivT/ModT).
  Added intSafeDivFunc/intSafeModFunc with preconditions (PR #444).
  Added Inhabited constraint to IntBoolFactory.
- SMTEncoder.lean: added main's SafeMod + DivT/ModT SMT encodings.
- LaurelToCoreTranslator.lean: took HEAD logic, wrapped init RHS
  values in `some` for PR #432's Cmd.init signature change.
- Laurel.lean: took main's version (PR #434 docstrings), then
  restored branch-specific changes: preconditions (list) in Procedure,
  postconditions (list) in Body.Opaque/Abstract, removed Determinism,
  added deriving Repr on WithMetadata and HighType, added Repr
  instance for Imperative.MetaData.
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.

4 participants