new if control for seq #430
Labels
No labels
bug
dependencies
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
refactor
rust
technical-debt
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
navicore/patch-seq#430
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
RFC: Replace
if/else/thenSyntax with Quotation-Based CombinatorsBackground
The language currently uses Forth-style control flow:
Example:
This works, but feels inconsistent with the rest of the language, which already
has first-class quotations, closures, and row-polymorphic stack effects.
Proposal
Remove
if,else, andthenas parser-level syntax. Replace them with asingle ordinary word:
ifbecomes a normal combinator that takes three things off the stack: aboolean and two quotations. No new syntax, no parser changes — the existing
quotation and row-polymorphism machinery already knows how to handle it.
The
absexample becomes:Or, with a
whencombinator (see below):Why This Fits the Language
It's the natural payoff of row polymorphism
The signature
( ..a Bool [ ..a -- ..b ] [ ..a -- ..b ] -- ..b )is onlyexpressible because
..aand..bexist. In a parametric-only system youcouldn't type
ifprecisely — you'd lose the guarantee that both branchesagree on stack effect.
Adopting quotation-based control flow cashes in a type-system feature the
language already has.
It unifies "control flow" with "everything else"
Loops (
while,until,times), error handling (recover), iteration(
each,map,filter), and conditionals all become combinators with thesame shape. Users learn one pattern instead of N special forms.
It composes
[ ... ] [ ... ] ifis a value. It can be stored, returned from a word, builtdynamically, passed to higher-order combinators. Forth-style
ifcannot doany of that.
Derived Combinators (Library, Not Syntax)
Once
ifis a word, a family of related combinators becomes ordinary librarycode:
Users can write their own. The parser doesn't need to know about any of them.
Translation From Existing Code
The migration is mechanical and local:
A migration tool can do this transformation automatically. Meeting existing
use cases isn't a hope — it's guaranteed by construction.
Nested Control Flow: A Concrete Win
The strongest argument for the change is how it handles deep nesting.
Today's pain
Five-deep nesting accumulates
thenterminators at the bottom. They carry noinformation — they're parser bookkeeping. You have to count them to know
you've closed the right number of blocks, and a missing or extra
thenproduces an error far from the actual mistake.
With quotations
The
]characters carry the same bookkeeping thethens did, but they pairvisually with their opening
[. Editors, linters, and humans all track thenesting structure for free. No counting.
Nesting often becomes unnecessary
Forth-style syntax forces nesting because
ifis a statement. Withcombinators, deeply nested decisions frequently flatten into a
cond:A lot of code that looks like it needs nested
ifs is really a flatdispatch in disguise. Forth-style syntax hides that because there's no
convenient way to express it.
Branches can also be named and reused, since they're values:
The factoring options Forth-style denies — naming a branch, reusing a branch,
building a branch dynamically — are exactly the tools needed to flatten deep
nesting. Quotations provide them.
Costs
To be honest about the tradeoffs:
migration tool.
cond [ A ] [ ] ifis a fewcharacters longer than
cond if A then. Thewhencombinator recoversmost of that.
[ ]for "do nothing" is a little ugly. Anoporidword can help; some languages just let[ ]stand.ifcompiles to a directconditional jump trivially. Quotation-based
ifproduces identical codeif the compiler inlines literal quotations at the call site — a standard
optimization (Factor does it). The cost only appears when the quotations
are dynamic, and in that case the feature didn't exist in the old syntax
at all.
Prior Art
Factor's
kernelandcombinatorsvocabularies are the canonical referencefor how far this style scales. Slava Pestov's writing on Factor's design
covers the rationale for quotations-over-syntax in depth. Joy is the original
quotation-based concatenative language and worth reading for the minimalist
take on the same idea.
https://github.com/navicore/patch-seq/pull/431