Partial applications now defined in terms of ref mutators

master
Mike Gerwitz 2014-12-01 01:20:29 -05:00
parent c526fa2925
commit 189eb0cdd6
3 changed files with 47 additions and 77 deletions

View File

@ -55,57 +55,32 @@
<param name="fnref" 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
represents the arity of the partially applied function, _not_
the target function -->
<variable name="arity" as="xs:integer"
select="f:arity( $ref )" />
select="f:arity( $fnref )" />
<variable name="argn" as="xs:integer"
select="count( $args )" />
<choose>
<when test="$argn eq $arity">
<sequence select="_f:apply-partial( $ref, $argout )" />
</when>
<when test="$argn gt $arity">
<apply-templates mode="f:partial-arity-error-hook"
select="$ref">
<with-param name="args" select="$argout" />
<with-param name="arity" select="$arity" />
<with-param name="argn" select="count( $argout )" />
select="$fnref">
<with-param name="fnref" select="$fnref" />
<with-param name="args" select="$args" />
</apply-templates>
</when>
<otherwise>
<f:ref>
<sequence select="$ref/@*" />
<variable name="new-ref"
select="f:push-args( $fnref, $args )" />
<!-- the arity of the new partial application -->
<attribute name="arity"
select="$arity - $argn" />
<sequence select="$ref/*" />
</f:ref>
<sequence select="$argout" />
<sequence select="if ( $argn eq $arity ) then
_f:apply-partial( $new-ref )
else
$new-ref" />
</otherwise>
</choose>
</function>
@ -149,24 +124,18 @@
match="f:ref"
priority="1">
<param name="args" as="item()*" />
<param name="arity" as="xs:decimal" />
<variable name="ref" as="element(f:ref)"
select="." />
<variable name="arity" as="xs:integer"
select="f:arity(.)" />
<variable name="argn" as="xs:decimal"
select="count( $args )" />
<variable name="fn"
select="$ref/*[1]" />
<variable name="fname"
select="concat( '{', namespace-uri( $fn ), '}',
$fn/local-name() )" />
<sequence
select="error(
QName( namespace-uri-for-prefix( 'f', $ref ),
QName( namespace-uri-for-prefix( 'f', . ),
'err:PARTIAL_PARAM_OVERFLOW' ),
concat( 'Attempted partial application of ',
$fname, '#', $arity, ' with ',
'function of arity ', $arity, ' with ',
$argn, ' arguments' ) )" />
</template>
@ -183,14 +152,16 @@
are supported}. This should be enough.
-->
<function name="_f:apply-partial">
<param name="fnref" as="element(f:ref)" />
<param name="args" as="item()*" />
<param name="fnref" as="item()+" />
<variable name="fn" as="element()"
<variable name="args"
select="f:args( $fnref )" />
<variable name="desc" as="element( f:ref )"
select="$fnref[ 1 ]" />
<!-- 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="arg2" select="$args[ 2 ]" />
<with-param name="arg3" select="$args[ 3 ]" />

View File

@ -41,8 +41,10 @@
<template mode="f:partial-arity-error-hook"
match="f:ref"
priority="5">
<param name="args" as="item()*" />
<param name="arity" as="xs:decimal" />
<param name="args" as="item()*" />
<variable name="arity"
select="f:arity(.)" />
<foo:partial-error arity="{$arity}" />

View File

@ -52,8 +52,7 @@
</call>
<expect label="returns reference to original target"
test="f:QName( $x:result[ 1 ] )
= f:QName( $fnref )" />
test="f:QName( $x:result) = f:QName( $fnref )" />
<expect label="returns nothing else"
test="count( $x:result ) = 1" />
@ -68,14 +67,12 @@
<expect label="returns target reference as first item in
sequence"
test="f:QName( $fnref )
= f:QName( $x:result[ 1 ] )" />
test="f:QName( $fnref ) = f:QName( $x:result )" />
<!-- FIXME: this breaks the abstraction; use opaque
introspection -->
<variable name="rargs" select="f:args( $x:result )" />
<expect label="returns each argument, ordered"
test="$x:result[ 2 ] is $args/foo:a
and $x:result[ 3 ] is $args/foo:b" />
test="$rargs[ 1 ] is $args/foo:a
and $rargs[ 2 ] is $args/foo:b" />
</scenario>
@ -92,10 +89,11 @@
test="$x:result[ 1 ]
and $x:result[ 1 ] = foo:ternary-applied" />
<variable name="rargs" select="f:args( $x:result )" />
<expect label="applies target function with arguments, by reference"
test="$x:result[ 2 ] is $args/foo:a
and $x:result[ 3 ] is $args/foo:b
and $x:result[ 4 ] is $args/foo:c" />
test="$rargs[ 1 ] is $args/foo:a
and $rargs[ 2 ] is $args/foo:b
and $rargs[ 3 ] is $args/foo:c" />
</scenario>
@ -107,18 +105,17 @@
<param name="args" select="$args/foo:b" />
</call>
<expect label="returns target reference as first item in
sequence"
test="f:QName( $fnref )
= f:QName( $x:result[ 1 ] )" />
<expect label="retains target reference"
test="f:QName( $fnref ) = f:QName( $x:result )" />
<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
first in sequence"
test="$x:result[ 2 ] is $args/foo:a
and $x:result[ 3 ] is $args/foo:b" />
test="$rargs[ 1 ] is $args/foo:a
and $rargs[ 2 ] is $args/foo:b" />
</scenario>
@ -193,9 +190,9 @@
test="$x:result[ 1 ] = foo:applied[ @n = 3 ]" />
<expect label="applies target function with arguments, by reference"
test="$x:result[ 2 ] is $args/foo:a
and $x:result[ 3 ] is $args/foo:b
and $x:result[ 4 ] is $args/foo:c" />
test="$x:result[ 2 ] = $args/foo:a
and $x:result[ 3 ] = $args/foo:b
and $x:result[ 4 ] = $args/foo:c" />
</scenario>
@ -442,8 +439,7 @@
<expect label="provides source dynamic function reference"
test="$x:result[ 2 ] is $fnref" />
<expect label="provides arity of target (root of all partial
applications) function"
<expect label="provides arity of ref"
test="$x:result[ 1 ]/@arity = 3" />
<expect label="provides each argument of partial application
@ -465,10 +461,11 @@
$args/foo:d" />
</call>
<expect label="produces error with all arguments"
<expect label="produces error with arguments of final
application"
test="exists( foo:partial-error )
and $x:result[ 3 ] is $args/foo:a
and $x:result[ 6 ] is $args/foo:d" />
and $x:result[ 3 ] is $args/foo:c
and $x:result[ 4 ] is $args/foo:d" />
</scenario>
</scenario>
</description>