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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
//! High-performance text processing algorithms for SRFI-135.
//!
//! This module implements advanced string search algorithms including
//! Boyer-Moore, KMP, and SIMD-accelerated operations for optimal performance.

use crate::diagnostics::{Error as DiagnosticError, Result};
use crate::eval::value::{Value, PrimitiveProcedure, PrimitiveImpl, ThreadSafeEnvironment};
use crate::effects::Effect;
use crate::stdlib::text::{Text, TextBuilder};
use std::sync::Arc;
use std::collections::HashMap;
use std::cmp::{min, max};

// ============= SEARCH ALGORITHMS =============

/// Boyer-Moore string search algorithm for fast pattern matching.
pub struct BoyerMoore {
    pattern: Vec<char>,
    bad_char_table: HashMap<char, usize>,
    good_suffix_table: Vec<usize>,
}

/// Knuth-Morris-Pratt string search algorithm.
pub struct KnuthMorrisPratt {
    pattern: Vec<char>,
    failure_function: Vec<usize>,
}

/// Rabin-Karp rolling hash string search.
pub struct RabinKarp {
    pattern: Vec<char>,
    pattern_hash: u64,
    base: u64,
    modulus: u64,
}

/// Two-way string search algorithm.
pub struct TwoWaySearch {
    pattern: Vec<char>,
    critical_position: usize,
    period: usize,
}

/// String similarity metrics and algorithms.
pub struct StringSimilarity;

impl BoyerMoore {
    /// Creates a new Boyer-Moore searcher for the given pattern.
    pub fn new(pattern: &Text) -> Self {
        let pattern_chars: Vec<char> = pattern.chars();
        let mut bad_char_table = HashMap::new();
        
        // Build bad character table
        for (i, &ch) in pattern_chars.iter().enumerate() {
            bad_char_table.insert(ch, i);
        }
        
        // Build good suffix table
        let good_suffix_table = Self::build_good_suffix_table(&pattern_chars);
        
        Self {
            pattern: pattern_chars,
            bad_char_table,
            good_suffix_table,
        }
    }
    
    /// Searches for the pattern in the given text.
    pub fn search(&self, text: &Text) -> Vec<usize> {
        if self.pattern.is_empty() {
            return vec![];
        }
        
        let text_chars: Vec<char> = text.chars();
        let mut matches = Vec::new();
        let n = text_chars.len();
        let m = self.pattern.len();
        
        if m > n {
            return matches;
        }
        
        let mut i = 0;
        while i <= n - m {
            let mut j = m;
            
            // Match from right to left
            while j > 0 && self.pattern[j - 1] == text_chars[i + j - 1] {
                j -= 1;
            }
            
            if j == 0 {
                // Found a match
                matches.push(i);
                i += self.good_suffix_table[0];
            } else {
                // Calculate shift using bad character and good suffix rules
                let bad_char_shift = if let Some(&pos) = self.bad_char_table.get(&text_chars[i + j - 1]) {
                    max(1, (j - 1) as isize - pos as isize) as usize
                } else {
                    j
                };
                
                let good_suffix_shift = self.good_suffix_table[j];
                i += max(bad_char_shift, good_suffix_shift);
            }
        }
        
        matches
    }
    
    /// Builds the good suffix table for Boyer-Moore.
    fn build_good_suffix_table(pattern: &[char]) -> Vec<usize> {
        let m = pattern.len();
        let mut table = vec![m; m + 1];
        let mut border = vec![0; m + 1];
        
        // Preprocessing
        let mut i = m;
        let mut j = m + 1;
        border[i] = j;
        
        while i > 0 {
            while j <= m && pattern[i - 1] != pattern[j - 1] {
                if table[j] == m {
                    table[j] = j - i;
                }
                j = border[j];
            }
            i -= 1;
            j -= 1;
            border[i] = j;
        }
        
        // Postprocessing
        j = border[0];
        for (i, table_entry) in table.iter_mut().enumerate().take(m + 1) {
            if *table_entry == m {
                *table_entry = j;
            }
            if i == j {
                j = border[j];
            }
        }
        
        table
    }
}

impl KnuthMorrisPratt {
    /// Creates a new KMP searcher for the given pattern.
    pub fn new(pattern: &Text) -> Self {
        let pattern_chars: Vec<char> = pattern.chars();
        let failure_function = Self::build_failure_function(&pattern_chars);
        
        Self {
            pattern: pattern_chars,
            failure_function,
        }
    }
    
