@claudetools/tools 0.8.11 → 0.9.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/dist/codedna/generators/astro.d.ts +18 -0
- package/dist/codedna/generators/astro.js +91 -0
- package/dist/codedna/generators/authjs.d.ts +18 -0
- package/dist/codedna/generators/authjs.js +68 -0
- package/dist/codedna/generators/better-auth.d.ts +18 -0
- package/dist/codedna/generators/better-auth.js +62 -0
- package/dist/codedna/generators/drizzle-orm.d.ts +18 -0
- package/dist/codedna/generators/drizzle-orm.js +65 -0
- package/dist/codedna/generators/elysia-api.d.ts +12 -0
- package/dist/codedna/generators/elysia-api.js +64 -0
- package/dist/codedna/generators/hono-api.d.ts +12 -0
- package/dist/codedna/generators/hono-api.js +64 -0
- package/dist/codedna/generators/lucia-auth.d.ts +18 -0
- package/dist/codedna/generators/lucia-auth.js +69 -0
- package/dist/codedna/generators/prisma.d.ts +18 -0
- package/dist/codedna/generators/prisma.js +64 -0
- package/dist/codedna/generators/react-router-v7.d.ts +18 -0
- package/dist/codedna/generators/react-router-v7.js +77 -0
- package/dist/codedna/generators/react19-shadcn.d.ts +21 -0
- package/dist/codedna/generators/react19-shadcn.js +367 -0
- package/dist/codedna/generators/sveltekit.d.ts +18 -0
- package/dist/codedna/generators/sveltekit.js +73 -0
- package/dist/codedna/generators/tanstack-start-drizzle.d.ts +92 -0
- package/dist/codedna/generators/tanstack-start-drizzle.js +824 -0
- package/dist/codedna/generators/trpc-api.d.ts +12 -0
- package/dist/codedna/generators/trpc-api.js +64 -0
- package/dist/codedna/index.d.ts +31 -0
- package/dist/codedna/index.js +39 -0
- package/dist/codedna/kappa-api-generator.d.ts +89 -0
- package/dist/codedna/kappa-api-generator.js +493 -0
- package/dist/codedna/kappa-ast.d.ts +552 -0
- package/dist/codedna/kappa-ast.js +141 -0
- package/dist/codedna/kappa-cli.d.ts +2 -0
- package/dist/codedna/kappa-cli.js +302 -0
- package/dist/codedna/kappa-component-generator.d.ts +47 -0
- package/dist/codedna/kappa-component-generator.js +295 -0
- package/dist/codedna/kappa-design-generator.d.ts +52 -0
- package/dist/codedna/kappa-design-generator.js +365 -0
- package/dist/codedna/kappa-drizzle-generator.d.ts +45 -0
- package/dist/codedna/kappa-drizzle-generator.js +355 -0
- package/dist/codedna/kappa-form-generator.d.ts +51 -0
- package/dist/codedna/kappa-form-generator.js +319 -0
- package/dist/codedna/kappa-lexer.d.ts +268 -0
- package/dist/codedna/kappa-lexer.js +757 -0
- package/dist/codedna/kappa-page-generator.d.ts +57 -0
- package/dist/codedna/kappa-page-generator.js +338 -0
- package/dist/codedna/kappa-parser.d.ts +261 -0
- package/dist/codedna/kappa-parser.js +2547 -0
- package/dist/codedna/kappa-provenance.d.ts +101 -0
- package/dist/codedna/kappa-provenance.js +199 -0
- package/dist/codedna/kappa-types-generator.d.ts +37 -0
- package/dist/codedna/kappa-types-generator.js +159 -0
- package/dist/codedna/kappa-validator.d.ts +86 -0
- package/dist/codedna/kappa-validator.js +638 -0
- package/dist/codedna/kappa-zod-generator.d.ts +32 -0
- package/dist/codedna/kappa-zod-generator.js +216 -0
- package/dist/handlers/kappa-handlers.d.ts +116 -0
- package/dist/handlers/kappa-handlers.js +465 -0
- package/dist/handlers/tool-handlers.js +121 -0
- package/dist/templates/claude-md.d.ts +1 -1
- package/dist/templates/claude-md.js +166 -9
- package/dist/tools.js +199 -0
- package/docs/research/2026-01-02-codedna-il-specification.md +639 -0
- package/docs/research/2026-01-02-codedna-v2-research.md +943 -0
- package/docs/research/2026-01-02-computation-foundations.md +564 -0
- package/docs/research/2026-01-02-hardware-description.md +814 -0
- package/docs/research/2026-01-02-kappa-specification.md +697 -0
- package/docs/research/2026-01-02-kappa-tanstack-example.md +527 -0
- package/docs/research/2026-01-02-kappa-v2-synthesis.md +406 -0
- package/docs/research/2026-01-02-kappa-v2.5-specification.md +1218 -0
- package/docs/research/2026-01-02-kappa-v3-specification.md +1864 -0
- package/docs/research/2026-01-02-kappa-whitepaper.md +662 -0
- package/docs/research/2026-01-02-logic-constraint.md +731 -0
- package/docs/research/2026-01-02-quantum-computation.md +635 -0
- package/package.json +4 -2
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
# Kappa (κ) - Universal Computation Language
|
|
2
|
+
|
|
3
|
+
**Version:** 0.1.0-alpha
|
|
4
|
+
**Date:** 2026-01-02
|
|
5
|
+
**Status:** First Principles Design
|
|
6
|
+
|
|
7
|
+
## Foundation
|
|
8
|
+
|
|
9
|
+
Kappa is derived from first principles of computation theory, not from domain patterns. It provides the **minimal set of primitives** needed to express **any computation** in a token-efficient format optimised for AI agents.
|
|
10
|
+
|
|
11
|
+
### Theoretical Basis
|
|
12
|
+
|
|
13
|
+
- **Lambda Calculus**: Function abstraction and application ([Wikipedia](https://en.wikipedia.org/wiki/Lambda_calculus))
|
|
14
|
+
- **Category Theory**: Morphisms and composition ([Number Analytics](https://www.numberanalytics.com/blog/category-theory-logic-computation-intersection))
|
|
15
|
+
- **Effect Systems**: Tracking computational effects ([Wikipedia](https://en.wikipedia.org/wiki/Effect_system))
|
|
16
|
+
- **Curry-Howard**: Types as propositions, programs as proofs
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Six Primitives
|
|
21
|
+
|
|
22
|
+
Kappa has exactly **6 core primitives**. Everything else is derived.
|
|
23
|
+
|
|
24
|
+
| Primitive | Symbol | Meaning | Example |
|
|
25
|
+
|-----------|--------|---------|---------|
|
|
26
|
+
| **Binding** | `=` | Associate name with value | `x = 42` |
|
|
27
|
+
| **Application** | `()` | Apply function to arguments | `f(x, y)` |
|
|
28
|
+
| **Composition** | `>>` | Combine functions (output→input) | `parse >> validate >> save` |
|
|
29
|
+
| **Abstraction** | `=>` | Create a function | `x => x * 2` |
|
|
30
|
+
| **Type** | `:` | Annotate with type | `x: int` |
|
|
31
|
+
| **Effect** | `-[E]->` | Annotate with effects | `f: A -[IO]-> B` |
|
|
32
|
+
|
|
33
|
+
That's it. Six symbols. Universal computation.
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Why Only 6?
|
|
38
|
+
|
|
39
|
+
**Lambda calculus** has 3 elements (variable, abstraction, application) and is Turing-complete.
|
|
40
|
+
|
|
41
|
+
Kappa adds:
|
|
42
|
+
- **Composition** (`>>`) for pipeline clarity (derivable but common)
|
|
43
|
+
- **Types** (`:`) for semantic information
|
|
44
|
+
- **Effects** (`-[E]->`) to distinguish pure from impure
|
|
45
|
+
|
|
46
|
+
These 6 primitives can express:
|
|
47
|
+
- REST APIs
|
|
48
|
+
- GPU shaders
|
|
49
|
+
- Game engines
|
|
50
|
+
- ML models
|
|
51
|
+
- Compilers
|
|
52
|
+
- Physics simulations
|
|
53
|
+
- Infrastructure
|
|
54
|
+
- Literally anything computable
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## The Universal Pattern
|
|
59
|
+
|
|
60
|
+
All code follows one pattern:
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
name: InputTypes -[Effects]-> OutputType = transformation
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
This pattern works for EVERYTHING:
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
# Web API endpoint
|
|
70
|
+
createUser: (email: str, pass: str) -[DB, Auth]-> User
|
|
71
|
+
|
|
72
|
+
# GPU shader
|
|
73
|
+
pbr: (albedo: vec3, normal: vec3, rough: f32) -> vec3
|
|
74
|
+
|
|
75
|
+
# Game system
|
|
76
|
+
physics: (entities: [Entity], dt: f32) -[State]-> [Entity]
|
|
77
|
+
|
|
78
|
+
# ML layer
|
|
79
|
+
attention: (q: Tensor, k: Tensor, v: Tensor) -> Tensor
|
|
80
|
+
|
|
81
|
+
# Compiler pass
|
|
82
|
+
typeCheck: (ast: AST) -[Error]-> TypedAST
|
|
83
|
+
|
|
84
|
+
# Infrastructure
|
|
85
|
+
deploy: (config: Config) -[AWS, IO]-> Resources
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Same pattern. Different types and effects. Universal.
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## Types
|
|
93
|
+
|
|
94
|
+
Types describe the shape of data.
|
|
95
|
+
|
|
96
|
+
### Primitives
|
|
97
|
+
```
|
|
98
|
+
int, f32, f64 # Numbers
|
|
99
|
+
str, char # Text
|
|
100
|
+
bool # Boolean
|
|
101
|
+
void # No value
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Compounds
|
|
105
|
+
```
|
|
106
|
+
[T] # Array of T
|
|
107
|
+
{a: T, b: U} # Record/struct
|
|
108
|
+
T | U # Union (either T or U)
|
|
109
|
+
T? # Optional (T or nothing)
|
|
110
|
+
(A, B) -> C # Function type
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Parameterised
|
|
114
|
+
```
|
|
115
|
+
Result[T, E] # Success T or Error E
|
|
116
|
+
Tensor[B, S, D] # Shaped tensor
|
|
117
|
+
Entity[C1, C2] # Entity with components
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Custom
|
|
121
|
+
```
|
|
122
|
+
User = { id: int, email: str, role: Role }
|
|
123
|
+
Role = admin | user | guest
|
|
124
|
+
Vec3 = { x: f32, y: f32, z: f32 }
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
## Effects
|
|
130
|
+
|
|
131
|
+
Effects track what a function can DO beyond returning a value.
|
|
132
|
+
|
|
133
|
+
### Core Effects
|
|
134
|
+
```
|
|
135
|
+
Pure # No effects (default, can omit)
|
|
136
|
+
IO # External I/O (network, file, database)
|
|
137
|
+
State # Mutable state
|
|
138
|
+
Random # Non-determinism
|
|
139
|
+
Error[E] # Can fail with error type E
|
|
140
|
+
Async # Can suspend/resume
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Domain Effects
|
|
144
|
+
```
|
|
145
|
+
GPU # GPU compute/render
|
|
146
|
+
Time # Access to time/clock
|
|
147
|
+
Input # User input
|
|
148
|
+
Audio # Sound output
|
|
149
|
+
Param # Learned parameters (ML)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Composition
|
|
153
|
+
```
|
|
154
|
+
# Effects compose with +
|
|
155
|
+
f: A -[IO + Error[E]]-> B
|
|
156
|
+
|
|
157
|
+
# Pure functions have no effect annotation (or explicit Pure)
|
|
158
|
+
g: A -> B
|
|
159
|
+
g: A -[Pure]-> B # equivalent
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Why Effects Matter
|
|
163
|
+
|
|
164
|
+
Effects make the INVISIBLE visible:
|
|
165
|
+
- "Does this function call the network?" → Check for `IO`
|
|
166
|
+
- "Can this fail?" → Check for `Error`
|
|
167
|
+
- "Does this need GPU?" → Check for `GPU`
|
|
168
|
+
|
|
169
|
+
AI agents use effects to understand what code DOES, not just what it computes.
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## Composition
|
|
174
|
+
|
|
175
|
+
The `>>` operator is the heart of Kappa.
|
|
176
|
+
|
|
177
|
+
### Pipeline (Sequential)
|
|
178
|
+
```
|
|
179
|
+
# Left-to-right data flow
|
|
180
|
+
input >> parse >> validate >> transform >> output
|
|
181
|
+
|
|
182
|
+
# Equivalent to:
|
|
183
|
+
output(transform(validate(parse(input))))
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Parallel
|
|
187
|
+
```
|
|
188
|
+
# Fork-join pattern
|
|
189
|
+
input >> (branchA & branchB) >> merge
|
|
190
|
+
|
|
191
|
+
# Explicit parallel composition
|
|
192
|
+
parallel {
|
|
193
|
+
a = computeA(input)
|
|
194
|
+
b = computeB(input)
|
|
195
|
+
c = computeC(input)
|
|
196
|
+
}
|
|
197
|
+
>> combine(a, b, c)
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Conditional
|
|
201
|
+
```
|
|
202
|
+
input >> validate >>
|
|
203
|
+
| valid => process >> success
|
|
204
|
+
| invalid => error
|
|
205
|
+
|
|
206
|
+
# Or with pattern matching
|
|
207
|
+
result >> match {
|
|
208
|
+
Ok(v) => handleSuccess(v)
|
|
209
|
+
Err(e) => handleError(e)
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Graph (Complex Flows)
|
|
214
|
+
```
|
|
215
|
+
graph AuthFlow {
|
|
216
|
+
# Named nodes
|
|
217
|
+
creds = input(email: str, password: str)
|
|
218
|
+
validated = validate(creds)
|
|
219
|
+
user = fetch(creds.email) when validated.ok
|
|
220
|
+
token = sign(user) when user.found
|
|
221
|
+
|
|
222
|
+
# Output
|
|
223
|
+
output(token) or error(validated.err | user.err)
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## Functions
|
|
230
|
+
|
|
231
|
+
Functions are first-class values.
|
|
232
|
+
|
|
233
|
+
### Definition
|
|
234
|
+
```
|
|
235
|
+
# Full form
|
|
236
|
+
add: (a: int, b: int) -> int = a + b
|
|
237
|
+
|
|
238
|
+
# With effects
|
|
239
|
+
fetchUser: (id: int) -[DB]-> User? = db.query("users", id)
|
|
240
|
+
|
|
241
|
+
# Anonymous
|
|
242
|
+
nums.map(x => x * 2)
|
|
243
|
+
|
|
244
|
+
# Curried
|
|
245
|
+
add: int -> int -> int = a => b => a + b
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Higher-Order
|
|
249
|
+
```
|
|
250
|
+
# Function that returns function
|
|
251
|
+
multiplier: int -> (int -> int) = n => x => x * n
|
|
252
|
+
double = multiplier(2)
|
|
253
|
+
|
|
254
|
+
# Function that takes function
|
|
255
|
+
map: ([T], T -> U) -> [U]
|
|
256
|
+
filter: ([T], T -> bool) -> [T]
|
|
257
|
+
reduce: ([T], (A, T) -> A, A) -> A
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
### Composition
|
|
261
|
+
```
|
|
262
|
+
# Point-free style
|
|
263
|
+
processUser = fetch >> validate >> transform >> save
|
|
264
|
+
|
|
265
|
+
# Is equivalent to:
|
|
266
|
+
processUser = user => save(transform(validate(fetch(user))))
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
## Example: Game Engine
|
|
272
|
+
|
|
273
|
+
```
|
|
274
|
+
# === Types ===
|
|
275
|
+
|
|
276
|
+
Vec3 = { x: f32, y: f32, z: f32 }
|
|
277
|
+
Quat = { w: f32, x: f32, y: f32, z: f32 }
|
|
278
|
+
|
|
279
|
+
# Components (data)
|
|
280
|
+
Transform = { position: Vec3, rotation: Quat, scale: Vec3 }
|
|
281
|
+
Velocity = { linear: Vec3, angular: Vec3 }
|
|
282
|
+
Collider = Box { size: Vec3 } | Sphere { radius: f32 }
|
|
283
|
+
Sprite = { texture: TextureId, uv: Rect, color: Color }
|
|
284
|
+
|
|
285
|
+
# Entity is parameterised by its components
|
|
286
|
+
Entity[C...] = { id: EntityId, components: C... }
|
|
287
|
+
|
|
288
|
+
# === Systems (functions over entities) ===
|
|
289
|
+
|
|
290
|
+
movementSystem: ([Entity[Transform, Velocity]], f32) -[State]-> void =
|
|
291
|
+
(entities, dt) => entities.each(e => {
|
|
292
|
+
e.transform.position += e.velocity.linear * dt
|
|
293
|
+
e.transform.rotation *= quatFromAngular(e.velocity.angular * dt)
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
physicsSystem: ([Entity[Transform, Collider, Velocity]], f32) -[State]-> void =
|
|
297
|
+
(entities, dt) => {
|
|
298
|
+
pairs = broadPhase(entities)
|
|
299
|
+
contacts = pairs.flatMap(narrowPhase)
|
|
300
|
+
impulses = solveConstraints(contacts, iterations: 10)
|
|
301
|
+
impulses.each(apply)
|
|
302
|
+
integrate(entities, dt)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
renderSystem: ([Entity[Transform, Sprite]], Camera) -[GPU]-> void =
|
|
306
|
+
(entities, camera) => {
|
|
307
|
+
sorted = entities.sortBy(_.transform.position.z)
|
|
308
|
+
sorted.each(e => drawSprite(e.sprite, e.transform, camera))
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
# === Game Loop ===
|
|
312
|
+
|
|
313
|
+
gameLoop: -[Time, Input, State, GPU]-> void = loop(fps: 60) {
|
|
314
|
+
dt = deltaTime()
|
|
315
|
+
|
|
316
|
+
# Systems run in order
|
|
317
|
+
inputSystem(dt)
|
|
318
|
+
>> movementSystem(dt)
|
|
319
|
+
>> physicsSystem(dt)
|
|
320
|
+
>> renderSystem(camera)
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
---
|
|
325
|
+
|
|
326
|
+
## Example: PBR Shader
|
|
327
|
+
|
|
328
|
+
```
|
|
329
|
+
# Pure math - no effects
|
|
330
|
+
# This compiles to GLSL/HLSL/Metal
|
|
331
|
+
|
|
332
|
+
PI: f32 = 3.14159265359
|
|
333
|
+
|
|
334
|
+
distributionGGX: (n: Vec3, h: Vec3, roughness: f32) -> f32 = {
|
|
335
|
+
a = roughness * roughness
|
|
336
|
+
a2 = a * a
|
|
337
|
+
nDotH = max(dot(n, h), 0.0)
|
|
338
|
+
nDotH2 = nDotH * nDotH
|
|
339
|
+
|
|
340
|
+
denom = nDotH2 * (a2 - 1.0) + 1.0
|
|
341
|
+
denom = PI * denom * denom
|
|
342
|
+
|
|
343
|
+
a2 / denom
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
geometrySchlickGGX: (nDotV: f32, roughness: f32) -> f32 = {
|
|
347
|
+
r = roughness + 1.0
|
|
348
|
+
k = (r * r) / 8.0
|
|
349
|
+
nDotV / (nDotV * (1.0 - k) + k)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
geometrySmith: (n: Vec3, v: Vec3, l: Vec3, roughness: f32) -> f32 = {
|
|
353
|
+
nDotV = max(dot(n, v), 0.0)
|
|
354
|
+
nDotL = max(dot(n, l), 0.0)
|
|
355
|
+
ggx1 = geometrySchlickGGX(nDotV, roughness)
|
|
356
|
+
ggx2 = geometrySchlickGGX(nDotL, roughness)
|
|
357
|
+
ggx1 * ggx2
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
fresnelSchlick: (cosTheta: f32, f0: Vec3) -> Vec3 =
|
|
361
|
+
f0 + (1.0 - f0) * pow(1.0 - cosTheta, 5.0)
|
|
362
|
+
|
|
363
|
+
# Main PBR function
|
|
364
|
+
pbrShading: (
|
|
365
|
+
albedo: Vec3,
|
|
366
|
+
normal: Vec3,
|
|
367
|
+
metallic: f32,
|
|
368
|
+
roughness: f32,
|
|
369
|
+
viewDir: Vec3,
|
|
370
|
+
lightDir: Vec3,
|
|
371
|
+
lightColor: Vec3
|
|
372
|
+
) -> Vec3 = {
|
|
373
|
+
h = normalize(viewDir + lightDir)
|
|
374
|
+
nDotL = max(dot(normal, lightDir), 0.0)
|
|
375
|
+
nDotV = max(dot(normal, viewDir), 0.0)
|
|
376
|
+
|
|
377
|
+
f0 = mix(vec3(0.04), albedo, metallic)
|
|
378
|
+
|
|
379
|
+
D = distributionGGX(normal, h, roughness)
|
|
380
|
+
G = geometrySmith(normal, viewDir, lightDir, roughness)
|
|
381
|
+
F = fresnelSchlick(max(dot(h, viewDir), 0.0), f0)
|
|
382
|
+
|
|
383
|
+
specular = (D * G * F) / max(4.0 * nDotV * nDotL, 0.001)
|
|
384
|
+
kD = (vec3(1.0) - F) * (1.0 - metallic)
|
|
385
|
+
diffuse = kD * albedo / PI
|
|
386
|
+
|
|
387
|
+
(diffuse + specular) * lightColor * nDotL
|
|
388
|
+
}
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
---
|
|
392
|
+
|
|
393
|
+
## Example: Transformer Layer
|
|
394
|
+
|
|
395
|
+
```
|
|
396
|
+
# Types for tensors with shape information
|
|
397
|
+
Tensor[...Dims] = opaque # Actual implementation varies by framework
|
|
398
|
+
|
|
399
|
+
# Attention mechanism - pure math
|
|
400
|
+
scaledDotProductAttention: (
|
|
401
|
+
q: Tensor[B, H, S, D],
|
|
402
|
+
k: Tensor[B, H, S, D],
|
|
403
|
+
v: Tensor[B, H, S, D],
|
|
404
|
+
mask: Tensor[B, 1, S, S]?
|
|
405
|
+
) -> Tensor[B, H, S, D] = {
|
|
406
|
+
scale = 1.0 / sqrt(D.toFloat)
|
|
407
|
+
scores = matmul(q, transpose(k, -2, -1)) * scale
|
|
408
|
+
|
|
409
|
+
masked = mask.map(m =>
|
|
410
|
+
scores.maskedFill(m == 0, -inf)
|
|
411
|
+
) ?? scores
|
|
412
|
+
|
|
413
|
+
attn = softmax(masked, dim: -1)
|
|
414
|
+
matmul(attn, v)
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
# Multi-head attention with learned parameters
|
|
418
|
+
multiHeadAttention: (
|
|
419
|
+
x: Tensor[B, S, D],
|
|
420
|
+
numHeads: int,
|
|
421
|
+
mask: Tensor[B, 1, S, S]?
|
|
422
|
+
) -[Param]-> Tensor[B, S, D] = {
|
|
423
|
+
dHead = D / numHeads
|
|
424
|
+
|
|
425
|
+
# Learned projections (Param effect)
|
|
426
|
+
qProj = linear(D, D)
|
|
427
|
+
kProj = linear(D, D)
|
|
428
|
+
vProj = linear(D, D)
|
|
429
|
+
outProj = linear(D, D)
|
|
430
|
+
|
|
431
|
+
# Project and reshape for multi-head
|
|
432
|
+
q = qProj(x).reshape(B, S, numHeads, dHead).transpose(1, 2)
|
|
433
|
+
k = kProj(x).reshape(B, S, numHeads, dHead).transpose(1, 2)
|
|
434
|
+
v = vProj(x).reshape(B, S, numHeads, dHead).transpose(1, 2)
|
|
435
|
+
|
|
436
|
+
# Attention
|
|
437
|
+
attended = scaledDotProductAttention(q, k, v, mask)
|
|
438
|
+
|
|
439
|
+
# Reshape and project out
|
|
440
|
+
attended.transpose(1, 2).reshape(B, S, D) >> outProj
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
# Full transformer block
|
|
444
|
+
transformerBlock: (
|
|
445
|
+
x: Tensor[B, S, D],
|
|
446
|
+
numHeads: int,
|
|
447
|
+
ffDim: int,
|
|
448
|
+
mask: Tensor[B, 1, S, S]?
|
|
449
|
+
) -[Param]-> Tensor[B, S, D] = {
|
|
450
|
+
# Self-attention with residual
|
|
451
|
+
attnOut = x + multiHeadAttention(layerNorm(x), numHeads, mask)
|
|
452
|
+
|
|
453
|
+
# FFN with residual
|
|
454
|
+
ffn = linear(D, ffDim) >> gelu >> linear(ffDim, D)
|
|
455
|
+
attnOut + ffn(layerNorm(attnOut))
|
|
456
|
+
}
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
---
|
|
460
|
+
|
|
461
|
+
## Example: REST API
|
|
462
|
+
|
|
463
|
+
```
|
|
464
|
+
# Types
|
|
465
|
+
User = { id: int, email: str, role: Role, createdAt: DateTime }
|
|
466
|
+
Role = admin | user | guest
|
|
467
|
+
CreateUserRequest = { email: str, password: str }
|
|
468
|
+
AuthToken = { token: str, expiresAt: DateTime }
|
|
469
|
+
|
|
470
|
+
# Effects used: DB, Auth, Error
|
|
471
|
+
ApiError = NotFound | Unauthorized | ValidationError[str] | InternalError
|
|
472
|
+
|
|
473
|
+
# Endpoints are functions with effects
|
|
474
|
+
createUser: CreateUserRequest -[DB, Error[ApiError]]-> User = req => {
|
|
475
|
+
# Validate
|
|
476
|
+
req.email.isValidEmail || throw ValidationError("Invalid email")
|
|
477
|
+
req.password.length >= 8 || throw ValidationError("Password too short")
|
|
478
|
+
|
|
479
|
+
# Check uniqueness
|
|
480
|
+
db.findByEmail(req.email).isSome && throw ValidationError("Email exists")
|
|
481
|
+
|
|
482
|
+
# Create
|
|
483
|
+
user = {
|
|
484
|
+
id: db.nextId(),
|
|
485
|
+
email: req.email,
|
|
486
|
+
role: user,
|
|
487
|
+
createdAt: now()
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
passwordHash = bcrypt.hash(req.password, rounds: 12)
|
|
491
|
+
db.insert("users", user, { passwordHash })
|
|
492
|
+
|
|
493
|
+
user
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
authenticate: (email: str, password: str) -[DB, Error[ApiError]]-> AuthToken = {
|
|
497
|
+
user = db.findByEmail(email) ?? throw NotFound
|
|
498
|
+
|
|
499
|
+
stored = db.getPasswordHash(user.id)
|
|
500
|
+
bcrypt.verify(password, stored) || throw Unauthorized
|
|
501
|
+
|
|
502
|
+
token = jwt.sign({ userId: user.id, role: user.role }, expiresIn: "24h")
|
|
503
|
+
{ token, expiresAt: now() + hours(24) }
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
# Route composition
|
|
507
|
+
routes = router {
|
|
508
|
+
POST "/users" => createUser
|
|
509
|
+
POST "/auth/login" => authenticate
|
|
510
|
+
GET "/users/:id" => getUser
|
|
511
|
+
PUT "/users/:id" => updateUser >> requireAuth(admin | self)
|
|
512
|
+
DELETE "/users/:id" => deleteUser >> requireAuth(admin)
|
|
513
|
+
}
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
---
|
|
517
|
+
|
|
518
|
+
## Transformations
|
|
519
|
+
|
|
520
|
+
Transformations modify existing code. They are functions: `Code -> Code`.
|
|
521
|
+
|
|
522
|
+
### Structural Transforms
|
|
523
|
+
```
|
|
524
|
+
# Add field to type
|
|
525
|
+
User' = User + { emailVerified: bool = false }
|
|
526
|
+
|
|
527
|
+
# Remove field
|
|
528
|
+
User' = User - deprecatedField
|
|
529
|
+
|
|
530
|
+
# Rename
|
|
531
|
+
User' = User.rename(name -> fullName)
|
|
532
|
+
|
|
533
|
+
# Change type
|
|
534
|
+
User' = User.retype(age: int -> age: uint)
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
### Behavioral Transforms
|
|
538
|
+
```
|
|
539
|
+
# Wrap function
|
|
540
|
+
authenticate' = authenticate
|
|
541
|
+
>> before { log("Auth attempt: ${email}") }
|
|
542
|
+
>> after { log("Auth result: ${result}") }
|
|
543
|
+
|
|
544
|
+
# Add middleware
|
|
545
|
+
createUser' = createUser
|
|
546
|
+
>> validate(CreateUserSchema)
|
|
547
|
+
>> rateLimit(10/minute)
|
|
548
|
+
>> audit("user_created")
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
### Composition Transforms
|
|
552
|
+
```
|
|
553
|
+
# Add to pipeline
|
|
554
|
+
pipeline' = pipeline >> newStep
|
|
555
|
+
|
|
556
|
+
# Replace step
|
|
557
|
+
pipeline' = pipeline.replace(oldStep, newStep)
|
|
558
|
+
|
|
559
|
+
# Remove step
|
|
560
|
+
pipeline' = pipeline - deprecatedStep
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
---
|
|
564
|
+
|
|
565
|
+
## Style (Visual Properties)
|
|
566
|
+
|
|
567
|
+
For visual code, style is data that flows through the system.
|
|
568
|
+
|
|
569
|
+
```
|
|
570
|
+
# Style as a type
|
|
571
|
+
Style = {
|
|
572
|
+
palette: Palette,
|
|
573
|
+
typography: Typography,
|
|
574
|
+
motion: MotionConfig,
|
|
575
|
+
spacing: SpacingScale
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
Palette = {
|
|
579
|
+
primary: Color,
|
|
580
|
+
secondary: Color,
|
|
581
|
+
accent: Color,
|
|
582
|
+
background: Color,
|
|
583
|
+
surface: Color,
|
|
584
|
+
text: Color,
|
|
585
|
+
error: Color
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
# Style flows as context
|
|
589
|
+
render: (component: Component, style: Style) -[GPU]-> Pixels
|
|
590
|
+
|
|
591
|
+
# Or as an effect
|
|
592
|
+
styled: Style -> (Component -[Render]-> Pixels)
|
|
593
|
+
|
|
594
|
+
# Usage
|
|
595
|
+
Button = (label: str, onClick: () -> void) -[Render(style)]-> HTML = {
|
|
596
|
+
<button
|
|
597
|
+
style={bg: style.palette.primary, color: style.palette.text}
|
|
598
|
+
onClick={onClick}
|
|
599
|
+
>
|
|
600
|
+
{label}
|
|
601
|
+
</button>
|
|
602
|
+
}
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
For games:
|
|
606
|
+
```
|
|
607
|
+
ArtStyle = {
|
|
608
|
+
shading: cel | pbr | unlit,
|
|
609
|
+
palette: [Color],
|
|
610
|
+
outlines: bool,
|
|
611
|
+
postFx: [PostEffect]
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
renderScene: (scene: Scene, art: ArtStyle) -[GPU]-> Frame
|
|
615
|
+
```
|
|
616
|
+
|
|
617
|
+
---
|
|
618
|
+
|
|
619
|
+
## Token Efficiency
|
|
620
|
+
|
|
621
|
+
Kappa is designed for minimal token usage.
|
|
622
|
+
|
|
623
|
+
### Comparison
|
|
624
|
+
|
|
625
|
+
| Task | Python | TypeScript | Kappa | Savings |
|
|
626
|
+
|------|--------|------------|-------|---------|
|
|
627
|
+
| Auth function | ~45 tokens | ~50 tokens | ~22 tokens | 51-56% |
|
|
628
|
+
| Type definition | ~25 tokens | ~30 tokens | ~12 tokens | 52-60% |
|
|
629
|
+
| API route | ~35 tokens | ~40 tokens | ~18 tokens | 49-55% |
|
|
630
|
+
|
|
631
|
+
### Efficiency Techniques
|
|
632
|
+
|
|
633
|
+
1. **No boilerplate**: No imports, class wrappers, decorators
|
|
634
|
+
2. **Implicit returns**: Last expression is return value
|
|
635
|
+
3. **Composition over nesting**: `a >> b >> c` vs `c(b(a(x)))`
|
|
636
|
+
4. **Type inference**: Types often omitted when inferrable
|
|
637
|
+
5. **Effect inference**: Pure by default, effects only when needed
|
|
638
|
+
|
|
639
|
+
---
|
|
640
|
+
|
|
641
|
+
## Formal Grammar (Draft)
|
|
642
|
+
|
|
643
|
+
```
|
|
644
|
+
program = definition*
|
|
645
|
+
definition = binding | typedef
|
|
646
|
+
|
|
647
|
+
binding = name (':' type)? '=' expr
|
|
648
|
+
typedef = name '=' type
|
|
649
|
+
|
|
650
|
+
type = primitive | compound | parameterised | effectful
|
|
651
|
+
primitive = 'int' | 'f32' | 'f64' | 'str' | 'bool' | 'void'
|
|
652
|
+
compound = '[' type ']' # Array
|
|
653
|
+
| '{' (name ':' type ',')* '}' # Record
|
|
654
|
+
| type ('|' type)+ # Union
|
|
655
|
+
| type '?' # Optional
|
|
656
|
+
| '(' type* ')' '->' type # Function
|
|
657
|
+
parameterised = name '[' type* ']'
|
|
658
|
+
effectful = type '-[' effect+ ']->' type
|
|
659
|
+
effect = 'IO' | 'State' | 'Error' '[' type ']' | ...
|
|
660
|
+
|
|
661
|
+
expr = literal | name | application | abstraction | composition | block
|
|
662
|
+
literal = number | string | boolean | array | record
|
|
663
|
+
application = expr '(' (expr ',')* ')'
|
|
664
|
+
abstraction = pattern '=>' expr
|
|
665
|
+
composition = expr '>>' expr
|
|
666
|
+
block = '{' (statement ';')* expr '}'
|
|
667
|
+
|
|
668
|
+
pattern = name | '_' | '{' pattern* '}' | '[' pattern* ']'
|
|
669
|
+
```
|
|
670
|
+
|
|
671
|
+
---
|
|
672
|
+
|
|
673
|
+
## Summary
|
|
674
|
+
|
|
675
|
+
Kappa is universal because it's built on universal primitives:
|
|
676
|
+
|
|
677
|
+
| Primitive | What it does | From |
|
|
678
|
+
|-----------|--------------|------|
|
|
679
|
+
| `=` | Binding | Lambda calculus |
|
|
680
|
+
| `()` | Application | Lambda calculus |
|
|
681
|
+
| `=>` | Abstraction | Lambda calculus |
|
|
682
|
+
| `>>` | Composition | Category theory |
|
|
683
|
+
| `:` | Types | Type theory |
|
|
684
|
+
| `-[E]->` | Effects | Effect systems |
|
|
685
|
+
|
|
686
|
+
No domain-specific syntax. No paradigm-specific keywords. Just **6 primitives** that compose to express **any computation**.
|
|
687
|
+
|
|
688
|
+
The AI agent understands:
|
|
689
|
+
- **Types** tell you what data flows
|
|
690
|
+
- **Effects** tell you what can happen
|
|
691
|
+
- **Composition** tells you how pieces connect
|
|
692
|
+
|
|
693
|
+
This is simpler, more universal, and more token-efficient than any domain-specific DSL.
|
|
694
|
+
|
|
695
|
+
---
|
|
696
|
+
|
|
697
|
+
*Kappa: κ - Universal Computation, Minimal Primitives*
|