Re: Bug 4378: absent context item

Thanks, I like this explanation a lot.  So, I guess we just need to clarify the static type error code (e.g. XPTY0004) and clarify that the context item static type component may be absent, as you suggest?

Thanks,
Josh

> On Nov 4, 2015, at 5:21 AM, Tim Mills <tim@cbcl.co.uk> wrote:
> 
> I think it is unfortunate that we say that the "context item static type component is absent[DM30]." without saying in 2.1.1 Static Context:
> 
> [Definition: Context item static type. This component defines the static type of the context item within the scope of a given expression.  It may be a type, or absent ]
> 
> The above change would make it clear that this component of the static context can be absent.
> 
> For the faint hearted, there's a bit of Formal Semantics stuff which follows.  If you don't like FS, skip on to "The implications".
> 
> In FS, the context item was represented as a variable $fs:dot, and the context item static type in the [XPath/XQuery] static context is represented by the binding of the variable $fs:dot to its corresponding type in statEnv.varType.  Recall that statEnv.varType is a mapping from expanded variable name to static type.
> 
> declare context item as xs:integer external;
> .
> 
> is effectively
> 
> declare variable $fs:dot as xs:integer external;
> 
> $fs:dot
> 
> Because statEnv.varType is a map, the absence of the context item's static type can be represented as the absence of a mapping in statEnv.varType.
> 
> FS says to type a variable reference (and therefore the context item):
> 
> "In the static semantics, the type of a variable is simply its type in the static environment statEnv.varType:
> 
> statEnv |- VarName of var expands to Variable
> statEnv.varType(Variable) = Type
> ----------------------------------------------
> statEnv |- $VarName : Type
> 
> 
> If the variable is not bound in the static environment, a static type error is raised."
> 
> 
> So adapting this for the context item, we get
> 
> statEnv |- $Fsdot of var expands to {the context item}
> statEnv.varType({the context item}) = Type
> ----------------------------------------------
> statEnv |- $fs:dot : Type
> 
> 
> That is, in an environment in which the context item has static type "Type", the expression "." has type "Type".  But crucially:
> 
> "If the {context item} is not bound in the static environment, a static type error is raised."
> 
> ==============
> The implications
> ==============
> 
> Now, when performing static type analysis on:
> 
> declare function local:func()
> {
>  .
> };
> 
> we are operating in an environment where there is no mapping in statEnv.varType for the context item.  So a type error is raised, say, XPTY0004.
> 
> 
> Suppose we have the query
> 
> declare context item as item() external;
> 
> .
> 
> 
> and invoke it without specifying a context item.  Here the static type of the context item is item().  The value of the context item is absent.  Here we get XPDY0002.
> 
> 
> Consider XPath.  XPath expressions are typed using a static context provided by the external environment.  If we admit that the context item static type may be absent, the environment may legitimately specify that the context item static type is absent.  Then the XPath expression '.' could raise a static type error XPTY0004. Alternatively if the environment specified a type for the context item but the dynamic context supplied no value, we'd get XPDY0002.
> 
> 
> Looking at XSLT's context item declaration, it appears that XSLT never permits the static type of the context item to be absent because
> 
> <xsl:global-context-item use="absent" />
> 
> is equivalent to
> 
> <xsl:global-context-item use="absent" as="item()" />
> 
> It's quite explicit: the value may be absent, but it's static type definitely is not.
> 
> Suppose we have an XSLT stylesheet with a named template "foo", and the stylesheet is invoked with no initial context node.
> 
> <xsl:template name="foo">
>  <xsl:sequence select="." />
> </xsl:template>
> 
> 
> Templates behave as if they were functions with an implicit context-item argument, e.g.
> 
> declare function template:foo($fs:dot as item())
> {
>  .
> };
> 
> There is no problem typing this function.
> 
> Invocation of the stylesheet is effectively a function call
> 
> template:foo(.)
> 
> This would fail with XPDY0002, since it requires the value of the absent context item.
> 
> Cheers,
>    Tim
> 
> 
> 

Received on Wednesday, 4 November 2015 19:08:38 UTC