aprender 0.31.2

Next-generation ML framework in pure Rust — `cargo install aprender` for the `apr` CLI
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
<!-- PCU: ml-fundamentals-compiler-in-the-loop | contract: contracts/apr-page-ml-fundamentals-compiler-in-the-loop-v1.yaml -->
<!-- Example: cargo run -p aprender-core --example none -->
<!-- Status: enforced -->

# Compiler-in-the-Loop Learning

A comprehensive guide to self-supervised learning paradigms that use compiler feedback as an automatic labeling oracle.

## Overview

**Compiler-in-the-Loop Learning (CITL)** is a specialized form of self-supervised learning where a compiler (or interpreter) serves as an automatic oracle for providing ground truth about code correctness. Unlike traditional supervised learning that requires expensive human annotations, CITL systems leverage the deterministic nature of compilers to generate training signals automatically.

This paradigm is particularly powerful for:
- Code transpilation (source-to-source translation)
- Automated program repair
- Code generation and synthesis
- Type inference and annotation

## The Core Feedback Loop

```
┌─────────────────────────────────────────────────────────────────┐
│                    COMPILER-IN-THE-LOOP                        │
│                                                                 │
│   ┌──────────┐    ┌───────────┐    ┌──────────┐                │
│   │  Source  │───►│ Transform │───►│  Target  │                │
│   │   Code   │    │  (Model)  │    │   Code   │                │
│   └──────────┘    └───────────┘    └────┬─────┘                │
│                         ▲               │                       │
│                         │               ▼                       │
│                   ┌─────┴─────┐   ┌──────────┐                 │
│                   │   Learn   │◄──│ Compiler │                 │
│                   │ from Error│   │ Feedback │                 │
│                   └───────────┘   └──────────┘                 │
│                                        │                        │
│                                        ▼                        │
│                                 ┌────────────┐                  │
│                                 │  Success/  │                  │
│                                 │   Error    │                  │
│                                 └────────────┘                  │
└─────────────────────────────────────────────────────────────────┘
```

The key insight is that **compilers provide a perfect, deterministic reward function**. Unlike human feedback which is:
- Expensive to obtain
- Subjective and inconsistent
- Limited in availability

Compiler feedback is:
- Free and instant
- Objective and deterministic
- Unlimited in quantity

## Related ML/AI Paradigms

### 1. Reinforcement Learning from Compiler Feedback (RLCF)

Analogous to **RLHF (Reinforcement Learning from Human Feedback)**, but using compiler output as the reward signal.

```
┌─────────────────────────────────────────────────────────────────┐
│                          RLCF                                   │
│                                                                 │
│   Policy π(action | state) = Transpilation Strategy             │
│                                                                 │
│   State s = (source_code, context, history)                     │
│                                                                 │
│   Action a = Generated target code                              │
│                                                                 │
│   Reward r = { +1  if compiles successfully                     │
│              { -1  if compilation fails                         │
│              { +bonus for passing tests                         │
│                                                                 │
│   Objective: max E[Σ γ^t r_t]                                   │
└─────────────────────────────────────────────────────────────────┘
```

