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
//! Algebraic Data Types implementation for Lambdust.
//!
//! This module provides complete Sum Types and Product Types with pattern matching
//! capabilities, supporting the advanced type system needed for R7RS-large compliance.
//!
//! Features:
//! - Sum types (union types with tagged variants)
//! - Product types (tuples and records)
//! - Pattern matching with exhaustiveness checking
//! - Type constructors with parameters
//! - Recursive type definitions
//! - Integration with existing gradual type system

#![allow(missing_docs)]

use super::{Type, TypeVar, Kind, TypeScheme};
use crate::diagnostics::{Error, Result, Span};
use crate::eval::value::Value;
use std::collections::{HashMap, HashSet};
use std::fmt;

/// An algebraic data type definition.
#[derive(Debug, Clone, PartialEq)]
pub struct AlgebraicDataType {
    /// Name of the type
    pub name: String,
    /// Type parameters (for generic types)
    pub type_params: Vec<TypeVar>,
    /// Type constructors (variants for sum types, fields for product types)
    pub constructors: Vec<DataConstructor>,
    /// Kind of this type
    pub kind: Kind,
    /// Whether this is a sum type or product type
    pub variant_type: AlgebraicVariant,
    /// Span for error reporting
    pub span: Option<Span>,
}

/// Variant type for algebraic data types.
#[derive(Debug, Clone, PartialEq)]
pub enum AlgebraicVariant {
    /// Sum type (tagged union)
    Sum,
    /// Product type (record/tuple)
    Product,
    /// GADT (Generalized Algebraic Data Type)
    GADT,
}

/// A data constructor for algebraic types.
#[derive(Debug, Clone, PartialEq)]
pub struct DataConstructor {
    /// Name of the constructor
    pub name: String,
    /// Parameter types
    pub param_types: Vec<Type>,
    /// Return type (for GADTs)
    pub return_type: Option<Type>,
    /// Tag for runtime type identification
    pub tag: usize,
    /// Span for error reporting
    pub span: Option<Span>,
}

/// A pattern for pattern matching.
#[derive(Debug, Clone, PartialEq)]
pub enum Pattern {
    /// Wildcard pattern (_)
    Wildcard,
    /// Variable pattern (binds to variable)
    Variable(String),
    /// Literal pattern (matches specific value)
    Literal(crate::ast::Literal),
    /// Constructor pattern (matches constructor with sub-patterns)
    Constructor {
        name: String,
        patterns: Vec<Pattern>,
    },
    /// Tuple pattern
    Tuple(Vec<Pattern>),
    /// Record pattern
    Record {
        fields: HashMap<String, Pattern>,
        rest: Option<Box<Pattern>>,
    },
    /// Or pattern (p1 | p2)
    Or(Vec<Pattern>),
    /// Guard pattern (pattern if condition)
    Guard {
        pattern: Box<Pattern>,
        guard: String, // Simplified: store as string for now
    },
}

/// A pattern match clause.
#[derive(Debug, Clone)]
pub struct MatchClause {
    /// Pattern to match
    pub pattern: Pattern,
    /// Optional guard condition
    pub guard: Option<String>,
    /// Expression to evaluate if pattern matches
    pub body: String, // Simplified: store as string for now
    /// Span for error reporting
    pub span: Option<Span>,
}

/// Pattern match expression.
#[derive(Debug, Clone)]
pub struct MatchExpression {
    /// Expression to match against
    pub scrutinee: String, // Simplified: store as string for now
    /// Match clauses
    pub clauses: Vec<MatchClause>,
    /// Span for error reporting
    pub span: Option<Span>,
}

/// Pattern matching compiler and analyzer.
pub struct PatternMatcher {
    /// Known algebraic data types
    types: HashMap<String, AlgebraicDataType>,
    /// Pattern compilation cache
    cache: HashMap<String, CompiledPattern>,
}

