@nahisaho/musubix-synthesis 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +100 -0
- package/dist/dsl/DSL.d.ts +73 -0
- package/dist/dsl/DSL.d.ts.map +1 -0
- package/dist/dsl/DSL.js +250 -0
- package/dist/dsl/DSL.js.map +1 -0
- package/dist/dsl/DSLBuilder.d.ts +33 -0
- package/dist/dsl/DSLBuilder.d.ts.map +1 -0
- package/dist/dsl/DSLBuilder.js +51 -0
- package/dist/dsl/DSLBuilder.js.map +1 -0
- package/dist/dsl/TypeSystem.d.ts +51 -0
- package/dist/dsl/TypeSystem.d.ts.map +1 -0
- package/dist/dsl/TypeSystem.js +253 -0
- package/dist/dsl/TypeSystem.js.map +1 -0
- package/dist/dsl/index.d.ts +8 -0
- package/dist/dsl/index.d.ts.map +1 -0
- package/dist/dsl/index.js +8 -0
- package/dist/dsl/index.js.map +1 -0
- package/dist/errors.d.ts +93 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +142 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/rules/MetaLearner.d.ts +50 -0
- package/dist/rules/MetaLearner.d.ts.map +1 -0
- package/dist/rules/MetaLearner.js +144 -0
- package/dist/rules/MetaLearner.js.map +1 -0
- package/dist/rules/RuleExtractor.d.ts +69 -0
- package/dist/rules/RuleExtractor.d.ts.map +1 -0
- package/dist/rules/RuleExtractor.js +290 -0
- package/dist/rules/RuleExtractor.js.map +1 -0
- package/dist/rules/RuleLibrary.d.ts +55 -0
- package/dist/rules/RuleLibrary.d.ts.map +1 -0
- package/dist/rules/RuleLibrary.js +190 -0
- package/dist/rules/RuleLibrary.js.map +1 -0
- package/dist/rules/index.d.ts +9 -0
- package/dist/rules/index.d.ts.map +1 -0
- package/dist/rules/index.js +9 -0
- package/dist/rules/index.js.map +1 -0
- package/dist/synthesis/Enumerator.d.ts +78 -0
- package/dist/synthesis/Enumerator.d.ts.map +1 -0
- package/dist/synthesis/Enumerator.js +292 -0
- package/dist/synthesis/Enumerator.js.map +1 -0
- package/dist/synthesis/PBESynthesizer.d.ts +37 -0
- package/dist/synthesis/PBESynthesizer.d.ts.map +1 -0
- package/dist/synthesis/PBESynthesizer.js +187 -0
- package/dist/synthesis/PBESynthesizer.js.map +1 -0
- package/dist/synthesis/VersionSpace.d.ts +50 -0
- package/dist/synthesis/VersionSpace.d.ts.map +1 -0
- package/dist/synthesis/VersionSpace.js +102 -0
- package/dist/synthesis/VersionSpace.js.map +1 -0
- package/dist/synthesis/WitnessEngine.d.ts +64 -0
- package/dist/synthesis/WitnessEngine.d.ts.map +1 -0
- package/dist/synthesis/WitnessEngine.js +217 -0
- package/dist/synthesis/WitnessEngine.js.map +1 -0
- package/dist/synthesis/index.d.ts +9 -0
- package/dist/synthesis/index.d.ts.map +1 -0
- package/dist/synthesis/index.js +9 -0
- package/dist/synthesis/index.js.map +1 -0
- package/dist/types.d.ts +372 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +52 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PBE Synthesizer
|
|
3
|
+
* @module @nahisaho/musubix-synthesis
|
|
4
|
+
* @description Programming by Example synthesizer
|
|
5
|
+
* Traces to: REQ-SYN-003 (PBE Synthesis)
|
|
6
|
+
*/
|
|
7
|
+
import { SynthesisTimeoutError } from '../errors.js';
|
|
8
|
+
import { Enumerator, resetProgramIdCounter } from './Enumerator.js';
|
|
9
|
+
/**
|
|
10
|
+
* Default synthesis options
|
|
11
|
+
*/
|
|
12
|
+
const DEFAULT_OPTIONS = {
|
|
13
|
+
maxDepth: 5,
|
|
14
|
+
maxCost: 20,
|
|
15
|
+
timeout: 30000,
|
|
16
|
+
maxCandidates: 100,
|
|
17
|
+
useNeuralGuidance: false,
|
|
18
|
+
pruneThreshold: 0.1,
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* PBE Synthesizer implementation
|
|
22
|
+
*/
|
|
23
|
+
export class PBESynthesizer {
|
|
24
|
+
searchNodes;
|
|
25
|
+
pruned;
|
|
26
|
+
lastDsl;
|
|
27
|
+
constructor() {
|
|
28
|
+
this.searchNodes = 0;
|
|
29
|
+
this.pruned = 0;
|
|
30
|
+
this.lastDsl = null;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Synthesize a program from specification
|
|
34
|
+
*/
|
|
35
|
+
async synthesize(spec, dsl, options) {
|
|
36
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
37
|
+
const startTime = Date.now();
|
|
38
|
+
this.searchNodes = 0;
|
|
39
|
+
this.pruned = 0;
|
|
40
|
+
this.lastDsl = dsl;
|
|
41
|
+
// Reset ID counter for consistent test results
|
|
42
|
+
resetProgramIdCounter();
|
|
43
|
+
const candidates = [];
|
|
44
|
+
const enumerator = new Enumerator(dsl);
|
|
45
|
+
try {
|
|
46
|
+
// Enumerate programs
|
|
47
|
+
const enumeration = enumerator.enumerateForSpec(spec, {
|
|
48
|
+
maxDepth: opts.maxDepth,
|
|
49
|
+
maxCost: opts.maxCost,
|
|
50
|
+
yieldInterval: 100,
|
|
51
|
+
});
|
|
52
|
+
for await (const program of enumeration) {
|
|
53
|
+
// Check timeout
|
|
54
|
+
if (Date.now() - startTime > opts.timeout) {
|
|
55
|
+
throw new SynthesisTimeoutError(opts.timeout);
|
|
56
|
+
}
|
|
57
|
+
this.searchNodes++;
|
|
58
|
+
// Check if program satisfies all examples
|
|
59
|
+
if (this.satisfiesAllExamples(program, spec.examples, dsl)) {
|
|
60
|
+
candidates.push(program);
|
|
61
|
+
// Return immediately if we have a good solution
|
|
62
|
+
if (candidates.length >= opts.maxCandidates) {
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
this.pruned++;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const duration = Date.now() - startTime;
|
|
71
|
+
if (candidates.length > 0) {
|
|
72
|
+
// Sort by cost
|
|
73
|
+
candidates.sort((a, b) => (a.cost ?? 0) - (b.cost ?? 0));
|
|
74
|
+
return {
|
|
75
|
+
success: true,
|
|
76
|
+
program: candidates[0],
|
|
77
|
+
candidates,
|
|
78
|
+
duration,
|
|
79
|
+
synthesisTime: duration,
|
|
80
|
+
searchNodes: this.searchNodes,
|
|
81
|
+
candidatesExplored: this.searchNodes,
|
|
82
|
+
pruned: this.pruned,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
success: false,
|
|
87
|
+
candidates: [],
|
|
88
|
+
duration,
|
|
89
|
+
synthesisTime: duration,
|
|
90
|
+
searchNodes: this.searchNodes,
|
|
91
|
+
candidatesExplored: this.searchNodes,
|
|
92
|
+
pruned: this.pruned,
|
|
93
|
+
error: 'No program found that satisfies all examples',
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
if (error instanceof SynthesisTimeoutError) {
|
|
98
|
+
const duration = Date.now() - startTime;
|
|
99
|
+
return {
|
|
100
|
+
success: candidates.length > 0,
|
|
101
|
+
program: candidates[0],
|
|
102
|
+
candidates,
|
|
103
|
+
duration,
|
|
104
|
+
synthesisTime: duration,
|
|
105
|
+
searchNodes: this.searchNodes,
|
|
106
|
+
candidatesExplored: this.searchNodes,
|
|
107
|
+
pruned: this.pruned,
|
|
108
|
+
error: candidates.length > 0 ? undefined : 'Synthesis timed out',
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get candidates from last synthesis
|
|
116
|
+
*/
|
|
117
|
+
getCandidates(spec, dsl, limit) {
|
|
118
|
+
// Simple enumeration-based candidate generation
|
|
119
|
+
const enumerator = new Enumerator(dsl);
|
|
120
|
+
const programs = enumerator.enumerate({ maxDepth: 3, maxPrograms: limit ?? 10 });
|
|
121
|
+
// Filter programs that satisfy at least one example
|
|
122
|
+
return programs.filter((p) => {
|
|
123
|
+
try {
|
|
124
|
+
const firstExample = spec.examples[0];
|
|
125
|
+
if (!firstExample)
|
|
126
|
+
return true;
|
|
127
|
+
const result = dsl.execute(p, firstExample.input);
|
|
128
|
+
return this.valuesEqual(result, firstExample.output);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}).slice(0, limit ?? 10);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Disambiguate candidates with additional example
|
|
137
|
+
*/
|
|
138
|
+
disambiguate(candidates, example) {
|
|
139
|
+
if (!this.lastDsl)
|
|
140
|
+
return candidates;
|
|
141
|
+
return candidates.filter((p) => {
|
|
142
|
+
try {
|
|
143
|
+
const result = this.lastDsl.execute(p, example.input);
|
|
144
|
+
return this.valuesEqual(result, example.output);
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Check if program satisfies all examples
|
|
153
|
+
*/
|
|
154
|
+
satisfiesAllExamples(program, examples, dsl) {
|
|
155
|
+
for (const example of examples) {
|
|
156
|
+
try {
|
|
157
|
+
const result = dsl.execute(program, example.input);
|
|
158
|
+
if (!this.valuesEqual(result, example.output)) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Check value equality
|
|
170
|
+
*/
|
|
171
|
+
valuesEqual(a, b) {
|
|
172
|
+
if (a === b)
|
|
173
|
+
return true;
|
|
174
|
+
if (typeof a !== typeof b)
|
|
175
|
+
return false;
|
|
176
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
177
|
+
if (a.length !== b.length)
|
|
178
|
+
return false;
|
|
179
|
+
return a.every((v, i) => this.valuesEqual(v, b[i]));
|
|
180
|
+
}
|
|
181
|
+
if (typeof a === 'object' && a !== null && b !== null) {
|
|
182
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
183
|
+
}
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
//# sourceMappingURL=PBESynthesizer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PBESynthesizer.js","sourceRoot":"","sources":["../../src/synthesis/PBESynthesizer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAWH,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAEpE;;GAEG;AACH,MAAM,eAAe,GAA+B;IAClD,QAAQ,EAAE,CAAC;IACX,OAAO,EAAE,EAAE;IACX,OAAO,EAAE,KAAK;IACd,aAAa,EAAE,GAAG;IAClB,iBAAiB,EAAE,KAAK;IACxB,cAAc,EAAE,GAAG;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,OAAO,cAAc;IACjB,WAAW,CAAS;IACpB,MAAM,CAAS;IACf,OAAO,CAAc;IAE7B;QACE,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QAChB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,UAAU,CACd,IAAmB,EACnB,GAAS,EACT,OAA0B;QAE1B,MAAM,IAAI,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,OAAO,EAAE,CAAC;QAChD,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAC7B,IAAI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AS,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QACjD,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version Space
|
|
3
|
+
* @module @nahisaho/musubix-synthesis
|
|
4
|
+
* @description Efficient representation of candidate programs
|
|
5
|
+
*/
|
|
6
|
+
import type { Example, IDSL, IVersionSpace, Program } from '../types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Version space implementation
|
|
9
|
+
*/
|
|
10
|
+
export declare class VersionSpace implements IVersionSpace {
|
|
11
|
+
private candidates;
|
|
12
|
+
constructor(_dsl?: IDSL);
|
|
13
|
+
/**
|
|
14
|
+
* Add a program to the version space
|
|
15
|
+
*/
|
|
16
|
+
add(program: Program): void;
|
|
17
|
+
/**
|
|
18
|
+
* Refine version space with example
|
|
19
|
+
*/
|
|
20
|
+
refine(example: Example, dsl: IDSL): IVersionSpace;
|
|
21
|
+
/**
|
|
22
|
+
* Check if version space has converged to single program
|
|
23
|
+
*/
|
|
24
|
+
isConverged(): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Get the single program if converged
|
|
27
|
+
*/
|
|
28
|
+
getProgram(): Program | null;
|
|
29
|
+
/**
|
|
30
|
+
* Get number of candidates
|
|
31
|
+
*/
|
|
32
|
+
size(): number;
|
|
33
|
+
/**
|
|
34
|
+
* Get candidates up to limit
|
|
35
|
+
*/
|
|
36
|
+
getCandidates(limit?: number): Program[];
|
|
37
|
+
/**
|
|
38
|
+
* Check if program satisfies example
|
|
39
|
+
*/
|
|
40
|
+
private satisfiesExample;
|
|
41
|
+
/**
|
|
42
|
+
* Check if programs are equal
|
|
43
|
+
*/
|
|
44
|
+
private programsEqual;
|
|
45
|
+
/**
|
|
46
|
+
* Value equality check
|
|
47
|
+
*/
|
|
48
|
+
private valuesEqual;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=VersionSpace.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VersionSpace.d.ts","sourceRoot":"","sources":["../../src/synthesis/VersionSpace.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEzE;;GAEG;AACH,qBAAa,YAAa,YAAW,aAAa;IAChD,OAAO,CAAC,UAAU,CAAY;gBAElB,IAAI,CAAC,EAAE,IAAI;IAIvB;;OAEG;IACH,GAAG,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAO3B;;OAEG;IACH,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,GAAG,aAAa;IAYlD;;OAEG;IACH,WAAW,IAAI,OAAO;IAItB;;OAEG;IACH,UAAU,IAAI,OAAO,GAAG,IAAI;IAO5B;;OAEG;IACH,IAAI,IAAI,MAAM;IAId;;OAEG;IACH,aAAa,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,EAAE;IAOxC;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAaxB;;OAEG;IACH,OAAO,CAAC,aAAa;IAIrB;;OAEG;IACH,OAAO,CAAC,WAAW;CAYpB"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version Space
|
|
3
|
+
* @module @nahisaho/musubix-synthesis
|
|
4
|
+
* @description Efficient representation of candidate programs
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Version space implementation
|
|
8
|
+
*/
|
|
9
|
+
export class VersionSpace {
|
|
10
|
+
candidates;
|
|
11
|
+
constructor(_dsl) {
|
|
12
|
+
this.candidates = [];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Add a program to the version space
|
|
16
|
+
*/
|
|
17
|
+
add(program) {
|
|
18
|
+
// Avoid duplicates
|
|
19
|
+
if (!this.candidates.some((p) => this.programsEqual(p, program))) {
|
|
20
|
+
this.candidates.push(program);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Refine version space with example
|
|
25
|
+
*/
|
|
26
|
+
refine(example, dsl) {
|
|
27
|
+
const refined = new VersionSpace(dsl);
|
|
28
|
+
for (const candidate of this.candidates) {
|
|
29
|
+
if (this.satisfiesExample(candidate, example, dsl)) {
|
|
30
|
+
refined.add(candidate);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return refined;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Check if version space has converged to single program
|
|
37
|
+
*/
|
|
38
|
+
isConverged() {
|
|
39
|
+
return this.candidates.length === 1;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get the single program if converged
|
|
43
|
+
*/
|
|
44
|
+
getProgram() {
|
|
45
|
+
if (this.candidates.length === 1) {
|
|
46
|
+
return this.candidates[0];
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get number of candidates
|
|
52
|
+
*/
|
|
53
|
+
size() {
|
|
54
|
+
return this.candidates.length;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get candidates up to limit
|
|
58
|
+
*/
|
|
59
|
+
getCandidates(limit) {
|
|
60
|
+
if (limit !== undefined) {
|
|
61
|
+
return this.candidates.slice(0, limit);
|
|
62
|
+
}
|
|
63
|
+
return [...this.candidates];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Check if program satisfies example
|
|
67
|
+
*/
|
|
68
|
+
satisfiesExample(program, example, dsl) {
|
|
69
|
+
try {
|
|
70
|
+
const result = dsl.execute(program, example.input);
|
|
71
|
+
return this.valuesEqual(result, example.output);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Check if programs are equal
|
|
79
|
+
*/
|
|
80
|
+
programsEqual(a, b) {
|
|
81
|
+
return JSON.stringify(a.expression) === JSON.stringify(b.expression);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Value equality check
|
|
85
|
+
*/
|
|
86
|
+
valuesEqual(a, b) {
|
|
87
|
+
if (a === b)
|
|
88
|
+
return true;
|
|
89
|
+
if (typeof a !== typeof b)
|
|
90
|
+
return false;
|
|
91
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
92
|
+
if (a.length !== b.length)
|
|
93
|
+
return false;
|
|
94
|
+
return a.every((v, i) => this.valuesEqual(v, b[i]));
|
|
95
|
+
}
|
|
96
|
+
if (typeof a === 'object' && a !== null && b !== null) {
|
|
97
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
98
|
+
}
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=VersionSpace.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VersionSpace.js","sourceRoot":"","sources":["../../src/synthesis/VersionSpace.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;GAEG;AACH,MAAM,OAAO,YAAY;IACf,UAAU,CAAY;IAE9B,YAAY,IAAW;QACrB,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,OAAgB;QAClB,mBAAmB;QACnB,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC;YACjE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,OAAgB,EAAE,GAAS;QAChC,MAAM,OAAO,GAAG,IAAI,YAAY,CAAC,GAAG,CAAC,CA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C,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;IACvE,CAAC;IAED;;OAEG;IACK,WAAW,CAAC,CAAU,EAAE,CAAU;QACxC,IAAI,CAAC,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QACzB,IAAI,OAAO,CAAC,KAAK,OAAO,CAAC;YAAE,OAAO,KAAK,CAAC;QACxC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;YACzC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;gBAAE,OAAO,KAAK,CAAC;YACxC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACtD,CAAC;QACD,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;YACtD,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QACjD,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Witness Engine
|
|
3
|
+
* @module @nahisaho/musubix-synthesis
|
|
4
|
+
* @description Deductive synthesis with witness functions
|
|
5
|
+
* Traces to: REQ-SYN-002 (Witness Functions)
|
|
6
|
+
*/
|
|
7
|
+
import type { DecomposedSpec, IDSL, IWitnessEngine, Program, Specification, SynthesisOptions, SynthesisResult, WitnessFunction } from '../types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Witness engine implementation
|
|
10
|
+
*/
|
|
11
|
+
export declare class WitnessEngine implements IWitnessEngine {
|
|
12
|
+
private readonly dsl;
|
|
13
|
+
private readonly witnesses;
|
|
14
|
+
constructor(dsl: IDSL);
|
|
15
|
+
/**
|
|
16
|
+
* Register a witness function
|
|
17
|
+
*/
|
|
18
|
+
register(witness: WitnessFunction): void;
|
|
19
|
+
/**
|
|
20
|
+
* Register a witness function (alias for tests)
|
|
21
|
+
*/
|
|
22
|
+
registerWitness(witness: WitnessFunction): void;
|
|
23
|
+
/**
|
|
24
|
+
* Get all witness functions for an operator
|
|
25
|
+
*/
|
|
26
|
+
getWitnesses(operator: string): WitnessFunction[];
|
|
27
|
+
/**
|
|
28
|
+
* Clear all registered witnesses
|
|
29
|
+
*/
|
|
30
|
+
clearWitnesses(): void;
|
|
31
|
+
/**
|
|
32
|
+
* Decompose a specification using witness functions
|
|
33
|
+
*/
|
|
34
|
+
decompose(spec: Specification, operator: string): DecomposedSpec;
|
|
35
|
+
/**
|
|
36
|
+
* Synthesize using witness functions
|
|
37
|
+
*/
|
|
38
|
+
synthesizeWithWitness(spec: Specification, options?: SynthesisOptions): Promise<SynthesisResult>;
|
|
39
|
+
/**
|
|
40
|
+
* Synthesize deductively using witness functions (interface method)
|
|
41
|
+
*/
|
|
42
|
+
synthesizeDeductively(_dsl: IDSL, spec: Specification): Promise<Program | null>;
|
|
43
|
+
/**
|
|
44
|
+
* Internal deductive synthesis with depth limit
|
|
45
|
+
*/
|
|
46
|
+
private synthesizeDeductivelyInternal;
|
|
47
|
+
/**
|
|
48
|
+
* Compute confidence of decomposition
|
|
49
|
+
*/
|
|
50
|
+
private computeConfidence;
|
|
51
|
+
/**
|
|
52
|
+
* Check if input directly satisfies spec
|
|
53
|
+
*/
|
|
54
|
+
private inputSatisfiesSpec;
|
|
55
|
+
/**
|
|
56
|
+
* Check if constant satisfies spec
|
|
57
|
+
*/
|
|
58
|
+
private constantSatisfiesSpec;
|
|
59
|
+
/**
|
|
60
|
+
* Value equality check
|
|
61
|
+
*/
|
|
62
|
+
private valuesEqual;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=WitnessEngine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WitnessEngine.d.ts","sourceRoot":"","sources":["../../src/synthesis/WitnessEngine.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,cAAc,EACd,IAAI,EACJ,cAAc,EACd,OAAO,EACP,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,eAAe,EAChB,MAAM,aAAa,CAAC;AAErB;;GAEG;AACH,qBAAa,aAAc,YAAW,cAAc;IAClD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAO;IAC3B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAiC;gBAE/C,GAAG,EAAE,IAAI;IAKrB;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI;IAIxC;;OAEG;IACH,eAAe,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI;IAM/C;;OAEG;IACH,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,eAAe,EAAE;IAIjD;;OAEG;IACH,cAAc,IAAI,IAAI;IAItB;;OAEG;IACH,SAAS,CAAC,IAAI,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,GAAG,cAAc;IA6BhE;;OAEG;IACG,qBAAqB,CACzB,IAAI,EAAE,aAAa,EACnB,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,eAAe,CAAC;IAkC3B;;OAEG;IACG,qBAAqB,CACzB,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,aAAa,GAClB,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAI1B;;OAEG;YACW,6BAA6B;IAoE3C;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAazB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAS1B;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAS7B;;OAEG;IACH,OAAO,CAAC,WAAW;CAYpB"}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Witness Engine
|
|
3
|
+
* @module @nahisaho/musubix-synthesis
|
|
4
|
+
* @description Deductive synthesis with witness functions
|
|
5
|
+
* Traces to: REQ-SYN-002 (Witness Functions)
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Witness engine implementation
|
|
9
|
+
*/
|
|
10
|
+
export class WitnessEngine {
|
|
11
|
+
dsl;
|
|
12
|
+
witnesses;
|
|
13
|
+
constructor(dsl) {
|
|
14
|
+
this.dsl = dsl;
|
|
15
|
+
this.witnesses = new Map();
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Register a witness function
|
|
19
|
+
*/
|
|
20
|
+
register(witness) {
|
|
21
|
+
this.registerWitness(witness);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Register a witness function (alias for tests)
|
|
25
|
+
*/
|
|
26
|
+
registerWitness(witness) {
|
|
27
|
+
const existing = this.witnesses.get(witness.operator) ?? [];
|
|
28
|
+
existing.push(witness);
|
|
29
|
+
this.witnesses.set(witness.operator, existing);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Get all witness functions for an operator
|
|
33
|
+
*/
|
|
34
|
+
getWitnesses(operator) {
|
|
35
|
+
return this.witnesses.get(operator) ?? [];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Clear all registered witnesses
|
|
39
|
+
*/
|
|
40
|
+
clearWitnesses() {
|
|
41
|
+
this.witnesses.clear();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Decompose a specification using witness functions
|
|
45
|
+
*/
|
|
46
|
+
decompose(spec, operator) {
|
|
47
|
+
const witnesses = this.witnesses.get(operator);
|
|
48
|
+
if (!witnesses || witnesses.length === 0) {
|
|
49
|
+
return {
|
|
50
|
+
operator,
|
|
51
|
+
argSpecs: [],
|
|
52
|
+
confidence: 0,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const argSpecs = [];
|
|
56
|
+
// Apply each witness function
|
|
57
|
+
for (const witness of witnesses) {
|
|
58
|
+
const witnessFunc = witness.witness ?? witness.inverse;
|
|
59
|
+
if (witnessFunc) {
|
|
60
|
+
const specs = witnessFunc(spec);
|
|
61
|
+
argSpecs.push(...specs);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
operator,
|
|
66
|
+
argSpecs,
|
|
67
|
+
confidence: this.computeConfidence(argSpecs, spec),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Synthesize using witness functions
|
|
72
|
+
*/
|
|
73
|
+
async synthesizeWithWitness(spec, options) {
|
|
74
|
+
const startTime = Date.now();
|
|
75
|
+
const maxDepth = options?.maxDepth ?? 3;
|
|
76
|
+
let searchNodes = 0;
|
|
77
|
+
// Try to synthesize using deductive approach
|
|
78
|
+
const program = await this.synthesizeDeductivelyInternal(spec, maxDepth, () => {
|
|
79
|
+
searchNodes++;
|
|
80
|
+
});
|
|
81
|
+
const duration = Date.now() - startTime;
|
|
82
|
+
if (program) {
|
|
83
|
+
return {
|
|
84
|
+
success: true,
|
|
85
|
+
program,
|
|
86
|
+
duration,
|
|
87
|
+
synthesisTime: duration,
|
|
88
|
+
searchNodes,
|
|
89
|
+
candidatesExplored: searchNodes,
|
|
90
|
+
pruned: 0,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
success: false,
|
|
95
|
+
duration,
|
|
96
|
+
synthesisTime: duration,
|
|
97
|
+
searchNodes,
|
|
98
|
+
candidatesExplored: searchNodes,
|
|
99
|
+
pruned: 0,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Synthesize deductively using witness functions (interface method)
|
|
104
|
+
*/
|
|
105
|
+
async synthesizeDeductively(_dsl, spec) {
|
|
106
|
+
return this.synthesizeDeductivelyInternal(spec, 3, () => { });
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Internal deductive synthesis with depth limit
|
|
110
|
+
*/
|
|
111
|
+
async synthesizeDeductivelyInternal(spec, maxDepth, onNode) {
|
|
112
|
+
onNode();
|
|
113
|
+
if (maxDepth <= 0) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
// Base case: check if constant satisfies spec
|
|
117
|
+
for (const [, constant] of this.dsl.constants) {
|
|
118
|
+
if (this.constantSatisfiesSpec(constant.value, spec)) {
|
|
119
|
+
return {
|
|
120
|
+
id: `const-${Date.now()}`,
|
|
121
|
+
expression: { kind: 'constant', name: constant.name },
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// Check if input directly satisfies spec
|
|
126
|
+
if (this.inputSatisfiesSpec(spec)) {
|
|
127
|
+
return {
|
|
128
|
+
id: `input-${Date.now()}`,
|
|
129
|
+
expression: { kind: 'variable', name: 'input' },
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
// Try each operator with witnesses
|
|
133
|
+
for (const [, operator] of this.dsl.operators) {
|
|
134
|
+
const decomp = this.decompose(spec, operator.name);
|
|
135
|
+
if (decomp.argSpecs.length > 0) {
|
|
136
|
+
// Try to synthesize each argument
|
|
137
|
+
const argPrograms = [];
|
|
138
|
+
let allSolved = true;
|
|
139
|
+
for (const argSpec of decomp.argSpecs) {
|
|
140
|
+
const argProgram = await this.synthesizeDeductivelyInternal(argSpec, maxDepth - 1, onNode);
|
|
141
|
+
if (argProgram) {
|
|
142
|
+
argPrograms.push(argProgram);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
allSolved = false;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (allSolved && argPrograms.length === operator.inputTypes.length) {
|
|
150
|
+
return {
|
|
151
|
+
id: `deductive-${Date.now()}`,
|
|
152
|
+
expression: {
|
|
153
|
+
kind: 'application',
|
|
154
|
+
operator: operator.name,
|
|
155
|
+
args: argPrograms.map((p) => p.expression),
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Compute confidence of decomposition
|
|
165
|
+
*/
|
|
166
|
+
computeConfidence(argSpecs, _outputSpec) {
|
|
167
|
+
if (argSpecs.length === 0)
|
|
168
|
+
return 0;
|
|
169
|
+
// Higher confidence for more constrained specs
|
|
170
|
+
let confidence = 1.0;
|
|
171
|
+
for (const spec of argSpecs) {
|
|
172
|
+
confidence *= 1 / (1 + spec.examples.length * 0.1);
|
|
173
|
+
}
|
|
174
|
+
return confidence;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Check if input directly satisfies spec
|
|
178
|
+
*/
|
|
179
|
+
inputSatisfiesSpec(spec) {
|
|
180
|
+
for (const example of spec.examples) {
|
|
181
|
+
if (!this.valuesEqual(example.input, example.output)) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Check if constant satisfies spec
|
|
189
|
+
*/
|
|
190
|
+
constantSatisfiesSpec(value, spec) {
|
|
191
|
+
for (const example of spec.examples) {
|
|
192
|
+
if (!this.valuesEqual(value, example.output)) {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Value equality check
|
|
200
|
+
*/
|
|
201
|
+
valuesEqual(a, b) {
|
|
202
|
+
if (a === b)
|
|
203
|
+
return true;
|
|
204
|
+
if (typeof a !== typeof b)
|
|
205
|
+
return false;
|
|
206
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
207
|
+
if (a.length !== b.length)
|
|
208
|
+
return false;
|
|
209
|
+
return a.every((v, i) => this.valuesEqual(v, b[i]));
|
|
210
|
+
}
|
|
211
|
+
if (typeof a === 'object' && a !== null && b !== null) {
|
|
212
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
213
|
+
}
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
//# sourceMappingURL=WitnessEngine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WitnessEngine.js","sourceRoot":"","sources":["../../src/synthesis/WitnessEngine.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAaH;;GAEG;AACH,MAAM,OAAO,aAAa;IACP,GAAG,CAAO;IACV,SAAS,CAAiC;IAE3D,YAAY,GAAS;QACnB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,SAAS,GAAG,IAAI,GAAG,EAAE,CAAC;IAC7B,CAAC;IAED;;OAEG;IACH,QAAQ,CAAC,OAAwB;QAC/B,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC;IAED;;OAEG;IACH,eAAe,CAAC,OAAwB;QACtC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;QAC5D,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACvB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IACjD,CAAC;IAED;;OAEG;IACH,YAAY,CAAC,QAAgB;QAC3B,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;IAC5C,CAAC;IAED;;OAEG;IACH,cAAc;QACZ,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,SAAS,CAAC,IAAmB,EAAE,QAAgB;QAC7C,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAA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,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACtD,CAAC;QACD,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;YACtD,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QACjD,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synthesis Module Exports
|
|
3
|
+
* @module @nahisaho/musubix-synthesis/synthesis
|
|
4
|
+
*/
|
|
5
|
+
export { Enumerator, resetProgramIdCounter } from './Enumerator.js';
|
|
6
|
+
export { PBESynthesizer } from './PBESynthesizer.js';
|
|
7
|
+
export { WitnessEngine } from './WitnessEngine.js';
|
|
8
|
+
export { VersionSpace } from './VersionSpace.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/synthesis/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synthesis Module Exports
|
|
3
|
+
* @module @nahisaho/musubix-synthesis/synthesis
|
|
4
|
+
*/
|
|
5
|
+
export { Enumerator, resetProgramIdCounter } from './Enumerator.js';
|
|
6
|
+
export { PBESynthesizer } from './PBESynthesizer.js';
|
|
7
|
+
export { WitnessEngine } from './WitnessEngine.js';
|
|
8
|
+
export { VersionSpace } from './VersionSpace.js';
|
|
9
|
+
//# sourceMappingURL=index.js.map
|