aufbau 0.1.0

Type-aware constrained decoding for LLMs using context-dependent grammars with typing rules
Documentation
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
#![allow(dead_code)]
#![allow(unused_imports)]

use crate::logic::grammar::Grammar;
use crate::logic::partial::MetaParser;
use crate::validation::completability::{complete, CompletionResult};
use crate::validation::complexity::{determine_complexity_exponent, ComplexityData};
use rand::{rngs::StdRng, Rng, SeedableRng};
use std::time::Instant;

fn fun_grammar() -> Grammar {
    use std::path::Path;
    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let path = Path::new(manifest_dir).join("examples").join("fun.auf");
    let content = std::fs::read_to_string(&path).expect("Failed to read fun.auf");
    Grammar::load(&content).expect("Failed to load fun grammar")
}

/// Generate nested parenthesized literals:
/// `(((1)))`
fn generate_parenthesized_literal(n: usize) -> String {
    let mut out = "1".to_string();
    for _ in 0..n {
        out = format!("({})", out);
    }
    out
}

/// Generate linear let chains with literal RHS:
/// `let x0: Int = 1; let x1: Int = 1; ...; xN`
fn generate_let_literal_chain(n: usize) -> String {
    if n == 0 {
        return "1".to_string();
    }

    let mut out = String::new();
    for i in 0..n {
        out.push_str(&format!("let x{}: Int = 1; ", i));
    }

    out.push_str(&format!("x{}", n - 1));
    out
}

/// Generate deterministic pseudo-random "weird" fun-like inputs.
///
/// Intentionally mixes valid fragments and incomplete tails to exercise
/// parser behavior on noisy real-world prefixes.
fn generate_weird_random_fun(n: usize) -> String {
    let mut rng = StdRng::seed_from_u64(0xC0FFEE_u64.wrapping_mul((n as u64) + 1));
    let atoms = ["1", "0", "true", "false", "x", "y", "(1)", "(true)"];
    let odd_tails = ["(", ")", "->", "-", "=>", ";", ".", ""];

    let mut out = atoms[rng.gen_range(0..atoms.len())].to_string();

    for i in 0..=n {
        match rng.gen_range(0..6) {
            0 => out = format!("({})", out),
            1 => out = format!("{} {}", out, atoms[rng.gen_range(0..atoms.len())]),
            2 => out = format!("let x{}: Int = 1; {}", i % 4, out),
            3 => out = format!("(x: Int) => {}", out),
            4 => out.push_str(&format!(
                " {}",
                odd_tails[rng.gen_range(0..odd_tails.len())]
            )),
            _ => out = format!("{} {}", atoms[rng.gen_range(0..atoms.len())], out),
        }
    }

    out
}

/// Generate operator-heavy chains: `1 + 2 - 3 * 4 / 5 + ...`.
fn generate_operator_chain(n: usize) -> String {
    if n == 0 {
        return "1".to_string();
    }
    let mut rng = StdRng::seed_from_u64(0xF00D_u64.wrapping_mul((n as u64) + 1));
    let ops = ["+", "-", "*", "/"];
    let mut out = "1".to_string();
    for i in 1..=n {
        let op = ops[rng.gen_range(0..ops.len())];
        out = format!("{} {} {}", out, op, (i % 10).to_string());
    }
    out
}

/// Generate nested lambda and application chains: `(x => (y => (z => ...))) a b c`.
fn generate_nested_lambda(n: usize) -> String {
    let mut lam = String::new();
    for i in 0..n {
        lam.push_str(&format!("(x{}: Int) => ", i));
    }
    lam.push_str("1");
    for i in 0..(n / 2) {
        lam.push_str(&format!(" a{}", i));
    }
    lam
}

