// 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> { 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> { 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 }
download show line numbers debug dex old transpilations
Travelled to 1 computer(s): mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035386 |
Snippet name: | BStack experimentation |
Eternal ID of this version: | #1035386/1 |
Text MD5: | c69334631678260a4714c4777af3375d |
Author: | stefan |
Category: | javax |
Type: | JavaX (incomplete) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-05-05 15:20:35 |
Source code size: | 1926 bytes / 74 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 126 / 136 |
Referenced in: | -