logicaffeine_data/
types.rs

1//! Core runtime type definitions.
2//!
3//! This module defines the primitive types used by LOGOS programs at runtime.
4//! These are type aliases that map LOGOS types to their Rust equivalents.
5//!
6//! ## Type Mappings
7//!
8//! | LOGOS Type | Rust Type | Description |
9//! |------------|-----------|-------------|
10//! | `Nat` | `u64` | Natural numbers (non-negative) |
11//! | `Int` | `i64` | Signed integers |
12//! | `Real` | `f64` | Floating-point numbers |
13//! | `Text` | `String` | UTF-8 strings |
14//! | `Bool` | `bool` | Boolean values |
15//! | `Unit` | `()` | The unit type |
16//! | `Char` | `char` | Unicode scalar values |
17//! | `Byte` | `u8` | Raw bytes |
18//! | `Seq<T>` | `Vec<T>` | Ordered sequences |
19//! | `Set<T>` | `HashSet<T>` | Unordered unique elements |
20//! | `Map<K,V>` | `HashMap<K,V>` | Key-value mappings |
21
22use std::hash::Hash;
23
24/// Non-negative integers. Maps to Peano `Nat` in the kernel.
25pub type Nat = u64;
26/// Signed integers.
27pub type Int = i64;
28/// IEEE 754 floating-point numbers.
29pub type Real = f64;
30/// UTF-8 encoded text strings.
31pub type Text = String;
32/// Boolean truth values.
33pub type Bool = bool;
34/// The unit type (single value).
35pub type Unit = ();
36/// Unicode scalar values.
37pub type Char = char;
38/// Raw bytes (0-255).
39pub type Byte = u8;
40
41/// Ordered sequences (lists).
42pub type Seq<T> = Vec<T>;
43
44/// Key-value mappings with hash-based lookup.
45pub type Map<K, V> = std::collections::HashMap<K, V>;
46
47/// Unordered collections of unique elements.
48pub type Set<T> = std::collections::HashSet<T>;
49
50/// Unified containment testing for all collection types.
51///
52/// This trait provides a consistent `logos_contains` method across Logos's
53/// collection types, abstracting over the different containment semantics
54/// of vectors (by value), sets (by membership), maps (by key), and
55/// strings (by substring or character).
56///
57/// # Implementations
58///
59/// - [`Vec<T>`]: Tests if the vector contains an element equal to the value
60/// - [`HashSet<T>`]: Tests if the element is a member of the set
61/// - [`HashMap<K, V>`]: Tests if a key exists in the map
62/// - [`String`]: Tests for substring (`&str`) or character (`char`) presence
63/// - [`ORSet<T, B>`]: Tests if the element is in the CRDT set
64///
65/// # Examples
66///
67/// ```
68/// use logicaffeine_data::LogosContains;
69///
70/// // Vector: contains by value equality
71/// let v = vec![1, 2, 3];
72/// assert!(v.logos_contains(&2));
73/// assert!(!v.logos_contains(&5));
74///
75/// // String: contains by substring
76/// let s = String::from("hello world");
77/// assert!(s.logos_contains(&"world"));
78///
79/// // String: contains by character
80/// assert!(s.logos_contains(&'o'));
81/// ```
82pub trait LogosContains<T> {
83    /// Check if this collection contains the given value.
84    fn logos_contains(&self, value: &T) -> bool;
85}
86
87impl<T: PartialEq> LogosContains<T> for Vec<T> {
88    fn logos_contains(&self, value: &T) -> bool {
89        self.contains(value)
90    }
91}
92
93impl<T: Eq + Hash> LogosContains<T> for std::collections::HashSet<T> {
94    fn logos_contains(&self, value: &T) -> bool {
95        self.contains(value)
96    }
97}
98
99impl<K: Eq + Hash, V> LogosContains<K> for std::collections::HashMap<K, V> {
100    fn logos_contains(&self, key: &K) -> bool {
101        self.contains_key(key)
102    }
103}
104
105impl LogosContains<&str> for String {
106    fn logos_contains(&self, value: &&str) -> bool {
107        self.contains(*value)
108    }
109}
110
111impl LogosContains<char> for String {
112    fn logos_contains(&self, value: &char) -> bool {
113        self.contains(*value)
114    }
115}
116
117impl<T: Eq + Hash + Clone, B: crate::crdt::SetBias> LogosContains<T>
118    for crate::crdt::ORSet<T, B>
119{
120    fn logos_contains(&self, value: &T) -> bool {
121        self.contains(value)
122    }
123}
124
125/// Dynamic value type for heterogeneous collections.
126///
127/// `Value` enables tuples and other heterogeneous data structures in Logos.
128/// It supports basic arithmetic between compatible types and provides
129/// runtime type coercion where sensible.
130///
131/// # Variants
132///
133/// - `Int(i64)` - Integer values
134/// - `Float(f64)` - Floating-point values
135/// - `Bool(bool)` - Boolean values
136/// - `Text(String)` - String values
137/// - `Char(char)` - Single character values
138/// - `Nothing` - Unit/null value
139///
140/// # Arithmetic
141///
142/// Arithmetic operations are supported between numeric types:
143/// - `Int op Int` → `Int`
144/// - `Float op Float` → `Float`
145/// - `Int op Float` or `Float op Int` → `Float` (promotion)
146/// - `Text + Text` → `Text` (concatenation)
147///
148/// # Panics
149///
150/// Arithmetic on incompatible variants panics at runtime.
151///
152/// # Examples
153///
154/// ```
155/// use logicaffeine_data::Value;
156///
157/// let a = Value::Int(10);
158/// let b = Value::Int(3);
159/// assert_eq!(a + b, Value::Int(13));
160///
161/// let x = Value::Float(2.5);
162/// let y = Value::Int(2);
163/// assert_eq!(x * y, Value::Float(5.0));
164/// ```
165#[derive(Clone, Debug, PartialEq)]
166pub enum Value {
167    /// Integer values.
168    Int(i64),
169    /// Floating-point values.
170    Float(f64),
171    /// Boolean values.
172    Bool(bool),
173    /// String values.
174    Text(String),
175    /// Single character values.
176    Char(char),
177    /// Unit/null value.
178    Nothing,
179}
180
181impl std::fmt::Display for Value {
182    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183        match self {
184            Value::Int(n) => write!(f, "{}", n),
185            Value::Float(n) => write!(f, "{}", n),
186            Value::Bool(b) => write!(f, "{}", b),
187            Value::Text(s) => write!(f, "{}", s),
188            Value::Char(c) => write!(f, "{}", c),
189            Value::Nothing => write!(f, "nothing"),
190        }
191    }
192}
193
194// Conversion traits for Value
195impl From<i64> for Value {
196    fn from(n: i64) -> Self { Value::Int(n) }
197}
198
199impl From<f64> for Value {
200    fn from(n: f64) -> Self { Value::Float(n) }
201}
202
203impl From<bool> for Value {
204    fn from(b: bool) -> Self { Value::Bool(b) }
205}
206
207impl From<String> for Value {
208    fn from(s: String) -> Self { Value::Text(s) }
209}
210
211impl From<&str> for Value {
212    fn from(s: &str) -> Self { Value::Text(s.to_string()) }
213}
214
215impl From<char> for Value {
216    fn from(c: char) -> Self { Value::Char(c) }
217}
218
219/// Tuple type: Vec of heterogeneous Values (uses LogosIndex from indexing module)
220pub type Tuple = Vec<Value>;
221
222// NOTE: Showable impl for Value is in logicaffeine_system (io module)
223// This crate (logicaffeine_data) has NO IO dependencies.
224
225// Arithmetic operations for Value
226impl std::ops::Add for Value {
227    type Output = Value;
228
229    fn add(self, other: Value) -> Value {
230        match (self, other) {
231            (Value::Int(a), Value::Int(b)) => Value::Int(a + b),
232            (Value::Float(a), Value::Float(b)) => Value::Float(a + b),
233            (Value::Int(a), Value::Float(b)) => Value::Float(a as f64 + b),
234            (Value::Float(a), Value::Int(b)) => Value::Float(a + b as f64),
235            (Value::Text(a), Value::Text(b)) => Value::Text(format!("{}{}", a, b)),
236            _ => panic!("Cannot add these value types"),
237        }
238    }
239}
240
241impl std::ops::Sub for Value {
242    type Output = Value;
243
244    fn sub(self, other: Value) -> Value {
245        match (self, other) {
246            (Value::Int(a), Value::Int(b)) => Value::Int(a - b),
247            (Value::Float(a), Value::Float(b)) => Value::Float(a - b),
248            (Value::Int(a), Value::Float(b)) => Value::Float(a as f64 - b),
249            (Value::Float(a), Value::Int(b)) => Value::Float(a - b as f64),
250            _ => panic!("Cannot subtract these value types"),
251        }
252    }
253}
254
255impl std::ops::Mul for Value {
256    type Output = Value;
257
258    fn mul(self, other: Value) -> Value {
259        match (self, other) {
260            (Value::Int(a), Value::Int(b)) => Value::Int(a * b),
261            (Value::Float(a), Value::Float(b)) => Value::Float(a * b),
262            (Value::Int(a), Value::Float(b)) => Value::Float(a as f64 * b),
263            (Value::Float(a), Value::Int(b)) => Value::Float(a * b as f64),
264            _ => panic!("Cannot multiply these value types"),
265        }
266    }
267}
268
269impl std::ops::Div for Value {
270    type Output = Value;
271
272    fn div(self, other: Value) -> Value {
273        match (self, other) {
274            (Value::Int(a), Value::Int(b)) => Value::Int(a / b),
275            (Value::Float(a), Value::Float(b)) => Value::Float(a / b),
276            (Value::Int(a), Value::Float(b)) => Value::Float(a as f64 / b),
277            (Value::Float(a), Value::Int(b)) => Value::Float(a / b as f64),
278            _ => panic!("Cannot divide these value types"),
279        }
280    }
281}