Skip to content

Conversation

@MikaelMayer
Copy link
Contributor

@MikaelMayer MikaelMayer commented Jan 27, 2026

Add support for function declarations within statement blocks

This PR adds the ability to declare functions as a statement Stmt.funcDecl in Strata Core, enabling local function definitions within procedures.

Changes

  • Added Stmt.funcDecl for declaring functions within statement blocks
  • Implemented statement-level operations (variable analysis, substitution, type erasure) for funcDecl
  • Added type checking that converts between PureFunc and Function representations
  • Extended all program transformations to handle funcDecl

Semantics

  • Refactored semantics to thread the evaluator δ (SemanticEval) as state through EvalStmt and EvalBlock
  • When a function is declared, the evaluator is extended with the new function definition
  • Closure capture semantics: free variables from the enclosing scope are captured by value (lexical scoping)
  • Updated EvalCmdParam signature to include δ' output parameter for evaluator changes
  • Updated transformation correctness proofs to work with the new evaluator threading

Testing

Added tests demonstrating funcDecl functionality including polymorphic functions with type parameters. Existing tests pass.

@MikaelMayer MikaelMayer changed the title Add support for function declarations within statement blocks Add support for function declarations statements Jan 27, 2026
@MikaelMayer MikaelMayer changed the title Add support for function declarations statements Add support for function declaration statements Jan 27, 2026
@MikaelMayer MikaelMayer force-pushed the add-func-decl-to-statements branch from 6016b5a to b8ec252 Compare January 28, 2026 18:21
…or Func

- Added ToFormat for generic Func with proper constraints
- Added [ToFormat T.IDMeta] to Factory.lean section variables
- Removed unnecessary ToFormat instances from test files and Program.lean
- Removed custom Env.format function (now uses default ToFormat)
- Function bodies now display properly instead of showing <body>
- Resolved conflict in Factory.lean (Factory_wf moved to FactoryWF.lean in main)
- Applied rotate_left fix to FactoryWF.lean
@MikaelMayer MikaelMayer marked this pull request as ready for review January 29, 2026 20:27
@MikaelMayer MikaelMayer requested a review from a team as a code owner January 29, 2026 20:27
MikaelMayer and others added 6 commits January 29, 2026 14:35
- Modified testFuncDeclSymbolic to show functions capture variables by reference
- Function declared with n=10, then n mutated to 20, function uses n=20 at call time
- Proof obligation correctly shows result should be 25 (5+20), not 15 (5+10)
- Reverted Env.lean to main (custom scope formatting not needed)
…claration time

- Functions now capture variable values at declaration time, not by reference
- Free variables in function body are substituted with their current values from environment
- Test demonstrates: n=10 at declaration, n=20 after mutation, function uses n=10
- Proof obligation correctly shows result is 15 (5+10), not 25 (5+20)
Copy link
Contributor

@joscoh joscoh left a comment

Choose a reason for hiding this comment

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

Overall looks good, had a few questions about polymorphism. Also, do the small step semantics for Statements (StmtSemanticsSmallStep.lean) need to be updated?

@MikaelMayer
Copy link
Contributor Author

I updated StmtSemanticsSmallStep.lean to support function declaration semantics. Because functions are closures, I had to add support for a generic free variable replacement so that it works for any Func , not just LFunc

@MikaelMayer MikaelMayer enabled auto-merge February 2, 2026 19:53
- Add WFfuncDeclProp structure in WF.lean checking input parameter uniqueness
- Add LFunc.type_inputs_nodup theorem in Factory.lean
- Add Function.typeCheck_inputs_nodup theorem in FunctionType.lean
- Add listMap_keys_map_snd helper lemma in StatementWF.lean
- Replace sorry in funcDecl case with complete proof
MikaelMayer and others added 4 commits February 5, 2026 16:18
…Check

- Simplify FuncWF.body_freevars to use subset notation (porting PR #386)
- Simplify FuncWF.body_freevars_decidable to a 1-line proof
- Extract PureFunc.typeCheck helper in FunctionType.lean to reduce code duplication
- Update StatementType.lean to use the shared PureFunc.typeCheck helper
- Changed extendEval from existential parameter inside step_funcDecl to
  explicit parameter of StepStmt inductive type
- Updated StepStmtStar, EvalStmtSmall, EvalStmtsSmall, IsTerminal to
  accept extendEval parameter
- Updated evalStmtsSmallNil and terminalIsTerminal theorems accordingly
- Also includes PureFunc.typeCheck returning Function to avoid duplicate
  conversion in StatementType.lean
Address PR review comment: LFuncWF is now a type alias (abbreviation) of
FuncWF, instantiated with Lambda-specific getName and getVarNames functions.
This maintains the same semantics while reducing code duplication.
aqjune-aws
aqjune-aws previously approved these changes Feb 6, 2026
@MikaelMayer MikaelMayer added this pull request to the merge queue Feb 6, 2026
Merged via the queue into main with commit faa0874 Feb 6, 2026
15 checks passed
@MikaelMayer MikaelMayer deleted the add-func-decl-to-statements branch February 6, 2026 20:54
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.

5 participants