1 | // TODO: use BaseNative |
2 | /*static class MyNative implements Prolog.Native {
|
3 | public boolean yo(Prolog p, Lisp term) {
|
4 | new Map<S, Lisp> m; |
5 | |
6 | if (nlMatch("$n = a random number between $a and $b inc.", term, m)) {
|
7 | int a = parseInt(m.get("a").raw());
|
8 | int b = parseInt(m.get("b").raw());
|
9 | int n = a+random(b-a+1); |
10 | ret p.unify(m.get("n"), lisp(str(n)));
|
11 | } |
12 | |
13 | ret false; |
14 | } |
15 | }*/ |
16 | |
17 | static class IsQuoted extends Prolog.BaseNative {
|
18 | *() { super("isQuoted($x)"); }
|
19 | |
20 | boolean yo() {
|
21 | Lisp x = get("x");
|
22 | ret !(x instanceof Prolog.Var) && x.isLeaf() && isQuoted(x.head); |
23 | } |
24 | } |
25 | |
26 | static class IsWord extends Prolog.BaseNative {
|
27 | *() { super("isWord($x)"); }
|
28 | |
29 | boolean yo() {
|
30 | Lisp x = get("x");
|
31 | ret !(x instanceof Prolog.Var) && x.isLeaf() && isExtendedIdentifier(x.head); |
32 | } |
33 | } |
34 | |
35 | static class IntMul extends Prolog.BaseNative {
|
36 | *() { super("$a = intmul($x, $y)"); }
|
37 | |
38 | boolean yo() {
|
39 | BigInteger x = bigint(m.get("x").raw());
|
40 | BigInteger y = bigint(m.get("y").raw());
|
41 | ret p.unify(m.get("a"), lisp(str(x.multiply(y))));
|
42 | } |
43 | } |
44 | |
45 | static class IntDiv extends Prolog.BaseNative {
|
46 | *() { super("$a = intdiv($x, $y)"); }
|
47 | |
48 | boolean yo() {
|
49 | BigInteger x = bigint(m.get("x").raw());
|
50 | BigInteger y = bigint(m.get("y").raw());
|
51 | ret p.unify(m.get("a"), lisp(str(x.divide(y))));
|
52 | } |
53 | } |
54 | |
55 | static class IntAdd extends Prolog.BaseNative {
|
56 | *() { super("$a = intadd($x, $y)"); }
|
57 | |
58 | boolean yo() {
|
59 | BigInteger x = bigint(m.get("x").raw());
|
60 | BigInteger y = bigint(m.get("y").raw());
|
61 | ret p.unify(m.get("a"), lisp(str(x.add(y))));
|
62 | } |
63 | } |
64 | |
65 | static class IntMinus extends Prolog.BaseNative {
|
66 | *() { super("$a = intminus($x, $y)"); }
|
67 | |
68 | boolean yo() {
|
69 | BigInteger x = bigint(m.get("x").raw());
|
70 | BigInteger y = bigint(m.get("y").raw());
|
71 | ret p.unify(m.get("a"), lisp(str(x.subtract(y))));
|
72 | } |
73 | } |
74 | |
75 | static class LessThan extends Prolog.BaseNative {
|
76 | *() { super("$x is less than $y"); }
|
77 | |
78 | boolean yo() {
|
79 | BigInteger x = bigint(m.get("x").raw());
|
80 | BigInteger y = bigint(m.get("y").raw());
|
81 | ret x.compareTo(y) < 0; |
82 | } |
83 | } |
84 | |
85 | static class GreaterThan extends Prolog.BaseNative {
|
86 | *() { super("$x is greater than $y"); }
|
87 | |
88 | boolean yo() {
|
89 | BigInteger x = bigint(m.get("x").raw());
|
90 | BigInteger y = bigint(m.get("y").raw());
|
91 | ret x.compareTo(y) > 0; |
92 | } |
93 | } |
94 | |
95 | // TODO: fix |
96 | static class HeadExists extends Prolog.BaseNative {
|
97 | *() { super("head $h exists in theory $t"); }
|
98 | |
99 | boolean yo() {
|
100 | Lisp head = get("h");
|
101 | S theoryName = unq("t");
|
102 | Lisp theory = getParsedTheory(theoryName); |
103 | |
104 | // TODO: this is probably inefficient |
105 | for (Lisp rule : theory) {
|
106 | Prolog.Clause clause = p.clause(rule); |
107 | if (p.unifyOrRollback(head, clause.head)) |
108 | ret true; |
109 | } |
110 | ret false; |
111 | } |
112 | } |
113 | |
114 | static class TheoriesList extends Prolog.BaseNative {
|
115 | *() { super("$x = all theory names"); }
|
116 | |
117 | boolean yo() {
|
118 | ret unify("x", nlMinimalList(getTheoryNames()));
|
119 | } |
120 | } |
121 | |
122 | static class tocons extends Prolog.BaseNative {
|
123 | *() { super("$x = tocons($y)"); }
|
124 | |
125 | boolean yo() {
|
126 | Lisp y = m.get("y");
|
127 | ret unify("x", nlToCons(y));
|
128 | } |
129 | } |
130 | |
131 | static class fromcons extends Prolog.BaseNative {
|
132 | *() { super("$x = fromcons($y)"); }
|
133 | |
134 | boolean yo() {
|
135 | Lisp y = m.get("y");
|
136 | ret unify("x", nlFromCons(y));
|
137 | } |
138 | } |
139 | |
140 | static class operator extends Prolog.BaseNative {
|
141 | *() { super("$x = operator($y)"); }
|
142 | |
143 | boolean yo() {
|
144 | Lisp y = get("y");
|
145 | ret unify("x", lisp(quote(y.head)));
|
146 | } |
147 | } |
148 | |
149 | static class Unquote extends Prolog.BaseNative {
|
150 | *() { super("$x = unquote($y)"); }
|
151 | |
152 | boolean yo() {
|
153 | Lisp y = get("y");
|
154 | ret unify("x", lisp(unquote(y.head)));
|
155 | } |
156 | } |
157 | |
158 | |
159 | static class arg extends Prolog.BaseNative {
|
160 | *() { super("$x = arg $i in $y"); }
|
161 | |
162 | boolean yo() {
|
163 | Lisp y = m.get("y");
|
164 | int i = parseInt(m.get("i").raw());
|
165 | if (i-1 >= 0 && i-1 < y.size()) |
166 | ret unify("x", y.get(i-1));
|
167 | else |
168 | ret false; |
169 | } |
170 | } |
171 | |
172 | static class Arity extends Prolog.BaseNative {
|
173 | *() { super("$x = arity of $y"); }
|
174 | |
175 | boolean yo() {
|
176 | Lisp y = m.get("y");
|
177 | ret unify("x", lisp(str(y.size())));
|
178 | } |
179 | } |
180 | |
181 | static class RemoveWord extends Prolog.BaseNative {
|
182 | *() { super("$x = removeWord($word, $y)"); }
|
183 | |
184 | boolean yo() {
|
185 | Lisp y = get("y");
|
186 | S word = unq("word");
|
187 | ret unify("x", nlRemoveWord(word, y));
|
188 | } |
189 | } |
190 | |
191 | static class GetTheory extends Prolog.BaseNative {
|
192 | *() { super("$x = theory $name"); }
|
193 | |
194 | boolean yo() {
|
195 | S name = unq("name");
|
196 | Lisp parsed = getParsedTheory(name); |
197 | ret parsed != null && unify("x", parsed);
|
198 | } |
199 | } |
200 | |
201 | static class TextOfTheory extends Prolog.BaseNative {
|
202 | *() { super("$x = text of theory $name"); }
|
203 | |
204 | boolean yo() {
|
205 | S name = unq("name");
|
206 | S text = getTheoryFromBot(name); |
207 | ret text != null && unify("x", lisp(quote(text)));
|
208 | } |
209 | } |
210 | |
211 | static class Solve1 extends Prolog.BaseNative {
|
212 | *() { super("solve1 $text in $theories"); }
|
213 | |
214 | boolean yo() {
|
215 | Lisp question = get("text");
|
216 | Lisp t = m.get("theories");
|
217 | L<S> theoryNames = nlParseStringList(t); |
218 | |
219 | Prolog sub = newSubProlog(theoryNames, false); |
220 | |
221 | new IdentityHashMap<Lisp, S> varMap; |
222 | Lisp exported = exportVars(question, varMap); |
223 | if (showLog) {
|
224 | p.log("Exported: " + exported);
|
225 | p.log("Var map: " + structure(varMap));
|
226 | } |
227 | sub.start(exported); |
228 | sub.startTime = p.startTime; |
229 | Map<S, Lisp> solution = sub.nextSolution(); |
230 | if (solution == null) ret false; |
231 | ret unifyForeign(varMap, solution); |
232 | } |
233 | } |
234 | |
235 | static class Think extends Prolog.BaseNative {
|
236 | *() { super("think $x"); }
|
237 | |
238 | boolean yo() {
|
239 | p.think(m.get("x"));
|
240 | ret true; |
241 | } |
242 | } |
243 | |
244 | static class MemorizeImpl extends Prolog.BaseNative {
|
245 | *() { super("memorize_impl $x"); }
|
246 | |
247 | boolean yo() {
|
248 | Lisp x = m.get("x");
|
249 | if (!p.isClosedTerm(x)) {
|
250 | if (p.showStuff) |
251 | p.log("Not a closed term, can't memorize: " + nlUnparse(x));
|
252 | ret false; |
253 | } |
254 | if (p.outTheory != null) |
255 | incrementTheory(p.outTheory, x); |
256 | p.think(lisp("[]", "memorized", x));
|
257 | ret true; |
258 | } |
259 | } |
260 | |
261 | static class RewriteWithTheory extends Prolog.BaseNative {
|
262 | *() { super("rewrite with theory $x"); }
|
263 | |
264 | boolean yo() {
|
265 | S theoryName = unq("x");
|
266 | S text = findTheory(theoryName); |
267 | if (text == null) {
|
268 | // clause succeeds even if theory not found |
269 | if (p.showStuff) |
270 | p.log(format("Warning: rewrite theory * not found", theoryName));
|
271 | } else {
|
272 | L<Prolog.Clause> rewriteTheory = getProgram(text); |
273 | if (p.showStuff) |
274 | for (Prolog.Clause c : rewriteTheory) |
275 | p.log(" REW " + p.showClause(c));
|
276 | L<Lisp> rewritten = p.rewriteWith(rewriteTheory); |
277 | if (p.showStuff) |
278 | p.log("Got " + l(rewritten) + " rewritten terms.");
|
279 | for (Lisp term : rewritten) {
|
280 | if (p.showStuff) |
281 | p.log("Adding rewritten term: " + nlUnparse(term));
|
282 | p.addClause(term, "rewritten"); |
283 | } |
284 | } |
285 | ret true; |
286 | } |
287 | } |
288 | |
289 | static class NLSafeParse extends Prolog.BaseNative {
|
290 | *() { super("$x = nlSafeParse($y)"); }
|
291 | |
292 | boolean yo() {
|
293 | S input = unq("y");
|
294 | ret unify("x", nlSafeParse(input));
|
295 | } |
296 | } |
297 | |
298 | // ignores case |
299 | static class StartsWith extends Prolog.BaseNative {
|
300 | *() { super("$x starts with $y"); }
|
301 | |
302 | boolean yo() {
|
303 | S x = unq("x"), y = unq("y");
|
304 | ret swic(x, y); |
305 | } |
306 | } |
307 | |
308 | // ignores case |
309 | static class EndsWith extends Prolog.BaseNative {
|
310 | *() { super("$x ends with $y"); }
|
311 | |
312 | boolean yo() {
|
313 | S x = unq("x"), y = unq("y");
|
314 | ret endsWithIgnoreCase(x, y); |
315 | } |
316 | } |
Began life as a copy of #1002841
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1002933 |
| Snippet name: | Prolog Native Modules (include) |
| Eternal ID of this version: | #1002933/1 |
| Text MD5: | a7dc431753c23e436380b3cf71702256 |
| Author: | stefan |
| Category: | eleu / nl |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2016-10-19 19:22:46 |
| Source code size: | 7805 bytes / 316 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 900 / 1084 |
| Referenced in: | [show references] |