lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
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
//! Distributed processing foundation with RPC and serialization.
//!
//! This module provides the building blocks for distributed computing
//! including remote procedure calls, serialization, and network communication.

use crate::eval::Value;
use crate::diagnostics::{Error, Result};
use super::ConcurrencyError;
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::time::{Duration, Instant};
use tokio::net::{TcpListener, TcpStream};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use serde::{Serialize, Deserialize};
use uuid::Uuid;

/// Node identifier in a distributed system.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct NodeId(Uuid);

impl NodeId {
    /// Creates a new unique node ID.
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }

    /// Creates a node ID from a UUID.
    pub fn from_uuid(uuid: Uuid) -> Self {
        Self(uuid)
    }

    /// Gets the underlying UUID.
    pub fn as_uuid(&self) -> Uuid {
        self.0
    }
}

impl std::fmt::Display for NodeId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "node-{}", self.0)
    }
}

impl Default for NodeId {
    fn default() -> Self {
        Self::new()
    }
}

/// Remote procedure call request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RpcRequest {
    /// Unique request ID
    pub id: String,
    /// Target service name
    pub service: String,
    /// Method to call
    pub method: String,
    /// Arguments
    pub args: Vec<SerializableValue>,
    /// Sender node ID
    pub sender: NodeId,
    /// Request timestamp
    pub timestamp: u64,
    /// Timeout in milliseconds
    pub timeout: Option<u64>,
}

/// Remote procedure call response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RpcResponse {
    /// Request ID this response corresponds to
    pub request_id: String,
    /// Response result
    pub result: std::result::Result<SerializableValue, String>,
    /// Response timestamp
    pub timestamp: u64,
    /// Processing time in microseconds
    pub processing_time: Option<u64>,
}

/// Serializable version of Value for network transmission.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SerializableValue {
    /// Nil/empty value
    Nil,
    /// Boolean true/false
    Boolean(bool),
    /// 64-bit signed integer
    Integer(i64),
    /// 64-bit floating point number
    Float(f64),
    /// UTF-8 string
    String(String),
    /// Lisp symbol
    Symbol(String),
    /// Ordered list of values
    List(Vec<SerializableValue>),
    /// Indexed vector of values
    Vector(Vec<SerializableValue>),
    /// Key-value mapping
    Map(HashMap<String, SerializableValue>),
    /// Raw byte data
    Bytes(Vec<u8>),
}

impl SerializableValue {
    /// Converts a Value to SerializableValue.
    pub fn from_value(value: &Value) -> Result<Self> {
        match value {
            Value::Nil => Ok(SerializableValue::Nil),
            Value::Literal(lit) => match lit {
                crate::ast::Literal::Boolean(b) => Ok(SerializableValue::Boolean(*b)),
                crate::ast::Literal::ExactInteger(i) => Ok(SerializableValue::Integer(*i)),
                crate::ast::Literal::InexactReal(f) => Ok(SerializableValue::Float(*f)),
                crate::ast::Literal::Number(f) => Ok(SerializableValue::Float(*f)),
                crate::ast::Literal::Rational { numerator, denominator } => {
                    if *denominator == 1 {
                        Ok(SerializableValue::Integer(*numerator))
                    } else {
                        Ok(SerializableValue::Float(*numerator as f64 / *denominator as f64))
                    }
                }
                crate::ast::Literal::Complex { real, imaginary: _ } => 
                    Ok(SerializableValue::Float(*real)), // Only serialize real part
                crate::ast::Literal::String(s) => Ok(SerializableValue::String(s.clone())),
                crate::ast::Literal::Character(c) => Ok(SerializableValue::String(c.to_string())),
                // Handle other literal types
                crate::ast::Literal::Bytevector(bytes) => 
                    Ok(SerializableValue::String(format!("bytevector-{}", bytes.len()))),
                crate::ast::Literal::Nil => Ok(SerializableValue::Nil),
                crate::ast::Literal::Unspecified => Ok(SerializableValue::String("unspecified".to_string())),
            },
            Value::Symbol(sym) => Ok(SerializableValue::Symbol(format!("symbol-{}", sym.0))),
            Value::Pair(_car, _cdr) => {
                // Convert pair to list
                let mut list = Vec::new();
                let mut current = value;
                
                loop {
                    match current {
                        Value::Pair(car, cdr) => {
                            list.push(Self::from_value(car)?);
                            current = cdr;
                        }
                        Value::Nil => break,
                        _ => {
                            // Improper list - add the final element
                            list.push(Self::from_value(current)?);
                            break;
                        }
                    }
                }
                
                Ok(SerializableValue::List(list))
            }
            Value::Vector(vec) => {
                let guard = vec.read().unwrap();
                let mut serializable_vec = Vec::new();
                for item in guard.iter() {
                    serializable_vec.push(Self::from_value(item)?);
                }
                Ok(SerializableValue::Vector(serializable_vec))
            }
            _ => Err(Box::new(Error::runtime_error(
                format!("Cannot serialize value type: {value:?}"),
                None,
            ))),
        }
    }

