//: Continuations are a powerful primitive for constructing advanced kinds of //: control *policies* like back-tracking. //: //: In Mu, continuations are first-class and delimited, and are constructed //: out of two primitives: //: //: * 'call-with-continuation-mark' marks the top of the call stack and then //: calls the provided recipe. //: * 'return-continuation-until-mark' copies the top of the stack //: until the mark, and returns it as the result of //: 'call-with-continuation-mark' (which might be a distant ancestor on the //: call stack; intervening calls don't return) //: //: The resulting slice of the stack can now be called just like a regular //: recipe. //: //: See the example programs continuation*.mu to get a sense for the //: possibilities. //: //: Refinements: //: * You can call a single continuation multiple times, and it will preserve //: the state of its local variables at each stack frame between calls. //: The stack frames of a continuation are not destroyed until the //: continuation goes out of scope. See continuation2.mu. //: * 'return-continuation-until-mark' doesn't consume the mark, so you can //: return multiple continuations based on a single mark. In combination //: with the fact that 'return-continuation-until-mark' can return from //: regular calls, just as long as there was an earlier call to //: 'call-with-continuation-mark', this gives us a way to create resumable //: recipes. See continuation3.mu. //: * 'return-continuation-until-mark' can take ingredients to return just //: like other 'return' instructions. It just implicitly also returns a //: continuation as the first result. See continuation4.mu. //: * Conversely, you can pass ingredients to a continuation when calling it, //: to make it available to products of 'return-continuation-until-mark'. //: See continuation5.mu. //: * There can be multiple continuation marks on the stack at once; //: 'call-with-continuation-mark' and 'return-continuation-until-mark' both //: need to pass in a tag to coordinate on the correct mark. This allows us //: to save multiple continuations for different purposes (say if one is //: for exceptions) with overlapping stack frames. See exception.mu. //: //: Inspired by James and Sabry, "Yield: Mainstream delimited continuations", //: Workshop on the Theory and Practice of Delimited Continuations, 2011. //: https://www.cs.indiana.edu/~sabry/papers/yield.pdf //: //: Caveats: //: * At the moment we can't statically type-check continuations. So we raise //: runtime errors for a call that doesn't return a continuation when the //: caller expects, or one that returns a continuation when the caller //: doesn't expect it. This shouldn't cause memory corruption, though. //: There should still be no way to lookup addresses that aren't allocated. :(before "End Mu Types Initialization") type_ordinal continuation = Type_ordinal["continuation"] = Next_type_ordinal++; Type[continuation].name = "continuation"; //: A continuation can be called like a recipe. :(before "End is_mu_recipe Atom Cases(r)") if (r.type->name == "continuation") return true; //: However, it can't be type-checked like most recipes. Pretend it's like a //: header-less recipe. :(after "Begin Reagent->Recipe(r, recipe_header)") if (r.type->atom && r.type->name == "continuation") { result_header.has_header = false; return result_header; } :(scenario delimited_continuation) recipe main [ 1:continuation <- call-with-continuation-mark 233/mark, f, 77 # 77 is an argument to f 2:num <- copy 5 { 2:num <- call 1:continuation, 2:num # jump to 'return-continuation-until-mark' below 3:bool <- greater-or-equal 2:num, 8 break-if 3:bool loop } ] recipe f [ 11:num <- next-ingredient 12:num <- g 11:num return 12:num ] recipe g [ 21:num <- next-ingredient 22:num <- return-continuation-until-mark 233/mark 23:num <- add 22:num, 1 return 23:num ] # first call of 'g' executes the part before return-continuation-until-mark +mem: storing 77 in location 21 +run: {2: "number"} <- copy {5: "literal"} +mem: storing 5 in location 2 # calls of the continuation execute the part after return-continuation-until-mark +run: {2: "number"} <- call {1: "continuation"}, {2: "number"} +mem: storing 5 in location 22 +mem: storing 6 in location 2 +run: {2: "number"} <- call {1: "continuation"}, {2: "number"} +mem: storing 6 in location 22 +mem: storing 7 in location 2 +run: {2: "number"} <- call {1: "continuation"}, {2: "number"} +mem: storing 7 in location 22 +mem: storing 8 in location 2 # first call of 'g' does not execute the part after return-continuation-until-mark -mem: storing 77 in location 22 # calls of the continuation don't execute the part before return-continuation-until-mark -mem: storing 5 in location 21 -mem: storing 6 in location 21 -mem: storing 7 in location 21 # termination -mem: storing 9 in location 2 :(before "End call Fields") int continuation_mark_tag; :(before "End call Constructor") continuation_mark_tag = 0; :(before "End Primitive Recipe Declarations") CALL_WITH_CONTINUATION_MARK, :(before "End Primitive Recipe Numbers") Recipe_ordinal["call-with-continuation-mark"] = CALL_WITH_CONTINUATION_MARK; :(before "End Primitive Recipe Checks") case CALL_WITH_CONTINUATION_MARK: { if (SIZE(inst.ingredients) < 2) { raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' requires at least two ingredients: a mark number and a recipe to call\n" << end(); } break; } :(before "End Primitive Recipe Implementations") case CALL_WITH_CONTINUATION_MARK: { // like call, but mark the current call as a 'base of continuation' call // before pushing the next one on it if (Trace_stream) { ++Trace_stream->callstack_depth; trace("trace") << "delimited continuation; incrementing callstack depth to " << Trace_stream->callstack_depth << end(); assert(Trace_stream->callstack_depth < 9000); // 9998-101 plus cushion } instruction/*copy*/ caller_instruction = current_instruction(); Current_routine->calls.front().continuation_mark_tag = current_instruction().ingredients.at(0).value; Current_routine->calls.push_front(call(ingredients.at(1).at(0))); // drop the mark caller
# Check our support for fake file systems in scenarios.
scenario read-from-fake-file [
local-scope
assume-resources [
[a] <- [
|xyz|
]
]
contents:&:source:char <- start-reading resources, [a]
1:char/raw <- read contents
2:char/raw <- read contents
3:char/raw <- read contents
4:char/raw <-