-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgebraTemplates.ts
More file actions
153 lines (129 loc) · 3.92 KB
/
algebraTemplates.ts
File metadata and controls
153 lines (129 loc) · 3.92 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
// algebraTemplates.ts
// Event 017: Algebra Synthesis from Properties
// Ontologically safe templates for algebra generation
import type { Algebra } from '../evolution/operators.js';
import type { AlgebraSpec, AlgebraSemantics } from './algebraSpec.js';
/**
* Generate algebra from template based on semantic category
*
* @returns Algebra function or null if cannot generate
*/
export function generateFromTemplate(spec: AlgebraSpec): Algebra<any, any> | null {
if (!spec.semantics) {
return null; // Cannot generate without semantic hint
}
switch (spec.semantics) {
case 'additive':
return generateAdditive(spec);
case 'multiplicative':
return generateMultiplicative(spec);
case 'extremal':
return generateExtremal(spec);
case 'concatenative':
return generateConcatenative(spec);
default:
return null; // Custom semantics not supported
}
}
/**
* Generate additive algebra (sum)
*/
function generateAdditive(spec: AlgebraSpec): Algebra<any, any> | null {
if (spec.valueType !== 'number') {
return null; // Additive only for numbers
}
if (spec.identity !== undefined && spec.identity !== 0) {
return null; // Additive identity must be 0
}
return (acc: number, val: number) => acc + val;
}
/**
* Generate multiplicative algebra (product)
*/
function generateMultiplicative(spec: AlgebraSpec): Algebra<any, any> | null {
if (spec.valueType !== 'number') {
return null; // Multiplicative only for numbers
}
if (spec.identity !== undefined && spec.identity !== 1) {
return null; // Multiplicative identity must be 1
}
return (acc: number, val: number) => acc * val;
}
/**
* Generate extremal algebra (max/min)
*/
function generateExtremal(spec: AlgebraSpec): Algebra<any, any> | null {
if (spec.valueType !== 'number') {
return null; // Extremal only for numbers
}
if (spec.identity === -Infinity) {
// Max algebra
return (acc: number, val: number) => Math.max(acc, val);
} else if (spec.identity === Infinity) {
// Min algebra
return (acc: number, val: number) => Math.min(acc, val);
}
return null; // Extremal requires -Infinity (max) or Infinity (min)
}
/**
* Generate concatenative algebra (string/array concat)
*/
function generateConcatenative(spec: AlgebraSpec): Algebra<any, any> | null {
if (spec.valueType === 'string') {
if (spec.identity !== undefined && spec.identity !== '') {
return null; // String concat identity must be ''
}
return (acc: string, val: string) => acc + val;
}
if (spec.valueType === 'array') {
if (spec.identity !== undefined && JSON.stringify(spec.identity) !== '[]') {
return null; // Array concat identity must be []
}
return (acc: any[], val: any) => [...acc, val];
}
return null; // Concatenative only for string/array
}
/**
* Get a human-readable name for a generated algebra
*/
export function getGeneratedName(spec: AlgebraSpec): string {
if (spec.name) {
return spec.name;
}
if (spec.semantics) {
switch (spec.semantics) {
case 'additive':
return 'sum';
case 'multiplicative':
return 'product';
case 'extremal':
return spec.identity === -Infinity ? 'max' : 'min';
case 'concatenative':
return spec.valueType === 'string' ? 'concat' : 'collect';
default:
return 'generated';
}
}
return `${spec.class}_${spec.valueType}`;
}
/**
* Validate that a template can be generated for a spec
*/
export function canGenerate(spec: AlgebraSpec): boolean {
return generateFromTemplate(spec) !== null;
}
/**
* Get all possible templates for a value type
*/
export function getAvailableTemplates(valueType: string): AlgebraSemantics[] {
switch (valueType) {
case 'number':
return ['additive', 'multiplicative', 'extremal'];
case 'string':
return ['concatenative'];
case 'array':
return ['concatenative'];
default:
return [];
}
}