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
//! Continuation monad implementation for call/cc support.
//!
//! This module implements the continuation monad that underlies call/cc
//! and provides the mathematical foundation for non-local control flow
//! in Lambdust's operational semantics.

#![allow(missing_docs)]

use crate::diagnostics::{Error, Result, Span};
use crate::eval::value::{Value, ThreadSafeEnvironment};
use crate::ast::Literal;
use std::collections::HashMap;
use std::fmt;
use std::rc::Rc;
use std::sync::Arc;
use std::any::TypeId;

/// Trait for converting between Scheme values and Rust types
pub trait FromValue: Sized {
    fn from_value(value: Value) -> Result<Self>;
}

/// Trait for converting Rust types to Scheme values
pub trait ToValue {
    fn to_value(self) -> Value;
}

/// Implement conversions for common types
impl FromValue for Value {
    fn from_value(value: Value) -> Result<Self> {
        Ok(value)
    }
}

impl ToValue for Value {
    fn to_value(self) -> Value {
        self
    }
}

// Note: For generic types, we'll implement conversions on a case-by-case basis
// This avoids the need for unstable features like specialization

impl FromValue for i32 {
    fn from_value(value: Value) -> Result<Self> {
        match value {
            Value::Literal(Literal::ExactInteger(i)) => Ok(i as i32),
            Value::Literal(Literal::InexactReal(f)) if f.fract() == 0.0 => Ok(f as i32),
            Value::Literal(literal) if literal.is_number() => {
                if let Some(f) = literal.to_f64() {
                    Ok(f as i32)
                } else {
                    Err(Box::new(Error::type_error("Cannot convert number to i32", Span::new(0, 0))))
                }
            }
            _ => Err(Box::new(Error::type_error("Expected number", Span::new(0, 0)))),
        }
    }
}

impl ToValue for i32 {
    fn to_value(self) -> Value {
        Value::Literal(Literal::integer(self as i64))
    }
}

impl FromValue for String {
    fn from_value(value: Value) -> Result<Self> {
        match value {
            Value::Literal(Literal::String(s)) => Ok(s),
            _ => Err(Box::new(Error::type_error("Expected string", Span::new(0, 0)))),
        }
    }
}

impl ToValue for String {
    fn to_value(self) -> Value {
        Value::Literal(Literal::String(self))
    }
}

impl FromValue for bool {
    fn from_value(value: Value) -> Result<Self> {
        match value {
            Value::Literal(Literal::Boolean(b)) => Ok(b),
            _ => Err(Box::new(Error::type_error("Expected boolean", Span::new(0, 0)))),
        }
    }
}

impl ToValue for bool {
    fn to_value(self) -> Value {
        Value::Literal(Literal::Boolean(self))
    }
}

/// The continuation monad - represents computations that can be suspended
/// and resumed with a continuation.
///
/// In Scheme terms, this implements the semantic foundation for call/cc:
/// `Cont r a = (a -> r) -> r`
///
/// Where:
/// - `a` is the value type being computed
/// - `r` is the final answer type 
/// - The continuation `(a -> r)` represents "what to do with the value"
#[derive(Debug, Clone)]
pub struct ContinuationMonad<A> {
    /// The computation: takes a continuation and produces a result
    computation: ContComputation<A>,
}

/// Function wrapper that implements Debug and Clone for continuation functions
#[derive(Clone)]
pub struct ContinuationFunc<A, B> {
    /// Unique identifier for debugging
    id: u64,
    /// Function implementation using Arc for shared ownership
    func: Arc<dyn Fn(A) -> B + Send + Sync + 'static>,
}

impl<A, B> ContinuationFunc<A, B> {
    pub fn new<F>(id: u64, func: F) -> Self
    where
        F: Fn(A) -> B + Send + Sync + 'static,
    {
        Self {
            id,
            func: Arc::new(func),
        }
    }
    
    pub fn call(&self, arg: A) -> B {
        (self.func)(arg)
    }
}

impl<A, B> std::fmt::Debug for ContinuationFunc<A, B> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ContinuationFunc({})", self.id)
    }
}

/// Internal representation of a continuation computation.
/// This captures the essence of `(a -> r) -> r` in Rust with proper trait support.
#[derive(Debug, Clone)]
pub enum ContComputation<A> {
    /// Pure value - just apply the continuation to it
    Pure(A),
    
    /// Call/cc operation - capture current continuation 
    CallCC {
        /// Function that receives the captured continuation
        proc: ContinuationFunc<ContinuationFunction, ContinuationMonad<A>>,
    },
    
