// Goal: Java code with backtracking, like this, to return 4 different values: // Color color = Color.black or Color.white; // S shape = "circle" or "square"; // return "A " + color + " " + shape; // 1. Simple version without backtracking embedded S functionSimple() { S color = "black"; S shape = "circle"; return "A " + color + " " + shape; } //ret functionSimple(); // 2. Convert to virtual stack (VStack) /*class FunctionSimple extends VStackComputableWithStep { String color, shape; void step(VStack stack) { if (step == 0) { color = "black"; step = 1; } else if (step == 1) { shape = "circle"; step = 2; } else stack.return("A " + color + " " + shape); } } return vstackCompute(new FunctionSimple);*/ // 3. Add backtracking with BStack. (BacktrackableStack, BacktrackingStack, VStackWithBacktracking?) global { sclass FunctionSimple extends VStackComputableWithStep { String color, shape; void step(VStack stack) { cast stack to BStack; if (step == 0) { step = 1; stack.options(this, f -> f.color = "black", f -> f.color = "white"); } else if (step == 1) { step = 2; stack.options(this, f -> f.shape = "circle", f -> f.shape = "square"); } else stack.return("A " + color + " " + shape); } }} // Make a stack with our computation BStack stack = new BStack(new FunctionSimple); repeat 100 { // 100 steps max for safety // Get first or next result of function print("Result: " + stack.nextResultWithPrintStruct(10)); // Ask the stack to backtrack to get another result stack = stack.backtrack(); // If there is no more result, print message and exit if (stack == null) { print("No further result"); return; } // There is another result, so continue loop }