Promises.html (6719B)
1 <html lang="en"> 2 <head> 3 <title>Promises - 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="Encapsulations.html#Encapsulations" title="Encapsulations"> 9 <link rel="next" href="Keyed-Variables.html#Keyed-Variables" title="Keyed Variables"> 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="Promises"></a> 28 <p> 29 Next: <a rel="next" accesskey="n" href="Keyed-Variables.html#Keyed-Variables">Keyed Variables</a>, 30 Previous: <a rel="previous" accesskey="p" href="Encapsulations.html#Encapsulations">Encapsulations</a>, 31 Up: <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">12 Promises</h2> 37 38 <p><a name="index-promises-155"></a> 39 <!-- TODO xref to $lazy, memoize, force --> 40 A promise is an object that represents the potential to determine a 41 value. The value may be the result of an arbitrary computation that 42 will not be performed until the value must be determined (constructor 43 <code>$lazy</code>); or, in advanced usage, the value may be determined 44 before the promise is constructed (constructor <code>memoize</code>). 45 46 <p>The value determined by a promise is obtained by forcing it 47 (applicative <code>force</code>). A given promise cannot determine 48 different values on different occasions that it is forced. Also, if a 49 promise determines its value by computation, and that computation has 50 already been completed, forcing the promise again will produce the 51 previously determined result without re-initiating the computation to 52 determine it. 53 54 <p>The Kernel data type promise is encapsulated. 55 56 <!-- TODO add xref to eq? and equal? --> 57 <p>The general rules for predicate <code>eq?</code> only require it to 58 distinguish promises if they can exhibit different behavior; the 59 resulting leeway for variation between implementations is similar, in 60 both cause and effect, to that for <code>eq?</code>-ness of operatives. For 61 example, if two promises, constructed on different occasions, would 62 perform the same computation to determine their values, and that 63 computation has no side-effects and must always return the same value, 64 the promises may or may not be <code>eq?</code>. Two promises are 65 <code>equal?</code> iff they are <code>eq?</code>. 66 67 <div class="defun"> 68 — Applicative: <b>promise?</b> (<var>promise? . objects</var>)<var><a name="index-promise_003f-156"></a></var><br> 69 <blockquote><p> The primitive type predicate for type promise. <code>promise?</code> 70 returns true iff all the objects in <code>objects</code> are of type 71 promise. 72 </p></blockquote></div> 73 74 <div class="defun"> 75 — Applicative: <b>force</b> (<var>force object</var>)<var><a name="index-force-157"></a></var><br> 76 <blockquote><p> If <code>object</code> is a promise, applicative <code>force</code> returns the 77 value determined by promise; otherwise, it returns <code>object</code>. 78 79 <p>The means used to force a promise depend on how the promise was 80 constructed. The description of each promise constructor specifies 81 how to force promises constructed by that constructor. 82 </p></blockquote></div> 83 84 <div class="defun"> 85 — Operative: <b>$lazy</b> (<var>$lazy expression</var>)<var><a name="index-g_t_0024lazy-158"></a></var><br> 86 <blockquote><p> Operative <code>$lazy</code> constructs and returns a new object of type 87 promise, representing potential evaluation of expression in the 88 dynamic environment from which <code>$lazy</code> was called. 89 90 <p>When the promise is forced, if a value has not previously been 91 determined for it, <code>expression</code> is evaluated in the dynamic 92 environment of the constructing call to <code>$lazy</code>. If, when the 93 evaluation returns a result, a value is found to have been determined 94 for the promise during the evaluation, the result is discarded in 95 favor of the previously determined value; otherwise, the result is 96 forced, and the value returned by that forcing becomes the value 97 determined by the promise. 98 99 <!-- TODO add xref to tail context --> 100 <p>Forcing an undetermined lazy promise (i.e., a promise constructed by 101 $lazy for which no value has yet been determined) may cause a 102 sequential series of evaluations, each of which returns a promise that 103 is forced and thus initiates the next evaluation in the series. The 104 implementation must support series of this kind with unbounded length 105 (i.e., unbounded number of sequential evaluations). 106 107 <!-- TODO add xref to eq? --> 108 <p>Note that forcing concerns the value determined by a given promise, 109 not the result of evaluating a given expression in a given 110 environment. Distinct promises (judged by <code>eq?</code> represent 111 different occasions of evaluation; so, even if they do represent 112 evaluation of the same expression in the same environment, forcing one 113 does not necessarily determine the value for the other, and actual 114 evaluation will take place the first time each of them is forced. 115 </p></blockquote></div> 116 117 <div class="defun"> 118 — Applicative: <b>memoize</b> (<var>memoize object</var>)<var><a name="index-memoize-159"></a></var><br> 119 <blockquote><p> Applicative <code>memoize</code> constructs and returns a new object of 120 type promise, representing memoization of <code>object</code>. Whenever the 121 promise is forced, it determines <code>object</code>. 122 </p></blockquote></div> 123 124 <div class="defun"> 125 — Operative: <b>$delay</b> (<var>$delay <expression></var>)<var><a name="index-g_t_0024delay-160"></a></var><br> 126 <blockquote><p>Operative <code>delay</code> behaves as the composition of <code>$lazy</code> and 127 <code>memoize</code>, that is: 128 <pre class="example"> ($delay <expr>) == ($lazy (memoize <expr>)) 129 </pre> 130 <p>SOURCE NOTE: this is taken from r7rs. 131 </p></blockquote></div> 132 133 <!-- *-texinfo-*- --> 134 </body></html> 135