    /// Apply a captured continuation (non-local jump)
    ApplyContinuation {
        continuation: ContinuationFunction,
        value: A,
    },
    
    /// Bind operation for monadic composition
    Bind {
        inner: Box<ContinuationMonad<Value>>,
        next: ContinuationFunc<Value, ContinuationMonad<A>>,
    },
    
    /// Effectful computation embedded in the continuation monad
    Effect {
        effect_computation: EffectfulComputation,
        continuation: ContinuationFunc<Value, ContinuationMonad<A>>,
    },
}

/// A continuation function - represents "what to do next"
/// This is the mathematical continuation in call/cc
#[derive(Debug, Clone)]
pub struct ContinuationFunction {
    /// Unique identifier for this continuation
    pub id: u64,
    
    /// The captured environment at continuation creation
    pub environment: Arc<ThreadSafeEnvironment>,
    
    /// The continuation computation - represents the "rest of the program"
    pub computation: ContinuationComputation,
    
    /// Whether this continuation has been invoked (single-shot semantics)
    pub invoked: bool,
}

/// The actual continuation computation that was captured
#[derive(Debug, Clone)]
pub enum ContinuationComputation {
    /// Return to a specific evaluation context
    EvaluationContext {
        /// The saved evaluation stack
        stack: Vec<EvaluationFrame>,
        
        /// The environment where the continuation was captured
        captured_env: Arc<ThreadSafeEnvironment>,
    },
    
    /// Direct function call continuation
    FunctionCall {
        /// Function to call with the value
        function: Value,
        
        /// Additional arguments to the function
        args: Vec<Value>,
        
        /// Environment for the function call
        env: Arc<ThreadSafeEnvironment>,
    },
    
    /// Composition of continuations
    Composed {
        /// First continuation to apply
        first: Box<ContinuationFunction>,
        
        /// Second continuation to apply to the result
        second: Box<ContinuationFunction>,
    },
}

/// Represents a frame in the evaluation stack for continuation capture
#[derive(Debug, Clone)]
pub enum EvaluationFrame {
    /// Function application frame
    Application {
        /// The function being applied
        function: Value,
        
        /// Arguments already evaluated
        evaluated_args: Vec<Value>,
        
        /// Arguments still to be evaluated
        pending_args: Vec<Value>,
        
        /// Environment for evaluation
        env: Arc<ThreadSafeEnvironment>,
    },
    
    /// Conditional evaluation frame
    Conditional {
        /// The then branch
        then_branch: Value,
        
        /// The else branch  
        else_branch: Value,
        
        /// Environment for evaluation
        env: Arc<ThreadSafeEnvironment>,
    },
    
    /// Begin sequence frame
    Sequence {
        /// Remaining expressions to evaluate
        remaining: Vec<Value>,
        
        /// Environment for evaluation
        env: Arc<ThreadSafeEnvironment>,
    },
    
    /// Let binding frame
    LetBinding {
        /// Variable bindings
        bindings: HashMap<String, Value>,
        
        /// Body expression
        body: Value,
        
        /// Environment for evaluation
        env: Arc<ThreadSafeEnvironment>,
    },
}

/// Effectful computations that can be embedded in the continuation monad
#[derive(Debug, Clone)]
pub enum EffectfulComputation {
    /// IO operation
    IO {
        /// The IO action to perform
        action: ContIOAction,
    },
    
    /// State modification
    State {
        /// The state operation
        action: ContStateAction,
        
        /// Current state
        state: Arc<ThreadSafeEnvironment>,
    },
    
    /// Error handling
    Error {
        /// The error to handle
        error: Error,
    },
}

/// IO actions within the continuation monad (continuation-specific)
#[derive(Debug, Clone)]
pub enum ContIOAction {
    /// Read a value
    Read,
    
    /// Write a value
    Write(Value),
    
    /// Print a value
    Print(Value),
    
    /// Return a pure value from IO
    Return(Value),
}

// Keep the old name for compatibility
pub use ContIOAction as IOAction;

/// State actions within the continuation monad (continuation-specific)
#[derive(Debug, Clone)]  
pub enum ContStateAction {
    /// Get current state
    Get,
    
    /// Set new state
    Put(Arc<ThreadSafeEnvironment>),
    
    /// Return a value with current state
    Return(Value),
}

// Keep the old name for compatibility
pub use ContStateAction as StateAction;

impl<A> ContinuationMonad<A> {
    /// Create a pure continuation computation
    pub fn pure(value: A) -> Self {
        Self {
            computation: ContComputation::Pure(value),
        }
    }
    
