Skip to content

Commit 5af3b88

Browse files
cknittclaude
andcommitted
Say precisely when literals use an if and what folds
Rule 7 counts the literals that get a case, as the code does, and rule 11 names the values constant folding sees: literals and blocks of constants. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent c549460 commit 5af3b88

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ constructor:
278278
| `@as(null) LitNull \| AnyObject({x: int})` | `LitNull` | `LitNull` |
279279

280280
The literals are compared with `===`: by one `switch (x)` over the literal
281-
arms, whose default holds the payload dispatch, or, with at most two
282-
literals, by an `if` per arm body. A literal without an arm of its own goes
281+
arms, whose default holds the payload dispatch, or, when at most two
282+
literals get a `case`, by an `if` per arm body. A literal without an arm of its own goes
283283
to the default arm. It gets a `case` leading there when it could pass a
284284
check of the payload dispatch (a string literal beside a `string` arm, any
285285
literal beside an unknown-kind arm) or when the variant has class cases;
@@ -396,9 +396,11 @@ primitives of the variant never reach it.
396396

397397
### 11. Constant folding
398398

399-
A `switch` on a constant untagged value folds to the arm that the run-time
400-
dispatch selects for the constant's JavaScript value, so `S("a")` matched
401-
against `@as("a") A | S(string)` folds to `A`.
399+
A `switch` on an untagged value the compiler knows, a literal or a block of
400+
constants, folds to the arm that the run-time dispatch selects for its
401+
JavaScript value, so `S("a")` matched against `@as("a") A | S(string)` folds
402+
to `A`. A value built at run time, such as `A([1])` with its array, is
403+
matched at run time.
402404

403405
- Implementation: `lambda.ml:switch` (`untagged_action`, `literal_denotes_same`).
404406
- Tests: `tests/tests/src/unboxed_variant_fold_test.res`.

0 commit comments

Comments
 (0)