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
//! Literal value types for the Lambdust AST.

use serde::{Deserialize, Serialize};
use std::fmt;
use std::hash::{Hash, Hasher};

/// Literal values in the Lambdust language.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Literal {
    /// Exact integer numbers (preserves exactness)
    ExactInteger(i64),
    
    /// Inexact floating-point numbers
    InexactReal(f64),
    
    /// Legacy number representation for backward compatibility
    #[deprecated(note = "Use ExactInteger or InexactReal instead")]
    Number(f64),
    
    /// Rational numbers (exact fractions)
    Rational { numerator: i64, denominator: i64 },
    
    /// Complex numbers (can be exact or inexact depending on components)
    Complex { real: f64, imaginary: f64 },
    
    /// String literals
    String(String),
    
    /// Character literals
    Character(char),
    
    /// Boolean values
    Boolean(bool),
    
    /// Bytevector literals
    Bytevector(Vec<u8>),
    
    /// The empty list (nil)
    Nil,
    
    /// Unspecified value (result of side-effecting operations)
    Unspecified,
}

impl Literal {
    /// Creates an exact integer literal.
    pub fn integer(value: i64) -> Self {
        Self::ExactInteger(value)
    }

    /// Creates an inexact real literal.
    pub fn float(value: f64) -> Self {
        Self::InexactReal(value)
    }
    
    /// Creates a number literal - chooses exact or inexact based on value
    pub fn number(value: f64) -> Self {
        if value.fract() == 0.0 && value.is_finite() && value.abs() <= i64::MAX as f64 {
            Self::ExactInteger(value as i64)
        } else {
            Self::InexactReal(value)
        }
    }

    /// Creates a rational literal.
    pub fn rational(numerator: i64, denominator: i64) -> Self {
        if denominator == 0 {
            panic!("Rational number cannot have zero denominator");
        }
        
        // Normalize the rational number
        let gcd = gcd(numerator.unsigned_abs(), denominator.unsigned_abs()) as i64;
        let num = numerator / gcd;
        let den = denominator / gcd;
        
        // Ensure denominator is positive
        if den < 0 {
            Self::Rational { numerator: -num, denominator: -den }
        } else {
            Self::Rational { numerator: num, denominator: den }
        }
    }

    /// Creates a complex literal.
    pub fn complex(real: f64, imaginary: f64) -> Self {
        Self::Complex { real, imaginary }
    }

    /// Creates a string literal.
    pub fn string(value: impl Into<String>) -> Self {
        Self::String(value.into())
    }

    /// Creates a character literal.
    pub fn character(value: char) -> Self {
        Self::Character(value)
    }

    /// Creates a boolean literal.
    pub fn boolean(value: bool) -> Self {
        Self::Boolean(value)
    }

    /// Creates a bytevector literal.
    pub fn bytevector(value: Vec<u8>) -> Self {
        Self::Bytevector(value)
    }

    /// Returns true if this literal is a number.
    pub fn is_number(&self) -> bool {
        matches!(self, 
            Literal::ExactInteger(_) | 
            Literal::InexactReal(_) |
            Literal::Number(_) |
            Literal::Rational { .. } | 
            Literal::Complex { .. }
        )
    }

    /// Returns true if this literal is exact (integer or rational).
    pub fn is_exact(&self) -> bool {
        match self {
            Literal::ExactInteger(_) | Literal::Rational { .. } => true,
            Literal::Number(n) => n.fract() == 0.0 && n.is_finite(),
            Literal::Complex { real, imaginary } => {
                // Complex is exact only if both parts are exact (represent as rationals)
                real.fract() == 0.0 && imaginary.fract() == 0.0
            }
            _ => false,
        }
    }

    /// Returns true if this literal is inexact (floating point).
    pub fn is_inexact(&self) -> bool {
        match self {
            Literal::InexactReal(_) => true,
            Literal::Number(n) => n.fract() != 0.0 || !n.is_finite(),
            Literal::Complex { real, imaginary } => {
                // Complex is inexact if any part is inexact
                real.fract() != 0.0 || imaginary.fract() != 0.0 || !real.is_finite() || !imaginary.is_finite()
            }
            _ => false,
        }
    }

