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
//! Type operations for the Lambdust standard library.
//!
//! This module implements Lambdust-specific type operations including
//! type checking, type manipulation, and gradual typing support.

use crate::diagnostics::{Error as DiagnosticError, Result};
use crate::eval::value::{Value, PrimitiveProcedure, PrimitiveImpl, ThreadSafeEnvironment};
use crate::effects::Effect;
use std::sync::Arc;

/// Creates type operation bindings for the standard library.
pub fn create_type_bindings(env: &Arc<ThreadSafeEnvironment>) {
    // Type queries
    bind_type_queries(env);
    
    // Type operations
    bind_type_operations(env);
    
    // Type checking
    bind_type_checking(env);
    
    // Gradual typing support
    bind_gradual_typing(env);
}

/// Binds type query operations.
fn bind_type_queries(env: &Arc<ThreadSafeEnvironment>) {
    // type-of
    env.define("type-of".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "type-of".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_type_of),
        effects: vec![Effect::Pure],
    })));
    
    // type?
    env.define("type?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "type?".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_type_p),
        effects: vec![Effect::Pure],
    })));
    
    // type-name
    env.define("type-name".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "type-name".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_type_name),
        effects: vec![Effect::Pure],
    })));
}

/// Binds type operation functions.
fn bind_type_operations(env: &Arc<ThreadSafeEnvironment>) {
    // type-union
    env.define("type-union".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "type-union".to_string(),
        arity_min: 2,
        arity_max: None,
        implementation: PrimitiveImpl::RustFn(primitive_type_union),
        effects: vec![Effect::Pure],
    })));
    
    // type-intersection
    env.define("type-intersection".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "type-intersection".to_string(),
        arity_min: 2,
        arity_max: None,
        implementation: PrimitiveImpl::RustFn(primitive_type_intersection),
        effects: vec![Effect::Pure],
    })));
    
    // type-difference
    env.define("type-difference".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "type-difference".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_type_difference),
        effects: vec![Effect::Pure],
    })));
    
    // subtype?
    env.define("subtype?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "subtype?".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_subtype_p),
        effects: vec![Effect::Pure],
    })));
    
    // type-equivalent?
    env.define("type-equivalent?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "type-equivalent?".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_type_equivalent_p),
        effects: vec![Effect::Pure],
    })));
}

/// Binds type checking operations.
fn bind_type_checking(env: &Arc<ThreadSafeEnvironment>) {
    // type-check
    env.define("type-check".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "type-check".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_type_check),
        effects: vec![Effect::Pure],
    })));
    
    // type-assert
    env.define("type-assert".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "type-assert".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_type_assert),
        effects: vec![Effect::Error], // Can throw type errors
    })));
    
    // type-cast
    env.define("type-cast".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "type-cast".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_type_cast),
        effects: vec![Effect::Pure],
    })));
}

/// Binds gradual typing support.
fn bind_gradual_typing(env: &Arc<ThreadSafeEnvironment>) {
    // any-type
    env.define("any-type".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "any-type".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_any_type),
        effects: vec![Effect::Pure],
    })));
    
    // unknown-type
    env.define("unknown-type".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "unknown-type".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_unknown_type),
        effects: vec![Effect::Pure],
    })));
    
    // make-function-type
    env.define("make-function-type".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "make-function-type".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_make_function_type),
        effects: vec![Effect::Pure],
    })));
    
    // function-type?
    env.define("function-type?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "function-type?".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_function_type_p),
        effects: vec![Effect::Pure],
    })));
    
    // function-parameter-types
    env.define("function-parameter-types".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "function-parameter-types".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_function_parameter_types),
        effects: vec![Effect::Pure],
    })));
    
    // function-return-type
    env.define("function-return-type".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "function-return-type".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_function_return_type),
        effects: vec![Effect::Pure],
    })));
}

// ============= IMPLEMENTATIONS =============