/// Compiled pattern for efficient matching.
#[derive(Debug, Clone)]
pub struct CompiledPattern {
    /// Decision tree for pattern matching
    pub tree: DecisionTree,
    /// Variable bindings
    pub bindings: Vec<String>,
}

/// Decision tree for pattern matching compilation.
#[derive(Debug, Clone)]
pub enum DecisionTree {
    /// Leaf node (successful match)
    Success {
        bindings: HashMap<String, Value>,
        action: String,
    },
    /// Failure node (no match)
    Failure,
    /// Test node (conditional branch)
    Test {
        test: PatternTest,
        success: Box<DecisionTree>,
        failure: Box<DecisionTree>,
    },
    /// Switch node (multi-way branch)
    Switch {
        scrutinee: String,
        branches: HashMap<String, DecisionTree>,
        default: Option<Box<DecisionTree>>,
    },
}

/// Pattern test for decision tree.
#[derive(Debug, Clone)]
pub enum PatternTest {
    /// Constructor test
    Constructor {
        name: String,
        arity: usize,
    },
    /// Literal test
    Literal(crate::ast::Literal),
    /// Type test
    Type(Type),
    /// Guard test
    Guard(String),
}

impl AlgebraicDataType {
    /// Creates a new algebraic data type.
    pub fn new(
        name: String,
        type_params: Vec<TypeVar>,
        variant_type: AlgebraicVariant,
        span: Option<Span>,
    ) -> Self {
        Self {
            name,
            type_params,
            constructors: Vec::new(),
            kind: Kind::Type, // Will be computed based on type_params
            variant_type,
            span,
        }
    }

    /// Adds a constructor to this type.
    pub fn add_constructor(&mut self, mut constructor: DataConstructor) {
        constructor.tag = self.constructors.len();
        self.constructors.push(constructor);
    }

    /// Gets the kind of this type based on its parameters.
    pub fn compute_kind(&self) -> Kind {
        self.type_params.iter().fold(Kind::Type, |acc, _| {
            Kind::arrow(Kind::Type, acc)
        })
    }

    /// Creates a type application with the given arguments.
    pub fn apply(&self, args: Vec<Type>) -> Result<Type> {
        if args.len() != self.type_params.len() {
            return Err(Box::new(Error::type_error(
                format!(
                    "Type {} expects {} arguments, got {}",
                    self.name,
                    self.type_params.len(),
                    args.len()
                ),
                self.span.unwrap_or_default(),
            )));
        }

        // Substitute type parameters with arguments
        let mut substitution = HashMap::new();
        for (param, arg) in self.type_params.iter().zip(args.iter()) {
            substitution.insert(param.clone(), arg.clone());
        }

        Ok(Type::Constructor {
            name: self.name.clone(),
            kind: self.compute_kind(),
        })
    }

    /// Gets all constructors for this type.
    pub fn constructors(&self) -> &[DataConstructor] {
        &self.constructors
    }

    /// Gets a constructor by name.
    pub fn get_constructor(&self, name: &str) -> Option<&DataConstructor> {
        self.constructors.iter().find(|c| c.name == name)
    }

    /// Checks if this is a recursive type.
    pub fn is_recursive(&self) -> bool {
        self.constructors.iter().any(|c| {
            c.param_types.iter().any(|t| self.contains_self_reference(t))
        })
    }

    fn contains_self_reference(&self, ty: &Type) -> bool {
        match ty {
            Type::Constructor { name, .. } => name == &self.name,
            Type::Application { constructor, argument } => {
                self.contains_self_reference(constructor) || self.contains_self_reference(argument)
            }
            Type::Function { params, return_type } => {
                params.iter().any(|p| self.contains_self_reference(p))
                    || self.contains_self_reference(return_type)
            }
            _ => false,
        }
    }
}

impl DataConstructor {
    /// Creates a new data constructor.
    pub fn new(name: String, param_types: Vec<Type>, span: Option<Span>) -> Self {
        Self {
            name,
            param_types,
            return_type: None,
            tag: 0, // Will be set when added to type
            span,
        }
    }

