Debian Codegraph @

doc/coroutine.n

raw
1 '\"
2 '\" Copyright (c) 2009 Donal K. Fellows.
3 '\"
4 '\" See the file "license.terms" for information on usage and redistribution
5 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6 '\"
7 .TH coroutine n 8.6 Tcl "Tcl Built-In Commands"
8 .so man.macros
9 .BS
10 '\" Note: do not modify the .SH NAME line immediately below!
11 .SH NAME
12 coroutine, yield, yieldto \- Create and produce values from coroutines
13 .SH SYNOPSIS
14 .nf
15 \fBcoroutine \fIname command\fR ?\fIarg...\fR?
16 \fByield\fR ?\fIvalue\fR?
17 .VS TIP396
18 \fByieldto\fR \fIcommand\fR ?\fIarg...\fR?
19 \fIname\fR ?\fIvalue...\fR?
20 .VE TIP396
21 .fi
22 .BE
23 .SH DESCRIPTION
24 .PP
25 The \fBcoroutine\fR command creates a new coroutine context (with associated
26 command) named \fIname\fR and executes that context by calling \fIcommand\fR,
27 passing in the other remaining arguments without further interpretation. Once
28 \fIcommand\fR returns normally or with an exception (e.g., an error) the
29 coroutine context \fIname\fR is deleted.
30 .PP
31 Within the context, values may be generated as results by using the
32 \fByield\fR command; if no \fIvalue\fR is supplied, the empty string is used.
33 When that is called, the context will suspend execution and the
34 \fBcoroutine\fR command will return the argument to \fByield\fR. The execution
35 of the context can then be resumed by calling the context command, optionally
36 passing in the \fIsingle\fR value to use as the result of the \fByield\fR call
37 that caused
38 the context to be suspended. If the coroutine context never yields and instead
39 returns conventionally, the result of the \fBcoroutine\fR command will be the
40 result of the evaluation of the context.
41 .PP
42 .VS TIP396
43 The coroutine may also suspend its execution by use of the \fByieldto\fR
44 command, which instead of returning, cedes execution to some command called
45 \fIcommand\fR (resolved in the context of the coroutine) and to which \fIany
46 number\fR of arguments may be passed. Since every coroutine has a context
47 command, \fByieldto\fR can be used to transfer control directly from one
48 coroutine to another (this is only advisable if the two coroutines are
49 expecting this to happen) but \fIany\fR command may be the target. If a
50 coroutine is suspended by this mechanism, the coroutine processing can be
51 resumed by calling the context command optionally passing in an arbitrary
52 number of arguments. The return value of the \fByieldto\fR call will be the
53 list of arguments passed to the context command; it is up to the caller to
54 decide what to do with those values.
55 .PP
56 The recommended way of writing a version of \fByield\fR that allows resumption
57 with multiple arguments is by using \fByieldto\fR and the \fBreturn\fR
58 command, like this:
59 .PP
60 .CS
61 proc yieldm {value} {
62 \fByieldto\fR return -level 0 $value
63 }
64 .CE
65 .VE TIP396
66 .PP
67 The coroutine can also be deleted by destroying the command \fIname\fR, and
68 the name of the current coroutine can be retrieved by using
69 \fBinfo coroutine\fR.
70 If there are deletion traces on variables in the coroutine's
71 implementation, they will fire at the point when the coroutine is explicitly
72 deleted (or, naturally, if the command returns conventionally).
73 .PP
74 At the point when \fIcommand\fR is called, the current namespace will be the
75 global namespace and there will be no stack frames above it (in the sense of
76 \fBupvar\fR and \fBuplevel\fR). However, which command to call will be
77 determined in the namespace that the \fBcoroutine\fR command was called from.
78 .SH EXAMPLES
79 .PP
80 This example shows a coroutine that will produce an infinite sequence of
81 even values, and a loop that consumes the first ten of them.
82 .PP
83 .CS
84 proc allNumbers {} {
85 \fByield\fR
86 set i 0
87 while 1 {
88 \fByield\fR $i
89 incr i 2
90 }
91 }
92 \fBcoroutine\fR nextNumber allNumbers
93 for {set i 0} {$i < 10} {incr i} {
94 puts "received [\fInextNumber\fR]"
95 }
96 rename nextNumber {}
97 .CE
98 .PP
99 In this example, the coroutine acts to add up the arguments passed to it.
100 .PP
101 .CS
102 \fBcoroutine\fR accumulator apply {{} {
103 set x 0
104 while 1 {
105 incr x [\fByield\fR $x]
106 }
107 }}
108 for {set i 0} {$i < 10} {incr i} {
109 puts "$i -> [\fIaccumulator\fR $i]"
110 }
111 .CE
112 .PP
113 This example demonstrates the use of coroutines to implement the classic Sieve
114 of Eratosthenes algorithm for finding prime numbers. Note the creation of
115 coroutines inside a coroutine.
116 .PP
117 .CS
118 proc filterByFactor {source n} {
119 \fByield\fR [info coroutine]
120 while 1 {
121 set x [\fI$source\fR]
122 if {$x % $n} {
123 \fByield\fR $x
124 }
125 }
126 }
127 \fBcoroutine\fR allNumbers apply {{} {while 1 {\fByield\fR [incr x]}}}
128 \fBcoroutine\fR eratosthenes apply {c {
129 \fByield\fR
130 while 1 {
131 set n [\fI$c\fR]
132 \fByield\fR $n
133 set c [\fBcoroutine\fR prime$n filterByFactor $c $n]
134 }
135 }} allNumbers
136 for {set i 1} {$i <= 20} {incr i} {
137 puts "prime#$i = [\fIeratosthenes\fR]"
138 }
139 .CE
140 .PP
141 .VS TIP396
142 This example shows how a value can be passed around a group of three
143 coroutines that yield to each other:
144 .PP
145 .CS
146 proc juggler {name target {value ""}} {
147 if {$value eq ""} {
148 set value [\fByield\fR [info coroutine]]
149 }
150 while {$value ne ""} {
151 puts "$name : $value"
152 set value [string range $value 0 end-1]
153 lassign [\fByieldto\fR $target $value] value
154 }
155 }
156 \fBcoroutine\fR j1 juggler Larry [
157 \fBcoroutine\fR j2 juggler Curly [
158 \fBcoroutine\fR j3 juggler Moe j1]] "Nyuck!Nyuck!Nyuck!"
159 .CE
160 .VE TIP396
161 .SS "DETAILED SEMANTICS"
162 .PP
163 This example demonstrates that coroutines start from the global namespace, and
164 that \fIcommand\fR resolution happens before the coroutine stack is created.
165 .PP
166 .CS
167 proc report {where level} {
168 # Where was the caller called from?
169 set ns [uplevel 2 {namespace current}]
170 \fByield\fR "made $where $level context=$ns name=[info coroutine]"
171 }
172 proc example {} {
173 report outer [info level]
174 }
175 namespace eval demo {
176 proc example {} {
177 report inner [info level]
178 }
179 proc makeExample {} {
180 puts "making from [info level]"
181 puts [\fBcoroutine\fR coroEg example]
182 }
183 makeExample
184 }
185 .CE
186 .PP
187 Which produces the output below. In particular, we can see that stack
188 manipulation has occurred (comparing the levels from the first and second
189 line) and that the parent level in the coroutine is the global namespace. We
190 can also see that coroutine names are local to the current namespace if not
191 qualified, and that coroutines may yield at depth (e.g., in called
192 procedures).
193 .PP
194 .CS
195 making from 2
196 made inner 1 context=:: name=::demo::coroEg
197 .CE
198 .SH "SEE ALSO"
199 apply(n), info(n), proc(n), return(n)
200 .SH KEYWORDS
201 coroutine, generator
202 '\" Local Variables:
203 '\" mode: nroff
204 '\" fill-column: 78
205 '\" End: