-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgebraSearch.ts
More file actions
203 lines (168 loc) · 5.21 KB
/
Copy pathalgebraSearch.ts
File metadata and controls
203 lines (168 loc) · 5.21 KB
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
// algebraSearch.ts
// Event 017: Algebra Synthesis from Properties
// Search existing algebras by specification
import type { ClassifiedAlgebra } from '../meta/algebraClassifier.js';
import { classifyAlgebra } from '../meta/algebraClassifier.js';
import { algebras } from '../domains/fold.js';
import type { AlgebraSpec } from './algebraSpec.js';
/**
* Known algebras database
* These are pre-classified and ready to match against specs
*/
export const knownAlgebras: Map<string, ClassifiedAlgebra<any, any>> = new Map();
/**
* Initialize known algebras database
*/
export function initializeKnownAlgebras(): void {
// Numeric algebras
knownAlgebras.set('sum', classifyAlgebra('sum', algebras.sum, {
identityCandidates: [0],
numSamples: 100,
}));
knownAlgebras.set('product', classifyAlgebra('product', algebras.product, {
identityCandidates: [1],
numSamples: 100,
}));
knownAlgebras.set('max', classifyAlgebra('max', algebras.max, {
identityCandidates: [-Infinity],
numSamples: 100,
}));
knownAlgebras.set('min', classifyAlgebra('min', algebras.min, {
identityCandidates: [Infinity],
numSamples: 100,
}));
knownAlgebras.set('count', classifyAlgebra('count', algebras.count, {
identityCandidates: [0],
numSamples: 100,
}));
// String algebras
knownAlgebras.set('concat', classifyAlgebra('concat', algebras.concat, {
identityCandidates: [''],
numSamples: 100,
}));
// Array algebras
// Note: collect skipped for now (property detection doesn't handle array accumulators well)
}
/**
* Search for an existing algebra matching the specification
*
* @returns ClassifiedAlgebra if found, null otherwise
*/
export function searchExisting(spec: AlgebraSpec): ClassifiedAlgebra<any, any> | null {
// Initialize if not done
if (knownAlgebras.size === 0) {
initializeKnownAlgebras();
}
// Search through known algebras
for (const [name, algebra] of knownAlgebras.entries()) {
if (matchesSpec(algebra, spec)) {
return algebra;
}
}
return null;
}
/**
* Check if a classified algebra matches a specification
*/
function matchesSpec(algebra: ClassifiedAlgebra<any, any>, spec: AlgebraSpec): boolean {
// 1. Class must match or be stronger
if (!classMatches(algebra.class, spec.class)) {
return false;
}
// 2. Identity must match (if specified)
if (spec.identity !== undefined && algebra.properties.identity !== spec.identity) {
return false;
}
// 3. Constraints must be satisfied (if specified)
if (spec.constraints) {
if (spec.constraints.associative !== undefined &&
algebra.properties.associative !== spec.constraints.associative) {
return false;
}
if (spec.constraints.commutative !== undefined &&
algebra.properties.commutative !== spec.constraints.commutative) {
return false;
}
if (spec.constraints.idempotent !== undefined &&
algebra.properties.idempotent !== spec.constraints.idempotent) {
return false;
}
if (spec.constraints.hasInverse !== undefined &&
algebra.properties.hasInverse !== spec.constraints.hasInverse) {
return false;
}
}
// 4. Semantic hint (if provided, helps narrow down)
if (spec.semantics) {
return matchesSemantic(algebra.name, spec.semantics, spec.identity);
}
return true;
}
/**
* Check if algebra class matches or exceeds required class
*/
function classMatches(actual: string, required: string): boolean {
// Exact match
if (actual === required) {
return true;
}
// Allow stronger classes
// For example, CommutativeMonoid satisfies Monoid requirement
const hierarchy: Record<string, number> = {
'Magma': 0,
'Semigroup': 1,
'Monoid': 2,
'CommutativeMonoid': 3,
'IdempotentMonoid': 2.5,
'IdempotentCommutativeMonoid': 3.5,
'Group': 4,
'AbelianGroup': 5,
};
return (hierarchy[actual] || 0) >= (hierarchy[required] || 0);
}
/**
* Check if algebra name matches semantic hint
*/
function matchesSemantic(
name: string,
semantics: string,
identity: unknown
): boolean {
switch (semantics) {
case 'additive':
return name === 'sum' && identity === 0;
case 'multiplicative':
return name === 'product' && identity === 1;
case 'extremal':
return (name === 'max' && identity === -Infinity) ||
(name === 'min' && identity === Infinity);
case 'concatenative':
return (name === 'concat' && identity === '') ||
(name === 'collect' && JSON.stringify(identity) === '[]');
default:
return true; // Custom semantics, accept any
}
}
/**
* Get all algebras matching a partial spec (for exploration)
*/
export function searchAll(partialSpec: Partial<AlgebraSpec>): ClassifiedAlgebra<any, any>[] {
if (knownAlgebras.size === 0) {
initializeKnownAlgebras();
}
const results: ClassifiedAlgebra<any, any>[] = [];
for (const algebra of knownAlgebras.values()) {
let matches = true;
if (partialSpec.class && algebra.class !== partialSpec.class) {
matches = false;
}
if (partialSpec.identity !== undefined &&
algebra.properties.identity !== partialSpec.identity) {
matches = false;
}
if (matches) {
results.push(algebra);
}
}
return results;
}