Discussion:
[xquery-talk] Question / Suggestion : Template mechanisms and operator overloading for XQUERY 3.++ ?
jean-marc Mercier
2015-01-09 07:51:19 UTC
Permalink
Hello,

I would like to discuss the opportunity to improve template programming and
operator overloading for XQUERY ?

1) template programming : XQUERY provides a quite natural template
mechanism using type switch and instance:

declare function yell($animal) {
if ($animal instance of element()) then

return typeswitch($animal)
case element(cow) return "Moo" default return "..."

else "this is not an animal"
};

I think that it could be worthwhile to provide such a mechanism directly at
the interpreter level. For instance, the following code seems coherent and
concise :

declare function yell($animal as element(cow) ) {"Moo"}
declare function yell($animal as element() ) {".."}
declare function yell($animal ) {"This is not an animal"}

As far as I understand, the previous code violates XQUERY functions
identification, achieved through a couple (name, arity). Would it be
difficult to extend this identification mechanism ?

2) A quite interesting application to 1) would be to define operator
overloading

Indeed, I don't know how to overload operators with XQUERY. For instance I
would like to define something like

declare function operator+($left,$right){fn:for-each-pair($left,$right,
function($a + $b) { $a + $b } )}

I know that I can survive writing

declare function local:plus($left,$right){fn:for-each-pair($left,$right,
function($a + $b) { $a + $b } )}

but when one start to write algorithmic code, it is far easier and
understandable to write $a+$b than array:plus($a,$b). Moreover, it can be
used to expressions template, quite useful for linear algebra.
Michael Kay
2015-01-09 08:32:46 UTC
Permalink
You might like to look at how Evan Lenz tackled this idea in Carrot

http://www.balisage.net/Proceedings/vol7/html/Lenz01/BalisageVol7-Lenz01.html

Michael Kay
Saxonica
***@saxonica.com
+44 (0) 118 946 5893
Post by jean-marc Mercier
Hello,
I would like to discuss the opportunity to improve template programming and operator overloading for XQUERY ?
declare function yell($animal) {
if ($animal instance of element()) then
return typeswitch($animal)
case element(cow) return "Moo"
default return "..."
else "this is not an animal"
};
declare function yell($animal as element(cow) ) {"Moo"}
declare function yell($animal as element() ) {".."}
declare function yell($animal ) {"This is not an animal"}
As far as I understand, the previous code violates XQUERY functions identification, achieved through a couple (name, arity). Would it be difficult to extend this identification mechanism ?
2) A quite interesting application to 1) would be to define operator overloading
Indeed, I don't know how to overload operators with XQUERY. For instance I would like to define something like
declare function operator+($left,$right){fn:for-each-pair($left,$right,function($a + $b) { $a + $b } )}
I know that I can survive writing
declare function local:plus($left,$right){fn:for-each-pair($left,$right,function($a + $b) { $a + $b } )}
but when one start to write algorithmic code, it is far easier and understandable to write $a+$b than array:plus($a,$b). Moreover, it can be used to expressions template, quite useful for linear algebra.
_______________________________________________
http://x-query.com/mailman/listinfo/talk
Loading...