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
//! Parallel computation primitives with work-stealing scheduler.
//!
//! This module provides high-performance parallel versions of common
//! functional programming patterns like map, filter, and reduce.

use crate::eval::Value;
use crate::diagnostics::{Error, Result};
use super::{ConcurrencyError, futures::Future};
use std::sync::{Arc, Mutex};
use rayon::prelude::*;
use crossbeam::deque::{Injector, Stealer, Worker};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;
use num_cpus;

/// Parallel computation configuration.
#[derive(Debug, Clone)]
pub struct ParallelConfig {
    /// Number of worker threads (None for CPU count)
    pub num_threads: Option<usize>,
    /// Chunk size for work distribution
    pub chunk_size: usize,
    /// Enable work stealing
    pub work_stealing: bool,
    /// CPU affinity settings
    pub cpu_affinity: Option<Vec<usize>>,
}

impl Default for ParallelConfig {
    fn default() -> Self {
        Self {
            num_threads: None,
            chunk_size: 1000,
            work_stealing: true,
            cpu_affinity: None,
        }
    }
}

/// Work-stealing scheduler for parallel tasks.
pub struct WorkStealingScheduler {
    injector: Arc<Injector<Task>>,
    stealers: Vec<Stealer<Task>>,
    workers: Vec<Worker<Task>>,
    num_threads: usize,
    active_tasks: Arc<AtomicUsize>,
}

/// A task in the work-stealing scheduler.
type Task = Box<dyn FnOnce() -> Result<Value> + Send + 'static>;

impl WorkStealingScheduler {
    /// Creates a new work-stealing scheduler.
    pub fn new(config: ParallelConfig) -> Self {
        let num_threads = config.num_threads.unwrap_or_else(num_cpus::get);
        let injector = Arc::new(Injector::new());
        let mut workers = Vec::new();
        let mut stealers = Vec::new();

        for _ in 0..num_threads {
            let worker = Worker::new_fifo();
            stealers.push(worker.stealer());
            workers.push(worker);
        }

        Self {
            injector,
            stealers,
            workers,
            num_threads,
            active_tasks: Arc::new(AtomicUsize::new(0)),
        }
    }

    /// Submits a task to the scheduler.
    pub fn submit<F>(&self, task: F) -> Result<()>
    where
        F: FnOnce() -> Result<Value> + Send + 'static,
    {
        self.injector.push(Box::new(task));
        self.active_tasks.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }

    /// Runs the scheduler until all tasks are completed.
    pub fn run_to_completion(self) -> Result<()> {
        let stealers = self.stealers.clone();
        let injector = self.injector.clone();
        let active_tasks = self.active_tasks.clone();

        thread::scope(|s| {
            for (i, worker) in self.workers.into_iter().enumerate() {
                let stealers = stealers.clone();
                let injector = injector.clone();
                let active_tasks = active_tasks.clone();
                
                s.spawn(move || {
                    loop {
                        // Try to get a task from local queue first
                        if let Some(task) = worker.pop() {
                            let _ = task(); // Execute task
                            active_tasks.fetch_sub(1, Ordering::SeqCst);
                            continue;
                        }

                        // Try to steal from global injector
                        if let crossbeam::deque::Steal::Success(task) = injector.steal() {
                            let _ = task(); // Execute task
                            active_tasks.fetch_sub(1, Ordering::SeqCst);
                            continue;
                        }

                        // Try to steal from other workers
                        let mut found_work = false;
                        for (j, stealer) in stealers.iter().enumerate() {
                            if i != j {
                                if let crossbeam::deque::Steal::Success(task) = stealer.steal() {
                                    let _ = task(); // Execute task
                                    active_tasks.fetch_sub(1, Ordering::SeqCst);
                                    found_work = true;
                                    break;
                                }
                            }
                        }

                        if !found_work {
                            // No work available, check if we should exit
                            if active_tasks.load(Ordering::SeqCst) == 0 {
                                break;
                            }
                            // Yield to other threads
                            thread::yield_now();
                        }
                    }
                });
            }
        });

        Ok(())
    }
}