    /// Converts SerializableValue to Value.
    pub fn to_value(&self) -> Result<Value> {
        match self {
            SerializableValue::Nil => Ok(Value::Nil),
            SerializableValue::Boolean(b) => Ok(Value::Literal(crate::ast::Literal::Boolean(*b))),
            SerializableValue::Integer(i) => Ok(Value::Literal(crate::ast::Literal::integer(*i))),
            SerializableValue::Float(f) => Ok(Value::Literal(crate::ast::Literal::float(*f))),
            SerializableValue::String(s) => Ok(Value::Literal(crate::ast::Literal::String(s.clone()))),
            SerializableValue::Symbol(s) => {
                // Extract symbol ID from the string (simplified)
                Ok(Value::Symbol(crate::utils::SymbolId(s.len())))
            }
            SerializableValue::List(list) => {
                let mut result = Value::Nil;
                for item in list.iter().rev() {
                    let value = item.to_value()?;
                    result = Value::pair(value, result);
                }
                Ok(result)
            }
            SerializableValue::Vector(vec) => {
                let mut values = Vec::new();
                for item in vec {
                    values.push(item.to_value()?);
                }
                Ok(Value::Vector(Arc::new(std::sync::RwLock::new(values))))
            }
            SerializableValue::Map(_map) => {
                // Convert to hash table or similar structure
                Ok(Value::Nil) // Placeholder
            }
            SerializableValue::Bytes(bytes) => {
                // Convert to bytevector
                Ok(Value::Literal(crate::ast::Literal::String(
                    String::from_utf8_lossy(bytes).to_string()
                )))
            }
        }
    }
}

/// RPC service trait.
#[async_trait::async_trait]
pub trait RpcService: Send + Sync + std::fmt::Debug {
    /// Handles an RPC request.
    async fn handle_request(&self, request: RpcRequest) -> RpcResponse;
    
    /// Gets the service name.
    fn service_name(&self) -> &str;
}

/// RPC client for making remote calls.
#[derive(Debug, Clone)]
pub struct RpcClient {
    node_id: NodeId,
    connections: Arc<Mutex<HashMap<NodeId, Arc<Connection>>>>,
}

