6.4 Variable Access in a Break Loop

In a break loop access to variables of the current break level and higher levels is possible, but if the same variable name is used for different objects or if a function calls itself recursively, of course only the variable at the lowest level can be accessed.

  • DownEnv( [nr] ) F

    Moves up nr steps in the environment and allows one to inspect variables on this level. If nr is negative it steps down in the environment again. nr defaults to 1 if not given.

    gap> test:= function( n )
    >    if n > 3 then Error( "!" ); fi; test( n+1 ); end;;
    gap> test( 1 );
    Error ! at
    Error( "!" );
    Entering break read-eval-print loop,
    you can 'quit;' to quit to outer loop,
    or you can return to continue
    brk> Where();
    test( n + 1 ); called from
    test( n + 1 ); called from
    test( n + 1 ); called from
    <function>( <arguments> ) called from read-eval-loop
    brk> n;
    4
    brk> DownEnv();
    brk> n;
    3
    brk> Where();
    test( n + 1 ); called from
    test( n + 1 ); called from
    <function>( <arguments> ) called from read-eval-loop
    brk> DownEnv( 2 );
    brk> n;
    1
    brk> Where();
    <function>( <arguments> ) called from read-eval-loop
    brk> DownEnv( -2 );
    brk> n;
    3
    

    Note that the change of the environment caused by DownEnv only affects variable access in the break loop. If you use return to continue a calculation GAP automatically jumps to the right environment level again.

    Note also that search for variables looks first in the chain of outer functions which enclosed the definition of a currently executing function, before it looks at the chain of calling functions which led to the current invocation of the function.

    gap> foo := function() 
    > local x; x := 1; 
    > return function() local y; y := x*x; Error("!!"); end; 
    > end;  
    function(  ) ... end
    gap> bar := foo();
    function(  ) ... end
    gap> fun := function() local x; x := 3; bar(); end;
    function(  ) ... end
    gap> fun();
    Error !! at
    Error( "!!" );
    bar(  ); called from
    <function>( <arguments> ) called from read-eval-loop
    Entering break read-eval-print loop, you can 'quit;' to quit to outer loop,
    or you can return to continue
    brk> x;
    1
    brk> DownEnv(1);
    brk> x;
    3
    

    Here the x of foo which contained the definition of bar is found before that of fun which caused its execution. Using DownEnv we can access the x from fun.

    [Top] [Previous] [Up] [Next] [Index]

    GAP 4 manual
    February 2000