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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
use crate::logic::grammar::Grammar;
use crate::logic::partial::PartialAST;
use crate::logic::partial::{Node, NonTerminal, Terminal};
use crate::logic::typing::core::TreeStatus;
use crate::logic::typing::eval::check_tree;
use serde::Serialize;
use std::collections::HashMap;

#[derive(Debug, Serialize, Clone)]
pub struct GraphNode {
    pub id: String,
    pub label: String,
    pub status: String,                // "complete" | "partial" | "terminal"
    pub reconstructed: Option<String>, // Reconstructed input from terminals below this node
    pub meta: NodeMeta,
}

#[derive(Debug, Serialize, Clone)]
pub struct GraphEdge {
    pub from: String,
    pub to: String,
    pub label: Option<String>,
    pub style: String, // "solid" | "dashed"
}

#[derive(Debug, Serialize, Clone)]
pub struct GraphData {
    pub nodes: Vec<GraphNode>,
    pub edges: Vec<GraphEdge>,
    pub trees: Vec<TreeInfo>,
    /// Reconstructed inputs from all complete trees (for checking associativity etc)
    pub reconstructed_inputs: Vec<ReconstructedInput>,
}

#[derive(Debug, Serialize, Clone)]
pub struct ReconstructedInput {
    pub tree_index: usize,
    pub text: String,
    pub complete: bool,
}

#[derive(Debug, Serialize, Clone)]
pub struct TreeInfo {
    pub id: String,
    pub index: usize,
    pub complete: bool,      // Syntactically complete
    pub well_typed: bool,    // Passes type checking
    pub type_status: String, // "valid" | "malformed" | "partial"
    pub node_count: usize,
    pub context: Vec<ContextEntry>, // Variables in scope after this tree
    pub reconstructed: String,      // Reconstructed input text from this tree
}

#[derive(Debug, Serialize, Clone)]
pub struct ContextEntry {
    pub name: String,
    pub ty: String,
}

#[derive(Debug, Serialize, Clone)]
pub struct NodeMeta {
    pub kind: String,
    pub value: Option<String>,
    pub binding: Option<String>,
    pub production: Option<ProductionInfo>,
    pub typing_rule: Option<TypingRuleInfo>,
    pub inferred_type: Option<String>,
    pub context: Vec<ContextEntry>,
    pub constraints: Vec<ConstraintInfo>,
    pub alternative: usize,
}

#[derive(Debug, Serialize, Clone)]
pub struct ConstraintInfo {
    pub kind: String, // "premise" | "setting" | "unify" | "membership"
    pub text: String,
}

#[derive(Debug, Serialize, Clone)]
pub struct ProductionInfo {
    pub rhs: Vec<String>,
    pub cursor: usize,
    pub complete: bool,
}

#[derive(Debug, Serialize, Clone)]
pub struct TypingRuleInfo {
    pub name: String,
    pub premises: Vec<String>,
    pub conclusion: String,
    /// Pretty multiline formatting of the full inference rule.
    pub pretty: String,
}

