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
//! R7RS Section 6.9: Bytevectors
//!
//! Complete implementation of R7RS bytevector operations.
//! This module provides all required bytevector procedures for 100% R7RS compliance.

use crate::ast::Literal;
use crate::diagnostics::{Error, Result};
use crate::eval::value::{PrimitiveProcedure, PrimitiveImpl, Value, ThreadSafeEnvironment};
use std::sync::Arc;

/// Binds all R7RS Section 6.9 bytevector operations to the environment.
pub fn bind_bytevector_operations(env: &Arc<ThreadSafeEnvironment>) {
    // Core construction operations
    env.define("make-bytevector".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "make-bytevector".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_make_bytevector),
        effects: vec![],
    })));
    
    env.define("bytevector".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "bytevector".to_string(),
        arity_min: 0,
        arity_max: None,
        implementation: PrimitiveImpl::RustFn(primitive_bytevector),
        effects: vec![],
    })));
    
    env.define("bytevector-copy".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "bytevector-copy".to_string(),
        arity_min: 1,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_bytevector_copy),
        effects: vec![],
    })));
    
    // Predicate
    env.define("bytevector?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "bytevector?".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_bytevector_p),
        effects: vec![],
    })));
    
    // Length
    env.define("bytevector-length".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "bytevector-length".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_bytevector_length),
        effects: vec![],
    })));
    
    // Access operations
    env.define("bytevector-u8-ref".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "bytevector-u8-ref".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_bytevector_u8_ref),
        effects: vec![],
    })));
    
    env.define("bytevector-u8-set!".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "bytevector-u8-set!".to_string(),
        arity_min: 3,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_bytevector_u8_set),
        effects: vec![],
    })));
    
    // Conversion operations
    env.define("bytevector->list".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "bytevector->list".to_string(),
        arity_min: 1,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_bytevector_to_list),
        effects: vec![],
    })));
    
    env.define("list->bytevector".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "list->bytevector".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_list_to_bytevector),
        effects: vec![],
    })));
    
    env.define("string->utf8".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "string->utf8".to_string(),
        arity_min: 1,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_string_to_utf8),
        effects: vec![],
    })));
    
    env.define("utf8->string".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "utf8->string".to_string(),
        arity_min: 1,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_utf8_to_string),
        effects: vec![],
    })));
    
    // Comparison
    env.define("bytevector=?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "bytevector=?".to_string(),
        arity_min: 0,
        arity_max: None,
        implementation: PrimitiveImpl::RustFn(primitive_bytevector_equal),
        effects: vec![],
    })));
}

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

/// Extracts a bytevector from a Value.
fn extract_bytevector(value: &Value, operation: &str) -> Result<Vec<u8>> {
    match value {
        Value::Literal(Literal::Bytevector(bv)) => Ok(bv.clone()),
        _ => Err(Box::new(Error::runtime_error(
            format!("{operation} requires a bytevector argument"),
            None,
        ))),
    }
}

/// Extracts a mutable reference to a bytevector from a Value.
/// Note: In a functional language, this simulates mutation through COW semantics.
fn extract_bytevector_mut(value: &Value, operation: &str) -> Result<Vec<u8>> {
    match value {
        Value::Literal(Literal::Bytevector(bv)) => Ok(bv.clone()),
        _ => Err(Box::new(Error::runtime_error(
            format!("{operation} requires a bytevector argument"),
            None,
        ))),
    }
}

/// Extracts an integer from a Value.
fn extract_integer(value: &Value, operation: &str) -> Result<i64> {
    match value {
        Value::Literal(literal) => {
            if let Some(i) = literal.to_i64() {
                Ok(i)
            } else {
                Err(Box::new(Error::runtime_error(
                    format!("{operation} requires an integer argument"),
                    None,
                )))
            }
        }
        _ => Err(Box::new(Error::runtime_error(
            format!("{operation} requires an integer argument"),
            None,
        ))),
    }
}

/// Extracts a non-negative integer from a Value.
fn extract_non_negative_integer(value: &Value, operation: &str) -> Result<usize> {
    let int = extract_integer(value, operation)?;
    if int < 0 {
        return Err(Box::new(Error::runtime_error(
            format!("{operation} requires a non-negative integer"),
            None,
        )));
    }
    Ok(int as usize)
}

