File size: 16,537 Bytes
1d3f990 | 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 | # ROWM API Reference β Crate-by-Crate Guide
**Version:** 1.0.0
**Status:** Normative
**Authors:** Ahmad Ali Parr, Jessica SNAPKITTYWEST
---
## Overview
ROWM consists of 8 core Rust crates, each with distinct responsibilities. This document provides quick reference for each crate's public API.
For detailed implementation, see source files in `crates/*/src/`.
---
## 1. subleq-vm β Virtual Machine Core
**Location:** `crates/subleq-vm/`
**Purpose:** Execute SUBLEQ bytecode with mutation tracking and checkpointing
### Public Types
```rust
pub struct Memory {
cells: Vec<i64>, // Von Neumann unified address space
}
pub struct VirtualMachine {
memory: Memory,
instruction_pointer: usize,
mutations: Vec<MutationEvent>,
checkpoints: VecDeque<Checkpoint>,
}
pub struct MutationEvent {
pub address: usize,
pub old_value: i64,
pub new_value: i64,
pub timestamp: u64,
}
pub struct Checkpoint {
pub id: Uuid,
pub memory_snapshot: Vec<i64>,
pub instruction_pointer: usize,
pub timestamp: u64,
pub hash: String, // Blake3
}
```
### Public Methods
```rust
impl VirtualMachine {
pub fn new(program: Vec<i64>) -> Self;
pub fn execute(&mut self) -> Result<ExecutionResult>;
pub fn step(&mut self) -> Result<Option<MutationEvent>>;
pub fn create_checkpoint(&mut self) -> Uuid;
pub fn rollback(&mut self, checkpoint_id: Uuid) -> Result<()>;
pub fn get_mutations(&self) -> &[MutationEvent];
pub fn memory_at(&self, address: usize) -> i64;
pub fn set_memory(&mut self, address: usize, value: i64);
pub fn get_telemetry(&self) -> TelemetrySnapshot;
}
pub struct ExecutionResult {
pub status: ExecutionStatus, // Normal | Timeout | ViolationHalt
pub final_memory: Vec<i64>,
pub mutation_count: usize,
pub checkpoint_count: usize,
pub total_cycles: u64,
}
pub enum ExecutionStatus {
Normal,
Timeout,
ViolationHalt,
InvariantViolation(String),
}
```
---
## 2. subleq-ir β Intermediate Representation
**Location:** `crates/subleq-ir/`
**Purpose:** Parse source code to AST, compile to bytecode, lower to SUBLEQ
### Public Types
```rust
pub enum Expr {
Const(i64),
Var(String),
BinOp(BinOp, Box<Expr>, Box<Expr>),
UnOp(UnOp, Box<Expr>),
If(Box<Expr>, Box<Expr>, Box<Expr>),
Call(String, Vec<Expr>),
}
pub enum Stmt {
Let(String, Expr),
Assign(String, Expr),
If(Expr, Vec<Stmt>, Vec<Stmt>),
While(Expr, Vec<Stmt>),
Return(Expr),
FunctionDef(String, Vec<String>, Vec<Stmt>),
}
pub struct Program {
pub statements: Vec<Stmt>,
pub functions: HashMap<String, Function>,
}
pub enum Bytecode {
LoadConst(i64),
LoadVar(String),
Store(String),
BinOp(BinOp),
Jump(usize),
JumpIfZero(usize),
Call(String),
Return,
}
pub struct Type {
pub kind: TypeKind, // Int64 | Array | Function | Unknown
pub nullable: bool,
}
```
### Public Methods
```rust
impl Program {
pub fn from_ast(ast: Vec<Stmt>) -> Result<Self>;
pub fn to_bytecode(&self) -> Result<Vec<Bytecode>>;
pub fn to_subleq(&self) -> Result<Vec<i64>>;
pub fn validate(&self) -> Result<Vec<ValidationError>>;
}
pub struct BytecodeCompiler;
impl BytecodeCompiler {
pub fn compile(ast: &Program) -> Result<Vec<Bytecode>>;
pub fn allocate_registers(bytecode: &[Bytecode]) -> RegisterMap;
}
pub struct SubleqCodegen;
impl SubleqCodegen {
pub fn lower(bytecode: &[Bytecode], register_map: &RegisterMap) -> Vec<i64>;
}
```
---
## 3. polyglot-frontend β Multi-Language Parsing
**Location:** `crates/polyglot-frontend/`
**Purpose:** Parse 30+ languages, normalize to unified AST
### Public Types
```rust
pub enum Language {
// Tier 1 (Full)
Rust, Python, JavaScript, Subleq,
// Tier 2 (Solid)
Haskell, Ada, Agda, Lean,
// Tier 3 (Supported)
Prolog, Lisp, Scheme, Bqn,
// Tier 4 (Partial)
C, Go, Zig, Apl, Forth,
// Tier 5 (Experimental)
Factor, Brainfuck, J, Holyc, Emojicode,
}
pub struct LanguageRegistry {
parsers: HashMap<Language, Box<dyn Parser>>,
}
pub trait Parser: Send + Sync {
fn parse(&self, source: &str) -> Result<Ast>;
fn language(&self) -> Language;
}
pub struct Ast {
pub root: AstNode,
pub source_hash: String, // Blake3(source)
}
pub enum AstNode {
Program(Vec<AstNode>),
Function(FunctionDef),
Statement(Statement),
Expression(Expression),
}
```
### Public Methods
```rust
impl LanguageRegistry {
pub fn new() -> Self;
pub fn register(&mut self, language: Language, parser: Box<dyn Parser>);
pub fn parse(&self, language: Language, source: &str) -> Result<Ast>;
pub fn detect_language(&self, source: &str) -> Option<Language>;
pub fn supported_languages(&self) -> Vec<Language>;
}
pub struct PytonParser;
impl Parser for PythonParser {
fn parse(&self, source: &str) -> Result<Ast>;
fn language(&self) -> Language { Language::Python }
}
pub struct RustParser;
impl Parser for RustParser {
fn parse(&self, source: &str) -> Result<Ast>;
fn language(&self) -> Language { Language::Rust }
}
```
---
## 4. invariant-extractor β Symbolic Execution & Verification
**Location:** `crates/invariant-extractor/`
**Purpose:** Extract loop invariants, generate proof obligations
### Public Types
```rust
pub enum SymbolicValue {
Const(i64),
Mem(usize), // memory address
Reg(usize), // register index
BinOp(BinOp, Box<SymbolicValue>, Box<SymbolicValue>),
UnOp(UnOp, Box<SymbolicValue>),
}
pub enum Predicate {
True,
False,
Eq(SymbolicValue, SymbolicValue),
Le(SymbolicValue, SymbolicValue),
And(Box<Predicate>, Box<Predicate>),
Or(Box<Predicate>, Box<Predicate>),
Not(Box<Predicate>),
}
pub struct SymbolicState {
pub registers: HashMap<usize, SymbolicValue>,
pub memory: HashMap<usize, SymbolicValue>,
pub assumptions: Vec<Predicate>,
}
pub struct ProofObligation {
pub id: String,
pub name: String, // InvariantPreservation, etc
pub formula: Predicate,
pub status: ProofStatus,
}
pub enum ProofStatus {
Unknown,
Proved,
Disproved,
Manual,
}
pub struct Invariant {
pub loop_id: usize,
pub predicate: Predicate,
pub proof_strategy: ProofStrategy,
}
```
### Public Methods
```rust
pub struct InvariantExtractor;
impl InvariantExtractor {
pub fn extract(&self, bytecode: &[Bytecode]) -> Result<Vec<Invariant>>;
pub fn generate_proof_obligations(&self, program: &Program) -> Vec<ProofObligation>;
pub fn compute_abstract_domain(bytecode: &[Bytecode]) -> AbstractDomain;
}
pub struct SymbolicExecutor;
impl SymbolicExecutor {
pub fn trace(bytecode: &[Bytecode]) -> Result<Vec<ExecutionPath>>;
pub fn evaluate(&self, expr: &SymbolicValue, state: &SymbolicState) -> SymbolicValue;
}
pub struct AbstractInterpreter;
impl AbstractInterpreter {
pub fn fixpoint(bytecode: &[Bytecode]) -> HashMap<usize, Interval>;
pub fn widen(v1: Interval, v2: Interval) -> Interval;
pub fn narrow(v: Interval, constraint: &Predicate) -> Interval;
}
```
---
## 5. proof-validator β Proof Checking & WORM Rollback
**Location:** `crates/proof-validator/`
**Purpose:** Type-check proofs (Curry-Howard), manage checkpoints for rollback
### Public Types
```rust
pub enum ProofTerm {
Var(String),
Abs(String, Box<ProofTerm>), // lambda
App(Box<ProofTerm>, Box<ProofTerm>), // application
Const(Constant),
Pair(Box<ProofTerm>, Box<ProofTerm>),
Fst(Box<ProofTerm>), // projection
Snd(Box<ProofTerm>),
}
pub struct ProofContext {
pub assumptions: Vec<(String, ProofTerm)>,
}
pub struct ProofObligation {
pub id: String,
pub theorem: Predicate,
pub required_by_stage: String, // "compiled" | "executed" | "verified"
}
pub struct RollbackManager {
checkpoints: VecDeque<Checkpoint>,
max_checkpoints: usize,
}
pub struct ProofValidator;
```
### Public Methods
```rust
pub struct TypeChecker;
impl TypeChecker {
pub fn check(&self, term: &ProofTerm, expected_type: &Type, ctx: &ProofContext) -> Result<()>;
pub fn infer(&self, term: &ProofTerm, ctx: &ProofContext) -> Result<Type>;
}
impl RollbackManager {
pub fn new(max_checkpoints: usize) -> Self;
pub fn push(&mut self, checkpoint: Checkpoint) -> Result<()>;
pub fn pop(&mut self) -> Option<Checkpoint>;
pub fn rollback_to(&mut self, checkpoint_id: Uuid) -> Result<()>;
pub fn list_checkpoints(&self) -> Vec<CheckpointSummary>;
}
pub struct ProofValidator;
impl ProofValidator {
pub fn validate_obligation(&self, obligation: &ProofObligation) -> Result<ProofStatus>;
pub fn emit_audit(&self, event: ProofEvent) -> Result<()>;
}
pub enum ProofEvent {
Validated { obligation_id: String, result: ProofStatus },
Violated { obligation_id: String, evidence: String },
RolledBack { checkpoint_id: Uuid },
}
```
---
## 6. m4-morph β Macro Engine & Self-Modification
**Location:** `crates/m4-morph/`
**Purpose:** GNU M4 macro expansion with sandboxing and state feedback
### Public Types
```rust
pub struct SandboxLimits {
pub max_expansion_depth: usize, // default 100
pub max_output_size: usize, // default 1MB
pub max_recursion: usize, // default 50
pub timeout_ms: u64, // default 5000
}
pub enum SandboxPreset {
Permissive, // for trusted sources
Strict, // for untrusted sources
}
pub struct FeedbackBuffer {
pub definitions: VecDeque<(String, String)>, // bounded 50
pub outputs: VecDeque<String>, // bounded 100
}
pub struct M4Engine {
pub sandbox: SandboxConfig,
pub feedback: FeedbackBuffer,
}
pub struct M4Engine {
pub sandbox: SandboxConfig,
}
```
### Public Methods
```rust
impl M4Engine {
pub fn new(limits: SandboxLimits) -> Self;
pub fn expand(&mut self, source: &str) -> Result<String>;
pub fn define(&mut self, name: &str, value: &str);
pub fn get_definition(&self, name: &str) -> Option<&str>;
pub fn set_sandbox(&mut self, config: SandboxConfig);
}
pub struct FeedbackBuffer;
impl FeedbackBuffer {
pub fn push_definition(&mut self, name: String, value: String);
pub fn push_output(&mut self, output: String);
pub fn get_recent_outputs(&self, n: usize) -> Vec<String>;
pub fn get_all_definitions(&self) -> Vec<(String, String)>;
}
```
---
## 7. notebook-kernel β Jupyter Protocol & Cell Execution
**Location:** `crates/notebook-kernel/`
**Purpose:** Jupyter protocol implementation, cell-to-cell communication, execution ring
### Public Types
```rust
pub enum JupyterMessageType {
ExecuteRequest,
ExecuteReply,
DisplayData,
Stream,
Error,
Status,
}
pub struct JupyterMessage {
pub message_type: JupyterMessageType,
pub metadata: HashMap<String, String>,
pub content: serde_json::Value,
}
pub struct NotebookKernel {
pub kernel_id: String,
pub execution_ring: ExecutionRing,
pub ipc_channels: HashMap<usize, IpcChannel>,
}
pub struct CellConfig {
pub cell_id: String,
pub language: Language,
pub kernel: String,
pub visibility: CellVisibility,
}
pub enum CellVisibility {
Visible,
Hidden,
Collapsed,
}
pub struct ExecutionRing {
pub work_queue: VecDeque<CellInstruction>,
pub active_cells: HashSet<String>,
}
pub struct IpcChannel {
pub shared_buffer: Arc<RwLock<Vec<u8>>>,
}
```
### Public Methods
```rust
impl NotebookKernel {
pub fn new(kernel_id: String) -> Self;
pub fn handle_message(&mut self, msg: JupyterMessage) -> Result<()>;
pub fn execute_cell(&mut self, cell: CellConfig, source: &str) -> Result<CellOutput>;
pub fn get_execution_status(&self) -> ExecutionStatus;
}
impl ExecutionRing {
pub fn enqueue(&mut self, instruction: CellInstruction);
pub fn dequeue(&mut self) -> Option<CellInstruction>;
pub fn poll(&mut self) -> Vec<ExecutionEvent>;
}
impl IpcChannel {
pub fn send(&self, data: &[u8]) -> Result<()>;
pub fn recv(&self) -> Result<Vec<u8>>;
}
```
---
## 8. notebook-orchestrator β Non-Recursive Receipt Chain
**Location:** `crates/notebook-orchestrator/`
**Purpose:** 8-stage pipeline, receipt generation, Prolog integration
### Public Types
```rust
pub struct Instruction {
pub id: String, // SHA-256
pub agent: String,
pub capability: String,
pub runtime: String,
pub permission: String,
pub payload: Vec<u8>,
}
pub enum Stage {
Receive,
Translate,
Verify,
Dispatch,
Execute,
Encode,
Seal,
Complete,
}
pub struct Receipt {
pub id: String, // Blake3(contents)
pub sequence: usize,
pub instruction_hash: String,
pub stage: Stage,
pub result: ExecutionResult,
pub timestamp: u64,
pub signature: String, // Ed25519
pub previous_receipt_hash: String, // chain link
}
pub struct Orchestrator {
pub work_queue: VecDeque<Instruction>,
pub receipts: Vec<Receipt>,
pub prolog_engine: PrologBridge,
}
```
### Public Methods
```rust
pub struct Orchestrator;
impl Orchestrator {
pub fn new(prolog_path: &str) -> Result<Self>;
pub fn process_instruction(&mut self, instr: Instruction) -> Result<Receipt>;
pub fn get_receipt_chain(&self) -> Vec<Receipt>;
pub fn verify_chain_integrity(&self) -> bool;
pub fn emit_receipt(&mut self, receipt: Receipt) -> Result<()>;
}
pub struct PrologBridge;
impl PrologBridge {
pub fn query(&self, predicate: &str, args: &[&str]) -> Result<Vec<String>>;
pub fn assert_fact(&self, fact: &str) -> Result<()>;
pub fn check_authorization(&self, agent: &str, cap: &str, runtime: &str, perm: &str) -> Result<bool>;
pub fn is_release_ready(&self) -> Result<bool>;
}
impl Receipt {
pub fn compute_hash(contents: &[u8]) -> String;
pub fn verify_signature(&self, pubkey: &str) -> Result<bool>;
}
```
---
## Error Handling
All public methods return `Result<T, Error>` where `Error` implements `std::error::Error`.
```rust
pub enum Error {
ParseError(String),
TypeError(String),
ExecutionError(String),
ProofError(String),
AuthorizationError(String),
TimeoutError(String),
ReceiptError(String),
}
```
---
## Examples
### Execute SUBLEQ Bytecode
```rust
use subleq_vm::{VirtualMachine, ExecutionStatus};
let program = vec![
0, 0, 3, // M[0] -= M[0]; IP=3
100, 100, 106, // M[100] -= M[100]; IP=106
// ... rest of program
];
let mut vm = VirtualMachine::new(program);
let result = vm.execute()?;
match result.status {
ExecutionStatus::Normal => println!("Success: {} mutations", result.mutation_count),
ExecutionStatus::Timeout => println!("Timed out"),
ExecutionStatus::ViolationHalt => println!("Invariant violated"),
}
```
### Parse Python and Compile to SUBLEQ
```rust
use polyglot_frontend::{LanguageRegistry, Language};
use subleq_ir::Program;
let mut registry = LanguageRegistry::new();
let python_parser = PythonParser::new();
registry.register(Language::Python, Box::new(python_parser));
let source = r#"
x = 10
y = 20
z = x + y
"#;
let ast = registry.parse(Language::Python, source)?;
let program = Program::from_ast(ast)?;
let bytecode = program.to_bytecode()?;
let subleq = program.to_subleq()?;
```
### Extract Invariants and Generate Proofs
```rust
use invariant_extractor::InvariantExtractor;
let extractor = InvariantExtractor::new();
let invariants = extractor.extract(&bytecode)?;
let obligations = extractor.generate_proof_obligations(&program)?;
for obligation in obligations {
println!("Obligation: {}", obligation.name);
println!("Formula: {:?}", obligation.formula);
}
```
---
**For detailed implementation examples, consult crate-specific documentation in each source directory.**
*"LOC WRITES. LEDGER CERTIFIES. METATRON SEALS."*
|