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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
//! SRFI-14 Character Sets Implementation
//!
//! This module provides character set data types and operations for text processing.
//! It implements the complete SRFI-14 specification with efficient Unicode support.

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

/// Character set implementation using a BTreeSet for efficient Unicode support.
/// 
/// BTreeSet provides:
/// - O(log n) insertion, deletion, and lookup
/// - Efficient range operations
/// - Ordered iteration
/// - Compact storage for sparse character sets
/// - Good performance for both ASCII and Unicode
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CharSet {
    /// Characters in this set, stored as a sorted set of Unicode code points
    chars: BTreeSet<char>,
}

impl CharSet {
    /// Creates a new empty character set.
    pub fn new() -> Self {
        Self {
            chars: BTreeSet::new(),
        }
    }

    /// Creates a character set from an iterator of characters.
    pub fn from_chars<I>(chars: I) -> Self
    where
        I: IntoIterator<Item = char>,
    {
        Self {
            chars: chars.into_iter().collect(),
        }
    }

    /// Creates a character set from a string.
    pub fn from_string(s: &str) -> Self {
        Self::from_chars(s.chars())
    }

    /// Creates a character set from a Unicode range.
    pub fn from_range(start: char, end: char) -> Self {
        if start > end {
            return Self::new();
        }
        
        let start_code = start as u32;
        let end_code = end as u32;
        let chars = (start_code..=end_code)
            .filter_map(char::from_u32)
            .collect();
        
        Self { chars }
    }

    /// Creates a character set with a single character.
    pub fn singleton(c: char) -> Self {
        let mut chars = BTreeSet::new();
        chars.insert(c);
        Self { chars }
    }

    /// Checks if the character set is empty.
    pub fn is_empty(&self) -> bool {
        self.chars.is_empty()
    }

    /// Returns the number of characters in the set.
    pub fn size(&self) -> usize {
        self.chars.len()
    }

    /// Checks if a character is in the set.
    pub fn contains(&self, c: char) -> bool {
        self.chars.contains(&c)
    }

    /// Adds a character to the set (returns a new set).
    pub fn insert(&self, c: char) -> Self {
        let mut new_chars = self.chars.clone();
        new_chars.insert(c);
        Self { chars: new_chars }
    }

    /// Removes a character from the set (returns a new set).
    pub fn remove(&self, c: char) -> Self {
        let mut new_chars = self.chars.clone();
        new_chars.remove(&c);
        Self { chars: new_chars }
    }

    /// Returns the union of two character sets.
    pub fn union(&self, other: &Self) -> Self {
        let chars = self.chars.union(&other.chars).cloned().collect();
        Self { chars }
    }

    /// Returns the intersection of two character sets.
    pub fn intersection(&self, other: &Self) -> Self {
        let chars = self.chars.intersection(&other.chars).cloned().collect();
        Self { chars }
    }

    /// Returns the difference between two character sets (self - other).
    pub fn difference(&self, other: &Self) -> Self {
        let chars = self.chars.difference(&other.chars).cloned().collect();
        Self { chars }
    }

    /// Returns the symmetric difference (XOR) of two character sets.
    pub fn symmetric_difference(&self, other: &Self) -> Self {
        let chars = self.chars.symmetric_difference(&other.chars).cloned().collect();
        Self { chars }
    }

    /// Returns the complement of this character set (all Unicode characters not in this set).
    /// Note: This is impractical for Unicode, so we implement it for a reasonable subset.
    pub fn complement(&self) -> Self {
        // For practical purposes, complement against printable ASCII + common Unicode ranges
        let mut complement_chars = BTreeSet::new();
        
        // Add ASCII printable characters not in the set
        for code in 32..=126 {
            if let Some(c) = char::from_u32(code) {
                if !self.chars.contains(&c) {
                    complement_chars.insert(c);
                }
            }
        }
        
        // Add common whitespace characters not in the set
        let whitespace_chars = ['\t', '\n', '\r', ' '];
        for &c in &whitespace_chars {
            if !self.chars.contains(&c) {
                complement_chars.insert(c);
            }
        }
        
        Self { chars: complement_chars }
    }

    /// Checks if this set is a subset of another set.
    pub fn is_subset(&self, other: &Self) -> bool {
        self.chars.is_subset(&other.chars)
    }