pub fn build_graph(ast: &PartialAST, grammar: &Grammar) -> GraphData {
    let roots = ast.roots();
    let mut nodes: Vec<GraphNode> = Vec::new();
    let mut edges: Vec<GraphEdge> = Vec::new();
    let mut trees: Vec<TreeInfo> = Vec::new();
    let mut reconstructed_inputs: Vec<ReconstructedInput> = Vec::new();

    // Create a virtual root for the forest
    let root_id = "root";
    nodes.push(GraphNode {
        id: root_id.to_string(),
        label: format!("Forest ({})", roots.len()),
        status: if ast.is_complete() {
            "complete"
        } else {
            "partial"
        }
        .to_string(),
        reconstructed: None,
        meta: NodeMeta {
            kind: "forest".to_string(),
            value: None,
            binding: None,
            production: None,
            typing_rule: None,
            inferred_type: None,
            context: vec![],
            constraints: vec![],
            alternative: 0,
        },
    });

    for (i, root) in roots.iter().enumerate() {
        let child_id = format!("t{}", i);
        let tree_complete = root.is_complete();
        let node_count_before = nodes.len();

        // Run type checking on the tree
        let type_result = check_tree(root, grammar);
        let (well_typed, type_status) = match &type_result {
            TreeStatus::Valid(_) => (true, "valid"),
            TreeStatus::Partial(_) => (true, "partial"), // Partial is OK for incomplete trees
            TreeStatus::Malformed => (false, "malformed"),
            TreeStatus::TooDeep => (false, "too deep"),
        };

        // Extract context from the tree (for let expressions etc)
        let context = extract_context_from_tree(root, grammar);

        // Reconstruct input text from the tree
        let root_node = Node::NonTerminal(root.clone());
        let reconstructed = reconstruct_input(&root_node);

        edges.push(GraphEdge {
            from: root_id.to_string(),
            to: child_id.clone(),
            label: Some(format!("alt {}", i)),
            style: if well_typed { "solid" } else { "dashed" }.to_string(),
        });

        // Collect per-node typing metadata (context extensions / inferred types / constraints)
        let typing_meta = collect_typing_meta_with_root_id(&child_id, &root_node, grammar);

        walk_nt(
            &child_id,
            root,
            grammar,
            &mut nodes,
            &mut edges,
            well_typed,
            &typing_meta,
        );

        let node_count = nodes.len() - node_count_before;

        // Add to reconstructed inputs list
        reconstructed_inputs.push(ReconstructedInput {
            tree_index: i,
            text: reconstructed.clone(),
            complete: tree_complete,
        });

        trees.push(TreeInfo {
            id: child_id,
            index: i,
            complete: tree_complete,
            well_typed,
            type_status: type_status.to_string(),
            node_count,
            context,
            reconstructed,
        });
    }

    GraphData {
        nodes,
        edges,
        trees,
        reconstructed_inputs,
    }
}

fn walk_nt(
    node_id: &str,
    nt: &NonTerminal,
    grammar: &Grammar,
    nodes: &mut Vec<GraphNode>,
    edges: &mut Vec<GraphEdge>,
    tree_well_typed: bool,
    typing_meta: &HashMap<String, NodeTypingMeta>,
) {
    let is_complete = nt.is_complete();

    let prod_info = ProductionInfo {
        rhs: nt
            .production
            .rhs
            .iter()
            .map(|s| format!("{:?}", s))
            .collect(),
        cursor: nt.children.len(),
        complete: is_complete,
    };

    // Get typing rule info if present
    let typing_rule = nt.production.rule.as_ref().and_then(|rule_name| {
        grammar
            .typing_rules
            .get(rule_name)
            .map(|rule| TypingRuleInfo {
                name: rule_name.clone(),
                premises: rule.premises.iter().map(|p| p.to_string()).collect(),
                conclusion: rule.conclusion.to_string(),
                pretty: rule.pretty(0),
            })
    });

    let label = if let Some(ref rule) = nt.production.rule {
        format!("{}({})", nt.name, rule)
    } else {
        nt.name.clone()
    };

    // Status combines syntactic completeness and type validity
    let status = if !tree_well_typed {
        "error" // Type error in this tree
    } else if is_complete {
        "complete"
    } else {
        "partial"
    };

    let inferred_type = if tree_well_typed {
        match check_tree(nt, grammar) {
            TreeStatus::Valid(ty) | TreeStatus::Partial(ty) => Some(format!("{ty}")),
            TreeStatus::Malformed | TreeStatus::TooDeep => None,
        }
    } else {
        None
    };

    let (context, constraints) = typing_meta
        .get(node_id)
        .map(|m| (m.context.clone(), m.constraints.clone()))
        .unwrap_or((vec![], vec![]));

    // Reconstruct input from this subtree
    let reconstructed = reconstruct_node_input(&Node::NonTerminal(nt.clone()));

    nodes.push(GraphNode {
        id: node_id.to_string(),
        label,
        status: status.to_string(),
        reconstructed: Some(reconstructed),
        meta: NodeMeta {
            kind: "nonterminal".to_string(),
            value: Some(nt.name.clone()),
            binding: nt.binding.clone(),
            production: Some(prod_info),
            typing_rule,
            inferred_type,
            context,
            constraints,
            alternative: nt.alternative_index,
        },
    });

    for (i, child) in nt.children.iter().enumerate() {
        let child_id = format!("{}_{}", node_id, i);

        // Get the symbol name for edge label
        let edge_label = nt.production.rhs.get(i).map(|sym| match sym {
            crate::logic::grammar::Symbol::Nonterminal { name, binding } => {
                if let Some(b) = binding {
                    format!("{}[{}]", name, b)
                } else {
                    name.clone()
                }
            }
            crate::logic::grammar::Symbol::Terminal { binding, .. } => {
                if let Some(b) = binding {
                    format!("[{}]", b)
                } else {
                    String::new()
                }
            }
        });

        edges.push(GraphEdge {
            from: node_id.to_string(),
            to: child_id.clone(),
            label: edge_label,
            style: "solid".to_string(),
        });
        walk_node(
            &child_id,
            child,
            grammar,
            nodes,
            edges,
            tree_well_typed,
            typing_meta,
        );
    }
}

