!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 it = shuffledIterator(cloneList(unmappedQuestions)); IF0 alternative = new IF0 { public O get() { if (!it.hasNext()) false; ret bot.withAlternative(this, litcimap(x := it.next())); } }; ret alternative!; }); bot.addNativePredicate("find (an unmapped line) z", (map, env) -> { print("find unmapped line z"); if (empty(unmappedLines)) false; if (!env.wantAlternatives()) ret litcimap(z := random(unmappedLines)); ret mapI(shuffledIterator(cloneList(unmappedLines)), z -> litcimap(+z)); }); 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.addNativePredicate("run x with backtracking", (SS map) -> { S x = map.get("x"); print("Run x with backtracking: " + x); bot.runParsedProcedure(bot.parseProcedure(x)); // TODO print("Ran x with backtracking: " + x); true; }); bot.run(); } }