    /// Checks if this set is equal to another set.
    pub fn is_equal(&self, other: &Self) -> bool {
        self.chars == other.chars
    }

    /// Returns an iterator over the characters in the set.
    pub fn iter(&self) -> impl Iterator<Item = &char> {
        self.chars.iter()
    }

    /// Converts the character set to a vector of characters.
    pub fn to_vec(&self) -> Vec<char> {
        self.chars.iter().cloned().collect()
    }

    /// Filters a character set using a predicate function.
    pub fn filter<F>(&self, predicate: F) -> Self
    where
        F: Fn(char) -> bool,
    {
        let chars = self.chars.iter().filter(|&&c| predicate(c)).cloned().collect();
        Self { chars }
    }

    /// Counts characters in the set that satisfy a predicate.
    pub fn count<F>(&self, predicate: F) -> usize
    where
        F: Fn(char) -> bool,
    {
        self.chars.iter().filter(|&&c| predicate(c)).count()
    }
}

impl Default for CharSet {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for CharSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "#<char-set")?;
        if self.chars.is_empty() {
            write!(f, " empty")?;
        } else {
            write!(f, " size={}", self.size())?;
            
            // Show a preview of characters for small sets
            if self.size() <= 10 {
                write!(f, " {{")?;
                for (i, &c) in self.chars.iter().enumerate() {
                    if i > 0 {
                        write!(f, " ")?;
                    }
                    if c.is_ascii_graphic() || c == ' ' {
                        write!(f, "{c}")?;
                    } else {
                        write!(f, "\\u{{{:04x}}}", c as u32)?;
                    }
                }
                write!(f, "}}")?;
            }
        }
        write!(f, ">")
    }
}

/// Standard character sets as defined by SRFI-14
pub struct StandardCharSets;

impl StandardCharSets {
    /// Lower-case letters
    pub fn lower_case() -> CharSet {
        CharSet::from_chars(('a'..='z').chain('à'..='ÿ').filter(|c| c.is_lowercase()))
    }

    /// Upper-case letters
    pub fn upper_case() -> CharSet {
        CharSet::from_chars(('A'..='Z').chain('À'..='Þ').filter(|c| c.is_uppercase()))
    }

    /// ASCII digits
    pub fn digit() -> CharSet {
        CharSet::from_chars('0'..='9')
    }

    /// Letters (both upper and lower case)
    pub fn letter() -> CharSet {
        Self::lower_case().union(&Self::upper_case())
    }

    /// Alphanumeric characters (letters + digits)
    pub fn letter_plus_digit() -> CharSet {
        Self::letter().union(&Self::digit())
    }

    /// Whitespace characters
    pub fn whitespace() -> CharSet {
        CharSet::from_chars([' ', '\t', '\n', '\r', '\x0C', '\x0B'].iter().cloned())
    }

    /// ASCII punctuation characters
    pub fn punctuation() -> CharSet {
        let punct_chars = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
        CharSet::from_string(punct_chars)
    }

    /// ASCII graphic characters (visible characters)
    pub fn graphic() -> CharSet {
        CharSet::from_chars((33..=126).filter_map(char::from_u32))
    }

    /// ASCII printable characters (graphic + space)
    pub fn printing() -> CharSet {
        Self::graphic().union(&CharSet::singleton(' '))
    }

    /// ASCII control characters
    pub fn ascii() -> CharSet {
        CharSet::from_chars((0..=127).filter_map(char::from_u32))
    }

    /// Empty character set
    pub fn empty() -> CharSet {
        CharSet::new()
    }

    /// Full ASCII character set
    pub fn full() -> CharSet {
        Self::ascii()
    }

    /// Hexadecimal digit characters
    pub fn hex_digit() -> CharSet {
        CharSet::from_string("0123456789ABCDEFabcdef")
    }

    /// Blank characters (space and tab)
    pub fn blank() -> CharSet {
        CharSet::from_chars([' ', '\t'].iter().cloned())
    }

    /// ISO control characters
    pub fn iso_control() -> CharSet {
        CharSet::from_chars((0..=31).chain(127..=159).filter_map(char::from_u32))
    }
}