/// Parallel computation operations.
pub struct ParallelOps {
    scheduler: WorkStealingScheduler,
    config: ParallelConfig,
}

impl ParallelOps {
    /// Creates a new parallel operations instance.
    pub fn new(config: ParallelConfig) -> Self {
        Self {
            scheduler: WorkStealingScheduler::new(config.clone()),
            config,
        }
    }

    /// Parallel map operation.
    pub fn par_map<F>(&self, values: Vec<Value>, f: F) -> Future
    where
        F: Fn(Value) -> Result<Value> + Send + Sync + 'static,
    {
        let f = Arc::new(f);
        let chunk_size = self.config.chunk_size;

        Future::new(async move {
            if values.is_empty() {
                return Ok(Value::Nil);
            }

            // Use rayon for simplicity and performance
            let par_results: std::result::Result<Vec<_>, Box<Error>> = values
                .into_par_iter()
                .with_min_len(chunk_size)
                .map(|value| f(value))
                .collect();

            match par_results {
                Ok(results) => {
                    let mut list = Value::Nil;
                    for value in results.into_iter().rev() {
                        list = Value::pair(value, list);
                    }
                    Ok(list)
                }
                Err(error) => Err(error),
            }
        })
    }

    /// Parallel filter operation.
    pub fn par_filter<F>(&self, values: Vec<Value>, predicate: F) -> Future
    where
        F: Fn(&Value) -> Result<bool> + Send + Sync + 'static,
    {
        let predicate = Arc::new(predicate);
        let chunk_size = self.config.chunk_size;

        Future::new(async move {
            if values.is_empty() {
                return Ok(Value::Nil);
            }

            let filtered: std::result::Result<Vec<_>, Box<Error>> = values
                .into_par_iter()
                .with_min_len(chunk_size)
                .filter_map(|value| {
                    match predicate(&value) {
                        Ok(true) => Some(Ok(value)),
                        Ok(false) => None,
                        Err(e) => Some(Err(e)),
                    }
                })
                .collect();

            match filtered {
                Ok(results) => {
                    let mut list = Value::Nil;
                    for value in results.into_iter().rev() {
                        list = Value::pair(value, list);
                    }
                    Ok(list)
                }
                Err(error) => Err(error),
            }
        })
    }

    /// Parallel reduce operation.
    pub fn par_reduce<F>(&self, values: Vec<Value>, identity: Value, f: F) -> Future
    where
        F: Fn(Value, Value) -> Result<Value> + Send + Sync + 'static,
    {
        let f = Arc::new(f);
        let chunk_size = self.config.chunk_size;

        Future::new(async move {
            if values.is_empty() {
                return Ok(identity);
            }

            // Convert values to Results and reduce
            let results: Vec<Result<Value>> = values
                .into_par_iter()
                .with_min_len(chunk_size)
                .map(Ok)
                .collect();

            // Sequential reduce to handle errors properly
            let mut acc = identity;
            for result in results {
                match result {
                    Ok(val) => {
                        acc = f(acc, val)?;
                    }
                    Err(e) => return Err(e),
                }
            }

            Ok(acc)
        })
    }

    /// Parallel fold operation with chunking.
    pub fn par_fold<F>(&self, values: Vec<Value>, identity: Value, f: F) -> Future
    where
        F: Fn(Value, Value) -> Result<Value> + Send + Sync + 'static,
    {
        let f = Arc::new(f);
        let chunk_size = self.config.chunk_size;

        Future::new(async move {
            if values.is_empty() {
                return Ok(identity);
            }

            let result = values
                .into_par_iter()
                .with_min_len(chunk_size)
                .try_fold(|| identity.clone(), |acc, value| f(acc, value))
                .try_reduce(|| identity.clone(), |a, b| f(a, b))?;

            Ok(result)
        })
    }

