klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

Continuations.html (12670B)


      1 <html lang="en">
      2 <head>
      3 <title>Continuations - klisp Reference Manual</title>
      4 <meta http-equiv="Content-Type" content="text/html">
      5 <meta name="description" content="klisp Reference Manual">
      6 <meta name="generator" content="makeinfo 4.13">
      7 <link title="Top" rel="start" href="index.html#Top">
      8 <link rel="prev" href="Combiners.html#Combiners" title="Combiners">
      9 <link rel="next" href="Encapsulations.html#Encapsulations" title="Encapsulations">
     10 <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
     11 <meta http-equiv="Content-Style-Type" content="text/css">
     12 <style type="text/css"><!--
     13   pre.display { font-family:inherit }
     14   pre.format  { font-family:inherit }
     15   pre.smalldisplay { font-family:inherit; font-size:smaller }
     16   pre.smallformat  { font-family:inherit; font-size:smaller }
     17   pre.smallexample { font-size:smaller }
     18   pre.smalllisp    { font-size:smaller }
     19   span.sc    { font-variant:small-caps }
     20   span.roman { font-family:serif; font-weight:normal; } 
     21   span.sansserif { font-family:sans-serif; font-weight:normal; } 
     22 --></style>
     23 <link rel="stylesheet" type="text/css" href="css/style.css">
     24 </head>
     25 <body>
     26 <div class="node">
     27 <a name="Continuations"></a>
     28 <p>
     29 Next:&nbsp;<a rel="next" accesskey="n" href="Encapsulations.html#Encapsulations">Encapsulations</a>,
     30 Previous:&nbsp;<a rel="previous" accesskey="p" href="Combiners.html#Combiners">Combiners</a>,
     31 Up:&nbsp;<a rel="up" accesskey="u" href="index.html#Top">Top</a>
     32 <hr>
     33 </div>
     34 
     35 <!-- node-name,  next,  previous,  up -->
     36 <h2 class="chapter">10 Continuations</h2>
     37 
     38 <p><a name="index-continuations-141"></a>
     39   A continuation is a plan for all future computation, parameterized
     40 by a value to be provided, and contingent on the states of all mutable
     41 data structures (which notably may include environments). When the
     42 Kernel evaluator is invoked, the invoker provides a continuation to
     43 which the result of the evaluation will normally be returned.
     44 
     45    <p>For example, when <code>$if</code> evaluates its test operand, the
     46 continuation provided for the result expects to be given a boolean
     47 value; and, depending on which boolean it gets, it will evaluate
     48 either the consequent or the alternative operand as a tail context —
     49 that is, the continuation provided for the result of evaluating the
     50 selected operand is the same continuation that was provided for the
     51 result of the call to <code>$if</code>.
     52 
     53    <p>A Kernel program may sometimes capture a continuation; that is,
     54 acquire a reference to it as a first-class object. The basic means of
     55 continuation capture is applicative <code>call/cc</code>.  Given a
     56 first-class continuation <code>c</code>, a combiner can be constructed that
     57 will abnormally pass its operand tree to <code>c</code> (as opposed to the
     58 <!-- TODO add xref to abnormal pass -->
     59 normal return of values to continuations). In the simplest case, the
     60 abnormally passed value arrives at <code>c</code> as if it had been normally
     61 returned to <code>c</code>. In general, continuations bypassed by the
     62 abnormal pass may have entry/exit guards attached to them, and these
     63 guards can intercept the abnormal pass before it reaches <code>c</code>. 
     64 Each entry/exit guard consists of a selector continuation, which
     65 designates which abnormal passes the guard will intercept, and an
     66 interceptor applicative that performs the interception when selected. 
     67 <!-- TODO add xref to guard-continuation, continuation->applicative -->
     68 <!-- and abnormal pass -->
     69 
     70    <p>Continuations are immutable, and are <code>equal?</code> iff <code>eq?</code>. 
     71 The continuation type is encapsulated.
     72 
     73 <!-- TODO add dynamic extent & guard selection/interception to the intro -->
     74 <div class="defun">
     75 &mdash; Applicative: <b>continuation?</b> (<var>continuation? . objects</var>)<var><a name="index-continuation_003f-142"></a></var><br>
     76 <blockquote><p>  The primitive type predicate for type continuation. 
     77 <code>continuation?</code> returns true iff all the objects in
     78 <code>objects</code> are of type continuation. 
     79 </p></blockquote></div>
     80 
     81 <div class="defun">
     82 &mdash; Applicative: <b>call/cc</b> (<var>call/cc combiner</var>)<var><a name="index-call_002fcc-143"></a></var><br>
     83 <blockquote><p>  Calls <code>combiner</code> in the dynamic environment as a tail context,
     84 passing as sole operand to it the continuation to which <code>call/cc</code>
     85 would normally return its result.  (That is, constructs such a
     86 combination and evaluates it in the dynamic environment.) 
     87 <!-- TODO add xref Cf. operative $let/cc , §7.3.2. -->
     88 </p></blockquote></div>
     89 
     90 <div class="defun">
     91 &mdash; Applicative: <b>extend-continuation</b> (<var>extend-continuation continuation applicative </var>[<var>environment</var>])<var><a name="index-extend_002dcontinuation-144"></a></var><br>
     92 <blockquote><p>  The <code>extend-continuation</code> applicative constructs and returns a
     93 new child of <code>continuation</code> that, when it normally receives a
     94 value v, calls the underlying combiner of <code>applicative</code> with
     95 dynamic environment <code>environment</code> (or an empty environment if
     96 none was specified) and operand tree <code>v</code>, the result of the call
     97 normally to be returned to <code>continuation</code>.
     98 
     99         <p>The following equivalnece defines the short version:
    100      <pre class="example">          (extend-continuation c a) ==
    101             (extend-continuation c a (make-environment))
    102 </pre>
    103         </blockquote></div>
    104 
    105 <div class="defun">
    106 &mdash; Applicative: <b>guard-continuation</b> (<var>guard-continuation entry-guards continuation exit-guards</var>)<var><a name="index-guard_002dcontinuation-145"></a></var><br>
    107 <blockquote><p>  <code>entry-guards</code> and <code>exit-guards</code> should each be a list of
    108 clauses; each clause should be a list of length two, whose first
    109 element is a continuation, and whose second element is an applicative
    110 whose underlying combiner is operative.
    111 
    112         <p>Applicative <code>guard-continuation</code> constructs two continuations:
    113 a child of continuation, called the <code>outer continuation</code>; and a
    114 child of the <code>outer continuation</code>, called the <code>inner
    115 continuation</code>.  The <code>inner continuation</code> is returned as the
    116 result of the call to <code>guard-continuation</code>.
    117 
    118         <p>When the <code>inner continuation</code> normally receives a value, it
    119 passes the value normally to the <code>outer continuation</code>; and when
    120 the <code>outer continuation</code> normally receives a value, it passes the
    121 value normally to <code>continuation</code>. Thus, in the absence of
    122 abnormal passing, the inner and outer continuations each have the same
    123 behavior as <code>continuation</code>.
    124 
    125         <p>The two elements of each guard clause are called, respectively, the
    126 <code>selector</code> and the <code>interceptor</code>.  The <code>selector</code>
    127 continuation is used in deciding whether to intercept a given abnormal
    128 pass, and the <code>interceptor</code> applicative is called to perform
    129 <!-- TODO add xref to selection and interception -->
    130 customized action when interception occurs.
    131 
    132      <!-- TODO add xref to evaluation structure -->
    133         <p>At the beginning of the call to <code>guard-continuation</code>, internal
    134 copies are made of the evaluation structures of <code>entry-guards</code>
    135 and <code>exit-guards</code>, so that the selectors and interceptors
    136 contained in the arguments at that time remain fixed thereafter,
    137 independent of any subsequent mutations to the arguments. 
    138 </p></blockquote></div>
    139 
    140 <div class="defun">
    141 &mdash; Applicative: <b>continuation-&gt;applicative</b> (<var>continuation-&gt;applicative continuation</var>)<var><a name="index-continuation_002d_003eapplicative-146"></a></var><br>
    142 <blockquote><p>  Returns an applicative whose underlying operative abnormally passes
    143 its operand tree to <code>continuation</code>, thus: A series of
    144 interceptors are selected to handle the abnormal pass, and a
    145 continuation is derived that will normally perform all the
    146 interceptions in sequence and pass some value to the destination of
    147 the originally abnormal pass.  The operand tree is then normally
    148 passed to the derived continuation. 
    149 <!-- TODO add xref to selection and interception -->
    150 </p></blockquote></div>
    151 
    152 <div class="defun">
    153 &mdash; Variable: <b>root-continuation</b><var><a name="index-root_002dcontinuation-147"></a></var><br>
    154 <blockquote><p>  This continuation is the ancestor of all other continuations. When
    155 it normally receives a value, it terminates the Kernel session. (For
    156 example, if the system is running a read-eval-print loop, it exits the
    157 loop.) 
    158 <!-- TODO add xref  Cf. applicative exit, §7.3.4. -->
    159 </p></blockquote></div>
    160 
    161 <div class="defun">
    162 &mdash; Variable: <b>error-continuation</b><var><a name="index-error_002dcontinuation-148"></a></var><br>
    163 <blockquote><p>  The dynamic extent of this continuation is mutually disjoint from
    164 the dynamic extent in which Kernel computation usually occurs (such as
    165 the dynamic extent in which the Kernel system would run a
    166 read-eval-print loop).
    167 
    168         <p>When this continuation normally receives a value, it provides a
    169 diagnostic message to the user of the Kernel system, on the assumption
    170 that the received value is an attempt to describe some error that
    171 aborted a computation; and then resumes operation of the Kernel system
    172 at some point that is outside of all user-defined computation. (For
    173 example, if the system is running a read-eval-print loop, operation
    174 may resume by continuing from the top of the loop.)
    175 
    176         <p>The diagnostic message is not made available to any Kernel
    177 computation, and is therefore permitted to contain information that
    178 violates abstractions within the system.
    179 
    180      <!-- TODO add details about klisp error messages -->
    181         <p>When an error is signaled during a Kernel computation, the signaling
    182 action consists of an abnormal pass to some continuation in the
    183 dynamic extent of <code>error-continuation</code>. 
    184 </p></blockquote></div>
    185 
    186 <div class="defun">
    187 &mdash; Applicative: <b>apply-continuation</b> (<var>apply-continuation continuation object</var>)<var><a name="index-apply_002dcontinuation-149"></a></var><br>
    188 <blockquote><p>  Applicative <code>apply-continuation</code> converts its first argument to
    189 an applicative as if by <code>continuation-&gt;applicative</code>, and then
    190 applies it as usual.
    191 
    192         <p>That is:
    193      <pre class="example">          (apply-continuation continuation object) ==
    194             (apply (continuation-&gt;applicative continuation) object)
    195 </pre>
    196         </blockquote></div>
    197 
    198 <div class="defun">
    199 &mdash; Operative: <b>$let/cc</b> (<var>$let/cc &lt;symbol&gt; . &lt;objects&gt;</var>)<var><a name="index-g_t_0024let_002fcc-150"></a></var><br>
    200 <blockquote><p>  A child environment <code>e</code> of the dynamic environment is created,
    201 containing a binding of <code>&lt;symbol&gt;</code> to the continuation to which
    202 the result of the call to <code>$let/cc</code> should normally return; then,
    203 the subexpressions of <code>&lt;objects&gt;</code> are evaluated in <code>e</code> from
    204 left to right, with the last (if any) evaluated as a tail context, or
    205 if <code>&lt;objects&gt;</code> is empty the result is inert.
    206 
    207         <p>That is:
    208      <pre class="example">          ($let/cc symbol . objects) ==
    209             (call/cc ($lambda (symbol) . objects))
    210 </pre>
    211         </blockquote></div>
    212 
    213 <div class="defun">
    214 &mdash; Applicative: <b>guard-dynamic-extent</b> (<var>guard-dynamic-extent entry-guards combiner exit-guards</var>)<var><a name="index-guard_002ddynamic_002dextent-151"></a></var><br>
    215 <blockquote><p>  This applicative extends the current continuation with the specified
    216 guards, and calls <code>combiner</code> in the dynamic extent of the new
    217 continuation, with no operands and the dynamic environment of the call
    218 to <code>guard-dynamic-extent</code>. 
    219 </p></blockquote></div>
    220 
    221 <div class="defun">
    222 &mdash; Applicative: <b>exit</b> (<var>exit </var>[<var>object</var>])<var><a name="index-exit-152"></a></var><br>
    223 <blockquote><!-- TODO add xref -->
    224         <p>Applicative <code>exit</code> initiates an abnormal transfer of
    225 <code>object</code> (or <code>#inert</code> if <code>object</code> was not specified),
    226 to <code>root-continuation</code>. 
    227   That is:
    228      <pre class="example">          (exit) == (apply-continuation root-continuation #inert)
    229           (exit obj) == (apply-continuation root-continuation obj)
    230 </pre>
    231         <p>SOURCE NOTE: This applicative doesn't have the optional argument in
    232 the report.  It was added to klisp to allow a simple way to terminate
    233 the interpreter passing a value that is then tried to convert to an
    234 exit status. 
    235 </p></blockquote></div>
    236 
    237 <!-- *-texinfo-*- -->
    238    </body></html>
    239