/// Binds character set procedures to the environment
pub fn create_charset_bindings(env: &Arc<ThreadSafeEnvironment>) {
    // Character set predicates
    bind_charset_predicates(env);
    
    // Character set constructors
    bind_charset_constructors(env);
    
    // Character set operations
    bind_charset_operations(env);
    
    // Standard character sets
    bind_standard_charsets(env);
    
    // Character set conversions
    bind_charset_conversions(env);
}

fn bind_charset_predicates(env: &Arc<ThreadSafeEnvironment>) {
    // char-set?
    env.define("char-set?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set?".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_char_set_p),
        effects: vec![Effect::Pure],
    })));

    // char-set=
    env.define("char-set=".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set=".to_string(),
        arity_min: 2,
        arity_max: None,
        implementation: PrimitiveImpl::RustFn(primitive_char_set_equal),
        effects: vec![Effect::Pure],
    })));

    // char-set<=
    env.define("char-set<=".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set<=".to_string(),
        arity_min: 2,
        arity_max: None,
        implementation: PrimitiveImpl::RustFn(primitive_char_set_subset),
        effects: vec![Effect::Pure],
    })));

    // char-set-contains?
    env.define("char-set-contains?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set-contains?".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_char_set_contains),
        effects: vec![Effect::Pure],
    })));
}

fn bind_charset_constructors(env: &Arc<ThreadSafeEnvironment>) {
    // char-set
    env.define("char-set".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set".to_string(),
        arity_min: 0,
        arity_max: None,
        implementation: PrimitiveImpl::RustFn(primitive_char_set),
        effects: vec![Effect::Pure],
    })));

    // list->char-set
    env.define("list->char-set".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "list->char-set".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_list_to_char_set),
        effects: vec![Effect::Pure],
    })));

    // string->char-set
    env.define("string->char-set".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "string->char-set".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_string_to_char_set),
        effects: vec![Effect::Pure],
    })));

    // char-set-filter
    env.define("char-set-filter".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set-filter".to_string(),
        arity_min: 2,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_char_set_filter),
        effects: vec![Effect::Pure],
    })));

    // ucs-range->char-set
    env.define("ucs-range->char-set".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "ucs-range->char-set".to_string(),
        arity_min: 2,
        arity_max: Some(4),
        implementation: PrimitiveImpl::RustFn(primitive_ucs_range_to_char_set),
        effects: vec![Effect::Pure],
    })));
}

fn bind_charset_operations(env: &Arc<ThreadSafeEnvironment>) {
    // char-set-size
    env.define("char-set-size".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set-size".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_char_set_size),
        effects: vec![Effect::Pure],
    })));

    // char-set-count
    env.define("char-set-count".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set-count".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_char_set_count),
        effects: vec![Effect::Pure],
    })));

    // char-set-union
    env.define("char-set-union".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set-union".to_string(),
        arity_min: 0,
        arity_max: None,
        implementation: PrimitiveImpl::RustFn(primitive_char_set_union),
        effects: vec![Effect::Pure],
    })));

    // char-set-intersection
    env.define("char-set-intersection".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set-intersection".to_string(),
        arity_min: 0,
        arity_max: None,
        implementation: PrimitiveImpl::RustFn(primitive_char_set_intersection),
        effects: vec![Effect::Pure],
    })));

    // char-set-difference
    env.define("char-set-difference".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set-difference".to_string(),
        arity_min: 1,
        arity_max: None,
        implementation: PrimitiveImpl::RustFn(primitive_char_set_difference),
        effects: vec![Effect::Pure],
    })));

    // char-set-complement
    env.define("char-set-complement".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set-complement".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_char_set_complement),
        effects: vec![Effect::Pure],
    })));

    // char-set-xor
    env.define("char-set-xor".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set-xor".to_string(),
        arity_min: 0,
        arity_max: None,
        implementation: PrimitiveImpl::RustFn(primitive_char_set_xor),
        effects: vec![Effect::Pure],
    })));
}

