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
//! Standard library implementation for Lambdust.
//!
//! This module provides R7RS-compatible standard library functions
//! plus Lambdust-specific extensions including effect system support.
//!
//! The standard library is organized into modules:
//! - `arithmetic`: Number operations and mathematical functions
//! - `strings`: String manipulation and conversion
//! - `lists`: List processing and higher-order functions
//! - `vectors`: Vector operations and conversions
//! - `characters`: Character operations and predicates
//! - `io`: Input/output operations
//! - `control`: Control flow procedures
//! - `types`: Type operations and predicates
//! - `effects`: Effect system integration

#![allow(missing_docs)]

/// Arithmetic operations and mathematical functions.
pub mod arithmetic;
/// Bytevector operations and utilities.
pub mod bytevector;
/// Character operations and predicates.
pub mod characters;
/// Character set operations and SRFI-14 implementation.
pub mod charset;
/// Concurrency primitives and parallel operations.
pub mod concurrency;
/// Control flow procedures (if, cond, case, etc.).
pub mod control;
/// Effect system integration and monadic operations.
pub mod effects;
/// Exception handling and error operations.
pub mod exceptions;
/// Basic input/output operations.
pub mod io;
/// List processing and higher-order functions.
pub mod lists;
/// Parameter objects (SRFI-39) implementation.
pub mod parameters;
/// Simple record types implementation.
pub mod records_simple;
/// Enhanced SRFI-23 error handling.
pub mod srfi23_enhanced;
/// SRFI-9 record types macro system.
pub mod srfi9_macro;
/// Set operations and SRFI-113 implementation.
pub mod sets;
/// Bag (multiset) operations and SRFI-113 implementation.
pub mod bags;
/// Generator operations and SRFI-121 implementation.
pub mod generators;
/// String manipulation and conversion operations.
pub mod strings;
/// System interface and process operations.
pub mod system;
/// Type operations, predicates, and reflection.
pub mod types;
/// Vector operations and conversions.
pub mod vectors;

// Advanced I/O system modules (R7RS-large)
/// Advanced I/O operations and utilities.
pub mod advanced_io;
/// Asynchronous I/O operations.
pub mod async_io;
/// Network I/O and socket operations.
pub mod network_io;
/// Streaming I/O and lazy sequences.
pub mod streaming_io;
/// Platform-specific I/O operations.
pub mod platform_io;
/// Secure I/O operations and sandboxing.
pub mod security_io;
/// I/O system integration and coordination.
pub mod io_integration;

// SRFI-135 Text processing modules
/// Text processing and manipulation (SRFI-135).
pub mod text;
/// Regular expression support for text processing.
pub mod text_regex;
/// Advanced text algorithms and utilities.
pub mod text_algorithms;
/// SRFI-135 specific text implementations.
pub mod text_srfi135;
/// Performance-optimized text operations.
pub mod text_performance;

/// Text processing test suite.
#[cfg(test)]
pub mod text_tests;

// Individual structure modules
/// Standard library core implementation and bindings.
pub mod standard_library;

pub use standard_library::*;

use crate::eval::value::{Value, PrimitiveProcedure, PrimitiveImpl, ThreadSafeEnvironment};
use crate::effects::Effect;
use std::sync::Arc;

/// Built-in procedure types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BuiltinProcedure {
    // Arithmetic
    Add,
    Subtract,
    Multiply,
    Divide,
    
    // Comparison
    Equal,
    LessThan,
    GreaterThan,
    
    // List operations
    Cons,
    Car,
    Cdr,
    List,
    
    // I/O
    Display,
    Newline,
    
    // System functions
    Features,
    CurrentSecond,
    CurrentJiffy,
    JiffiesPerSecond,
    CommandLine,
    GetEnvironmentVariable,
    GetEnvironmentVariables,
    Exit,
    EmergencyExit,
}