    /// Parallel for-each operation.
    pub fn par_for_each<F>(&self, values: Vec<Value>, f: F) -> Future
    where
        F: Fn(Value) -> Result<()> + Send + Sync + 'static,
    {
        let f = Arc::new(f);
        let chunk_size = self.config.chunk_size;

        Future::new(async move {
            let result: std::result::Result<(), Box<Error>> = values
                .into_par_iter()
                .with_min_len(chunk_size)
                .map(|value| f(value))
                .collect();

            result?;
            Ok(Value::Unspecified)
        })
    }

    /// Parallel partition operation.
    pub fn par_partition<F>(&self, values: Vec<Value>, predicate: F) -> Future
    where
        F: Fn(&Value) -> Result<bool> + Send + Sync + 'static,
    {
        let predicate = Arc::new(predicate);
        let chunk_size = self.config.chunk_size;

        Future::new(async move {
            if values.is_empty() {
                return Ok(Value::from_vec(vec![Value::Nil, Value::Nil]));
            }

            let (trues, falses): (Vec<_>, Vec<_>) = values
                .into_par_iter()
                .with_min_len(chunk_size)
                .map(|value| {
                    let matches = predicate(&value)?;
                    Ok((value, matches))
                })
                .collect::<std::result::Result<Vec<_>, Box<Error>>>()?
                .into_iter()
                .partition(|(_, matches)| *matches);

            let true_list = trues.into_iter()
                .map(|(value, _)| value)
                .collect::<Vec<_>>();
            let false_list = falses.into_iter()
                .map(|(value, _)| value)
                .collect::<Vec<_>>();

            Ok(Value::from_vec(vec![
                Value::from_vec(true_list),
                Value::from_vec(false_list),
            ]))
        })
    }

    /// Parallel find operation.
    pub fn par_find<F>(&self, values: Vec<Value>, predicate: F) -> Future
    where
        F: Fn(&Value) -> Result<bool> + Send + Sync + 'static,
    {
        let predicate = Arc::new(predicate);
        let chunk_size = self.config.chunk_size;

        Future::new(async move {
            let result = values
                .into_par_iter()
                .with_min_len(chunk_size)
                .find_first(|value| {
                    predicate(value).unwrap_or(false)
                });

            Ok(result.unwrap_or(Value::Nil))
        })
    }

    /// Parallel any operation.
    pub fn par_any<F>(&self, values: Vec<Value>, predicate: F) -> Future
    where
        F: Fn(&Value) -> Result<bool> + Send + Sync + 'static,
    {
        let predicate = Arc::new(predicate);
        let chunk_size = self.config.chunk_size;

        Future::new(async move {
            let result = values
                .into_par_iter()
                .with_min_len(chunk_size)
                .any(|value| {
                    predicate(&value).unwrap_or(false)
                });

            Ok(Value::boolean(result))
        })
    }

    /// Parallel all operation.
    pub fn par_all<F>(&self, values: Vec<Value>, predicate: F) -> Future
    where
        F: Fn(&Value) -> Result<bool> + Send + Sync + 'static,
    {
        let predicate = Arc::new(predicate);
        let chunk_size = self.config.chunk_size;

        Future::new(async move {
            let result = values
                .into_par_iter()
                .with_min_len(chunk_size)
                .all(|value| {
                    predicate(&value).unwrap_or(false)
                });

            Ok(Value::boolean(result))
        })
    }

    /// Parallel sort operation.
    pub fn par_sort<F>(&self, mut values: Vec<Value>, compare: F) -> Future
    where
        F: Fn(&Value, &Value) -> Result<std::cmp::Ordering> + Send + Sync + 'static,
    {
        let compare = Arc::new(compare);

        Future::new(async move {
            values.par_sort_by(|a, b| {
                compare(a, b).unwrap_or(std::cmp::Ordering::Equal)
            });

            Ok(Value::from_vec(values))
        })
    }
}

