-
Notifications
You must be signed in to change notification settings - Fork 3
/
kanren.spec.ts
187 lines (160 loc) · 9.39 KB
/
kanren.spec.ts
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
import { kanren } from "./kanren";
import { IState } from "./data/State";
import { List } from "immutable";
import { Goal } from "./data/Goal";
import { Stream } from "./data/Stream";
const { unify, runAll, conj, disj, callWithFresh } = kanren({});
const hasSolutions = (solutions: List<IState>): boolean => !solutions.isEmpty();
describe("mk", () => {
const succeed = (state: IState): Stream<IState> => List<IState>([state]);
const fail = (_: IState): Stream<IState> => List<IState>([]);
describe("scope goals", () => {
describe("callWithFresh", () => {
it("injects a new logic variable into the provided goal", () => {
const f = runAll({ goal: callWithFresh((a) => unify(a, 5)) });
expect(hasSolutions(f)).toBeTruthy();
expect(f.size).toBe(1);
const solution1 = f.get(0);
expect(solution1.count).toBe(1);
expect(solution1.substitution.size).toBe(1);
const state1 = solution1.substitution.get(0);
expect(state1).toEqual({ left: Symbol.for(`${solution1.count - 1}`), right: 5 });
});
it("does not affect the provided goal past injecting the logic variable", () => {
expect(hasSolutions(runAll({ goal: callWithFresh((a) => succeed) }))).toBeTruthy();
expect(hasSolutions(runAll({ goal: callWithFresh((a) => fail) }))).toBeFalsy();
});
});
});
describe("aggregator goals", () => {
describe("conj", () => {
it("evaluates to the empty set when one, the other, or both goals do not succeed in the state", () => {
expect(hasSolutions(runAll({ goal: conj(succeed, fail) }))).toBeFalsy();
expect(hasSolutions(runAll({ goal: conj(fail, succeed) }))).toBeFalsy();
expect(hasSolutions(runAll({ goal: conj(fail, fail) }))).toBeFalsy();
});
it("evaluates to a non-empty set when both goals succeed in the state", () => {
expect(hasSolutions(runAll({ goal: conj(succeed, succeed) }))).toBeTruthy();
});
});
describe("disj", () => {
it("evaluated to a non-empty set when one, the other, or both goals succeed in the state", () => {
expect(hasSolutions(runAll({ goal: disj(succeed, fail) }))).toBeTruthy();
expect(hasSolutions(runAll({ goal: disj(succeed, succeed) }))).toBeTruthy();
expect(hasSolutions(runAll({ goal: disj(succeed, succeed) }))).toBeTruthy();
});
it("evaluates to the empty set when neither goal succeeds", () => {
expect(hasSolutions(runAll({ goal: disj(fail, fail) }))).toBeFalsy();
});
});
});
describe("constraint goals", () => {
describe("unify", () => {
const canUnify = (goal: Goal): boolean => hasSolutions(runAll({ goal }));
describe("symbols (logic variables)", () => {
it("can be unified with a symbol", () => {
expect(canUnify(unify(Symbol("a"), Symbol("b")))).toBeTruthy();
});
});
describe("array terms", () => {
it("can be unified with a symbol", () => {
expect(canUnify(unify([], Symbol("[]")))).toBeTruthy();
expect(canUnify(unify(Symbol("[]"), []))).toBeTruthy();
const lvar = Symbol("lvar");
expect(canUnify(conj(unify(lvar, [2]), unify(lvar, [2])))).toBeTruthy();
});
it("cannot be unified with a symbol that's been unified to another term", () => {
const lvar = Symbol("lvar");
expect(canUnify(conj(unify(lvar, 1), unify(lvar, [2])))).toBeFalsy();
expect(canUnify(conj(unify(lvar, true), unify(lvar, [2])))).toBeFalsy();
expect(canUnify(conj(unify(lvar, false), unify(lvar, [2])))).toBeFalsy();
expect(canUnify(conj(unify(lvar, []), unify(lvar, [2])))).toBeFalsy();
expect(canUnify(conj(unify(lvar, ""), unify(lvar, [2])))).toBeFalsy();
});
it("can be unified with other arrays of the same known structure", () => {
expect(canUnify(unify([], []))).toBeTruthy();
expect(canUnify(unify([1], [1]))).toBeTruthy();
expect(canUnify(unify([1, [2]], [1, [2]]))).toBeTruthy();
expect(canUnify(unify([Symbol("a")], [1]))).toBeTruthy();
});
it("cannot be unified with other arrays of different structures", () => {
expect(canUnify(unify([], [1]))).toBeFalsy();
const lvar = Symbol("lvar");
expect(canUnify(conj(unify(lvar, 1), unify([lvar], [2])))).toBeFalsy();
});
});
describe("number terms", () => {
it("can be unified with a symbol", () => {
expect(canUnify(unify(1, Symbol("1")))).toBeTruthy();
expect(canUnify(unify(Symbol("1"), 1))).toBeTruthy();
const lvar = Symbol("lvar");
expect(canUnify(conj(unify(lvar, 1), unify(lvar, 1)))).toBeTruthy();
});
it("cannot be unified with a symbol that's been unified to another term", () => {
const lvar = Symbol("lvar");
expect(canUnify(conj(unify(lvar, 2), unify(lvar, 1)))).toBeFalsy();
expect(canUnify(conj(unify(lvar, true), unify(lvar, 1)))).toBeFalsy();
expect(canUnify(conj(unify(lvar, false), unify(lvar, 1)))).toBeFalsy();
expect(canUnify(conj(unify(lvar, []), unify(lvar, 1)))).toBeFalsy();
expect(canUnify(conj(unify(lvar, ""), unify(lvar, 1)))).toBeFalsy();
});
it("can be unified with other numbers of the same known structure", () => {
expect(canUnify(unify(1, 1))).toBeTruthy();
expect(canUnify(unify(2, 2))).toBeTruthy();
});
it("cannot be unified with other numbers of different structures", () => {
expect(canUnify(unify(1, 2))).toBeFalsy();
const lvar = Symbol("lvar");
expect(canUnify(conj(unify(lvar, 1), unify(lvar, 2)))).toBeFalsy();
});
});
describe("boolean terms", () => {
it("can be unified with a symbol", () => {
expect(canUnify(unify(true, Symbol("true")))).toBeTruthy();
expect(canUnify(unify(Symbol("true"), true))).toBeTruthy();
const lvar = Symbol("lvar");
expect(canUnify(conj(unify(lvar, true), unify(lvar, true)))).toBeTruthy();
});
it("cannot be unified with a symbol that's been unified to another term", () => {
const lvar = Symbol("lvar");
expect(canUnify(conj(unify(lvar, 2), unify(lvar, true)))).toBeFalsy();
expect(canUnify(conj(unify(lvar, false), unify(lvar, true)))).toBeFalsy();
expect(canUnify(conj(unify(lvar, []), unify(lvar, true)))).toBeFalsy();
expect(canUnify(conj(unify(lvar, ""), unify(lvar, true)))).toBeFalsy();
});
it("can be unified with other booleans of the same known structure", () => {
expect(canUnify(unify(true, true))).toBeTruthy();
expect(canUnify(unify(false, false))).toBeTruthy();
});
it("cannot be unified with other booleans of different structures", () => {
expect(canUnify(unify(true, false))).toBeFalsy();
const lvar = Symbol("lvar");
expect(canUnify(conj(unify(lvar, true), unify(lvar, false)))).toBeFalsy();
});
});
describe("string terms", () => {
it("can be unified with a symbol", () => {
expect(canUnify(unify("", Symbol("")))).toBeTruthy();
expect(canUnify(unify(Symbol(""), ""))).toBeTruthy();
const lvar = Symbol("lvar");
expect(canUnify(conj(unify(lvar, ""), unify(lvar, "")))).toBeTruthy();
});
it("cannot be unified with a symbol that's been unified to another term", () => {
const lvar = Symbol("lvar");
expect(canUnify(conj(unify(lvar, 2), unify(lvar, "")))).toBeFalsy();
expect(canUnify(conj(unify(lvar, false), unify(lvar, "")))).toBeFalsy();
expect(canUnify(conj(unify(lvar, []), unify(lvar, "")))).toBeFalsy();
});
it("can be unified with other strings of the same known structure", () => {
expect(canUnify(unify("", ""))).toBeTruthy();
expect(canUnify(unify("a", "a"))).toBeTruthy();
});
it("cannot be unified with other strings of different structures", () => {
expect(canUnify(unify("a", "b"))).toBeFalsy();
const lvar = Symbol("lvar");
expect(canUnify(conj(unify(lvar, "a"), unify(lvar, "b")))).toBeFalsy();
});
});
});
});
});