**Key Components:**
- **Policy**: The transpilation model (neural network, rule-based, or hybrid)
- **State**: Source code + AST + type information + compilation history
- **Action**: The generated target code
- **Reward**: Binary (compiles/doesn't) + continuous (test coverage, performance)

### 2. Neural Program Repair (APR)

A classic software engineering research area that learns to fix code based on error patterns.

```rust
// Example: Learning from compilation errors
struct ErrorPattern {
    error_code: String,      // E0308: mismatched types
    error_context: String,   // expected `i32`, found `&str`
    fix_strategy: FixType,   // TypeConversion, TypeAnnotation, etc.
}

enum FixType {
    TypeConversion,     // Add .parse(), .to_string(), etc.
    TypeAnnotation,     // Add explicit type annotation
    BorrowingFix,       // Add &, &mut, .clone()
    LifetimeAnnotation, // Add 'a, 'static, etc.
    ImportAddition,     // Add use statement
}
```

The system builds a mapping: `(error_type, context) → fix_strategy`

**Research lineage:**
- GenProg (2012) - Genetic programming for patches
- Prophet (2016) - Learning code correctness
- DeepFix (2017) - Deep learning for syntax errors
- Getafix (2019) - Facebook's automated fix tool
- Codex/Copilot (2021+) - Large language models

### 3. Execution-Guided Synthesis

Generate code, execute/compile it, refine based on feedback.

```
┌─────────────────────────────────────────────────────────────────┐
│              EXECUTION-GUIDED SYNTHESIS                         │
│                                                                 │
│   for iteration in 1..max_iterations:                           │
│       candidate = generate(specification)                       │
│       result = execute(candidate)  // or compile                │
│                                                                 │
│       if result.success:                                        │
│           return candidate                                      │
│       else:                                                     │
│           feedback = analyze_failure(result)                    │
│           update_model(feedback)                                │
└─────────────────────────────────────────────────────────────────┘
```

This is similar to **self-play** systems (like AlphaGo) where the game rules provide absolute ground truth.

### 4. Self-Training / Bootstrapping

Uses its own successful outputs as training data for iterative improvement.

```
┌─────────────────────────────────────────────────────────────────┐
│                    SELF-TRAINING LOOP                           │
│                                                                 │
│   Initial: Small set of verified (source, target) pairs        │
│                                                                 │
│   Loop:                                                         │
│     1. Train model on current dataset                           │
│     2. Generate candidates for unlabeled sources                │
│     3. Filter: Keep only those that compile                     │
│     4. Add verified pairs to training set                       │
│     5. Repeat until convergence                                 │
│                                                                 │
│   Result: Model improves using its own verified outputs         │
└─────────────────────────────────────────────────────────────────┘
```

### 5. Curriculum Learning with Error Difficulty

Progressively train on harder examples based on error complexity.

```
Level 1: Simple type mismatches (String vs &str)
Level 2: Borrowing and ownership errors
Level 3: Lifetime annotations
Level 4: Complex trait bounds
Level 5: Async/concurrent code patterns
```

## Tiered Diagnostic Capture

Modern CITL systems employ a **four-tier diagnostic architecture** that captures compiler feedback at multiple granularity levels:

```
┌─────────────────────────────────────────────────────────────────┐
│                  FOUR-TIER DIAGNOSTICS                          │
│                                                                 │
│   Tier 1: ERROR-LEVEL (Must Fix)                               │
│   ├── E0308: Type mismatch                                      │
│   ├── E0382: Use of moved value                                 │
│   └── E0597: Borrowed value doesn't live long enough            │
│                                                                 │
│   Tier 2: WARNING-LEVEL (Should Fix)                           │
│   ├── unused_variables                                          │
│   ├── dead_code                                                 │
│   └── unreachable_patterns                                      │
│                                                                 │
│   Tier 3: CLIPPY LINTS (Style/Performance)                     │
│   ├── clippy::unwrap_used                                       │
│   ├── clippy::clone_on_copy                                     │
│   └── clippy::manual_memcpy                                     │
│                                                                 │
│   Tier 4: SEMANTIC VALIDATION (Tests/Behavior)                 │
│   ├── Test failures                                             │
│   ├── Property violations                                       │
│   └── Semantic equivalence checks                               │
└─────────────────────────────────────────────────────────────────┘
```

### Adaptive Tier Progression

Training follows curriculum learning with adaptive tier progression:

```rust
struct TierProgression {
    current_tier: u8,
    tier_success_rate: [f64; 4],
    promotion_threshold: f64,    // Default: 0.85 (85% success)
}

impl TierProgression {
    fn should_promote(&self) -> bool {
        self.tier_success_rate[self.current_tier as usize] >= self.promotion_threshold
    }

    fn next_tier(&mut self) {
        if self.current_tier < 3 && self.should_promote() {
            self.current_tier += 1;
        }
    }
}
```

This ensures the model masters simpler error patterns before tackling complex scenarios.

## Decision Traces

CITL systems generate **decision traces** - structured records of every transformation decision made during transpilation. These traces enable:
- Debugging transformation failures
- Training fix predictors
- Auditing code generation

### Seven Decision Categories

```rust
#[derive(Debug, Clone, Serialize, Deserialize)]
enum DecisionCategory {
    /// Type inference and mapping decisions
    TypeMapping {
        python_type: String,
        rust_type: String,
        confidence: f64,
    },

    /// Borrow vs owned strategy selection
    BorrowStrategy {
        variable: String,
        strategy: BorrowKind,  // Owned, Borrowed, MutBorrowed
        reason: String,
    },

    /// Lifetime inference and annotation
    LifetimeInfer {
        function: String,
        inferred: Vec<String>,  // ['a, 'b, ...]
        elision_applied: bool,
    },

    /// Error handling transformation
    ErrorHandling {
        python_pattern: String,  // try/except, assert, etc.
        rust_pattern: String,    // Result, Option, panic!, etc.
    },

    /// Loop transformation decisions
    LoopTransform {
        python_construct: String,  // for, while, comprehension
        rust_construct: String,    // for, loop, iter().map()
        iterator_type: String,
    },

    /// Memory allocation strategy
    MemoryAlloc {
        pattern: String,        // list, dict, set
        rust_type: String,      // Vec, HashMap, HashSet
        capacity_hint: Option<usize>,
    },

    /// Concurrency model mapping
    ConcurrencyMap {
        python_pattern: String,  // threading, asyncio, multiprocessing
        rust_pattern: String,    // std::thread, tokio, rayon
    },
}
```

### Decision Trace Format

Traces are stored as memory-mapped files for efficient streaming:

```rust
struct DecisionTrace {
    /// Lamport timestamp for causal ordering
    lamport_clock: u64,

    /// Source location (file:line:col)
    source_span: SourceSpan,

    /// Decision category and details
    category: DecisionCategory,

    /// Compiler feedback if transformation failed
    compiler_result: Option<CompilerResult>,

    /// Parent decision (for tree structure)
    parent_id: Option<TraceId>,
}

// Efficient binary format for streaming
impl DecisionTrace {
    fn to_bytes(&self) -> Vec<u8>;
    fn from_bytes(data: &[u8]) -> Result<Self, DecodeError>;
}
```

### Error-Decision Correlation

The system learns correlations between decisions and compiler errors:

```
┌─────────────────────────────────────────────────────────────────┐
│              ERROR-DECISION CORRELATION                         │
│                                                                 │
│   Error E0308 (Type Mismatch) correlates with:                 │
│     - TypeMapping decisions (92% correlation)                   │
│     - ErrorHandling decisions (73% correlation)                 │
│                                                                 │
│   Error E0382 (Use of Moved Value) correlates with:            │
│     - BorrowStrategy decisions (89% correlation)               │
│     - LoopTransform decisions (67% correlation)                │
│                                                                 │
│   Error E0597 (Lifetime) correlates with:                      │
│     - LifetimeInfer decisions (95% correlation)                │
│     - BorrowStrategy decisions (81% correlation)               │
└─────────────────────────────────────────────────────────────────┘
```

## Oracle Query Loop

The **Oracle Query Loop** is a key advancement in CITL systems - it enables models to persist learned patterns and query them for new transformations.

### .apr Model Persistence

```
┌─────────────────────────────────────────────────────────────────┐
│                    ORACLE QUERY LOOP                            │
│                                                                 │
│   ┌──────────┐    ┌───────────┐    ┌──────────────────┐        │
│   │  Source  │───►│ Transform │───►│ Query Oracle     │        │
│   │   Code   │    │           │    │ (trained.apr)    │        │
│   └──────────┘    └───────────┘    └────────┬─────────┘        │
│                                              │                  │
│                         ┌────────────────────┘                  │
│                         ▼                                       │
│   ┌─────────────────────────────────────────────────────┐      │
│   │              .apr Model File                         │      │
│   │                                                      │      │
│   │   • Decision pattern embeddings                      │      │
│   │   • Error→Fix mappings with confidence               │      │
│   │   • Tier progression state                           │      │
│   │   • CRC32 integrity checksum                         │      │
│   └─────────────────────────────────────────────────────┘      │
│                         │                                       │
│                         ▼                                       │
│   ┌──────────────┐    ┌───────────────┐    ┌────────────┐      │
│   │ Apply Best   │───►│   Compile     │───►│  Success/  │      │
│   │    Fix       │    │   & Verify    │    │   Retry    │      │
│   └──────────────┘    └───────────────┘    └────────────┘      │
└─────────────────────────────────────────────────────────────────┘
```

### Oracle File Format

```rust
/// .apr file structure with versioned header
struct OracleModel {
    header: OracleHeader,
    decision_embeddings: Vec<DecisionEmbedding>,
    error_fix_mappings: HashMap<ErrorCode, Vec<FixStrategy>>,
    tier_state: TierProgression,
    checksum: u32,  // CRC32
}

struct OracleHeader {
    magic: [u8; 4],      // "AORC" (Aprender ORaCle)
    version: u16,        // Format version
    created_at: u64,     // Unix timestamp
    training_samples: u64,
}
```

### Query API

```rust
// Query the oracle for fix suggestions
let oracle = OracleModel::load("trained.apr")?;

let suggestion = oracle.query(
    error_code: "E0308",
    error_context: "expected `i32`, found `String`",
    decision_history: &recent_decisions,
)?;

// Returns ranked fix strategies
for fix in suggestion.ranked_fixes {
    println!("Fix: {} (confidence: {:.1}%)",
             fix.description,
             fix.confidence * 100.0);
}
```

### Hybrid Retrieval (Sparse + Dense)

For large pattern libraries, the oracle uses hybrid retrieval combining:

1. **Sparse retrieval**: BM25 on error message text
2. **Dense retrieval**: Cosine similarity on decision embeddings

```rust
struct HybridRetriever {
    bm25_index: BM25Index,
    embedding_index: VectorIndex,
    alpha: f64,  // Weight for sparse vs dense (default: 0.5)
}

impl HybridRetriever {
    fn retrieve(&self, query: &Query, k: usize) -> Vec<FixCandidate> {
        let sparse_scores = self.bm25_index.search(&query.text, k * 2);
        let dense_scores = self.embedding_index.search(&query.embedding, k * 2);

        // Reciprocal rank fusion
        self.fuse_rankings(sparse_scores, dense_scores, k)
    }
}
```

## Golden Traces and Semantic Equivalence

Beyond syntactic compilation, CITL systems validate **semantic equivalence** between source and target programs using golden traces.

### Golden Traces with Lamport Clocks

A **golden trace** captures the complete execution behavior of a program with causal ordering:

```rust
struct GoldenTrace {
    /// Lamport timestamp for happens-before ordering
    lamport_clock: u64,

    /// Program execution events
    events: Vec<ExecutionEvent>,

    /// Syscall sequence for I/O equivalence
    syscalls: Vec<SyscallRecord>,

    /// Memory allocation pattern
    allocations: Vec<AllocationEvent>,
}

#[derive(Debug)]
enum ExecutionEvent {
    FunctionEntry { name: String, args: Vec<Value> },
    FunctionExit { name: String, result: Value },
    VariableAssign { name: String, value: Value },
    BranchTaken { condition: bool, location: SourceSpan },
}

struct SyscallRecord {
    number: i64,        // syscall number
    args: [u64; 6],     // arguments
    result: i64,        // return value
    timestamp: u64,     // Lamport clock
}
```

### Syscall-Level Semantic Validation

True semantic equivalence requires matching I/O behavior at the syscall level:

```
┌─────────────────────────────────────────────────────────────────┐
│              SYSCALL SEMANTIC VALIDATION                        │
│                                                                 │
│   Python Source          Transpiled Rust                        │
│   ─────────────          ───────────────                        │
│   open("f.txt")    ═══►  std::fs::File::open("f.txt")          │
│   ↓                      ↓                                      │
│   openat(AT_FDCWD,       openat(AT_FDCWD,                       │
│          "f.txt", ...)           "f.txt", ...)                  │
│                                                                 │
│   read(fd, buf, n) ═══►  file.read(&mut buf)                   │
│   ↓                      ↓                                      │
│   read(3, ptr, 4096)     read(3, ptr, 4096)                     │
│                                                                 │
│   close(fd)        ═══►  drop(file)                            │
│   ↓                      ↓                                      │
│   close(3)               close(3)                               │
│                                                                 │
│   VERDICT: ✅ SEMANTICALLY EQUIVALENT                           │
│   (Same syscall sequence with compatible arguments)             │
└─────────────────────────────────────────────────────────────────┘
```

### Performance Metrics from Real-World Transpilation

Syscall-level validation reveals optimization opportunities:

```
┌─────────────────────────────────────────────────────────────────┐
│              REAL-WORLD PERFORMANCE GAINS                       │
│                                                                 │
│   Metric                    Python    Rust      Improvement     │
│   ────────────────────────  ──────    ────      ───────────     │
│   Total syscalls            185,432   10,073    18.4× fewer     │
│   Memory allocations        45,231    2,891     15.6× fewer     │
│   Context switches          1,203     89        13.5× fewer     │
│   Peak RSS (MB)             127.4     23.8      5.4× smaller    │
│   Wall clock time (s)       4.23      0.31      13.6× faster    │
│                                                                 │
│   Source: reprorusted-python-cli benchmark suite                │
└─────────────────────────────────────────────────────────────────┘
```

### Trace Comparison Algorithm

```rust
fn compare_traces(golden: &GoldenTrace, actual: &GoldenTrace) -> EquivalenceResult {
    // 1. Check syscall sequence equivalence (relaxed ordering)
    let syscall_match = compare_syscalls_relaxed(
        &golden.syscalls,
        &actual.syscalls
    );

    // 2. Check function call/return equivalence
    let function_match = compare_function_events(
        &golden.events,
        &actual.events
    );

    // 3. Check observable state at program end
    let state_match = compare_final_state(golden, actual);

    EquivalenceResult {
        semantically_equivalent: syscall_match && function_match && state_match,
        syscall_reduction: compute_reduction(&golden.syscalls, &actual.syscalls),
        performance_improvement: compute_perf_improvement(golden, actual),
    }
}
```

## Practical Example: Depyler Oracle

The **depyler** Python-to-Rust transpiler demonstrates CITL in practice:

```
┌─────────────────────────────────────────────────────────────────┐
│                    DEPYLER ORACLE SYSTEM                        │
│                                                                 │
│   Input: Python source code                                     │
│                                                                 │
│   1. Parse Python → AST                                         │
│   2. Transform AST → HIR (High-level IR)                        │
│   3. Generate Rust code from HIR                                │
│   4. Attempt compilation with rustc                             │
│                                                                 │
│   If compilation fails:                                         │
│     - Parse error message (E0308, E0382, E0597, etc.)           │
│     - Match against known error patterns                        │
│     - Apply learned fix strategy                                │
│     - Retry compilation                                         │
│                                                                 │
│   Training data: (error_pattern, context) → successful_fix      │
└─────────────────────────────────────────────────────────────────┘
```

### Error Pattern Learning

```rust
// Depyler learns mappings like:
//
// [E0308] mismatched types: expected `Vec<_>`, found `&[_]`
//   → Apply: .to_vec()
//
// [E0382] borrow of moved value
//   → Apply: .clone() before move
//
// [E0597] borrowed value does not live long enough
//   → Apply: Restructure scoping or use owned type
```

### The Oracle's Training Sample Structure

```rust
struct TrainingSample {
    /// The Python source that was transpiled
    python_source: String,

    /// The initial (incorrect) Rust output
    initial_rust: String,

    /// The compiler error received
    compiler_error: CompilerError,

    /// The corrected Rust code that compiles
    corrected_rust: String,

    /// The fix that was applied
    fix_applied: Fix,
}

struct CompilerError {
    code: String,           // "E0308"
    message: String,        // "mismatched types"
    span: SourceSpan,       // Location in code
    expected: Option<Type>, // Expected type
    found: Option<Type>,    // Actual type
    suggestions: Vec<String>,
}
```

## Comparison with Other Learning Paradigms

| Paradigm | Feedback Source | Cost | Latency | Accuracy |
|----------|----------------|------|---------|----------|
| Supervised Learning | Human labels | High | Days | Subjective |
| RLHF | Human preferences | Very High | Hours | Noisy |
| **CITL/RLCF** | **Compiler** | **Free** | **Milliseconds** | **Perfect** |
| Self-Supervised | Data structure | Free | Variable | Task-dependent |
| Semi-Supervised | Partial labels | Medium | Variable | Moderate |

## Advantages of Compiler-in-the-Loop

1. **Perfect Oracle**: Compilers are deterministic - code either compiles or it doesn't
2. **Rich Error Messages**: Modern compilers (especially Rust) provide detailed diagnostics
3. **Free at Scale**: No human annotation cost
4. **Instant Feedback**: Compilation takes milliseconds
5. **Objective Ground Truth**: No inter-annotator disagreement

## Challenges and Limitations

1. **Semantic Correctness**: Code that compiles isn't necessarily correct
   - Solution: Combine with test execution

2. **Multiple Valid Solutions**: Many ways to fix an error
   - Solution: Prefer minimal changes, use heuristics

3. **Error Message Quality**: Varies by compiler
   - Rust: Excellent diagnostics
   - C++: Often cryptic template errors

4. **Distribution Shift**: Training errors may differ from production
   - Solution: Diverse training corpus

## Exporting Training Data for ML Pipelines

CITL systems generate valuable training corpora. The **depyler** project supports exporting this data for downstream ML consumption via the **Organizational Intelligence Plugin (OIP)**.

### Export Command

```bash
# Export to Parquet (recommended for large corpora)
depyler oracle export-oip -i ./python_sources -o corpus.parquet --format parquet

# Export to JSONL (human-readable)
depyler oracle export-oip -i ./python_sources -o corpus.jsonl --format jsonl

# With confidence filtering and reweighting
depyler oracle export-oip -i ./src \
    -o training_data.parquet \
    --min-confidence 0.80 \
    --include-clippy \
    --reweight 1.5
```

### OIP Training Example Schema

Each exported sample contains rich diagnostic metadata:

```rust
struct OipTrainingExample {
    source_file: String,       // Original Python file
    rust_file: String,         // Generated Rust file
    error_code: Option<String>, // E0308, E0277, etc.
    clippy_lint: Option<String>, // Optional Clippy lint
    level: String,             // error, warning
    message: String,           // Full diagnostic message
    oip_category: String,      // DefectCategory taxonomy
    confidence: f64,           // Mapping confidence (0.0-1.0)
    line_start: i64,           // Error location
    line_end: i64,
    suggestion: Option<String>, // Compiler suggestion
    python_construct: Option<String>, // Source Python pattern
    weight: f32,               // Sample weight for training
}
```

### Error Code to DefectCategory Mapping

Rust error codes map to OIP's DefectCategory taxonomy:

| Error Code | OIP Category | Confidence |
|------------|--------------|------------|
| E0308 | TypeErrors | 0.95 |
| E0277 | TraitBounds | 0.95 |
| E0502, E0503, E0505 | OwnershipBorrow | 0.95 |
| E0597, E0499, E0716 | LifetimeErrors | 0.90 |
| E0433, E0412 | ImportResolution | 0.90 |
| E0425, E0599 | NameResolution | 0.85 |
| E0428, E0592 | DuplicateDefinitions | 0.85 |

### Feldman Long-Tail Reweighting

For imbalanced error distributions, apply reweighting to emphasize rare error classes:

```bash
# Apply 1.5x weight boost to rare categories
depyler oracle export-oip -i ./src -o corpus.parquet --reweight 1.5
```

This implements Feldman (2020) long-tail weighting, ensuring rare but important error patterns aren't drowned out by common type mismatches.

### Integration with alimentar

Export uses **alimentar** for efficient Arrow-based serialization:

```rust
use alimentar::ArrowDataset;

// Load exported corpus
let dataset = ArrowDataset::from_parquet("corpus.parquet")?;

// Create batched DataLoader for training
let loader = dataset
    .shuffle(true)
    .batch_size(32)
    .into_loader()?;

for batch in loader {
    // Train on batch...
}
```

### Running Examples

Try alimentar's data loading examples to see the pipeline in action:

```bash
# Clone and run alimentar examples
cd alimentar

# Basic loading (Parquet, CSV, JSON)
cargo run --example basic_loading

# Batched DataLoader with shuffling
cargo run --example dataloader_batching

# Streaming for large corpora (memory-bounded)
cargo run --example streaming_large

# Data quality validation
cargo run --example quality_check
```

End-to-end CITL export workflow:

```bash
# 1. Generate training corpus from Python files
depyler oracle improve -i ./python_src --export-corpus ./corpus.jsonl

# 2. Export to Parquet for ML consumption
depyler oracle export-oip -i ./python_src -o ./corpus.parquet --format parquet

# 3. Load in your training script
cargo run --example basic_loading  # Adapt for corpus.parquet
```

## Implementation in Aprender

Aprender provides building blocks for CITL systems:

```rust
use aprender::nn::{Module, Linear, Sequential};
use aprender::transfer::{OnlineDistillation, ProgressiveDistillation};

// Error pattern classifier
let error_classifier = Sequential::new()
    .add(Linear::new(error_embedding_dim, 256))
    .add(ReLU::new())
    .add(Linear::new(256, num_error_types));

// Fix strategy predictor
let fix_predictor = Sequential::new()
    .add(Linear::new(context_dim, 512))
    .add(ReLU::new())
    .add(Linear::new(512, num_fix_strategies));
```

## Research Directions

1. **Multi-Compiler Learning**: Train on feedback from multiple compilers (GCC, Clang, rustc)
2. **Error Explanation Generation**: Generate human-readable explanations alongside fixes
3. **Proactive Error Prevention**: Predict errors before generation
4. **Cross-Language Transfer**: Apply patterns learned from one language to another
5. **Formal Verification Integration**: Combine compiler feedback with theorem provers

## Key Papers and Resources

- Gupta et al. (2017). "DeepFix: Fixing Common C Language Errors by Deep Learning"
- Yasunaga & Liang (2020). "Graph-based, Self-Supervised Program Repair"
- Chen et al. (2021). "Evaluating Large Language Models Trained on Code" (Codex)
- Jain et al. (2022). "Jigsaw: Large Language Models meet Program Synthesis"
- Meta (2022). "Getafix: Learning to Fix Bugs Automatically"

## Summary

Compiler-in-the-Loop Learning represents a powerful paradigm for automated code transformation and repair. By treating the compiler as an oracle, systems can:

- Learn from unlimited free feedback
- Achieve objective correctness metrics
- Scale without human annotation bottlenecks
- Iteratively improve through self-training

The key insight: **compilers are perfect teachers** - they never lie about correctness, provide detailed explanations, and are available 24/7 at zero cost.