-
Notifications
You must be signed in to change notification settings - Fork 0
/
JScriptLog_Builtins.js
1612 lines (1262 loc) · 43.3 KB
/
JScriptLog_Builtins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******
This file is part of JScriptLog. This notice must remain.
Created by Glendon Holst. Copyright 2005.
JLog is free software licensed under the GNU General Public License.
See the included LICENSE.txt and GNU.txt files.
Check <http://jlogic.sourceforge.net/> for further information.
*******/
///////////////////////////////////
// * Builtin functions
///////////////////////////////////
///////////////////////////////////
// *_try_fn(goal) is a traversal function called when attempting to prove
// goal. goal was just removed from frontier, but is not on either frontier or explored stack.
// goal will need to be placed on one of either the frontier or explored stack.
// Returns true if the goal can be explored further, false if not.
// If false, then all bindings must be undone.
// If an exception is thrown, only the bindings for goal are undone externally. If the *_try_fn
// has caused any bindings for another goal, it must try / catch for exceptions and clean up
// before re-throwing.
// Assumes goal.bindings == new Array()
///////////////////////////////////
function true_try_fn(goal,prover)
{
prover.explored.push(goal);
return true;
}
function cut_try_fn(goal,prover)
{var g;
var gs_with_undo_fn = null;
while ((g = prover.explored.pop()) != undefined)
{
if (g == goal.parent) // cutoff parent found -- break
{
if (gs_with_undo_fn != null) // non-mergeable goals to return to explored stack
{
prover.explored.push(g);
while ((g = gs_with_undo_fn.pop()) != undefined)
prover.explored.push(g);
prover.explored.push(goal);
}
else if (goal.parent_is_ancestor)
{
prover.explored.push(g);
}
else if (g.parent != null && prover.explored[prover.explored.length - 1] == g.parent)
{var i = prover.frontier.length - 1;
while (i >= 0 && prover.frontier[i].parent == g)
{
prover.frontier[i].parent = g.parent;
prover.frontier[i].parent_is_ancestor = true;
--i;
}
mergeGoalBindings(g,g.parent);
}
else
{
g.noretry = true;
prover.explored.push(g);
}
return true;
}
else if (g.undo_fn != undefined) // goal with unmergeable bindings (must wait for retry to undo)
{
if (gs_with_undo_fn == null)
gs_with_undo_fn = new Array();
gs_with_undo_fn.push(g);
}
else // sub-goal with mergeable bindings
{
mergeGoalBindings(g,g.parent);
}
};
prover.explored.push(goal);
return true;
}
function internal_clause_try_fn(goal,prover)
{var encl = getFinalEnclosure(goal.encl);
var head = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var body = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var rref = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[2]));
var idx = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[3]));
var ruleset;
// find ruleset
if (isObjectReference(rref.term) && (isAtom(head.term) || isVariable(head.term)))
goal.subgoal = newAtomGoalFromRuleSet(head,goal,rref.term.name,goal.kb);
else if (isAtom(head.term))
goal.subgoal = newAtomGoal(head,goal,goal.kb);
else
throw newErrorException("Expected head atom or a ruleset object reference in internal:clause/5.");
if (isVariable(idx.term))
goal.subgoal.rule_index = 0;
else if (isInteger(idx.term))
goal.subgoal.rule_index = idx.term.name;
else
throw newErrorException("Expected variable or integer for rule index in internal:clause/5.");
goal.subgoal.kb = goal.kb;
if (goal.subgoal.ruleset == undefined)
{
prover.frontier.push(goal);
goal.subgoal = null;
return false;
}
return internal_clause_test(body,rref,idx,goal,prover);
}
// helper for internal_clause_*_fn
// removes all goal bindings on failure.
function internal_clause_test(body,rref,idx,goal,prover)
{var rule_body;
do
{
rule_body = nextUnifiedRuleBodyForGoal(goal.subgoal);
if (rule_body != null)
{var rbody = getConsPairEnclosureFromEnclosureArray(rule_body);
var rset = newTermEnclosure(newObjectReference(goal.subgoal.ruleset));
var n = newTermEnclosure(newNumber(goal.subgoal.rule_index));
if (jslog_unify(body,rbody,goal.bindings) && jslog_unify(rref,rset,goal.bindings) &&
jslog_unify(idx,n,goal.bindings))
{
prover.explored.push(goal);
return true;
}
else
{
removeBindings(goal.bindings);
internal_clause_undo_fn(goal);
goal.subgoal.rule_index++;
}
}
} while (rule_body != null && !isNumber(idx.term)) // one chance only if idx bound
// no rules matches
{
removeBindings(goal.bindings);
internal_clause_undo_fn(goal);
prover.frontier.push(goal);
goal.subgoal = null;
return false;
}
}
function internal_current_op_try_fn(goal,prover)
{var encl = getFinalEnclosure(goal.encl);
var name = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var optype = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var pri = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[2]));
var rref = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[3]));
goal.rulesets_array = new Array();
goal.rulesets_index = 0;
if (isObjectReference(rref.term))
{
if (!isOperatorRuleSet(rref.term.name))
throw newErrorException("Invalid ruleset reference:current_op/4.");
goal.rulesets_array[0] = rref.term.name;
}
else if (isConstant(name.term))
{
if (isConstant(optype.term))
{
var type_num = getOperatorTypeFromString(optype.term.name);
if (type_num == null)
throw newErrorException("Invalid operator type ("+optype.term.name+"):current_op/4.");
var arity;
if (type_num < OP_TYPE_FX)
arity = 2;
else
arity = 1;
var ruleset = getRuleSetFromNameArity(goal.kb,name.term.name,arity);
if (ruleset != null)
goal.rulesets_array[0] = ruleset;
}
else
{
var ruleset;
ruleset = getRuleSetFromNameArity(goal.kb,name.term.name,1);
if (ruleset != null)
goal.rulesets_array[goal.rulesets_array.length] = ruleset;
ruleset = getRuleSetFromNameArity(goal.kb,name.term.name,2);
if (ruleset != null)
goal.rulesets_array[goal.rulesets_array.length] = ruleset;
}
}
else if (isConstant(optype.term))
{
var type_num = getOperatorTypeFromString(optype.term.name);
var pri_num = null;
if (type_num == null)
throw newErrorException("Invalid operator type ("+optype.term.name+"):current_op/4.");
if (isInteger(pri.term))
{
if (pri.term.name < 0 || pri.term.name > 1200)
throw newErrorException("Invalid operator priority value ("+pri.term.name+"):current_op/4.");
pri_num = pri.term.name;
}
internal_current_op_collect_rulesets(goal.rulesets_array,goal.kb,type_num,pri_num);
}
else if (isInteger(pri.term))
{
if (pri.term.name < 0 || pri.term.name > 1200)
throw newErrorException("Invalid operator priority value ("+pri.term.name+"):current_op/4.");
internal_current_op_collect_rulesets(goal.rulesets_array,goal.kb,null,pri.term.name);
}
else // collect all rulesets
{
internal_current_op_collect_rulesets(goal.rulesets_array,goal.kb,null,null);
}
if (goal.rulesets_array.length <= 0)
{
prover.frontier.push(goal);
return false;
}
return internal_current_op_test(name,optype,pri,rref,goal,prover);
}
// helper for internal_current_op_*_fn
// addes all rulesets matching type_num and pri_num filters to rulesets_array
// null filters match any value.
function internal_current_op_collect_rulesets(rulesets_array,kb,type_num,pri_num)
{
for (var r in kb.rulesets)
{
var ruleset = kb.rulesets[r];
if (isOperatorRuleSet(ruleset) &&
(type_num == null || type_num == getOperatorType(ruleset)) &&
(pri_num == null || pri_num == getOperatorPrecedence(ruleset)))
rulesets_array[rulesets_array.length] = ruleset;
}
}
// helper for internal_current_op_*_fn
// removes all goal bindings on failure.
function internal_current_op_test(name,optype,pri,rref,goal,prover)
{
while (goal.rulesets_index < goal.rulesets_array.length)
{
var ruleset = goal.rulesets_array[goal.rulesets_index];
var rref2 = newTermEnclosure(newObjectReference(ruleset));
var name2 = newTermEnclosure(newConstant(getRuleSetName(ruleset)));
var pri2 = newTermEnclosure(newNumber(getOperatorPrecedence(ruleset)));
var optype2 = newTermEnclosure(newConstant(getOperatorTypeStringFromType(getOperatorType(ruleset))));
if (jslog_unify(name,name2,goal.bindings) && jslog_unify(rref,rref2,goal.bindings) &&
jslog_unify(optype,optype2,goal.bindings) && jslog_unify(pri,pri2,goal.bindings))
{
prover.explored.push(goal);
return true;
}
else
{
removeBindings(goal.bindings);
goal.rulesets_index++;
}
}
// no ruleset matches
{
removeBindings(goal.bindings);
prover.frontier.push(goal);
return false;
}
}
function internal_catch_try_fn(goal,prover)
{var encl = getFinalEnclosure(goal.encl);
var g = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var t3 = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[2]));
goal.prover = newQueryProver(prover.kb,g);
try
{var t = newSubtermEnclosure(encl.enclosure,newConstant('true'));
if (proveProver(goal.prover) && jslog_unify(t3,t,goal.bindings))
{
prover.explored.push(goal);
return true;
}
removeBindings(goal.bindings);
stopProver(goal.prover);
prover.frontier.push(goal);
return false;
}
catch (err)
{
return internal_catch_handle_catch(encl,err,t3,goal,prover);
}
}
function internal_catch_handle_catch(encl,err,t3,goal,prover)
{var e;
var ex = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
stopProver(goal.prover);
if (err.constructor == Exception)
e = err.encl;
else
{
err = newInternalErrorException(err.toString())
e = err.encl;
}
if (jslog_unify(e,ex,goal.bindings))
{var f = newSubtermEnclosure(encl.enclosure,newConstant('fail'));
if (jslog_unify(t3,f,goal.bindings))
{
prover.explored.push(goal);
return true;
}
removeBindings(goal.bindings);
prover.frontier.push(goal);
return false;
}
else
{
removeBindings(goal.bindings);
throw err;
}
}
///////////////////////////////////
// *_retry_fn(goal,prover) is a traversal function called when attempting to prove
// goal again (i.e., a goal retry). goal was just removed from explored, but is not on either
// frontier or explored stack.
// goal will need to be placed on one of either the frontier or explored stack.
// Returns true if the goal can be explored further, false if not.
// If false, then all bindings must be undone.
// If an exception is thrown, only the bindings for goal are undone externally. If the *_try_fn
// has caused any bindings for another goal, it must try / catch for exceptions and clean up
// before re-throwing.
// Assumes goal.bindings == new Array()
///////////////////////////////////
function cut_retry_fn(goal,prover)
{var g;
if (!goal.parent_is_ancestor)
removeChildGoalsFromFrontier(goal.parent,prover.frontier)
while ((g = prover.explored.pop()) != undefined)
{
undoGoal(g,false);
if (g == goal.parent)
{
if (goal.parent_is_ancestor)
prover.explored.push(g);
else
prover.frontier.push(g);
break;
}
};
return false;
}
function internal_clause_retry_fn(goal,prover)
{var encl = getFinalEnclosure(goal.encl);
var body = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var rref = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[2]));
var idx = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[3]));
var doinc = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[4]));
// if idx was bound, there is no retry
if (isNumber(idx.term))
{
prover.frontier.push(goal);
goal.subgoal = null;
return false;
}
if (isInteger(doinc.term))
goal.subgoal.rule_index += doinc.term.name;
else
goal.subgoal.rule_index++;
return internal_clause_test(body,rref,idx,goal,prover);
}
function internal_current_op_retry_fn(goal,prover)
{var encl = getFinalEnclosure(goal.encl);
var name = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var optype = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var pri = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[2]));
var rref = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[3]));
goal.rulesets_index++;
if (goal.rulesets_index >= goal.rulesets_array.length)
{
prover.frontier.push(goal);
return false;
}
return internal_current_op_test(name,optype,pri,rref,goal,prover);
}
function internal_catch_retry_fn(goal,prover)
{var encl = getFinalEnclosure(goal.encl);
var t3 = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[2]));
if (isProverStateDone(goal.prover))
{
prover.frontier.push(goal);
return false;
}
try
{var t = newSubtermEnclosure(encl.enclosure,newConstant('true'));
if (retryProver(goal.prover) && jslog_unify(t3,t,goal.bindings))
{
prover.explored.push(goal);
return true;
}
removeBindings(goal.bindings);
stopProver(goal.prover);
prover.frontier.push(goal);
return false;
}
catch (err)
{
return internal_catch_handle_catch(encl,err,t3,goal,prover);
}
}
///////////////////////////////////
// *_undo_fn(goal,retry) is a binding undo function called when undoing or retrying traversal goal.
// If retry is true, then a retry attempt will follow, if retry if false, undo completely.
// Only needs to undo bindings other than goal.bindings.
// It is important that *_undo_fn functions never fail to undo their bindings, even if called
// multiple times for the same goal, and they should never throw exceptions -- except if system
// integrity is compromised.
///////////////////////////////////
function internal_clause_undo_fn(goal,retry)
{
if (goal.subgoal != null && goal.subgoal.bindings != null)
removeBindings(goal.subgoal.bindings);
}
function internal_catch_undo_fn(goal,retry)
{
if (!retry && goal.prover != null)
stopProver(goal.prover);
}
///////////////////////////////////
// *_fn(goal) is a function called when attempting to prove goal.
// Returns true if the goal succeeds, false if not.
// If false, then all bindings must be undone. Only use goal.bindings to register bindings.
// Assumes goal.bindings == new Array()
///////////////////////////////////
function is_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = newSubtermEnclosure(encl.enclosure,encl.term.children[0]);
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var x;
x = jslog_Evaluate(goal.kb,rhs);
if (!isNumber(x))
throw newErrorException("Expected RHS to evaluate to a number in operator: is/2");
return jslog_unify(lhs,newBlankEnclosure(0,x),goal.bindings);
}
function throw_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var term = newDuplicateTermFromEnclosure(lhs);
throw new Exception(newTermEnclosure(term));
return false;
}
function isvar_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
return (isVariable(lhs.term));
}
function isnonvar_fn(goal)
{
return (!isvar_fn(goal));
}
function isconstant_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
return (isConstant(lhs.term));
}
function isconstornum_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
return (isConstant(lhs.term) || isNumber(lhs.term));
}
function iscompound_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
return (isAtom(lhs.term) && lhs.term.children.length > 0);
}
function isnumber_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
return (isNumber(lhs.term));
}
function isinteger_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
return (isInteger(lhs.term));
}
function lt_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var x,y;
x = jslog_Evaluate(goal.kb,lhs);
y = jslog_Evaluate(goal.kb,rhs);
if (isNumber(x) && isNumber(y))
return x.name < y.name;
else
throw newErrorException("Expression evaluating to Number expected in operator: </2");
}
function lte_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var x,y;
x = jslog_Evaluate(goal.kb,lhs);
y = jslog_Evaluate(goal.kb,rhs);
if (isNumber(x) && isNumber(y))
return x.name <= y.name;
else
throw newErrorException("Expression evaluating to Number expected in operator: =</2");
}
function gt_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var x,y;
x = jslog_Evaluate(goal.kb,lhs);
y = jslog_Evaluate(goal.kb,rhs);
if (isNumber(x) && isNumber(y))
return x.name > y.name;
else
throw newErrorException("Expression evaluating to Number expected in operator: >/2");
}
function gte_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var x,y;
x = jslog_Evaluate(goal.kb,lhs);
y = jslog_Evaluate(goal.kb,rhs);
if (isNumber(x) && isNumber(y))
return x.name >= y.name;
else
throw newErrorException("Expression evaluating to Number expected in operator: >=/2");
}
function eq_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var x,y;
x = jslog_Evaluate(goal.kb,lhs);
y = jslog_Evaluate(goal.kb,rhs);
if (isNumber(x) && isNumber(y))
return x.name == y.name;
else
throw newErrorException("Expression evaluating to Number expected in operator: =:=/2");
}
function neq_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var x,y;
x = jslog_Evaluate(goal.kb,lhs);
y = jslog_Evaluate(goal.kb,rhs);
if (isNumber(x) && isNumber(y))
return x.name != y.name;
else
throw newErrorException("Expression evaluating to Number expected in operator: =\=/2");
}
function unify_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = newSubtermEnclosure(encl.enclosure,encl.term.children[0]);
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[1]);
return jslog_unify(lhs,rhs,goal.bindings);
}
function unify_with_occurs_check_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = newSubtermEnclosure(encl.enclosure,encl.term.children[0]);
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[1]);
return jslog_unify_with_occurs_check(lhs,rhs,goal.bindings);
}
function identical_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = newSubtermEnclosure(encl.enclosure,encl.term.children[0]);
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[1]);
var result = jslog_compare(lhs,rhs);
return (result == 0);
}
function internal_equivalent_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
return jslog_equivalent(lhs,rhs);
}
function internal_nequivalent_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
return !jslog_equivalent(lhs,rhs);
}
function internal_compare_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = newSubtermEnclosure(encl.enclosure,encl.term.children[0]);
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[1]);
var cret = newSubtermEnclosure(encl.enclosure,encl.term.children[2]);
var result = jslog_compare(lhs,rhs);
var cval;
if (result < 0)
cval = '<';
else if (result > 0)
cval = '>';
else
cval = '=';
return jslog_unify(cret,newSubtermEnclosure(encl.enclosure,newConstant(cval)),goal.bindings);
}
function compare_lt_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = newSubtermEnclosure(encl.enclosure,encl.term.children[0]);
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[1]);
var result = jslog_compare(lhs,rhs);
return (result < 0);
}
function compare_lte_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = newSubtermEnclosure(encl.enclosure,encl.term.children[0]);
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[1]);
var result = jslog_compare(lhs,rhs);
return (result <= 0);
}
function compare_gt_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = newSubtermEnclosure(encl.enclosure,encl.term.children[0]);
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[1]);
var result = jslog_compare(lhs,rhs);
return (result > 0);
}
function compare_gte_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = newSubtermEnclosure(encl.enclosure,encl.term.children[0]);
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[1]);
var result = jslog_compare(lhs,rhs);
return (result >= 0);
}
function arg_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var idx = newSubtermEnclosure(encl.enclosure,encl.term.children[0]);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[2]);
if (isInteger(idx.term))
{var i = idx.term.name - 1;
if (isAtom(lhs.term))
{
if (i >= 0 && i < lhs.term.children.length)
{var t = getFinalEnclosure(newSubtermEnclosure(lhs.enclosure,lhs.term.children[i]));
return jslog_unify(rhs,t,goal.bindings);
}
else
throw newErrorException("Index out of bounds in arg/3.");
}
else
throw newErrorException("Expected atom in arg/3.");
}
else
throw newErrorException("Expected integer in arg/3.");
return false;
}
function atom_to_list_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
if (isAtom(lhs.term))
{var list = newListNull();
for (var i = lhs.term.children.length - 1; i >= 0; i--)
list = newListPair(lhs.term.children[i],list);
lhs = newSubtermEnclosure(lhs.enclosure,newListPair(newConstant(lhs.term.name),list));
}
else if (isNumber(lhs.term))
{
lhs = newBlankEnclosure(0,newListPair(lhs.term,newListNull()));
}
else if (isListPair(rhs.term))
{var head = getFinalEnclosure(newSubtermEnclosure(rhs.enclosure,rhs.term.children[0]));
var atom;
if (isConstant(head.term))
{
atom = newBlankEnclosure(0,newAtom(head.term.name,[]));
}
else
throw newErrorException("Expected constant value in =../2.");
do
{
rhs = getFinalEnclosure(newSubtermEnclosure(rhs.enclosure,rhs.term.children[1]));
if (isListPair(rhs.term))
{var i = atom.term.children.length;
var v = newVariable('_');
head = getFinalEnclosure(newSubtermEnclosure(rhs.enclosure,rhs.term.children[0]));
v.children[0] = i;
atom.term.children[i] = v;
atom.enclosure[i] = head;
}
else if (isListNull(rhs.term))
break;
else
throw newErrorException("Expected list pair in =../2.");
} while (head != null);
rhs = atom;
}
else
throw newErrorException("Expected valid instantiated value in =../2.");
return jslog_unify(lhs,rhs,goal.bindings);
}
function atom_length_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[1]);
var l;
if (!isConstant(lhs.term))
throw newErrorException("Expected constant atom in atom_lenth/2.");
l = newSubtermEnclosure(encl.enclosure,newNumber(lhs.term.name.length));
return jslog_unify(rhs,l,goal.bindings);
}
function char_code_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var c;
if (isConstant(lhs.term))
{
if (lhs.term.name.length != 1)
throw newErrorException("Expected single character atom in char_code/2.");
c = newSubtermEnclosure(encl.enclosure,newNumber(lhs.term.name.charCodeAt(0)));
return jslog_unify(rhs,c,goal.bindings);
}
else if (isInteger(rhs.term))
{
c = newSubtermEnclosure(encl.enclosure,newConstant(String.fromCharCode(rhs.term.name)));
return jslog_unify(lhs,c,goal.bindings);
}
else
throw newErrorException("Expected either atom or integer in char_code/2.");
return false;
}
function atom_chars_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var c;
if (isConstant(lhs.term))
{var cp = newListNull();
for (var i = lhs.term.name.length - 1; i >= 0; i--)
cp = newListPair(newConstant(lhs.term.name.charAt(i)),cp);
c = newSubtermEnclosure(encl.enclosure,cp);
return jslog_unify(rhs,c,goal.bindings);
}
else
{var s = "";
var cp;
while (!isListNull(rhs.term))
{
if (!isListPair(rhs.term))
throw newErrorException("Expected either atom constant or character list in atom_chars/2.");
cp = getFinalEnclosure(newSubtermEnclosure(rhs.enclosure,rhs.term.children[0]));
if (isConstant(cp.term) && cp.term.name.length == 1)
{
s += cp.term.name;
}
else
throw newErrorException("Expected single character atoms in character list in atom_chars/2.");
rhs = getFinalEnclosure(newSubtermEnclosure(rhs.enclosure,rhs.term.children[1]));
}
c = newSubtermEnclosure(encl.enclosure,newConstant(s));
return jslog_unify(lhs,c,goal.bindings);
}
}
function internal_number_atom_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[1]));
var c;
if (isNumber(lhs.term))
{
c = newSubtermEnclosure(encl.enclosure,newConstant(lhs.term.name.toString()));
return jslog_unify(rhs,c,goal.bindings);
}
else if (isConstant(rhs.term))
{var n = parseFloat(rhs.term.name);
if (isNaN(n))
throw newErrorException("Expected atom representing number in internal:number_atom/2.");
c = newSubtermEnclosure(encl.enclosure,newNumber(n));
return jslog_unify(lhs,c,goal.bindings);
}
else
throw newErrorException("Expected number or atom constant in internal:number_atom/2.");
return false;
}
function copy_term_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[1]);
return jslog_unify(newDuplicateEnclosure(lhs),rhs,goal.bindings);
}
function internal_copy_term_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[1]);
var term = newDuplicateTermFromEnclosure(lhs);
return jslog_unify(newTermEnclosure(term),rhs,goal.bindings);
}
function internal_term_variables_fn(goal)
{var encl = getFinalEnclosure(goal.encl);
var lhs = getFinalEnclosure(newSubtermEnclosure(encl.enclosure,encl.term.children[0]));
var rhs = newSubtermEnclosure(encl.enclosure,encl.term.children[1]);
var vencls = enumFinalVariableEnclosures(lhs);
var vlist;
var result;
vlist = newListNull();
for (var i = vencls.length - 1; i >= 0; i--)
{var v = newVariable('_');
v.children[0] = i;
vlist = newListPair(v,vlist);
}
result = newSubtermEnclosure(vencls,vlist);
return jslog_unify(result,rhs,goal.bindings);
}