Re: Clarification of variable scoping

I just noticed Andy's response to my comment in the archives:
http://lists.w3.org/Archives/Public/public-rdf-dawg-comments/2011Oct/0006
It looks like he forgot to copy me on it.

I realize that the spec is not intended to be a tutorial, but it also
needs to be penetrable by informed technical readers who need to look up
details of SPARQL's syntax and semantics.  I think it is good to have
the precision of the SPARQL algebra, but it is *very* difficult to
figure out the variable scoping from that.  I have spent over 2-3 hours
trying to figure out the variable scoping from the spec -- as opposed to
figuring it out from trial and error in an implementation -- and I have
not yet succeeded, in spite of being a highly technical reader who is
quite experienced with reading specs and other technical documents.  

I specifically believe that the variable scoping is one of the most
confusing and non-intuitive aspects of the SPARQL language, and I
suspect that a lot of benefit could be gained by the addition of just a
few sentences and a couple of clarifying examples.  Perhaps the examples
that I included in my comment might be a good basis, or you may have
better ones.  

For instance, Sec 8.3 includes some clarifying examples for explaining
the semantics of NOT EXISTS and MINUS.  Something analogous for variable
scope would be very helpful.

In short, I really do think this is a case where a little more should be
added to the spec as an aid to the reader.  Your explanation to me below
would be a good starting point, though it still needs a little more and
some differentiating examples.

Thanks,
David


> From: Andy Seaborne <andy.seaborne@epimorphics.com>
> Date: Fri, 07 Oct 2011 09:02:01 +0100
> Message-ID: <4E8EB1F9.9050105@epimorphics.com>
> To: public-rdf-dawg-comments@w3.org
> 
> David,
> 
> Section 10.1 "BIND: Assigning to Variables" says it ends the current 
> basic graph pattern. Section 18.2.2.5 "Translate Graph Patterns" defines 
> how it maps the "extend" operator in the SPARQL algebra. It is then 
> within a group graph pattern. The purpose of bind is to set a variable 
> based on the current basic graph pattern, not the whole query.
> 
> Evaluation of a group graph pattern in SPARQL happens by evaluating each 
> of the separate elements of the group and then combining them with JOIN. 
> While it can be tempting to top-to-bottom, that is not how query 
> evaluation is defined (from SPARQL 1.0).
> 
> The query document isn't a complete tutorial for SPARQL (that would be a 
> much larger task). The formal sections give a precise definition and 
> section 18.2.2.5 covers how the BIND syntax maps to the extend operator 
> and how it interacts with the syntax group, becoming a "join" to be 
> evaluated.
> 
> We would be grateful if you would acknowledge that your comment has been 
> answered by sending a reply to this mailing list.
> 
> Andy (on behalf of the SPARQL WG)
> 
> (more inline)
> 
> On 13/09/11 18:20, David Booth wrote:
> > First, thanks again for the great work on SPARQL!
> >
> > I think a bit more clarification (and perhaps examples) of variable
> > scoping would be helpful in the SPARQL Query spec.
> >
> > I tried the following query (in Parliament 2.7.1):
> >
> >    # Example 1: no results
> >    SELECT *
> >    WHERE {
> >      BIND( 10 as ?ten )
> >      GRAPH<http://example/bookStore>  {
> >        FILTER( ?ten>= ?ten )
> >      }
> >    }
> >
> > and it produced 0 results.  However, when I moved the BIND inside the
> > GRAPH clause, the following query worked, producing one result with 10
> > bound to ?ten:
> >
> >    # Example 2: one result
> >    SELECT *
> >    WHERE {
> >      GRAPH<http://example/bookStore>  {
> >        BIND( 10 as ?ten )
> >        FILTER( ?ten>= ?ten )
> >      }
> >    }
> >
> > Based on investigation, it looks like ?ten is unbound at the point of
> > the FILTER clause in example 1, whereas it *is* bound at the point of
> > the FILTER clause in example 2.   This behavior was non-intuitive (to
> > me), but it is not evident to me from the spec whether this is
> > considered correct behavior or not.  In looking at the variable scoping
> > rules in section 18.2.1
> > http://www.w3.org/TR/sparql11-query/#variableScope
> > the table says that "v is in-scope".  But "in-scope" *where*?  It is not
> > clear where the scopes are and which scope is meant.  The spec *does*
> > clearly say that "use of a variable in FILTER, or in MINUS does not
> > cause a variable to be in-scope outside of those forms".  But I would
> > have expected the BIND statement to *cause* the variable to be bound
> > within the FILTER clause.
> >
> > This behavior seems counterintuitive to one who is accustomed to
> > variable scoping rules in programming languages, since AFAIK the purpose
> > of BIND is to set a variable that can be used in throughout the query.
> > But if the behavior above is correct, I do not yet see how to do that.
> > For example, I'd like to use the same variable ?limit bound to the
> > constant 10 in multiple GRAPH clauses like this:
> >
> >    SELECT *
> >    WHERE {
> >      GRAPH ... {
> >        FILTER( ?n<= ?limit )
> >      }
> >      GRAPH ... {
> >        FILTER( ?n<= ?limit )
> >      }
> >      GRAPH ... {
> >        FILTER( ?n<= ?limit )
> >      }
> >
> >    }
> >
> > But where should I put the BIND statement to achieve this?
> 
> You may be able to achieve the effect by placing the FILTER further out:
> 
> SELECT *
> WHERE {
>    { SELECT * # including ?n
>      {
>        GRAPH ... { }
>        GRAPH ... { }
>        GRAPH ... { }
>      }
>    }
>    FILTER( ?n <= ?limit )
> }
> 
> 
> >
> >
> > Thanks
> 




-- 
David Booth, Ph.D.
http://dbooth.org/

Opinions expressed herein are those of the author and do not necessarily
reflect those of his employer.

Received on Tuesday, 18 October 2011 20:14:52 UTC