3.11. kink/KONT

Provides tagged delimited continuation (kont) of reset/shift.

Using reset/shift pair, the continuation can be extracted as a fun. Example:

:KONT.require_from('kink/')

:paren <- KONT.reset('Paren'){
  '(' + KONT.shift('Paren'){(:k) $k } + ')'
}

stdout.print_line(paren('foo')) # => (foo)
stdout.print_line(paren('bar')) # => (bar)

If KONT.shift is not called, KONT.reset returns the result of $thunk.

:KONT.require_from('kink/')
:Result <- KONT.reset('Not_used'){ 42 }
stdout.print_line(Result.repr) # => 42

Non-local exit can also be implemented using reset/shift pair. Example:

:KONT.require_from('kink/')

# jump out to the invoation of trap, with the result Val
:jump <- {(:Val)
  KONT.shift('Trap'){ Val }
}

# trap the jump
:trap <- {(:thunk)
  KONT.reset('Trap'){
    thunk
  }
}

:callee <- {
  jump('aborted')
  stdout.print_line('must not reach here')
}

:caller <- {
  trap{
    callee
    stdout.print_line('must not reach here')
  }
}

stdout.print_line(caller) # => aborted

Note that there is a fun dedicated to non-local exit: kink/CONTROL.escape. It is preferable to use this fun because it invokes cleanup thunks properly.

3.11.1. KONT.reset(Kont_tag $thunk)

KONT.reset delimits the continuation with the specified Kont_tag.

Preconditions:

• Kont_tag must be a str.

• $thunk must be a fun which takes no arg.

Using the terminology defined in Language specification → Execution chapter, KONT.reset pushes a delimitor frame with Kont_tag as the continuation tag string, then invokes $thunk with no arguments.

3.11.2. KONT.shift(Kont_tag $abort)

KONT.shift extracts the continuation delimited by KONT.reset.

Preconditions:

• Kont_tag must be a str.

• $abort must be a fun which takes a continuation function.

Using the terminology defined in Language specification → Execution chapter:

First, KONT.shift performs test-delimitor-existence operation with Kont_tag as the continuation tag string. If no delimitor frame with the continuation tag exists, an exception is raised.

Second, if a delimitor frame with the continuation tag exists, KONT.shift performs extract-slice operation, and makes a continuation function with the slice.

Third, KONT.shift performs remove-until-delimitor operation with the continuation tag.

Finally, KONT.shift invokes $abort with the continuation function as the argument.

3.11.3. KONT.can_shift?(Kont_tag)

KONT.can_shift? returns whether KONT.shift can be invoked with the specified Kont_tag.

Precondition:

• Kont_tag must be a str

Using the terminology defined in Language specification → Execution chapter, KONT.can_shift? performs test-delimitor-existence operation with Kont_tag as the continuation tag string. If a delimitor frame with the continuation tag exists, it continues with true value. If no delimitor frame with the continuation tag exists, it continues with false value.