    /// Creates a GADT constructor with explicit return type.
    pub fn gadt(
        name: String,
        param_types: Vec<Type>,
        return_type: Type,
        span: Option<Span>,
    ) -> Self {
        Self {
            name,
            param_types,
            return_type: Some(return_type),
            tag: 0,
            span,
        }
    }

    /// Gets the arity (number of parameters) of this constructor.
    pub fn arity(&self) -> usize {
        self.param_types.len()
    }

    /// Checks if this constructor takes no parameters.
    pub fn is_nullary(&self) -> bool {
        self.param_types.is_empty()
    }

    /// Gets the type scheme for this constructor.
    pub fn type_scheme(&self, result_type: &Type) -> TypeScheme {
        if self.param_types.is_empty() {
            // Nullary constructor: just return the result type
            TypeScheme::monomorphic(result_type.clone())
        } else {
            // Constructor function: params -> result
            let func_type = Type::function(self.param_types.clone(), result_type.clone());
            TypeScheme::monomorphic(func_type)
        }
    }
}

impl Pattern {
    /// Checks if this pattern is irrefutable (always matches).
    pub fn is_irrefutable(&self) -> bool {
        match self {
            Pattern::Wildcard | Pattern::Variable(_) => true,
            Pattern::Tuple(patterns) => patterns.iter().all(|p| p.is_irrefutable()),
            Pattern::Record { fields, rest } => {
                fields.values().all(|p| p.is_irrefutable()) && rest.is_none()
            }
            _ => false,
        }
    }

    /// Gets all variable names bound by this pattern.
    pub fn bound_variables(&self) -> HashSet<String> {
        let mut vars = HashSet::new();
        self.collect_variables(&mut vars);
        vars
    }

    fn collect_variables(&self, vars: &mut HashSet<String>) {
        match self {
            Pattern::Variable(name) => {
                vars.insert(name.clone());
            }
            Pattern::Constructor { patterns, .. } => {
                for pattern in patterns {
                    pattern.collect_variables(vars);
                }
            }
            Pattern::Tuple(patterns) => {
                for pattern in patterns {
                    pattern.collect_variables(vars);
                }
            }
            Pattern::Record { fields, rest } => {
                for pattern in fields.values() {
                    pattern.collect_variables(vars);
                }
                if let Some(rest_pattern) = rest {
                    rest_pattern.collect_variables(vars);
                }
            }
            Pattern::Or(patterns) => {
                for pattern in patterns {
                    pattern.collect_variables(vars);
                }
            }
            Pattern::Guard { pattern, .. } => {
                pattern.collect_variables(vars);
            }
            _ => {}
        }
    }

    /// Type-checks this pattern against the given type.
    pub fn type_check(&self, expected_type: &Type) -> Result<HashMap<String, Type>> {
        let mut bindings = HashMap::new();
        self.type_check_impl(expected_type, &mut bindings)?;
        Ok(bindings)
    }

