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

554/754 Simplify the new transitive-closure function #761

Merged
merged 2 commits into from
Nov 14, 2023

Conversation

michaelhkay
Copy link
Contributor

@michaelhkay michaelhkay commented Oct 18, 2023

Drops the $min and $max parameters, with the effect that this corresponds much more closely to the general computer-science definition of a transitive closure.

The function now computes the set of nodes delivered by the transitive closure of the supplied $step function when applied to a given $start node, rather than returning a function item that must then be applied to the chosen $start node. This is hopefully easier for most users to understand, and does not lose any useful functionality.

The $min parameter of the old function is effectively forced to its default value of 1, and the $max value to its default of infinity.

Fix #754
Fix #554

This PR addresses the main points of #554 in making the function correspond more closely to the mathematical (or at least the computer-science) definition of transitive closure. It doesn't implement other ideas in #554, like returning the depth of search alongside the actual closure. That's because I believe in the principle that wherever possible a function should do one thing in as simple a way as possible.

@michaelhkay michaelhkay added XQFO An issue related to Functions and Operators Enhancement A change or improvement to an existing feature labels Oct 18, 2023
Copy link
Contributor

@ChristianGruen ChristianGruen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for simplifying the function signature and removing min/max. It’s much easier now to understand what the function does, and I agree that it should be up to the user to avoid nondeterministic behavior.

@@ -18349,10 +18349,9 @@ return sort($in, $SWEDISH)</eg>

<fos:function name="transitive-closure" prefix="fn">
<fos:signatures>
<fos:proto name="transitive-closure" return-type="function(node()) as node()*">
<fos:proto name="transitive-closure" return-type="node()*">
<fos:arg name="start" type="node()?"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that naming the parameter $node would be more consistent with the remaining spec. It seems obvious enough that it will be the node from which the operation starts. Next, it would be a helpful hint, as transitive closures could potentially be computed from data structures other than nodes.

is unbounded. In this situation the function <var>TC</var> continues execution until no further nodes
are added to the result set. Note that if the <code>$step</code> function constructs new nodes, this
can lead to non-termination. Specifying an explicit value for <code>$max</code> guarantees termination.</p>
<eg><![CDATA[declare %private function tc-inclusive(
Copy link
Contributor

@ChristianGruen ChristianGruen Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve sllightly tweaked the formatting (and proactively renamed $start to $node):

declare %private function tc-inclusive(
  $nodes as node()*,
  $step  as function(node()) as node()*
) as node()* {
  let $nextStep := $nodes/$step(.)
  let $newNodes := $nextStep except $nodes
  return if (exists($newNodes))
         then $nodes | tc-inclusive($newNodes, $step)
         else $nodes
};
declare function transitive-closure(
  $node as node(),
  $step as function(node()) as node()*
) as node()* {
  tc-inclusive($node/$step(.), $step)
};

<fos:expression><eg>let $tc := transitive-closure($direct-reports)
return $tc($data//person[@id="2"])/string(@id)</eg></fos:expression>
<fos:expression><eg>transitive-closure(
$data//person[@id="2"]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$data//person[@id="2"]),
$data//person[@id="2"],

return $tc($data//person[@id="2"])/string(@id)</eg></fos:expression>
<fos:expression><eg>transitive-closure(
$data//person[@id="2"]),
$direct-reports)/string(@id)</eg></fos:expression>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$direct-reports)/string(@id)</eg></fos:expression>
$direct-reports
)/string(@id)</eg></fos:expression>

@@ -18441,12 +18453,6 @@ return $tc($data)/@id/string()</eg></fos:expression>
return $tc($root) =!> document-uri()</eg>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last example needs to be updated to match the new function signature:

transitive-closure($root, fn { document(//(xsl:import|xsl:include)/@href) })
=!> document-uri()

@ChristianGruen
Copy link
Contributor

@michaelhkay Apart from the minor adjustments, I would approve the PR.

@michaelhkay michaelhkay force-pushed the 754-simplify-transitive-closure branch from 7ce3c03 to 2530ac7 Compare October 31, 2023 18:56
@ChristianGruen ChristianGruen added the Propose Merge without Discussion Change is editorial or minor label Nov 8, 2023
@ndw
Copy link
Contributor

ndw commented Nov 14, 2023

The group agreed to merge this PR at meeting 054

@ndw ndw merged commit a416443 into qt4cg:master Nov 14, 2023
2 checks passed
@ChristianGruen ChristianGruen removed the Propose Merge without Discussion Change is editorial or minor label Jan 2, 2024
@michaelhkay michaelhkay added Tests Added Tests have been added to the test suites Completed PR has been applied, tests written and tagged, no further action needed labels Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Completed PR has been applied, tests written and tagged, no further action needed Enhancement A change or improvement to an existing feature Tests Added Tests have been added to the test suites XQFO An issue related to Functions and Operators
Projects
None yet
3 participants