// A represents the return type of the backtrackable main computation sclass BStack_v2 extends VStack { static int idCounter; int stackID = ++idCounter; // for debugging // cloned version made at last branch point BStack_v2 snapshot; // remaining options at last branch point Iterator> options; // steps to undo before switching to the alternative L undos; *() {} *(Computable computation) { push(computation); } *(Iterable l) { pushAll(l); } sclass NoOptionsException extends RuntimeException {} BStack_v2 cloneStack() { new BStack_v2 s; s.snapshot = snapshot; s.options = options; s.undos = undos; s.stack = shallowCloneElements(stack); ret s; } void addUndo aka temp(AutoCloseable undo) { if (undo == null) ret; if (undos == null) undos = new L; undos.add(undo); } void options(B function, IVF1... options) { options(function, asList(options)); } void options(B function, Iterable> options) { Iterator> iterator = nonNullIterator(iterator(options)); if (!iterator.hasNext()) throw new NoOptionsException; IVF1 firstOption = iterator.next(); // All options except first are stored as alternatives // in cloned stacks. setOptions(iterator); // Execute the first option firstOption.get(function); } srecord noeq ExecuteOption(IVF1 option) is VStack.Computable { public void step(VStack stack, O subComputationResult) { O target = stack.caller(); stack.return(); option.get(target); } } void setOptions(Iterator> options) { snapshot = cloneStack(); this.options = options; undos = null; } // Try to backtrack one step and return a new stack // Returns null if no backtracking possible BStack_v2 backtrack() ctex { // Apply undos in reverse order for (int i = l(undos)-1; i >= 0; i--) undos.get(i).close(); if (options == null || !options.hasNext()) null; BStack_v2 stack = snapshot; snapshot.push(new ExecuteOption(options.next())); ret snapshot; } A nextResult() { stepAll(this); return (A) latestResult; } // calculate next result and print stack at every step A nextResultWithPrintStruct(long maxSteps) { stepMaxWithPrintIndentedStruct(this, maxSteps); return (A) latestResult; } }