    fn type_check_impl(
        &self,
        expected_type: &Type,
        bindings: &mut HashMap<String, Type>,
    ) -> Result<()> {
        match self {
            Pattern::Wildcard => Ok(()),
            Pattern::Variable(name) => {
                bindings.insert(name.clone(), expected_type.clone());
                Ok(())
            }
            Pattern::Literal(lit) => {
                let lit_type = literal_to_type(lit);
                if types_compatible(&lit_type, expected_type) {
                    Ok(())
                } else {
                    Err(Box::new(Error::type_error(
                        format!(
                            "Pattern literal type {lit_type} doesn't match expected type {expected_type}"
                        ),
                        Span::default(),
                    )))
                }
            }
            Pattern::Constructor { name: _, patterns } => {
                // This would need access to the type environment to resolve constructor types
                // For now, return Ok as a placeholder
                for pattern in patterns {
                    pattern.type_check_impl(&Type::Dynamic, bindings)?;
                }
                Ok(())
            }
            Pattern::Tuple(patterns) => {
                // Check if expected_type is a tuple type
                // For now, type-check each pattern against Dynamic
                for pattern in patterns {
                    pattern.type_check_impl(&Type::Dynamic, bindings)?;
                }
                Ok(())
            }
            Pattern::Record { fields, rest: _ } => {
                // Check if expected_type is a record type
                // For now, type-check each field pattern against Dynamic
                for pattern in fields.values() {
                    pattern.type_check_impl(&Type::Dynamic, bindings)?;
                }
                Ok(())
            }
            Pattern::Or(patterns) => {
                // All patterns in an Or must bind the same variables with the same types
                let mut first_bindings = HashMap::new();
                if let Some(first) = patterns.first() {
                    first.type_check_impl(expected_type, &mut first_bindings)?;
                }

                for pattern in patterns.iter().skip(1) {
                    let mut pattern_bindings = HashMap::new();
                    pattern.type_check_impl(expected_type, &mut pattern_bindings)?;

                    if first_bindings != pattern_bindings {
                        return Err(Box::new(Error::type_error(
                            "All branches in or-pattern must bind the same variables with the same types".to_string(),
                            Span::default(),
                        )));
                    }
                }

                bindings.extend(first_bindings);
                Ok(())
            }
            Pattern::Guard { pattern, .. } => {
                pattern.type_check_impl(expected_type, bindings)
            }
        }
    }
}

impl PatternMatcher {
    /// Creates a new pattern matcher.
    pub fn new() -> Self {
        Self {
            types: HashMap::new(),
            cache: HashMap::new(),
        }
    }

    /// Registers an algebraic data type.
    pub fn register_type(&mut self, adt: AlgebraicDataType) {
        self.types.insert(adt.name.clone(), adt);
    }

    /// Compiles a match expression into a decision tree.
    pub fn compile_match(&mut self, match_expr: &MatchExpression) -> Result<CompiledPattern> {
        let cache_key = format!("{match_expr:?}");
        
        if let Some(cached) = self.cache.get(&cache_key) {
            return Ok(cached.clone());
        }

        let tree = self.compile_clauses(&match_expr.clauses)?;
        let bindings = self.extract_bindings(&match_expr.clauses);

        let compiled = CompiledPattern { tree, bindings };
        self.cache.insert(cache_key, compiled.clone());
        
        Ok(compiled)
    }

    fn compile_clauses(&self, clauses: &[MatchClause]) -> Result<DecisionTree> {
        if clauses.is_empty() {
            return Ok(DecisionTree::Failure);
        }

        // Simplified compilation: just create a basic decision tree
        // In a full implementation, this would use sophisticated algorithms
        // like the one described in "Compiling Pattern Matching to Good Decision Trees"
        
        let first_clause = &clauses[0];
        if first_clause.pattern.is_irrefutable() {
            Ok(DecisionTree::Success {
                bindings: HashMap::new(), // Would extract from pattern
                action: first_clause.body.clone(),
            })
        } else {
            let test = self.pattern_to_test(&first_clause.pattern)?;
            let success = Box::new(DecisionTree::Success {
                bindings: HashMap::new(),
                action: first_clause.body.clone(),
            });
            let failure = Box::new(self.compile_clauses(&clauses[1..])?);
            
            Ok(DecisionTree::Test {
                test,
                success,
                failure,
            })
        }
    }

    fn pattern_to_test(&self, pattern: &Pattern) -> Result<PatternTest> {
        match pattern {
            Pattern::Literal(lit) => Ok(PatternTest::Literal(lit.clone())),
            Pattern::Constructor { name, patterns } => Ok(PatternTest::Constructor {
                name: name.clone(),
                arity: patterns.len(),
            }),
            Pattern::Guard { guard, .. } => Ok(PatternTest::Guard(guard.clone())),
            _ => Err(Box::new(Error::type_error(
                "Cannot convert pattern to test".to_string(),
                Span::default(),
            )))
        }
    }

