-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgebraClassifier.ts
More file actions
245 lines (215 loc) · 8.34 KB
/
algebraClassifier.ts
File metadata and controls
245 lines (215 loc) · 8.34 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// algebraClassifier.ts
// Event 016: Meta-Algebra Analysis
// Classification of algebraic structures into ontological hierarchy
import type { Algebra } from '../evolution/operators.js';
import type { AlgebraProperties } from './algebraProperties.js';
import { detectProperties } from './algebraProperties.js';
// Re-export for external use
export type { AlgebraProperties } from './algebraProperties.js';
/**
* Ontological hierarchy of algebraic structures
*
* Magma → Semigroup → Monoid → CommutativeMonoid → Group → AbelianGroup
* ↓
* IdempotentMonoid
*/
export type AlgebraClass =
| 'Magma' // Just a binary operation (no guarantees)
| 'Semigroup' // Associative
| 'Monoid' // Associative + Identity
| 'CommutativeMonoid' // Associative + Identity + Commutative
| 'IdempotentMonoid' // Monoid + Idempotent
| 'IdempotentCommutativeMonoid' // CommutativeMonoid + Idempotent
| 'Group' // Monoid + Inverse
| 'AbelianGroup'; // Group + Commutative
/**
* Implications derived from algebra class
*/
export interface AlgebraImplications {
parallelizable: boolean; // Can use MapReduce (requires commutative + associative)
foldable: boolean; // Can fold in any direction (always true for algebras)
safeForUnordered: boolean; // Result independent of order (requires commutative)
safeForDuplicates: boolean; // Duplicates don't change result (requires idempotent)
hasIdentity: boolean; // Has identity element (required for empty fold)
invertible: boolean; // Operations can be reversed (requires inverse)
}
/**
* Complete algebra metadata
*/
export interface ClassifiedAlgebra<A, B> {
name: string;
fn: Algebra<A, B>;
properties: AlgebraProperties;
class: AlgebraClass;
implications: AlgebraImplications;
}
/**
* Classify algebra into ontological hierarchy
*
* Decision tree:
* 1. If not associative → Magma
* 2. If associative but no identity → Semigroup
* 3. If has identity but not commutative, no inverse → Monoid
* 4. If commutative but no inverse → CommutativeMonoid (+ Idempotent variant)
* 5. If has inverse but not commutative → Group
* 6. If has inverse and commutative → AbelianGroup
*/
export function classify(properties: AlgebraProperties): AlgebraClass {
// Level 1: Check associativity
if (!properties.associative) {
return 'Magma';
}
// Level 2: Check identity
if (properties.identity === null) {
return 'Semigroup';
}
// Level 3: Check inverse
if (properties.hasInverse) {
if (properties.commutative) {
return 'AbelianGroup';
} else {
return 'Group';
}
}
// Level 4: No inverse, so Monoid or its variants
if (!properties.commutative) {
return 'Monoid';
}
// Level 5: Commutative monoid variants
if (properties.idempotent) {
return 'IdempotentCommutativeMonoid';
}
return 'CommutativeMonoid';
}
/**
* Derive implications from algebra class
*/
export function deriveImplications(
algebraClass: AlgebraClass,
properties: AlgebraProperties
): AlgebraImplications {
const hasIdentity = properties.identity !== null;
const isCommutative = properties.commutative;
const isAssociative = properties.associative;
const isIdempotent = properties.idempotent;
const hasInverse = properties.hasInverse;
return {
// Parallelizable requires both commutative AND associative
parallelizable: isCommutative && isAssociative,
// All algebras can be folded (even non-associative ones, just order-dependent)
foldable: true,
// Safe for unordered data requires commutativity
safeForUnordered: isCommutative,
// Safe for duplicates requires idempotence
safeForDuplicates: isIdempotent,
// Has identity element
hasIdentity,
// Invertible operations
invertible: hasInverse,
};
}
/**
* Classify an algebra and derive all metadata
*/
export function classifyAlgebra<A, B>(
name: string,
algebra: Algebra<A, B>,
config?: {
identityCandidates?: unknown[];
numSamples?: number;
}
): ClassifiedAlgebra<A, B> {
const properties = detectProperties(algebra, config);
const algebraClass = classify(properties);
const implications = deriveImplications(algebraClass, properties);
return {
name,
fn: algebra,
properties,
class: algebraClass,
implications,
};
}
/**
* Check if an algebra belongs to a specific class or higher
*
* Hierarchy levels (higher = more structure):
* Magma (0) < Semigroup (1) < Monoid (2) < CommutativeMonoid (3) < Group (4) < AbelianGroup (5)
*/
export function isAtLeast(actual: AlgebraClass, required: AlgebraClass): boolean {
const hierarchy: Record<AlgebraClass, number> = {
'Magma': 0,
'Semigroup': 1,
'Monoid': 2,
'CommutativeMonoid': 3,
'IdempotentMonoid': 2.5, // Branch from Monoid
'IdempotentCommutativeMonoid': 3.5, // Branch from CommutativeMonoid
'Group': 4,
'AbelianGroup': 5,
};
return hierarchy[actual] >= hierarchy[required];
}
/**
* Generate human-readable report for classified algebra
*/
export function generateReport<A, B>(classified: ClassifiedAlgebra<A, B>): string {
const { name, properties, class: algebraClass, implications } = classified;
const propertyList = [
properties.associative && 'Associative',
properties.commutative && 'Commutative',
properties.identity !== null && `Identity: ${properties.identity}`,
properties.idempotent && 'Idempotent',
properties.hasInverse && 'Invertible',
].filter(Boolean).join(', ');
const implicationList = [
implications.parallelizable && 'Parallelizable',
implications.safeForUnordered && 'Order-independent',
implications.safeForDuplicates && 'Idempotent (safe for duplicates)',
implications.hasIdentity && 'Has identity (safe for empty)',
implications.invertible && 'Invertible',
].filter(Boolean).join(', ');
return `
╔═══════════════════════════════════════════════════════════════════╗
║ Algebra: ${name.padEnd(58)} ║
╠═══════════════════════════════════════════════════════════════════╣
║ Class: ${algebraClass.padEnd(60)} ║
╠═══════════════════════════════════════════════════════════════════╣
║ Properties: ║
║ ${propertyList.padEnd(64)} ║
╠═══════════════════════════════════════════════════════════════════╣
║ Implications: ║
║ ${implicationList.padEnd(64)} ║
╚═══════════════════════════════════════════════════════════════════╝
`.trim();
}
/**
* Compare two algebras for equivalence
*
* Two algebras are equivalent if they have the same properties
* (not the same implementation, but the same mathematical structure)
*/
export function algebrasEquivalent(
props1: AlgebraProperties,
props2: AlgebraProperties
): boolean {
return (
props1.associative === props2.associative &&
props1.commutative === props2.commutative &&
props1.identity === props2.identity &&
props1.idempotent === props2.idempotent &&
props1.hasInverse === props2.hasInverse
);
}
/**
* Get the ontological "distance" between two algebra classes
* (number of property differences)
*/
export function algebraDistance(props1: AlgebraProperties, props2: AlgebraProperties): number {
let distance = 0;
if (props1.associative !== props2.associative) distance++;
if (props1.commutative !== props2.commutative) distance++;
if ((props1.identity !== null) !== (props2.identity !== null)) distance++;
if (props1.idempotent !== props2.idempotent) distance++;
if (props1.hasInverse !== props2.hasInverse) distance++;
return distance;
}