Both sides had independently fixed the duplicate-declaration build break
(Basic.lean and Selective.lean each declared Parser, ParseResult, ParseError
and Location, and the root module imported both). Master resolved it by
gutting Selective.lean down to the class; this branch had resolved it the
other way, keeping an error-parameterized `Parser e t`.
Keep master's `Parser t` with the `parse` field: `ParseError` stays baked in,
which is simpler and matches Parsley, whose `Parsley[A]` has no error type
parameter. Drop the `Parser e t` variant entirely.
Carry over onto it the fixes that master does not have:
- The Monad instance's `pure` returned an empty remainder, discarding the
rest of the input. Thread the input through unchanged, as the monad laws
require and as the proof work will need.
- Give Selective's `select`/`branch` explicit type variables. They were
unbound identifiers, so Lean auto-bound them as parameters of the class
itself rather than as per-method implicits.
- Add the Selective instance for Parser, and a `branch` default in terms of
`select`, so instances need only supply `select`.
- Restore `expect`, which had been commented out when the types changed.
Implement `lift2` as `f <$> zip p1 p2`; it was a stub returning
`ParseResult.ok ("", "")` at type `Parser γ`, which did not compile, so
master's HEAD was red.
Bump the toolchain 4.29.1 -> 4.33.0; the slice API needed no changes.
Add CLAUDE.md describing the project's four stages and this state.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GPcEywkQGRifaT1VFXNRBL
5.7 KiB
CLAUDE.md
What this project is
A four-stage project, currently at stage 1:
- Understand and port Parsley. Reimplement the Parsley parser combinator library in Lean 4. Parsley is the reference design; when in doubt about naming, types, or backtracking semantics, match Parsley.
- Prove correctness properties of the combinator framework in Lean —
functor/applicative/monad/alternative laws, and parser-level properties
(e.g. failure without consumption,
manytermination, error-position correctness). - Expose the framework over the C ABI using Lean's FFI, so it can be linked as a library by any C-ABI-speaking language.
- Exercise it from Rust, via the Rust crate at the repo root.
Stages 2–4 do not exist yet. Do not assume infrastructure for them is present.
Layout
lean-parser-generator/ Lean 4 package (lake) — the real work
LeanParserGenerator.lean root module; imports Basic, Json, Selective
LeanParserGenerator/
Basic.lean the Parser type, Monad instance, primitives
Selective.lean Selective class + instance for Parser
Json.lean JSON parser sketch, currently commented out
Main.lean stub executable
justfile `just run`
src/ Rust crate (`parser-generator`)
lr.rs LR-item/grammar experiment using chumsky
main.rs stub
The Rust side is currently unrelated exploratory work — hand-rolled
LR(1) item/grammar structures parsed with chumsky. It predates the Lean
effort. Its eventual role is to be the FFI consumer in stage 4; it is not
that yet, so don't treat src/lr.rs as the target of the Lean work.
Build and run
# Lean
cd lean-parser-generator && lake build
cd lean-parser-generator && lake env lean LeanParserGenerator/Basic.lean # check one module
lake exe lean-parser-generator # or: just run
# Rust
cargo build
cargo test
There are no tests anywhere yet, in either language.
Stay on the latest Lean. This project deliberately tracks the newest
stable Lean 4 release rather than pinning to an old one — it leans on
recently-added stdlib API (String.Slice), and the proof work in stage 2
benefits from current tactics and Std/Mathlib compatibility. When a new
release lands, bump lean-toolchain, rebuild, and fix the fallout as part
of normal maintenance; don't let the pin drift. Currently on
leanprover/lean4:v4.33.0.
Bumping the pin to a toolchain elan hasn't installed triggers a large
download on the next lake build — mention it before kicking one off.
State of the Lean code — read before editing
There is exactly one Parser type, in Basic.lean:
structure Parser (t: Type) where
parse : String.Slice -> ParseResult t
ParseError is baked into ParseResult rather than being a type parameter.
This mirrors Parsley, whose Parsley[A] carries no error parameter. It was
also arrived at twice independently, so treat it as settled: if a combinator
seems to want a different representation, change this one rather than
introducing a second alongside it.
Module roles:
Basic.lean— theParsertype, itsMonadinstance,ToStringinstances forLocationandParseResult, and the combinators so far:satisfy,char,item,zip,lift2,expect.Selective.lean— theSelectiveclass and itsParserinstance.branchhas a default implementation in terms ofselect, so instances only need to supplyselect.Json.lean— a JSON parser meant as the driving example. Everything except theJsonValuedatatype is commented out; it has not been brought back in line with the currentParsertype.
lake build succeeds. It prints one info line — the #eval of zip in
Basic.lean — which is a deliberate scratch check, not a warning.
Prefer the Functor/Monad instances (<$>, >>=, do-notation) over adding
free-standing map/pure functions. Earlier revisions had both, which made
unqualified pure ambiguous with Pure.pure.
Not yet implemented, roughly in the order Parsley introduces them:
Alternative (<|>) and atomic, lookAhead/notFollowedBy,
many/some/option, the remaining sequencing operators (~>, <~,
<::>), and as/#>. Real source positions are also missing — every error
is constructed with location (0, 0).
Conventions
- Parsers consume
String.Slice, notString— that's the Lean 4.29+ slice API (front?,drop,take,startsWith,dropPrefix,dropWhile,positions). Keep using it; it's what makes non-copying backtracking cheap. - Prefer typeclass instances (
Functor,Applicative,Monad,Alternative,Selective) over free-standingmap/purefunctions, so combinators compose through the standard machinery and the laws have something to be stated about. - Parsley's
<|>does not backtrack when the left branch consumed input; backtracking is opt-in viaatomic. Preserve that semantics — it's load bearing for error quality, and it's one of the properties worth proving. - Parsley favours applicative/selective composition over monadic bind. Follow
that: reach for
lift2/<*>/selectfirst and keepbindfor cases that genuinely need a value-dependent parser. sorryis acceptable as a placeholder here, but flag every one you leave.
Things to avoid
- Don't add FFI (
@[export],extern "C",lean_object*plumbing) or abuild.rsuntil stage 3 is actually being worked on. - Don't grow
src/lr.rs. The LR generator is a separate experiment; the project direction is combinators, not table generation.