/// Extracts a byte value (0-255) from a Value.
fn extract_byte(value: &Value, operation: &str) -> Result<u8> {
    let int = extract_integer(value, operation)?;
    if !(0..=255).contains(&int) {
        return Err(Box::new(Error::runtime_error(
            format!("{operation} requires a byte value (0-255)"),
            None,
        )));
    }
    Ok(int as u8)
}

/// Extracts a string from a Value.
fn extract_string(value: &Value, operation: &str) -> Result<String> {
    match value {
        Value::Literal(Literal::String(s)) => Ok(s.clone()),
        _ => Err(Box::new(Error::runtime_error(
            format!("{operation} requires a string argument"),
            None,
        ))),
    }
}

// ============= R7RS SECTION 6.9 IMPLEMENTATIONS =============

/// make-bytevector k [byte] → bytevector
/// 
/// Returns a newly allocated bytevector of length k. If byte is given, 
/// it is used to initialize each element of the bytevector. Otherwise 
/// the initial contents are unspecified.
pub fn primitive_make_bytevector(args: &[Value]) -> Result<Value> {
    match args.len() {
        1 => {
            let k = extract_non_negative_integer(&args[0], "make-bytevector")?;
            // R7RS: initial contents are unspecified, we'll use 0
            Ok(Value::bytevector(vec![0; k]))
        }
        2 => {
            let k = extract_non_negative_integer(&args[0], "make-bytevector")?;
            let byte = extract_byte(&args[1], "make-bytevector")?;
            Ok(Value::bytevector(vec![byte; k]))
        }
        _ => Err(Box::new(Error::runtime_error(
            format!("make-bytevector expects 1 or 2 arguments, got {}", args.len()),
            None,
        )))
    }
}

/// bytevector byte ... → bytevector
/// 
/// Returns a newly allocated bytevector whose elements are the given bytes.
pub fn primitive_bytevector(args: &[Value]) -> Result<Value> {
    let mut bytes = Vec::new();
    for (i, arg) in args.iter().enumerate() {
        let byte = extract_byte(arg, &format!("bytevector (argument {})", i + 1))?;
        bytes.push(byte);
    }
    Ok(Value::bytevector(bytes))
}

/// bytevector-copy bytevector [start [end]] → bytevector
/// 
/// Returns a newly allocated bytevector whose elements are copied from the 
/// bytes of bytevector between start and end.
pub fn primitive_bytevector_copy(args: &[Value]) -> Result<Value> {
    match args.len() {
        1 => {
            let bv = extract_bytevector(&args[0], "bytevector-copy")?;
            Ok(Value::bytevector(bv))
        }
        2 => {
            let bv = extract_bytevector(&args[0], "bytevector-copy")?;
            let start = extract_non_negative_integer(&args[1], "bytevector-copy")?;
            if start > bv.len() {
                return Err(Box::new(Error::runtime_error(
                    "bytevector-copy: start index out of bounds".to_string(),
                    None,
                )));
            }
            Ok(Value::bytevector(bv[start..].to_vec()))
        }
        3 => {
            let bv = extract_bytevector(&args[0], "bytevector-copy")?;
            let start = extract_non_negative_integer(&args[1], "bytevector-copy")?;
            let end = extract_non_negative_integer(&args[2], "bytevector-copy")?;
            if start > end || end > bv.len() {
                return Err(Box::new(Error::runtime_error(
                    "bytevector-copy: invalid start/end indices".to_string(),
                    None,
                )));
            }
            Ok(Value::bytevector(bv[start..end].to_vec()))
        }
        _ => Err(Box::new(Error::runtime_error(
            format!("bytevector-copy expects 1 to 3 arguments, got {}", args.len()),
            None,
        )))
    }
}