fn bind_standard_charsets(env: &Arc<ThreadSafeEnvironment>) {
    // Standard character sets
    env.define("char-set:lower-case".to_string(), Value::CharSet(Arc::new(StandardCharSets::lower_case())));
    env.define("char-set:upper-case".to_string(), Value::CharSet(Arc::new(StandardCharSets::upper_case())));
    env.define("char-set:digit".to_string(), Value::CharSet(Arc::new(StandardCharSets::digit())));
    env.define("char-set:letter".to_string(), Value::CharSet(Arc::new(StandardCharSets::letter())));
    env.define("char-set:letter+digit".to_string(), Value::CharSet(Arc::new(StandardCharSets::letter_plus_digit())));
    env.define("char-set:whitespace".to_string(), Value::CharSet(Arc::new(StandardCharSets::whitespace())));
    env.define("char-set:punctuation".to_string(), Value::CharSet(Arc::new(StandardCharSets::punctuation())));
    env.define("char-set:graphic".to_string(), Value::CharSet(Arc::new(StandardCharSets::graphic())));
    env.define("char-set:printing".to_string(), Value::CharSet(Arc::new(StandardCharSets::printing())));
    env.define("char-set:ascii".to_string(), Value::CharSet(Arc::new(StandardCharSets::ascii())));
    env.define("char-set:empty".to_string(), Value::CharSet(Arc::new(StandardCharSets::empty())));
    env.define("char-set:full".to_string(), Value::CharSet(Arc::new(StandardCharSets::full())));
    env.define("char-set:hex-digit".to_string(), Value::CharSet(Arc::new(StandardCharSets::hex_digit())));
    env.define("char-set:blank".to_string(), Value::CharSet(Arc::new(StandardCharSets::blank())));
    env.define("char-set:iso-control".to_string(), Value::CharSet(Arc::new(StandardCharSets::iso_control())));
}

fn bind_charset_conversions(env: &Arc<ThreadSafeEnvironment>) {
    // char-set->list
    env.define("char-set->list".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set->list".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_char_set_to_list),
        effects: vec![Effect::Pure],
    })));

    // char-set->string
    env.define("char-set->string".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "char-set->string".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_char_set_to_string),
        effects: vec![Effect::Pure],
    })));
}

// ============= PRIMITIVE IMPLEMENTATIONS =============

/// Helper function to extract CharSet from Value
fn get_charset(value: &Value) -> Result<&CharSet> {
    match value {
        Value::CharSet(charset) => Ok(charset),
        _ => Err(Box::new(DiagnosticError::runtime_error(
            "Expected character set".to_string(),
            None,
        ))),
    }
}

/// Helper function to extract char from Value
fn get_char(value: &Value) -> Result<char> {
    match value {
        Value::Literal(Literal::Character(c)) => Ok(*c),
        _ => Err(Box::new(DiagnosticError::runtime_error(
            "Expected character".to_string(),
            None,
        ))),
    }
}

// Predicates

fn primitive_char_set_p(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("char-set? expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    Ok(Value::boolean(matches!(args[0], Value::CharSet(_))))
}

fn primitive_char_set_equal(args: &[Value]) -> Result<Value> {
    if args.len() < 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            "char-set= requires at least 2 arguments".to_string(),
            None,
        )));
    }

    let first = get_charset(&args[0])?;
    for arg in &args[1..] {
        let charset = get_charset(arg)?;
        if !first.is_equal(charset) {
            return Ok(Value::boolean(false));
        }
    }
    
    Ok(Value::boolean(true))
}

fn primitive_char_set_subset(args: &[Value]) -> Result<Value> {
    if args.len() < 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            "char-set<= requires at least 2 arguments".to_string(),
            None,
        )));
    }

    for i in 0..args.len() - 1 {
        let current = get_charset(&args[i])?;
        let next = get_charset(&args[i + 1])?;
        if !current.is_subset(next) {
            return Ok(Value::boolean(false));
        }
    }
    
    Ok(Value::boolean(true))
}

fn primitive_char_set_contains(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("char-set-contains? expects 2 arguments, got {}", args.len()),
            None,
        )));
    }

    let charset = get_charset(&args[0])?;
    let c = get_char(&args[1])?;
    
    Ok(Value::boolean(charset.contains(c)))
}

// Constructors

fn primitive_char_set(args: &[Value]) -> Result<Value> {
    let mut chars = Vec::new();
    
    for arg in args {
        let c = get_char(arg)?;
        chars.push(c);
    }
    
    Ok(Value::CharSet(Arc::new(CharSet::from_chars(chars))))
}

