=begin pod :kind("Language") :subkind("Language") :category("fundamental")

=TITLE Phasers

=SUBTITLE Program execution phases and corresponding phaser blocks

The lifetime (execution timeline) of a program is broken up into phases.
A I<phaser> is a block of code called during a specific execution phase.

=head1 Phasers

A phaser block is just a trait of the closure containing it, and is
automatically called at the appropriate moment. These auto-called blocks
are known as I<phasers>, since they generally mark the transition from
one phase of computing to another. For instance, a C<CHECK> block is
called at the end of compiling a compilation unit. Other kinds of
phasers can be installed as well; these are automatically called at
various times as appropriate, and some of them respond to various
control exceptions and exit values. For instance, some phasers might be
called if the exit from a block is successful or not, with I<success> in
this case defined by returning with a defined value or list without any
C<Failure> or exception in the process.

Here is a summary:

=begin code :skip-test<listing>
  BEGIN {...} #  * at compile time, as soon as possible, only ever runs once
  CHECK {...} #  * at compile time, as late as possible, only ever runs once
   INIT {...} #  * at runtime, as soon as possible, only ever runs once
    END {...} #  at runtime, as late as possible, only ever runs once
    DOC [BEGIN|CHECK|INIT] {...} # only in documentation mode

  ENTER {...} #  * at every block entry time, repeats on loop blocks.
  LEAVE {...} #  at every block exit time (even stack unwinds from exceptions)
   KEEP {...} #  at every successful block exit, part of LEAVE queue
   UNDO {...} #  at every unsuccessful block exit, part of LEAVE queue

  FIRST {...} #  at loop initialization time, before any ENTER
   NEXT {...} #  at loop continuation time, before any LEAVE
   LAST {...} #  at loop termination time, after any LEAVE

    PRE {...} #  assert precondition at every block entry, before ENTER
   POST {...} #  assert postcondition at every block exit, after LEAVE

  CATCH {...} #  catch exceptions, before LEAVE
CONTROL {...} #  catch control exceptions, before LEAVE

   LAST {...} #  supply tapped by whenever-block is done, runs very last
   QUIT {...} #  catch async exceptions within a whenever-block, runs very last

COMPOSE {...} #  when a role is composed into a class (Not yet implemented)
  CLOSE {...} #  appears in a supply block, called when the supply is closed
=end code

Phasers marked with a C<*> have a runtime value, and if evaluated
earlier than their surrounding expression, they simply save their result
for use in the expression later when the rest of the expression is
evaluated:

    my $compiletime = BEGIN { now };
    our $random = ENTER { rand };

As with other statement prefixes, these value-producing constructs may
be placed in front of either a block or a statement:

    my $compiletime = BEGIN now;
    our $random = ENTER rand;

Most of these phasers will take either a block or a function reference.
The statement form can be particularly useful to expose a lexically
scoped declaration to the surrounding lexical scope without "trapping"
it inside a block.

These declare the same variables with the same scope as the preceding
example, but run the statements as a whole at the indicated time:

    BEGIN my $compiletime = now;
    ENTER our $random = rand;

(Note, however, that the value of a variable calculated at compile time may not
persist under runtime cloning of any surrounding closure.)

Most of the non-value-producing phasers may also be so used:

    END say my $accumulator;

Note, however, that

    END say my $accumulator = 0;

sets the variable to 0 at C<END> time, since that is when the "my" declaration
is actually executed. Only argumentless phasers may use the statement form.
This means that C<CATCH> and C<CONTROL> always require a block, since they take
an argument that sets C<$_> to the current topic, so that the innards are able
to behave as a switch statement. (If bare statements were allowed, the
temporary binding of C<$_> would leak out past the end of the C<CATCH> or
C<CONTROL>, with unpredictable and quite possibly dire consequences. Exception
handlers are supposed to reduce uncertainty, not increase it.)

X<|Phasers, will trait>
Some of these phasers also have corresponding traits that can be set on
variables; they use C<will> followed by the name of the phaser in lowercase.
These have the advantage of passing the variable in question into the closure as
its topic:

    our $h will enter { .rememberit() } will undo { .forgetit() };

