A Python frontend for LemmaScript: proposing a small external-frontend interface

Open
#228 2 comments 1 reaction 0 assignees View on GitHub

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 requires agree;
  • the real code never raises inside the precondition;
  • the two results are equal;
  • every ensures holds 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):

  1. Raw IR input.
    • lsc gen|check|regen|extract|info accepts a versioned Raw IR envelope: foo.rawir.json, or any extension mapped to a frontend command in a new frontends key in lemmascript.json.
    • The envelope carries what //@ option / //@ backend lines provide for TS sources.
    • TS behaviour is unchanged: every example regenerates byte-identically and the fixture suite passes.
  2. Pre-parsed specs. Spec fields (requires, ensures, invariants, …) may hold an already-parsed RawExpr as well as annotation text, so a frontend can use its own expression grammar.
  3. floordiv / floormod binops. These lower to a FloorDiv/FloorMod preamble pair built on Dafny's Euclidean ops. FloorMod ensures a == b * FloorDiv(a, b) + FloorMod(a, b), which keeps proofs linear.

Questions:

  1. 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.
  2. 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?
  3. Once there's a stable interface, the plan is to move the Python frontend to its own repo that depends on the lemmascript npm 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from midspiral/LemmaScript

All issues in midspiral/LemmaScript

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.