[xquery-talk] Arrow Operator to a Partially Applied Function

Adam Retter adam.retter at googlemail.com
Sun Mar 10 22:20:33 PDT 2019


Without thinking too deeply, whilst writing some XQuery recently I was
initially surprised to discover that the following (simplified) query
fails with the static error - [XPST0017] map:for-each(map,function): 3
arguments supplied, 2 expected.


xquery version "3.1";
declare namespace map = "http://www.w3.org/2005/xpath-functions/map";

function($k, $v) {
    "$k=" || $k
} =>
    map:for-each(
        map {
            "a" : "1",
            "b" : "2",
            "c" : "3"
        },
        ?
    )()


After having looked into the specs in more detail, I thought I was
able to derive a reason for this... i.e. the partial function
application syntax is not processed until the dynamic evaluation
phase. Therefore when the arrow operator is processed first during the
static analysis phase, it tries to find a map:for-each function that
takes 3 parameters, the first being of the function type on LHS of the
arrow operator, and second being the map, and the third being the
unbound parameter `?`.

However, I then noticed that if I reformulatedby query with an
indirection through a variable binding `$x` then it compiles and
executes just fine. Presumably this indicates that my understanding
above is incorrect, as whilst the variable binding may change the
evaluation order, the static analysis and dynamic evaluation phases
should still happen in the same order.


xquery version "3.1";
declare namespace map = "http://www.w3.org/2005/xpath-functions/map";

let $x :=
    map:for-each(
        map {
            "a" : "1",
            "b" : "2",
            "c" : "3"
        },
        ?
    )
return

  function($k, $v) {
      "$k=" || $k
  } => $x()


I basically get the same results on eXist-db, Saxon, and BaseX. I
would appreciate if someone could help me understand what I am seeing
here...

Thanks Adam.

-- 
Adam Retter

skype: adam.retter
tweet: adamretter
http://www.adamretter.org.uk


More information about the talk mailing list