Only phasers that can occur multiple times within a block are eligible for this
per-variable form; this excludes C<CATCH> and others like C<CLOSE> or C<QUIT>.

The topic of the block outside a phaser is still available as C<< OUTER::<$_>
>>. Whether the return value is modifiable may be a policy of the phaser in
question. In particular, the return value should not be modified within a
C<POST> phaser, but a C<LEAVE> phaser could be more liberal.

Any phaser defined in the lexical scope of a method is a closure that closes
over C<self> as well as normal lexicals. (Or equivalently, an implementation
may simply turn all such phasers into submethods whose primed invocant is the
current object.)

When multiple phasers are scheduled to run at the same moment, the general
tiebreaking principle is that initializing phasers execute in order declared,
while finalizing phasers execute in the opposite order, because setup and
teardown usually want to happen in the opposite order from each other.

=head2 Execution order

Compilation begins

=for code :skip-test<listing>
      BEGIN {...} #  at compile time, As soon as possible, only ever runs once
      CHECK {...} #  at compile time, As late as possible, only ever runs once
    COMPOSE {...} #  when a role is composed into a class (Not yet implemented)

Execution begins

=for code :skip-test<listing>
       INIT {...} #  at runtime, as soon as possible, only ever runs once

Before block execution begins

=for code :skip-test<listing>
        PRE {...} #  assert precondition at every block entry, before ENTER

Loop execution begins

=for code :skip-test<listing>
      FIRST {...} #  at loop initialization time, before any ENTER

Block execution begins

=for code :skip-test<listing>
      ENTER {...} #  at every block entry time, repeats on loop blocks.

Exception maybe happens

=for code :skip-test<listing>
      CATCH {...} #  catch exceptions, before LEAVE
    CONTROL {...} #  catch control exceptions, before LEAVE

End of loop, either continuing or finished

=for code :skip-test<listing>
       NEXT {...} #  at loop continuation time, before any LEAVE
       LAST {...} #  at loop termination time, after any LEAVE

End of block

=for code :skip-test<listing>
      LEAVE {...} #  when blocks exits, even stack unwinds from exceptions
       KEEP {...} #  at every successful block exit, part of LEAVE queue
       UNDO {...} #  at every unsuccessful block exit, part of LEAVE queue

Postcondition for block

=for code :skip-test<listing>
       POST {...} #  assert postcondition at every block exit, after LEAVE

Async whenever-block is complete

=for code :skip-test<listing>
       LAST {...} #  if ended normally with done, runs once after block
       QUIT {...} #  catch async exceptions

Program terminating

=for code :skip-test<listing>
        END {...} #  at runtime, ALAP, only ever runs once

=head1 Program execution phasers

=head2 X<BEGIN|Phasers, BEGIN>

Runs at compile time, as soon as the code in the phaser has compiled,
only runs once.

The return value is available for use in later phases:

    say "About to print 3 things";
    for ^3 {
        say ^10 .pick ~ '-' ~ BEGIN { say  "Generating BEGIN value"; ^10 .pick }
    }
    # OUTPUT:
    # Generating BEGIN value
    # About to print 3 things
    # 3-3
    # 4-3
    # 6-3

The C<^10 .pick> in the phaser is generated only once and is then re-used by the loop
during runtime. Note how the L<say|/routine/say> in the C<BEGIN> block is executed before
the L<say|/routine/say> that is above the loop.

=head2 X<CHECK|Phasers, CHECK>

Runs at compile time, as late as possible, only runs once.

Can have a return value that is provided even in later phases.

Code that is generated at runtime can still fire off C<CHECK> and C<INIT>
phasers, though of course those phasers can't do things that would require
travel back in time. You need a wormhole for that.

=head2 X<INIT|Phasers, INIT>

Runs after compilation during main execution, as soon as possible, only
runs once. It can have a return value that is provided even in later
phases.