    /// Returns true if this literal is real (not complex).
    pub fn is_real(&self) -> bool {
        matches!(self, 
            Literal::ExactInteger(_) |
            Literal::InexactReal(_) | 
            Literal::Number(_) |
            Literal::Rational { .. }
        ) || matches!(self, Literal::Complex { imaginary, .. } if *imaginary == 0.0)
    }

    /// Returns true if this literal is an integer.
    pub fn is_integer(&self) -> bool {
        match self {
            Literal::ExactInteger(_) => true,
            Literal::InexactReal(n) => n.fract() == 0.0 && n.is_finite(),
            Literal::Number(n) => n.fract() == 0.0 && n.is_finite(),
            Literal::Rational { denominator, .. } => *denominator == 1,
            Literal::Complex { real, imaginary } => {
                *imaginary == 0.0 && real.fract() == 0.0 && real.is_finite()
            }
            Literal::String(_) | Literal::Character(_) | Literal::Boolean(_) 
            | Literal::Bytevector(_) | Literal::Nil | Literal::Unspecified => false,
        }
    }

    /// Converts this literal to a floating-point number if possible.
    pub fn to_f64(&self) -> Option<f64> {
        match self {
            Literal::ExactInteger(n) => Some(*n as f64),
            Literal::InexactReal(n) => Some(*n),
            Literal::Number(n) => Some(*n),
            Literal::Rational { numerator, denominator } => {
                Some(*numerator as f64 / *denominator as f64)
            }
            Literal::Complex { real, imaginary } if *imaginary == 0.0 => Some(*real),
            _ => None,
        }
    }

    /// Converts this literal to an integer if possible.
    pub fn to_i64(&self) -> Option<i64> {
        match self {
            Literal::ExactInteger(n) => Some(*n),
            Literal::InexactReal(n) if n.fract() == 0.0 && n.is_finite() => {
                let i = *n as i64;
                if i as f64 == *n { Some(i) } else { None }
            }
            Literal::Number(n) if n.fract() == 0.0 && n.is_finite() => {
                let i = *n as i64;
                if i as f64 == *n { Some(i) } else { None }
            }
            Literal::Rational { numerator, denominator } if *denominator == 1 => Some(*numerator),
            _ => None,
        }
    }

    /// Converts this literal to a non-negative usize if possible.
    pub fn to_usize(&self) -> Option<usize> {
        self.to_i64().and_then(|i| {
            if i >= 0 {
                Some(i as usize)
            } else {
                None
            }
        })
    }

    /// Returns true if this literal is truthy in Scheme semantics.
    pub fn is_truthy(&self) -> bool {
        !matches!(self, Literal::Boolean(false))
    }

    /// Returns true if this literal is falsy in Scheme semantics.
    pub fn is_falsy(&self) -> bool {
        matches!(self, Literal::Boolean(false))
    }

    /// Helper: Extract numeric value as f64 for compatibility during migration
    /// This allows existing code that pattern matches on `Number(n)` to work
    pub fn as_numeric_f64(&self) -> Option<f64> {
        match self {
            Literal::ExactInteger(i) => Some(*i as f64),
            Literal::InexactReal(f) => Some(*f),
            Literal::Number(n) => Some(*n),
            Literal::Rational { numerator, denominator } => {
                Some(*numerator as f64 / *denominator as f64)
            }
            Literal::Complex { real, imaginary } if *imaginary == 0.0 => Some(*real),
            _ => None,
        }
    }
    
    /// Helper: Check if this is any numeric literal (for pattern matching replacement)
    pub fn is_any_numeric(&self) -> bool {
        self.is_number()
    }
    
    /// Convert from legacy f64 representation (for compatibility during migration)
    #[deprecated(note = "Use integer() or float() instead")]
    pub fn from_f64(value: f64) -> Self {
        if value.fract() == 0.0 && value.is_finite() && value.abs() <= i64::MAX as f64 {
            Self::ExactInteger(value as i64)
        } else {
            Self::InexactReal(value)
        }
    }
}