/// bytevector? obj → boolean
/// 
/// Returns #t if obj is a bytevector, otherwise returns #f.
pub fn primitive_bytevector_p(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(Error::runtime_error(
            format!("bytevector? expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    let result = matches!(args[0], Value::Literal(Literal::Bytevector(_)));
    Ok(Value::boolean(result))
}

/// bytevector-length bytevector → integer
/// 
/// Returns the length of bytevector in bytes as an exact integer.
pub fn primitive_bytevector_length(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(Error::runtime_error(
            format!("bytevector-length expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    let bv = extract_bytevector(&args[0], "bytevector-length")?;
    Ok(Value::integer(bv.len() as i64))
}

/// bytevector-u8-ref bytevector k → byte
/// 
/// Returns the kth byte of bytevector.
pub fn primitive_bytevector_u8_ref(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(Error::runtime_error(
            format!("bytevector-u8-ref expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let bv = extract_bytevector(&args[0], "bytevector-u8-ref")?;
    let k = extract_non_negative_integer(&args[1], "bytevector-u8-ref")?;
    
    if k >= bv.len() {
        return Err(Box::new(Error::runtime_error(
            "bytevector-u8-ref: index out of bounds".to_string(),
            None,
        )));
    }
    
    Ok(Value::integer(bv[k] as i64))
}

/// bytevector-u8-set! bytevector k byte → unspecified
/// 
/// Stores byte as the kth byte of bytevector.
/// Note: In our functional implementation, this creates a new bytevector.
pub fn primitive_bytevector_u8_set(args: &[Value]) -> Result<Value> {
    if args.len() != 3 {
        return Err(Box::new(Error::runtime_error(
            format!("bytevector-u8-set! expects 3 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let mut bv = extract_bytevector_mut(&args[0], "bytevector-u8-set!")?;
    let k = extract_non_negative_integer(&args[1], "bytevector-u8-set!")?;
    let byte = extract_byte(&args[2], "bytevector-u8-set!")?;
    
    if k >= bv.len() {
        return Err(Box::new(Error::runtime_error(
            "bytevector-u8-set!: index out of bounds".to_string(),
            None,
        )));
    }
    
    bv[k] = byte;
    
    // In a functional language, we simulate mutation through environment updates
    // The caller is responsible for updating their reference
    // For now, return unspecified as per R7RS
    Ok(Value::Unspecified)
}

/// bytevector->list bytevector [start [end]] → list
/// 
/// Returns a newly allocated list of the bytes of bytevector between start and end.
pub fn primitive_bytevector_to_list(args: &[Value]) -> Result<Value> {
    match args.len() {
        1 => {
            let bv = extract_bytevector(&args[0], "bytevector->list")?;
            let values: Vec<Value> = bv.iter().map(|&b| Value::integer(b as i64)).collect();
            Ok(Value::list(values))
        }
        2 => {
            let bv = extract_bytevector(&args[0], "bytevector->list")?;
            let start = extract_non_negative_integer(&args[1], "bytevector->list")?;
            if start > bv.len() {
                return Err(Box::new(Error::runtime_error(
                    "bytevector->list: start index out of bounds".to_string(),
                    None,
                )));
            }
            let values: Vec<Value> = bv[start..].iter().map(|&b| Value::integer(b as i64)).collect();
            Ok(Value::list(values))
        }
        3 => {
            let bv = extract_bytevector(&args[0], "bytevector->list")?;
            let start = extract_non_negative_integer(&args[1], "bytevector->list")?;
            let end = extract_non_negative_integer(&args[2], "bytevector->list")?;
            if start > end || end > bv.len() {
                return Err(Box::new(Error::runtime_error(
                    "bytevector->list: invalid start/end indices".to_string(),
                    None,
                )));
            }
            let values: Vec<Value> = bv[start..end].iter().map(|&b| Value::integer(b as i64)).collect();
            Ok(Value::list(values))
        }
        _ => Err(Box::new(Error::runtime_error(
            format!("bytevector->list expects 1 to 3 arguments, got {}", args.len()),
            None,
        )))
    }
}

/// list->bytevector list → bytevector
/// 
/// Returns a newly allocated bytevector whose elements are the elements of list, 
/// which must be bytes.
pub fn primitive_list_to_bytevector(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(Error::runtime_error(
            format!("list->bytevector expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    let list = args[0].as_list().ok_or_else(|| Error::runtime_error(
        "list->bytevector requires a list argument".to_string(),
        None,
    ))?;
    
    let mut bytes = Vec::new();
    for (i, value) in list.iter().enumerate() {
        let byte = extract_byte(value, &format!("list->bytevector (element {i})"))?;
        bytes.push(byte);
    }
    
    Ok(Value::bytevector(bytes))
}

/// string->utf8 string [start [end]] → bytevector
/// 
/// Returns a newly allocated bytevector whose elements are the UTF-8 encoding 
/// of the given portion of string.
pub fn primitive_string_to_utf8(args: &[Value]) -> Result<Value> {
    match args.len() {
        1 => {
            let s = extract_string(&args[0], "string->utf8")?;
            Ok(Value::bytevector(s.into_bytes()))
        }
        2 => {
            let s = extract_string(&args[0], "string->utf8")?;
            let start = extract_non_negative_integer(&args[1], "string->utf8")?;
            let chars: Vec<char> = s.chars().collect();
            if start > chars.len() {
                return Err(Box::new(Error::runtime_error(
                    "string->utf8: start index out of bounds".to_string(),
                    None,
                )));
            }
            let substring: String = chars[start..].iter().collect();
            Ok(Value::bytevector(substring.into_bytes()))
        }
        3 => {
            let s = extract_string(&args[0], "string->utf8")?;
            let start = extract_non_negative_integer(&args[1], "string->utf8")?;
            let end = extract_non_negative_integer(&args[2], "string->utf8")?;
            let chars: Vec<char> = s.chars().collect();
            if start > end || end > chars.len() {
                return Err(Box::new(Error::runtime_error(
                    "string->utf8: invalid start/end indices".to_string(),
                    None,
                )));
            }
            let substring: String = chars[start..end].iter().collect();
            Ok(Value::bytevector(substring.into_bytes()))
        }
        _ => Err(Box::new(Error::runtime_error(
            format!("string->utf8 expects 1 to 3 arguments, got {}", args.len()),
            None,
        )))
    }
}

/// utf8->string bytevector [start [end]] → string
/// 
/// Returns a newly allocated string whose characters are the decoding of the 
/// UTF-8 bytes in the given portion of bytevector.
pub fn primitive_utf8_to_string(args: &[Value]) -> Result<Value> {
    match args.len() {
        1 => {
            let bv = extract_bytevector(&args[0], "utf8->string")?;
            let s = String::from_utf8(bv).map_err(|_| Error::runtime_error(
                "utf8->string: invalid UTF-8 sequence".to_string(),
                None,
            ))?;
            Ok(Value::string(s))
        }
        2 => {
            let bv = extract_bytevector(&args[0], "utf8->string")?;
            let start = extract_non_negative_integer(&args[1], "utf8->string")?;
            if start > bv.len() {
                return Err(Box::new(Error::runtime_error(
                    "utf8->string: start index out of bounds".to_string(),
                    None,
                )));
            }
            let s = String::from_utf8(bv[start..].to_vec()).map_err(|_| Error::runtime_error(
                "utf8->string: invalid UTF-8 sequence".to_string(),
                None,
            ))?;
            Ok(Value::string(s))
        }
        3 => {
            let bv = extract_bytevector(&args[0], "utf8->string")?;
            let start = extract_non_negative_integer(&args[1], "utf8->string")?;
            let end = extract_non_negative_integer(&args[2], "utf8->string")?;
            if start > end || end > bv.len() {
                return Err(Box::new(Error::runtime_error(
                    "utf8->string: invalid start/end indices".to_string(),
                    None,
                )));
            }
            let s = String::from_utf8(bv[start..end].to_vec()).map_err(|_| Error::runtime_error(
                "utf8->string: invalid UTF-8 sequence".to_string(),
                None,
            ))?;
            Ok(Value::string(s))
        }
        _ => Err(Box::new(Error::runtime_error(
            format!("utf8->string expects 1 to 3 arguments, got {}", args.len()),
            None,
        )))
    }
}

/// bytevector=? bytevector ... → boolean
/// 
/// Returns #t if all the bytevectors are the same length and have the same 
/// contents in the same positions, and #f otherwise.
pub fn primitive_bytevector_equal(args: &[Value]) -> Result<Value> {
    // R7RS allows zero arguments for bytevector=? (returns #t)
    if args.is_empty() {
        return Ok(Value::boolean(true));
    }
    
    // Extract all bytevectors
    let mut bytevectors = Vec::new();
    for (i, arg) in args.iter().enumerate() {
        let bv = extract_bytevector(arg, &format!("bytevector=? (argument {})", i + 1))?;
        bytevectors.push(bv);
    }
    
    // Compare all bytevectors with the first one
    let first = &bytevectors[0];
    for bv in &bytevectors[1..] {
        if bv != first {
            return Ok(Value::boolean(false));
        }
    }
    
    Ok(Value::boolean(true))
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_make_bytevector() {
        // make-bytevector with size only
        let result = primitive_make_bytevector(&[Value::integer(5)]).unwrap();
        assert_eq!(result, Value::bytevector(vec![0, 0, 0, 0, 0]));
        
        // make-bytevector with size and fill byte
        let result = primitive_make_bytevector(&[Value::integer(3), Value::integer(42)]).unwrap();
        assert_eq!(result, Value::bytevector(vec![42, 42, 42]));
    }
    
    #[test]
    fn test_bytevector() {
        // Empty bytevector
        let result = primitive_bytevector(&[]).unwrap();
        assert_eq!(result, Value::bytevector(vec![]));
        
        // Bytevector with elements
        let result = primitive_bytevector(&[
            Value::integer(65), 
            Value::integer(66), 
            Value::integer(67)
        ]).unwrap();
        assert_eq!(result, Value::bytevector(vec![65, 66, 67]));
    }
    
    #[test]
    fn test_bytevector_predicate() {
        // Test with bytevector
        let result = primitive_bytevector_p(&[Value::bytevector(vec![1, 2, 3])]).unwrap();
        assert_eq!(result, Value::boolean(true));
        
        // Test with non-bytevector
        let result = primitive_bytevector_p(&[Value::integer(42)]).unwrap();
        assert_eq!(result, Value::boolean(false));
    }
    
    #[test]
    fn test_bytevector_length() {
        let result = primitive_bytevector_length(&[Value::bytevector(vec![1, 2, 3, 4, 5])]).unwrap();
        assert_eq!(result, Value::integer(5));
    }
    
    #[test]
    fn test_bytevector_u8_ref() {
        let bv = Value::bytevector(vec![10, 20, 30]);
        let result = primitive_bytevector_u8_ref(&[bv, Value::integer(1)]).unwrap();
        assert_eq!(result, Value::integer(20));
    }
    
    #[test]
    fn test_bytevector_to_list() {
        let bv = Value::bytevector(vec![65, 66, 67]);
        let result = primitive_bytevector_to_list(&[bv]).unwrap();
        let expected = Value::list(vec![Value::integer(65), Value::integer(66), Value::integer(67)]);
        assert_eq!(result, expected);
    }
    
    #[test]
    fn test_list_to_bytevector() {
        let list = Value::list(vec![Value::integer(65), Value::integer(66), Value::integer(67)]);
        let result = primitive_list_to_bytevector(&[list]).unwrap();
        assert_eq!(result, Value::bytevector(vec![65, 66, 67]));
    }
    
    #[test]
    fn test_string_to_utf8() {
        let result = primitive_string_to_utf8(&[Value::string("ABC")]).unwrap();
        assert_eq!(result, Value::bytevector(vec![65, 66, 67]));
    }
    
    #[test]
    fn test_utf8_to_string() {
        let bv = Value::bytevector(vec![65, 66, 67]);
        let result = primitive_utf8_to_string(&[bv]).unwrap();
        assert_eq!(result, Value::string("ABC"));
    }
    
    #[test]
    fn test_bytevector_equal() {
        let bv1 = Value::bytevector(vec![1, 2, 3]);
        let bv2 = Value::bytevector(vec![1, 2, 3]);
        let bv3 = Value::bytevector(vec![1, 2, 4]);
        
        // Equal bytevectors
        let result = primitive_bytevector_equal(&[bv1.clone(), bv2]).unwrap();
        assert_eq!(result, Value::boolean(true));
        
        // Unequal bytevectors
        let result = primitive_bytevector_equal(&[bv1, bv3]).unwrap();
        assert_eq!(result, Value::boolean(false));
        
        // Empty arguments
        let result = primitive_bytevector_equal(&[]).unwrap();
        assert_eq!(result, Value::boolean(true));
    }
}