Skip to content

Testing for agreement between Lean and Z3 semantics - #11

Draft
simonspies wants to merge 3 commits into
mainfrom
semantic-testing
Draft

Testing for agreement between Lean and Z3 semantics#11
simonspies wants to merge 3 commits into
mainfrom
semantic-testing

Conversation

@simonspies

Copy link
Copy Markdown
Owner

Building on #10, this PR adds a test that compares the semantics of various operations, including div and mod between Z3 and Lean to make sure there are no accidental discrepancies.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the semantic consistency between Lean and Z3 for integer arithmetic operations, particularly division and modulo. It integrates the mod operator into the TinyML language and ensures that its behavior, along with div, is correctly translated and verified by Z3. The changes include defining custom Z3 functions for these operations, updating the compiler to handle them with appropriate assertions, and introducing comprehensive tests to validate the agreement across a wide range of inputs.

Highlights

  • Modulo Operator Introduction: The mod binary operator has been introduced into the TinyML language, including its expression definition, lexing, parsing, operational semantics, printing, and typing rules.
  • Z3 Semantic Alignment for Division and Modulo: Custom SMT-LIB functions zdiv and zmod were defined in Mica/Engine/Driver.lean to ensure that Z3's integer division and modulo semantics precisely match Lean's behavior, especially concerning zero divisors.
  • Verifier Compilation for Division and Modulo: The verifier's compilation process for div and mod operations was updated to explicitly assert non-zero divisors in Z3, ensuring correctness and consistency with Lean's semantics.
  • New Semantic Agreement Tests: A new test suite (Test/Semantics.lean) was added to systematically compare the evaluation of integer binary operations (including div and mod) between Lean and Z3 across various inputs, including edge cases like zero divisors.
  • New Example Files and Test Runner: New example files (Examples/division.ml, Examples/gcd.ml) were added, and a test.sh script was introduced to automate running both the new semantic agreement tests and existing example verification tests.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for the mod operator and adds comprehensive tests to verify the semantic agreement between Lean and Z3 for various binary operations, including edge cases like division by zero. The changes are well-structured, touching the lexer, parser, operational semantics, and verifier to correctly integrate the new operator. The verifier's approach to handling division and modulo by asserting a non-zero divisor is a solid improvement. I have a couple of suggestions to enhance code quality by addressing code duplication and removing dead code, aligning with the project's style guide.

Comment on lines +354 to +372
first
| (refine ⟨.int (a / b), ?_, ?_⟩
· simp [TinyML.evalBinOp, hne_zero]
· exact hpost (.int (a / b)) ρ_r st₂ .int _ hΨ_post
(by intro v hv; simp [Term.freeVars] at hv
rcases hv with hv | hv
· exact (sl.wfIn_mono hsl_wf hdecls_r) v hv
· exact hsr_wf v hv)
(by simp [Term.eval, UnOp.eval, BinOp.eval, hsl_ρ_r, hsr_eval])
(.int _))
| (refine ⟨.int (a % b), ?_, ?_⟩
· simp [TinyML.evalBinOp, hne_zero]
· exact hpost (.int (a % b)) ρ_r st₂ .int _ hΨ_post
(by intro v hv; simp [Term.freeVars] at hv
rcases hv with hv | hv
· exact (sl.wfIn_mono hsl_wf hdecls_r) v hv
· exact hsr_wf v hv)
(by simp [Term.eval, UnOp.eval, BinOp.eval, hsl_ρ_r, hsr_eval])
(.int _))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This block has a significant amount of duplicated code for the div and mod cases. The proof tactics for well-formedness (wfIn) and evaluation (eval) are identical in both branches of the first block. This duplication can be reduced by extracting the common proof tactics into let-bindings, which improves readability and maintainability, adhering to the style guide's principle of avoiding duplication.

              let wf_proof := by
                intro v hv; simp [Term.freeVars] at hv
                rcases hv with hv | hv
                · exact (sl.wfIn_mono hsl_wf hdecls_r) v hv
                · exact hsr_wf v hv
              let eval_proof := by simp [Term.eval, UnOp.eval, BinOp.eval, hsl_ρ_r, hsr_eval]
              first
              | (refine ⟨.int (a / b), ?_, ?_⟩
                 · simp [TinyML.evalBinOp, hne_zero]
                 · exact hpost (.int (a / b)) ρ_r st₂ .int _ hΨ_post wf_proof eval_proof (.int _))
              | (refine ⟨.int (a % b), ?_, ?_⟩
                 · simp [TinyML.evalBinOp, hne_zero]
                 · exact hpost (.int (a % b)) ρ_r st₂ .int _ hΨ_post wf_proof eval_proof (.int _))
References
  1. The style guide recommends avoiding code duplication. The current implementation for div and mod proof cases contains repeated tactic blocks that can be refactored. (link)

Comment thread Test/Semantics.lean
Comment on lines +35 to +40
def checkIntBinOp (name : String) (op : BinOp .int .int .int) (a b : Int)
: VerifM (String × Bool) := do
let lhs := Term.binop op (.const (.i a)) (.const (.i b))
let rhs : Term .int := .const (.i (op.eval a b))
let ok ← VerifM.check (Formula.eq .int lhs rhs)
return (s!"{name}({a}, {b})", ok)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The function checkIntBinOp is defined here but is not used anywhere in the file. The main function uses runTest to execute the tests. This unused function constitutes dead code and should be removed to improve maintainability, as per the project's style guide.

References
  1. The style guide's completion discipline states that dead code, such as abandoned helper functions, should be deleted. (link)

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