Uses 911K of libraries. Click here for Pure Java version (6733L/34K).
!7 cmodule PhilosophyBot2 extends DynPrintLog { transient L unmappedLines = ll("good", "early morning"); transient L unmappedQuestions = ll("what is the time", "how do you feel"); transient SS mappings = ciMap(); transient S program = [[ start start => proc { run (proc { while (there are unmapped questions): find (an unmapped question) x find (an unmapped line) y map x to y } ) with backtracking } ]]; transient new PhilosophyBot1 bot; start-thread { bot.program = program; bot.addNativePredicate("there are unmapped questions", () -> nempty(unmappedQuestions)); bot.addNativePredicate("find (an unmapped question) x", () -> { print("find unmapped question x"); if (empty(unmappedQuestions)) false; Iterator<S> it = shuffledIterator(cloneList(unmappedQuestions)); IF0<O> alternative = new IF0<O> { public O get() { if (!it.hasNext()) false; ret bot.withAlternative(this, litcimap(x := it.next())); } }; ret alternative!; }); bot.addNativePredicate("find (an unmapped line) z", () -> { print("find unmapped line z"); if (empty(unmappedLines)) false; Iterator<S> it = shuffledIterator(cloneList(unmappedLines)); IF0<O> alternative = new IF0<O> { public O get() { if (!it.hasNext()) false; ret bot.withAlternative(this, litcimap(z := it.next())); } }; ret alternative!; }); bot.addNativePredicate("map x to y", (SS map) -> { S x = map.get("x"), y = map.get("y"); unmappedQuestions.remove(x); unmappedLines.remove(y); print("Remaining unmapped questions: " + l(unmappedQuestions)); mappings.put(x, y); print("Mapped question " + quote(x) + " to answer " + quote(y)); ret bot.withAlternative(() -> { print("Undoing mapping of " + quote(x) + " to answer " + quote(y)); unmappedQuestions.add(x); unmappedLines.add(y); false; }, true); }); bot.onProcedureEnded.add(voidfunc(O proc, PhilosophyBot1.Path path) { print("A procedure ended after " + nSteps(path.steps) + ": " + path); }); bot.addNativePredicate("run x with backtracking", (SS map) -> { S x = map.get("x"); print("Run x with backtracking: " + x); new PhilosophyBot1.Path path; bot.runParsedProcedure(bot.parseProcedure(x), path); print("Ran x with backtracking: " + x); while licensed { if (!path.valid) ret true with print("Path not valid anymore"); print("Have " + nSteps(path.steps)); if (empty(path.steps)) ret true with print("Path is empty, nothing to backtrack"); PhilosophyBot1.Step step = last(path.steps); print("Backtracking last step: " + step); O result = step.alternative(); print(" Result: " + result); if (isFalse(result)) { print("Discarding last step as it is done"); popLast(path.steps); } } true; }); bot.run(); } }
download show line numbers debug dex old transpilations
Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1025606 |
Snippet name: | Philosophy Bot 2 - match questions and answers [v2 with backtracking, dev.] |
Eternal ID of this version: | #1025606/12 |
Text MD5: | ee965bf35badcbd345a766b3d7978c65 |
Transpilation MD5: | 0d9b4f309203d68b3cdcd7c5e3172d20 |
Author: | stefan |
Category: | |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-10-08 13:42:48 |
Source code size: | 3273 bytes / 101 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 287 / 650 |
Version history: | 11 change(s) |
Referenced in: | [show references] |