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
//! Future/Promise system with async/await paradigm for Scheme.
//!
//! This module provides a comprehensive Future/Promise implementation
//! that integrates with Rust's async ecosystem while providing
//! Scheme-friendly APIs.

use crate::eval::Value;
use crate::diagnostics::{Error, Result};
use super::ConcurrencyError;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::time::{sleep, timeout};
use futures::future::{BoxFuture, FutureExt};

/// A Future represents a computation that will complete in the future.
///
/// This is the main future type exposed to Scheme code.
#[derive(Debug, Clone)]
pub struct Future {
    inner: Arc<Mutex<FutureState>>,
}

/// Internal state of a Future.
enum FutureState {
    /// Future is still pending
    Pending(BoxFuture<'static, Result<Value>>),
    /// Future completed successfully
    Resolved(Value),
    /// Future failed with an error
    Rejected(Box<Error>),
}

impl std::fmt::Debug for FutureState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FutureState::Pending(_) => f.debug_tuple("Pending").field(&"<future>").finish(),
            FutureState::Resolved(value) => f.debug_tuple("Resolved").field(value).finish(),
            FutureState::Rejected(error) => f.debug_tuple("Rejected").field(error).finish(),
        }
    }
}

impl Future {
    /// Creates a new future from an async computation.
    pub fn new<F>(future: F) -> Self
    where
        F: std::future::Future<Output = Result<Value>> + Send + 'static,
    {
        Self {
            inner: Arc::new(Mutex::new(FutureState::Pending(future.boxed()))),
        }
    }

    /// Creates an already resolved future.
    pub fn resolved(value: Value) -> Self {
        Self {
            inner: Arc::new(Mutex::new(FutureState::Resolved(value))),
        }
    }

    /// Creates an already rejected future.
    pub fn rejected(error: Error) -> Self {
        Self {
            inner: Arc::new(Mutex::new(FutureState::Rejected(Box::new(error)))),
        }
    }

    /// Creates a future from a promise.
    pub fn from_promise(promise: Promise) -> Self {
        promise.future
    }

    /// Checks if the future is completed (resolved or rejected).
    pub fn is_completed(&self) -> bool {
        let state = self.inner.lock().unwrap();
        matches!(*state, FutureState::Resolved(_) | FutureState::Rejected(_))
    }

    /// Checks if the future is resolved.
    pub fn is_resolved(&self) -> bool {
        let state = self.inner.lock().unwrap();
        matches!(*state, FutureState::Resolved(_))
    }

    /// Checks if the future is rejected.
    pub fn is_rejected(&self) -> bool {
        let state = self.inner.lock().unwrap();
        matches!(*state, FutureState::Rejected(_))
    }

    /// Waits for the future to complete and returns the result.
    pub async fn await_result(&self) -> Result<Value> {
        // Check if already completed
        {
            let mut state = self.inner.lock().unwrap();
            match &mut *state {
                FutureState::Resolved(value) => return Ok(value.clone()),
                FutureState::Rejected(error) => return Err(error.clone()),
                FutureState::Pending(_) => {}
            }
        }

        // Need to await the future
        let mut future_opt = None;
        {
            let mut state = self.inner.lock().unwrap();
            if let FutureState::Pending(future) = &mut *state {
                // Take ownership of the future
                future_opt = Some(std::mem::replace(future, futures::future::pending().boxed()));
            }
        }

        if let Some(future) = future_opt {
            let result = future.await;
            
            // Update state with result
            {
                let mut state = self.inner.lock().unwrap();
                *state = match &result {
                    Ok(value) => FutureState::Resolved(value.clone()),
                    Err(error) => FutureState::Rejected(error.clone()),
                };
            }
            
            result
        } else {
            // Future was completed while we were waiting
            let state = self.inner.lock().unwrap();
            match &*state {
                FutureState::Resolved(value) => Ok(value.clone()),
                FutureState::Rejected(error) => Err(error.clone()),
                FutureState::Pending(_) => unreachable!(),
            }
        }
    }

    /// Waits for the future with a timeout.
    pub async fn await_timeout(&self, duration: Duration) -> Result<Value> {
        match timeout(duration, self.await_result()).await {
            Ok(result) => result,
            Err(_) => Err(ConcurrencyError::Timeout.into()),
        }
    }

    /// Maps the future's value with a function.
    pub fn map<F>(self, f: F) -> Future
    where
        F: FnOnce(Value) -> Result<Value> + Send + 'static,
    {
        let future = async move {
            let value = self.await_result().await?;
            f(value)
        };
        Future::new(future)
    }

    /// Flat-maps the future with a function that returns another future.
    pub fn flat_map<F>(self, f: F) -> Future
    where
        F: FnOnce(Value) -> Future + Send + 'static,
    {
        let future = async move {
            let value = self.await_result().await?;
            f(value).await_result().await
        };
        Future::new(future)
    }

    /// Chains multiple futures together.
    pub fn then<F>(self, f: F) -> Future
    where
        F: FnOnce(Result<Value>) -> Future + Send + 'static,
    {
        let future = async move {
            let result = self.await_result().await;
            f(result).await_result().await
        };
        Future::new(future)
    }

    /// Handles errors in the future.
    pub fn catch<F>(self, f: F) -> Future
    where
        F: FnOnce(Error) -> Result<Value> + Send + 'static,
    {
        let future = async move {
            match self.await_result().await {
                Ok(value) => Ok(value),
                Err(error) => f(*error),
            }
        };
        Future::new(future)
    }
}

