Skip to content

Commit 153f5c6

Browse files
introspection: +9 helper builtins + 14 tests
Compact-form helpers for LLM round-trips that want less data: omc_help_brief(name) -> string (sig + desc, no example) omc_help_signature(name) -> string (just the signature) omc_help_example(name) -> string (just the example) omc_help_category(name) -> string (just the category) omc_is_unique(name) -> int (1 if OMC-unique) omc_count_in_category(c) -> int (population count) omc_random_builtin() -> string (random name) omc_random_unique_builtin() -> string (random OMC-unique name) omc_token_vocab_dump(n?) -> string (first N vocab entries) For an LLM the workflow becomes: - omc_help_signature for quick disambiguation - omc_help_example when stuck - omc_help_brief for batch exploration - omc_random_unique_builtin to discover differentiators Tests: 14 covering each helper + known/unknown lookup, category- filter sanity, vocab-dump default vs explicit size. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent fb5fb03 commit 153f5c6

5 files changed

Lines changed: 330 additions & 3 deletions

File tree

OMC_REFERENCE.md

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Auto-generated from `omnimcode-core/src/docs.rs`. Run `omc --gen-docs > OMC_REFERENCE.md` to regenerate.
44

5-
**Total documented builtins**: 619
5+
**Total documented builtins**: 628
66

77
**OMC-unique**: 63 (no direct Python/NumPy equivalent — these are why you reach for OMC over numpy)
88

@@ -23,8 +23,8 @@ Auto-generated from `omnimcode-core/src/docs.rs`. Run `omc --gen-docs > OMC_REFE
2323
- [json](#json) (2 builtins)
2424
- [stdlib](#stdlib) (26 builtins)
2525
- [exceptions](#exceptions) (2 builtins)
26-
- [introspection](#introspection) (22 builtins)
27-
- [tokenizer](#tokenizer) (16 builtins)
26+
- [introspection](#introspection) (30 builtins)
27+
- [tokenizer](#tokenizer) (17 builtins)
2828
- [code_intel](#code_intel) (16 builtins)
2929
- [llm_workflow](#llm_workflow) (7 builtins)
3030
- [math](#math) (82 builtins)
@@ -4510,6 +4510,86 @@ Substring search across name + description. Find what you don't know the name of
45104510
omc_search_builtins("softmax") // ["arr_softmax"]
45114511
```
45124512

4513+
### `omc_help_brief`
4514+
4515+
**Signature**: `(name: string) -> string`
4516+
4517+
Compact help: signature + description only (no example). For dense scan.
4518+
4519+
```omc
4520+
omc_help_brief("arr_softmax")
4521+
```
4522+
4523+
### `omc_help_signature`
4524+
4525+
**Signature**: `(name: string) -> string`
4526+
4527+
Just the signature string. Compactest possible.
4528+
4529+
```omc
4530+
omc_help_signature("arr_get") // "(arr, index) -> any"
4531+
```
4532+
4533+
### `omc_help_example`
4534+
4535+
**Signature**: `(name: string) -> string`
4536+
4537+
Just the example for a builtin.
4538+
4539+
```omc
4540+
omc_help_example("arr_softmax")
4541+
```
4542+
4543+
### `omc_help_category`
4544+
4545+
**Signature**: `(name: string) -> string`
4546+
4547+
Just the category for a builtin.
4548+
4549+
```omc
4550+
omc_help_category("arr_softmax") // "ml_kernels"
4551+
```
4552+
4553+
### `omc_is_unique`
4554+
4555+
**Signature**: `(name: string) -> int`
4556+
4557+
1 if the builtin is flagged unique_to_omc.
4558+
4559+
```omc
4560+
omc_is_unique("is_attractor") // 1
4561+
```
4562+
4563+
### `omc_count_in_category`
4564+
4565+
**Signature**: `(category: string) -> int`
4566+
4567+
Builtin count in a given category.
4568+
4569+
```omc
4570+
omc_count_in_category("substrate") // ~25
4571+
```
4572+
4573+
### `omc_random_builtin`
4574+
4575+
**Signature**: `() -> string`
4576+
4577+
A random builtin name. Useful for exploring or fuzzing.
4578+
4579+
```omc
4580+
omc_random_builtin() // "arr_zip"
4581+
```
4582+
4583+
### `omc_random_unique_builtin`
4584+
4585+
**Signature**: `() -> string`
4586+
4587+
A random OMC-unique builtin name. For learning the differentiators.
4588+
4589+
```omc
4590+
omc_random_unique_builtin() // "arr_substrate_attention"
4591+
```
4592+
45134593
### `cleanup_array`
45144594