    fn extract_bindings(&self, clauses: &[MatchClause]) -> Vec<String> {
        clauses
            .iter()
            .flat_map(|clause| clause.pattern.bound_variables())
            .collect()
    }

    /// Checks exhaustiveness of pattern matching.
    pub fn check_exhaustiveness(&self, patterns: &[Pattern], _ty: &Type) -> Result<bool> {
        // Simplified exhaustiveness checking
        // A full implementation would use constraint solving
        
        // Check if any pattern is irrefutable
        if patterns.iter().any(|p| p.is_irrefutable()) {
            return Ok(true);
        }

        // For now, assume non-exhaustive
        Ok(false)
    }

    /// Performs redundancy analysis on patterns.
    pub fn check_redundancy(&self, _patterns: &[Pattern]) -> Vec<usize> {
        // Simplified redundancy checking
        // A full implementation would use decision tree analysis
        Vec::new()
    }
}

// Helper functions

fn literal_to_type(lit: &crate::ast::Literal) -> Type {
    match lit {
        crate::ast::Literal::ExactInteger(_) => Type::Number,
        crate::ast::Literal::InexactReal(_) => Type::Number,
        crate::ast::Literal::Number(_) => Type::Number,
        crate::ast::Literal::Rational { .. } => Type::Number,
        crate::ast::Literal::Complex { .. } => Type::Number,
        crate::ast::Literal::String(_) => Type::String,
        crate::ast::Literal::Boolean(_) => Type::Boolean,
        crate::ast::Literal::Character(_) => Type::Char,
        crate::ast::Literal::Bytevector(_) => Type::Bytevector,
        crate::ast::Literal::Nil => Type::Unit,
        crate::ast::Literal::Unspecified => Type::Unit,
    }
}

fn types_compatible(t1: &Type, t2: &Type) -> bool {
    // Simplified type compatibility checking
    match (t1, t2) {
        (Type::Dynamic, _) | (_, Type::Dynamic) => true,
        _ => t1 == t2,
    }
}

impl fmt::Display for AlgebraicDataType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "data {}", self.name)?;
        
        if !self.type_params.is_empty() {
            write!(f, " (")?;
            for (i, param) in self.type_params.iter().enumerate() {
                if i > 0 { write!(f, " ")?; }
                write!(f, "{param}")?;
            }
            write!(f, ")")?;
        }
        
        match self.variant_type {
            AlgebraicVariant::Sum => {
                for (i, constructor) in self.constructors.iter().enumerate() {
                    if i == 0 {
                        write!(f, " = ")?;
                    } else {
                        write!(f, " | ")?;
                    }
                    write!(f, "{constructor}")?;
                }
            }
            AlgebraicVariant::Product => {
                write!(f, " {{")?;
                for (i, constructor) in self.constructors.iter().enumerate() {
                    if i > 0 { write!(f, ", ")?; }
                    write!(f, "{constructor}")?;
                }
                write!(f, "}}")?;
            }
            AlgebraicVariant::GADT => {
                write!(f, " where")?;
                for constructor in &self.constructors {
                    write!(f, "\n  {constructor}")?;
                }
            }
        }
        
        Ok(())
    }
}

impl fmt::Display for DataConstructor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name)?;
        
        if !self.param_types.is_empty() {
            write!(f, " (")?;
            for (i, param_type) in self.param_types.iter().enumerate() {
                if i > 0 { write!(f, " ")?; }
                write!(f, "{param_type}")?;
            }
            write!(f, ")")?;
        }
        
        if let Some(return_type) = &self.return_type {
            write!(f, " : {return_type}")?;
        }
        
        Ok(())
    }
}