    /// Create a call/cc computation with proper trait support
    pub fn call_cc<F>(f: F) -> ContinuationMonad<Value>
    where
        F: Fn(ContinuationFunction) -> ContinuationMonad<Value> + Send + Sync + 'static,
    {
        static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let id = COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        
        ContinuationMonad {
            computation: ContComputation::CallCC {
                proc: ContinuationFunc::new(id, f),
            },
        }
    }
    
    /// Apply a captured continuation (perform non-local jump)
    pub fn apply_continuation(cont: ContinuationFunction, value: A) -> Self {
        Self {
            computation: ContComputation::ApplyContinuation {
                continuation: cont,
                value,
            },
        }
    }
    
    /// Monadic bind specialized for Value type (avoids complex type conversions)
    pub fn bind(self, f: impl Fn(A) -> ContinuationMonad<Value> + Send + Sync + 'static) -> ContinuationMonad<Value>
    where
        A: 'static,
    {
        // For simplicity, we'll implement this for the most common case where A = Value
        // and provide a default error for other cases
        ContinuationMonad::pure(Value::Unspecified) // Simplified implementation
    }
    
    
    /// Lift an effectful computation into the continuation monad
    pub fn lift_effect(effect: EffectfulComputation) -> ContinuationMonad<Value> {
        static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let id = COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        
        ContinuationMonad {
            computation: ContComputation::Effect {
                effect_computation: effect,
                continuation: ContinuationFunc::new(id, ContinuationMonad::pure),
            },
        }
    }
}

impl ContinuationFunction {
    /// Create a new continuation function
    pub fn new(
        id: u64,
        environment: Arc<ThreadSafeEnvironment>,
        computation: ContinuationComputation,
    ) -> Self {
        Self {
            id,
            environment,
            computation,
            invoked: false,
        }
    }
    
    /// Apply this continuation to a value (call the continuation)
    pub fn apply(&mut self, value: Value) -> Result<Value> {
        if self.invoked {
            return Err(Box::new(Error::runtime_error(
                "Continuation has already been invoked".to_string(),
                None,
            )));
        }
        
        self.invoked = true;
        
        match &self.computation {
            ContinuationComputation::EvaluationContext { stack, captured_env } => {
                // Restore the evaluation context and continue with the value
                // This implements the non-local jump semantics of call/cc
                self.restore_evaluation_context(stack, captured_env.clone(), value)
            }
            
            ContinuationComputation::FunctionCall { function, args, env } => {
                // Apply the function to the value plus additional args
                let mut all_args = vec![value];
                all_args.extend_from_slice(args);
                
                // This would call the evaluator - simplified for now
                Ok(Value::Unspecified)
            }
            
            ContinuationComputation::Composed { first, second } => {
                // Apply first continuation, then second
                let intermediate = first.clone().apply(value)?;
                second.clone().apply(intermediate)
            }
        }
    }
    
    /// Restore an evaluation context (implements the operational semantics)
    fn restore_evaluation_context(
        &self,
        _stack: &[EvaluationFrame],
        _env: Arc<ThreadSafeEnvironment>,
        value: Value,
    ) -> Result<Value> {
        // In a full implementation, this would restore the entire evaluation stack
        // and continue evaluation from where the continuation was captured.
        // For now, we simply return the value.
        Ok(value)
    }
    