fn walk_node(
    node_id: &str,
    node: &Node,
    grammar: &Grammar,
    nodes: &mut Vec<GraphNode>,
    edges: &mut Vec<GraphEdge>,
    tree_well_typed: bool,
    typing_meta: &HashMap<String, NodeTypingMeta>,
) {
    match node {
        Node::Terminal(t) => {
            let (value, binding, status, reconstructed) = match t {
                Terminal::Complete { value, binding, .. } => {
                    let s = if tree_well_typed { "terminal" } else { "error" };
                    (value.clone(), binding.clone(), s, value.clone())
                }
                Terminal::Partial { value, binding, .. } => {
                    let recon = if value.is_empty() {
                        "".to_string()
                    } else {
                        format!("{}", value)
                    };
                    (value.clone(), binding.clone(), "partial", recon)
                }
            };

            let label = if value.is_empty() {
                "".to_string()
            } else {
                format!("\"{}\"", value)
            };

            nodes.push(GraphNode {
                id: node_id.to_string(),
                label,
                status: status.to_string(),
                reconstructed: Some(reconstructed),
                meta: NodeMeta {
                    kind: "terminal".to_string(),
                    value: Some(value),
                    binding,
                    production: None,
                    typing_rule: None,
                    inferred_type: None,
                    context: vec![],
                    constraints: vec![],
                    alternative: 0,
                },
            });
        }
        Node::NonTerminal(nt) => {
            walk_nt(
                node_id,
                nt,
                grammar,
                nodes,
                edges,
                tree_well_typed,
                typing_meta,
            );
        }
    }
}

#[derive(Debug, Clone)]
struct NodeTypingMeta {
    context: Vec<ContextEntry>,
    constraints: Vec<ConstraintInfo>,
}