/// Promise is the writable side of a Future.
///
/// It allows you to resolve or reject a future manually.
#[derive(Debug)]
pub struct Promise {
    sender: Option<tokio::sync::oneshot::Sender<Result<Value>>>,
    future: Future,
}

impl Promise {
    /// Creates a new promise/future pair.
    pub fn new() -> Self {
        let (sender, receiver) = tokio::sync::oneshot::channel();
        
        let future = Future::new(async move {
            match receiver.await {
                Ok(result) => result,
                Err(_) => Err(ConcurrencyError::Cancelled.into()),
            }
        });

        Self {
            sender: Some(sender),
            future,
        }
    }

    /// Gets the associated future.
    pub fn future(&self) -> Future {
        self.future.clone()
    }

    /// Resolves the promise with a value.
    pub fn resolve(mut self, value: Value) -> Result<()> {
        if let Some(sender) = self.sender.take() {
            sender.send(Ok(value)).map_err(|_| ConcurrencyError::Cancelled.into())
        } else {
            Err(Error::runtime_error("Promise already completed".to_string(), None).into())
        }
    }

    /// Rejects the promise with an error.
    pub fn reject(mut self, error: Error) -> Result<()> {
        if let Some(sender) = self.sender.take() {
            sender.send(Err(error.into())).map_err(|_| ConcurrencyError::Cancelled.into())
        } else {
            Err(Error::runtime_error("Promise already completed".to_string(), None).into())
        }
    }

    /// Checks if the promise is still pending.
    pub fn is_pending(&self) -> bool {
        self.sender.is_some()
    }
}

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

/// Utility functions for working with futures.
pub struct FutureOps;

impl FutureOps {
    /// Creates a future that resolves after a delay.
    pub fn delay(duration: Duration) -> Future {
        Future::new(async move {
            sleep(duration).await;
            Ok(Value::Unspecified)
        })
    }

    /// Creates a future that resolves with a value after a delay.
    pub fn delay_value(duration: Duration, value: Value) -> Future {
        Future::new(async move {
            sleep(duration).await;
            Ok(value)
        })
    }

    /// Races multiple futures, returning the first one to complete.
    pub fn race(futures: Vec<Future>) -> Future {
        if futures.is_empty() {
            return Future::rejected(Error::runtime_error("No futures to race".to_string(), None));
        }

        Future::new(async move {
            let futures: Vec<_> = futures.into_iter()
                .map(|f| Box::pin(async move { f.await_result().await }))
                .collect();
            
            futures::future::select_all(futures).await.0
        })
    }

    /// Waits for all futures to complete, collecting results.
    pub fn all(futures: Vec<Future>) -> Future {
        Future::new(async move {
            let mut results = Vec::new();
            
            for future in futures {
                results.push(future.await_result().await?);
            }
            
            // Convert to Scheme list
            let mut list = Value::Nil;
            for value in results.into_iter().rev() {
                list = Value::pair(value, list);
            }
            
            Ok(list)
        })
    }

    /// Waits for all futures to settle (complete or fail).
    pub fn all_settled(futures: Vec<Future>) -> Future {
        Future::new(async move {
            let mut results = Vec::new();
            
            for future in futures {
                match future.await_result().await {
                    Ok(value) => {
                        let result = vec![
                            Value::symbol_from_str("fulfilled"),
                            value,
                        ];
                        results.push(Value::from_vec(result));
                    }
                    Err(error) => {
                        let result = vec![
                            Value::symbol_from_str("rejected"),
                            Value::string(error.to_string()),
                        ];
                        results.push(Value::from_vec(result));
                    }
                }
            }
            
            // Convert to Scheme list
            let mut list = Value::Nil;
            for value in results.into_iter().rev() {
                list = Value::pair(value, list);
            }
            
            Ok(list)
        })
    }

    /// Retries a future-producing function with exponential backoff.
    pub fn retry<F, Fut>(f: F, max_attempts: usize, initial_delay: Duration) -> Future
    where
        F: Fn() -> Fut + Send + 'static,
        Fut: std::future::Future<Output = Result<Value>> + Send + 'static,
    {
        Future::new(async move {
            let mut attempt = 0;
            let mut delay = initial_delay;
            
            loop {
                attempt += 1;
                
                match f().await {
                    Ok(value) => return Ok(value),
                    Err(error) => {
                        if attempt >= max_attempts {
                            return Err(error);
                        }
                        
                        sleep(delay).await;
                        delay *= 2; // Exponential backoff
                    }
                }
            }
        })
    }
}

/// Extension trait for Value to support future operations.
pub trait ValueFutureExt {
    /// Converts a value to a resolved future.
    fn to_future(self) -> Future;
}

impl ValueFutureExt for Value {
    fn to_future(self) -> Future {
        Future::resolved(self)
    }
}

impl ValueFutureExt for Result<Value> {
    fn to_future(self) -> Future {
        match self {
            Ok(value) => Future::resolved(value),
            Err(error) => Future::rejected(*error),
        }
    }
}

/// Helper trait for creating futures from async functions.
pub trait IntoFuture<T> {
    /// Converts self into a Future.
    fn into_future(self) -> Future;
}

impl<F, Fut> IntoFuture<F> for F
where
    F: FnOnce() -> Fut + Send + 'static,
    Fut: std::future::Future<Output = Result<Value>> + Send + 'static,
{
    fn into_future(self) -> Future {
        Future::new(self())
    }
}