Re: In response to the question of Michael SpMcQ in the telcon of today regarding binding non-local variables

On 16-02-09 08:11 PM, Abel Braaksma wrote:
>
> (: in a function :)
> let $p := 10 return function($p, $q) { $p * $q }
>
> 2) In [this] example, the function body cannot access the variable $p
> (with value 10) because it is shadowed, as a result it has no effect on
> the body of the function,

Agreed.

> though technically it will be part of the non-local variable bindings

No, the Data Model says:
     * nonlocal variable bindings (a mapping from xs:QName to item()*):
       This provides a value for each of the function's free variables
       (i.e., variables referenced by the function's implementation,
       other than locals and parameters).

In the function's body, $p is not a free variable, so $p does not appear in
the function's nonlocal variable bindings.


> (: with pfa :)
> let $p := 10, $f := function($p, $q) { $p * $q } return $f(?, $p)
>
> 3) In [this] example, which is what I tried to get at w.r.t. to the
> current proposal for item 5.1.a., we have a non-local binding $p set to
> 10, $f is not part of the non-local bindings (recursive variables are not
> allowed), and the function creates a closure with local bindings ($p,
> $q), which are its arguments, and non-local bindings $p.

Again, $p is not a free variable in the function body, so it doesn't appear
in the anonymous function's nonlocal variable bindings.

> The expression returns a partially applied function with its second
> argument set to $p, which is 10.

Specifically, according to 5.a, it returns a function with the following
properties:
     name:            (absent)
     parameter names: $p
     signature:       function (item()*)
     implementation:  the FunctionBody { $p * $q }
     nlvb:            [[ $q => 10 ]]

(The implementation is also 'associated with' the static and dynamic context
of the expression '$f(?, $p)', though they don't affect its behaviour.)

> This does *not* shadow the local variable binding $p even
> though it carries the same identifier. Let's call the result of this
> expression $PFA. Then $PFA(22) will return 210,

220, I think you mean.

> not 100. The latter could happen if the current text remained in place
> and was taken too literally.

I don't think so. Do you still think so?

-Michael

Received on Wednesday, 10 February 2016 03:10:53 UTC