impl fmt::Display for Literal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Literal::ExactInteger(n) => {
                write!(f, "{n}")
            }
            Literal::InexactReal(n) => {
                if n.is_infinite() {
                    if n.is_sign_positive() {
                        write!(f, "+inf.0")
                    } else {
                        write!(f, "-inf.0")
                    }
                } else if n.is_nan() {
                    write!(f, "+nan.0")
                } else if n.fract() == 0.0 {
                    write!(f, "{}.0", *n as i64)
                } else {
                    write!(f, "{n}")
                }
            }
            Literal::Number(n) => {
                if n.is_infinite() {
                    if n.is_sign_positive() {
                        write!(f, "+inf.0")
                    } else {
                        write!(f, "-inf.0")
                    }
                } else if n.is_nan() {
                    write!(f, "+nan.0")
                } else if n.fract() == 0.0 {
                    write!(f, "{}", *n as i64)
                } else {
                    write!(f, "{n}")
                }
            }
            Literal::Rational { numerator, denominator } => {
                if *denominator == 1 {
                    write!(f, "{numerator}")
                } else {
                    write!(f, "{numerator}/{denominator}")
                }
            }
            Literal::Complex { real, imaginary } => {
                if *real == 0.0 {
                    if *imaginary == 1.0 {
                        write!(f, "i")
                    } else if *imaginary == -1.0 {
                        write!(f, "-i")
                    } else {
                        write!(f, "{imaginary}i")
                    }
                } else if *imaginary == 0.0 {
                    write!(f, "{real}")
                } else if *imaginary > 0.0 {
                    if *imaginary == 1.0 {
                        write!(f, "{real}+i")
                    } else {
                        write!(f, "{real}+{imaginary}i")
                    }
                } else if *imaginary == -1.0 {
                    write!(f, "{real}-i")
                } else {
                    write!(f, "{real}{imaginary}i")
                }
            }
            Literal::String(s) => write!(f, "\"{}\"", escape_string(s)),
            Literal::Character(c) => {
                match c {
                    ' ' => write!(f, "#\\space"),
                    '\n' => write!(f, "#\\newline"),
                    '\t' => write!(f, "#\\tab"),
                    '\r' => write!(f, "#\\return"),
                    _ => write!(f, "#\\{c}"),
                }
            }
            Literal::Boolean(true) => write!(f, "#t"),
            Literal::Boolean(false) => write!(f, "#f"),
            Literal::Bytevector(bv) => {
                write!(f, "#u8(")?;
                for (i, byte) in bv.iter().enumerate() {
                    if i > 0 {
                        write!(f, " ")?;
                    }
                    write!(f, "{byte}")?;
                }
                write!(f, ")")
            }
            Literal::Nil => write!(f, "()"),
            Literal::Unspecified => write!(f, "#<unspecified>"),
        }
    }
}

/// Escapes special characters in a string for display.
fn escape_string(s: &str) -> String {
    s.chars()
        .map(|c| match c {
            '"' => "\\\"".to_string(),
            '\\' => "\\\\".to_string(),
            '\n' => "\\n".to_string(),
            '\t' => "\\t".to_string(),
            '\r' => "\\r".to_string(),
            c if c.is_control() => format!("\\x{:02x}", c as u8),
            c => c.to_string(),
        })
        .collect()
}

/// Computes the greatest common divisor of two numbers.
fn gcd(a: u64, b: u64) -> u64 {
    if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }
}

