A Python frontend for LemmaScript: proposing a small external-frontend interface
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- python, typescript
Research direction
Start with the listed generic engine changes in tools/src and their tests, especially Raw IR input, pre-parsed specs, and floordiv/floormod; review the existing lemmascript-crosscheck approach for differential validation. The proposal asks maintainers to decide whether the interface and type format are acceptable and how to split the work. Done means an agreed interface with tests preserving TypeScript behavior and verifying the new Raw IR and operators.
Written by the indexing model from the issue text.
Description
Hi! I've been building a Python frontend for LemmaScript as a personal project, in a fork: https://github.com/tsakman23/lemmascript-python (branch feat/python-frontend). Fully type-annotated Python with #@ contracts is extracted to Raw IR and goes through your engine and Dafny backend unchanged:
def clamp(x: int, lo: int, hi: int) -> int:
#@ requires lo <= hi
#@ ensures lo <= result <= hi
if x < lo:
return lo
if x > hi:
return hi
return x
lsc check --backend=dafny clamp.py works like it does for a .ts file: it generates .dfy.gen, enforces the additions-only proof, and verifies.
Where it is. The first milestone covers int/bool functions with requires/ensures, if/elif, locals, // and %, raise/assert (modelled as unreachable, like throw), and Python truthiness. Anything outside that subset is rejected with a located error rather than approximated. Lists, loops and recursion are next.
Python semantics, not JS. The frontend lowers each construct to Raw IR whose engine meaning matches Python's. For example, truthiness is made explicit and comparison chains become conjunctions. Contracts are parsed with Python's ast and lowered by the same code as the body, so % in an ensures means Python's %.
Checking the translation. A differential harness compiles the verified .dfy with dafny translate py and runs it against the original Python on Hypothesis inputs. It checks four things:
- the Python and Dafny readings of
requiresagree; - the real code never raises inside the precondition;
- the two results are equal;
- every
ensuresholds on the real Python result.
It follows the approach of lemmascript-crosscheck. Deliberately wrong models are caught, for example, a model that uses Dafny's Euclidean % fails at a=1, b=-2.
The engine changes. These are generic, about 340 lines in tools/src plus tests, each marked HOOK(external-frontend):
- Raw IR input.
lsc gen|check|regen|extract|infoaccepts a versioned Raw IR envelope:foo.rawir.json, or any extension mapped to a frontend command in a newfrontendskey inlemmascript.json.- The envelope carries what
//@ option///@ backendlines provide for TS sources. - TS behaviour is unchanged: every example regenerates byte-identically and the fixture suite passes.
- Pre-parsed specs. Spec fields (
requires,ensures, invariants, …) may hold an already-parsedRawExpras well as annotation text, so a frontend can use its own expression grammar. floordiv/floormodbinops. These lower to aFloorDiv/FloorModpreamble pair built on Dafny's Euclidean ops.FloorModensuresa == b * FloorDiv(a, b) + FloorMod(a, b), which keeps proofs linear.
Questions:
- Would you be open to an external-frontend interface along these lines? If so, would you prefer the three pieces as separate PRs? I'm happy to reshape the envelope or the config key to fit your plans.
- Raw IR types are TS type strings today, and the Python frontend translates its annotations into them (
list[int]→int[]). Is that a format you're comfortable keeping stable for external producers, or is a structured type representation planned? - Once there's a stable interface, the plan is to move the Python frontend to its own repo that depends on the
lemmascriptnpm package. Does that match how you'd like satellites to work?
Thanks for LemmaScript. The engine was a pleasure to build on. Also many thanks to my colleague @quangng2000 for introducing me to LemmaScript.
- Dominant language
- TypeScript
- Stars
- 113
- Forks
- 13
- Avg merge
- 15h 35m
- Merged PRs (30d)
- 56
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from midspiral/LemmaScript
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
midspiral/LemmaScript#355 · 1 comment ·
Maintainers usually reply within 1 day
-
In a pure function body, a `const` declared inside an `if` branch stays in scope after the `if`, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
midspiral/LemmaScript#362 ·
Maintainers usually reply within 1 day
-
A `const` or `let` inside a bare `{ }` block stays in scope after the block, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 15/100
midspiral/LemmaScript#361 ·
Maintainers usually reply within 1 day
-
A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 18/100
midspiral/LemmaScript#360 ·
Maintainers usually reply within 1 day
-
An assignment used as an expression, such as `(x = 7);` or `const y = (x = 7)`, is extracted as an equality test, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 3/5 Half a day Newbie friendliness 30/100
midspiral/LemmaScript#356 · 1 reaction ·
Maintainers usually reply within 1 day
All issues in midspiral/LemmaScript
Similar issues
-
bug done
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neavo/LinguaGacha#1124 ·
Maintainers usually reply within 1 day
-
[fullsend] E2E: backstage-microsoft-auth — catalogQuery missing error handler for network errorsPossibly taken @fullsend-ai-coder claimed this today. Opene2e-failure ready-to-code
Difficulty 2/5 Under an hour Newbie friendliness 78/100
redhat-developer/rhdh-plugin-export-overlays#4821 · 1 comment ·
Maintainers usually reply within 1 day
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
brylie/mission-island#225 ·
Maintainers usually reply within 1 day
-
status/need-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
google-gemini/gemini-cli#30412 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day