Skip to main content

inference_tokens/
inference_tokens.rs

1//! Realized token win from the landed inference migration (return-type +
2//! parameter-type inference). Each pair is the SAME function, annotated vs the
3//! now-valid inferred form (`fn sq(n: i32) -> i32 {…}` → `f sq(n) {…}`), counted
4//! with the real cl100k + o200k BPE.
5//!
6//!   cargo run -p agentic-eval --example inference_tokens --features real-tokens
7
8use agentic_eval::tokens::Model;
9
10fn main() {
11    let cl = Model::OpenAiGpt4;
12    let o2 = Model::OpenAiGpt4o;
13    println!("=== Realized token win: type inference (return + params) ===");
14    println!(
15        "tokenizer: {}\n",
16        if cl.is_exact() { "REAL tiktoken (exact)" } else { "HEURISTIC — rerun with --features real-tokens" }
17    );
18
19    // (name, annotated [valid before], inferred [valid AFTER the landed change]).
20    let pairs: &[(&str, &str, &str)] = &[
21        ("square", "fn square(n: i32) -> i32 { n * n }", "f square(n) { n * n }"),
22        ("add", "fn add(a: i32, b: i32) -> i32 { a + b }", "f add(a, b) { a + b }"),
23        (
24            "factorial",
25            "fn factorial(n: u64) -> u64 { if n <= 1 { 1 } else { n * factorial(n - 1) } }",
26            "f factorial(n) { if n <= 1 { 1 } else { n * factorial(n - 1) } }",
27        ),
28        // Multi-statement body — fully brace-free + semicolon-free + inferred
29        // (return + param inference, `;`-optional, AND layout blocks — all landed).
30        (
31            "area3",
32            "fn area3(w: i32, h: i32) -> i32 { val a = w * h; val b = a + a; b }",
33            "f area3(w, h)\n  val a = w * h\n  val b = a + a\n  b",
34        ),
35    ];
36
37    println!("{:<11} {:>9} {:>9} {:>9}", "fn", "annot", "inferred", "saved");
38    let (mut ann_cl, mut inf_cl, mut ann_o, mut inf_o) = (0, 0, 0, 0);
39    for (name, ann, inf) in pairs {
40        let (a, i) = (cl.count(ann), cl.count(inf));
41        println!("{name:<11} {a:>9} {i:>9} {:>8}%", 100 - 100 * i / a);
42        ann_cl += a; inf_cl += i;
43        ann_o += o2.count(ann); inf_o += o2.count(inf);
44    }
45    println!("\nTOTAL  cl100k {ann_cl} → {inf_cl} ({}% saved)   o200k {ann_o} → {inf_o} ({}% saved)",
46        100 - 100 * inf_cl / ann_cl, 100 - 100 * inf_o / ann_o);
47    println!("\nThe inferred forms are now ACCEPTED by the compiler (return + param inference,");
48    println!("recursion-correct), so this saving is real, not hypothetical. The remaining tokens");
49    println!("are the payload (names/ops/literals) — the irreducible floor. area3 is now FULLY");
50    println!("brace-free + semicolon-free + inferred (layout blocks landed) — the form-C surface.");
51
52    // Honest sub-finding: is dropping braces a token win, or just aesthetic?
53    let braced_nosemi = "f area3(w, h) { val a = w * h\n val b = a + a\n b }";
54    let layout = "f area3(w, h)\n  val a = w * h\n  val b = a + a\n  b";
55    println!(
56        "\nBRACE vs LAYOUT (same fn, no `;`): braced {} → layout {} cl100k tokens",
57        cl.count(braced_nosemi),
58        cl.count(layout)
59    );
60    println!("  Dropping braces is ~token-NEUTRAL (often slightly worse): BPE charges for the");
61    println!("  indentation whitespace about what the two brace tokens saved. Same lesson as the");
62    println!("  'digital rain' — whitespace tokenizes too. The real wins were inference + `;`,");
63    println!("  NOT braces→layout. Layout is a readability/aesthetic choice, not a token lever.");
64
65    // Effect inference (trust-boundary model): a PRIVATE effectful function now
66    // infers its effects and drops the `/ effect` annotation — the public
67    // boundary still declares them, so safety is unchanged (boundary-enforced).
68    let with_eff = "f process(x: i32) / io { print(x) }";
69    let no_eff = "f process(x: i32) { print(x) }";
70    println!(
71        "\nEFFECT ANNOTATION (private fn): `/ io` {} → inferred {} cl100k tokens (−{})",
72        cl.count(with_eff),
73        cl.count(no_eff),
74        cl.count(with_eff) - cl.count(no_eff)
75    );
76    println!("  Private effectful fns now infer effects (the pub boundary still declares them) —");
77    println!("  a real, SAFETY-PRESERVING token saving: enforcement moved to the module surface.");
78}