This and other RFCs are available on the web at http://dev.perl.org/rfc/ =head1 TITLE Builtin: lazy =head1 VERSION Maintainer: David Nicol <[EMAIL PROTECTED]> Date: 17 Aug 2000 Last Modified: 1 Sep 2000 Mailing List: [EMAIL PROTECTED] Version: 2 Number: 123 Status: Developing =head1 ABSTRACT C<lazy> is suggested as a keyword to modify an array context as described in RFC24, "semi-finite (lazy) lists." C<?> will also work, when it is clear that we're not doing a ternary if. Also it will work with a block to create an array by subsequent calls to the block. "Busy" is proposed as a term for the normal lists we are familiar with, to differentiate them from "lazy" ones. A difference in assigning from lazy lists instead of busy lists is proposed. =head1 DESCRIPTION In addition to lazy evaluation optimizations provided by the language when it recognizes clear (to it, they may be totally invisible to even advanced programmers) syntactic hints, a "lazy array context" may be indicated by the presence of the new keyword C<lazy> or its shortened form C<?>. =head2 why a keyword is needed tie %h A_DATABASE_RETURNING_OBJECTS, ... ; for (grep {$h{$_}->STATE eq 'NY'} keys %h){ $h{$_}->send_advertisement(); }; We would like "keys" to return an RFC24 "lazy list" when it is compiler-obvious that it is safe to do so, meaning: =over =item the argument hash is never part of an L-value within the scope where the return value will be used =item the context is marked as accepting lazy lists ? =item the argument hash is never part of an L-value within a side effect =item the hash is marked iterator-safe, meaning that it is not vulnerable to deadlocks during iteration. =back The question is, how do we tell the compiler that "for" in this example requires a complete list, because the C<send_advertisement> method changes the value of its instance? Do all class methods start marking themselves as to whether they change their obejcts or not? This seems too complex, and very likely impossible to automate effectively at this time. =head2 make lazy keys the programmer's responsibility What if adding laziness to a list context was up to the programmer and passed through functions that can support it: for (lazy(grep {$h{$_}->STATE eq 'NY'} keys %h)){ $h{$_}->send_advertisement(); }; would cause a lazy list is passed to C<for>, and increment of the object's "letters_sent_total" field might break the iteration. for (grep {$h{$_}->STATE eq 'NY'} lazy(keys %h)){ $h{$_}->send_advertisement(); }; causes a lazy list to be passed to our filter function C<grep>, saving us from allocating the entire C<keys> array. C<Grep> is still in the default busy context, so it returns a busy array, which C<for> can iterate over safely. =head2 co-routine into array Another use of C<lazy> would be in defining a lazy array in the first place, as opposed to wanting to evaluate a subroutine. @naturals = lazy { my $x=1; yield $x++ }; alternately @naturals = ? { my $x=1; yield $x++ }; After this assignment, C<shift @naturals> will produce ever-increasing integers. The state of the subroutine can be reset to the initial state with C<reset \@naturals>. =head2 Assigning from lazy lists Lazy lists maintain their own iterator, which is advanced as accesses are made from them. I<Assigning a non-terminating lazy list to an unsized busy array will result in an infinite loop.> "Sized busy arrays" with a maximum point beyond which autovivification does not occur, should they exist, will not suffer this problem. When evaluated in a scalar context, instead of returning the total number of elements, which may not be knowable, a lazy list returns the first (or next) element, as if it was C<shift> is implicitly applied to it. Example: reset \@naturals; (@a,@b,@c2f{c..f}) = @naturals # $c2f{f} now == 6 $seven = @naturals; # $seven is 7 @keepgoing = ? @naturals; # lazy @keepgoing is now # a copy of @naturals reset \@naturals; print shift @keepgoing; # prints 8; print shift @naturals; # prints 1; @tenmore:size(10) = @keepgoing; # @tenmore is 9 .. 19 @default = @keepgoing; # endless loop =head2 Memoization of Lazy Lists Unless marked with an attribute indicating that the list is memoized, a lazy list is not only lazy but also forgetful. =head2 index within the co-routine it is possible that the "index" into the lazy array will be available via a reserved word from within such a co-routine. The name of this reserved word is a subject of great contention but I have not seen any convincing arguments against having such a keyword. Perhaps it will be a definable keyword. The following is therefore still developing: use CORE::LAZY::REFLEXIVEKEYWORD bubba; @fibonaccis:memoized=(0,1,?{$bubba[bubba:n-1]+$bubba[bubba:n-2]}); =head1 IMPLEMENTATION "Array context" splits into "lazy array context" and "busy array context" which is the default with which we are all familiar. Functions capable of returning lazy arrays (or which have been overloaded with homonymous functions that return lazy arrays, given overloading by context) return lazy arrays into a lazy array context. A lazy array evaluated in a busy array context becomes a busy array, so if there is no great loss in efficiency a lazy implementation, being more flexible, is preferred. If it becomes possible to identify lazy situations without a keyword, of course the optimizer is welcome to go for it. =head2 when the language will do it for us It is expected that in situations such as @KeySubset = grep {$h{$_}->STATE eq 'NY'} keys %h; where the entire array is to be accessed immediately, functions such as grep may automatically prefer lazy arrays over busy arrays. How this is specified in a parameter list is beyond the scope of this RFC. The stuff about blocks amounts to a wrapper that creates a package with a unique name and ties the lvalue to it. =head1 impact on legacy code Hardly any. $lazy, @lazy, %lazy are not affected. =head1 REFERENCES RFC24 semi-finite (lazy) lists RFC31 Co-routines (Conway) Tom Hughes pointed out the problem with hash iterators =head1 CHANGES minor rewrites, addition of optimizable example and allusion to the possibility of a lazy preference indicator in parameter lists. Introduction of the section on assigning Introduction of the section on reflexive Introduction of the section on memoization Ug.