forked from rulespace/rulespace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsp.js
226 lines (194 loc) · 3.29 KB
/
rsp.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
// see js-frontend/js2rsp for abstract syntax
//export { Sym } from './sexp-reader.js';
export class Program
{
constructor(relations, rules)
{
this.relations = relations;
this.rules = rules;
}
toString()
{
return this.tuples.concat(this.rules).join('\n');
}
}
export class Rule
{
static counter = 0;
constructor(head, body)
{
this.head = head;
this.body = body;
this._id = Rule.counter++;
}
aggregates()
{
return this.head.terms[this.head.terms.length - 1] instanceof Agg;
}
tupleArity()
{
return this.body.reduce((acc, exp) => ((exp instanceof Atom) || (exp instanceof Neg)) ? acc+1 : acc, 0);
}
toString()
{
if (this.body.length === 0)
{
return `(rule ${this.head})`;
}
return `(rule ${this.head} ${this.body.join(' ')})`;
}
}
export class Atom
{
constructor(pred, terms)
{
this.pred = pred;
this.terms = terms;
}
arity()
{
return this.terms.length;
}
toString()
{
if (this.terms.length === 0)
{
return `[${this.pred}]`;
}
return `[${this.pred} ${this.terms.map(toTermString).join(' ')}]`;
}
}
// export class Relation TODO
// {
// constructor(pred, terms)
// {
// this.pred = pred;
// this.terms = terms;
// }
// arity()
// {
// return this.terms.length;
// }
// toString()
// {
// if (this.terms.length === 0)
// {
// return `(declaration [${this.pred}])`;
// }
// return `(declaration [${this.pred} ${this.terms.map(toTermString).join(' ')}])`;
// }
// }
export class Neg
{
constructor(atom)
{
this.atom = atom;
}
toString()
{
return "¬" + this.atom;
}
}
export class Agg
{
constructor(aggregator, aggregate)
{
this.aggregator = aggregator;
this.aggregate = aggregate;
}
toString()
{
return `{${this.aggregator}: ${this.aggregate}}`;
}
}
export class Lit
{
constructor(value)
{
this.value = value;
}
toString()
{
const value = this.value;
if (typeof value === "string")
{
return "\"" + value + "\"";
}
if (value === true)
{
return "#t";
}
if (value === false)
{
return "#f";
}
return String(value);
}
}
export class Var
{
constructor(name)
{
this.name = name;
}
toString()
{
return String(this.name);
}
}
export class Assign
{
constructor(operator, left, right)
{
this.operator = operator;
this.left = left;
this.right = right;
}
toString()
{
return `(${this.operator} ${this.left} ${this.right})`;
}
}
export class App // Exp
{
constructor(operator, operands)
{
this.operator = operator;
this.operands = operands;
}
toString()
{
return `(${this.operator} ${this.operands.join(' ')})`;
}
}
export class Paren // Exp
{
constructor(exp)
{
this.exp = exp;
}
toString()
{
return this.exp.toString(); // sexp structure is enough (paren is 'helper' for C-like languages with notational ambiguity)
}
}
export class Lam
{
constructor(params, body)
{
this.params = params;
this.body = body;
}
toString()
{
return `(lambda (${this.params.join(' ')}) ${this.body})`;
}
}
function toTermString(x)
{
if (Array.isArray(x))
{
return `#(${x.map(toTermString).join(' ')})`;
}
return String(x);
}