45154595
**Signature**: `(...) -> any`
@@ -4694,6 +4774,16 @@ omc_token_byte_savings("arr_softmax") // 10 (11 bytes -> 1 token)
46944774
omc_token_compress_pct("arr_softmax") // ~90.9
46954775
```
46964776

4777+
### `omc_token_vocab_dump`
4778+
4779+
**Signature**: `(n?: int) -> string`
4780+
4781+
First N entries of the token vocabulary as numbered list. Default n=50.
4782+
4783+
```omc
4784+
omc_token_vocab_dump(10) // first 10 entries
4785+
```
4786+
46974787
---
46984788

46994789
## code_intel
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Tests for the new introspection helper builtins.
2+
3+
fn assert_eq(actual, expected, msg) {
4+
if actual != expected {
5+
test_record_failure(msg + ": expected " + to_string(expected) + " got " + to_string(actual));
6+
}
7+
}
8+
9+
fn assert_true(cond, msg) { if !cond { test_record_failure(msg); } }
10+
11+
fn test_help_brief() {
12+
h s = omc_help_brief("arr_softmax");
13+
assert_true(str_len(s) > 10, "brief non-empty");
14+
assert_true(re_match("arr_softmax", s) == 1, "mentions name");
15+
}
16+
17+
fn test_help_brief_unknown() {
18+
h s = omc_help_brief("zzz_no_such");
19+
assert_true(re_match("not in registry", s) == 1, "explains miss");
20+
}
21+
22+
fn test_help_signature() {
23+
h s = omc_help_signature("arr_softmax");
24+
assert_true(str_len(s) > 0, "has signature");
25+
assert_true(re_match("float", s) == 1, "mentions float");
26+
}
27+
28+
fn test_help_signature_unknown() {
29+
h s = omc_help_signature("doesnotexist");
30+
assert_eq(s, "", "empty for unknown");
31+
}
32+
33+
fn test_help_example() {
34+
h s = omc_help_example("arr_softmax");
35+
assert_true(str_len(s) > 0, "non-empty");
36+
}
37+
38+
fn test_help_category() {
39+
assert_eq(omc_help_category("arr_softmax"), "ml_kernels", "ml_kernels");
40+
assert_eq(omc_help_category("is_attractor"), "substrate", "substrate");
41+
}
42+
43+
fn test_is_unique_yes() {
44+
assert_eq(omc_is_unique("is_attractor"), 1, "is_attractor unique");
45+
}
46+
47+
fn test_is_unique_no() {
48+
assert_eq(omc_is_unique("arr_push"), 0, "arr_push not unique");
49+
}
50+
51+
fn test_count_in_category_substrate() {
52+
h n = omc_count_in_category("substrate");
53+
assert_true(n >= 5, "substrate has many");
54+
}
55+
56+
fn test_count_in_category_unknown() {
57+
h n = omc_count_in_category("nonexistent_xyz");
58+
assert_eq(n, 0, "unknown → 0");
59+
}
60+
61+
fn test_random_builtin_returns_name() {
62+
h n = omc_random_builtin();
63+
assert_true(str_len(n) > 0, "non-empty");
64+
}
65+
66+
fn test_random_unique_is_unique() {
67+
h n = omc_random_unique_builtin();
68+
assert_eq(omc_is_unique(n), 1, "is unique");
69+
}
70+
71+
fn test_vocab_dump_default() {
72+
h s = omc_token_vocab_dump();
73+
assert_true(str_len(s) > 100, "has many entries");
74+
}
75+
76+
fn test_vocab_dump_small() {
77+
h s = omc_token_vocab_dump(5);
78+
# 5 entries means 5 numbered lines
79+
h lines = str_split(s, "\n");
80+
assert_true(arr_len(lines) >= 5, "at least 5 lines");
81+
}

omnimcode-core/src/compiler.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ impl Compiler {
203203
| "omc_unique_count"
204204
| "omc_token_byte_savings" | "omc_remember"
205205
| "omc_recall_matches" | "omc_hbit_hash"
206+
| "omc_is_unique" | "omc_count_in_category"
206207
// tape_* op constructors return node IDs (int)
207208
| "tape_var" | "tape_const"
208209
| "tape_add" | "tape_sub" | "tape_mul" | "tape_div"

omnimcode-core/src/docs.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,69 @@ pub const BUILTINS: &[BuiltinDoc] = &[
11231123
example: "omc_id(src) // \"omcid-12345-abcd\"",
11241124
unique_to_omc: true,
11251125
},
1126+
BuiltinDoc {
1127+
name: "omc_token_vocab_dump", category: "tokenizer",
1128+
signature: "(n?: int) -> string",
1129+
description: "First N entries of the token vocabulary as numbered list. Default n=50.",
1130+
example: "omc_token_vocab_dump(10) // first 10 entries",
1131+
unique_to_omc: false,
1132+
},
1133+
BuiltinDoc {
1134+
name: "omc_help_brief", category: "introspection",
1135+
signature: "(name: string) -> string",
1136+
description: "Compact help: signature + description only (no example). For dense scan.",
1137+
example: "omc_help_brief(\"arr_softmax\")",
1138+
unique_to_omc: false,
1139+
},
1140+
BuiltinDoc {
1141+
name: "omc_help_signature", category: "introspection",
1142+
signature: "(name: string) -> string",
1143+
description: "Just the signature string. Compactest possible.",
1144+
example: "omc_help_signature(\"arr_get\") // \"(arr, index) -> any\"",
1145+
unique_to_omc: false,
1146+
},
1147+
BuiltinDoc {
1148+
name: "omc_help_example", category: "introspection",
1149+
signature: "(name: string) -> string",
1150+
description: "Just the example for a builtin.",
1151+
example: "omc_help_example(\"arr_softmax\")",
1152+
unique_to_omc: false,
1153+
},
1154+
BuiltinDoc {
1155+
name: "omc_help_category", category: "introspection",
1156+
signature: "(name: string) -> string",
1157+
description: "Just the category for a builtin.",
1158+
example: "omc_help_category(\"arr_softmax\") // \"ml_kernels\"",
1159+
unique_to_omc: false,
1160+
},
1161+
BuiltinDoc {
1162+
name: "omc_is_unique", category: "introspection",
1163+
signature: "(name: string) -> int",
1164+
description: "1 if the builtin is flagged unique_to_omc.",
1165+
example: "omc_is_unique(\"is_attractor\") // 1",
1166+
unique_to_omc: false,
1167+
},
1168+
BuiltinDoc {
1169+
name: "omc_count_in_category", category: "introspection",
1170+
signature: "(category: string) -> int",
1171+
description: "Builtin count in a given category.",
1172+
example: "omc_count_in_category(\"substrate\") // ~25",
1173+
unique_to_omc: false,
1174+
},
1175+
BuiltinDoc {
1176+
name: "omc_random_builtin", category: "introspection",
1177+
signature: "() -> string",
1178+
description: "A random builtin name. Useful for exploring or fuzzing.",
1179+
example: "omc_random_builtin() // \"arr_zip\"",
1180+
unique_to_omc: false,
1181+
},
1182+
BuiltinDoc {
1183+
name: "omc_random_unique_builtin", category: "introspection",
1184+
signature: "() -> string",
1185+
description: "A random OMC-unique builtin name. For learning the differentiators.",
1186+
example: "omc_random_unique_builtin() // \"arr_substrate_attention\"",
1187+
unique_to_omc: false,
1188+
},
11261189
// ---- Auto-generated docs for previously-undocumented builtins ----
11271190
// Each entry covers one runtime builtin that lacked introspection.
11281191
// Stubs are conservative — refine as you learn the actual signatures.

omnimcode-core/src/interpreter.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7793,6 +7793,98 @@ impl Interpreter {
77937793
}
77947794
Ok(Value::dict_from(map))
77957795
}
7796+
"omc_token_vocab_dump" => {
7797+
// First N entries of vocab as a numbered list.
7798+
let n = if !args.is_empty() {
7799+
self.eval_expr(&args[0])?.to_int().max(0) as usize
7800+
} else { 50 };
7801+
let mut s = String::new();
7802+
let len = crate::tokenizer::TOKEN_DICT.len().min(n);
7803+
for (i, entry) in crate::tokenizer::TOKEN_DICT.iter().take(len).enumerate() {
7804+
let display = entry.replace('\n', "\\n").replace('\t', "\\t");
7805+
s.push_str(&format!("{:4}: {:?}\n", i, display));
7806+
}
7807+
Ok(Value::String(s))
7808+
}
7809+
"omc_help_brief" => {
7810+
// Just signature + one-line description (no example). Useful
7811+
// when the LLM wants a compact view across many builtins.
7812+
if args.is_empty() {
7813+
return Err("omc_help_brief requires (name)".to_string());
7814+
}
7815+
let name = self.eval_expr(&args[0])?.to_display_string();
7816+
match crate::docs::lookup(&name) {
7817+
Some(d) => Ok(Value::String(format!(
7818+
"{} :: {}\n {}", d.name, d.signature, d.description
7819+
))),
7820+
None => Ok(Value::String(format!("{}: not in registry", name))),
7821+
}
7822+
}
7823+
"omc_help_signature" => {
7824+
// Just the signature string. Compactest possible.
7825+
if args.is_empty() {
7826+
return Err("omc_help_signature requires (name)".to_string());
7827+
}
7828+
let name = self.eval_expr(&args[0])?.to_display_string();
7829+
match crate::docs::lookup(&name) {
7830+
Some(d) => Ok(Value::String(d.signature.to_string())),
7831+
None => Ok(Value::String(String::new())),
7832+
}
7833+
}
7834+
"omc_help_example" => {
7835+
if args.is_empty() {
7836+
return Err("omc_help_example requires (name)".to_string());
7837+
}
7838+
let name = self.eval_expr(&args[0])?.to_display_string();
7839+
match crate::docs::lookup(&name) {
7840+
Some(d) => Ok(Value::String(d.example.to_string())),
7841+
None => Ok(Value::String(String::new())),
7842+
}
7843+
}
7844+
"omc_help_category" => {
7845+
if args.is_empty() {
7846+
return Err("omc_help_category requires (name)".to_string());
7847+
}
7848+
let name = self.eval_expr(&args[0])?.to_display_string();
7849+
match crate::docs::lookup(&name) {
7850+
Some(d) => Ok(Value::String(d.category.to_string())),
7851+
None => Ok(Value::String(String::new())),
7852+
}
7853+
}
7854+
"omc_is_unique" => {
7855+
// 1 if name is OMC-unique (no Python equivalent).
7856+
if args.is_empty() {
7857+
return Err("omc_is_unique requires (name)".to_string());
7858+
}
7859+
let name = self.eval_expr(&args[0])?.to_display_string();
7860+
match crate::docs::lookup(&name) {
7861+
Some(d) => Ok(Value::HInt(HInt::new(if d.unique_to_omc { 1 } else { 0 }))),
7862+
None => Ok(Value::HInt(HInt::new(0))),
7863+
}
7864+
}
7865+
"omc_count_in_category" => {
7866+
if args.is_empty() {
7867+
return Err("omc_count_in_category requires (category)".to_string());
7868+
}
7869+
let cat = self.eval_expr(&args[0])?.to_display_string();
7870+
let count = crate::docs::BUILTINS.iter()
7871+
.filter(|b| b.category == cat).count() as i64;
7872+
Ok(Value::HInt(HInt::new(count)))
7873+
}
7874+
"omc_random_builtin" => {
7875+
// Random builtin name. Useful for fuzzing or exploring.
7876+
let idx = (self.rng_next() % (crate::docs::BUILTINS.len() as u64)) as usize;
7877+
Ok(Value::String(crate::docs::BUILTINS[idx].name.to_string()))
7878+
}
7879+
"omc_random_unique_builtin" => {
7880+
let uniq: Vec<&str> = crate::docs::BUILTINS.iter()
7881+
.filter(|b| b.unique_to_omc).map(|b| b.name).collect();
7882+
if uniq.is_empty() {
7883+
return Ok(Value::String(String::new()));
7884+
}
7885+
let idx = (self.rng_next() % (uniq.len() as u64)) as usize;
7886+
Ok(Value::String(uniq[idx].to_string()))
7887+
}
77967888
"omc_search_builtins" => {
77977889
// Substring search across name + description. Returns
77987890
// matching names. Useful when you don't know what

0 commit comments

Comments
 (0)