Re: Formal equivalents using fold-left

As with everything else in the specification, the formal equivalents are designed to provide a contract between implementators and users, which both parties should be capable of understanding.

The ideal is that everything should be defined directly or indirectly in terms of primitives in the data model. We should avoid the "formal equivalents" becoming circular, and we should try to avoid reliance of constructs that don't themselves have a formal equivalent - which includes things like the "!" operator and FLWOR expressions. In an ideal world we would define a core language and everything else would be defined in terms of that core.

With the array functions I did actually check that all the formal equivalents passed the tests and had no dependencies on array operations other than the primitives in the data model. For sequence functions we're not there yet.

Michael Kay

> On 12 Mar 2025, at 10:36, Christian Grün <cg@basex.org> wrote:
> 
> > In most case it's easy enough, for example fn:filter becomes
> >
> > fold-left($input ! {'item': ., 'pos': position()},
> >          (),
> >          fn($result, $pair) {
> >            if ($predicate($pair?item, $pair?pos))
> >            then ($result, $pair?item)
> >            else $result
> >          })  
> 
> …and the function call would need to be followed by an ?item lookup.
> 
> One question may be who is supposed to be the recipient of the equivalent code. If we want to address users, I think that we should focus on simplicity. For fn:filter, a FLWOR expression may be much easier to digest:
> 
>   for $item at $pos in $input
>   where $predicate($item, $pos)
>   return $item
> 
> Next, many functions (including fn:index-of or fn:avg) don’t seem to have a formal counterpart. Maybe it’s not required in all cases? Folds are a good example: For advanced users, it is obvious what it does, and there will be no need to digest the equivalent code. For others, the main challenge is to understand the very principle, and tutorials will probably be more helpful than a concise recursive function.

Received on Wednesday, 12 March 2025 11:16:16 UTC