/// Binds core Scheme procedures that don't fit in specific categories.
fn bind_core_procedures(env: &Arc<ThreadSafeEnvironment>) {
    // eq? - identity equality
    env.define("eq?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "eq?".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_eq),
        effects: vec![Effect::Pure],
    })));
    
    // eqv? - operational equivalence
    env.define("eqv?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "eqv?".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_eqv),
        effects: vec![Effect::Pure],
    })));
    
    // equal? - structural equality
    env.define("equal?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "equal?".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_equal),
        effects: vec![Effect::Pure],
    })));
    
    // not - logical negation
    env.define("not".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "not".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_not),
        effects: vec![Effect::Pure],
    })));
    
    // boolean? - boolean predicate
    env.define("boolean?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "boolean?".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_boolean_p),
        effects: vec![Effect::Pure],
    })));
    
    // boolean=? - boolean equality
    env.define("boolean=?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "boolean=?".to_string(),
        arity_min: 2,
        arity_max: None,
        implementation: PrimitiveImpl::RustFn(primitive_boolean_equal),
        effects: vec![Effect::Pure],
    })));
    
    // symbol? - symbol predicate
    env.define("symbol?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "symbol?".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_symbol_p),
        effects: vec![Effect::Pure],
    })));
    
    // symbol->string - convert symbol to string
    env.define("symbol->string".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "symbol->string".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_symbol_to_string),
        effects: vec![Effect::Pure],
    })));
    
    // string->symbol - convert string to symbol
    env.define("string->symbol".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "string->symbol".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_string_to_symbol),
        effects: vec![Effect::Pure],
    })));
    
    // procedure? - procedure predicate
    env.define("procedure?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "procedure?".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_procedure_p),
        effects: vec![Effect::Pure],
    })));
}

// ============= CORE PROCEDURE IMPLEMENTATIONS =============

use crate::diagnostics::{Error as DiagnosticError, Result};