When phasers are in different modules, the C<INIT> and C<END> phasers are
treated as if declared at C<use> time in the using module. (It is erroneous to
depend on this order if the module is used more than once, however, since the
phasers are only installed the first time they're noticed.)

Code that is generated at runtime can still fire off C<CHECK> and C<INIT>
phasers, though of course those phasers can't do things that would require
travel back in time. You need a wormhole for that.

An C<INIT> only runs once for all copies of a cloned closure.

=head2 X<END|Phasers, END>

Runs after compilation during main execution, as late as possible, only runs
once. It will close any open handles automatically.

When phasers are in different modules, the C<INIT> and C<END> phasers are
treated as if declared at C<use> time in the using module. (It is erroneous to
depend on this order if the module is used more than once, however, since the
phasers are only installed the first time they're noticed.)

=head1 Block phasers

Execution in the context of a block has its own phases.

Block-leaving phasers wait until the call stack is actually unwound to run.
Unwinding happens only after some exception handler decides to handle the
exception that way. That is, just because an exception is thrown past a stack
frame does not mean we have officially left the block yet, since the exception
might be resumable. In any case, exception handlers are specified to run within
the dynamic scope of the failing code, whether or not the exception is
resumable. The stack is unwound and the phasers are called only if an exception
is not resumed.

These can occur multiple times within the block. So they aren't really traits,
exactly--they add themselves onto a list stored in the actual trait. If you
examine the C<ENTER> trait of a block, you'll find that it's really a list of
phasers rather than a single phaser.

All of these phaser blocks can see any previously declared lexical variables,
even if those variables have not been elaborated yet when the closure is
invoked (in which case the variables evaluate to an undefined value.)

=head2 X<ENTER|Phasers, ENTER>

Runs at every block entry time, repeats on loop blocks.

Can have a return value that is provided even in later phases.

An exception thrown from an C<ENTER> phaser will abort the C<ENTER> queue, but
one thrown from a C<LEAVE> phaser will not.

=head2 X<LEAVE|Phasers, LEAVE>

Runs at every block exit time (even stack unwinds from exceptions),
except when the program exits abruptly (e.g. with
L«C<exit>|/routine/exit»).

C<LEAVE> phasers for a given block are necessarily evaluated after any
C<CATCH> and C<CONTROL> phasers. This includes the C<LEAVE> variants, C<KEEP>
and C<UNDO>. C<POST> phasers are evaluated after everything else, to guarantee
that even C<LEAVE> phasers can't violate postconditions.

An exception thrown from an C<ENTER> phaser will abort the C<ENTER> queue, but
one thrown from a C<LEAVE> phaser will not.

If a C<POST> fails or any kind of C<LEAVE> block throws an exception while the
stack is unwinding, the unwinding continues and collects exceptions to be
handled. When the unwinding is completed all new exceptions are thrown from
that point.

    sub answer() {
        LEAVE say „I say after the return value.“;

        42 # this is the return value
    }

B<Note:> be mindful of C<LEAVE> phasers directly in blocks of routines, as
they will get executed even when an attempt to call the routine with wrong
arguments is made:

    sub foo (Int) {
        say "Hello!";
        LEAVE say "oh noes!"
    }
    try foo rand; # OUTPUT: «oh noes!␤»

Although the subroutine's body did not get run, because the sub expects
an L<Int|/type/Int> and L«C<rand>|/routine/rand» returned a L<Num|/type/Num>, its block was
entered and left (when param binding failed), and so the C<LEAVE> phaser
I<was> run.

=head2 X<KEEP|Phasers, KEEP>

Runs at every successful block exit, as part of the C<LEAVE> queue (shares the
same order of execution).

=head2 X<UNDO|Phasers, UNDO>

Runs at every unsuccessful block exit, as part of the C<LEAVE> queue (shares the
same order of execution).

=head2 X<PRE|Phasers, PRE>

Asserts a precondition at every block entry. Runs before the C<ENTER> phase.

C<PRE> phasers fire off before any C<ENTER> or C<FIRST>.

The exceptions thrown by failing C<PRE> and C<POST> phasers cannot be caught by
a C<CATCH> in the same block, which implies that C<POST> phaser are not run if
a C<PRE> phaser fails.

=head2 X<POST|Phasers, POST>

Asserts a postcondition at every block entry. Runs after the C<LEAVE> phase.

For phasers such as C<KEEP> and C<POST> that are run when exiting a scope
normally, the return value (if any) from that scope is available as the current
topic within the phaser.

The C<POST> block can be defined in one of two ways. Either the corresponding
C<POST> is defined as a separate phaser, in which case C<PRE> and C<POST> share
no lexical scope. Alternately, any C<PRE> phaser may define its corresponding
C<POST> as an embedded phaser block that closes over the lexical scope of the
C<PRE>.

If a C<POST> fails or any kind of C<LEAVE> block throws an exception while the
stack is unwinding, the unwinding continues and collects exceptions to be
handled. When the unwinding is completed all new exceptions are thrown from
that point.

The exceptions thrown by failing C<PRE> and C<POST> phasers cannot be caught by
a C<CATCH> in the same block, which implies that C<POST> phaser are not run if
a C<PRE> phaser fails.

=head1 Loop phasers

C<FIRST>, C<NEXT>, and C<LAST> are meaningful only within the lexical scope of
a loop, and may occur only at the top level of such a loop block.

=head2 X<FIRST|Phasers, FIRST>

Runs at loop initialization, before c<ENTER>.

=head2 X<NEXT|Phasers, NEXT>

Runs when loop is continued (either through C<next> or because you got to the
bottom of the loop and are looping back around), before C<LEAVE>.

A C<NEXT> executes only if the end of the loop block is reached normally, or an
explicit C<next> is executed. In distinction to C<LEAVE> phasers, a C<NEXT>
phaser is not executed if the loop block is exited via any exception other than
the control exception thrown by C<next>. In particular, a C<last> bypasses
evaluation of C<NEXT> phasers.

=head2 X<LAST|Phasers, LAST>

Runs when a loop is finished because the condition is met, or when it exits
using C<last> or C<return>; it is executed after C<LEAVE>.

=head1 Exception handling phasers

=head2 X<CATCH|Phasers, CATCH>

Runs when an exception is raised by the current block, before the C<LEAVE> phase.
Also see L<Catching exceptions|/language/exceptions#Catching_exceptions>.

=head2 X<CONTROL|Phasers, CONTROL>

Runs when a control exception is raised by the current block, before the C<LEAVE>
phase. It is raised by C<return>, C<fail>, C<redo>, C<next>, C<last>, C<done>, C<emit>,
C<take>, C<warn>, C<proceed> and C<succeed>.

    say elems gather {
        CONTROL {
            when CX::Warn { say "WARNING!!! $_"; .resume }
            when CX::Take { say "Don't take my stuff"; .resume }
            when CX::Done { say "Done"; .resume }
        }
        warn 'people take stuff here';
        take 'keys';
        done;
    }
    # OUTPUT:
    # WARNING!!! people take stuff here
    # Don't take my stuff
    # Done
    # 0


=head1 Object phasers

=head2 X<COMPOSE|Phasers, COMPOSE> (Not yet implemented)

Runs when a role is composed into a class.

=head1 Asynchronous phasers

=head2 X<LAST|Asynchronous Phasers, LAST>

Runs when a L<Supply|/type/Supply> finishes with a call to C<done> or when a
C<supply> block exits normally. It runs completely after the C<whenever> block
it is placed within finishes.

This phaser reuses the name C<LAST>, but works differently from the C<LAST> loop
phaser. This phaser is similar to setting the C<done> routine while tapping a
supply with C<tap>.

=head2 X<QUIT|Asynchronous Phasers, QUIT>

Runs when a L<Supply|/type/Supply> terminates early with an exception. It runs
after the C<whenever> block it is placed within finishes.

This phaser is similar to setting the C<quit> routine while tapping a Supply
with C<tap>.

=head2 X<CLOSE|Asynchronous Phasers, CLOSE>

Appears in a supply block. Called when the supply is closed.

=head1 DOC phasers

=head2 X<DOC|Asynchronous Phasers, DOC>

The phasers C<BEGIN>, C<CHECK> and C<INIT> are run only in documentation mode
when prefixed with the C<DOC> keyword. The compiler is in documentation mode when run
with C<--doc>.

  DOC INIT { say 'init'  }  # prints 'init' at initialization time when in documentation mode.

=end pod

# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6
