Skip to content

Commit 63f9fa3

Browse files
committed
Various optimizations
1 parent af6f45d commit 63f9fa3

13 files changed

+321
-176
lines changed

Makefile

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
benchmark/naiverev.js: benchmark/naiverev.pl
2-
./transpile.sh $<
3-
cat benchmark/header.js >> $@
2+
swipl -f src/compiler.pl -t transpile $<
43

54
naiverev: benchmark/naiverev.js
6-
node $<
5+
node benchmark/naiverev_benchmark.js
6+
7+
profile: benchmark/naiverev.js
8+
node --prof benchmark/naiverev_benchmark.js
9+
node-tick-processor
10+
11+
beautify: benchmark/naiverev.js
12+
js-beautify -o $<.beautified.js $<
13+
14+
examples/append.js: examples/append.pl
15+
swipl -f src/compiler.pl -t transpile $<
16+
17+
append: examples/append.js
18+
js-beautify -o $<.beautified.js $<
19+
20+
clean:
21+
rm -f benchmark/naiverev.js
22+
rm -f benchmark/naiverev.js.beautified.js
23+
rm -f examples/append.js
24+
rm -f examples/append.js.js.beautified.js
25+
rm -f v8.log
26+
27+
check:
28+
jshint runtime.js
29+
30+
.PHONY: clean check

README.md

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
prolog-target-js
22
================
33

4-
Simple Prolog to JS transpiler. The project is at very incomplete state at the moment.
4+
Simple Prolog to JS transpiler. The project is at rather incomplete state at the moment.
55

66
Example input:
77

@@ -13,32 +13,68 @@ append([X|Xs], Ys, [X|Zs]):- append(Xs, Ys, Zs).
1313
Example output:
1414

1515
```javascript
16-
function append_3($0, $1, $2, s, cb) {
17-
function append_3_0($0, $1, $2, s, cb) {
18-
var $3 = new Var();
19-
s.push(function() {
20-
return append_3_1($0, $1, $2, s, cb);
21-
});
22-
return unify_2($0, '[]', s, function() {
23-
return unify_2($1, $3, s, function() {
24-
return unify_2($2, $3, s, function() {
25-
return cb;
26-
});
27-
});
28-
});
29-
}
30-
function append_3_1($0, $1, $2, s, cb) {
31-
var $3 = new Var(), $4 = new Var(), $5 = new Var(), $6 = new Var();
32-
return unify_2($0, new Struct('.', $3, $4), s, function() {
33-
return unify_2($1, $5, s, function() {
34-
return unify_2($2, new Struct('.', $3, $6), s, function() {
35-
return append_3($4, $5, $6, s, function() {
36-
return cb;
37-
});
38-
});
39-
});
40-
});
41-
}
42-
return append_3_0($0, $1, $2, s, cb);
16+
var runtime = require("../runtime");
17+
var Var = runtime.Var;
18+
var Struct = runtime.Struct;
19+
var $U = runtime.unification;
20+
var $B = runtime.backtrack;
21+
22+
function append_3_0($0, $1, $2, s, cb) {
23+
var $3 = new Var();
24+
s.push(function() {
25+
return append_3_1($0, $1, $2, s, cb);
26+
});
27+
if ($U(s, $0, '[]')) {
28+
if ($U(s, $1, $3)) {
29+
if ($U(s, $2, $3)) {
30+
return cb;
31+
} else {
32+
return $B(s);
33+
}
34+
} else {
35+
return $B(s);
36+
}
37+
} else {
38+
return $B(s);
39+
}
40+
}
41+
function append_3_1($0, $1, $2, s, cb) {
42+
var $3 = new Var(),
43+
$4 = new Var(),
44+
$5 = new Var(),
45+
$6 = new Var();
46+
if ($U(s, $0, new Struct('.', $3, $4))) {
47+
if ($U(s, $1, $5)) {
48+
if ($U(s, $2, new Struct('.', $3, $6))) {
49+
return append_3_0($4, $5, $6, s, cb);
50+
} else {
51+
return $B(s);
52+
}
53+
} else {
54+
return $B(s);
55+
}
56+
} else {
57+
return $B(s);
58+
}
4359
}
60+
exports.append_3 = append_3_0;
4461
```
62+
63+
Calling the predicate
64+
---------------------
65+
66+
```javascript
67+
var runtime = require('../runtime');
68+
var util = require('../util');
69+
var append = require('./append');
70+
71+
var list1 = util.array2List([1, 2, 3, 4, 5]);
72+
var list2 = util.array2List([6, 7, 8, 9, 10]);
73+
74+
var result = new runtime.Var();
75+
76+
runtime.run(append.append_3(list1, list2, result, [], null));
77+
78+
console.log('Result is: ' + runtime.toString(result));
79+
```
80+

benchmark/naiverev.js

