<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>5696</bug_id>
          
          <creation_ts>2008-05-16 15:47:05 +0000</creation_ts>
          <short_desc>[XQuery 1.1] Feature request to aid static typing</short_desc>
          <delta_ts>2009-12-08 18:10:09 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>XPath / XQuery / XSLT</product>
          <component>XQuery 3.0</component>
          <version>Recommendation</version>
          <rep_platform>All</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>CLOSED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>enhancement</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Nick Jones">nick</reporter>
          <assigned_to name="Michael Dyck">jmdyck</assigned_to>
          <cc>jim.melton</cc>
    
    <cc>jmdyck</cc>
    
    <cc>tim</cc>
          
          <qa_contact name="Mailing list for public feedback on specs from XSL and XML Query WGs">public-qt-comments</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>20141</commentid>
    <comment_count>0</comment_count>
    <who name="Nick Jones">nick</who>
    <bug_when>2008-05-16 15:47:05 +0000</bug_when>
    <thetext>We&apos;ve been looking at static typing in the new functx tests, and have come across some queries which we are unable to rewrite so they type check, for example:

declare function functx:value-except
  ( $arg1 as xs:anyAtomicType* ,
    $arg2 as xs:anyAtomicType* )  as xs:anyAtomicType* {

  distinct-values($arg1[not(.=$arg2)])
 } ;

because fs:equal is not defined for all pairs of atomic types in xs:anyAtomicType an XPTY0004 is thrown.

Obviously, separate value-except-type functions could be defined for arguments of each atomic type. But we can&apos;t think of any other way to rewrite such a function to type check. We propose two options to &quot;fix&quot; this problem.

Option 1
--------

Recall the general comparisons expand (roughly) out to to the form:

some $x in $arg1 satisfies
  some $y in $arg2 satisfies
    $x op $y

So currently the queries

(1, &quot;foo&quot;) = &quot;foo&quot;

and

(&quot;foo&quot;, 1) = &quot;foo&quot;

may return true or XPTY0004, depending on the order in which
the implementation happens to process the query.

We&apos;d like to propose that if the value comparison used in the 
expansion of the general comparison is not defined for the input
types, then the result is false.

Thus

(1, &quot;foo&quot;) = &quot;bar&quot;

would return false.


Option 2
--------

Add generics.

declare function functx:value-except
  ( $arg1 as type(T) &lt;: xs:anyAtomicType* ,
    $arg2 as T )  as T {
  distinct-values($arg1[not(.=$arg2)])
 } ;

This of course would open up a need for a whole host of 
type-describing syntax, e.g. to specify subtype releationships,
type unions etc.

Option 3
--------

We could fix this query by using XQuery 1.1 try/catch.

&quot;.=$arg2&quot; would have to be expanded out manually to &quot;some&quot; expressions
with a try/catch to return &quot;false&quot; when the XPST0004 is raised.

This would be rather clumsy.

Option 4
--------

Define union, except, intersect functions/operators which work nicely
with xs:anyAtomicType* input.  Obviously this doesn&apos;t fix the general case.

Our guess is that option 1 will make a wider variety of queries run in 
static typing mode, but introducing a breaking change with XQuery 1.0
might be unacceptable.

Option 2 might be nice but might require a fair amount of work by the 
Working Group.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>20149</commentid>
    <comment_count>1</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2008-05-16 17:53:24 +0000</bug_when>
    <thetext>Presumably the issue also affects our own version of this function in

http://www.w3.org/TR/xpath-functions/#value-except

I&apos;ve often argued that a=b should return false if a and b are of incomparable types, but I&apos;ve always lost the argument to those who prefer the diagnostic power of raising an error. There are arguments both ways.

A possible circumvention is to use one of the functions that does allow comparison of incomparables, for example index-of(), distinct-values(), or deep-equal(). If you don&apos;t want (a eq b) to fail, you can always write it as deep-equal(a, b).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>22254</commentid>
    <comment_count>2</comment_count>
    <who name="Michael Dyck">jmdyck</who>
    <bug_when>2008-10-25 18:51:52 +0000</bug_when>
    <thetext>(In reply to comment #0)
&gt;
&gt; Option 3
&gt; --------
&gt; 
&gt; We could fix this query by using XQuery 1.1 try/catch.
&gt; 
&gt; &quot;.=$arg2&quot; would have to be expanded out manually to &quot;some&quot; expressions
&gt; with a try/catch to return &quot;false&quot; when the XPST0004 is raised.
&gt; 
&gt; This would be rather clumsy.

I don&apos;t understand why you&apos;d need to expand to &apos;some&apos; expressions.
Why not just replace
    .=$arg2
with
    try { .=$arg2 } catch err:XPTY0004 { false() }

Or, for better encapsulation, replace it with
    my-equal(., $arg2)
where &apos;my-equal&apos; has the semantics that Option 1 would give to &apos;=&apos;:
    declare function my-equal(...) as boolean()
    {
        try { $arg1 = $arg2 } catch err:XPTY0004 { false() }
    }</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>22266</commentid>
    <comment_count>3</comment_count>
    <who name="Tim Mills">tim</who>
    <bug_when>2008-10-28 13:00:12 +0000</bug_when>
    <thetext>Haskell has type classes, described at 

http://www.haskell.org/tutorial/classes.html 

which seem like a very well designed way of describing the sort of constraints on types suggested by Option 2.  This might be a useful addition to XQuery.

As an off-topic aside, I&apos;m personally a bit wary of try ... catch.  I&apos;ve got a horrible feeling I&apos;m turning into a functional purist.  Even element constructors make me feel queazy :)
</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>30018</commentid>
    <comment_count>4</comment_count>
    <who name="Michael Dyck">jmdyck</who>
    <bug_when>2009-12-08 18:09:44 +0000</bug_when>
    <thetext>At today&apos;s Working Group meeting, Nick Jones (the original poster) reviewed this bug, and said that try/catch allowed an adequate solution to the problem. Therefore, I am marking this bug Resolved-Fixed, and also Closed.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>