/// Generate a mixed, more-complex random FUN input combining many constructs.
fn generate_complex_random_fun(n: usize) -> String {
    let mut rng = StdRng::seed_from_u64(0xDEADBEEF_u64.wrapping_mul((n as u64) + 1));

    // Start with a base expression
    let mut out = match rng.gen_range(0..3) {
        0 => generate_operator_chain(n / 2),
        1 => generate_nested_lambda(n / 2),
        _ => generate_weird_random_fun(n / 2),
    };

    // Splice in extra constructs to increase ambiguity and length
    for i in 0..n {
        match rng.gen_range(0..7) {
            0 => out = format!("({})", out),
            1 => out = format!("let x{}: Int = {}; {}", i, (i % 5) + 1, out),
            2 => out = format!("{} + {}", out, (i % 10)),
            3 => out = format!("(x: Int) => {}", out),
            4 => out.push_str(&format!("; x{}", i % 6)),
            5 => out = format!("if {} then {} else {}", (i % 2 == 0), out, "1"),
            _ => out.push_str(" ;"),
        }
    }

    out
}

/// Generate an incomplete let-chain prefix that requires completion.
///
/// Example (n = 2):
/// `let x0: Int = 1; let x1: Int = 1; let x2: Int =`
fn generate_incomplete_let_chain(n: usize) -> String {
    let mut out = String::new();
    for i in 0..n {
        out.push_str(&format!("let x{}: Int = 1; ", i));
    }
    out.push_str(&format!("let x{}: Int =", n));
    out
}

fn measure_completion_time(
    grammar: &Grammar,
    input: &str,
    max_depth: usize,
) -> std::time::Duration {
    let start = Instant::now();
    let _ = complete(grammar, input, max_depth, None);
    start.elapsed()
}

fn run_completion_complexity_test(
    grammar: &Grammar,
    generator: fn(usize) -> String,
    name: &str,
    max_n: usize,
    tries: usize,
) -> Vec<ComplexityData> {
    println!("\n=== {} Complexity Test ===", name);
    println!("Testing completion input sizes from 1 to {}", max_n);

    assert!(tries >= max_n * 2);

    let indices: Vec<usize> = (0..=tries).map(|i| ((i + max_n / 2) % max_n) + 1).collect();
    let mut results = Vec::with_capacity(indices.len());

    for n in indices {
        let input = generator(n);
        let depth_budget = n + 4;
        let duration = measure_completion_time(grammar, &input, depth_budget);
        results.push(ComplexityData::new(n, duration, input));
    }

    for r in &results {
        println!("n={:2}: len={} -> {:?}", r.n, r.input.len(), r.time);
    }

    results
}

fn measure_parse_time(grammar: &Grammar, input: &str) -> std::time::Duration {
    // Keep a bounded recursion budget so complexity tests stay practical in CI.
    let mut parser = MetaParser::new(grammar.clone());
    let start = Instant::now();
    let _ = parser.partial(input);
    start.elapsed()
}

fn run_complexity_test(
    grammar: &Grammar,
    generator: fn(usize) -> String,
    name: &str,
    max_n: usize,
    tries: usize,
    jobs: Option<usize>,
) -> Vec<ComplexityData> {
    println!("\n=== {} Complexity Test ===", name);
    println!("Testing input sizes from 1 to {}", max_n);

    assert!(tries >= max_n * 2);

    let results = super::run_complexity_experiment(grammar, generator, name, max_n, tries, jobs);

    for r in &results {
        println!("n={:2}: len={} -> {:?}", r.n, r.input.len(), r.time);
    }

    results
}

/// Export FUN experiments
pub fn experiments(jobs: Option<usize>) -> Vec<(String, Vec<ComplexityData>)> {
    let grammar = fun_grammar();
    vec![
        (
            "Fun Parenthesized Literal".to_string(),
            run_complexity_test(
                &grammar,
                generate_parenthesized_literal,
                "Fun Parenthesized Literal",
                4,
                8,
                jobs,
            ),
        ),
        (
            "Fun Let Literal Chain".to_string(),
            run_complexity_test(
                &grammar,
                generate_let_literal_chain,
                "Fun Let Literal Chain",
                4,
                8,
                jobs,
            ),
        ),
        (
            "Fun Weird Random".to_string(),
            run_complexity_test(
                &grammar,
                generate_weird_random_fun,
                "Fun Weird Random",
                8,
                16,
                jobs,
            ),
        ),
        (
            "Fun Complex Random".to_string(),
            run_complexity_test(
                &grammar,
                generate_complex_random_fun,
                "Fun Complex Random",
                12,
                32,
                jobs,
            ),
        ),
        (
            "Fun Completion Let Prefix".to_string(),
            run_completion_complexity_test(
                &grammar,
                generate_incomplete_let_chain,
                "Fun Completion Let Prefix",
                6,
                18,
            ),
        ),
    ]
}