impl Hash for Literal {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            Literal::ExactInteger(n) => {
                0u8.hash(state);
                n.hash(state);
            }
            Literal::InexactReal(n) => {
                // For floating point numbers, we use their bit representation
                // This ensures that equal numbers have the same hash
                1u8.hash(state);
                n.to_bits().hash(state);
            }
            Literal::Number(n) => {
                // Legacy number representation - use discriminant 10 to avoid conflicts
                10u8.hash(state);
                n.to_bits().hash(state);
            }
            Literal::Rational { numerator, denominator } => {
                2u8.hash(state);
                numerator.hash(state);
                denominator.hash(state);
            }
            Literal::Complex { real, imaginary } => {
                3u8.hash(state);
                real.to_bits().hash(state);
                imaginary.to_bits().hash(state);
            }
            Literal::String(s) => {
                4u8.hash(state);
                s.hash(state);
            }
            Literal::Character(c) => {
                5u8.hash(state);
                c.hash(state);
            }
            Literal::Boolean(b) => {
                6u8.hash(state);
                b.hash(state);
            }
            Literal::Bytevector(bv) => {
                7u8.hash(state);
                bv.hash(state);
            }
            Literal::Nil => {
                8u8.hash(state);
            }
            Literal::Unspecified => {
                9u8.hash(state);
            }
        }
    }
}

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

    #[test]
    fn test_number_creation() {
        let int_lit = Literal::integer(42);
        let float_lit = Literal::float(3.05);
        
        assert!(int_lit.is_number());
        assert!(int_lit.is_integer());
        assert_eq!(int_lit.to_i64(), Some(42));
        
        assert!(float_lit.is_number());
        assert!(!float_lit.is_integer());
        assert_eq!(float_lit.to_f64(), Some(3.05));
    }

    #[test]
    fn test_rational_creation() {
        let rat = Literal::rational(3, 4);
        
        match rat {
            Literal::Rational { numerator, denominator } => {
                assert_eq!(numerator, 3);
                assert_eq!(denominator, 4);
            }
            _ => panic!("Expected rational"),
        }
        
        assert!(rat.is_exact());
        assert!(!rat.is_inexact());
        assert_eq!(rat.to_f64(), Some(0.75));
    }

    #[test]
    fn test_rational_normalization() {
        let rat = Literal::rational(6, 8);
        
        match rat {
            Literal::Rational { numerator, denominator } => {
                assert_eq!(numerator, 3);
                assert_eq!(denominator, 4);
            }
            _ => panic!("Expected rational"),
        }
    }

    #[test]
    fn test_complex_creation() {
        let complex = Literal::complex(3.0, 4.0);
        
        assert!(complex.is_number());
        assert!(!complex.is_real());
        assert!(!complex.is_integer());
    }

    #[test]
    fn test_truthiness() {
        assert!(Literal::boolean(true).is_truthy());
        assert!(!Literal::boolean(false).is_truthy());
        assert!(Literal::integer(0).is_truthy()); // 0 is truthy in Scheme
        assert!(Literal::string("").is_truthy()); // empty string is truthy
        assert!(Literal::Nil.is_truthy()); // empty list is truthy
    }

    #[test]
    fn test_display() {
        assert_eq!(format!("{}", Literal::integer(42)), "42");
        assert_eq!(format!("{}", Literal::float(3.05)), "3.05");
        assert_eq!(format!("{}", Literal::float(3.0)), "3.0");
        assert_eq!(format!("{}", Literal::rational(3, 4)), "3/4");
        assert_eq!(format!("{}", Literal::complex(3.0, 4.0)), "3+4i");
        assert_eq!(format!("{}", Literal::complex(0.0, 1.0)), "i");
        assert_eq!(format!("{}", Literal::complex(3.0, -1.0)), "3-i");
        assert_eq!(format!("{}", Literal::string("hello".to_string())), "\"hello\"");
        assert_eq!(format!("{}", Literal::character('a')), "#\\a");
        assert_eq!(format!("{}", Literal::character(' ')), "#\\space");
        assert_eq!(format!("{}", Literal::boolean(true)), "#t");
        assert_eq!(format!("{}", Literal::boolean(false)), "#f");
        assert_eq!(format!("{}", Literal::Nil), "()");
        
        // Special float values
        assert_eq!(format!("{}", Literal::InexactReal(f64::INFINITY)), "+inf.0");
        assert_eq!(format!("{}", Literal::InexactReal(f64::NEG_INFINITY)), "-inf.0");
        assert_eq!(format!("{}", Literal::InexactReal(f64::NAN)), "+nan.0");
    }

    #[test]
    fn test_string_escaping() {
        let s = "hello\n\"world\"";
        let escaped = escape_string(s);
        assert_eq!(escaped, "hello\\n\\\"world\\\"");
    }
}