/// type-of procedure
fn primitive_type_of(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("type-of expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    let type_name = get_value_type_name(&args[0]);
    Ok(Value::string(type_name))
}

/// type? predicate
fn primitive_type_p(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("type? expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    // Check if the value is a type representation
    let is_type = matches!(args[0], Value::Type(_));
    Ok(Value::boolean(is_type))
}

/// type-name procedure
fn primitive_type_name(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("type-name expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    match &args[0] {
        Value::Type(type_val) => {
            let name = match type_val.as_ref() {
                crate::eval::value::TypeValue::Base(name) => name.clone(),
                crate::eval::value::TypeValue::Function { .. } => "function".to_string(),
                crate::eval::value::TypeValue::Union(_) => "union".to_string(),
                crate::eval::value::TypeValue::Intersection(_) => "intersection".to_string(),
                crate::eval::value::TypeValue::Variable(name) => name.clone(),
            };
            Ok(Value::string(name))
        }
        _ => Err(Box::new(DiagnosticError::runtime_error(
            "type-name requires a type argument".to_string(),
            None,
        ))),
    }
}

/// type-union procedure
fn primitive_type_union(_args: &[Value]) -> Result<Value> {
    // Placeholder implementation
    Err(Box::new(DiagnosticError::runtime_error(
        "type-union requires type system integration (not yet implemented)".to_string(),
        None,
    )))
}

/// type-intersection procedure
fn primitive_type_intersection(_args: &[Value]) -> Result<Value> {
    // Placeholder implementation
    Err(Box::new(DiagnosticError::runtime_error(
        "type-intersection requires type system integration (not yet implemented)".to_string(),
        None,
    )))
}

/// type-difference procedure
fn primitive_type_difference(_args: &[Value]) -> Result<Value> {
    // Placeholder implementation
    Err(Box::new(DiagnosticError::runtime_error(
        "type-difference requires type system integration (not yet implemented)".to_string(),
        None,
    )))
}

/// subtype? predicate
fn primitive_subtype_p(_args: &[Value]) -> Result<Value> {
    // Placeholder implementation
    Err(Box::new(DiagnosticError::runtime_error(
        "subtype? requires type system integration (not yet implemented)".to_string(),
        None,
    )))
}

/// type-equivalent? predicate
fn primitive_type_equivalent_p(_args: &[Value]) -> Result<Value> {
    // Placeholder implementation
    Err(Box::new(DiagnosticError::runtime_error(
        "type-equivalent? requires type system integration (not yet implemented)".to_string(),
        None,
    )))
}

/// type-check procedure
fn primitive_type_check(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("type-check expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    // For now, just return whether the value matches the basic type
    let value = &args[0];
    let expected_type = args[1].as_string().unwrap_or("unknown");
    let actual_type = get_value_type_name(value);
    
    Ok(Value::boolean(actual_type == expected_type))
}

/// type-assert procedure
fn primitive_type_assert(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("type-assert expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let type_check_result = primitive_type_check(args)?;
    
    if type_check_result.is_truthy() {
        Ok(args[0].clone())
    } else {
        Err(Box::new(DiagnosticError::runtime_error(
            format!("Type assertion failed: expected {}, got {}", 
                    args[1], get_value_type_name(&args[0])),
            None,
        )))
    }
}

/// type-cast procedure
fn primitive_type_cast(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("type-cast expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    // For now, just return the value unchanged
    // In a full implementation, this would perform type conversion
    Ok(args[0].clone())
}

/// any-type procedure
fn primitive_any_type(_args: &[Value]) -> Result<Value> {
    // Return a representation of the "any" type
    Ok(Value::Type(Arc::new(crate::eval::value::TypeValue::Base("any".to_string()))))
}

/// unknown-type procedure
fn primitive_unknown_type(_args: &[Value]) -> Result<Value> {
    // Return a representation of the "unknown" type
    Ok(Value::Type(Arc::new(crate::eval::value::TypeValue::Base("unknown".to_string()))))
}

/// make-function-type procedure
fn primitive_make_function_type(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("make-function-type expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    // Placeholder implementation
    Err(Box::new(DiagnosticError::runtime_error(
        "make-function-type requires type system integration (not yet implemented)".to_string(),
        None,
    )))
}

/// function-type? predicate
fn primitive_function_type_p(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("function-type? expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    match &args[0] {
        Value::Type(type_val) => {
            let is_function = matches!(type_val.as_ref(), crate::eval::value::TypeValue::Function { .. });
            Ok(Value::boolean(is_function))
        }
        _ => Ok(Value::boolean(false)),
    }
}

/// function-parameter-types procedure
fn primitive_function_parameter_types(_args: &[Value]) -> Result<Value> {
    // Placeholder implementation
    Err(Box::new(DiagnosticError::runtime_error(
        "function-parameter-types requires type system integration (not yet implemented)".to_string(),
        None,
    )))
}

/// function-return-type procedure
fn primitive_function_return_type(_args: &[Value]) -> Result<Value> {
    // Placeholder implementation
    Err(Box::new(DiagnosticError::runtime_error(
        "function-return-type requires type system integration (not yet implemented)".to_string(),
        None,
    )))
}

// ============= HELPER FUNCTIONS =============

/// Gets the type name of a value.
fn get_value_type_name(value: &Value) -> String {
    match value {
        Value::Literal(lit) => match lit {
            crate::ast::Literal::ExactInteger(_) => "integer".to_string(),
            crate::ast::Literal::InexactReal(_) => "real".to_string(),
            crate::ast::Literal::Number(_) => "number".to_string(),
            crate::ast::Literal::Rational { .. } => "rational".to_string(),
            crate::ast::Literal::Complex { .. } => "complex".to_string(),
            crate::ast::Literal::String(_) => "string".to_string(),
            crate::ast::Literal::Character(_) => "character".to_string(),
            crate::ast::Literal::Boolean(_) => "boolean".to_string(),
            crate::ast::Literal::Bytevector(_) => "bytevector".to_string(),
            crate::ast::Literal::Nil => "null".to_string(),
            crate::ast::Literal::Unspecified => "unspecified".to_string(),
        },
        Value::Symbol(_) => "symbol".to_string(),
        Value::Keyword(_) => "keyword".to_string(),
        Value::Nil => "null".to_string(),
        Value::Unspecified => "unspecified".to_string(),
        Value::Pair(_, _) => "pair".to_string(),
        Value::MutablePair(_, _) => "pair".to_string(),
        Value::Vector(_) => "vector".to_string(),
        Value::Hashtable(_) => "hashtable".to_string(),
        Value::Procedure(_) => "procedure".to_string(),
        Value::Primitive(_) => "primitive".to_string(),
        Value::Continuation(_) => "continuation".to_string(),
        Value::Syntax(_) => "syntax".to_string(),
        Value::Port(_) => "port".to_string(),
        Value::Promise(_) => "promise".to_string(),
        Value::Type(_) => "type".to_string(),
        Value::Foreign(obj) => obj.type_name.clone(),
        Value::ErrorObject(_) => "error".to_string(),
        Value::CharSet(_) => "char-set".to_string(),
        Value::Parameter(_) => "parameter".to_string(),
        Value::CaseLambda(_) => "procedure".to_string(), // case-lambda is a type of procedure
        Value::Record(_) => "record".to_string(),
        // Advanced container types
        Value::AdvancedHashTable(_) => "advanced-hash-table".to_string(),
        Value::Ideque(_) => "ideque".to_string(),
        Value::PriorityQueue(_) => "priority-queue".to_string(),
        Value::OrderedSet(_) => "ordered-set".to_string(),
        Value::ListQueue(_) => "list-queue".to_string(),
        Value::RandomAccessList(_) => "random-access-list".to_string(),
        // Concurrency types (only available with async-runtime)
        #[cfg(feature = "async-runtime")]
        Value::Future(_) => "future".to_string(),
        #[cfg(feature = "async-runtime")]
        Value::Channel(_) => "channel".to_string(),
        #[cfg(feature = "async-runtime")]
        Value::Mutex(_) => "mutex".to_string(),
        #[cfg(feature = "async-runtime")]
        Value::Semaphore(_) => "semaphore".to_string(),
        #[cfg(feature = "async-runtime")]
        Value::AtomicCounter(_) => "atomic-counter".to_string(),
        #[cfg(feature = "async-runtime")]
        Value::DistributedNode(_) => "distributed-node".to_string(),
        Value::MutableString(_) => "string".to_string(),
        Value::Set(_) => "set".to_string(),
        Value::Bag(_) => "bag".to_string(),
        Value::Generator(_) => "generator".to_string(),
        Value::Opaque(_) => "opaque".to_string(),
    }
}

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

    #[test]
    fn test_type_of() {
        let args = vec![Value::integer(42)];
        let result = primitive_type_of(&args).unwrap();
        assert_eq!(result, Value::string("number"));
        
        let args = vec![Value::string("hello")];
        let result = primitive_type_of(&args).unwrap();
        assert_eq!(result, Value::string("string"));
        
        let args = vec![Value::boolean(true)];
        let result = primitive_type_of(&args).unwrap();
        assert_eq!(result, Value::string("boolean"));
    }
    
    #[test]
    fn test_type_predicate() {
        let not_type = Value::integer(42);
        let result = primitive_type_p(&[not_type]).unwrap();
        assert_eq!(result, Value::boolean(false));
        
        let type_val = Value::Type(Arc::new(crate::eval::value::TypeValue::Base("number".to_string())));
        let result = primitive_type_p(&[type_val]).unwrap();
        assert_eq!(result, Value::boolean(true));
    }
    
    #[test]
    fn test_type_check() {
        let args = vec![Value::integer(42), Value::string("number")];
        let result = primitive_type_check(&args).unwrap();
        assert_eq!(result, Value::boolean(true));
        
        let args = vec![Value::string("hello"), Value::string("number")];
        let result = primitive_type_check(&args).unwrap();
        assert_eq!(result, Value::boolean(false));
    }
    
    #[test]
    fn test_type_assert() {
        let args = vec![Value::integer(42), Value::string("number")];
        let result = primitive_type_assert(&args).unwrap();
        assert_eq!(result, Value::integer(42));
        
        let args = vec![Value::string("hello"), Value::string("number")];
        let result = primitive_type_assert(&args);
        assert!(result.is_err());
    }
    
    #[test]
    fn test_any_type() {
        let result = primitive_any_type(&[]).unwrap();
        assert!(matches!(result, Value::Type(_)));
    }
}