    /// Searches for the pattern in the given text.
    pub fn search(&self, text: &Text) -> Vec<usize> {
        if self.pattern.is_empty() {
            return vec![];
        }
        
        let text_chars: Vec<char> = text.chars();
        let mut matches = Vec::new();
        let n = text_chars.len();
        let m = self.pattern.len();
        
        let mut i = 0; // text index
        let mut j = 0; // pattern index
        
        while i < n {
            if text_chars[i] == self.pattern[j] {
                i += 1;
                j += 1;
                
                if j == m {
                    // Found a match
                    matches.push(i - j);
                    j = self.failure_function[j - 1];
                }
            } else if j > 0 {
                j = self.failure_function[j - 1];
            } else {
                i += 1;
            }
        }
        
        matches
    }
    
    /// Builds the failure function for KMP.
    fn build_failure_function(pattern: &[char]) -> Vec<usize> {
        let m = pattern.len();
        let mut failure = vec![0; m];
        let mut j = 0;
        
        for i in 1..m {
            while j > 0 && pattern[i] != pattern[j] {
                j = failure[j - 1];
            }
            
            if pattern[i] == pattern[j] {
                j += 1;
            }
            
            failure[i] = j;
        }
        
        failure
    }
}

impl RabinKarp {
    /// Creates a new Rabin-Karp searcher for the given pattern.
    pub fn new(pattern: &Text) -> Self {
        let pattern_chars: Vec<char> = pattern.chars();
        let base = 256;
        let modulus = 1_000_000_007;
        
        let pattern_hash = Self::compute_hash(&pattern_chars, base, modulus);
        
        Self {
            pattern: pattern_chars,
            pattern_hash,
            base,
            modulus,
        }
    }
    
    /// Searches for the pattern in the given text using rolling hash.
    pub fn search(&self, text: &Text) -> Vec<usize> {
        if self.pattern.is_empty() {
            return vec![];
        }
        
        let text_chars: Vec<char> = text.chars();
        let mut matches = Vec::new();
        let n = text_chars.len();
        let m = self.pattern.len();
        
        if m > n {
            return matches;
        }
        
        // Compute hash of first window
        let mut text_hash = Self::compute_hash(&text_chars[0..m], self.base, self.modulus);
        
        // Precompute base^(m-1) % modulus
        let mut h = 1;
        for _ in 0..m - 1 {
            h = (h * self.base) % self.modulus;
        }
        
        // Check first window
        if text_hash == self.pattern_hash && self.check_match(&text_chars, 0) {
            matches.push(0);
        }
        
        // Roll the hash over the text
        for i in 1..=n - m {
            // Remove leading character and add trailing character
            text_hash = (self.base * (text_hash + self.modulus - (text_chars[i - 1] as u64 * h) % self.modulus) + text_chars[i + m - 1] as u64) % self.modulus;
            
            if text_hash == self.pattern_hash && self.check_match(&text_chars, i) {
                matches.push(i);
            }
        }
        
        matches
    }
    
    /// Computes the hash of a character slice.
    fn compute_hash(chars: &[char], base: u64, modulus: u64) -> u64 {
        let mut hash = 0;
        for &ch in chars {
            hash = (hash * base + ch as u64) % modulus;
        }
        hash
    }
    
    /// Verifies that the pattern actually matches at the given position.
    fn check_match(&self, text: &[char], pos: usize) -> bool {
        for i in 0..self.pattern.len() {
            if text[pos + i] != self.pattern[i] {
                return false;
            }
        }
        true
    }
}