    /// Check if this continuation can be safely invoked
    pub fn is_valid(&self) -> bool {
        !self.invoked
    }
}

/// Execute a continuation monad computation
pub fn run_continuation<A>(cont: ContinuationMonad<A>) -> Result<A> 
where
    A: FromValue + ToValue + 'static,
{
    match cont.computation {
        ContComputation::Pure(value) => Ok(value),
        
        ContComputation::CallCC { proc } => {
            // Create a "dummy" continuation for demonstration
            // In practice, this would capture the real continuation
            let dummy_cont = ContinuationFunction::new(
                0,
                Arc::new(ThreadSafeEnvironment::new(None, 0)),
                ContinuationComputation::EvaluationContext {
                    stack: Vec::new(),
                    captured_env: Arc::new(ThreadSafeEnvironment::new(None, 0)),
                },
            );
            
            let result_cont = proc.call(dummy_cont);
            run_continuation(result_cont)
        }
        
        ContComputation::ApplyContinuation { mut continuation, value } => {
            // Apply the continuation - this performs the non-local jump
            let value_as_value = value.to_value();
            let result = continuation.apply(value_as_value)?;
            
            // For type safety, we convert through Value if A is not Value
            if std::any::TypeId::of::<A>() == std::any::TypeId::of::<Value>() {
                unsafe { Ok(std::mem::transmute_copy(&result)) }
            } else {
                // This is a simplified fallback - in practice we'd need proper conversion
                match A::from_value(result) {
                    Ok(converted) => Ok(converted),
                    Err(_) => Err(Box::new(Error::type_error("Type conversion failed in continuation application", Span::new(0, 0)))),
                }
            }
        }
        
        ContComputation::Bind { inner, next } => {
            // Execute the inner computation first
            let intermediate_result = run_continuation(*inner)?;
            
            // Apply the next function to the result
            let final_cont = next.call(intermediate_result);
            
            // Execute the final computation
            run_continuation(final_cont)
        }
        
        ContComputation::Effect { effect_computation, continuation } => {
            // Execute the effectful computation
            let effect_result = execute_effect(effect_computation)?;
            
            // Continue with the result
            let cont_result = continuation.call(effect_result);
            run_continuation(cont_result)
        }
    }
}

/// Execute an effectful computation
fn execute_effect(effect: EffectfulComputation) -> Result<Value> {
    match effect {
        EffectfulComputation::IO { action } => {
            match action {
                ContIOAction::Read => {
                    // Simplified - would read from stdin
                    Ok(Value::string("input".to_string()))
                }
                ContIOAction::Write(value) => {
                    print!("{value}");
                    Ok(Value::Unspecified)
                }
                ContIOAction::Print(value) => {
                    println!("{value}");
                    Ok(Value::Unspecified)
                }
                ContIOAction::Return(value) => Ok(value),
            }
        }
        
        EffectfulComputation::State { action, state: _ } => {
            match action {
                ContStateAction::Get => {
                    // Return current state - simplified
                    Ok(Value::Unspecified)
                }
                ContStateAction::Put(_new_state) => {
                    // Set new state - simplified
                    Ok(Value::Unspecified)
                }
                ContStateAction::Return(value) => Ok(value),
            }
        }
        
        EffectfulComputation::Error { error } => {
            Err(Box::new(error))
        }
    }
}

/// Helper functions for common continuation patterns
/// Create an escape continuation (typical call/cc usage)  
pub fn escape_continuation(escape_value: Value) -> ContinuationMonad<Value> {
    ContinuationMonad::<Value>::call_cc(move |escape| {
        ContinuationMonad::apply_continuation(escape, escape_value.clone())
    })
}

/// Create a retry continuation
pub fn retry_continuation<F>(computation: F) -> ContinuationMonad<Value>
where
    F: Fn() -> ContinuationMonad<Value> + Send + Sync + 'static,
{
    ContinuationMonad::<Value>::call_cc(move |_retry| {
        computation() // Simplified - just execute the computation
    })
}

impl fmt::Display for ContinuationFunction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Continuation({})", self.id)
    }
}

impl<A: fmt::Display> fmt::Display for ContinuationMonad<A> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.computation {
            ContComputation::Pure(value) => write!(f, "Pure({value})"),
            ContComputation::CallCC { .. } => write!(f, "CallCC(<procedure>)"),
            ContComputation::ApplyContinuation { continuation, .. } => {
                write!(f, "ApplyContinuation({continuation})")
            }
            ContComputation::Bind { .. } => write!(f, "Bind(<computation>)"),
            ContComputation::Effect { .. } => write!(f, "Effect(<computation>)"),
        }
    }
}

// Thread safety
unsafe impl<A: Send> Send for ContinuationMonad<A> {}
unsafe impl<A: Sync> Sync for ContinuationMonad<A> {}
unsafe impl Send for ContinuationFunction {}
unsafe impl Sync for ContinuationFunction {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pure_continuation() {
        let cont = ContinuationMonad::pure(42);
        let result = run_continuation(cont).unwrap();
        assert_eq!(result, 42);
    }

    #[test]
    fn test_continuation_bind() {
        let cont = ContinuationMonad::pure(21)
            .bind(|x| ContinuationMonad::pure((x * 2).to_value()));
        let result = run_continuation(cont).unwrap();
        assert_eq!(result, 42.to_value());
    }

    #[test]
    fn test_escape_continuation() {
        let cont = escape_continuation(42.to_value());
        let result = run_continuation(cont).unwrap();
        assert_eq!(result, 42.to_value());
    }

    #[test]
    fn test_continuation_function_validity() {
        let cont_func = ContinuationFunction::new(
            1,
            Arc::new(ThreadSafeEnvironment::new(None, 0)),
            ContinuationComputation::EvaluationContext {
                stack: Vec::new(),
                captured_env: Arc::new(ThreadSafeEnvironment::new(None, 0)),
            },
        );
        
        assert!(cont_func.is_valid());
    }
}