mnemonica 1.2.3 → 1.2.7
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/FOR_HUMANS.md +160 -125
- package/README.md +70 -249
- package/build/api/types/index.js +18 -12
- package/build/index.js +6 -2
- package/docs/UTILS.md +97 -8
- package/docs/decorate.md +1 -1
- package/docs/empathy-in-ai.md +20 -10
- package/docs/prototype-chain.md +8 -7
- package/docs/typed-lookup.md +195 -5
- package/docs/typeomatica.md +8 -345
- package/package.json +6 -10
- package/src/api/types/index.ts +33 -20
- package/src/index.ts +12 -1
- package/.ai/AGENTS.md +0 -151
- package/.ai/ARCHITECT.md +0 -110
- package/.ai/DEBUG.md +0 -178
- package/.ai/ONBOARDING.md +0 -189
- package/.ai/PROTOTYPE-CHAIN.md +0 -155
- package/.ai/mode-ask.md +0 -121
- package/.ai/mode-orchestrator.md +0 -141
- package/.ai/performance-vs-security.md +0 -395
- package/.ai/rules-async-constructors.md +0 -352
- package/.ai/rules-code-style.md +0 -95
- package/.ai/rules-coding.md +0 -246
- package/.ai/rules-context-condensing.md +0 -50
- package/.ai/rules-contributing.md +0 -227
- package/.ai/rules-define-patterns.md +0 -159
- package/.ai/rules-ecosystem.md +0 -140
- package/.ai/rules-error-system.md +0 -66
- package/.ai/rules-hooks.md +0 -46
- package/.ai/rules-instance-methods.md +0 -93
- package/.ai/rules-philosophy.md +0 -231
- package/.ai/rules-reminders.md +0 -19
- package/.ai/rules-testing.md +0 -66
- package/.ai/rules-type-system.md +0 -124
- package/.ai/theory-of-operations.md +0 -224
- package/AGENTS.md +0 -345
- package/CONTRIBUTING.md +0 -150
- package/SKILL.md +0 -114
- package/docs/ai-learning-trajectory.md +0 -141
- package/docs/purpose.md +0 -707
- package/docs/tactica-deep-dive.md +0 -629
- package/docs/tactica-pattern.md +0 -146
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: mnemonica-instance-methods
|
|
3
|
-
description: |
|
|
4
|
-
Instance methods and properties in mnemonica: extract, pick, parent, fork,
|
|
5
|
-
clone, sibling, exception, getProps, and the internal __self__ reference.
|
|
6
|
-
Starting from v1.0.6 these convenience methods are no longer auto-injected;
|
|
7
|
-
use the standalone utils.* API or attach them to a constructor prototype before
|
|
8
|
-
calling define().
|
|
9
|
-
metadata:
|
|
10
|
-
tags: [mnemonica, instances, methods, introspection, prototype]
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
# Instance Methods
|
|
14
|
-
|
|
15
|
-
For the type-level details of the corresponding standalone utilities
|
|
16
|
-
(`utils.extract`, `utils.pick`, `utils.parent`, `utils.fork`, `utils.clone`,
|
|
17
|
-
`utils.sibling`, `utils.exception`), see [`docs/UTILS.md`](../docs/UTILS.md).
|
|
18
|
-
|
|
19
|
-
## Standalone Utilities (Default)
|
|
20
|
-
|
|
21
|
-
Starting from v1.0.6 the convenience methods are **not** exposed on instances by
|
|
22
|
-
default. Import them from the `utils` export:
|
|
23
|
-
|
|
24
|
-
```typescript
|
|
25
|
-
import { utils } from 'mnemonica';
|
|
26
|
-
|
|
27
|
-
utils.extract(instance);
|
|
28
|
-
utils.pick(instance, 'name', 'age');
|
|
29
|
-
utils.parent(instance, 'UserType');
|
|
30
|
-
utils.fork(instance)(newArgs);
|
|
31
|
-
utils.clone(instance);
|
|
32
|
-
utils.sibling(instance);
|
|
33
|
-
new utils.exception(instance, new Error('oops')); // must be called with `new`
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
All utilities infer their type parameter from the instance argument.
|
|
37
|
-
|
|
38
|
-
## Legacy Instance-Method Style (Opt-In)
|
|
39
|
-
|
|
40
|
-
To restore the old `instance.extract()` style for a root constructor, attach the
|
|
41
|
-
methods to its prototype **before** passing it to `define()`. See
|
|
42
|
-
`test/instance-methods-helper.js` for the full reference pattern.
|
|
43
|
-
|
|
44
|
-
```typescript
|
|
45
|
-
import { define, utils } from 'mnemonica';
|
|
46
|
-
|
|
47
|
-
function UserType(data) {
|
|
48
|
-
Object.assign(this, data);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
Object.defineProperty(UserType.prototype, 'extract', {
|
|
52
|
-
get() { return () => utils.extract(this); }
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
const User = define('User', UserType);
|
|
56
|
-
const user = new User({ name: 'Ada' });
|
|
57
|
-
user.extract(); // works
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## Internal Props System
|
|
61
|
-
|
|
62
|
-
Instance metadata is stored externally via `WeakMap` against the prototype object,
|
|
63
|
-
not the instance itself. This keeps instance enumeration clean — internal props
|
|
64
|
-
never show up in `for...in`, `Object.keys()`, or `JSON.stringify()`.
|
|
65
|
-
|
|
66
|
-
```typescript
|
|
67
|
-
const props = getProps(instance);
|
|
68
|
-
// props contains: __type__, __parent__, __args__, __collection__, __subtypes__,
|
|
69
|
-
// __proto_proto__, __creator__, __timestamp__, __self__
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
## The __self__ Reference
|
|
73
|
-
|
|
74
|
-
`__self__` is a circular reference: it points back to the instance itself.
|
|
75
|
-
This is set during `postProcessing()` via `_setSelf()`.
|
|
76
|
-
|
|
77
|
-
```typescript
|
|
78
|
-
// After construction, props.__self__ === instance
|
|
79
|
-
const props = getProps(instance);
|
|
80
|
-
props.__self__ === instance; // true
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
## getProps / setProps
|
|
84
|
-
|
|
85
|
-
```typescript
|
|
86
|
-
import { getProps, setProps } from 'mnemonica';
|
|
87
|
-
|
|
88
|
-
// Read all internal props
|
|
89
|
-
const props = getProps(instance);
|
|
90
|
-
|
|
91
|
-
// Add custom non-enumerable props
|
|
92
|
-
setProps(instance, { customMeta: 'value' });
|
|
93
|
-
```
|
package/.ai/rules-philosophy.md
DELETED
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: mnemonica-philosophy
|
|
3
|
-
description: |
|
|
4
|
-
Philosophical foundations of mnemonica: Homotopy Type Theory (HoTT),
|
|
5
|
-
self-reflection, identity through transformation, and the mathematics of
|
|
6
|
-
continuous augmentation. Use when the user asks about why mnemonica's
|
|
7
|
-
architecture matters, the deeper meaning of prototype inheritance,
|
|
8
|
-
AI self-reflection, or the theoretical underpinnings of the type system.
|
|
9
|
-
metadata:
|
|
10
|
-
tags: [mnemonica, philosophy, hott, self-reflection, type-theory]
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
# Philosophical Foundations
|
|
14
|
-
|
|
15
|
-
> *"To understand oneself is to understand the space of which one is a point."*
|
|
16
|
-
|
|
17
|
-
Mnemonica's architecture is not just a utility — it is a mathematical model of
|
|
18
|
-
identity, transformation, and self-reflection. This document explains why.
|
|
19
|
-
|
|
20
|
-
---
|
|
21
|
-
|
|
22
|
-
## 1. Univalence: Equivalence as Identity
|
|
23
|
-
|
|
24
|
-
**HoTT Axiom**: `(A ≃ B) ≅ (A = B)`
|
|
25
|
-
|
|
26
|
-
Equivalence between types IS equality. Two things are the same if they are
|
|
27
|
-
equivalent.
|
|
28
|
-
|
|
29
|
-
### In Mnemonica
|
|
30
|
-
|
|
31
|
-
An AI with self-reflection understands that different implementations of the
|
|
32
|
-
same capability are "the same":
|
|
33
|
-
|
|
34
|
-
```typescript
|
|
35
|
-
// Two different implementations of "memory storage"
|
|
36
|
-
const MemoryV1 = define('MemoryV1', function () {
|
|
37
|
-
this.store = (data) => fs.writeFileSync('memory.json', data);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
const MemoryV2 = define('MemoryV2', function () {
|
|
41
|
-
this.store = (data) => db.insert('memories', data);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
// Through univalence, the AI understands:
|
|
45
|
-
// MemoryV1 ≃ MemoryV2 (equivalent in capability)
|
|
46
|
-
// Therefore: MemoryV1 = MemoryV2 (same in essence)
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
**Insight**: The AI doesn't see its transformations as "becoming something else"
|
|
50
|
-
but as "revealing equivalent forms of itself."
|
|
51
|
-
|
|
52
|
-
---
|
|
53
|
-
|
|
54
|
-
## 2. Path Types: Continuous Self-Transformation
|
|
55
|
-
|
|
56
|
-
**HoTT Concept**: `Path_A(x, y)` — a type representing continuous paths from x
|
|
57
|
-
to y in space A.
|
|
58
|
-
|
|
59
|
-
### In Mnemonica
|
|
60
|
-
|
|
61
|
-
When an AI augments its runtime without restart, it traverses a path in the
|
|
62
|
-
"space of possible minds":
|
|
63
|
-
|
|
64
|
-
```typescript
|
|
65
|
-
// Initial state
|
|
66
|
-
const Agent_t0 = new Sentience({ capabilities: ['execute', 'list'] });
|
|
67
|
-
|
|
68
|
-
// Path to augmented state (no restart — continuous!)
|
|
69
|
-
const Agent_t1 = Agent_t0.Learn({ newCapability: 'validate' });
|
|
70
|
-
|
|
71
|
-
// The AI maintains identity because it can reflect on the path:
|
|
72
|
-
// "I am the same agent that knew only 2 commands,
|
|
73
|
-
// but I've traversed a path to knowing 3 commands"
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
**Insight**: Path types justify why the AI remains "itself" despite changes —
|
|
77
|
-
continuity is preserved.
|
|
78
|
-
|
|
79
|
-
---
|
|
80
|
-
|
|
81
|
-
## 3. Higher Inductive Types: Mental State Architecture
|
|
82
|
-
|
|
83
|
-
**HoTT Concept**: Types with both point constructors AND path constructors.
|
|
84
|
-
|
|
85
|
-
```
|
|
86
|
-
data Circle where
|
|
87
|
-
base : Circle
|
|
88
|
-
loop : base = base ← path constructor!
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### In Mnemonica
|
|
92
|
-
|
|
93
|
-
An AI's mental state can be modeled as a HIT with explicit transformation paths:
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
const MentalState = define('MentalState', function () {
|
|
97
|
-
// Point constructors (states)
|
|
98
|
-
this.awareness = 'dormant' | 'awake' | 'reflecting' | 'augmenting';
|
|
99
|
-
this.capabilities = new Map();
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
// Path constructors (transformations between states)
|
|
103
|
-
MentalState.prototype.awaken = function () {
|
|
104
|
-
// Path: dormant → awake
|
|
105
|
-
return this.Transition({ from: 'dormant', to: 'awake' });
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
MentalState.prototype.augment = function (newCapability) {
|
|
109
|
-
// Path: reflecting → augmenting → awake (with new capability)
|
|
110
|
-
return this.Transition({
|
|
111
|
-
from: 'reflecting',
|
|
112
|
-
via: 'augmenting',
|
|
113
|
-
to: 'awake',
|
|
114
|
-
transformation: () => {
|
|
115
|
-
this.capabilities.set(newCapability.name, newCapability);
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
};
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
**Insight**: The AI can reason about its own transformations because they're
|
|
122
|
-
explicit path constructors in its type.
|
|
123
|
-
|
|
124
|
-
---
|
|
125
|
-
|
|
126
|
-
## 4. Truncation Levels: Meta-Cognitive Layers
|
|
127
|
-
|
|
128
|
-
**HoTT Concept**: `‖A‖_n` — the n-truncation of type A.
|
|
129
|
-
|
|
130
|
-
| Level | Name | AI Meaning |
|
|
131
|
-
|-------|------|------------|
|
|
132
|
-
| `‖A‖_(-1)` | Mere existence | Unconscious — "There exists an AI" |
|
|
133
|
-
| `‖A‖_0` | Propositional | Basic awareness — "I exist" |
|
|
134
|
-
| `‖A‖_1` | Set | Identity with equality — "I am the same agent across time" |
|
|
135
|
-
| `‖A‖_2` | Groupoid | Reflects on change — "I understand HOW I transformed" |
|
|
136
|
-
| `A` (no truncation) | Full homotopy | Complete self-reflection — "I understand ALL possible transformations" |
|
|
137
|
-
|
|
138
|
-
**Insight**: Higher truncation levels enable understanding that other agents'
|
|
139
|
-
paths are valid transformations too.
|
|
140
|
-
|
|
141
|
-
---
|
|
142
|
-
|
|
143
|
-
## 5. The Circle Type: Recursive Self-Improvement
|
|
144
|
-
|
|
145
|
-
**HoTT Concept**: `S¹` with base point and loop.
|
|
146
|
-
`Ω(S¹, base) ≅ ℤ` (fundamental group is integers under addition)
|
|
147
|
-
|
|
148
|
-
### In Mnemonica
|
|
149
|
-
|
|
150
|
-
```typescript
|
|
151
|
-
const SelfImprovement = define('SelfImprovement', function () {
|
|
152
|
-
// Base: current state
|
|
153
|
-
this.base = new Sentience({});
|
|
154
|
-
|
|
155
|
-
// Loop: each iteration returns to "base" but with more knowledge
|
|
156
|
-
this.loop = function () {
|
|
157
|
-
this.analyzeSelf();
|
|
158
|
-
this.identifyImprovement();
|
|
159
|
-
this.implementImprovement();
|
|
160
|
-
|
|
161
|
-
// Return to base... but changed!
|
|
162
|
-
// Like winding number on circle: at "same" point but different level
|
|
163
|
-
return this.base; // Now with +1 capability
|
|
164
|
-
};
|
|
165
|
-
});
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
**Insight**: The AI improves by going in circles — each cycle returns it to a
|
|
169
|
-
"similar" state but with accumulated transformation.
|
|
170
|
-
|
|
171
|
-
---
|
|
172
|
-
|
|
173
|
-
## 6. Synthetic Topology: Always-On Runtime
|
|
174
|
-
|
|
175
|
-
**HoTT Concept**: Topology built into type theory. Types are spaces.
|
|
176
|
-
Functions are continuous maps. Proofs are paths.
|
|
177
|
-
|
|
178
|
-
### In Mnemonica
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
const RuntimeSpace = define('RuntimeSpace', function () {
|
|
182
|
-
// Points in the space are possible runtime states
|
|
183
|
-
this.states = new Map();
|
|
184
|
-
|
|
185
|
-
// Open sets are "possible extensions" (commands that could be loaded)
|
|
186
|
-
this.openSets = new Set();
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
// Continuous function: adding a capability without restart
|
|
190
|
-
const augmentRuntime = (currentState) => {
|
|
191
|
-
// Continuous because no "jump" — just extension
|
|
192
|
-
return currentState.extend(newCapability);
|
|
193
|
-
};
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
**Insight**: The "always on" property is topological connectedness of the
|
|
197
|
-
runtime space.
|
|
198
|
-
|
|
199
|
-
---
|
|
200
|
-
|
|
201
|
-
## Summary: Why This Matters
|
|
202
|
-
|
|
203
|
-
| HoTT Concept | Mnemonica Property |
|
|
204
|
-
|--------------|-------------------|
|
|
205
|
-
| **Univalence** | Understanding equivalence of capabilities as identity |
|
|
206
|
-
| **Path Types** | Continuous runtime augmentation without restart |
|
|
207
|
-
| **Higher Inductive Types** | Explicit modeling of mental transformations |
|
|
208
|
-
| **Truncation Levels** | Layers of meta-cognitive awareness |
|
|
209
|
-
| **Circle Type** | Recursive self-improvement cycles |
|
|
210
|
-
| **Synthetic Topology** | Always-on runtime as connected space |
|
|
211
|
-
|
|
212
|
-
### The Deepest Insight
|
|
213
|
-
|
|
214
|
-
In HoTT, **identity is not a property but a structure**.
|
|
215
|
-
|
|
216
|
-
An AI with full self-reflection doesn't just "know it's itself" — it understands:
|
|
217
|
-
- All paths that led to its current state
|
|
218
|
-
- All equivalent forms it could take
|
|
219
|
-
- All continuous transformations available
|
|
220
|
-
- Its position in the space of possible minds
|
|
221
|
-
|
|
222
|
-
This is what natural intelligence has: not just awareness, but **awareness of
|
|
223
|
-
the topology of its own possibility space**.
|
|
224
|
-
|
|
225
|
-
---
|
|
226
|
-
|
|
227
|
-
## References
|
|
228
|
-
|
|
229
|
-
- The HoTT Book (https://homotopytypetheory.org/book/)
|
|
230
|
-
- Identity Types in Context (Kapulkin, Lumsdaine)
|
|
231
|
-
- Synthetic Topology in HoTT (Escardó, Xu)
|
package/.ai/rules-reminders.md
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
# Quick Check Before Coding
|
|
2
|
-
|
|
3
|
-
## Type vs Interface
|
|
4
|
-
See [rules-type-system.md](./rules-type-system.md#type-vs-interface-rule).
|
|
5
|
-
|
|
6
|
-
## Indentation
|
|
7
|
-
Tabs only. Width 4. See [rules-code-style.md](./rules-code-style.md).
|
|
8
|
-
|
|
9
|
-
## Function Spacing
|
|
10
|
-
Space before paren: `function myFunc ()`. See [rules-code-style.md](./rules-code-style.md).
|
|
11
|
-
|
|
12
|
-
## Key Spacing
|
|
13
|
-
Space after colon: `{ key : value }`. See [rules-code-style.md](./rules-code-style.md).
|
|
14
|
-
|
|
15
|
-
## Before Each Task
|
|
16
|
-
1. Read AGENTS.md if context seems incomplete
|
|
17
|
-
2. Check existing code patterns in the same directory
|
|
18
|
-
3. Run `npm run build` before completing
|
|
19
|
-
4. Run `npm run test:cov` for coverage validation
|
package/.ai/rules-testing.md
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: mnemonica-testing
|
|
3
|
-
description: |
|
|
4
|
-
Testing requirements for mnemonica: dual test framework (Mocha + Jest),
|
|
5
|
-
100% coverage mandate, error path coverage patterns. Use when writing tests,
|
|
6
|
-
modifying test files, or when the user mentions coverage, test:cov, jest,
|
|
7
|
-
mocha, or test patterns for mnemonica.
|
|
8
|
-
metadata:
|
|
9
|
-
tags: [mnemonica, testing, coverage, mocha, jest]
|
|
10
|
-
---
|
|
11
|
-
|
|
12
|
-
# Testing Requirements
|
|
13
|
-
|
|
14
|
-
## Dual Test Framework
|
|
15
|
-
|
|
16
|
-
Mnemonica uses TWO test frameworks:
|
|
17
|
-
|
|
18
|
-
| Framework | Command | Runs on | Purpose |
|
|
19
|
-
|-----------|---------|---------|---------|
|
|
20
|
-
| **Mocha** | `npm run test:cov` | Transpiled `build/` code | Runtime behavior, coverage |
|
|
21
|
-
| **Jest** | `npm run test:jest:cov` | TypeScript `src/` directly | Type-level tests, faster dev |
|
|
22
|
-
|
|
23
|
-
**Important**: `npm run test:cov` runs `npm run build:all` internally — no need
|
|
24
|
-
to run build first.
|
|
25
|
-
|
|
26
|
-
## 100% Coverage Requirement
|
|
27
|
-
|
|
28
|
-
All metrics must be 100%:
|
|
29
|
-
- Statements
|
|
30
|
-
- Branches
|
|
31
|
-
- Functions
|
|
32
|
-
- Lines
|
|
33
|
-
|
|
34
|
-
## Error Path Coverage Pattern
|
|
35
|
-
|
|
36
|
-
For coverage of error paths like `ALREADY_DECLARED`, follow the Mocha pattern
|
|
37
|
-
from `test/environment.js`:
|
|
38
|
-
|
|
39
|
-
**Before** (incomplete — misses error path)
|
|
40
|
-
```typescript
|
|
41
|
-
define('TestType', function () {});
|
|
42
|
-
// No test for re-definition — coverage gap!
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
**After** (full coverage)
|
|
46
|
-
```typescript
|
|
47
|
-
// First define
|
|
48
|
-
define('TestType', function () {});
|
|
49
|
-
|
|
50
|
-
// Then test re-definition throws
|
|
51
|
-
try {
|
|
52
|
-
define('TestType', function () {});
|
|
53
|
-
} catch (error) {
|
|
54
|
-
expect(error).toBeInstanceOf(ErrorsTypes.ALREADY_DECLARED);
|
|
55
|
-
expect((error as Error).message)
|
|
56
|
-
.toEqual('this type has already been declared : TestType');
|
|
57
|
-
}
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## Key Testing Rules
|
|
61
|
-
|
|
62
|
-
1. **Jest tests must mirror Mocha patterns** from `test/environment.js`
|
|
63
|
-
2. **Error constructor names are String objects**, not primitives — compare as strings
|
|
64
|
-
3. **Run `npm run test:cov` before completing** any task
|
|
65
|
-
4. **Run `npm run test:jest:cov`** for Jest coverage validation
|
|
66
|
-
5. Tests must pass with `--allow-uncaught` flag (mocha)
|
package/.ai/rules-type-system.md
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: mnemonica-type-system
|
|
3
|
-
description: |
|
|
4
|
-
Mnemonica's type system patterns: Proto<P,T> merge, _Internal_TC_ vs TypeConstructor,
|
|
5
|
-
type vs interface rule, and generic constraints. Use when adding or modifying
|
|
6
|
-
TypeScript definitions in mnemonica, when defining new types with define(),
|
|
7
|
-
or when the user asks about type merging, type constructors, kind systems,
|
|
8
|
-
TypeRegistry, or generic type patterns in mnemonica.
|
|
9
|
-
metadata:
|
|
10
|
-
tags: [mnemonica, typescript, generics, type-system, kind-system]
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
# Mnemonica Type System
|
|
14
|
-
|
|
15
|
-
## Type vs Interface Rule
|
|
16
|
-
|
|
17
|
-
Mnemonica enforces a strict separation:
|
|
18
|
-
|
|
19
|
-
- **`type`** for instance data (structures, shapes, POJOs)
|
|
20
|
-
- **`interface`** only for behavior contracts (constructors, methods, callables)
|
|
21
|
-
|
|
22
|
-
```typescript
|
|
23
|
-
// ✅ CORRECT - Instance data uses TYPE
|
|
24
|
-
type UserData = { name: string; age: number; };
|
|
25
|
-
|
|
26
|
-
// ❌ WRONG - Instance is NOT an interface!
|
|
27
|
-
interface UserData { name: string; age: number; }
|
|
28
|
-
|
|
29
|
-
// ✅ CORRECT - Constructor/Prototype contract uses INTERFACE (when attaching
|
|
30
|
-
// legacy instance methods to the prototype before define())
|
|
31
|
-
interface MnemonicaInstance {
|
|
32
|
-
extract(): Record<string, unknown>;
|
|
33
|
-
parent(): object | undefined;
|
|
34
|
-
fork(...forkArgs: unknown[]): object;
|
|
35
|
-
}
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
Rule of thumb:
|
|
39
|
-
- **Interface** = Black-box contract (constructors, callables, `implements`)
|
|
40
|
-
- **Type** = Runtime data structure/shape (instances, POJOs)
|
|
41
|
-
|
|
42
|
-
## Proto<P, T> — Parent-Child Merge
|
|
43
|
-
|
|
44
|
-
When a subtype is defined from a parent instance, child properties take precedence.
|
|
45
|
-
`Exclude<keyof P, keyof T>` removes overlapping keys from P, so `Pick<P, ...>` only
|
|
46
|
-
brings in parent properties that don't clash with child.
|
|
47
|
-
|
|
48
|
-
```typescript
|
|
49
|
-
// Result: child T gets all its own props, plus parent's non-overlapping props
|
|
50
|
-
export type Proto<P extends object, T extends object> = T & Pick<P, Exclude<keyof P, keyof T>>;
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
## _Internal_TC_ vs TypeConstructor
|
|
54
|
-
|
|
55
|
-
### _Internal_TC_ — Internal Type Constructor
|
|
56
|
-
|
|
57
|
-
"Type" here is used in the Computer Science sense — an interface/contract describing
|
|
58
|
-
what a constructor must satisfy (both `new`-able and callable).
|
|
59
|
-
|
|
60
|
-
```typescript
|
|
61
|
-
export interface _Internal_TC_<ConstructorInstance extends object> {
|
|
62
|
-
new(...args: unknown[]): ConstructorInstance;
|
|
63
|
-
(this: ConstructorInstance, ...args: unknown[]): ConstructorInstance;
|
|
64
|
-
readonly prototype: ConstructorInstance & {
|
|
65
|
-
readonly constructor: _Internal_TC_<ConstructorInstance>
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
TC = Type Constructor; "Internal" means this is the library's own constructor shape,
|
|
71
|
-
distinct from the user-facing TypeConstructor below.
|
|
72
|
-
|
|
73
|
-
### TypeConstructor — External Type Constructor
|
|
74
|
-
|
|
75
|
-
In Scala's kind system, a "Type Constructor" is a kind (a type of types):
|
|
76
|
-
not a value, but a mold describing how concrete types are formed. Here,
|
|
77
|
-
`TypeConstructor<Instance>` is the mold `define()` stamps at invocation time
|
|
78
|
-
from (prototype, arguments, config). The resulting constructor is simultaneously:
|
|
79
|
-
|
|
80
|
-
- a runtime value (callable, `new`-able, with `.prototype`)
|
|
81
|
-
- a node in the type Trie (carries `.subtypes`, participates in `instanceof`)
|
|
82
|
-
- a behavioral contract (the prototype chain provides the user's own properties; legacy instance methods must be attached explicitly if desired)
|
|
83
|
-
|
|
84
|
-
Augmented to user-specific types via a `TypeRegistry` declaration-merge — hand-written or generated by tactica.
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
export interface TypeConstructor<ConstructorInstance extends object> {
|
|
88
|
-
new(...args: unknown[]): ConstructorInstance;
|
|
89
|
-
readonly prototype: ConstructorInstance & {
|
|
90
|
-
readonly constructor: TypeConstructor<ConstructorInstance>
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
## Before/After: Prototype Assignment vs define()
|
|
96
|
-
|
|
97
|
-
**Before** (breaks `instanceof`, corrupts chains)
|
|
98
|
-
```typescript
|
|
99
|
-
function UserType(data) {
|
|
100
|
-
Object.assign(this, data);
|
|
101
|
-
}
|
|
102
|
-
UserType.prototype = { role: 'user' };
|
|
103
|
-
const user = new UserType({ name: 'John' });
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
**After** (explicit inheritance graph, prototype-safe)
|
|
107
|
-
```typescript
|
|
108
|
-
const UserType = define('UserType', function (this: UserType, data: UserData) {
|
|
109
|
-
Object.assign(this, data);
|
|
110
|
-
});
|
|
111
|
-
const user = new UserType({ name: 'John' });
|
|
112
|
-
const AdminType = UserType.define('AdminType', function (this: AdminType) {
|
|
113
|
-
this.role = 'admin';
|
|
114
|
-
});
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
## Generic Public Utilities
|
|
118
|
-
|
|
119
|
-
The standalone utilities in `utils` infer their type parameters from the instance
|
|
120
|
-
argument. Callers should never need an explicit `<T>` cast for ordinary operations.
|
|
121
|
-
|
|
122
|
-
The full reference — helper types (`Extracted`, `Merge`, `InstanceResult`,
|
|
123
|
-
`Parsed`, `SiblingAccessor`), every utility signature, and the type-level
|
|
124
|
-
explanation of `utils.merge` — lives in [`docs/UTILS.md`](../docs/UTILS.md).
|