Lines changed: 1 addition & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmark/naiverev.js.beautified.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var runtime = require("../runtime");
2+
var Var = runtime.Var;
3+
var Struct = runtime.Struct;
4+
var $U = runtime.unification;
5+
var $B = runtime.backtrack;
6+
7+
function naiverev_2_0($0, $1, s, cb) {
8+
s.push(function() {
9+
return naiverev_2_1($0, $1, s, cb);
10+
});
11+
if ($U(s, $0, '[]')) {
12+
if ($U(s, $1, '[]')) {
13+
return cb;
14+
} else {
15+
return $B(s);
16+
}
17+
} else {
18+
return $B(s);
19+
}
20+
}
21+
function naiverev_2_1($0, $1, s, cb) {
22+
var $2 = new Var(),
23+
$3 = new Var(),
24+
$4 = new Var(),
25+
$5 = new Var();
26+
if ($U(s, $0, new Struct('.', $2, $3))) {
27+
if ($U(s, $1, $4)) {
28+
return naiverev_2_0($3, $5, s, function() {
29+
return append_3_0($5, new Struct('.', $2, '[]'), $4, s, cb);
30+
});
31+
} else {
32+
return $B(s);
33+
}
34+
} else {
35+
return $B(s);
36+
}
37+
}
38+
exports.naiverev_2 = naiverev_2_0;
39+
40+
function append_3_0($0, $1, $2, s, cb) {
41+
var $3 = new Var();
42+
s.push(function() {
43+
return append_3_1($0, $1, $2, s, cb);
44+
});
45+
if ($U(s, $0, '[]')) {
46+
if ($U(s, $1, $3)) {
47+
if ($U(s, $2, $3)) {
48+
return cb;
49+
} else {
50+
return $B(s);
51+
}
52+
} else {
53+
return $B(s);
54+
}
55+
} else {
56+
return $B(s);
57+
}
58+
}
59+
function append_3_1($0, $1, $2, s, cb) {
60+
var $3 = new Var(),
61+
$4 = new Var(),
62+
$5 = new Var(),
63+
$6 = new Var();
64+
if ($U(s, $0, new Struct('.', $3, $4))) {
65+
if ($U(s, $1, $5)) {
66+
if ($U(s, $2, new Struct('.', $3, $6))) {
67+
return append_3_0($4, $5, $6, s, cb);
68+
} else {
69+
return $B(s);
70+
}
71+
} else {
72+
return $B(s);
73+
}
74+
} else {
75+
return $B(s);
76+
}
77+
}
78+
exports.append_3 = append_3_0;

benchmark/naiverev_benchmark.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var naiverev = require('./naiverev');
44
var Var = runtime.Var;
55

66
var array = [];
7-
for (var i = 0; i < 100; i++) {
7+
for (var i = 0; i < 1000; i++) {
88
array.push(i);
99
}
1010

@@ -17,4 +17,4 @@ console.time('reverse');
1717
runtime.run(top);
1818
console.timeEnd('reverse');
1919

20-
console.log('Reversed is: ' + runtime.toString(reverse));
20+
//console.log('Reversed is: ' + runtime.toString(reverse));

benchmark/naiverev_benchmark.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
run:-
44
findall(E, between(1, 1000, E), Es),
5-
time(naiverev(Es, _)).
5+
profile(naiverev(Es, _)).

examples/append.js

Lines changed: 1 addition & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/append.js.beautified.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var runtime = require("../runtime");
2+
var Var = runtime.Var;
3+
var Struct = runtime.Struct;
4+
var $U = runtime.unification;
5+
var $B = runtime.backtrack;
6+
7+
function append_3_0($0, $1, $2, s, cb) {
8+
var $3 = new Var();
9+
s.push(function() {
10+
return append_3_1($0, $1, $2, s, cb);
11+
});
12+
if ($U(s, $0, '[]')) {
13+
if ($U(s, $1, $3)) {
14+
if ($U(s, $2, $3)) {
15+
return cb;
16+
} else {
17+
return $B(s);
18+
}
19+
} else {
20+
return $B(s);
21+
}
22+
} else {
23+
return $B(s);
24+
}
25+
}
26+
function append_3_1($0, $1, $2, s, cb) {
27+
var $3 = new Var(),
28+
$4 = new Var(),
29+
$5 = new Var(),
30+
$6 = new Var();
31+
if ($U(s, $0, new Struct('.', $3, $4))) {
32+
if ($U(s, $1, $5)) {
33+
if ($U(s, $2, new Struct('.', $3, $6))) {
34+
return append_3_0($4, $5, $6, s, cb);
35+
} else {
36+
return $B(s);
37+
}
38+
} else {
39+
return $B(s);
40+
}
41+
} else {
42+
return $B(s);
43+
}
44+
}
45+
exports.append_3 = append_3_0;

examples/append_test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ var runtime = require('../runtime');
22
var util = require('../util');
33
var append = require('./append');
44

5-
function cons(head, tail) {
6-
return new Struct('.', head, tail);
7-
}
8-
95
var list1 = util.array2List([1, 2, 3, 4, 5]);
106
var list2 = util.array2List([6, 7, 8, 9, 10]);
117

0 commit comments

Comments
 (0)