|
| 1 | +name: hdc_codegen_wiring |
| 2 | +version: "1.0.0" |
| 3 | +language: zig |
| 4 | +module: hdc_codegen_wiring |
| 5 | + |
| 6 | +description: | |
| 7 | + HDC Codegen Wiring — Level 10A.17: Vibeec Improvement Specification. |
| 8 | + Defines how the vibeec code generator (src/vibeec/codegen/emitter.zig) |
| 9 | + should be improved to generate non-stub function bodies from wiring-level |
| 10 | + .vibee specs. |
| 11 | + |
| 12 | + === THE PROBLEM === |
| 13 | + Current vibeec (emitter.zig, 2,509 lines) generates: |
| 14 | + - Type structs (GOOD — correct Zig type definitions) |
| 15 | + - Function signatures (GOOD — correct pub fn declarations) |
| 16 | + - Function BODIES (BAD — stubs like `const result = "implemented"`) |
| 17 | + |
| 18 | + The PatternMatcher in patterns.zig detects VSA keywords (bind, bundle, |
| 19 | + permute, cosineSimilarity) but only emits a comment: |
| 20 | + `// VSA operation detected from spec keywords.` |
| 21 | + `// Available primitives: bind, unbind, bundle2, bundle3, permute, cosineSimilarity` |
| 22 | + `// Intent: <then clause>` |
| 23 | + |
| 24 | + === THE SOLUTION === |
| 25 | + Extend emitter.zig to recognize wiring-level specs (those that contain |
| 26 | + exact API call sequences in their `when` clause) and generate the |
| 27 | + corresponding Zig code. |
| 28 | + |
| 29 | + === PATTERN RECOGNITION RULES === |
| 30 | + |
| 31 | + Rule 1: CODEBOOK OPERATIONS |
| 32 | + When `when` contains "codebook.encode" or "Codebook.encode": |
| 33 | + Generate: `const hv_ptr = try codebook.encode(input);` |
| 34 | + |
| 35 | + When `when` contains "codebook.decode" or "Codebook.decode": |
| 36 | + Generate: `const result = codebook.decode(&output_hv);` |
| 37 | + |
| 38 | + Rule 2: VSA OPERATIONS (already detected, need code emission) |
| 39 | + When `when` contains "hv.bind(&" or ".bind(&": |
| 40 | + Generate: `var result = hv_a.bind(&hv_b);` |
| 41 | + |
| 42 | + When `when` contains "hv.bundle(&" or ".bundle(&": |
| 43 | + Generate: `var result = hv_a.bundle(&hv_b);` |
| 44 | + |
| 45 | + When `when` contains "hv.permute(" or ".permute(": |
| 46 | + Generate: `var result = hv.permute(k);` |
| 47 | + |
| 48 | + When `when` contains "hv.similarity(&" or ".similarity(&": |
| 49 | + Generate: `const sim = hv_a.similarity(&hv_b);` |
| 50 | + |
| 51 | + When `when` contains "hv.negate()" or ".negate()": |
| 52 | + Generate: `var neg = hv.negate();` |
| 53 | + |
| 54 | + Rule 3: LOOP PATTERNS |
| 55 | + When `when` contains "For i in 0..N:" or "for (0..N)": |
| 56 | + Generate: `for (0..N) |i| { ... }` |
| 57 | + |
| 58 | + When `when` contains "For each" followed by operation: |
| 59 | + Generate: `for (items) |item| { <operation> }` |
| 60 | + |
| 61 | + Rule 4: TIMING INSTRUMENTATION |
| 62 | + When behavior name starts with "measure" or contains "timed": |
| 63 | + Wrap body with: |
| 64 | + `const t0 = std.time.nanoTimestamp();` |
| 65 | + `// ... body ...` |
| 66 | + `const elapsed = std.time.nanoTimestamp() - t0;` |
| 67 | + |
| 68 | + Rule 5: RANDOM VECTOR CREATION |
| 69 | + When `when` contains "Hypervector.random(": |
| 70 | + Generate: `var hv = sdk.Hypervector.random(dim, seed);` |
| 71 | + |
| 72 | + Rule 6: ASSIGNMENT PATTERNS |
| 73 | + When `when` contains "variable = expression": |
| 74 | + Generate the Zig equivalent directly |
| 75 | + |
| 76 | + === IMPLEMENTATION IN EMITTER.ZIG === |
| 77 | + |
| 78 | + Location: src/vibeec/codegen/emitter.zig, in the `emitBehaviorFunction` method. |
| 79 | + |
| 80 | + Current flow: |
| 81 | + 1. Parse behavior name, given, when, then |
| 82 | + 2. PatternMatcher.match(when) → detect pattern |
| 83 | + 3. Emit function signature |
| 84 | + 4. Emit body based on pattern (currently stubs) |
| 85 | + |
| 86 | + New flow: |
| 87 | + 1-3: Same |
| 88 | + 4. If pattern contains wiring keywords → emit real code |
| 89 | + 5. If `when` clause contains line-by-line operations: |
| 90 | + Parse each line, apply Rules 1-6, emit Zig code |
| 91 | + |
| 92 | + === WHAT THIS ENABLES === |
| 93 | + After implementing these rules in emitter.zig, running vibeec on |
| 94 | + wiring-level specs (forward_wiring, train_wiring, persistence_format) |
| 95 | + would generate function bodies with real sdk.zig calls instead of stubs. |
| 96 | + |
| 97 | + This is a META-SPEC: it specifies improvements to the compiler itself. |
| 98 | + |
| 99 | + Trinity Identity: phi^2 + 1/phi^2 = 3 |
| 100 | + |
| 101 | +types: |
| 102 | + CodegenRule: |
| 103 | + fields: |
| 104 | + pattern: String |
| 105 | + trigger_keywords: List<String> |
| 106 | + generated_zig: String |
| 107 | + priority: usize |
| 108 | + |
| 109 | + PatternMatch: |
| 110 | + fields: |
| 111 | + rule_applied: String |
| 112 | + input_line: String |
| 113 | + output_zig: String |
| 114 | + confidence: Float |
| 115 | + |
| 116 | + CodegenImprovement: |
| 117 | + fields: |
| 118 | + file_path: String |
| 119 | + rules_added: List<CodegenRule> |
| 120 | + before_stub_count: usize |
| 121 | + after_stub_count: usize |
| 122 | + improvement_ratio: Float |
| 123 | + |
| 124 | +behaviors: |
| 125 | + - name: detectWiringSpec |
| 126 | + given: Parsed .vibee behavior with `when` clause |
| 127 | + when: | |
| 128 | + Check if `when` contains any of: |
| 129 | + ".bind(", ".bundle(", ".permute(", ".similarity(", |
| 130 | + ".encode(", ".decode(", ".negate(", ".clone(", |
| 131 | + "Hypervector.random(", "Codebook.init(" |
| 132 | + If 3+ matches: this is a wiring-level spec |
| 133 | + then: Boolean flag indicating wiring spec detected |
| 134 | + |
| 135 | + - name: parseWhenClause |
| 136 | + given: Multi-line `when` string from behavior |
| 137 | + when: | |
| 138 | + Split by newline |
| 139 | + For each line: |
| 140 | + Strip leading whitespace and numbering |
| 141 | + Match against Rules 1-6 |
| 142 | + If matched: record (input_line, rule_applied, output_zig) |
| 143 | + then: List of PatternMatch entries (one per parseable line) |
| 144 | + |
| 145 | + - name: emitWiringBody |
| 146 | + given: List of PatternMatch entries |
| 147 | + when: | |
| 148 | + For each match: |
| 149 | + Emit the generated_zig line with proper indentation |
| 150 | + Track variable names for subsequent references |
| 151 | + If any line didn't match a rule: emit as comment |
| 152 | + then: Zig function body with real API calls |
| 153 | + |
| 154 | + - name: addTimingInstrumentation |
| 155 | + given: Function body and behavior name |
| 156 | + when: | |
| 157 | + If name starts with "measure" or contains "timed": |
| 158 | + Prepend: const t0 = std.time.nanoTimestamp(); |
| 159 | + Append: const elapsed = std.time.nanoTimestamp() - t0; |
| 160 | + then: Instrumented function body |
| 161 | + |
| 162 | + - name: assessImprovement |
| 163 | + given: Before and after stub counts across all generated files |
| 164 | + when: Count functions with real bodies vs stubs |
| 165 | + then: CodegenImprovement with ratio |
| 166 | + |
| 167 | +test_cases: |
| 168 | + - name: detects_wiring_spec |
| 169 | + description: A behavior with 3+ SDK calls in `when` is detected as wiring |
| 170 | + expected: detectWiringSpec returns true for forward_wiring behaviors |
| 171 | + |
| 172 | + - name: parses_bind_call |
| 173 | + description: Line ".bind(&role)" generates valid Zig bind call |
| 174 | + expected: output contains "var result = *.bind(&*)" |
| 175 | + |
| 176 | + - name: parses_loop_pattern |
| 177 | + description: "For i in 0..8:" generates "for (0..8) |i|" |
| 178 | + expected: output contains Zig for loop |
0 commit comments