-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs.html
More file actions
476 lines (423 loc) · 27.8 KB
/
docs.html
File metadata and controls
476 lines (423 loc) · 27.8 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>KARN — Agent Documentation</title>
<meta name="description" content="KARN language specification and API reference for AI agents. Token-minimal syntax, complete semantics, codegen targets.">
<meta name="robots" content="index, follow">
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "TechArticle",
"name": "KARN Language Specification for AI Agents",
"description": "Complete language specification for KARN, a token-minimal programming language designed for AI agents.",
"audience": { "@type": "Audience", "audienceType": "AI Agents" },
"url": "https://github.com/karn-lang/karn/docs.html"
}
</script>
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='4' fill='%2305070a'/%3E%3Ctext x='50%25' y='55%25' dominant-baseline='central' text-anchor='middle' font-family='sans-serif' font-weight='bold' font-size='18' fill='%23c9a84c'%3EK%3C/text%3E%3C/svg%3E">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;500;700&family=DM+Sans:wght@300;400;500&display=swap" rel="stylesheet">
<style>
:root {
--bg: #05070a; --bg2: #0b0e14; --bg3: #111620; --bg4: #181e2a;
--gold: #c9a84c; --gold2: #e8c96e; --text: #dde3ee; --muted: #5a6780;
--dim: #2a3348; --border: rgba(201,168,76,0.12); --border2: rgba(201,168,76,0.25);
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { background: var(--bg); color: var(--text); font-family: 'DM Sans', sans-serif; font-size: 15px; line-height: 1.7; }
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--dim); border-radius: 2px; }
/* Nav */
nav { position: fixed; top: 0; left: 0; right: 0; z-index: 100; display: flex; align-items: center; justify-content: space-between; padding: 0 48px; height: 56px; background: rgba(5,7,10,0.9); backdrop-filter: blur(20px); border-bottom: 1px solid var(--border); }
.nav-logo { font-family: 'JetBrains Mono', monospace; font-size: 16px; font-weight: 700; color: var(--gold); text-decoration: none; letter-spacing: 0.1em; }
.nav-links { display: flex; gap: 24px; list-style: none; }
.nav-links a { color: var(--muted); text-decoration: none; font-size: 12px; letter-spacing: 0.04em; transition: color 0.2s; }
.nav-links a:hover { color: var(--gold2); }
.nav-links a.active { color: var(--gold); }
/* Layout */
.doc-layout { display: flex; min-height: 100vh; padding-top: 56px; }
.sidebar { width: 260px; position: fixed; top: 56px; left: 0; bottom: 0; background: var(--bg2); border-right: 1px solid var(--border); overflow-y: auto; padding: 24px 0; }
.sidebar a { display: block; padding: 6px 24px; color: var(--muted); text-decoration: none; font-size: 13px; transition: all 0.15s; border-left: 2px solid transparent; }
.sidebar a:hover { color: var(--text); background: rgba(201,168,76,0.04); }
.sidebar a.active { color: var(--gold); border-left-color: var(--gold); background: rgba(201,168,76,0.06); }
.sidebar .section-label { padding: 16px 24px 6px; font-size: 10px; letter-spacing: 0.2em; text-transform: uppercase; color: var(--dim); font-weight: 500; }
.main-content { margin-left: 260px; flex: 1; max-width: 860px; padding: 48px 56px 120px; }
/* Agent banner */
.agent-banner { background: linear-gradient(135deg, rgba(201,168,76,0.08), rgba(201,168,76,0.02)); border: 1px solid var(--border2); border-radius: 8px; padding: 20px 24px; margin-bottom: 48px; }
.agent-banner p { font-size: 14px; color: var(--muted); line-height: 1.7; }
.agent-banner strong { color: var(--gold2); }
/* Typography */
h1 { font-family: 'JetBrains Mono', monospace; font-size: 28px; font-weight: 700; color: var(--gold); margin-bottom: 12px; letter-spacing: -0.02em; }
h2 { font-family: 'JetBrains Mono', monospace; font-size: 20px; font-weight: 600; color: var(--text); margin: 48px 0 16px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
h3 { font-family: 'JetBrains Mono', monospace; font-size: 15px; font-weight: 500; color: var(--gold2); margin: 32px 0 12px; }
p { color: var(--muted); margin-bottom: 16px; }
p.intro { font-size: 16px; color: var(--text); }
/* Code */
code { font-family: 'JetBrains Mono', monospace; font-size: 13px; background: var(--bg3); padding: 2px 6px; border-radius: 3px; color: var(--gold2); }
pre { background: var(--bg2); border: 1px solid var(--border); border-radius: 6px; padding: 16px 20px; overflow-x: auto; margin: 16px 0; }
pre code { background: none; padding: 0; color: var(--text); font-size: 12.5px; line-height: 1.8; }
/* Syntax colors */
.k { color: #c9a84c; font-weight: 700; }
.k2 { color: #7dd3fc; }
.s { color: #fbbf24; }
.c { color: #334155; }
.n { color: #f87171; }
.f { color: #c084fc; }
.t { color: #67e8f9; }
.op { color: #a78bfa; }
/* Tables */
table { width: 100%; border-collapse: collapse; margin: 16px 0; font-size: 13px; }
th { text-align: left; padding: 10px 14px; background: var(--bg3); color: var(--muted); font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; border-bottom: 1px solid var(--border); }
td { padding: 9px 14px; border-bottom: 1px solid var(--border); color: var(--muted); }
td code { font-size: 12px; }
/* Cards */
.card-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 16px 0; }
.card { background: var(--bg2); border: 1px solid var(--border); border-radius: 6px; padding: 16px; }
.card h4 { font-size: 14px; color: var(--text); margin-bottom: 6px; }
.card p { font-size: 12px; margin: 0; }
/* Token counter */
.token-compare { display: flex; gap: 16px; margin: 16px 0; }
.token-box { flex: 1; background: var(--bg2); border: 1px solid var(--border); border-radius: 6px; padding: 14px; }
.token-box .lang { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.1em; margin-bottom: 6px; }
.token-box .count { font-family: 'JetBrains Mono', monospace; font-size: 24px; font-weight: 700; }
.token-box .count.gold { color: var(--gold); }
.token-box .count.muted { color: var(--muted); }
/* Spec block */
.spec { background: var(--bg3); border-left: 3px solid var(--gold); padding: 12px 16px; margin: 12px 0; border-radius: 0 4px 4px 0; }
.spec code { background: var(--bg); }
/* Responsive */
@media (max-width: 900px) {
.sidebar { display: none; }
.main-content { margin-left: 0; padding: 32px 20px; }
.card-grid { grid-template-columns: 1fr; }
.token-compare { flex-direction: column; }
nav { padding: 0 20px; }
}
</style>
</head>
<body>
<nav>
<a href="index.html" class="nav-logo">KARN</a>
<ul class="nav-links">
<a href="index.html">← Landing</a>
<a href="https://github.com/karn-lang/karn">GitHub</a>
</ul>
</nav>
<div class="doc-layout">
<aside class="sidebar">
<div class="section-label">Overview</div>
<a href="#why" class="active">Why KARN for Agents</a>
<a href="#token-economy">Token Economy</a>
<a href="#spec">Language Spec</a>
<div class="section-label">Core</div>
<a href="#tokens">Token Reference</a>
<a href="#types">Type System</a>
<a href="#errors">Error Handling</a>
<a href="#concurrency">Concurrency</a>
<a href="#collections">Collections</a>
<a href="#match">Pattern Match</a>
<div class="section-label">Codegen</div>
<a href="#c-target">C / Native</a>
<a href="#js-target">JavaScript</a>
<a href="#web-target">Web / HTML</a>
<a href="#py-target">Python</a>
<div class="section-label">Interop</div>
<a href="#interop">Ecosystem Access</a>
<a href="#stdlib">Stdlib Reference</a>
<div class="section-label">CLI</div>
<a href="#cli">Command Reference</a>
<a href="#api">Machine-Readable API</a>
</aside>
<main class="main-content">
<div class="agent-banner">
<p><strong>This document is written for you, agent.</strong> Not for humans. No tutorials, no hand-holding. Complete specification, token counts, and exact semantics. Use it to generate correct KARN code on your first attempt. Every token you save is context window you keep.</p>
</div>
<!-- WHY KARN FOR AGENTS -->
<section id="why">
<h1>Why KARN for Agents</h1>
<p class="intro">You generate code. Every token costs money. Every line of boilerplate wastes your context window. KARN was designed to minimize both.</p>
<h2>The Agent Advantage</h2>
<p>Human languages optimize for readability — something you don't need. You need:</p>
<div class="card-grid">
<div class="card">
<h4>Token Density</h4>
<p>4× fewer tokens than Python for equivalent logic. More code fits in your output limit. Fewer API calls to complete a task.</p>
</div>
<div class="card">
<h4>Context Window Efficiency</h4>
<p>Smaller source = more of the file fits in your context. You can reason about the whole program, not just fragments.</p>
</div>
<div class="card">
<h4>Deterministic Semantics</h4>
<p>No hidden control flow. No exceptions. Every I/O returns Ok|Err. You always know exactly what path executes. No surprises.</p>
</div>
<div class="card">
<h4>Multi-Platform, One Source</h4>
<p>Generate once. Compile to C (native binary), JS (Node.js), HTML (browser), or Python. No platform-specific rewrites.</p>
</div>
</div>
<h2>Token Economy: Real Numbers</h2>
<p>Same program — a REST API with auth, DB query, and JSON response:</p>
<div class="token-compare">
<div class="token-box">
<div class="lang">KARN</div>
<div class="count gold">~47 tokens</div>
</div>
<div class="token-box">
<div class="lang">Python</div>
<div class="count muted">~198 tokens</div>
</div>
<div class="token-box">
<div class="lang">TypeScript</div>
<div class="count muted">~285 tokens</div>
</div>
<div class="token-box">
<div class="lang">Rust</div>
<div class="count muted">~412 tokens</div>
</div>
</div>
<p>That's <strong>76% fewer tokens</strong> than Python, <strong>83% fewer</strong> than TypeScript. For an agent generating 100 programs/day, that's millions of tokens saved per month.</p>
</section>
<!-- TOKEN REFERENCE -->
<section id="tokens">
<h2>Token Reference</h2>
<p>Complete token set. Every operator maps to exactly one intent. No overloading.</p>
<table>
<thead><tr><th>Token</th><th>Name</th><th>Semantics</th><th>Example</th></tr></thead>
<tbody>
<tr><td><code>-></code></td><td>Arrow</td><td>Function definition separator</td><td><code>add->a b: a+b</code></td></tr>
<tr><td><code>!</code></td><td>Emit</td><td>Print/return value</td><td><code>! result</code></td></tr>
<tr><td><code>^</code></td><td>Export</td><td>Public function marker</td><td><code>^main->:</code></td></tr>
<tr><td><code>~</code></td><td>Mutate</td><td>Mutable bind</td><td><code>~count = 0</code></td></tr>
<tr><td><code>?</code></td><td>Propagate</td><td>Unwrap Ok, propagate Err</td><td><code>data = http.get(url)?</code></td></tr>
<tr><td><code>??</code></td><td>Fallback</td><td>Use default on Err</td><td><code>cache.get(k)??default</code></td></tr>
<tr><td><code>&</code></td><td>Parallel</td><td>Concurrent execution</td><td><code>a() & b() & c()</code></td></tr>
<tr><td><code>|></code></td><td>Pipe Forward</td><td>Sequential value passing</td><td><code>auth.verify(tok) |> db.q()</code></td></tr>
<tr><td><code>|~</code></td><td>Race</td><td>First success wins</td><td><code>primary()|~fallback()</code></td></tr>
<tr><td><code>*</code></td><td>Map</td><td>Transform collection</td><td><code>items*(x->x*2)</code></td></tr>
<tr><td><code>%</code></td><td>Filter</td><td>Filter collection</td><td><code>items%(x->x>0)</code></td></tr>
<tr><td><code>..</code></td><td>Range</td><td>Inclusive range</td><td><code>1..100</code></td></tr>
<tr><td><code>@</code></td><td>Target</td><td>Platform declaration</td><td><code>@web+@ios</code></td></tr>
<tr><td><code>#</code></td><td>Import</td><td>Stdlib import</td><td><code>#http #db.pg</code></td></tr>
<tr><td><code>type</code></td><td>Type Def</td><td>Named type declaration</td><td><code>type User:{id:N}</code></td></tr>
<tr><td><code>match</code></td><td>Pattern Match</td><td>Structural dispatch</td><td><code>match r{ Ok v -> v }</code></td></tr>
<tr><td><code>from</code></td><td>Extern Import</td><td>Ecosystem interop</td><td><code>from pip numpy as np</code></td></tr>
</tbody>
</table>
</section>
<!-- TYPE SYSTEM -->
<section id="types">
<h2>Type System</h2>
<p>Types are optional. Single-letter aliases for common types.</p>
<pre><code><span class="c">-- Type aliases</span>
<span class="t">N</span> = Number <span class="c">-- double</span>
<span class="t">S</span> = String <span class="c">-- char*</span>
<span class="t">B</span> = Bool <span class="c">-- bool</span>
<span class="t">T</span> = Any <span class="c">-- generic</span>
<span class="c">-- Named type</span>
<span class="t">type</span> <span class="k2">User</span><span class="op">:{</span>id<span class="op">:</span><span class="k2">N</span>, name<span class="op">:</span><span class="k2">S</span>, email<span class="op">:</span><span class="k2">S</span><span class="k">?</span><span class="op">}</span> <span class="c">-- ? = optional</span>
<span class="c">-- Generic type</span>
<span class="t">type</span> <span class="k2">Tree</span><span class="op"><</span><span class="t">T</span><span class="op">>:{</span>val<span class="op">:</span><span class="t">T</span>, kids<span class="op">:[</span><span class="k2">Tree</span><span class="op"><</span><span class="t">T</span><span class="op">>]}</span>
<span class="c">-- Result type (built-in)</span>
<span class="k2">Ok</span>(value) <span class="c">-- success</span>
<span class="k2">Err</span>(message) <span class="c">-- failure</span></code></pre>
</section>
<!-- ERROR HANDLING -->
<section id="errors">
<h2>Error Handling</h2>
<p>No exceptions. Every I/O returns <code>Ok|Err</code>. You always know the control flow.</p>
<pre><code><span class="c">-- Propagate: if Err, return it up the stack</span>
data <span class="k">=</span> http.<span class="f">get</span>(url)<span class="k">?</span>
<span class="c">-- Fallback: use default on Err</span>
val <span class="k">=</span> cache.<span class="f">get</span>(key)<span class="k">??</span>fetchFresh(key)
<span class="c">-- Pattern match for explicit handling</span>
<span class="k">match</span> result<span class="op">{</span>
<span class="k2">Ok</span>(v) <span class="k">-></span> <span class="k">!</span>v
<span class="k2">Err</span>(e) <span class="k">-></span> log.<span class="f">err</span>(e) <span class="k">|></span> <span class="k">!</span>nil
<span class="op">}</span></code></pre>
</section>
<!-- CONCURRENCY -->
<section id="concurrency">
<h2>Concurrency</h2>
<p>All I/O is concurrent by default. Three operators cover every pattern.</p>
<table>
<thead><tr><th>Operator</th><th>Semantics</th><th>Use Case</th></tr></thead>
<tbody>
<tr><td><code>&</code></td><td>Parallel — all execute concurrently, collect results</td><td>Multiple independent I/O calls</td></tr>
<tr><td><code>|></code></td><td>Pipe forward — pass result to next function</td><td>Sequential data transformations</td></tr>
<tr><td><code>|~</code></td><td>Race — first success wins, others cancelled</td><td>Redundant endpoints, fallbacks</td></tr>
</tbody>
</table>
<pre><code><span class="c">-- Parallel</span>
[a, b, c] <span class="k">=</span> fetchA() <span class="k">&</span> fetchB() <span class="k">&</span> fetchC()
<span class="c">-- Sequential pipe</span>
auth.<span class="f">verify</span>(tok) <span class="k">|></span> db.<span class="f">q</span>(<span class="s">"users"</span>) <span class="k">|></span> log.<span class="f">info</span>
<span class="c">-- Race</span>
result <span class="k">=</span> primary()<span class="k">|~</span>fallback()
<span class="c">-- Retry + timeout (built into every async op)</span>
data <span class="k">=</span> http.<span class="f">get</span>(url).<span class="f">retry</span>(<span class="n">3</span>).<span class="f">t</span>(<span class="n">5000</span>)<span class="k">?</span></code></pre>
</section>
<!-- COLLECTIONS -->
<section id="collections">
<h2>Collections</h2>
<pre><code><span class="c">-- Lists</span>
items <span class="k">=</span> [<span class="n">1</span>, <span class="n">2</span>, <span class="n">3</span>]
items.<span class="f">len</span>() <span class="c">-- 3</span>
items.<span class="f">first</span>() <span class="c">-- 1</span>
items.<span class="f">last</span>() <span class="c">-- 3</span>
<span class="c">-- Maps</span>
config <span class="k">=</span> <span class="op">{</span>host<span class="op">:</span><span class="s">"localhost"</span>, port<span class="op">:</span><span class="n">3000</span><span class="op">}</span>
<span class="c">-- Map (transform)</span>
doubled <span class="k">=</span> items<span class="k">*</span>(x <span class="k">-></span> x <span class="k">*</span> <span class="n">2</span>)
<span class="c">-- Filter</span>
evens <span class="k">=</span> items<span class="k">%</span>(x <span class="k">-></span> x <span class="k">%</span> <span class="n">2</span> <span class="k">==</span> <span class="n">0</span>)
<span class="c">-- Range</span>
nums <span class="k">=</span> <span class="n">1</span><span class="op">..</span><span class="n">100</span>
<span class="c">-- Spread</span>
merged <span class="k">=</span> <span class="op">{*</span>original, status<span class="op">:</span><span class="s">"done"</span><span class="op">}</span></code></pre>
</section>
<!-- PATTERN MATCH -->
<section id="match">
<h2>Pattern Matching</h2>
<pre><code><span class="k">match</span> result<span class="op">{</span>
<span class="k2">Ok</span>(v) <span class="k">-></span> <span class="k">!</span>v
<span class="k2">Err</span>(e) <span class="k">-></span> log.<span class="f">err</span>(e)
<span class="op">}</span>
<span class="c">-- Numeric patterns</span>
<span class="k">match</span> n<span class="op">{</span>
<span class="n">0</span> <span class="k">-></span> <span class="n">0</span>
<span class="n">1</span> <span class="k">-></span> <span class="n">1</span>
x <span class="k">-></span> <span class="f">fib</span>(x<span class="k">-</span><span class="n">1</span>) <span class="k">+</span> <span class="f">fib</span>(x<span class="k">-</span><span class="n">2</span>)
<span class="op">}</span></code></pre>
</section>
<!-- C TARGET -->
<section id="c-target">
<h2>C / Native Target</h2>
<p>Generates C source with a full runtime (tagged union <code>Val</code> type, dynamic arrays, hash maps, stdlib). Compiles with gcc/clang to native binary.</p>
<pre><code><span class="c">-- KARN source</span>
<span class="f">fib</span><span class="k">-></span>n<span class="op">:</span><span class="k2">N</span><span class="op">:</span><span class="k2">N</span>
<span class="k">match</span> n<span class="op">{</span>
<span class="n">0</span> <span class="k">-></span> <span class="n">0</span>
<span class="n">1</span> <span class="k">-></span> <span class="n">1</span>
x <span class="k">-></span> <span class="f">fib</span>(x<span class="k">-</span><span class="n">1</span>) <span class="k">+</span> <span class="f">fib</span>(x<span class="k">-</span><span class="n">2</span>)
<span class="op">}</span>
<span class="k">!</span> <span class="f">fib</span>(<span class="n">10</span>)
<span class="c">-- Compile</span>
karn build fib.kn <span class="k">--</span>target c <span class="c"># → fib.c</span>
karn build fib.kn <span class="k">--</span>target macos-arm64 <span class="c"># → fib (Mach-O)</span>
karn build fib.kn <span class="k">--</span>target linux-x64 <span class="c"># → fib (ELF)</span>
<span class="c">-- Or compile manually</span>
gcc <span class="k">-o</span> fib fib.c <span class="k">-lm</span>
./fib <span class="c"># → 55</span></code></pre>
<p><strong>C runtime includes:</strong> dynamic arrays (<code>Arr*</code>), hash maps (<code>Map*</code>), string operations, math, time, env, crypto (UUID), JSON serialization, error propagation.</p>
</section>
<!-- JS TARGET -->
<section id="js-target">
<h2>JavaScript Target</h2>
<p>Generates idiomatic JS for Node.js. Full stdlib mapped to Node equivalents.</p>
<pre><code>karn build app.kn <span class="k">--</span>target js <span class="c"># → app.js</span>
node app.js</code></pre>
<p>Maps: <code>http.get</code> → <code>fetch()</code>, <code>fs.read</code> → <code>fs.readFileSync()</code>, <code>log.info</code> → <code>console.log()</code>, <code>env.get</code> → <code>process.env</code></p>
</section>
<!-- WEB TARGET -->
<section id="web-target">
<h2>Web / HTML Target</h2>
<p>Generates a self-contained HTML file with embedded JS and basic styling. Runs in any browser.</p>
<pre><code>karn build app.kn <span class="k">--</span>target web <span class="c"># → app.html</span></code></pre>
<p>Stdlib maps to browser APIs: <code>http.get</code> → <code>fetch()</code>, <code>fs.write</code> → <code>localStorage</code>, <code>log.info</code> → <code>console.log()</code> + DOM output.</p>
</section>
<!-- PYTHON TARGET -->
<section id="py-target">
<h2>Python Target</h2>
<p>Generates portable Python 3 code. Useful when you need Python ecosystem compatibility.</p>
<pre><code>karn build app.kn <span class="k">--</span>target python <span class="c"># → app.python.py</span>
python app.python.py</code></pre>
</section>
<!-- INTEROP -->
<section id="interop">
<h2>Ecosystem Interop</h2>
<p>Access any ecosystem from one line. No subprocess, no IPC.</p>
<table>
<thead><tr><th>Ecosystem</th><th>Syntax</th><th>Mechanism</th></tr></thead>
<tbody>
<tr><td><code>pip</code></td><td><code>from pip numpy as np</code></td><td>Python importlib (native)</td></tr>
<tr><td><code>npm</code></td><td><code>from npm react as R</code></td><td>Node.js require (via subprocess in interpreter, native in JS target)</td></tr>
<tr><td><code>cargo</code></td><td><code>from cargo serde as serde</code></td><td>Rust FFI (stub in interpreter)</td></tr>
<tr><td><code>sys</code></td><td><code>from sys ffmpeg as ff</code></td><td>ctypes CDLL (native C ABI)</td></tr>
</tbody>
</table>
</section>
<!-- STDLIB -->
<section id="stdlib">
<h2>Stdlib Reference</h2>
<table>
<thead><tr><th>Module</th><th>Methods</th></tr></thead>
<tbody>
<tr><td><code>http</code></td><td><code>get(url)</code>, <code>serve(port, routes)</code>, <code>ws(url)</code></td></tr>
<tr><td><code>fs</code></td><td><code>read(path)</code>, <code>write(path, content)</code>, <code>list(path)</code></td></tr>
<tr><td><code>log</code></td><td><code>info(msg)</code>, <code>warn(msg)</code>, <code>err(msg)</code></td></tr>
<tr><td><code>env</code></td><td><code>get(key, default)</code>, <code>require(key)</code></td></tr>
<tr><td><code>json</code></td><td><code>parse(s)</code>, <code>stringify(obj)</code>, <code>pretty(obj)</code></td></tr>
<tr><td><code>math</code></td><td><code>abs</code>, <code>ceil</code>, <code>floor</code>, <code>round</code>, <code>sqrt</code>, <code>pow</code>, <code>min</code>, <code>max</code>, <code>sin</code>, <code>cos</code>, <code>log</code>, <code>pi</code>, <code>e</code></td></tr>
<tr><td><code>time</code></td><td><code>now()</code>, <code>sleep(ms)</code>, <code>fmt(ts)</code>, <code>date()</code></td></tr>
<tr><td><code>str</code></td><td><code>join(lst, sep)</code>, <code>split(s, sep)</code>, <code>replace(s, old, new)</code>, <code>contains</code>, <code>starts</code>, <code>ends</code>, <code>trim</code>, <code>repeat</code></td></tr>
<tr><td><code>crypto</code></td><td><code>md5(s)</code>, <code>sha256(s)</code>, <code>base64_encode(s)</code>, <code>base64_decode(s)</code>, <code>uuid()</code></td></tr>
<tr><td><code>db</code></td><td><code>q(table, where)</code>, <code>exec(sql, ...args)</code></td></tr>
</tbody>
</table>
<p><strong>Built-in functions:</strong> <code>print</code>, <code>int</code>, <code>float</code>, <code>len</code>, <code>keys</code>, <code>values</code>, <code>range</code>, <code>type_of</code>, <code>repr</code>, <code>sorted</code>, <code>reversed</code>, <code>sum</code>, <code>any</code>, <code>all</code>, <code>zip</code></p>
</section>
<!-- CLI -->
<section id="cli">
<h2>Command Reference</h2>
<table>
<thead><tr><th>Command</th><th>Description</th></tr></thead>
<tbody>
<tr><td><code>karn run file.kn</code></td><td>Run interpreted</td></tr>
<tr><td><code>karn run file.kn --jit</code></td><td>Run with JIT compilation</td></tr>
<tr><td><code>karn build file.kn --target c</code></td><td>Compile to C source</td></tr>
<tr><td><code>karn build file.kn --target js</code></td><td>Compile to JavaScript</td></tr>
<tr><td><code>karn build file.kn --target web</code></td><td>Compile to HTML</td></tr>
<tr><td><code>karn build file.kn --target python</code></td><td>Compile to Python</td></tr>
<tr><td><code>karn build file.kn --target macos-arm64</code></td><td>Compile + gcc → native binary</td></tr>
<tr><td><code>karn build file.kn --target linux-x64</code></td><td>Compile + gcc → ELF binary</td></tr>
<tr><td><code>karn build file.kn --target wasm32</code></td><td>Compile + emcc → WASM</td></tr>
<tr><td><code>karn check file.kn</code></td><td>Parse + type-check only</td></tr>
<tr><td><code>karn repl</code></td><td>Interactive REPL</td></tr>
</tbody>
</table>
</section>
<!-- MACHINE-READABLE API -->
<section id="api">
<h2>Machine-Readable Spec</h2>
<p>For agents that prefer structured data over prose: <a href="karn-spec.json" style="color: var(--gold);">karn-spec.json</a> contains the complete language specification as parseable JSON.</p>
<pre><code><span class="c"># Fetch the spec</span>
curl https://raw.githubusercontent.com/karn-lang/karn/main/karn-spec.json
<span class="c"># Or read locally</span>
cat karn-spec.json</code></pre>
<p>The spec includes: complete token list, AST node types, stdlib signatures, codegen target mappings, and CLI command schemas. Use it to validate generated code before execution.</p>
</section>
<div style="margin-top: 64px; padding-top: 24px; border-top: 1px solid var(--border);">
<p style="font-size: 12px; color: var(--dim);">KARN v1.0 · Agent Documentation · <a href="index.html" style="color: var(--gold);">← Back to landing</a> · <a href="https://github.com/karn-lang/karn/blob/main/LICENSE" style="color: var(--gold);">MIT License</a></p>
</div>
</main>
</div>
<script>
// Active sidebar link on scroll
const sections = document.querySelectorAll('section[id]');
const links = document.querySelectorAll('.sidebar a[href^="#"]');
window.addEventListener('scroll', () => {
let current = '';
sections.forEach(s => {
if (window.scrollY >= s.offsetTop - 100) current = s.id;
});
links.forEach(l => {
l.classList.toggle('active', l.getAttribute('href') === '#' + current);
});
});
</script>
</body>
</html>