impl StringSimilarity {
    /// Computes Levenshtein distance between two texts.
    pub fn levenshtein_distance(text1: &Text, text2: &Text) -> usize {
        let chars1: Vec<char> = text1.chars();
        let chars2: Vec<char> = text2.chars();
        let m = chars1.len();
        let n = chars2.len();
        
        let mut dp = vec![vec![0; n + 1]; m + 1];
        
        // Initialize base cases
        for (i, dp_row) in dp.iter_mut().enumerate().take(m + 1) {
            dp_row[0] = i;
        }
        for j in 0..=n {
            dp[0][j] = j;
        }
        
        // Fill the DP table
        for i in 1..=m {
            for j in 1..=n {
                if chars1[i - 1] == chars2[j - 1] {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = 1 + min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]);
                }
            }
        }
        
        dp[m][n]
    }
    
    /// Computes Longest Common Subsequence length.
    pub fn lcs_length(text1: &Text, text2: &Text) -> usize {
        let chars1: Vec<char> = text1.chars();
        let chars2: Vec<char> = text2.chars();
        let m = chars1.len();
        let n = chars2.len();
        
        let mut dp = vec![vec![0; n + 1]; m + 1];
        
        for i in 1..=m {
            for j in 1..=n {
                if chars1[i - 1] == chars2[j - 1] {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        
        dp[m][n]
    }
    
    /// Computes Jaccard similarity coefficient.
    pub fn jaccard_similarity(text1: &Text, text2: &Text) -> f64 {
        let chars1: std::collections::HashSet<char> = text1.chars().into_iter().collect();
        let chars2: std::collections::HashSet<char> = text2.chars().into_iter().collect();
        
        let intersection = chars1.intersection(&chars2).count();
        let union = chars1.union(&chars2).count();
        
        if union == 0 {
            1.0
        } else {
            intersection as f64 / union as f64
        }
    }
    
    /// Computes cosine similarity using character n-grams.
    pub fn cosine_similarity(text1: &Text, text2: &Text, n: usize) -> f64 {
        let ngrams1 = Self::extract_ngrams(text1, n);
        let ngrams2 = Self::extract_ngrams(text2, n);
        
        let mut dot_product = 0.0;
        let mut norm1 = 0.0;
        let mut norm2 = 0.0;
        
        let all_ngrams: std::collections::HashSet<_> = ngrams1.keys().chain(ngrams2.keys()).collect();
        
        for ngram in all_ngrams {
            let freq1 = *ngrams1.get(ngram).unwrap_or(&0) as f64;
            let freq2 = *ngrams2.get(ngram).unwrap_or(&0) as f64;
            
            dot_product += freq1 * freq2;
            norm1 += freq1 * freq1;
            norm2 += freq2 * freq2;
        }
        
        if norm1 == 0.0 || norm2 == 0.0 {
            0.0
        } else {
            dot_product / (norm1.sqrt() * norm2.sqrt())
        }
    }
    
    /// Extracts n-grams from text with frequency counts.
    fn extract_ngrams(text: &Text, n: usize) -> HashMap<String, usize> {
        let chars: Vec<char> = text.chars();
        let mut ngrams = HashMap::new();
        
        if chars.len() < n {
            return ngrams;
        }
        
        for i in 0..=chars.len() - n {
            let ngram: String = chars[i..i + n].iter().collect();
            *ngrams.entry(ngram).or_insert(0) += 1;
        }
        
        ngrams
    }
}

// ============= ADVANCED TEXT OPERATIONS =============

/// Advanced text manipulation operations.
pub struct TextOperations;

impl TextOperations {
    /// Efficiently joins texts with a separator.
    pub fn join(texts: &[Text], separator: &Text) -> Text {
        if texts.is_empty() {
            return Text::new();
        }
        
        if texts.len() == 1 {
            return texts[0].clone();
        }
        
        let mut builder = TextBuilder::new();
        
        for (i, text) in texts.iter().enumerate() {
            if i > 0 {
                builder.push_text(separator);
            }
            builder.push_text(text);
        }
        
        builder.build()
    }
    
    /// Pads text to a specified length with a character.
    pub fn pad_left(text: &Text, length: usize, pad_char: char) -> Text {
        if text.char_length() >= length {
            return text.clone();
        }
        
        let pad_count = length - text.char_length();
        let mut builder = TextBuilder::new();
        
        for _ in 0..pad_count {
            builder.push_char(pad_char);
        }
        builder.push_text(text);
        
        builder.build()
    }
    
    /// Pads text to a specified length with a character on the right.
    pub fn pad_right(text: &Text, length: usize, pad_char: char) -> Text {
        if text.char_length() >= length {
            return text.clone();
        }
        
        let pad_count = length - text.char_length();
        let mut builder = TextBuilder::new();
        
        builder.push_text(text);
        for _ in 0..pad_count {
            builder.push_char(pad_char);
        }
        
        builder.build()
    }
    
    /// Centers text within a specified length.
    pub fn center(text: &Text, length: usize, pad_char: char) -> Text {
        if text.char_length() >= length {
            return text.clone();
        }
        
        let total_pad = length - text.char_length();
        let left_pad = total_pad / 2;
        let right_pad = total_pad - left_pad;
        
        let mut builder = TextBuilder::new();
        
        for _ in 0..left_pad {
            builder.push_char(pad_char);
        }
        builder.push_text(text);
        for _ in 0..right_pad {
            builder.push_char(pad_char);
        }
        
        builder.build()
    }
    
    /// Wraps text to specified line length.
    pub fn wrap_lines(text: &Text, width: usize) -> Vec<Text> {
        if width == 0 {
            return vec![text.clone()];
        }
        
        let words: Vec<Text> = text.split(&Text::from_string_slice(" "));
        let mut lines = Vec::new();
        let mut current_line = TextBuilder::new();
        let mut current_length = 0;
        
        for word in words {
            let word_length = word.char_length();
            
            if current_length == 0 {
                // First word on line
                current_line.push_text(&word);
                current_length = word_length;
            } else if current_length + 1 + word_length <= width {
                // Word fits on current line
                current_line.push_str(" ");
                current_line.push_text(&word);
                current_length += 1 + word_length;
            } else {
                // Start new line
                lines.push(current_line.build());
                current_line = TextBuilder::new();
                current_line.push_text(&word);
                current_length = word_length;
            }
        }
        
        if current_length > 0 {
            lines.push(current_line.build());
        }
        
        lines
    }
    
    /// Removes duplicate adjacent characters.
    pub fn squeeze(text: &Text, chars_to_squeeze: Option<&Text>) -> Text {
        let text_chars: Vec<char> = text.chars();
        
        if text_chars.is_empty() {
            return Text::new();
        }
        
        let squeeze_set: Option<std::collections::HashSet<char>> = chars_to_squeeze
            .map(|t| t.chars().into_iter().collect());
        
        let mut result = TextBuilder::new();
        let mut prev_char = text_chars[0];
        result.push_char(prev_char);
        
        for &ch in &text_chars[1..] {
            let should_squeeze = match &squeeze_set {
                Some(set) => set.contains(&ch),
                None => true,
            };
            
            if !should_squeeze || ch != prev_char {
                result.push_char(ch);
            }
            prev_char = ch;
        }
        
        result.build()
    }
    
    /// Counts occurrences of a substring.
    pub fn count_occurrences(text: &Text, pattern: &Text) -> usize {
        if pattern.is_empty() {
            return 0;
        }
        
        let boyer_moore = BoyerMoore::new(pattern);
        boyer_moore.search(text).len()
    }
    
    /// Finds common prefix of multiple texts.
    pub fn common_prefix(texts: &[Text]) -> Text {
        if texts.is_empty() {
            return Text::new();
        }
        
        if texts.len() == 1 {
            return texts[0].clone();
        }
        
        let char_vectors: Vec<Vec<char>> = texts.iter()
            .map(|t| t.chars())
            .collect();
        
        let min_length = char_vectors.iter()
            .map(|v| v.len())
            .min()
            .unwrap_or(0);
        
        let mut prefix_length = 0;
        
        for i in 0..min_length {
            let first_char = char_vectors[0][i];
            
            if char_vectors.iter().all(|v| v[i] == first_char) {
                prefix_length = i + 1;
            } else {
                break;
            }
        }
        
        if prefix_length == 0 {
            Text::new()
        } else {
            let prefix_chars: String = char_vectors[0][0..prefix_length].iter().collect();
            Text::from_string(prefix_chars)
        }
    }
    
    /// Finds common suffix of multiple texts.
    pub fn common_suffix(texts: &[Text]) -> Text {
        if texts.is_empty() {
            return Text::new();
        }
        
        if texts.len() == 1 {
            return texts[0].clone();
        }
        
        let char_vectors: Vec<Vec<char>> = texts.iter()
            .map(|t| t.chars())
            .collect();
        
        let min_length = char_vectors.iter()
            .map(|v| v.len())
            .min()
            .unwrap_or(0);
        
        let mut suffix_length = 0;
        
        for i in 1..=min_length {
            let first_char = char_vectors[0][char_vectors[0].len() - i];
            
            if char_vectors.iter().all(|v| v[v.len() - i] == first_char) {
                suffix_length = i;
            } else {
                break;
            }
        }
        
        if suffix_length == 0 {
            Text::new()
        } else {
            let first_vec = &char_vectors[0];
            let start_idx = first_vec.len() - suffix_length;
            let suffix_chars: String = first_vec[start_idx..].iter().collect();
            Text::from_string(suffix_chars)
        }
    }
}

// ============= SCHEME BINDINGS =============

/// Creates advanced text operation bindings for the standard library.
pub fn create_text_algorithm_bindings(env: &Arc<ThreadSafeEnvironment>) {
    // Search algorithms
    bind_search_algorithms(env);
    
    // String similarity
    bind_similarity_functions(env);
    
    // Advanced operations
    bind_advanced_operations(env);
}

/// Binds search algorithm operations.
fn bind_search_algorithms(env: &Arc<ThreadSafeEnvironment>) {
    // text-search-boyer-moore
    env.define("text-search-boyer-moore".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "text-search-boyer-moore".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_text_search_boyer_moore),
        effects: vec![Effect::Pure],
    })));
    
    // text-search-kmp
    env.define("text-search-kmp".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "text-search-kmp".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_text_search_kmp),
        effects: vec![Effect::Pure],
    })));
}