impl RpcClient {
    /// Creates a new RPC client.
    pub fn new(node_id: NodeId) -> Self {
        Self {
            node_id,
            connections: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Connects to a remote node.
    pub async fn connect(&self, node_id: NodeId, addr: SocketAddr) -> Result<()> {
        let stream = TcpStream::connect(addr).await
            .map_err(|e| ConcurrencyError::Network(e.to_string()).boxed())?;
        
        let connection = Arc::new(Connection::new(stream));
        
        {
            let mut connections = self.connections.lock().unwrap();
            connections.insert(node_id, connection);
        }
        
        Ok(())
    }

    /// Makes a remote procedure call.
    pub async fn call(
        &self,
        target_node: NodeId,
        service: String,
        method: String,
        args: Vec<Value>,
        timeout: Option<Duration>,
    ) -> Result<Value> {
        let connection = {
            let connections = self.connections.lock().unwrap();
            connections.get(&target_node)
                .ok_or_else(|| ConcurrencyError::Network("Node not connected".to_string()).boxed())?
                .clone()
        };

        let request_id = Uuid::new_v4().to_string();
        let serializable_args: Result<Vec<_>> = args.iter()
            .map(SerializableValue::from_value)
            .collect();

        let request = RpcRequest {
            id: request_id.clone(),
            service,
            method,
            args: serializable_args?,
            sender: self.node_id,
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_millis() as u64,
            timeout: timeout.map(|t| t.as_millis() as u64),
        };

        let response = connection.send_request(request, timeout.unwrap_or(Duration::from_secs(30))).await?;
        
        match response.result {
            Ok(value) => value.to_value(),
            Err(error) => Err(Error::runtime_error(error, None).boxed()),
        }
    }
}

/// RPC server for handling remote calls.
#[derive(Debug)]
pub struct RpcServer {
    node_id: NodeId,
    listener: Option<TcpListener>,
    services: Arc<Mutex<HashMap<String, Arc<dyn RpcService>>>>,
    connections: Arc<Mutex<HashMap<NodeId, Arc<Connection>>>>,
}

impl RpcServer {
    /// Creates a new RPC server.
    pub fn new(node_id: NodeId) -> Self {
        Self {
            node_id,
            listener: None,
            services: Arc::new(Mutex::new(HashMap::new())),
            connections: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Binds the server to an address.
    pub async fn bind(&mut self, addr: SocketAddr) -> Result<()> {
        let listener = TcpListener::bind(addr).await
            .map_err(|e| ConcurrencyError::Network(e.to_string()).boxed())?;
        
        self.listener = Some(listener);
        Ok(())
    }

    /// Registers an RPC service.
    pub fn register_service(&self, service: Arc<dyn RpcService>) {
        let mut services = self.services.lock().unwrap();
        services.insert(service.service_name().to_string(), service);
    }

    /// Starts the server.
    pub async fn serve(&self) -> Result<()> {
        let listener = self.listener.as_ref()
            .ok_or_else(|| Error::runtime_error("Server not bound to address".to_string(), None))?;

        loop {
            let (stream, _addr) = listener.accept().await
                .map_err(|e| ConcurrencyError::Network(e.to_string()).boxed())?;

            let connection = Arc::new(Connection::new(stream));
            let services = self.services.clone();
            
            tokio::spawn(async move {
                if let Err(e) = Self::handle_connection(connection, services).await {
                    eprintln!("Connection error: {e}");
                }
            });
        }
    }

    /// Handles a client connection.
    async fn handle_connection(
        connection: Arc<Connection>,
        services: Arc<Mutex<HashMap<String, Arc<dyn RpcService>>>>,
    ) -> Result<()> {
        loop {
            match connection.receive_request().await {
                Ok(request) => {
                    let services = services.clone();
                    let connection = connection.clone();
                    
                    tokio::spawn(async move {
                        let response = Self::process_request(request, services).await;
                        if let Err(e) = connection.send_response(response).await {
                            eprintln!("Failed to send response: {e}");
                        }
                    });
                }
                Err(e) => {
                    eprintln!("Failed to receive request: {e}");
                    break;
                }
            }
        }
        
        Ok(())
    }

    /// Processes an RPC request.
    async fn process_request(
        request: RpcRequest,
        services: Arc<Mutex<HashMap<String, Arc<dyn RpcService>>>>,
    ) -> RpcResponse {
        let start_time = Instant::now();
        
        let service = {
            let services = services.lock().unwrap();
            services.get(&request.service).cloned()
        };

        if let Some(service) = service {
            service.handle_request(request.clone()).await
        } else {
            RpcResponse {
                request_id: request.id,
                result: Err(format!("Service '{service}' not found", service = request.service)),
                timestamp: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .as_millis() as u64,
                processing_time: Some(start_time.elapsed().as_micros() as u64),
            }
        }
    }
}

/// Network connection wrapper.
struct Connection {
    stream: Arc<tokio::sync::Mutex<TcpStream>>,
    pending_requests: Arc<tokio::sync::Mutex<HashMap<String, tokio::sync::oneshot::Sender<RpcResponse>>>>,
}

impl std::fmt::Debug for Connection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Connection")
            .field("stream", &"<TcpStream>")
            .field("pending_requests", &"<PendingRequests>")
            .finish()
    }
}

impl Connection {
    /// Creates a new connection.
    fn new(stream: TcpStream) -> Self {
        Self {
            stream: Arc::new(tokio::sync::Mutex::new(stream)),
            pending_requests: Arc::new(tokio::sync::Mutex::new(HashMap::new())),
        }
    }

    /// Sends an RPC request.
    async fn send_request(&self, request: RpcRequest, timeout: Duration) -> Result<RpcResponse> {
        let (tx, rx) = tokio::sync::oneshot::channel();
        
        {
            let mut pending = self.pending_requests.lock().await;
            pending.insert(request.id.clone(), tx);
        }

        // Serialize and send request
        let data = serde_json::to_vec(&request)
            .map_err(|e| ConcurrencyError::Serialization(e.to_string()).boxed())?;
        
        let mut stream = self.stream.lock().await;
        stream.write_u32(data.len() as u32).await
            .map_err(|e| ConcurrencyError::Network(e.to_string()).boxed())?;
        stream.write_all(&data).await
            .map_err(|e| ConcurrencyError::Network(e.to_string()).boxed())?;

        // Wait for response
        tokio::time::timeout(timeout, rx)
            .await
            .map_err(|_| ConcurrencyError::Timeout.boxed())?
            .map_err(|_| Error::runtime_error("Request cancelled".to_string(), None).boxed())
    }

    /// Receives an RPC request.
    async fn receive_request(&self) -> Result<RpcRequest> {
        let mut stream = self.stream.lock().await;
        
        let len = stream.read_u32().await
            .map_err(|e| ConcurrencyError::Network(e.to_string()).boxed())?;
        
        let mut buffer = vec![0u8; len as usize];
        stream.read_exact(&mut buffer).await
            .map_err(|e| ConcurrencyError::Network(e.to_string()).boxed())?;

        serde_json::from_slice(&buffer)
            .map_err(|e| ConcurrencyError::Serialization(e.to_string()).boxed())
    }

    /// Sends an RPC response.
    async fn send_response(&self, response: RpcResponse) -> Result<()> {
        let data = serde_json::to_vec(&response)
            .map_err(|e| ConcurrencyError::Serialization(e.to_string()).boxed())?;
        
        let mut stream = self.stream.lock().await;
        stream.write_u32(data.len() as u32).await
            .map_err(|e| ConcurrencyError::Network(e.to_string()).boxed())?;
        stream.write_all(&data).await
            .map_err(|e| ConcurrencyError::Network(e.to_string()).boxed())?;

        Ok(())
    }
}

/// Example RPC service implementation.
#[derive(Debug)]
pub struct CalculatorService;

#[async_trait::async_trait]
impl RpcService for CalculatorService {
    async fn handle_request(&self, request: RpcRequest) -> RpcResponse {
        let start_time = Instant::now();
        
        let result = match request.method.as_str() {
            "add" => {
                if request.args.len() != 2 {
                    Err("add requires exactly 2 arguments".to_string())
                } else {
                    match (&request.args[0], &request.args[1]) {
                        (SerializableValue::Integer(a), SerializableValue::Integer(b)) => {
                            Ok(SerializableValue::Integer(a + b))
                        }
                        (SerializableValue::Float(a), SerializableValue::Float(b)) => {
                            Ok(SerializableValue::Float(a + b))
                        }
                        _ => Err("add requires numeric arguments".to_string()),
                    }
                }
            }
            "multiply" => {
                if request.args.len() != 2 {
                    Err("multiply requires exactly 2 arguments".to_string())
                } else {
                    match (&request.args[0], &request.args[1]) {
                        (SerializableValue::Integer(a), SerializableValue::Integer(b)) => {
                            Ok(SerializableValue::Integer(a * b))
                        }
                        (SerializableValue::Float(a), SerializableValue::Float(b)) => {
                            Ok(SerializableValue::Float(a * b))
                        }
                        _ => Err("multiply requires numeric arguments".to_string()),
                    }
                }
            }
            _ => Err(format!("Unknown method: {}", request.method)),
        };

        RpcResponse {
            request_id: request.id,
            result,
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_millis() as u64,
            processing_time: Some(start_time.elapsed().as_micros() as u64),
        }
    }

    fn service_name(&self) -> &str {
        "calculator"
    }
}

/// Distributed node in the system.
#[derive(Debug)]
pub struct DistributedNode {
    id: NodeId,
    rpc_client: RpcClient,
    rpc_server: RpcServer,
}

impl DistributedNode {
    /// Creates a new distributed node.
    pub fn new() -> Self {
        let id = NodeId::new();
        Self {
            id,
            rpc_client: RpcClient::new(id),
            rpc_server: RpcServer::new(id),
        }
    }

    /// Gets the node ID.
    pub fn id(&self) -> NodeId {
        self.id
    }

    /// Gets the RPC client.
    pub fn rpc_client(&self) -> &RpcClient {
        &self.rpc_client
    }

    /// Gets the RPC server.
    pub fn rpc_server(&mut self) -> &mut RpcServer {
        &mut self.rpc_server
    }

    /// Starts the node on the given address.
    pub async fn start(&mut self, addr: SocketAddr) -> Result<()> {
        self.rpc_server.bind(addr).await?;
        
        // Note: Server would be started in background in a real implementation
        // For now, we just bind and return
        println!("RPC server bound to {addr}");
        
        Ok(())
    }
}

impl Default for DistributedNode {
    fn default() -> Self {
        Self::new()
    }
}

/// Utility functions for distributed computing.
pub struct DistributedOps;

impl DistributedOps {
    /// Creates a distributed map operation across multiple nodes.
    pub async fn distributed_map<F>(
        nodes: Vec<NodeId>,
        client: &RpcClient,
        data: Vec<Value>,
        _map_fn: F,
    ) -> Result<Vec<Value>>
    where
        F: Fn(&Value) -> Result<Value> + Send + Sync + 'static,
    {
        if nodes.is_empty() {
            return Err(Box::new(Error::runtime_error("No nodes available".to_string(), None)))
        }

        let chunk_size = data.len().div_ceil(nodes.len());
        let mut futures = Vec::new();

        for (i, chunk) in data.chunks(chunk_size).enumerate() {
            let node_id = nodes[i % nodes.len()];
            let chunk_data = chunk.to_vec();
            
            // In a real implementation, you'd serialize the function and send it
            // For now, we'll assume a predefined map service exists on remote nodes
            let future = client.call(
                node_id,
                "distributed".to_string(),
                "map".to_string(),
                chunk_data,
                Some(Duration::from_secs(30)),
            );
            
            futures.push(future);
        }

        let mut results = Vec::new();
        for future in futures {
            let result = future.await?;
            // Assuming result is a list of mapped values
            if let Value::Pair(_, _) = result {
                // Convert list back to Vec<Value>
                let mut current = &result;
                loop {
                    match current {
                        Value::Pair(car, cdr) => {
                            results.push((**car).clone());
                            current = cdr;
                        }
                        Value::Nil => break,
                        _ => {
                            results.push(current.clone());
                            break;
                        }
                    }
                }
            }
        }

        Ok(results)
    }

    /// Creates a distributed reduce operation across multiple nodes.
    pub async fn distributed_reduce(
        nodes: Vec<NodeId>,
        client: &RpcClient,
        data: Vec<Value>,
        identity: Value,
    ) -> Result<Value> {
        // First, perform local reductions on each node
        let partial_results = Self::distributed_map(
            nodes.clone(),
            client,
            data,
            |_| Ok(Value::Nil), // Placeholder
        ).await?;

        // Then, reduce the partial results locally or on a coordinator node
        let mut result = identity;
        for partial in partial_results {
            // Combine partial results (implementation depends on the reduction operation)
            result = partial; // Placeholder
        }

        Ok(result)
    }
}