1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 049continuation.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #d0d0d0; background-color: #080808; }
body { font-family: monospace; color: #d0d0d0; background-color: #080808; }
* { font-size: 1em; }
.traceContains { color: #008000; }
.Constant { color: #008080; }
.SalientComment { color: #00ffff; }
.traceAbsent { color: #c00000; }
.Comment { color: #8080ff; }
.Delimiter { color: #c000c0; }
.Special { color: #ff6060; }
.Identifier { color: #008080; }
.CommentedCode { color: #6c6c6c; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Continuations are a powerful primitive for constructing advanced kinds of</span>
<span class="Comment">//: control *policies* like back-tracking. They're usually provided using a</span>
<span class="Comment">//: primitive called 'call-cc': <a href="http://en.wikipedia.org/wiki/Call-with-current-continuation)">http://en.wikipedia.org/wiki/Call-with-current-continuation)</a></span>
<span class="Comment">//: But in mu 'call-cc' is constructed out of a combination of two primitives:</span>
<span class="Comment">//: 'current-continuation', which returns a continuation, and</span>
<span class="Comment">//: 'continue-from', which takes a continuation to switch to.</span>
<span class="Comment">//: todo: implement continuations in mu's memory</span>
<span class="Delimiter">:(before "End Globals")</span>
map<long long int<span class="Delimiter">,</span> call_stack> Continuation<span class="Delimiter">;</span>
long long int Next_continuation_id = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Setup")</span>
Continuation<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
Next_continuation_id = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Mu Types Initialization")</span>
type_number continuation = Type_number[<span class="Constant">"continuation"</span>] = Next_type_number++<span class="Delimiter">;</span>
Type[continuation]<span class="Delimiter">.</span>name = <span class="Constant">"continuation"</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
CURRENT_CONTINUATION<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
Recipe_number[<span class="Constant">"current-continuation"</span>] = CURRENT_CONTINUATION<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
case CURRENT_CONTINUATION: <span class="Delimiter">{</span>
<span class="Comment">// copy the current call stack</span>
Continuation[Next_continuation_id] = Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">;</span> <span class="Comment">// deep copy because calls have no pointers</span>
<span class="Comment">// make sure calling the copy doesn't spawn the same continuation again</span>
++Continuation[Next_continuation_id]<span class="Delimiter">.</span>front<span class="Delimiter">().</span>running_step_index<span class="Delimiter">;</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>Next_continuation_id<span class="Delimiter">);</span>
++Next_continuation_id<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">"current-continuation"</span><span class="Delimiter">)</span> << <span class="Constant">"new continuation "</span> << Next_continuation_id<span class="Delimiter">;</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
CONTINUE_FROM<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
Recipe_number[<span class="Constant">"continue-from"</span>] = CONTINUE_FROM<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
case CONTINUE_FROM: <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span>
long long int c = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
Current_routine<span class="Delimiter">-></span>calls = Continuation[c]<span class="Delimiter">;</span> <span class="Comment">// deep copy because calls have no pointers</span>
<span class="Comment">// refresh instruction_counter to next instruction after current-continuation</span>
instruction_counter = current_step_index<span class="Delimiter">()</span>+<span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// not done with caller; don't increment current_step_index() further</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario continuation)</span>
<span class="Comment"># simulate a loop using continuations</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Constant">2</span>:continuation<span class="Special"> <- </span>current-continuation
<span class="Delimiter">{</span>
<span class="CommentedCode">#? $print 1:number</span>
<span class="Constant">3</span>:boolean<span class="Special"> <- </span>greater-or-equal <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">3</span>:literal
<span class="Identifier">break</span>-if <span class="Constant">3</span>:boolean
<span class="Constant">1</span>:number<span class="Special"> <- </span>add <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">1</span>:literal
<span class="Identifier">continue</span>-from <span class="Constant">2</span>:continuation <span class="Comment"># loop</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+mem: storing 1 in location 1</span>
<span class="traceContains">+mem: storing 2 in location 1</span>
<span class="traceContains">+mem: storing 3 in location 1</span>
<span class="traceAbsent">-mem: storing 4 in location 1</span>
<span class="Comment"># ensure every iteration doesn't copy the stack over and over</span>
$current-continuation: <span class="Constant">1</span>
<span class="Delimiter">:(scenario continuation_inside_caller)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Constant">2</span>:continuation<span class="Special"> <- </span>loop-body
<span class="Delimiter">{</span>
<span class="Constant">3</span>:boolean<span class="Special"> <- </span>greater-or-equal <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">3</span>:literal
<span class="Identifier">break</span>-if <span class="Constant">3</span>:boolean
<span class="Identifier">continue</span>-from <span class="Constant">2</span>:continuation <span class="Comment"># loop</span>
<span class="Delimiter">}</span>
]
recipe loop-body [
<span class="Constant">4</span>:continuation<span class="Special"> <- </span>current-continuation
<span class="Constant">1</span>:number<span class="Special"> <- </span>add <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">1</span>:literal
]
<span class="traceContains">+mem: storing 1 in location 1</span>
<span class="traceContains">+mem: storing 2 in location 1</span>
<span class="traceContains">+mem: storing 3 in location 1</span>
<span class="traceAbsent">-mem: storing 4 in location 1</span>
<span class="SalientComment">//:: A variant of continuations is the 'delimited' continuation that can be called like any other recipe.</span>
<span class="Comment">//:</span>
<span class="Comment">//: In mu, this is constructed out of two primitives:</span>
<span class="Comment">//:</span>
<span class="Comment">//: * 'create-delimited-continuation' lays down a mark on the call</span>
<span class="Comment">//: stack and calls the provided function (it is sometimes called 'prompt')</span>
<span class="Comment">//: * 'reply-current-continuation' copies the top of the stack until the</span>
<span class="Comment">//: mark, and returns it as the response of create-delimited-continuation</span>
<span class="Comment">//: (which might be a distant ancestor on the call stack; intervening calls</span>
<span class="Comment">//: don't return)</span>
<span class="Comment">//:</span>
<span class="Comment">//: The resulting slice of the stack can now be called just like a regular</span>
<span class="Comment">//: function.</span>
<span class="Delimiter">:(scenario delimited_continuation)</span>
recipe main [
<span class="Constant">1</span>:continuation<span class="Special"> <- </span>create-delimited-continuation f:recipe <span class="Constant">12</span>:literal <span class="Comment"># 12 is an argument to f</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">5</span>:literal
<span class="Delimiter">{</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>call <span class="Constant">1</span>:continuation<span class="Delimiter">,</span> <span class="Constant">2</span>:number <span class="Comment"># 2 is an argument to g, the 'top' of the continuation</span>
<span class="Constant">3</span>:boolean<span class="Special"> <- </span>greater-or-equal <span class="Constant">2</span>:number<span class="Delimiter">,</span> <span class="Constant">8</span>:literal
<span class="Identifier">break</span>-if <span class="Constant">3</span>:boolean
loop
<span class="Delimiter">}</span>
]
recipe f [
<span class="Constant">11</span>:number<span class="Special"> <- </span>next-ingredient
<span class="Constant">12</span>:number<span class="Special"> <- </span>g <span class="Constant">11</span>:number
reply <span class="Constant">12</span>:number
]
recipe g [
<span class="Constant">21</span>:number<span class="Special"> <- </span>next-ingredient
rewind-ingredients
reply-delimited-continuation
<span class="Comment"># calls of the continuation start from here</span>
<span class="Constant">22</span>:number<span class="Special"> <- </span>next-ingredient
<span class="Constant">23</span>:number<span class="Special"> <- </span>add <span class="Constant">22</span>:number<span class="Delimiter">,</span> <span class="Constant">1</span>:literal
reply <span class="Constant">23</span>:number
]
<span class="CommentedCode">#? ?</span>
<span class="Comment"># first call of 'g' executes the part before reply-delimited-continuation</span>
<span class="traceContains">+mem: storing 12 in location 21</span>
<span class="traceContains">+run: 2:number <- copy 5:literal</span>
<span class="traceContains">+mem: storing 5 in location 2</span>
<span class="Comment"># calls of the continuation execute the part after reply-delimited-continuation</span>
<span class="traceContains">+run: 2:number <- call 1:continuation, 2:number</span>
<span class="traceContains">+mem: storing 5 in location 22</span>
<span class="traceContains">+mem: storing 6 in location 2</span>
<span class="traceContains">+run: 2:number <- call 1:continuation, 2:number</span>
<span class="traceContains">+mem: storing 6 in location 22</span>
<span class="traceContains">+mem: storing 7 in location 2</span>
<span class="traceContains">+run: 2:number <- call 1:continuation, 2:number</span>
<span class="traceContains">+mem: storing 7 in location 22</span>
<span class="traceContains">+mem: storing 8 in location 2</span>
<span class="Comment"># first call of 'g' does not execute the part after reply-delimited-continuation</span>
<span class="traceAbsent">-mem: storing 12 in location 22</span>
<span class="Comment"># calls of the continuation don't execute the part before reply-delimited-continuation</span>
<span class="traceAbsent">-mem: storing 5 in location 21</span>
<span class="traceAbsent">-mem: storing 6 in location 21</span>
<span class="traceAbsent">-mem: storing 7 in location 21</span>
<span class="Comment"># termination</span>
<span class="traceAbsent">-mem: storing 9 in location 2</span>
<span class="Comment">//: 'create-delimited-continuation' is like 'call' except it adds a 'reset' mark to</span>
<span class="Comment">//: the call stack</span>
<span class="Delimiter">:(before "End call Fields")</span>
bool is_reset<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End call Constructor")</span>
is_reset = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Comment">//: like call, but mark the current call as a 'reset' call before pushing the next one on it</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
CREATE_DELIMITED_CONTINUATION<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
Recipe_number[<span class="Constant">"create-delimited-continuation"</span>] = CREATE_DELIMITED_CONTINUATION<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
case CREATE_DELIMITED_CONTINUATION: <span class="Delimiter">{</span>
Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>front<span class="Delimiter">().</span>is_reset = <span class="Constant">true</span><span class="Delimiter">;</span>
Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>push_front<span class="Delimiter">(</span>call<span class="Delimiter">(</span>Recipe_number[current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name]<span class="Delimiter">));</span>
ingredients<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>begin<span class="Delimiter">());</span> <span class="Comment">// drop the callee</span>
<span class="Identifier">goto</span> complete_call<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">//: save the slice of current call stack until the 'create-delimited-continuation'</span>
<span class="Comment">//: call, and return it as the result.</span>
<span class="Comment">//: todo: implement delimited continuations in mu's memory</span>
<span class="Delimiter">:(before "End Globals")</span>
map<long long int<span class="Delimiter">,</span> call_stack> Delimited_continuation<span class="Delimiter">;</span>
long long int Next_delimited_continuation_id = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Setup")</span>
Delimited_continuation<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
Next_delimited_continuation_id = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
REPLY_DELIMITED_CONTINUATION<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
Recipe_number[<span class="Constant">"reply-delimited-continuation"</span>] = REPLY_DELIMITED_CONTINUATION<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
case REPLY_DELIMITED_CONTINUATION: <span class="Delimiter">{</span>
<span class="Comment">// first clear any existing ingredients, to isolate the creation of the</span>
<span class="Comment">// continuation from its calls</span>
Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>front<span class="Delimiter">().</span>ingredient_atoms<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>front<span class="Delimiter">().</span>next_ingredient_to_process = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Comment">// copy the current call stack until the most recent 'reset' call</span>
call_stack::iterator find_reset<span class="Delimiter">(</span>call_stack& c<span class="Delimiter">);</span> <span class="Comment">// manual prototype containing '::'</span>
call_stack::iterator reset = find_reset<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">);</span>
assert<span class="Delimiter">(</span>reset != Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>end<span class="Delimiter">());</span>
Delimited_continuation[Next_delimited_continuation_id] = call_stack<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> reset<span class="Delimiter">);</span>
while <span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>begin<span class="Delimiter">()</span> != reset<span class="Delimiter">)</span> <span class="Delimiter">{</span>
--Callstack_depth<span class="Delimiter">;</span>
Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>pop_front<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Comment">// return it as the result of the 'reset' call</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>Next_delimited_continuation_id<span class="Delimiter">);</span>
++Next_delimited_continuation_id<span class="Delimiter">;</span>
<span class="Comment">// refresh instruction_counter to caller's step_index</span>
instruction_counter = current_step_index<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
call_stack::iterator find_reset<span class="Delimiter">(</span>call_stack& c<span class="Delimiter">)</span> <span class="Delimiter">{</span>
for <span class="Delimiter">(</span>call_stack::iterator p = c<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != c<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span>
if <span class="Delimiter">(</span>p<span class="Delimiter">-></span>is_reset<span class="Delimiter">)</span> <span class="Identifier">return</span> p<span class="Delimiter">;</span>
<span class="Identifier">return</span> c<span class="Delimiter">.</span>end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Comment">//: overload 'call' for continuations</span>
<span class="Delimiter">:(after "case CALL:")</span>
if <span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>properties<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>second<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> == <span class="Constant">"continuation"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// copy multiple calls on to current call stack</span>
assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span>
assert<span class="Delimiter">(</span>Delimited_continuation<span class="Delimiter">.</span>find<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> != Delimited_continuation<span class="Delimiter">.</span>end<span class="Delimiter">());</span>
const call_stack& new_calls = Delimited_continuation[ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span>]<span class="Delimiter">;</span>
for <span class="Delimiter">(</span>call_stack::const_reverse_iterator p = new_calls<span class="Delimiter">.</span>rbegin<span class="Delimiter">();</span> p != new_calls<span class="Delimiter">.</span>rend<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span>
Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>push_front<span class="Delimiter">(</span>*p<span class="Delimiter">);</span>
++current_step_index<span class="Delimiter">();</span> <span class="Comment">// skip past the reply-delimited-continuation</span>
ingredients<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>begin<span class="Delimiter">());</span> <span class="Comment">// drop the callee</span>
<span class="Identifier">goto</span> complete_call<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|