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

Abierto
#228 2 comentarios 1 reacción 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
python, typescript

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
TypeScript
Estrellas
113
Forks
13
Merge medio
16 h 22 min
PR fusionados (30 d)
40

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de midspiral/LemmaScript

Todos los issues de midspiral/LemmaScript

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.