Partial applications now defined in terms of ref mutators
parent
c526fa2925
commit
189eb0cdd6
|
@ -55,57 +55,32 @@
|
||||||
<param name="fnref" as="item()+" />
|
<param name="fnref" as="item()+" />
|
||||||
<param name="args" as="item()*" />
|
<param name="args" as="item()*" />
|
||||||
|
|
||||||
<!-- perform type check here, not above, since we can be passed a
|
|
||||||
sequence (e.g.a partially applied function) -->
|
|
||||||
<variable name="ref"
|
|
||||||
as="element(f:ref)"
|
|
||||||
select="$fnref[ 1 ]" />
|
|
||||||
|
|
||||||
<variable name="argout" as="item()*">
|
|
||||||
<!-- include any previously applied arguments (if we're partially
|
|
||||||
applying a partial application) -->
|
|
||||||
<sequence select="remove( $fnref, 1 )" />
|
|
||||||
|
|
||||||
<!-- nested sequences are implicitly flattened, so we're not
|
|
||||||
returning a sub-sequence here -->
|
|
||||||
<sequence select="$args" />
|
|
||||||
</variable>
|
|
||||||
|
|
||||||
<!-- note that, if FNREF is partially applied, then this arity
|
<!-- note that, if FNREF is partially applied, then this arity
|
||||||
represents the arity of the partially applied function, _not_
|
represents the arity of the partially applied function, _not_
|
||||||
the target function -->
|
the target function -->
|
||||||
<variable name="arity" as="xs:integer"
|
<variable name="arity" as="xs:integer"
|
||||||
select="f:arity( $ref )" />
|
select="f:arity( $fnref )" />
|
||||||
|
|
||||||
<variable name="argn" as="xs:integer"
|
<variable name="argn" as="xs:integer"
|
||||||
select="count( $args )" />
|
select="count( $args )" />
|
||||||
|
|
||||||
<choose>
|
<choose>
|
||||||
<when test="$argn eq $arity">
|
|
||||||
<sequence select="_f:apply-partial( $ref, $argout )" />
|
|
||||||
</when>
|
|
||||||
|
|
||||||
<when test="$argn gt $arity">
|
<when test="$argn gt $arity">
|
||||||
<apply-templates mode="f:partial-arity-error-hook"
|
<apply-templates mode="f:partial-arity-error-hook"
|
||||||
select="$ref">
|
select="$fnref">
|
||||||
<with-param name="args" select="$argout" />
|
<with-param name="fnref" select="$fnref" />
|
||||||
<with-param name="arity" select="$arity" />
|
<with-param name="args" select="$args" />
|
||||||
<with-param name="argn" select="count( $argout )" />
|
|
||||||
</apply-templates>
|
</apply-templates>
|
||||||
</when>
|
</when>
|
||||||
|
|
||||||
<otherwise>
|
<otherwise>
|
||||||
<f:ref>
|
<variable name="new-ref"
|
||||||
<sequence select="$ref/@*" />
|
select="f:push-args( $fnref, $args )" />
|
||||||
|
|
||||||
<!-- the arity of the new partial application -->
|
<sequence select="if ( $argn eq $arity ) then
|
||||||
<attribute name="arity"
|
_f:apply-partial( $new-ref )
|
||||||
select="$arity - $argn" />
|
else
|
||||||
|
$new-ref" />
|
||||||
<sequence select="$ref/*" />
|
|
||||||
</f:ref>
|
|
||||||
|
|
||||||
<sequence select="$argout" />
|
|
||||||
</otherwise>
|
</otherwise>
|
||||||
</choose>
|
</choose>
|
||||||
</function>
|
</function>
|
||||||
|
@ -149,24 +124,18 @@
|
||||||
match="f:ref"
|
match="f:ref"
|
||||||
priority="1">
|
priority="1">
|
||||||
<param name="args" as="item()*" />
|
<param name="args" as="item()*" />
|
||||||
<param name="arity" as="xs:decimal" />
|
|
||||||
|
|
||||||
<variable name="ref" as="element(f:ref)"
|
<variable name="arity" as="xs:integer"
|
||||||
select="." />
|
select="f:arity(.)" />
|
||||||
<variable name="argn" as="xs:decimal"
|
<variable name="argn" as="xs:decimal"
|
||||||
select="count( $args )" />
|
select="count( $args )" />
|
||||||
<variable name="fn"
|
|
||||||
select="$ref/*[1]" />
|
|
||||||
<variable name="fname"
|
|
||||||
select="concat( '{', namespace-uri( $fn ), '}',
|
|
||||||
$fn/local-name() )" />
|
|
||||||
|
|
||||||
<sequence
|
<sequence
|
||||||
select="error(
|
select="error(
|
||||||
QName( namespace-uri-for-prefix( 'f', $ref ),
|
QName( namespace-uri-for-prefix( 'f', . ),
|
||||||
'err:PARTIAL_PARAM_OVERFLOW' ),
|
'err:PARTIAL_PARAM_OVERFLOW' ),
|
||||||
concat( 'Attempted partial application of ',
|
concat( 'Attempted partial application of ',
|
||||||
$fname, '#', $arity, ' with ',
|
'function of arity ', $arity, ' with ',
|
||||||
$argn, ' arguments' ) )" />
|
$argn, ' arguments' ) )" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
@ -183,14 +152,16 @@
|
||||||
are supported}. This should be enough.
|
are supported}. This should be enough.
|
||||||
-->
|
-->
|
||||||
<function name="_f:apply-partial">
|
<function name="_f:apply-partial">
|
||||||
<param name="fnref" as="element(f:ref)" />
|
<param name="fnref" as="item()+" />
|
||||||
<param name="args" as="item()*" />
|
|
||||||
|
|
||||||
<variable name="fn" as="element()"
|
<variable name="args"
|
||||||
|
select="f:args( $fnref )" />
|
||||||
|
|
||||||
|
<variable name="desc" as="element( f:ref )"
|
||||||
select="$fnref[ 1 ]" />
|
select="$fnref[ 1 ]" />
|
||||||
|
|
||||||
<!-- just as `f:apply', we support up to 8 arguments -->
|
<!-- just as `f:apply', we support up to 8 arguments -->
|
||||||
<apply-templates select="$fn" mode="f:apply">
|
<apply-templates select="$desc" mode="f:apply">
|
||||||
<with-param name="arg1" select="$args[ 1 ]" />
|
<with-param name="arg1" select="$args[ 1 ]" />
|
||||||
<with-param name="arg2" select="$args[ 2 ]" />
|
<with-param name="arg2" select="$args[ 2 ]" />
|
||||||
<with-param name="arg3" select="$args[ 3 ]" />
|
<with-param name="arg3" select="$args[ 3 ]" />
|
||||||
|
|
|
@ -41,8 +41,10 @@
|
||||||
<template mode="f:partial-arity-error-hook"
|
<template mode="f:partial-arity-error-hook"
|
||||||
match="f:ref"
|
match="f:ref"
|
||||||
priority="5">
|
priority="5">
|
||||||
<param name="args" as="item()*" />
|
<param name="args" as="item()*" />
|
||||||
<param name="arity" as="xs:decimal" />
|
|
||||||
|
<variable name="arity"
|
||||||
|
select="f:arity(.)" />
|
||||||
|
|
||||||
<foo:partial-error arity="{$arity}" />
|
<foo:partial-error arity="{$arity}" />
|
||||||
|
|
||||||
|
|
|
@ -52,8 +52,7 @@
|
||||||
</call>
|
</call>
|
||||||
|
|
||||||
<expect label="returns reference to original target"
|
<expect label="returns reference to original target"
|
||||||
test="f:QName( $x:result[ 1 ] )
|
test="f:QName( $x:result) = f:QName( $fnref )" />
|
||||||
= f:QName( $fnref )" />
|
|
||||||
|
|
||||||
<expect label="returns nothing else"
|
<expect label="returns nothing else"
|
||||||
test="count( $x:result ) = 1" />
|
test="count( $x:result ) = 1" />
|
||||||
|
@ -68,14 +67,12 @@
|
||||||
|
|
||||||
<expect label="returns target reference as first item in
|
<expect label="returns target reference as first item in
|
||||||
sequence"
|
sequence"
|
||||||
test="f:QName( $fnref )
|
test="f:QName( $fnref ) = f:QName( $x:result )" />
|
||||||
= f:QName( $x:result[ 1 ] )" />
|
|
||||||
|
|
||||||
<!-- FIXME: this breaks the abstraction; use opaque
|
<variable name="rargs" select="f:args( $x:result )" />
|
||||||
introspection -->
|
|
||||||
<expect label="returns each argument, ordered"
|
<expect label="returns each argument, ordered"
|
||||||
test="$x:result[ 2 ] is $args/foo:a
|
test="$rargs[ 1 ] is $args/foo:a
|
||||||
and $x:result[ 3 ] is $args/foo:b" />
|
and $rargs[ 2 ] is $args/foo:b" />
|
||||||
</scenario>
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
@ -92,10 +89,11 @@
|
||||||
test="$x:result[ 1 ]
|
test="$x:result[ 1 ]
|
||||||
and $x:result[ 1 ] = foo:ternary-applied" />
|
and $x:result[ 1 ] = foo:ternary-applied" />
|
||||||
|
|
||||||
|
<variable name="rargs" select="f:args( $x:result )" />
|
||||||
<expect label="applies target function with arguments, by reference"
|
<expect label="applies target function with arguments, by reference"
|
||||||
test="$x:result[ 2 ] is $args/foo:a
|
test="$rargs[ 1 ] is $args/foo:a
|
||||||
and $x:result[ 3 ] is $args/foo:b
|
and $rargs[ 2 ] is $args/foo:b
|
||||||
and $x:result[ 4 ] is $args/foo:c" />
|
and $rargs[ 3 ] is $args/foo:c" />
|
||||||
</scenario>
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
@ -107,18 +105,17 @@
|
||||||
<param name="args" select="$args/foo:b" />
|
<param name="args" select="$args/foo:b" />
|
||||||
</call>
|
</call>
|
||||||
|
|
||||||
<expect label="returns target reference as first item in
|
<expect label="retains target reference"
|
||||||
sequence"
|
test="f:QName( $fnref ) = f:QName( $x:result )" />
|
||||||
test="f:QName( $fnref )
|
|
||||||
= f:QName( $x:result[ 1 ] )" />
|
|
||||||
|
|
||||||
<expect label="returns arguments from both partial applications"
|
<expect label="returns arguments from both partial applications"
|
||||||
test="count( $x:result ) = 3" />
|
test="count( f:args( $x:result ) ) = 2" />
|
||||||
|
|
||||||
|
<variable name="rargs" select="f:args( $x:result )" />
|
||||||
<expect label="arguments are ordered with previous applications
|
<expect label="arguments are ordered with previous applications
|
||||||
first in sequence"
|
first in sequence"
|
||||||
test="$x:result[ 2 ] is $args/foo:a
|
test="$rargs[ 1 ] is $args/foo:a
|
||||||
and $x:result[ 3 ] is $args/foo:b" />
|
and $rargs[ 2 ] is $args/foo:b" />
|
||||||
</scenario>
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
@ -193,9 +190,9 @@
|
||||||
test="$x:result[ 1 ] = foo:applied[ @n = 3 ]" />
|
test="$x:result[ 1 ] = foo:applied[ @n = 3 ]" />
|
||||||
|
|
||||||
<expect label="applies target function with arguments, by reference"
|
<expect label="applies target function with arguments, by reference"
|
||||||
test="$x:result[ 2 ] is $args/foo:a
|
test="$x:result[ 2 ] = $args/foo:a
|
||||||
and $x:result[ 3 ] is $args/foo:b
|
and $x:result[ 3 ] = $args/foo:b
|
||||||
and $x:result[ 4 ] is $args/foo:c" />
|
and $x:result[ 4 ] = $args/foo:c" />
|
||||||
</scenario>
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
@ -442,8 +439,7 @@
|
||||||
<expect label="provides source dynamic function reference"
|
<expect label="provides source dynamic function reference"
|
||||||
test="$x:result[ 2 ] is $fnref" />
|
test="$x:result[ 2 ] is $fnref" />
|
||||||
|
|
||||||
<expect label="provides arity of target (root of all partial
|
<expect label="provides arity of ref"
|
||||||
applications) function"
|
|
||||||
test="$x:result[ 1 ]/@arity = 3" />
|
test="$x:result[ 1 ]/@arity = 3" />
|
||||||
|
|
||||||
<expect label="provides each argument of partial application
|
<expect label="provides each argument of partial application
|
||||||
|
@ -465,10 +461,11 @@
|
||||||
$args/foo:d" />
|
$args/foo:d" />
|
||||||
</call>
|
</call>
|
||||||
|
|
||||||
<expect label="produces error with all arguments"
|
<expect label="produces error with arguments of final
|
||||||
|
application"
|
||||||
test="exists( foo:partial-error )
|
test="exists( foo:partial-error )
|
||||||
and $x:result[ 3 ] is $args/foo:a
|
and $x:result[ 3 ] is $args/foo:c
|
||||||
and $x:result[ 6 ] is $args/foo:d" />
|
and $x:result[ 4 ] is $args/foo:d" />
|
||||||
</scenario>
|
</scenario>
|
||||||
</scenario>
|
</scenario>
|
||||||
</description>
|
</description>
|
||||||
|
|
Loading…
Reference in New Issue