impl fmt::Display for Pattern {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Pattern::Wildcard => write!(f, "_"),
            Pattern::Variable(name) => write!(f, "{name}"),
            Pattern::Literal(lit) => write!(f, "{lit}"),
            Pattern::Constructor { name, patterns } => {
                write!(f, "{name}")?;
                if !patterns.is_empty() {
                    write!(f, " (")?;
                    for (i, pattern) in patterns.iter().enumerate() {
                        if i > 0 { write!(f, " ")?; }
                        write!(f, "{pattern}")?;
                    }
                    write!(f, ")")?;
                }
                Ok(())
            }
            Pattern::Tuple(patterns) => {
                write!(f, "(")?;
                for (i, pattern) in patterns.iter().enumerate() {
                    if i > 0 { write!(f, ", ")?; }
                    write!(f, "{pattern}")?;
                }
                write!(f, ")")
            }
            Pattern::Record { fields, rest } => {
                write!(f, "{{")?;
                for (i, (name, pattern)) in fields.iter().enumerate() {
                    if i > 0 { write!(f, ", ")?; }
                    write!(f, "{name} = {pattern}")?;
                }
                if let Some(rest_pattern) = rest {
                    if !fields.is_empty() { write!(f, ", ")?; }
                    write!(f, "..{rest_pattern}")?;
                }
                write!(f, "}}")
            }
            Pattern::Or(patterns) => {
                for (i, pattern) in patterns.iter().enumerate() {
                    if i > 0 { write!(f, " | ")?; }
                    write!(f, "{pattern}")?;
                }
                Ok(())
            }
            Pattern::Guard { pattern, guard } => {
                write!(f, "{pattern} if {guard}")
            }
        }
    }
}

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

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

    #[test]
    fn test_algebraic_data_type_creation() {
        let mut maybe_type = AlgebraicDataType::new(
            "Maybe".to_string(),
            vec![TypeVar::with_name("a")],
            AlgebraicVariant::Sum,
            None,
        );

        let none_constructor = DataConstructor::new("None".to_string(), vec![], None);
        let some_constructor = DataConstructor::new(
            "Some".to_string(),
            vec![Type::named_var("a")],
            None,
        );

        maybe_type.add_constructor(none_constructor);
        maybe_type.add_constructor(some_constructor);

        assert_eq!(maybe_type.constructors.len(), 2);
        assert_eq!(maybe_type.constructors[0].name, "None");
        assert_eq!(maybe_type.constructors[1].name, "Some");
        assert_eq!(maybe_type.constructors[1].arity(), 1);
    }

    #[test]
    fn test_pattern_variables() {
        let pattern = Pattern::Constructor {
            name: "Some".to_string(),
            patterns: vec![Pattern::Variable("x".to_string())],
        };

        let vars = pattern.bound_variables();
        assert!(vars.contains("x"));
        assert_eq!(vars.len(), 1);
    }

    #[test]
    fn test_pattern_irrefutability() {
        assert!(Pattern::Wildcard.is_irrefutable());
        assert!(Pattern::Variable("x".to_string()).is_irrefutable());
        assert!(!Pattern::Literal(crate::ast::Literal::Boolean(true)).is_irrefutable());

        let tuple_pattern = Pattern::Tuple(vec![
            Pattern::Variable("x".to_string()),
            Pattern::Wildcard,
        ]);
        assert!(tuple_pattern.is_irrefutable());
    }

    #[test]
    fn test_constructor_type_scheme() {
        let constructor = DataConstructor::new(
            "Cons".to_string(),
            vec![Type::named_var("a"), Type::list(Type::named_var("a"))],
            None,
        );

        let result_type = Type::list(Type::named_var("a"));
        let scheme = constructor.type_scheme(&result_type);

        // Should be: a -> List a -> List a
        match &scheme.type_ {
            Type::Function { params, return_type } => {
                assert_eq!(params.len(), 2);
                assert_eq!(**return_type, result_type);
            }
            _ => panic!("Expected function type"),
        }
    }
}