Skip to content
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

(Un)Checked Evaluation #709

Closed
ChristianGruen opened this issue Sep 17, 2023 · 8 comments
Closed

(Un)Checked Evaluation #709

ChristianGruen opened this issue Sep 17, 2023 · 8 comments
Labels
Discussion A discussion on a general topic. Feature A change that introduces a new feature Propose Closing with No Action The WG should consider closing this issue with no action XPath An issue related to XPath

Comments

@ChristianGruen
Copy link
Contributor

Based on #707 (comment) and the comments following in that thread:

I've been thinking recently about adding checked{} and unchecked{} modes. For example

<xsl:apply-templates select="checked{.//item}"/>

would throw an error if there are no items. The mode of execution would propagate downwards, so checked{a/b/c/d} would be able to tell you that a b was found, but it had no c children.

checked{item[22]} would have the effect of making sequences behave more like arrays, with bound checking; conversely unchecked{item?22} would return an empty sequence instead of throwing an error.

@ChristianGruen ChristianGruen added XPath An issue related to XPath Feature A change that introduces a new feature Discussion A discussion on a general topic. labels Sep 17, 2023
@ChristianGruen
Copy link
Contributor Author

From the XQuery point of view, I think it’s better to avoid introducing new expression for evaluation directives. A workable solution for us was to rely on existing language features (although the syntax could be a bit more compact).

The following example shows how function inlining can be disabled on three levels:

(: 1. globally, applies to the complete module :)
declare option db:inlinelimit '0';

(: 2. function level: apply to the function body :)
declare %db:inlinelimit(0) function local:function() { }

(: 3. locally: apply to the enclosed expression :)
(# db:inlinelimit 0 #) {
  local:function()
}

We could do the same for disabling/enabling checked evaluation in expressions …but I asuume that annotations and/or pragmas would first need to be added to XPath.

@dnovatchev
Copy link
Contributor

dnovatchev commented Sep 17, 2023

We could do the same for disabling/enabling checked evaluation in expressions …but I asuume that annotations and/or pragmas would first need to be added to XPath.

No need for adding anything, as checked / unchecked are a good example of decorators.

This comment contains details: #707 (comment)

@ChristianGruen
Copy link
Contributor Author

No need for adding anything, as checked / unchecked are a good example of decorators.

Feel free to finalize #106

@michaelhkay
Copy link
Contributor

No need for adding anything, as checked / unchecked are a good example of decorators.

I don't think so. Firstly, they apply to expressions rather than functions. Secondly, they have nested scope (could be static or dynamic, but I prefer static) - they apply not only to the immediate expression, but to all subexpressions.

Of course, it would need some experiments to see if this actually works (in the sense of making debugging easier). It's possible that there are too many expressions that deliberately return an empty sequence to make the idea feasible.

@dnovatchev
Copy link
Contributor

dnovatchev commented Sep 17, 2023

No need for adding anything, as checked / unchecked are a good example of decorators.

I don't think so. Firstly, they apply to expressions rather than functions.

As already stated in #707 (comment)

SomeExpr($arg1, $arg2, ..., $argN)

is equivalent to:

fn($arg1, $arg2, ..., $argN) {SomeExpr($arg1, $arg2, ..., $argN)}

And a decorator can be applied to any function ( in this case equivalent to the expression)

Secondly, they have nested scope (could be static or dynamic, but I prefer static) - they apply not only to the immediate expression, but to all subexpressions.

Of course, it would need some experiments to see if this actually works (in the sense of making debugging easier). It's possible that there are too many expressions that deliberately return an empty sequence to make the idea feasible.

As already pointed out in #707 , relying on the empty sequence in general to denote some special/error value is wrong, because the empty sequence is a legitimate value returned by a map or array lookup.

So much for "they apply to all subexpressions".

Excluding from "all subexpressions" all subexpressions that don't contain any array or map lookup, may seem useful, but to me needing such a "feature" clearly shows that something is wrong with the language itself.

As this issue was originally raised for maps, the most natural and elegant solution I am aware of is setting the value of the default-key of the map (#105) to whatever best would represent "empty" (or "no result"), for example:

let $price-for-night := map {
1 : 100,
2 : 90,
3 : 80
'\' : ()
}
  return
    $price-for-night(10)

for hotel nightly prices, where people are allowed to stay at the hotel for no more than 3 nights.

Even in this particular example, a better value for the \ key would best be: xs:double("INF")

And an expression may contain lookups on different maps for which we may want to have different default values returned.

Thus, again the "one size fits all" approach is not applicable in this general case.

@dnovatchev
Copy link
Contributor

As for applying a decorator to "an expression and all its subexpression", this can be done by decorating the expression-evaluator itself, and returning a new expression evaluator function that instead of raising an error in particular situations, returns a "special value" instead.

Even if we do not introduce Decorators in the language, the fact remains that the proposed (Un)Checked Evaluation is equivalent to such decorator.

@ChristianGruen
Copy link
Contributor Author

I propose to close this one; I think it the vast majority would rarely use it in practice.

@ChristianGruen ChristianGruen added the Propose Closing with No Action The WG should consider closing this issue with no action label Feb 13, 2024
@ndw
Copy link
Contributor

ndw commented Feb 21, 2024

The CG agreed to close this issue without action at meeting 066

@ndw ndw closed this as completed Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Discussion A discussion on a general topic. Feature A change that introduces a new feature Propose Closing with No Action The WG should consider closing this issue with no action XPath An issue related to XPath
Projects
None yet
Development

No branches or pull requests

4 participants