Skip to content

parser: let a parameter destructure a tuple - #38

Merged
simontreanor merged 1 commit into
mainfrom
feat/parameter-patterns
Jul 31, 2026
Merged

parser: let a parameter destructure a tuple#38
simontreanor merged 1 commit into
mainfrom
feat/parameter-patterns

Conversation

@simontreanor

Copy link
Copy Markdown
Owner

Dogfooding finding #4 (see #34).

fun (t, sq) -> … did not parse, so anything folding over pairs needed a named helper wrapping a match. The dogfooded game had five such one-line functions 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 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:

  • Only a parenthesized parameter takes the pattern grammar. A bare uppercase identifier stays a parameter name, so 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 through pattern_vars for binder spans, and lowering unpacks it on the body's first line, after any global/nonlocal declarations, one statement per nesting level via a new PyStmt::UnpackAssign:

def tileScore(_pf_arg0):
    t, sq = _pf_arg0
    return t * sq

def nested(_pf_arg0):          # let nested ((a, b), c) = …
    _pf_arg0_0, c = _pf_arg0
    a, b = _pf_arg0_0

Python 3 removed tuple parameters and a Python lambda cannot hold a statement, so a destructuring lambda lowers to a named def however simple its body is. Caught that one from wrong emitted code (lambda acc, _pf_arg1: acc + a * b, with a and b free) rather than from a test, so there is now a test asserting no lambda is 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_fold lowering.

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 x shape, 4 typecheck cases, 4 lowering assertions and an e2e round trip. Full suite, clippy and fmt clean.

`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.
@simontreanor
simontreanor merged commit c708968 into main Jul 31, 2026
11 checks passed
@simontreanor
simontreanor deleted the feat/parameter-patterns branch July 31, 2026 11:22
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant