| 1 | # | ||
| 2 | # Copyright (c) 2021 Christian Jaeger, copying@christianjaeger.ch | ||
| 3 | # | ||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||
| 7 | # bundled with this file. | ||
| 8 | # | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | FP::noLazy - make lazy syntax non-lazy | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | 1 | ▶ | use FP::noLazy; # instead of: use FP::Lazy; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | |||
| 18 | eval { | ||
| 19 | lazy { 1 / 0 }; | ||
| 20 | }; | ||
| 21 | like $@, qr/division by zero/; | ||
| 22 | |||
| 23 | my $x = lazy { 123*456 }; | ||
| 24 | ok !is_promise $x; # currently; see 'BUGS' | ||
| 25 | is $x, 56088; | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | Lazy evaluation is very useful to change the valuation order in purely | ||
| 30 | functional programs. It can also make debugging confusing, as the | ||
| 31 | order in which lazy expressions are evaluated depends on how they are | ||
| 32 | being used, unlike in non-lazy (eager, imperative) programming, where | ||
| 33 | evaluation order is directly visible from reading the program code. | ||
| 34 | |||
| 35 | For this reason, it can be helpful to temporarily disable lazy | ||
| 36 | evaluation. This can mean that a program isn't as efficient as it | ||
| 37 | would be, that it allocates lots of memory, or even runs out of | ||
| 38 | memory, but seeing how it behaves may point out the programming | ||
| 39 | error. So this is one tool to debug lazy functional programs. (Other | ||
| 40 | tools are enabing debug mode, see "DEBUGGING" in L<FP::Lazy> to see | ||
| 41 | the contexts in which promises were created, splitting the program | ||
| 42 | into smaller pieces, adding warn statements.) | ||
| 43 | |||
| 44 | Turning off lazy evaluation globally can be problematic, since it also | ||
| 45 | changes e.g. L<FP::Stream> or L<FP::IOStream> to evaluate eagerly, | ||
| 46 | which can lead to the program running out of memory before reaching | ||
| 47 | the stages to be debugged. Changing `lazy` to `lazy_if` (from | ||
| 48 | L<FP::Lazy>) can be tedious. Importing `lazy` (and the other lazy | ||
| 49 | forms) from L<FP::noLazy> instead of L<FP::Lazy> turns all lazy | ||
| 50 | statements in the current module to normal non-lazy (eager) ones, | ||
| 51 | i.e. the program then behaves as if the lazy forms weren't used at | ||
| 52 | all. You can now debug the program "as normal". | ||
| 53 | |||
| 54 | =head1 BUGS | ||
| 55 | |||
| 56 | As mentioned above, your program can use more memory or run out of | ||
| 57 | memory when using this module. If that happens, instead go back to | ||
| 58 | using L<FP::Lazy> and instead use `lazy_if` selectively to track down | ||
| 59 | the bugs. | ||
| 60 | |||
| 61 | `lazy` etc. from this module don't (currently) wrap the result of the | ||
| 62 | computation in a promise. This means that `is_promise` will return | ||
| 63 | false. This could lead your program to behave differently, although | ||
| 64 | maybe that should be considered a bug in your program. If you think | ||
| 65 | otherwise, please tell, and this module could be changed to still use | ||
| 66 | promise wrappers (even though they are already-evaluated). (It would | ||
| 67 | also be possible to configure this behaviour.) | ||
| 68 | |||
| 69 | =head1 SEE ALSO | ||
| 70 | |||
| 71 | L<FP::Lazy> | ||
| 72 | |||
| 73 | =head1 NOTE | ||
| 74 | |||
| 75 | This is alpha software! Read the status section in the package README | ||
| 76 | or on the L<website|http://functional-perl.org/>. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | package FP::noLazy; | ||
| 81 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 82 | 2 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 83 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 84 | 2 | ▶ | use warnings FATAL => 'uninitialized'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 85 | 2 | ▶ | use Exporter "import"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 86 | |||
| 87 | 2 | our @EXPORT = qw(lazy lazyT lazy_if lazyT_if lazyLight force FORCE is_promise); | |
| 88 | 2 | our @EXPORT_OK = qw(delay force_noeval lazy_backtrace is_forced); | |
| 89 | 2 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |
| 90 | |||
| 91 | 2 | ▶ | use FP::Docstring; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 92 | |||
| 93 | # For re-export: | ||
| 94 | 2 | ▶ | use FP::Lazy qw(is_forced lazy_backtrace is_promise force force_noeval FORCE); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 95 | |||
| 96 | 2 | ▶ | sub lazy (&) { |
|
Subroutine
lazy: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 97 | 2 | __ '`lazy { expr }`: NOTE: this is a noop as this is the variant | |
| 98 | from FP::noLazy!'; | ||
| 99 | 2 | goto $_[0] | |
| 100 | } | ||
| 101 | |||
| 102 | 0 | ▶ | sub lazyT (&$) { |
|
Subroutine
lazyT: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 103 | 0 | __ '`lazyT { expr } $classname`: expr must return an object that | |
| 104 | satisfies ->isa($classname). NOTE: this is the variant from | ||
| 105 | FP::noLazy, hence is evaluating immediately!'; | ||
| 106 | 0 | goto $_[0] | |
| 107 | |||
| 108 | # XXX still type-check, right? and do the same in FP::Lazy? | ||
| 109 | } | ||
| 110 | |||
| 111 | 0 | ▶ | sub lazy_if (&$) { |
|
Subroutine
lazy_if: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 112 | 0 | __ '`lazy_if { expr } $boolean`: NOTE: this is the variant from | |
| 113 | FP::noLazy, hence evaluates expr always immediately, | ||
| 114 | regardless of $boolean!'; | ||
| 115 | 0 | goto $_[0] | |
| 116 | } | ||
| 117 | |||
| 118 | 0 | ▶ | sub lazyT_if (&$$) { |
|
Subroutine
lazyT_if: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 119 | 0 | __ '`lazyT_if { expr } $classname, $boolean`: expr must return an | |
| 120 | object that satisfies ->isa($classname); NOTE: this is the | ||
| 121 | variant from FP::noLazy, hence evaluates expr always | ||
| 122 | immediately, regardless of $boolean!'; | ||
| 123 | 0 | goto $_[0] | |
| 124 | |||
| 125 | # XXX still type-check, right? and do the same in FP::Lazy? | ||
| 126 | } | ||
| 127 | |||
| 128 | # not providing for caching (1-time-only evaluation) | ||
| 129 | 0 | ▶ | sub lazyLight (&) { |
|
Subroutine
lazyLight: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 130 | 0 | __ 'NOTE: this is the variant from FP::noLazy, hence evaluates | |
| 131 | expr always immediately!'; | ||
| 132 | 0 | goto $_[0] | |
| 133 | } | ||
| 134 | |||
| 135 | sub delay (&); | ||
| 136 | 2 | *delay = \&lazy; | |
| 137 | sub delayLight (&); | ||
| 138 | 2 | *delayLight = \&lazyLight; | |
| 139 | |||
| 140 | 2 | 1 | |