Skip to main content

vibe/stdlib_v2/topology_and_manifold/
mod.rs

1//! VibeScript v0.02 Standard Library — Track 4: Simplicial/Sheaf/Homology Topology,
2//! AI-Native Ontological Manifold, Algebraic Effects & Hardware-Sympathetic Systems.
3
4pub mod effects_and_systems;
5pub mod ontological_context;
6pub mod simplex_and_sheaf;
7
8use crate::error::{DiagCode, Diagnostic};
9use crate::span::Span;
10use crate::value::Value;
11use std::collections::BTreeMap;
12
13/// All Track 4 standard library bindings registered for VibeScript v0.02.
14pub const TOPOLOGY_AND_MANIFOLD_BINDINGS: &[&str] = &[
15    // Simplicial complexes, hypergraphs, cellular sheaves & persistent homology (§22)
16    "simplex.define",
17    "simplex.fold",
18    "hypergraph.bind_hyperedge",
19    "sheaf.project_stalk",
20    "sheaf.verify_intersection",
21    "sheaf.apply_glue",
22    "topology.betti_numbers",
23    "topology.detect_voids",
24    "homotopy.verify_equivalence",
25    // AI-native ontological context manifold, latent pointers & structural AST deltas (§21.4)
26    "context.fork_by_ontology",
27    "context.spawn_swarm",
28    "context.collapse_manifold",
29    "manifold.mount",
30    "context.to_latent_pointer",
31    "context.query_distance",
32    "inference.next_valid_logit_mask",
33    "ast.apply_structural_delta",
34    "diagnostic.to_compact_token_error",
35    // Algebraic effects, content-addressed code, concurrency, contracts, CRDT/ZK & systems (§§14–19)
36    "effect.perform",
37    "effect.handle",
38    "effect.resume",
39    "code.hash_ast",
40    "code.resolve",
41    "code.spawn_at",
42    "task.spawn",
43    "task.await",
44    "task.status",
45    "task.nursery",
46    "task.cancel",
47    "task.with_timeout",
48    "supervisor.start_link",
49    "contract.requires",
50    "contract.ensures",
51    "contract.invariant",
52    "contract.decreases",
53    "state.crdt_merge",
54    "value.attach_provenance",
55    "value.verify_provenance",
56    "crypto.prove_zk_predicate",
57    "crypto.verify_zk_predicate",
58    "crypto.commit_predicate",
59    "crypto.verify_commitment",
60    "runtime.with_budget",
61    "continuation.snapshot",
62    "continuation.rollback_to",
63    "memory.with_arena",
64    "memory.perceus_plan",
65    "memory.layout_soa",
66    "simd.validate_boundary",
67    "channel.lock_free_spsc",
68];
69
70/// Dispatch a Track 4 `topology_and_manifold` call. Returns `Ok(None)` if `path` is not owned here.
71pub fn call_topology_and_manifold(
72    path: &str,
73    args: &[Value],
74    span: Span,
75) -> Result<Option<Value>, Diagnostic> {
76    if let Some(v) = simplex_and_sheaf::call_simplex_and_sheaf(path, args, span)? {
77        return Ok(Some(v));
78    }
79    if let Some(v) = ontological_context::call_ontological_context(path, args, span)? {
80        return Ok(Some(v));
81    }
82    if let Some(v) = effects_and_systems::call_effects_and_systems(path, args, span)? {
83        return Ok(Some(v));
84    }
85    Ok(None)
86}
87
88pub(crate) fn fnv1a_64(bytes: &[u8]) -> u64 {
89    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
90    for &b in bytes {
91        hash ^= b as u64;
92        hash = hash.wrapping_mul(0x0100_0000_01b3);
93    }
94    hash
95}
96
97pub(crate) fn fnv1a_value(val: &Value) -> u64 {
98    fnv1a_64(format!("{val:?}").as_bytes())
99}
100
101pub(crate) fn extract_str<'a>(
102    args: &'a [Value],
103    idx: usize,
104    fn_name: &str,
105    span: Span,
106) -> Result<&'a str, Diagnostic> {
107    match args.get(idx) {
108        Some(Value::String(s) | Value::Iri(s)) => Ok(s.as_str()),
109        _ => Err(Diagnostic::new(
110            DiagCode::E100,
111            span,
112            format!("{fn_name}: argument {idx} must be a String or Iri"),
113        )),
114    }
115}
116
117pub(crate) fn extract_i64(
118    args: &[Value],
119    idx: usize,
120    fn_name: &str,
121    span: Span,
122) -> Result<i64, Diagnostic> {
123    match args.get(idx) {
124        Some(Value::I64(n)) => Ok(*n),
125        Some(Value::U64(n)) => Ok(*n as i64),
126        _ => Err(Diagnostic::new(
127            DiagCode::E100,
128            span,
129            format!("{fn_name}: argument {idx} must be an integer"),
130        )),
131    }
132}
133
134pub(crate) fn extract_record<'a>(
135    args: &'a [Value],
136    idx: usize,
137    fn_name: &str,
138    span: Span,
139) -> Result<&'a BTreeMap<String, Value>, Diagnostic> {
140    match args.get(idx) {
141        Some(Value::Record(r)) => Ok(r),
142        _ => Err(Diagnostic::new(
143            DiagCode::E100,
144            span,
145            format!("{fn_name}: argument {idx} must be a Record"),
146        )),
147    }
148}
149
150pub(crate) fn extract_list<'a>(
151    args: &'a [Value],
152    idx: usize,
153    fn_name: &str,
154    span: Span,
155) -> Result<&'a [Value], Diagnostic> {
156    match args.get(idx) {
157        Some(Value::List(l)) => Ok(l.as_slice()),
158        _ => Err(Diagnostic::new(
159            DiagCode::E100,
160            span,
161            format!("{fn_name}: argument {idx} must be a List"),
162        )),
163    }
164}