Uses 911K of libraries. Click here for Pure Java version (6733L/34K).
1 | !7 |
2 | |
3 | cmodule PhilosophyBot2 extends DynPrintLog { |
4 | transient L unmappedLines = ll("good", "early morning"); |
5 | transient L unmappedQuestions = ll("what is the time", "how do you feel"); |
6 | transient SS mappings = ciMap(); |
7 | |
8 | transient S program = [[ |
9 | start |
10 | |
11 | start => proc { |
12 | run (proc { |
13 | while (there are unmapped questions): |
14 | find (an unmapped question) x |
15 | find (an unmapped line) y |
16 | map x to y |
17 | } ) with backtracking |
18 | } |
19 | ]]; |
20 | |
21 | transient new PhilosophyBot1 bot; |
22 | |
23 | start-thread { |
24 | bot.program = program; |
25 | bot.addNativePredicate("there are unmapped questions", |
26 | () -> nempty(unmappedQuestions)); |
27 | bot.addNativePredicate("find (an unmapped question) x", |
28 | () -> { |
29 | print("find unmapped question x"); |
30 | if (empty(unmappedQuestions)) false; |
31 | Iterator<S> it = shuffledIterator(cloneList(unmappedQuestions)); |
32 | IF0<O> alternative = new IF0<O> { |
33 | public O get() { |
34 | if (!it.hasNext()) false; |
35 | ret bot.withAlternative(this, litcimap(x := it.next())); |
36 | } |
37 | }; |
38 | ret alternative!; |
39 | }); |
40 | bot.addNativePredicate("find (an unmapped line) z", |
41 | () -> { |
42 | print("find unmapped line z"); |
43 | if (empty(unmappedLines)) false; |
44 | Iterator<S> it = shuffledIterator(cloneList(unmappedLines)); |
45 | IF0<O> alternative = new IF0<O> { |
46 | public O get() { |
47 | if (!it.hasNext()) false; |
48 | ret bot.withAlternative(this, litcimap(z := it.next())); |
49 | } |
50 | }; |
51 | ret alternative!; |
52 | }); |
53 | |
54 | bot.addNativePredicate("map x to y", |
55 | (SS map) -> { |
56 | S x = map.get("x"), y = map.get("y"); |
57 | unmappedQuestions.remove(x); |
58 | unmappedLines.remove(y); |
59 | print("Remaining unmapped questions: " + l(unmappedQuestions)); |
60 | mappings.put(x, y); |
61 | print("Mapped question " + quote(x) + " to answer " + quote(y)); |
62 | ret bot.withAlternative(() -> { |
63 | print("Undoing mapping of " + quote(x) + " to answer " + quote(y)); |
64 | unmappedQuestions.add(x); |
65 | unmappedLines.add(y); |
66 | false; |
67 | }, true); |
68 | }); |
69 | |
70 | bot.onProcedureEnded.add(voidfunc(O proc, PhilosophyBot1.Path path) { |
71 | print("A procedure ended after " + nSteps(path.steps) + ": " + path); |
72 | }); |
73 | |
74 | bot.addNativePredicate("run x with backtracking", |
75 | (SS map) -> { |
76 | S x = map.get("x"); |
77 | print("Run x with backtracking: " + x); |
78 | new PhilosophyBot1.Path path; |
79 | bot.runParsedProcedure(bot.parseProcedure(x), path); |
80 | print("Ran x with backtracking: " + x); |
81 | |
82 | while licensed { |
83 | if (!path.valid) ret true with print("Path not valid anymore"); |
84 | print("Have " + nSteps(path.steps)); |
85 | if (empty(path.steps)) ret true with print("Path is empty, nothing to backtrack"); |
86 | PhilosophyBot1.Step step = last(path.steps); |
87 | print("Backtracking last step: " + step); |
88 | O result = step.alternative(); |
89 | print(" Result: " + result); |
90 | if (isFalse(result)) { |
91 | print("Discarding last step as it is done"); |
92 | popLast(path.steps); |
93 | } |
94 | } |
95 | |
96 | true; |
97 | }); |
98 | |
99 | bot.run(); |
100 | } |
101 | } |
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: | 289 / 654 |
Version history: | 11 change(s) |
Referenced in: | [show references] |