Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

331
LINES

< > BotCompany Repo | #1008120 // Compare Bot 2 [test hygienic parsing]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (11368L/81K).

1  
!7
2  
3  
sbool killDB = false;
4  
static int defaultMax = 1000; // steps to run
5  
6  
sS facts = [[
7  
  We want to find out what is more correct than what.
8  
  
9  
  "not correct" is a candidate.
10  
  "maybe quite correct" is a candidate.
11  
  "really really correct" is a candidate.
12  
  "buggy" is a candidate.
13  
  "total garbage" is a candidate.
14  
  "incorrect" is a candidate.
15  
  "almost correct" is a candidate.
16  
  "absolutely correct" is a candidate.
17  
  "very correct" is a candidate.
18  
  
19  
  [nfhwfngqkgoqtngs] (no question) and (X is a candidate.) and (Y is a candidate.) and (X <> Y) and (There is no answer to (Is X # more correct than Y?).) => (Ask (Is X # more correct than Y?))
20  
  // Note: The # is just a filler for parsing, we'll get rid of that in time. Also, you will be able to write stuff without brackets of course.
21  
  
22  
  Always apply rule nfhwfngqkgoqtngs.
23  
  
24  
  // Interpret answers
25  
  [iprphzfmwithvdfb] (The answer to (Is X # Y than Z?) is (yes).) => (X is Y than Z.)
26  
  Always apply rule iprphzfmwithvdfb.
27  
  [udsmtmbiedmxtxci] (The answer to (Is X # Y than Z?) is (no).) => (Z is Y than X.)
28  
  Always apply rule udsmtmbiedmxtxci.
29  
  
30  
  // Transitivity!
31  
  [pglcuchrtxdmlfvh] (X is AA than Y.) and (Y is AA than Z.) => (X is AA than Z.)
32  
  Always apply rule pglcuchrtxdmlfvh.
33  
  
34  
  [ijziyiplzanfwvqx] (Ask (X)) and (The answer to (X) is (Y).) => (Remove (Ask (X)))
35  
  Always apply rule ijziyiplzanfwvqx.
36  
  
37  
  no question :- There is no statement with operator fxxathfdlfzgmaov.
38  
  
39  
  // Just a pretty-printing statement
40  
  omrvwjslbkffaxoj hszwyiyetuuhcfwz acwmbrxqrlaplyqu
41  
]];
42  
43  
sS concepts = [[
44  
  ufzlismnlhvbjqoj - (X) and (Y) and (Z) and (AA) and (AB) => (AC)
45  
  phkdhqkhzytkwdpb - (X) and (Y) and (Z) and (AA) => (AB)
46  
  txsysipmstxmjhmi - (X) and (Y) => (Z)
47  
  wziyypnyayfphdxs - (X) => (Y)
48  
  xikyminwmeahxiws - There is no answer to (X).
49  
  pxavyqesoqyqbipb - The answer to (X) is (Y).
50  
  cdzttdbxadjreorm - We want to find out what is X than what.
51  
  ogqdgzepqbunuuht - X is a candidate.
52  
  hszwyiyetuuhcfwz - Is X # Y than Z?
53  
  acwmbrxqrlaplyqu - Is "X" Y than "Z"?
54  
  qgxjwufpfzkqvmxb - X is Y than Z.
55  
  ylvaoplzguvhegka - X is not Y than Z.
56  
  zxucpbfzmexohoiv - It is not true that (X).
57  
  buahjsodljsaxvaq - X <> Y
58  
  fxxathfdlfzgmaov - Ask (X)
59  
  chjllkocfhtwcgoj - Remove (X)
60  
  mzdvauejerzefagk - I don't know if (X)
61  
  omrvwjslbkffaxoj - Format operator X as template Y.
62  
  houyieutcmfdvgmz - Execute (X)
63  
  hzmtabxcmkrtffgh - Apply X
64  
  zvknqzxlrmuobnzx - Always apply rule X.
65  
  plprefzezbluijrw - no question
66  
  jexyjrtbtilmsmfw - setQuestion(X)
67  
  cuutddpitkuswthu - While (X) => (Y)
68  
  sicrogpdrtkiptun - X :- Y
69  
  zhoulgsatpswstfa - There is no statement with operator X.
70  
  ltyqczktgtjxrtlf - more correct
71  
  zveeggveflecjwvr - total garbage
72  
  wolealpbqkkqkvqq - not correct
73  
  tomkbzlqlpeiekvu - maybe quite correct
74  
  dnxyynujqydgcxcd - really really correct
75  
  trajdgvqepyhsumb - buggy
76  
  lwikikhkydtewtjc - incorrect
77  
  muxjpepnjiyjghwj - almost correct
78  
  wtnpjovqqiswlyay - absolutely correct
79  
  bmliokweerblfxco - very correct
80  
]];
81  
82  
concept MyTruth {
83  
  S globalID;
84  
  Lisp term;
85  
  S madeByRule;
86  
  
87  
  *() {}
88  
  *(S *globalID, Lisp *term, S *madeByRule) { change(); }
89  
}
90  
91  
static Map<S, F1<Lisp, Bool>> evaluators = new HashMap;
92  
static Lisp currentQuestion;
93  
  
94  
p-tt {
95  
  centerHigherConsole();
96  
  substance();
97  
  
98  
  // customize parsing and printing
99  
  set hygienicParse1_unquote;
100  
  set hygienicParse1_debug;
101  
  set englishToConceptLanguage_simple_noXYZ;
102  
  set formatXYZ_quotedVars;
103  
  
104  
  // load predefined & learned statements
105  
  useConceptsAndStatements(concepts, facts);
106  
  if (killDB) deleteDB();
107  
  db();
108  
  for (MyTruth t)
109  
    lispAddLocalTruth(t.globalID, t.term);
110  
  printLispStatements();
111  
  
112  
  // create evaluators
113  
  
114  
  // X <> Y
115  
  evaluators.put("buahjsodljsaxvaq", new F1<Lisp,Bool>() {
116  
    Bool get(Lisp l) {
117  
      ret l.size() == 2 && neq(l.get(0), l.get(1));
118  
    }
119  
  });
120  
  
121  
  // I don't know if (X)
122  
  evaluators.put("mzdvauejerzefagk", new F1<Lisp,Bool>() {
123  
    Bool get(Lisp l) {
124  
      ret l.size() == 1 && notKnownIf(l.get(0));
125  
    }
126  
  });
127  
  
128  
  // There is no statement with operator X.
129  
  evaluators.put("zhoulgsatpswstfa", new F1<Lisp,Bool>() {
130  
    Bool get(Lisp l) {
131  
      ret l.size() == 1 && !hasTruthWithHead(l.raw(0));
132  
    }
133  
  });
134  
  
135  
  // There is no answer to (X).
136  
  evaluators.put("xikyminwmeahxiws", new F1<Lisp,Bool>() {
137  
    Bool get(Lisp l) {
138  
      bool ok = l.size() == 1 && null == lispForward("pxavyqesoqyqbipb", l.get(0));
139  
      print("Checking for answer to " + l.get(0) + " => " + ok);
140  
      ret ok;
141  
    }
142  
  });
143  
  
144  
  // :- rules
145  
  for (final Lisp rule : lispTruth2("sicrogpdrtkiptun"))
146  
    evaluators.put(rule.raw(0), new F1<Lisp,Bool>() {
147  
    Bool get(Lisp l) {
148  
      ret l.isLeaf() && matchCondition(rule.get(1));
149  
    }
150  
  });
151  
  
152  
  bot();
153  
  
154  
  //applyRule("nfhwfngqkgoqtngs");
155  
  //applyAlwaysRules();
156  
  //runInstruction("yrgkrfrcwubpevzu");
157  
}
158  
159  
answer {
160  
  if "lisp" { printLispStatements(); ret "ok"; }
161  
  if "print" { printPrettyFacts(); ret "ok"; }
162  
  if "rule *" { applyRule($1); ret "ok"; }
163  
  if "step" { applyAlwaysRules_step(); ret "ok"; }
164  
  if "run" { applyAlwaysRules(defaultMax); ret "ok"; }
165  
  if "force" { force(defaultMax); ret "ok"; }
166  
}
167  
168  
svoid showQuestion() {
169  
  L<Lisp> l = lispTruth1("fxxathfdlfzgmaov");
170  
  if (l(l) > 1) warn("Multiple open questions");
171  
  if (nempty(l)) {
172  
    final Lisp q = first(l).get(0);
173  
    if (eq(currentQuestion, q)) ret;
174  
    currentQuestion = q;
175  
    print("Question: " + q);
176  
    swing {
177  
      showControls2(
178  
        centerAndSouth(
179  
          withBottomMargin(jcenteredBoldLabel(lispToEnglish_prettier(q))),
180  
          jcenteredline(answerButton("Yes"), answerButton("No"), answerButton("I don't know")));
181  
    }
182  
  }
183  
}
184  
185  
svoid runInstruction(S instructionID) {
186  
  Lisp rule = getLispTruth(instructionID);
187  
  if (rule == null) { print("Instruction not found: " + instructionID); ret; }
188  
  runInstruction(rule);
189  
}
190  
  
191  
svoid runInstruction(Lisp rule) {
192  
  print("runInstruction: " + rule);
193  
  if (rule.is("hzmtabxcmkrtffgh", 1)) // Apply X
194  
    applyRule(rule.raw(0));
195  
  else if (rule.is("cuutddpitkuswthu", 2)) { // While (X) => (Y)
196  
    while (ping() && matchCondition(rule.get(0)))
197  
      runInstruction(rule.get(1));
198  
  } /*else if (rule.is("houyieutcmfdvgmz", 1))
199  
    execute(rule.get(0));
200  
  /*else if (rule.is("jexyjrtbtilmsmfw", 1)) // setQuestion(X)
201  
    setQuestion(rule.get(0));*/
202  
  else print("Unknown instruction type: " + rule);
203  
}
204  
205  
svoid applyRule(S ruleID) {
206  
  // process the rule
207  
  Lisp rule = getLispTruth(ruleID);
208  
  if (rule == null) { print("Rule not found: " + ruleID); ret; }
209  
  //print("Applying rule " + ruleID);
210  
  print_setPrefixForThread(ruleID + "> ");
211  
  try {
212  
    L<Lisp> conditions = dropLast(rule.args);
213  
    Lisp out = last(rule.args);
214  
    //for (Lisp cond : conditions) print("Condition: " + cond);
215  
      
216  
    new Map<S, Lisp> matches;
217  
    int n = matchConditions_random(conditions, matches);
218  
    if (n == -1) {
219  
      print("Yo! " + struct(matches));
220  
      out = lispReplaceVars(out, matches);
221  
      if (lispTrue(out))
222  
        print("Already had: " + out);
223  
      else {
224  
        print("Defining: " + lispToEnglish_prettier(out));
225  
        emit(out, ruleID);
226  
      }
227  
    } else
228  
      print("Only " + n + " of " + n(conditions, "condition") + " met");
229  
  } finally {
230  
    print_setPrefixForThread("");
231  
  }
232  
}
233  
234  
// if match: returns -1
235  
// if no match: returns how many conditions were met
236  
static int matchConditions_random(L<Lisp> conditions, Map<S, Lisp> m) {
237  
  if (empty(conditions)) ret -1;
238  
  Lisp condition = first(conditions);
239  
  Map<S, Lisp> m2 = cloneMap(m);
240  
  if (!matchCondition_random(condition, m2)) {
241  
    print("Condition failed: " + condition);
242  
    ret 0;
243  
  }
244  
  print("Condition matched, " + struct(m2));
245  
  int n = matchConditions_random(dropFirst(conditions), m2);
246  
  if (n == -1) {
247  
    m.putAll(m2);
248  
    ret -1;
249  
  }
250  
  ret n+1; // failure
251  
}
252  
253  
sbool matchCondition(Lisp condition) {
254  
  print("matchCondition " + condition);
255  
  O evaluator = evaluators.get(condition.head);
256  
  if (evaluator != null)
257  
    ret isTrue(callF(evaluator, condition));
258  
  ret lispTrue(condition);
259  
}
260  
261  
static bool matchCondition_random(Lisp condition, Map<S, Lisp> m) {
262  
  L<Lisp> facts = lispTruth();
263  
  
264  
  O evaluator = evaluators.get(condition.head);
265  
  if (evaluator != null)
266  
    ret isTrue(callF(evaluator, lispReplaceVars(condition, m)));
267  
268  
  L<Map<S, Lisp>> candidates = new L;
269  
  for (Lisp fact : facts) {
270  
    Map<S, Lisp> m2 = cloneMap(m);
271  
    if (lispMatchIC_xyzVars_sub(condition, fact, m2))
272  
      candidates.add(m2);
273  
  }
274  
  //print("Have " + n(candidates, "candidate"));
275  
  if (empty(candidates)) false;
276  
  m.putAll(random(candidates));
277  
  true;
278  
}
279  
280  
svoid force(long steps) {
281  
  for (long step = 0; step < steps; step++)
282  
    applyAlwaysRules_step();
283  
}
284  
285  
svoid applyAlwaysRules(long maxSteps) {
286  
  long count = 0, step = 0;
287  
  do {
288  
    count = lispChangeCount();
289  
    applyAlwaysRules_step();
290  
  } while (++step < maxSteps && lispChangeCount() != count);
291  
}
292  
293  
svoid applyAlwaysRules_step() {
294  
  for (Lisp l : lispTruth1("zvknqzxlrmuobnzx"))
295  
    applyRule(l.raw(0)); // TODO: not only random application
296  
  for (Lisp l : lispTruth1("chjllkocfhtwcgoj")) { // Remove (X)
297  
    unemit(l);
298  
    unemit(l.get(0));
299  
  }
300  
  for (Lisp l : lispTruth1("hzmtabxcmkrtffgh")) { // Apply X
301  
    unemit(l);
302  
    applyRule(l.raw(0));
303  
  }
304  
  showQuestion();
305  
}
306  
307  
svoid emit(Lisp l, S madeByRule) {
308  
  LispStatement s = lispAddLocalTruth(l);
309  
  if (s != null)
310  
    new MyTruth(s.globalID, l, madeByRule);
311  
}
312  
313  
svoid unemit(Lisp l) {
314  
  LispStatement s = findLispStatement(l);
315  
  if (s == null) ret;
316  
  removeLispStatement(s);
317  
  deleteConcepts(MyTruth, globalID := s.globalID);
318  
}
319  
320  
static JButton answerButton(fS text) {
321  
  ret jbutton(text, r {
322  
    emit(lisp("pxavyqesoqyqbipb", currentQuestion, englishToLisp(text)), "button");
323  
    applyAlwaysRules(defaultMax);
324  
  });
325  
}
326  
327  
// hygienic parsing
328  
sS englishToConceptLanguage(S s) {
329  
  Lisp l = assertNotNull(hygienicParse1(s));
330  
  ret clUnparse(l);
331  
}

Author comment

Began life as a copy of #1008086

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1008120
Snippet name: Compare Bot 2 [test hygienic parsing]
Eternal ID of this version: #1008120/10
Text MD5: ed6b11cd37583a682d57549f891f837d
Transpilation MD5: 01c58206c609096892e87fb0e9bc668b
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-05-21 09:09:52
Source code size: 10179 bytes / 331 lines
Pitched / IR pitched: No / No
Views / Downloads: 470 / 925
Version history: 9 change(s)
Referenced in: [show references]