fn primitive_list_to_char_set(args: &[Value]) -> Result<Value> {
    if args.is_empty() || args.len() > 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("list->char-set expects 1 or 2 arguments, got {}", args.len()),
            None,
        )));
    }

    let char_list = args[0].as_list().ok_or_else(|| {
        Box::new(DiagnosticError::runtime_error("Expected a list".to_string(), None))
    })?;

    let mut chars = Vec::new();
    for value in char_list {
        let c = get_char(&value)?;
        chars.push(c);
    }

    let base_charset = if args.len() == 2 {
        get_charset(&args[1])?.clone()
    } else {
        CharSet::new()
    };

    let new_charset = CharSet::from_chars(chars).union(&base_charset);
    Ok(Value::CharSet(Arc::new(new_charset)))
}

fn primitive_string_to_char_set(args: &[Value]) -> Result<Value> {
    if args.is_empty() || args.len() > 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("string->char-set expects 1 or 2 arguments, got {}", args.len()),
            None,
        )));
    }

    let s = args[0].as_string().ok_or_else(|| {
        Box::new(DiagnosticError::runtime_error("Expected a string".to_string(), None))
    })?;

    let base_charset = if args.len() == 2 {
        get_charset(&args[1])?.clone()
    } else {
        CharSet::new()
    };

    let new_charset = CharSet::from_string(s).union(&base_charset);
    Ok(Value::CharSet(Arc::new(new_charset)))
}

fn primitive_char_set_filter(args: &[Value]) -> Result<Value> {
    if args.len() < 2 || args.len() > 3 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("char-set-filter expects 2 or 3 arguments, got {}", args.len()),
            None,
        )));
    }

    // For now, we'll implement a simple version that doesn't support procedure filtering
    // This would require the evaluator context to call the predicate function
    Err(Box::new(DiagnosticError::runtime_error(
        "char-set-filter with procedure predicates not yet implemented".to_string(),
        None,
    )))
}

fn primitive_ucs_range_to_char_set(args: &[Value]) -> Result<Value> {
    if args.len() < 2 || args.len() > 4 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("ucs-range->char-set expects 2-4 arguments, got {}", args.len()),
            None,
        )));
    }

    let start = args[0].as_integer().ok_or_else(|| {
        Box::new(DiagnosticError::runtime_error("Expected integer for start".to_string(), None))
    })? as u32;

    let end = args[1].as_integer().ok_or_else(|| {
        Box::new(DiagnosticError::runtime_error("Expected integer for end".to_string(), None))
    })? as u32;

    let error_on_invalid = if args.len() >= 3 {
        args[2].is_truthy()
    } else {
        false
    };

    let base_charset = if args.len() == 4 {
        get_charset(&args[3])?.clone()
    } else {
        CharSet::new()
    };

    if start > end {
        return Ok(Value::CharSet(Arc::new(base_charset)));
    }

    let chars: Result<Vec<char>> = (start..end)
        .map(|code| {
            char::from_u32(code).ok_or_else(|| {
                if error_on_invalid {
                    Box::new(DiagnosticError::runtime_error(
                        format!("Invalid Unicode code point: {code}"),
                        None,
                    ))
                } else {
                    Box::new(DiagnosticError::runtime_error("Invalid Unicode code point".to_string(), None))
                }
            })
        })
        .collect();

    let chars = chars?;
    let new_charset = CharSet::from_chars(chars).union(&base_charset);
    Ok(Value::CharSet(Arc::new(new_charset)))
}

// Operations

fn primitive_char_set_size(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("char-set-size expects 1 argument, got {}", args.len()),
            None,
        )));
    }

    let charset = get_charset(&args[0])?;
    Ok(Value::integer(charset.size() as i64))
}

fn primitive_char_set_count(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("char-set-count expects 2 arguments, got {}", args.len()),
            None,
        )));
    }

    // For now, we'll implement a simple version that doesn't support procedure filtering
    // This would require the evaluator context to call the predicate function
    Err(Box::new(DiagnosticError::runtime_error(
        "char-set-count with procedure predicates not yet implemented".to_string(),
        None,
    )))
}

fn primitive_char_set_union(args: &[Value]) -> Result<Value> {
    if args.is_empty() {
        return Ok(Value::CharSet(Arc::new(CharSet::new())));
    }

    let mut result = get_charset(&args[0])?.clone();
    for arg in &args[1..] {
        let charset = get_charset(arg)?;
        result = result.union(charset);
    }

    Ok(Value::CharSet(Arc::new(result)))
}

