OK, I've fixed it - but explaining it is a challenge. Essentially, when we do lazy evaluation of a variable or a function result, we return a Closure object that contains the expression and the values of all local variables on which it depends. We're failing to include the local variables in the case where (a) the returned value is a "coerced function item", that is, a function item wrapped in code that dynamically coerces it to the function type required by the static context where the function item is used, and (b) the dependency is on the arguments of that function item. Because these local variables are not included in the Closure, eventual evaluation of the Closure fails with an AssertionError saying there is no value for the required variable.

I think (it's late at night...) that the reason function coercion is invoked is that the signature of tree:insert isn't the same as the signature required for the first argument of fn:fold-left.

It's sufficiently complex that I'm not sure I can suggest a workaround.

Michael Kay
Saxonica


On 05/03/2011 22:13, Dimitre Novatchev wrote:
I have a problem with fold-left() which I cannot explain and this
might be a bug in Saxon9.3.04EE. All necessary files are attached in a
zip file.

This works as expected (applications/BinaryTree/SpellCheckWithSet.xquery):

import module namespace set = "http://fxsl.sf.net/data/ordered-set"
             at "../../Set/set.xquery";

 let $words :=
  ('more', 'a', 'with', 'did', 'about', 'just', 'at', 'above', 'can', 'am',
  'after', 'as', 'against', 'again', 'all', 'and', 'an', 'are', 'any',
  'being', 'be', 'by', 'been', 'because', 'before', 'between', 'below',
  'but', 'both', 'had', 'do', 'itself', 'during', 'does', 'further', 'don',
  'doing', 'down', 'few', 'each', 'from', 'for', 'if', 'has', 'its', 'having',
  'have', 'how', 'here', 'into', 'in', 'it', 'is', 'their', 'most', 'will',
  'out', 'no', 'the', 'off', 'nor', 'other', 'now', 'not', 'of', 'once', 'on',
  'or', 'only', 'so', 'over', 'that', 's', 'own', 'should', 'same', 'such',
  'some', 'than', 't', 'until', 'them', 'why', 'this', 'then', 'under',
  'these', 'there', 'they', 'through', 'those', 'too', 'to', 'when', 'up',
  'whom', 'was', 'very', 'what', 'were', 'which', 'where', 'who', 'while'),

    $dict := fold-left(set:add#2, set:set(), $words)

    return
      set:serialize($dict)



This produces:

"SystemID: C:\CVS-DDN\fxsl3\DataStructures\applications\binarytree\SpellCheck.xquery
Engine name: Saxon-EE XQuery 9.3.0.4
Severity: error
Description: Value of variable is undefined (null). Variable reference
$pTree at line 59 of
file:/C:/CVS-DDN/fxsl3/DataStructures/BinaryTree/bintreeModule.xquery.
Variable reference $dict at line 20 of
file:/C:/CVS-DDN/fxsl3/DataStructures/applications/binarytree/SpellCheck.xquery"


I would appreciate any help in explaining this behavior and for a
fix/workaround.

Here is the code of the main module (applications/BinaryTree/SpellCheck.xquery):

import module namespace tree = "http://fxsl.sf.net/data/bintree"
             at "../../BinaryTree/bintreeModule.xquery";

 let $words :=
  ('more', 'a', 'with', 'did', 'about', 'just', 'at', 'above', 'can', 'am',
  'after', 'as', 'against', 'again', 'all', 'and', 'an', 'are', 'any',
  'being', 'be', 'by', 'been', 'because', 'before', 'between', 'below',
  'but', 'both', 'had', 'do', 'itself', 'during', 'does', 'further', 'don',
  'doing', 'down', 'few', 'each', 'from', 'for', 'if', 'has', 'its', 'having',
  'have', 'how', 'here', 'into', 'in', 'it', 'is', 'their', 'most', 'will',
  'out', 'no', 'the', 'off', 'nor', 'other', 'now', 'not', 'of', 'once', 'on',
  'or', 'only', 'so', 'over', 'that', 's', 'own', 'should', 'same', 'such',
  'some', 'than', 't', 'until', 'them', 'why', 'this', 'then', 'under',
  'these', 'there', 'they', 'through', 'those', 'too', 'to', 'when', 'up',
  'whom', 'was', 'very', 'what', 'were', 'which', 'where', 'who', 'while'),

    $dict := fold-left(tree:insert#2, tree:tree(), $words)

    return
      tree:serialize($dict)




--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
Facts do not cease to exist because they are ignored.



------------------------------------------------------------------------------ What You Don't Know About Data Connectivity CAN Hurt You This paper provides an overview of data connectivity, details its effect on application quality, and explores various alternative solutions. http://p.sf.net/sfu/progress-d2d
_______________________________________________ saxon-help mailing list archived at http://saxon.markmail.org/ saxon-help@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/saxon-help