| @@ -0,0 +1,127 @@ |
| 1 | +# ADR 0061: Parens-free functions: application by juxtaposition, currying, labelled arguments |
| 2 | + |
| 3 | +**Status:** Proposed (2026-10-23, issue #314); needs engineering review (#44). The largest breaking change the language has taken: every function definition and call site changes shape, and the std, packages, examples and tests are rewritten by a tool, not by hand. |
| 4 | + |
| 5 | +## Context |
| 6 | + |
| 7 | +Louise's `fn f(a: Int, b: Int)` / `f(a, b)` is the C/Rust/Gleam shape. It costs: |
| 8 | + |
| 9 | +- **No partial application.** The only mechanisms are the one-`_` capture (`f(a, _)`, ADR 0023) and eta-expansion of a named function used as a value. `list.map(xs, f)` works; `list.map(xs, f(a))`-style partials do not exist. |
| 10 | +- **A grammar wart that formatting cannot fix.** A call's `(` must sit on the callee's line (`postfix`, `grammar/expr.rs`), because `f\n(x)` is two statements. ADR 0059 Revision 3 wanted ormolu's "a broken group moves under its head" everywhere and could not have it for calls; `f( a\n, b\n)` hugs in place instead. |
| 11 | +- **`x |> f(a)` is a HIR desugar with its own rules** (ADR 0023), because application and piping are different syntaxes for "give f more arguments". |
| 12 | + |
| 13 | +OCaml, Haskell and F# show the alternative: definitions and calls by juxtaposition — `fn add a b { a + b }`, `add a b` — with every function curried: `add : fn(Int) -> fn(Int) -> Int`, and `add 1` is a function. ADR 0018 already allows *type*-level partial application; this is the value-level mirror. |
| 14 | + |
| 15 | +## Decision |
| 16 | + |
| 17 | +### Definitions |
| 18 | + |
| 19 | +Parameters become space-separated patterns; the parentheses go: |
| 20 | + |
| 21 | +```text |
| 22 | +fn add a b { a + b } |
| 23 | + |
| 24 | +fn on_message (State(..) as state) msg -> Next<State> / <store_fx> { .. } |
| 25 | + |
| 26 | +fn k_integrate ~weight:(f: fn(Float, Bool) -> Float / <ffi>) from: Float to: Float |
| 27 | + -> Float / <ffi> { .. } |
| 28 | +``` |
| 29 | + |
| 30 | +A parameter is a pattern (as today) with an optional `: T`; parentheses are needed only to group a pattern. Lambdas, effect operations, handler clauses, trait and impl methods share the shape: `fn log (msg: String) -> ()`, `log m -> resume(m)`, `fn first c { .. }`. A `fn` with no parameters is `fn main { .. }` — and `main` the *value* is the same name, so `fn f` in signature position is the zero-parameter function (its type is `fn() -> T`; see §Types). |
| 31 | + |
| 32 | +### Application |
| 33 | + |
| 34 | +**Application is juxtaposition, left-associative, binding tighter than every infix operator:** `f a b` is `(f a) b`; `f a + g b` is `(f a) + (g b)`. An argument is an *atom*: a path (`x`, `list.map`), a literal, a list or tuple or parenthesised expression, a `fn` lambda, a tagged template (`` f pug`..` ``), an `if`/`match`/`handle`/`region` expression, a labelled argument (§Labels). Not atoms: a prefix-operator expression (parenthesise: `f (-1)`, `f (!ready)`), a `{` block (parenthesise; `if c { .. }` must not read its block as an argument), an HTML literal (parenthesise). |
| 35 | + |
| 36 | +**Line breaks:** an application continues onto a following line that starts with an atom and is *more indented* than the statement it belongs to; a line at the statement's indent starts a new statement. This is the fifth newline rule, beside the four of `docs/reference/grammar.md` (HTML at a line start, `return` taking the rest of its line, a constructor's `where` ending at end of line, and the call-parenthesis rule — which dies here). A line starting with an infix operator still continues an infix chain (the leading-operator rule wins over application: operators are not atoms). A match arm's body is greedy to the end of its line: `Some(v) -> f v,` is a call (today it is an error). |
| 37 | + |
| 38 | +```text |
| 39 | +let req = |
| 40 | + request |
| 41 | + "GET" |
| 42 | + "/" |
| 43 | + headers |
| 44 | +``` |
| 45 | + |
| 46 | +The formatter gains the under-form for application that ADR 0059 Revision 3 reserved for this change (`f\n a\n b`), and `Revision 3`'s call exception is retired. |
| 47 | + |
| 48 | +### Currying |
| 49 | + |
| 50 | +Every function type is a chain of one-parameter arrows. Too few arguments is not an error: it is a function. The exact-arity error (E0205) shrinks to *too many* arguments (applying a non-function) and to arity mismatches between a trait and its impl. |
| 51 | + |
| 52 | +```text |
| 53 | +let add2 = add 2 // fn(Int) -> Int |
| 54 | +let render = render_page ~theme:dark // labels partially applied by name |
| 55 | +``` |
| 56 | + |
| 57 | +At runtime the semantics is curried but the implementation is n-ary where visible: a full application of a known callee calls the n-ary body directly (the compiler knows arities; `arity_of` and the wrapper machinery exist), and only genuinely partial applications build closures. The uniform ABI (docs/runtime-abi.md) stays the fallback; ADR 0046's exact-arity experiment stays unlanded — currying does not revive it. |
| 58 | + |
| 59 | +### Labels |
| 60 | + |
| 61 | +Labels move into the function's *type*, OCaml-style, spelled `~name` (fork (a), issue #314): |
| 62 | + |
| 63 | +```text |
| 64 | +fn insert ~into:(table: String) ~key:(k: String) value: String -> () / <io> |
| 65 | + |
| 66 | +insert ~into: "users" ~key: "ada" "lovelace" |
| 67 | +insert ~key: "ada" ~into: "users" "lovelace" // same call |
| 68 | +``` |
| 69 | + |
| 70 | +- In a type, a labelled arrow is `fn(~name: T) -> ..`; the label is part of the type, so partial application by name is well-defined in any order. |
| 71 | +- `~name` puns `~name:name` at both definition and call; `~name:(p: T)` renames the binding. |
| 72 | +- Unlabelled arguments fill the remaining *unlabelled* parameters in order — today's "unlabelled fills the next free parameter" (issue #161), per atom instead of per slot list. |
| 73 | +- Constructor *declarations* keep `field: T` (`:` stays the of-type sigil); construction and record update use `~` at use sites: `User(~name: "ada", ~hash: h)`, `User(..base, ~name: "x")`. Constructor patterns likewise: `User(~name: n, ..)`. |
| 74 | +- HTML components are constructor-like, not applications: `<C prop={v}/>` is unchanged. |
| 75 | + |
| 76 | +### Effects |
| 77 | + |
| 78 | +**An effect row lives on the last arrow of a chain** (fork (b)): `fn f a b / <e>` is `fn(a) -> fn(b) -> T / <e>`. Effects are performed at full application; a partial application's type has empty rows up to the arrow that does the work. A *returned* closure keeps its own row with parentheses: `fn(a) -> (fn(b) -> c / <e>)`. Capabilities (ADR 0010) bind at the arrow whose row names them — already per-arrow machinery. A mid-chain row (`fn(a) / <io> -> fn(b) -> c`) is not expressible; revisit when a use case exists. |
| 79 | + |
| 80 | +### Constructors, externs, traits |
| 81 | + |
| 82 | +- **Constructors stay n-ary.** `User(~name: "ada", ~hash: h)` is construction syntax, not application; there is no partial construction (OCaml's `C of a * b` precedent). A constructor used as a value still eta-expands (`stub`). |
| 83 | +- **Externs stay uncurried at the ABI** (`Value::CallExtern`, ADR 0005): their types are curried like everyone else's; a full application (the norm) lowers to the direct C call, a partial one (rare) to the existing wrapper path. |
| 84 | +- **Trait dictionaries and effect capabilities** are consumed at the arrow that needs them: a method's dictionary at its first application; capabilities at the arrow whose row names them. |
| 85 | + |
| 86 | +### Pipes, placeholders, `use` |
| 87 | + |
| 88 | +- `x |> f a b` keeps Louise's x-first reading via the HIR desugar: it is `f x a b`. No arity-dependence (ADR 0023's objection is moot: the desugar is syntactic). |
| 89 | +- `_` stays as the explicit eta-hole for skipped positions: `f a _ b` is `fn(x) { f a x b }`. One `_` per application chain; still not in `use` (E0116). Partial application covers what most captures served. |
| 90 | +- `use a <- f x` appends the continuation as the final argument: `f x (fn a { rest })`. Unchanged in spirit (ADR 0001), simpler in mechanism: it is just application. |
| 91 | + |
| 92 | +### Types |
| 93 | + |
| 94 | +Canonical function types are curried chains; `fn(a, b) -> c / <e>` remains as sugar for `fn(a) -> fn(b) -> c / <e>` (signatures stay short; the row lands on the last arrow either way). `send fn(a) -> b` is the capture-rows form (ADR 0004 rev. 6); a partially applied `send fn` captures the supplied arguments in the inner arrow's capture row. |
| 95 | + |
| 96 | +### What does not change |
| 97 | + |
| 98 | +`if`/`match`/`handle`/`region`/`block`/`return`, patterns (paren-grouped constructor patterns included), the `use` keyword's shape, HTML literals, templates and their adjacency rule (ADR 0048), imports, and everything about types other than `fn` types and labels. |
| 99 | + |
| 100 | +## Migration |
| 101 | + |
| 102 | +**Flag day, with a tool (fork (d)).** One branch changes the grammar; a `louise migrate` subcommand parses the old syntax (the pre-change parser kept as a frozen grammar) and prints the new, built on the formatter's infrastructure — the same play as ADR 0059's repository reformats, larger. std, packages, examples and tests are migrated in the branch; users (toki2) run the same tool. A dual-syntax period was rejected: `f(a, b)` is a call today and a tuple application tomorrow, and adjacency (`f (a, b)`) is too subtle a line to live on for a release. |
| 103 | + |
| 104 | +The compiler's own parse of std keeps `louise check` honest at every step; the UI suite's markers pin the diagnostics' new shapes. |
| 105 | + |
| 106 | +## Consequences |
| 107 | + |
| 108 | +- Every `.lse` file changes; the migration tool, not hands, rewrites them. Review is by diff-stats and the test suite. |
| 109 | +- ADR 0059 gains Revision 4 (application style); Revision 3's call exception is deleted. |
| 110 | +- ADR 0023's rejections are re-read: partial application exists now, so the one-`_` capture is the explicit form, not the only form. |
| 111 | +- The type checker's application rule becomes the binary rule; `order_args`'s slot-filling becomes per-atom label matching on the curried type; "too few arguments" stops being an error class. |
| 112 | +- The formatter's `under` already expresses the new shapes; tree-sitter needs a new scanner rule for indented-atom continuation (its `_call_lparen` token dies with the parentheses). |
| 113 | +- Issue #312's class of confusion (where an argument lands) is unaffected; the `use` continuation is still the final argument. |
| 114 | + |
| 115 | +## Alternatives considered |
| 116 | + |
| 117 | +- **Keep the parentheses.** The costs in §Context stay; the formatter's call compromise stays; partial application stays second-class. Rejected: the language is pre-1.0, and this is the time. |
| 118 | +- **Haskell-style: no labels.** Labels are load-bearing (records, components, `use`'s callback, issue #161). Rejected. |
| 119 | +- **F#-style pipes (`x |> f a` = `f a x`).** Louise's pipe is x-first today and code reads `x |> list.map f` as "x, then map with f"; flipping the argument side breaks every pipe silently. Rejected. |
| 120 | +- **Dual-syntax transition.** §Migration: `f(a, b)` collides with tuple application. Rejected. |
| 121 | + |
| 122 | +## Open for review |
| 123 | + |
| 124 | +- The indentation rule's exact statement (what counts as "the statement's indent" inside match arms, handler clauses and `use` bodies), and its tree-sitter scanner twin. |
| 125 | +- Whether `fn f` with zero parameters and `f` the value read well enough, or zero-parameter definitions keep `fn f()`. |
| 126 | +- `~` in constructor patterns and record update, or `:` kept there as construction-not-application. |
| 127 | +- The migration tool's fidelity on comments and hand formatting (it owes a token check, like the formatter's). |