Re: Brackets, OPTIONALs, BINDs, semantics, oh my.

Dear Gary,

(ot an official WG opinion of course, since the group itself is closed, but an attempt for an explanation:

 BIND may be viewed as syntactic sugar for a subselect with the scope being defined as all the patterns in the same group before the BIND clause.

That is,

>  select * {
>       ?a :ppp1 ?c .
>        bind( 2 * ?c as ?twiceC )
>     }

can be viewed as:

 select * {
      SELECT ?a ?c ( 2 * ?c as ?twiceC )
      WHERE {?a :ppp1 ?c . }

whereas:

> select * {
>       ?a :ppp1 ?c .
>        {
>          bind( 2 * ?c as ?twiceC )
>        }
>     }



would be equivalent to:

select * {
      ?a :ppp1 ?c .
       {
         SELECT ( 2 * ?c as ?twiceC )
         WHERE {}
       }
    }

in this reading, the difference should be obvious, so, whereas OPTIONAL is really a binary operator, BIND is - as described - rather to be viewed as syntactic sugar for a Sub-SELECT. 

best regards,
Axel

> On 16 Dec 2015, at 14:57, Gary King <gwking@metabang.com> wrote:
> 
> Hi all,
> 
> I’ve a question on the model query writers should have when reasoning about their work. I think the naive view would be that adding grouping would _not_ alter the meaning of the query but this is not the case. 
> 
> A first example comes from OPTIONAL. A query like
> 
>    select * {
>      ?a :ppp1 ?c .
>      {
>        optional { ?a :ppp2 ?d }
>      }
>   }
> 
> Is technically equivalent to
> 
>    select * {
>      ?a :ppp1 ?c .
>      ?a :ppp2 ?d .
>   }
> 
> because the first form looks like
> 
>     (join (bgp ?a :ppp1 ?c)
>             (left-join identity (bgp ?a :ppp2 ?d))
> 
> and the left-join will discard no solutions at run-time leaving the outer-most join to merge the two patterns and _discard_ any ?a’s that don’t have a :ppp2 triple.
> 
> This follows pretty clearly from the SPARQL definition but also seems surprising from a query writers perspective.
> 
> A second example occurs with BIND. 
> 
>     select * {
>       ?a :ppp1 ?c .
>        bind( 2 * ?c as ?twiceC )
>     }
> 
> is _not_ the same as
> 
>     select * {
>       ?a :ppp1 ?c .
>        {
>          bind( 2 * ?c as ?twiceC )
>        }
>     }
> 
> The second form will leave ?twiceC unbound as ?c has no binding inside of the BIND. Note that this also means that you cannot distribute common patterns out a union because
> 
>    select * {
>      { 
>        ?x a ?p .
>        ?p ex:bar ?foo .
>      }
>      union
>      { 
>        ?x a ?p .
>        bind(?p as ?foo)
>      }
>    }
> 
> is _not_ the same as
> 
>    select * {
>      ?x a ?p .
>      { 
>        ?p ex:bar ?foo .
>      }
>      union
>      { 
>        bind(?p as ?foo)
>      }
>    }
> 
> 
> If my examples are correct, then SPARQL seems more difficult than it should be to reason about than it should be. I’d welcome comments and thoughts.
> 
> thanks,
> --
> Gary Warren King, metabang.com 
> Cell: (413) 559 8738
> Fax: (206) 338-4052
> gwkkwg on Skype * garethsan on AIM * gwking on twitter
> 
> 

Received on Wednesday, 16 December 2015 14:30:22 UTC