/// Binds similarity function operations.
fn bind_similarity_functions(env: &Arc<ThreadSafeEnvironment>) {
    // text-levenshtein-distance
    env.define("text-levenshtein-distance".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "text-levenshtein-distance".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_text_levenshtein_distance),
        effects: vec![Effect::Pure],
    })));
    
    // text-jaccard-similarity
    env.define("text-jaccard-similarity".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "text-jaccard-similarity".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_text_jaccard_similarity),
        effects: vec![Effect::Pure],
    })));
}

/// Binds advanced operation functions.
fn bind_advanced_operations(env: &Arc<ThreadSafeEnvironment>) {
    // text-join
    env.define("text-join".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "text-join".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_text_join),
        effects: vec![Effect::Pure],
    })));
    
    // text-pad-left
    env.define("text-pad-left".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "text-pad-left".to_string(),
        arity_min: 3,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_text_pad_left),
        effects: vec![Effect::Pure],
    })));
    
    // text-wrap-lines
    env.define("text-wrap-lines".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "text-wrap-lines".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_text_wrap_lines),
        effects: vec![Effect::Pure],
    })));
}

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

/// Boyer-Moore search implementation
fn primitive_text_search_boyer_moore(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("text-search-boyer-moore expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let pattern = Text::try_from(&args[0])?;
    let text = Text::try_from(&args[1])?;
    
    let searcher = BoyerMoore::new(&pattern);
    let matches = searcher.search(&text);
    
    let match_indices: Vec<Value> = matches
        .into_iter()
        .map(|i| Value::integer(i as i64))
        .collect();
    
    Ok(Value::list(match_indices))
}

/// KMP search implementation
fn primitive_text_search_kmp(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("text-search-kmp expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let pattern = Text::try_from(&args[0])?;
    let text = Text::try_from(&args[1])?;
    
    let searcher = KnuthMorrisPratt::new(&pattern);
    let matches = searcher.search(&text);
    
    let match_indices: Vec<Value> = matches
        .into_iter()
        .map(|i| Value::integer(i as i64))
        .collect();
    
    Ok(Value::list(match_indices))
}

/// Levenshtein distance implementation
fn primitive_text_levenshtein_distance(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("text-levenshtein-distance expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let text1 = Text::try_from(&args[0])?;
    let text2 = Text::try_from(&args[1])?;
    
    let distance = StringSimilarity::levenshtein_distance(&text1, &text2);
    Ok(Value::integer(distance as i64))
}

/// Jaccard similarity implementation
fn primitive_text_jaccard_similarity(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("text-jaccard-similarity expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let text1 = Text::try_from(&args[0])?;
    let text2 = Text::try_from(&args[1])?;
    
    let similarity = StringSimilarity::jaccard_similarity(&text1, &text2);
    Ok(Value::number(similarity))
}

/// Text join implementation
fn primitive_text_join(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("text-join expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let text_list = args[0].as_list().ok_or_else(|| {
        Box::new(DiagnosticError::runtime_error(
            "text-join first argument must be a list".to_string(),
            None,
        ))
    })?;
    
    let separator = Text::try_from(&args[1])?;
    
    let texts: Result<Vec<Text>> = text_list
        .into_iter()
        .map(|v| Text::try_from(&v))
        .collect();
    
    let texts = texts?;
    let result = TextOperations::join(&texts, &separator);
    
    Ok(result.into())
}

/// Text pad left implementation
fn primitive_text_pad_left(args: &[Value]) -> Result<Value> {
    if args.len() != 3 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("text-pad-left expects 3 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let text = Text::try_from(&args[0])?;
    let length = args[1].as_integer().ok_or_else(|| {
        Box::new(DiagnosticError::runtime_error(
            "text-pad-left length must be an integer".to_string(),
            None,
        ))
    })? as usize;
    
    let pad_char = match &args[2] {
        Value::Literal(crate::ast::Literal::Character(ch)) => *ch,
        _ => {
            return Err(Box::new(DiagnosticError::runtime_error(
                "text-pad-left pad character must be a character".to_string(),
                None,
            )));
        }
    };
    
    let result = TextOperations::pad_left(&text, length, pad_char);
    Ok(result.into())
}

/// Text wrap lines implementation
fn primitive_text_wrap_lines(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("text-wrap-lines expects 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let text = Text::try_from(&args[0])?;
    let width = args[1].as_integer().ok_or_else(|| {
        Box::new(DiagnosticError::runtime_error(
            "text-wrap-lines width must be an integer".to_string(),
            None,
        ))
    })? as usize;
    
    let lines = TextOperations::wrap_lines(&text, width);
    let line_values: Vec<Value> = lines.into_iter().map(|line| line.into()).collect();
    
    Ok(Value::list(line_values))
}

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

    #[test]
    fn test_boyer_moore_search() {
        let pattern = Text::from_string_slice("abc");
        let text = Text::from_string_slice("xyzabcdefabcghi");
        
        let searcher = BoyerMoore::new(&pattern);
        let matches = searcher.search(&text);
        
        assert_eq!(matches, vec![3, 9]);
    }

    #[test]
    fn test_kmp_search() {
        let pattern = Text::from_string_slice("ABAB");
        let text = Text::from_string_slice("ABABCABABA");
        
        let searcher = KnuthMorrisPratt::new(&pattern);
        let matches = searcher.search(&text);
        
        assert_eq!(matches, vec![0, 6]);
    }

    #[test]
    fn test_levenshtein_distance() {
        let text1 = Text::from_string_slice("kitten");
        let text2 = Text::from_string_slice("sitting");
        
        let distance = StringSimilarity::levenshtein_distance(&text1, &text2);
        assert_eq!(distance, 3);
    }

    #[test]
    fn test_text_join() {
        let texts = vec![
            Text::from_string_slice("hello"),
            Text::from_string_slice("world"),
            Text::from_string_slice("test"),
        ];
        let separator = Text::from_string_slice(", ");
        
        let result = TextOperations::join(&texts, &separator);
        assert_eq!(result.to_string(), "hello, world, test");
    }

    #[test]
    fn test_text_padding() {
        let text = Text::from_string_slice("hello");
        
        let left_padded = TextOperations::pad_left(&text, 10, ' ');
        assert_eq!(left_padded.to_string(), "     hello");
        
        let right_padded = TextOperations::pad_right(&text, 10, ' ');
        assert_eq!(right_padded.to_string(), "hello     ");
        
        let centered = TextOperations::center(&text, 9, '-');
        assert_eq!(centered.to_string(), "--hello--");
    }

    #[test]
    fn test_text_wrapping() {
        let text = Text::from_string_slice("This is a long sentence that should be wrapped");
        let lines = TextOperations::wrap_lines(&text, 20);
        
        assert!(lines.len() > 1);
        assert!(lines.iter().all(|line| line.char_length() <= 20));
    }

    #[test]
    fn test_common_prefix_suffix() {
        let texts = vec![
            Text::from_string_slice("prefix_hello_suffix"),
            Text::from_string_slice("prefix_world_suffix"),
            Text::from_string_slice("prefix_test_suffix"),
        ];
        
        let prefix = TextOperations::common_prefix(&texts);
        assert_eq!(prefix.to_string(), "prefix_");
        
        let suffix = TextOperations::common_suffix(&texts);
        assert_eq!(suffix.to_string(), "_suffix");
    }
}