Skip to content

Commit fc8e790

Browse files
committed
Enable optimal reduction
1 parent b3bd65f commit fc8e790

3 files changed

Lines changed: 34 additions & 43 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ all: compile.js
44
node lambda -d samples/debug.mlc
55
node lambda -p samples/fact.mlc -a normal
66
node lambda -p samples/fact.mlc -a closed
7-
node lambda -p samples/larger.mlc -a normal
8-
node lambda -p samples/larger.mlc -a closed
9-
node lambda -p samples/exp.mlc -a closed
7+
node lambda -p samples/fact.mlc -a optimal
108

119
compile.js: grammar.jison
1210
npm install jison@0.4.15

‎encoding/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
//exports.abstract = require("./abstract");
44
exports.closed = require("./closed");
55
exports.normal = require("./normal");
6-
//exports.optimal = require("./optimal");
6+
exports.optimal = require("./optimal");
77
//exports.turning = require("./turning");

‎encoding/optimal/index.js‎

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,57 @@ const mkwire = generic.mkwire;
1010
const mktwins = generic.mktwins;
1111
const getfv = generic.getfv;
1212

13-
function psi(shared)
13+
function psi(shared, list)
1414
{
15-
var list = [];
16-
var template = "%s = \\fan_{0}(%s, %s)";
17-
var atom;
18-
19-
for (atom in shared) {
20-
var twins = shared[atom];
21-
var wleft = twins.left;
22-
var wright = twins.right;
23-
var eqn = template;
15+
for (const atom in shared) {
16+
const twins = shared[atom];
17+
const wleft = twins.left;
18+
const wright = twins.right;
19+
let eqn = "%s = \\fan_{0}(%s, %s)";
2420

2521
eqn = eqn.replace("%s", atom);
2622
eqn = eqn.replace("%s", wright);
2723
eqn = eqn.replace("%s", wleft);
2824

2925
list.push(eqn);
3026
}
31-
32-
return list;
3327
}
3428

3529
function mkscope(n)
3630
{
37-
var s = "%s";
38-
var i;
31+
let s = "%s";
3932

40-
for (i = 0; i < n; i++)
33+
for (let i = 0; i < n; i++)
4134
s = "\\scope_{0}(" + s + ")";
4235

4336
return s;
4437
}
4538

46-
function gamma(obj, root)
39+
function gamma(obj, root, list)
4740
{
48-
var list = [];
49-
var node = obj.node;
41+
const node = obj.node;
5042

5143
if ("atom" == node) {
5244
if (obj.free) {
53-
var agent = "\\atom_{this.mkid(\"%s\")}";
45+
let agent = "\\atom_{this.mkid(\"%s\")}";
5446

5547
agent = agent.replace("%s", obj.name);
48+
5649
list.push(root + " = " + agent);
5750
} else {
58-
var agent = mkscope(obj.index);
51+
let agent = mkscope(obj.index);
5952

6053
agent = agent.replace("%s", root);
54+
6155
list.push(obj.name + " = " + agent);
6256
}
6357
} else if ("abst" == node) {
64-
var tree = "\\lambda(%s, %s)";
65-
var body = obj.body;
66-
var fv = getfv(body);
67-
var id = obj.var;
68-
var wire = mkwire();
58+
const id = obj.var;
59+
const body = obj.body;
60+
const fv = getfv(body);
61+
const wire = mkwire();
62+
let tree = "\\lambda(%s, %s)";
63+
let agent;
6964

7065
if (id in fv)
7166
agent = id;
@@ -74,29 +69,27 @@ function gamma(obj, root)
7469

7570
tree = tree.replace("%s", agent);
7671
tree = tree.replace("%s", wire);
72+
7773
list.push(root + " = " + tree);
7874

79-
body = gamma(body, wire);
80-
list = list.concat(body);
75+
gamma(body, wire, list);
8176
} else if ("appl" == node) {
82-
var wleft = mkwire();
83-
var wright = mkwire();
84-
var agent = "\\apply(%s, %s)";
85-
var left = obj.left;
86-
var right = obj.right;
87-
var shared = mktwins(left, right);
77+
const wleft = mkwire();
78+
const wright = mkwire();
79+
const left = obj.left;
80+
const right = obj.right;
81+
const shared = mktwins(left, right);
82+
let agent = "\\apply(%s, %s)";
8883

8984
agent = agent.replace("%s", wright);
9085
agent = agent.replace("%s", root);
86+
9187
list.push(wleft + " = " + agent);
9288

93-
left = gamma(left, wleft);
94-
right = gamma(right, wright);
95-
shared = psi(shared);
96-
list = list.concat(left, right, shared);
89+
gamma(left, wleft, list);
90+
gamma(right, wright, list);
91+
psi(shared, list);
9792
}
98-
99-
return list;
10093
}
10194

10295
function encode(term)

0 commit comments

Comments
 (0)