fn collect_typing_meta_with_root_id_rec(
    root_id: &str,
    node: &Node,
    grammar: &Grammar,
    out: &mut HashMap<String, NodeTypingMeta>,
) {
    if let Node::NonTerminal(nt) = node {
        // Record context extensions for this non-terminal (if any)
        let mut ctx_entries: Vec<ContextEntry> = vec![];
        let mut constraints: Vec<ConstraintInfo> = vec![];

        if let Some(rule_name) = &nt.production.rule {
            if let Some(rule) = grammar.typing_rules.get(rule_name) {
                // Surface premises + conclusion as constraints for quick inspection.
                for p in &rule.premises {
                    constraints.push(ConstraintInfo {
                        kind: "premise".into(),
                        text: p.to_string(),
                    });
                }
                constraints.push(ConstraintInfo {
                    kind: "conclusion".into(),
                    text: rule.conclusion.to_string(),
                });

                // Also include the full inference rule in pretty multiline form.
                // The UI treats this as preformatted text.
                constraints.push(ConstraintInfo {
                    kind: "rule".into(),
                    text: rule.pretty(0),
                });

                // Context output extensions (let-style)
                if let Some(output) = &rule.conclusion.context.output {
                    for (var_name, ty_expr) in &output.extensions {
                        if let Some(paths) = grammar.binding_map.get(var_name, rule_name) {
                            for path in paths {
                                if let Ok(results) =
                                    crate::logic::binding::resolve_binding_path(node, path)
                                {
                                    if let Some(res) = results.first() {
                                        if let Some(name) = get_node_text(res.node()) {
                                            ctx_entries.push(ContextEntry {
                                                name,
                                                ty: format!("{ty_expr}"),
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        out.insert(
            root_id.to_string(),
            NodeTypingMeta {
                context: ctx_entries,
                constraints,
            },
        );

        for (i, child) in nt.children.iter().enumerate() {
            let child_id = format!("{}_{}", root_id, i);
            collect_typing_meta_with_root_id_rec(&child_id, child, grammar, out);
        }
    }
}

fn collect_typing_meta_with_root_id(
    root_id: &str,
    root: &Node,
    grammar: &Grammar,
) -> HashMap<String, NodeTypingMeta> {
    let mut out: HashMap<String, NodeTypingMeta> = HashMap::new();
    collect_typing_meta_with_root_id_rec(root_id, root, grammar, &mut out);
    out
}

/// Extract context bindings from a tree (e.g., from let expressions)
fn extract_context_from_tree(root: &NonTerminal, grammar: &Grammar) -> Vec<ContextEntry> {
    let mut entries = Vec::new();
    collect_context_entries(&Node::NonTerminal(root.clone()), grammar, &mut entries);
    entries
}

fn collect_context_entries(node: &Node, grammar: &Grammar, entries: &mut Vec<ContextEntry>) {
    if let Node::NonTerminal(nt) = node {
        // Check if this node has a rule with context transform
        if let Some(rule_name) = &nt.production.rule {
            if let Some(rule) = grammar.typing_rules.get(rule_name) {
                // Check if conclusion has output context (let-style)
                if let Some(output) = &rule.conclusion.context.output {
                    for (var_name, ty_expr) in &output.extensions {
                        // Try to resolve var_name to actual value
                        if let Some(paths) = grammar.binding_map.get(var_name, rule_name) {
                            for path in paths {
                                if let Ok(results) =
                                    crate::logic::binding::resolve_binding_path(node, path)
                                {
                                    if let Some(res) = results.first() {
                                        if let Some(name) = get_node_text(res.node()) {
                                            entries.push(ContextEntry {
                                                name,
                                                ty: format!("{}", ty_expr),
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // Recurse into children
        for child in &nt.children {
            collect_context_entries(child, grammar, entries);
        }
    }
}

fn get_node_text(node: &Node) -> Option<String> {
    match node {
        Node::Terminal(Terminal::Complete { value, .. }) => Some(value.clone()),
        Node::Terminal(Terminal::Partial { value, .. }) if !value.is_empty() => Some(value.clone()),
        Node::Terminal(Terminal::Partial { .. }) => None,
        Node::NonTerminal(nt) => {
            let mut s = String::new();
            for child in &nt.children {
                s.push_str(&get_node_text(child)?);
            }
            Some(s)
        }
    }
}

/// Reconstruct the input string from terminals in a tree.
/// Shows placeholder markers for incomplete/missing parts.
fn reconstruct_input(node: &Node) -> String {
    match node {
        Node::Terminal(Terminal::Complete { value, .. }) => value.clone(),
        Node::Terminal(Terminal::Partial { value, .. }) => {
            if value.is_empty() {
                "".to_string() // Placeholder for empty partial
            } else {
                format!("{}", value) // Partial value with ellipsis
            }
        }
        Node::NonTerminal(nt) => {
            let mut parts: Vec<String> = Vec::new();
            for child in &nt.children {
                parts.push(reconstruct_input(child));
            }
            // If tree is incomplete, show what's missing
            if !nt.is_complete() && nt.children.len() < nt.production.rhs.len() {
                parts.push("".to_string());
            }
            parts.join("")
        }
    }
}

/// Reconstruct input for a specific node (used for per-node reconstruction)
fn reconstruct_node_input(node: &Node) -> String {
    reconstruct_input(node)
}