/// eq? procedure - identity equality
fn primitive_eq(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("eq? expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    // For now, use PartialEq implementation
    // In a full implementation, this would check reference equality for mutable objects
    Ok(Value::boolean(args[0] == args[1]))
}

/// eqv? procedure - operational equivalence
fn primitive_eqv(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("eqv? expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    // For now, use PartialEq implementation
    // In a full implementation, this would be more nuanced than eq? but less than equal?
    Ok(Value::boolean(args[0] == args[1]))
}

/// equal? procedure - structural equality
fn primitive_equal(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("equal? expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    // For now, use PartialEq implementation
    // In a full implementation, this would recursively check structural equality
    Ok(Value::boolean(args[0] == args[1]))
}

/// not procedure - logical negation
fn primitive_not(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("not expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    Ok(Value::boolean(args[0].is_falsy()))
}

/// boolean? predicate
fn primitive_boolean_p(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("boolean? expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    let is_boolean = matches!(args[0], Value::Literal(crate::ast::Literal::Boolean(_)));
    Ok(Value::boolean(is_boolean))
}

/// boolean=? procedure - R7RS boolean equality
fn primitive_boolean_equal(args: &[Value]) -> Result<Value> {
    if args.len() < 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            "boolean=? requires at least 2 arguments".to_string(),
            None,
        )));
    }
    
    // All arguments must be boolean
    for (i, arg) in args.iter().enumerate() {
        if !matches!(arg, Value::Literal(crate::ast::Literal::Boolean(_))) {
            return Err(Box::new(DiagnosticError::runtime_error(
                format!("boolean=? argument {} is not a boolean", i + 1),
                None,
            )));
        }
    }
    
    // Check if all booleans are equal
    if let Value::Literal(crate::ast::Literal::Boolean(first)) = &args[0] {
        for arg in &args[1..] {
            if let Value::Literal(crate::ast::Literal::Boolean(val)) = arg {
                if first != val {
                    return Ok(Value::boolean(false));
                }
            }
        }
        Ok(Value::boolean(true))
    } else {
        // This shouldn't happen given the check above, but be safe
        Ok(Value::boolean(false))
    }
}

/// symbol? predicate
fn primitive_symbol_p(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("symbol? expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    Ok(Value::boolean(args[0].is_symbol()))
}

/// procedure? predicate
fn primitive_procedure_p(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("procedure? expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    Ok(Value::boolean(args[0].is_procedure()))
}

/// symbol->string procedure
fn primitive_symbol_to_string(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("symbol->string expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    match &args[0] {
        Value::Symbol(symbol_id) => {
            // Get the symbol name from the global symbol table
            use crate::utils::symbol::symbol_name;
            if let Some(name) = symbol_name(*symbol_id) {
                Ok(Value::string(name))
            } else {
                Err(Box::new(DiagnosticError::runtime_error(
                    "Invalid symbol ID".to_string(),
                    None,
                )))
            }
        },
        _ => Err(Box::new(DiagnosticError::runtime_error(
            "symbol->string requires a symbol argument".to_string(),
            None,
        ))),
    }
}

/// string->symbol procedure
fn primitive_string_to_symbol(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("string->symbol expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    match &args[0] {
        Value::Literal(crate::ast::Literal::String(s)) => {
            // Intern the string as a symbol
            use crate::utils::symbol::intern_symbol;
            let symbol_id = intern_symbol(s.clone());
            Ok(Value::symbol(symbol_id))
        },
        _ => Err(Box::new(DiagnosticError::runtime_error(
            "string->symbol requires a string argument".to_string(),
            None,
        ))),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils::symbol::intern_symbol;

    #[test]
    fn test_boolean_predicate() {
        // Test boolean? with boolean values
        let args = vec![Value::boolean(true)];
        let result = primitive_boolean_p(&args).unwrap();
        assert_eq!(result, Value::boolean(true));
        
        let args = vec![Value::boolean(false)];
        let result = primitive_boolean_p(&args).unwrap();
        assert_eq!(result, Value::boolean(true));
        
        // Test boolean? with non-boolean values
        let args = vec![Value::integer(42)];
        let result = primitive_boolean_p(&args).unwrap();
        assert_eq!(result, Value::boolean(false));
        
        let args = vec![Value::string("hello")];
        let result = primitive_boolean_p(&args).unwrap();
        assert_eq!(result, Value::boolean(false));
    }
    
    #[test]
    fn test_boolean_equal() {
        // Test boolean=? with equal booleans
        let args = vec![Value::boolean(true), Value::boolean(true)];
        let result = primitive_boolean_equal(&args).unwrap();
        assert_eq!(result, Value::boolean(true));
        
        let args = vec![Value::boolean(false), Value::boolean(false)];
        let result = primitive_boolean_equal(&args).unwrap();
        assert_eq!(result, Value::boolean(true));
        
        // Test boolean=? with unequal booleans
        let args = vec![Value::boolean(true), Value::boolean(false)];
        let result = primitive_boolean_equal(&args).unwrap();
        assert_eq!(result, Value::boolean(false));
        
        // Test boolean=? with multiple equal booleans
        let args = vec![Value::boolean(true), Value::boolean(true), Value::boolean(true)];
        let result = primitive_boolean_equal(&args).unwrap();
        assert_eq!(result, Value::boolean(true));
        
        // Test boolean=? with multiple mixed booleans
        let args = vec![Value::boolean(true), Value::boolean(true), Value::boolean(false)];
        let result = primitive_boolean_equal(&args).unwrap();
        assert_eq!(result, Value::boolean(false));
    }
    
    #[test]
    fn test_boolean_equal_errors() {
        // Test boolean=? with non-boolean arguments
        let args = vec![Value::integer(42), Value::boolean(true)];
        let result = primitive_boolean_equal(&args);
        assert!(result.is_err());
        
        let args = vec![Value::boolean(true), Value::string("hello")];
        let result = primitive_boolean_equal(&args);
        assert!(result.is_err());
        
        // Test boolean=? with too few arguments
        let args = vec![Value::boolean(true)];
        let result = primitive_boolean_equal(&args);
        assert!(result.is_err());
        
        let result = primitive_boolean_equal(&[]);
        assert!(result.is_err());
    }
    
    #[test]
    fn test_symbol_predicate() {
        // Test symbol? with symbol values
        let symbol_id = intern_symbol("test");
        let args = vec![Value::symbol(symbol_id)];
        let result = primitive_symbol_p(&args).unwrap();
        assert_eq!(result, Value::boolean(true));
        
        // Test symbol? with non-symbol values
        let args = vec![Value::string("test")];
        let result = primitive_symbol_p(&args).unwrap();
        assert_eq!(result, Value::boolean(false));
        
        let args = vec![Value::integer(42)];
        let result = primitive_symbol_p(&args).unwrap();
        assert_eq!(result, Value::boolean(false));
    }
    
    #[test]
    fn test_symbol_to_string() {
        // Test symbol->string conversion
        let symbol_id = intern_symbol("hello");
        let args = vec![Value::symbol(symbol_id)];
        let result = primitive_symbol_to_string(&args).unwrap();
        assert_eq!(result, Value::string("hello"));
        
        // Test another symbol
        let symbol_id = intern_symbol("world");
        let args = vec![Value::symbol(symbol_id)];
        let result = primitive_symbol_to_string(&args).unwrap();
        assert_eq!(result, Value::string("world"));
    }
    
    #[test]
    fn test_string_to_symbol() {
        // Test string->symbol conversion
        let args = vec![Value::string("hello")];
        let result = primitive_string_to_symbol(&args).unwrap();
        
        // Verify it's a symbol by converting back
        if let Value::Symbol(_) = result {
            let back_to_string = primitive_symbol_to_string(&[result]).unwrap();
            assert_eq!(back_to_string, Value::string("hello"));
        } else {
            panic!("Expected symbol result");
        }
    }
    
    #[test]
    fn test_symbol_roundtrip() {
        // Test that string->symbol and symbol->string are inverses
        let original = "test-symbol";
        let string_val = Value::string(original);
        
        // Convert to symbol
        let symbol_val = primitive_string_to_symbol(&[string_val]).unwrap();
        
        // Convert back to string
        let result_val = primitive_symbol_to_string(&[symbol_val]).unwrap();
        
        assert_eq!(result_val, Value::string(original));
    }
    
    #[test]
    fn test_symbol_string_errors() {
        // Test symbol->string with non-symbol
        let args = vec![Value::string("not-a-symbol")];
        let result = primitive_symbol_to_string(&args);
        assert!(result.is_err());
        
        // Test string->symbol with non-string
        let args = vec![Value::integer(42)];
        let result = primitive_string_to_symbol(&args);
        assert!(result.is_err());
        
        // Test wrong number of arguments
        let result = primitive_symbol_to_string(&[]);
        assert!(result.is_err());
        
        let result = primitive_string_to_symbol(&[]);
        assert!(result.is_err());
        
        let args = vec![Value::string("a"), Value::string("b")];
        let result = primitive_string_to_symbol(&args);
        assert!(result.is_err());
    }
    
    #[test]
    fn test_core_predicates() {
        // Test other core predicates from mod.rs
        
        // Test not
        let args = vec![Value::boolean(false)];
        let result = primitive_not(&args).unwrap();
        assert_eq!(result, Value::boolean(true));
        
        let args = vec![Value::boolean(true)];
        let result = primitive_not(&args).unwrap();
        assert_eq!(result, Value::boolean(false));
        
        let args = vec![Value::integer(0)]; // Everything except #f is truthy
        let result = primitive_not(&args).unwrap();
        assert_eq!(result, Value::boolean(false));
        
        // Test procedure?
        let proc = Arc::new(PrimitiveProcedure {
            name: "test".to_string(),
            arity_min: 0,
            arity_max: None,
            implementation: PrimitiveImpl::RustFn(|_| Ok(Value::Unspecified)),
            effects: vec![Effect::Pure],
        });
        let args = vec![Value::Primitive(proc)];
        let result = primitive_procedure_p(&args).unwrap();
        assert_eq!(result, Value::boolean(true));
        
        let args = vec![Value::integer(42)];
        let result = primitive_procedure_p(&args).unwrap();
        assert_eq!(result, Value::boolean(false));
    }
}