#[test]
fn fun_parenthesized_literal_complexity() {
    let grammar = fun_grammar();
    let data = run_complexity_test(
        &grammar,
        generate_parenthesized_literal,
        "Fun Parenthesized Literal",
        4,
        8,
        None,
    );

    let k = determine_complexity_exponent(&data);

    println!("\nEmpirical complexity: O(n^{:.2})", k);
    println!("Expected: near-polynomial with parser memoization");

    assert!(
        k < 5.0,
        "Fun parenthesized-literal parsing should remain below ~O(n^5), got O(n^{:.2})",
        k
    );
    assert!(
        k > 0.01,
        "Complexity exponent should be > 0 for non-trivial inputs"
    );
}

#[test]
fn fun_let_literal_chain_complexity() {
    let grammar = fun_grammar();
    let data = run_complexity_test(
        &grammar,
        generate_let_literal_chain,
        "Fun Let Literal Chain",
        4,
        8,
        None,
    );

    let k = determine_complexity_exponent(&data);

    println!("\nEmpirical complexity: O(n^{:.2})", k);
    println!("Linear let-chains stress sequential grammar growth and bindings.");

    assert!(
        k < 5.0,
        "Fun let-literal-chain parsing should stay below ~O(n^5), got O(n^{:.2})",
        k
    );
}

#[test]
fn fun_weird_random_complexity() {
    let grammar = fun_grammar();
    let data = run_complexity_test(
        &grammar,
        generate_weird_random_fun,
        "Fun Weird Random",
        8,
        16,
        None,
    );

    let k = determine_complexity_exponent(&data);

    println!("\nEmpirical complexity: O(n^{:.2})", k);
    println!("Weird/random prefixes simulate noisy, partially malformed edits.");

    assert!(
        k < 6.0,
        "Fun weird-random parsing should stay below ~O(n^6), got O(n^{:.2})",
        k
    );
}

#[test]
fn fun_complex_random_complexity() {
    let grammar = fun_grammar();
    let data = run_complexity_test(
        &grammar,
        generate_complex_random_fun,
        "Fun Complex Random",
        12,
        32,
        None,
    );

    let k = determine_complexity_exponent(&data);

    println!("\nEmpirical complexity: O(n^{:.2})", k);
    println!("Complex-random generator mixes operator chains, nested lambdas and lets.");

    // Allow a higher ceiling because these inputs are intentionally adversarial.
    assert!(
        k < 8.0,
        "Fun complex-random parsing should stay below ~O(n^8), got O(n^{:.2})",
        k
    );
}

#[test]
fn fun_completion_let_prefix_complexity() {
    let grammar = fun_grammar();
    let data = run_completion_complexity_test(
        &grammar,
        generate_incomplete_let_chain,
        "Fun Completion Let Prefix",
        6,
        18,
    );

    let k = determine_complexity_exponent(&data);

    println!("\nEmpirical completion complexity: O(n^{:.2})", k);
    println!("Completion input grows as incomplete let-chain prefixes.");

    assert!(
        k < 8.0,
        "Fun completion on let prefixes should stay below ~O(n^8), got O(n^{:.2})",
        k
    );
    assert!(k > 0.01, "Complexity exponent should be > 0");

    let mut observed_success = false;
    for point in &data {
        let input = generate_incomplete_let_chain(point.n);
        let depth_budget = point.n + 4;
        if matches!(
            complete(&grammar, &input, depth_budget, None),
            CompletionResult::Success { .. }
        ) {
            observed_success = true;
            break;
        }
    }
    assert!(
        observed_success,
        "Expected at least one successful completion across sampled n"
    );
}