fn primitive_char_set_intersection(args: &[Value]) -> Result<Value> {
    if args.is_empty() {
        return Ok(Value::CharSet(Arc::new(StandardCharSets::full())));
    }

    let mut result = get_charset(&args[0])?.clone();
    for arg in &args[1..] {
        let charset = get_charset(arg)?;
        result = result.intersection(charset);
    }

    Ok(Value::CharSet(Arc::new(result)))
}

fn primitive_char_set_difference(args: &[Value]) -> Result<Value> {
    if args.is_empty() {
        return Err(Box::new(DiagnosticError::runtime_error(
            "char-set-difference requires at least 1 argument".to_string(),
            None,
        )));
    }

    let mut result = get_charset(&args[0])?.clone();
    for arg in &args[1..] {
        let charset = get_charset(arg)?;
        result = result.difference(charset);
    }

    Ok(Value::CharSet(Arc::new(result)))
}

fn primitive_char_set_complement(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("char-set-complement expects 1 argument, got {}", args.len()),
            None,
        )));
    }

    let charset = get_charset(&args[0])?;
    let complement = charset.complement();
    Ok(Value::CharSet(Arc::new(complement)))
}

fn primitive_char_set_xor(args: &[Value]) -> Result<Value> {
    if args.is_empty() {
        return Ok(Value::CharSet(Arc::new(CharSet::new())));
    }

    let mut result = get_charset(&args[0])?.clone();
    for arg in &args[1..] {
        let charset = get_charset(arg)?;
        result = result.symmetric_difference(charset);
    }

    Ok(Value::CharSet(Arc::new(result)))
}

// Conversions

fn primitive_char_set_to_list(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("char-set->list expects 1 argument, got {}", args.len()),
            None,
        )));
    }

    let charset = get_charset(&args[0])?;
    let chars: Vec<Value> = charset.iter()
        .map(|&c| Value::Literal(Literal::Character(c)))
        .collect();

    Ok(Value::list(chars))
}

fn primitive_char_set_to_string(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("char-set->string expects 1 argument, got {}", args.len()),
            None,
        )));
    }

    let charset = get_charset(&args[0])?;
    let s: String = charset.iter().collect();
    Ok(Value::string(s))
}

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

    #[test]
    fn test_charset_creation() {
        let empty = CharSet::new();
        assert!(empty.is_empty());
        assert_eq!(empty.size(), 0);

        let abc = CharSet::from_string("abc");
        assert!(!abc.is_empty());
        assert_eq!(abc.size(), 3);
        assert!(abc.contains('a'));
        assert!(abc.contains('b'));
        assert!(abc.contains('c'));
        assert!(!abc.contains('d'));
    }

    #[test]
    fn test_charset_operations() {
        let ab = CharSet::from_string("ab");
        let bc = CharSet::from_string("bc");

        let union = ab.union(&bc);
        assert_eq!(union.size(), 3);
        assert!(union.contains('a'));
        assert!(union.contains('b'));
        assert!(union.contains('c'));

        let intersection = ab.intersection(&bc);
        assert_eq!(intersection.size(), 1);
        assert!(intersection.contains('b'));

        let difference = ab.difference(&bc);
        assert_eq!(difference.size(), 1);
        assert!(difference.contains('a'));
    }

    #[test]
    fn test_standard_charsets() {
        let digits = StandardCharSets::digit();
        assert_eq!(digits.size(), 10);
        assert!(digits.contains('0'));
        assert!(digits.contains('9'));
        assert!(!digits.contains('a'));

        let lower = StandardCharSets::lower_case();
        assert!(lower.contains('a'));
        assert!(lower.contains('z'));
        assert!(!lower.contains('A'));

        let upper = StandardCharSets::upper_case();
        assert!(upper.contains('A'));
        assert!(upper.contains('Z'));
        assert!(!upper.contains('a'));
    }

    #[test]
    fn test_charset_display() {
        let empty = CharSet::new();
        assert_eq!(format!("{}", empty), "#<char-set empty>");

        let abc = CharSet::from_string("abc");
        let display = format!("{}", abc);
        assert!(display.contains("size=3"));
        assert!(display.contains("{a b c}"));
    }
}