/// Thread pool for CPU-intensive tasks.
pub struct ThreadPool {
    pool: rayon::ThreadPool,
}

impl ThreadPool {
    /// Creates a new thread pool with the given configuration.
    pub fn new(config: ParallelConfig) -> Result<Self> {
        let num_threads = config.num_threads.unwrap_or_else(num_cpus::get);
        
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(num_threads)
            .build()
            .map_err(|e| Error::runtime_error(format!("Failed to create thread pool: {e}"), None))?;

        Ok(Self { pool })
    }

    /// Executes a task on the thread pool.
    pub fn execute<F, R>(&self, task: F) -> Future
    where
        F: FnOnce() -> Result<R> + Send + 'static,
        R: Into<Value> + Send + 'static,
    {
        let (sender, receiver) = tokio::sync::oneshot::channel();
        
        self.pool.spawn(move || {
            let result = task().map(|r| r.into());
            let _ = sender.send(result);
        });

        Future::new(async move {
            receiver.await
                .map_err(|_| ConcurrencyError::Cancelled.boxed())?
        })
    }

    /// Executes multiple tasks in parallel.
    pub fn execute_all<F, R>(&self, tasks: Vec<F>) -> Future
    where
        F: FnOnce() -> Result<R> + Send + 'static,
        R: Into<Value> + Send + 'static,
    {
        let (sender, receiver) = tokio::sync::oneshot::channel();
        let num_tasks = tasks.len();
        let results = Arc::new(Mutex::new(Vec::with_capacity(num_tasks)));
        let counter = Arc::new(AtomicUsize::new(0));
        let sender = Arc::new(Mutex::new(Some(sender)));

        for (i, task) in tasks.into_iter().enumerate() {
            let results = results.clone();
            let counter = counter.clone();
            let sender = sender.clone();

            self.pool.spawn(move || {
                let result = task().map(|r| r.into());
                
                {
                    let mut results = results.lock().unwrap();
                    if results.len() <= i {
                        results.resize(i + 1, Value::Nil);
                    }
                    results[i] = result.unwrap_or(Value::Nil);
                }

                let completed = counter.fetch_add(1, Ordering::SeqCst) + 1;
                if completed == num_tasks {
                    if let Some(sender) = sender.lock().unwrap().take() {
                        let final_results = results.lock().unwrap().clone();
                        let _ = sender.send(Ok(Value::from_vec(final_results)));
                    }
                }
            });
        }

        Future::new(async move {
            receiver.await
                .map_err(|_| ConcurrencyError::Cancelled.boxed())?
        })
    }
}

/// CPU affinity utilities.
pub struct CpuAffinity;

impl CpuAffinity {
    /// Sets CPU affinity for the current thread.
    #[cfg(target_os = "linux")]
    pub fn set_affinity(cpu_ids: &[usize]) -> Result<()> {
        use std::mem;
        
        let mut cpu_set: libc::cpu_set_t = unsafe { mem::zeroed() };
        
        for &cpu_id in cpu_ids {
            unsafe {
                libc::CPU_SET(cpu_id, &mut cpu_set);
            }
        }
        
        let result = unsafe {
            libc::sched_setaffinity(0, mem::size_of::<libc::cpu_set_t>(), &cpu_set)
        };
        
        if result != 0 {
            Err(Error::runtime_error("Failed to set CPU affinity".to_string(), None).into())
        } else {
            Ok(())
        }
    }

    /// Sets CPU affinity for the current thread (no-op on non-Linux systems).
    #[cfg(not(target_os = "linux"))]
    pub fn set_affinity(_cpu_ids: &[usize]) -> Result<()> {
        // CPU affinity is not supported on this platform
        Ok(())
    }

    /// Gets the number of available CPUs.
    pub fn cpu_count() -> usize {
        num_cpus::get()
    }

    /// Gets the optimal number of threads for CPU-bound tasks.
    pub fn optimal_thread_count() -> usize {
        num_cpus::get()
    }
}