[% setvar title Builtin: lazy %]
Note: these documents may be out of date. Do not use as reference! |
To see what is currently happening visit http://www.perl6.org/
Builtin: lazy
Maintainer: David Nicol <perl6rfc@davidnicol.com> Date: 17 Aug 2000 Last Modified: 1 Sep 2000 Mailing List: perl6-language@perl.org Number: 123 Version: 2 Status: Developing
lazy
is suggested as a keyword to modify an array context
as described in RFC24, "semi-finite (lazy) lists." ?
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.
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 lazy
or
its shortened form ?
.
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:
the argument hash is never part of an L-value within the scope where the return value will be used
the context is marked as accepting lazy lists ?
the argument hash is never part of an L-value within a side effect
the hash is marked iterator-safe, meaning that it is not vulnerable to deadlocks during iteration.
The question is, how do we tell the compiler that "for" in this
example requires a complete list, because the 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.
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 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 grep
, saving
us from allocating the entire keys
array. Grep
is still in
the default busy context, so it returns a busy array, which for
can iterate over safely.
Another use of 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, shift @naturals
will produce ever-increasing
integers. The state of the subroutine can be reset to the initial state
with reset \@naturals
.
Lazy lists maintain their own iterator, which is advanced as accesses are made from them.
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 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
Unless marked with an attribute indicating that the list is memoized, a lazy list is not only lazy but also forgetful.
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]});
"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.
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.
Hardly any. $lazy, @lazy, %lazy are not affected.
RFC24 semi-finite (lazy) lists
RFC31 Co-routines (Conway)
Tom Hughes pointed out the problem with hash iterators
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.