parser: let a parameter destructure a tuple - #38
Merged
Conversation
`fun (t, sq) -> …` did not parse, so anything folding over pairs needed a named helper wrapping a match: five such one-line functions in the dogfooded game, existing only to unpack a tuple. `Param` now carries the pattern it binds rather than a name, and the parser admits the shapes that cannot fail: a name, `_`, or a tuple of those, nested. Refutable shapes are rejected where they are written, naming what was seen, since a parameter has nowhere to fail to. Only a parenthesized parameter takes the pattern grammar, so `let f Some x = …` still means two parameters rather than becoming one constructor pattern. `_` as a parameter is new in the same change: the lexer's `_` was never an identifier, so it could not be written before. Each phase reuses what it already had. Typing binds the parameter's pattern through `bind_pattern`, the same path a match arm takes. Lowering gives a destructuring parameter a synthetic argument name and unpacks it on the body's first line, after any `global`/`nonlocal` declarations, one statement per nesting level (`PyStmt::UnpackAssign`); Python 3 removed tuple parameters, and a Python lambda cannot hold a statement, so a destructuring lambda takes the named-def path however simple its body is. A lone `_` parameter keeps Python's own throwaway name. The LSP walks the pattern for binder spans, so hover and go-to-definition land on the element under the cursor. The fold-to-loop pass rejects a folder with a destructuring parameter: it rewrites by substituting parameter names, and there is no single name to substitute, so such a fold falls through to the byte-identical `_pf_fold` lowering.
This was referenced Jul 31, 2026
Merged
simontreanor
added a commit
that referenced
this pull request
Jul 31, 2026
Two dogfooding reports from real programs, and the standard-library sweep they triggered. Language: * a `type` declaration can name an imported type, bare or module-qualified (#36) — the one gap that changed a program's architecture rather than its phrasing, forcing two modules into one file * field access resolves from the base's type when it is known, so two records may share a field name without prefixes (#37) * parameters destructure: tuples (#38), records (#40), and `_` * a direct self tail call lowers to a loop, so an interactive turn loop no longer walks the stack (#39, #41) Standard library — about 115 new members, taking every module to the F# core set: List (#42), Seq (#44), Set and Map (#46), String (#47), Option and Result (#48), then a member-by-member FSharp.Core audit (#51). Every built-in member now carries a one-line description and its complexity in hover and completion (#43, #49), enforced by tests. Fixes: * `pyfun run` on a single file gives the program its own stdin, so an interactive program is runnable by the command whose job is running programs (#35) * a partially applied lambda closes over its argument instead of being wrapped, so `List.map ((+) 2)` emits `lambda b: 2 + b` (#52) * every multi-argument callback's scheme put the effect variable on the wrong arrows, so `List.fold` could never accept an effectful folder (#51) * `Seq.empty` lowered to a bare `iter()`, a TypeError (#51) One source-incompatible change, which is why this is 0.4.0 and not 0.3.1: a dotted `extern` target whose module prefix cannot be decided from the text is now a compile error naming the `extern import` to add (#50). `sys.stdout.flush` used to emit `import sys.stdout` and fail at runtime; declaring `extern import sys` fixes it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dogfooding finding #4 (see #34).
fun (t, sq) -> …did not parse, so anything folding over pairs needed a named helper wrapping amatch. The dogfooded game had five such one-line functions existing only to unpack a tuple.Paramnow carries the pattern it binds rather than a name, and the parser admits the shapes that cannot fail to match: a name,_, or a tuple of those, nested. Refutable shapes are rejected where written, naming what was seen (a constructor pattern,a record pattern, …), since a parameter has nowhere to fail to.Two decisions worth review:
let f Some x = …keeps meaning two parameters instead of silently becoming one constructor pattern. Test pins this._as a parameter comes along for free and is new: the lexer's_was never an identifier, so it could not be written before. A lone_parameter emits as Python's own_; a second one takes a synthetic name, since duplicate argument names are a SyntaxError.Each phase reuses existing machinery: typing binds the pattern through
bind_pattern(the match-arm path), the LSP walks it throughpattern_varsfor binder spans, and lowering unpacks it on the body's first line, after anyglobal/nonlocaldeclarations, one statement per nesting level via a newPyStmt::UnpackAssign:Python 3 removed tuple parameters and a Python lambda cannot hold a statement, so a destructuring lambda lowers to a named
defhowever simple its body is. Caught that one from wrong emitted code (lambda acc, _pf_arg1: acc + a * b, withaandbfree) rather than from a test, so there is now a test asserting nolambdais emitted for that shape.Soundness note: the fold-to-loop pass rejects a folder with a destructuring parameter. It rewrites by substituting parameter names and there is no single name to substitute, so such a fold falls through to the byte-identical
_pf_foldlowering.Not covered, deliberately: record patterns in parameters (
fun (Cell { letter }) -> …). They are irrefutable and would fit, but they need attribute-reading lowering rather than tuple unpacking, and #37 (field access by the base's type) removes most of the pressure for them. Happy to add if you'd rather.Tests: 8 roundtrip programs, 7 rejection cases plus the
let f Some xshape, 4 typecheck cases, 4 lowering assertions and an e2e round trip. Full suite, clippy and fmt clean.