-
Notifications
You must be signed in to change notification settings - Fork 8
[4/15] Fix result modes in ifelse(), t(), and diag(); enforce ifelse() branch shapes #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
t-kalinowski
merged 5 commits into
t-kalinowski:main
from
mns-nordicals:conformability/04-ifelse-t-diag
Jul 31, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4097552
Promote ifelse() branches and shape the result like test
mns-nordicals 9f554a0
Preserve input mode in t() and diag()
mns-nordicals e1b3e77
Enforce the ifelse() branch-shape contract
mns-nordicals ab11aa0
Test the ifelse() rank-mismatch error, drop an unreachable guard
mns-nordicals 9af5e14
Preserve logical storage through diag()
t-kalinowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,108 @@ | ||
| # r2f-conditionals.R | ||
| # Handlers for vectorized conditionals: ifelse | ||
|
|
||
| # --- Local Helpers --- | ||
|
|
||
| ifelse_branch_shape_msg <- paste0( | ||
| "ifelse() `yes` and `no` must be scalars or match the shape of `test`; ", | ||
| "R-style recycling is not supported" | ||
| ) | ||
|
|
||
| # Three-valued conformability verdict for one axis of an ifelse() branch | ||
| # against `test`: ok+known (no guard), not-ok+known (compile error), or | ||
| # unknown (runtime guard). NA dims are always unknown: two unknown lengths | ||
| # are not the same quantity. | ||
| ifelse_axis_verdict <- function(test_dim, branch_dim) { | ||
| if (is_wholenumber(test_dim) && is_wholenumber(branch_dim)) { | ||
| return(list( | ||
| ok = identical(as.integer(test_dim), as.integer(branch_dim)), | ||
| unknown = FALSE | ||
| )) | ||
| } | ||
| if (!is_scalar_na(test_dim) && !is_scalar_na(branch_dim)) { | ||
| test_norm <- fortranize_expr_symbols(test_dim) | ||
| branch_norm <- fortranize_expr_symbols(branch_dim) | ||
| if (identical(test_norm, branch_norm)) { | ||
| return(list(ok = TRUE, unknown = FALSE)) | ||
| } | ||
| } | ||
| list(ok = TRUE, unknown = TRUE) | ||
| } | ||
|
|
||
| # Enforce the shape contract for one ifelse() branch: scalars broadcast | ||
| # natively; a non-scalar branch must match `test`'s shape, because | ||
| # merge() requires conformable arguments and a runtime mismatch would | ||
| # read past the shorter branch. Statically unequal dims are a compile | ||
| # error; symbolic dims get a statement-level runtime size guard, emitted into | ||
| # `hoist` -- always a live hoist context, since r2f() substitutes a fresh one | ||
| # before dispatching to any handler. | ||
| check_ifelse_branch_shape <- function(branch, mask, hoist, scope) { | ||
| if (passes_as_scalar(branch@value)) { | ||
| return(invisible()) | ||
| } | ||
| if (branch@value@rank != mask@value@rank) { | ||
| stop(ifelse_branch_shape_msg, call. = FALSE) | ||
| } | ||
| unknown_axes <- integer() | ||
| for (axis in seq_len(mask@value@rank)) { | ||
| verdict <- ifelse_axis_verdict( | ||
| dim_or_one(mask, axis), | ||
| dim_or_one(branch, axis) | ||
| ) | ||
| if (!verdict$ok) { | ||
| stop(ifelse_branch_shape_msg, call. = FALSE) | ||
| } | ||
| if (verdict$unknown) { | ||
| unknown_axes <- c(unknown_axes, axis) | ||
| } | ||
| } | ||
| if (!length(unknown_axes)) { | ||
| return(invisible()) | ||
| } | ||
| # size() is an inquiry, so applying it to operand expression text does | ||
| # not evaluate the operands. | ||
| condition <- str_flatten( | ||
| map_chr( | ||
| unknown_axes, | ||
| function(axis) glue("size({branch}, {axis}) /= size({mask}, {axis})") | ||
| ), | ||
| " .or. " | ||
| ) | ||
| emit_quickr_error_if(condition, ifelse_branch_shape_msg, hoist, scope) | ||
| invisible() | ||
| } | ||
|
|
||
| # --- Handlers --- | ||
|
|
||
| r2f_handlers[["ifelse"]] <- function(args, scope, ...) { | ||
| .[mask, tsource, fsource] <- lapply(args, r2f, scope, ...) | ||
| r2f_handlers[["ifelse"]] <- function(args, scope, ..., hoist = NULL) { | ||
| .[mask, tsource, fsource] <- lapply(args, r2f, scope, ..., hoist = hoist) | ||
|
|
||
| # R: the result is shaped like `test` (branches only contribute values). | ||
| # A scalar test with array branches is not representable with merge(). | ||
| if ( | ||
| passes_as_scalar(mask@value) && | ||
| !(passes_as_scalar(tsource@value) && passes_as_scalar(fsource@value)) | ||
| ) { | ||
| stop( | ||
| "ifelse() result takes the shape of `test`; ", | ||
| "array-valued yes/no with scalar test is not supported", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
|
|
||
| # Checked before casts so guards splice the bare operand text. | ||
| check_ifelse_branch_shape(tsource, mask, hoist, scope) | ||
| check_ifelse_branch_shape(fsource, mask, hoist, scope) | ||
|
|
||
| mask <- booleanize_logical_as_int(mask) | ||
| # (tsource, fsource, mask) | ||
| mode <- tsource@value@mode | ||
| dims <- conform(mask@value, tsource@value, fsource@value)@dims | ||
| Fortran(glue("merge({tsource}, {fsource}, {mask})"), Variable(mode, dims)) | ||
|
|
||
| # merge() requires same-typed branches; promote both to their common mode. | ||
| promoted <- promote_operands(list(tsource, fsource), context = "ifelse()") | ||
| .[tsource, fsource] <- promoted$args | ||
| mode <- promoted$mode | ||
|
t-kalinowski marked this conversation as resolved.
|
||
|
|
||
| Fortran( | ||
| glue("merge({tsource}, {fsource}, {mask})"), | ||
| Variable(mode, mask@value@dims) | ||
| ) | ||
| } | ||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.