quantalang 1.0.0

The QuantaLang compiler — an effects-oriented systems language with multi-backend codegen (C, HLSL, GLSL, SPIR-V, LLVM IR, WebAssembly, x86-64, ARM64)
Documentation
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
// ===============================================================================
// QUANTALANG TYPE SYSTEM - EFFECT SYSTEM
// ===============================================================================
// Copyright (c) 2022-2026 Zain Dana Harper. MIT License.
// ===============================================================================

//! Effect system for tracking and controlling side effects.
//!
//! QuantaLang uses an algebraic effect system to track:
//! - I/O operations
//! - Exceptions/errors
//! - State mutations
//! - Async/await
//! - Non-determinism
//! - Custom effects
//!
//! ## Syntax
//!
//! ```quanta
//! // Function with effects
//! fn read_file(path: str) -> String with IO, Error {
//!     // ...
//! }
//!
//! // Effect handlers
//! handle {
//!     read_file("data.txt")
//! } with {
//!     IO::read(fd) => resume(mock_data),
//!     Error::throw(e) => default_value,
//! }
//!
//! // Effect polymorphism
//! fn map<A, B, E>(xs: List<A>, f: fn(A) -> B with E) -> List<B> with E {
//!     // ...
//! }
//! ```
//!
//! ## Effect Rows
//!
//! Effects are tracked as rows (sets) that can be:
//! - Empty: `{}` (pure)
//! - Concrete: `{IO, Error}`
//! - Polymorphic: `{IO | E}` (IO plus unknown effects E)

use std::collections::{HashMap, HashSet};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;

use super::{DefId, Ty};

/// A unique identifier for effect variables.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct EffectVarId(pub u32);

impl EffectVarId {
    /// Create a fresh effect variable ID.
    pub fn fresh() -> Self {
        static COUNTER: AtomicU32 = AtomicU32::new(0);
        Self(COUNTER.fetch_add(1, Ordering::SeqCst))
    }
}

impl fmt::Display for EffectVarId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "?E{}", self.0)
    }
}

/// A single effect.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Effect {
    /// The effect name.
    pub name: Arc<str>,
    /// Type parameters for the effect.
    pub params: Vec<Ty>,
}

impl Effect {
    /// Create a new effect.
    pub fn new(name: impl Into<Arc<str>>) -> Self {
        Self {
            name: name.into(),
            params: Vec::new(),
        }
    }

    /// Create an effect with type parameters.
    pub fn with_params(name: impl Into<Arc<str>>, params: Vec<Ty>) -> Self {
        Self {
            name: name.into(),
            params,
        }
    }

    /// The IO effect.
    pub fn io() -> Self {
        Self::new("IO")
    }

    /// The Error effect with error type.
    pub fn error(err_ty: Ty) -> Self {
        Self::with_params("Error", vec![err_ty])
    }

    /// The Async effect.
    pub fn async_effect() -> Self {
        Self::new("Async")
    }

    /// The State effect with state type.
    pub fn state(state_ty: Ty) -> Self {
        Self::with_params("State", vec![state_ty])
    }

    /// The NonDet effect (non-determinism).
    pub fn nondet() -> Self {
        Self::new("NonDet")
    }

    /// The Pure effect (no effects).
    pub fn pure() -> Self {
        Self::new("Pure")
    }

    /// Check if this is the IO effect.
    pub fn is_io(&self) -> bool {
        self.name.as_ref() == "IO"
    }

    /// Check if this is the Error effect.
    pub fn is_error(&self) -> bool {
        self.name.as_ref() == "Error"
    }

    /// Check if this is the Async effect.
    pub fn is_async(&self) -> bool {
        self.name.as_ref() == "Async"
    }
}

impl fmt::Display for Effect {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name)?;
        if !self.params.is_empty() {
            write!(f, "<")?;
            for (i, param) in self.params.iter().enumerate() {
                if i > 0 {
                    write!(f, ", ")?;
                }
                write!(f, "{}", param)?;
            }
            write!(f, ">")?;
        }
        Ok(())
    }
}

/// An effect row - a set of effects with optional tail variable.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EffectRow {
    /// Concrete effects in this row.
    pub effects: HashSet<Effect>,
    /// Optional tail variable for open rows.
    pub tail: Option<EffectVarId>,
}

impl Hash for EffectRow {
    fn hash<H: Hasher>(&self, state: &mut H) {
        // Sort effects by name for deterministic hashing
        let mut sorted: Vec<_> = self.effects.iter().collect();
        sorted.sort_by(|a, b| a.name.cmp(&b.name));
        for eff in &sorted {
            eff.hash(state);
        }
        self.tail.hash(state);
    }
}

impl EffectRow {
    /// Create an empty (pure) effect row.
    pub fn empty() -> Self {
        Self {
            effects: HashSet::new(),
            tail: None,
        }
    }

    /// Create a closed row with specific effects.
    pub fn closed(effects: impl IntoIterator<Item = Effect>) -> Self {
        Self {
            effects: effects.into_iter().collect(),
            tail: None,
        }
    }

    /// Create an open row with a tail variable.
    pub fn open(effects: impl IntoIterator<Item = Effect>, tail: EffectVarId) -> Self {
        Self {
            effects: effects.into_iter().collect(),
            tail: Some(tail),
        }
    }

    /// Create a row with just a variable (fully polymorphic).
    pub fn var(var: EffectVarId) -> Self {
        Self {
            effects: HashSet::new(),
            tail: Some(var),
        }
    }

    /// Create a fresh effect variable row.
    pub fn fresh_var() -> Self {
        Self::var(EffectVarId::fresh())
    }

    /// Check if this row is empty (pure).
    pub fn is_empty(&self) -> bool {
        self.effects.is_empty() && self.tail.is_none()
    }

    /// Check if this row is closed.
    pub fn is_closed(&self) -> bool {
        self.tail.is_none()
    }

    /// Check if this row is open (has a tail variable).
    pub fn is_open(&self) -> bool {
        self.tail.is_some()
    }

    /// Check if this row contains a specific effect.
    pub fn contains(&self, effect: &Effect) -> bool {
        self.effects.contains(effect)
    }

    /// Check if this row contains IO.
    pub fn has_io(&self) -> bool {
        self.effects.iter().any(|e| e.is_io())
    }

    /// Check if this row contains Error.
    pub fn has_error(&self) -> bool {
        self.effects.iter().any(|e| e.is_error())
    }

    /// Check if this row contains Async.
    pub fn has_async(&self) -> bool {
        self.effects.iter().any(|e| e.is_async())
    }

    /// Add an effect to this row.
    pub fn add(&mut self, effect: Effect) {
        self.effects.insert(effect);
    }

    /// Remove an effect from this row.
    pub fn remove(&mut self, effect: &Effect) -> bool {
        self.effects.remove(effect)
    }

    /// Merge two rows.
    pub fn merge(&self, other: &EffectRow) -> EffectRow {
        let effects: HashSet<_> = self.effects.union(&other.effects).cloned().collect();
        let tail = match (self.tail, other.tail) {
            (Some(v1), Some(v2)) if v1 == v2 => Some(v1),
            (Some(v), None) | (None, Some(v)) => Some(v),
            (Some(_), Some(_)) => Some(EffectVarId::fresh()), // Different vars, need fresh
            (None, None) => None,
        };

        EffectRow { effects, tail }
    }

    /// Substitute effect variables.
    pub fn substitute(&self, subst: &EffectSubstitution) -> EffectRow {
        let mut result = EffectRow {
            effects: self.effects.clone(),
            tail: None,
        };

        if let Some(var) = self.tail {
            if let Some(row) = subst.get(var) {
                // Merge with substituted row
                result.effects.extend(row.effects.clone());
                result.tail = row.tail;
            } else {
                result.tail = Some(var);
            }
        }

        result
    }
}

impl fmt::Display for EffectRow {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_empty() {
            return write!(f, "Pure");
        }

        let effects: Vec<_> = self.effects.iter().map(|e| format!("{}", e)).collect();

        if let Some(tail) = self.tail {
            if effects.is_empty() {
                write!(f, "{}", tail)
            } else {
                write!(f, "{} | {}", effects.join(", "), tail)
            }
        } else {
            write!(f, "{}", effects.join(", "))
        }
    }
}

/// A substitution for effect variables.
#[derive(Debug, Clone, Default)]
pub struct EffectSubstitution {
    map: HashMap<EffectVarId, EffectRow>,
}

impl EffectSubstitution {
    /// Create an empty substitution.
    pub fn new() -> Self {
        Self::default()
    }

    /// Insert a mapping.
    pub fn insert(&mut self, var: EffectVarId, row: EffectRow) {
        self.map.insert(var, row);
    }

    /// Get the row for a variable.
    pub fn get(&self, var: EffectVarId) -> Option<&EffectRow> {
        self.map.get(&var)
    }

    /// Check if a variable is bound.
    pub fn contains(&self, var: EffectVarId) -> bool {
        self.map.contains_key(&var)
    }

    /// Compose two substitutions.
    pub fn compose(&self, other: &EffectSubstitution) -> EffectSubstitution {
        let mut result = EffectSubstitution::new();

        for (var, row) in &other.map {
            result.insert(*var, row.substitute(self));
        }

        for (var, row) in &self.map {
            if !result.contains(*var) {
                result.insert(*var, row.clone());
            }
        }

        result
    }
}

/// An effect definition.
#[derive(Debug, Clone)]
pub struct EffectDef {
    /// Definition ID.
    pub def_id: DefId,
    /// Effect name.
    pub name: Arc<str>,
    /// Type parameters.
    pub type_params: Vec<Arc<str>>,
    /// Operations defined by this effect.
    pub operations: Vec<EffectOperation>,
}

impl EffectDef {
    /// Create a new effect definition.
    pub fn new(def_id: DefId, name: impl Into<Arc<str>>) -> Self {
        Self {
            def_id,
            name: name.into(),
            type_params: Vec::new(),
            operations: Vec::new(),
        }
    }

    /// Add a type parameter.
    pub fn with_type_param(mut self, name: impl Into<Arc<str>>) -> Self {
        self.type_params.push(name.into());
        self
    }

    /// Add an operation.
    pub fn with_operation(mut self, op: EffectOperation) -> Self {
        self.operations.push(op);
        self
    }
}

/// An operation within an effect.
#[derive(Debug, Clone)]
pub struct EffectOperation {
    /// Operation name.
    pub name: Arc<str>,
    /// Parameter types.
    pub params: Vec<Ty>,
    /// Return type.
    pub return_ty: Ty,
}

impl EffectOperation {
    /// Create a new operation.
    pub fn new(name: impl Into<Arc<str>>, params: Vec<Ty>, return_ty: Ty) -> Self {
        Self {
            name: name.into(),
            params,
            return_ty,
        }
    }
}

/// An effect handler.
#[derive(Debug, Clone)]
pub struct EffectHandler {
    /// The effect being handled.
    pub effect: Effect,
    /// Handler clauses for each operation.
    pub clauses: Vec<HandlerClause>,
    /// Return clause type (input type -> output type).
    pub return_clause: Option<(Ty, Ty)>,
}

/// A clause in an effect handler.
#[derive(Debug, Clone)]
pub struct HandlerClause {
    /// Operation name.
    pub operation: Arc<str>,
    /// Parameter names.
    pub param_names: Vec<Arc<str>>,
    /// Whether this clause resumes.
    pub resumes: bool,
}

impl HandlerClause {
    /// Create a new handler clause.
    pub fn new(operation: impl Into<Arc<str>>, param_names: Vec<Arc<str>>) -> Self {
        Self {
            operation: operation.into(),
            param_names,
            resumes: true,
        }
    }

    /// Set whether this clause resumes.
    pub fn with_resume(mut self, resumes: bool) -> Self {
        self.resumes = resumes;
        self
    }
}

/// Effectful function type.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EffectfulFn {
    /// Parameter types.
    pub params: Vec<Ty>,
    /// Return type.
    pub return_ty: Ty,
    /// Effect row.
    pub effects: EffectRow,
}

impl EffectfulFn {
    /// Create a new effectful function type.
    pub fn new(params: Vec<Ty>, return_ty: Ty, effects: EffectRow) -> Self {
        Self {
            params,
            return_ty,
            effects,
        }
    }

    /// Create a pure function (no effects).
    pub fn pure(params: Vec<Ty>, return_ty: Ty) -> Self {
        Self::new(params, return_ty, EffectRow::empty())
    }

    /// Check if this function is pure.
    pub fn is_pure(&self) -> bool {
        self.effects.is_empty()
    }
}

impl fmt::Display for EffectfulFn {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "fn(")?;
        for (i, param) in self.params.iter().enumerate() {
            if i > 0 {
                write!(f, ", ")?;
            }
            write!(f, "{}", param)?;
        }
        write!(f, ") -> {}", self.return_ty)?;

        if !self.effects.is_empty() {
            write!(f, " with {}", self.effects)?;
        }

        Ok(())
    }
}

/// Effect inference context.
#[derive(Debug, Default)]
pub struct EffectContext {
    /// Effect definitions.
    effects: HashMap<Arc<str>, EffectDef>,
    /// Current effect substitution.
    subst: EffectSubstitution,
    /// Effect constraints.
    constraints: Vec<EffectConstraint>,
}

impl EffectContext {
    /// Create a new effect context.
    pub fn new() -> Self {
        let mut ctx = Self::default();
        ctx.register_builtin_effects();
        ctx
    }

    /// Register built-in effects.
    fn register_builtin_effects(&mut self) {
        // IO effect
        let io = EffectDef::new(DefId::new(0, 0), "IO")
            .with_operation(EffectOperation::new("print", vec![Ty::str()], Ty::unit()))
            .with_operation(EffectOperation::new("read_line", vec![], Ty::str()));
        self.register_effect(io);

        // Error effect
        let error = EffectDef::new(DefId::new(0, 1), "Error")
            .with_type_param("E")
            .with_operation(EffectOperation::new(
                "throw",
                vec![Ty::param("E", 0)],
                Ty::never(),
            ))
            .with_operation(EffectOperation::new("catch", vec![], Ty::param("E", 0)));
        self.register_effect(error);

        // Async effect
        let async_eff = EffectDef::new(DefId::new(0, 2), "Async")
            .with_operation(EffectOperation::new(
                "await",
                vec![Ty::param("T", 0)],
                Ty::param("T", 0),
            ))
            .with_operation(EffectOperation::new("spawn", vec![], Ty::unit()));
        self.register_effect(async_eff);

        // State effect
        let state = EffectDef::new(DefId::new(0, 3), "State")
            .with_type_param("S")
            .with_operation(EffectOperation::new("get", vec![], Ty::param("S", 0)))
            .with_operation(EffectOperation::new(
                "put",
                vec![Ty::param("S", 0)],
                Ty::unit(),
            ))
            .with_operation(EffectOperation::new(
                "modify",
                vec![Ty::function(vec![Ty::param("S", 0)], Ty::param("S", 0))],
                Ty::unit(),
            ));
        self.register_effect(state);

        // NonDet effect
        let nondet = EffectDef::new(DefId::new(0, 4), "NonDet")
            .with_operation(EffectOperation::new(
                "choose",
                vec![Ty::param("T", 0), Ty::param("T", 0)],
                Ty::param("T", 0),
            ))
            .with_operation(EffectOperation::new("fail", vec![], Ty::never()));
        self.register_effect(nondet);
    }

    /// Register an effect definition.
    pub fn register_effect(&mut self, effect: EffectDef) {
        self.effects.insert(effect.name.clone(), effect);
    }

    /// Get an effect definition.
    pub fn get_effect(&self, name: &str) -> Option<&EffectDef> {
        self.effects.get(name)
    }

    /// Get all registered effect definitions.
    pub fn all_effects(&self) -> Vec<&EffectDef> {
        self.effects.values().collect()
    }

    /// Add an effect constraint.
    pub fn add_constraint(&mut self, constraint: EffectConstraint) {
        self.constraints.push(constraint);
    }

    /// Unify two effect rows.
    pub fn unify_rows(&mut self, r1: &EffectRow, r2: &EffectRow) -> Result<(), EffectError> {
        let r1 = r1.substitute(&self.subst);
        let r2 = r2.substitute(&self.subst);

        match (r1.tail, r2.tail) {
            // Both closed
            (None, None) => {
                if r1.effects == r2.effects {
                    Ok(())
                } else {
                    Err(EffectError::Mismatch {
                        expected: r1.clone(),
                        found: r2.clone(),
                    })
                }
            }

            // One open, one closed
            (Some(v), None) => {
                // Check that r2 contains all effects from r1
                if r1.effects.is_subset(&r2.effects) {
                    let diff: HashSet<_> = r2.effects.difference(&r1.effects).cloned().collect();
                    self.subst.insert(v, EffectRow::closed(diff));
                    Ok(())
                } else {
                    Err(EffectError::Mismatch {
                        expected: r1.clone(),
                        found: r2.clone(),
                    })
                }
            }

            (None, Some(v)) => {
                // Check that r1 contains all effects from r2
                if r2.effects.is_subset(&r1.effects) {
                    let diff: HashSet<_> = r1.effects.difference(&r2.effects).cloned().collect();
                    self.subst.insert(v, EffectRow::closed(diff));
                    Ok(())
                } else {
                    Err(EffectError::Mismatch {
                        expected: r1.clone(),
                        found: r2.clone(),
                    })
                }
            }

            // Both open
            (Some(v1), Some(v2)) if v1 == v2 => {
                // Same variable, check concrete effects match
                if r1.effects == r2.effects {
                    Ok(())
                } else {
                    Err(EffectError::Mismatch {
                        expected: r1.clone(),
                        found: r2.clone(),
                    })
                }
            }

            (Some(v1), Some(v2)) => {
                // Different variables, create fresh variable for the join
                let fresh = EffectVarId::fresh();
                let union: HashSet<_> = r1.effects.union(&r2.effects).cloned().collect();

                // v1 = r1.effects + fresh
                let r1_diff: HashSet<_> = union.difference(&r1.effects).cloned().collect();
                self.subst.insert(v1, EffectRow::open(r1_diff, fresh));

                // v2 = r2.effects + fresh
                let r2_diff: HashSet<_> = union.difference(&r2.effects).cloned().collect();
                self.subst.insert(v2, EffectRow::open(r2_diff, fresh));

                Ok(())
            }
        }
    }

    /// Check if effects are subsumed (r1 ⊆ r2).
    pub fn subsumes(&self, r1: &EffectRow, r2: &EffectRow) -> bool {
        let r1 = r1.substitute(&self.subst);
        let r2 = r2.substitute(&self.subst);

        // All effects in r1 must be in r2
        if !r1.effects.is_subset(&r2.effects) {
            return false;
        }

        // If r1 is open, r2 must also be open (or contain all possible effects)
        match (r1.tail, r2.tail) {
            (None, _) => true,
            (Some(_), None) => false,         // r1 is open but r2 is closed
            (Some(v1), Some(v2)) => v1 == v2, // Same tail variable
        }
    }

    /// Apply current substitution to a row.
    pub fn apply_subst(&self, row: &EffectRow) -> EffectRow {
        row.substitute(&self.subst)
    }
}

/// An effect constraint.
#[derive(Debug, Clone)]
pub enum EffectConstraint {
    /// r1 = r2
    Equal(EffectRow, EffectRow),
    /// r1 ⊆ r2
    Subsumes(EffectRow, EffectRow),
    /// Effect must be handled
    MustHandle(Effect),
}

/// Effect errors.
#[derive(Debug, Clone)]
pub enum EffectError {
    /// Effect row mismatch.
    Mismatch {
        expected: EffectRow,
        found: EffectRow,
    },

    /// Unhandled effect.
    Unhandled(Effect),

    /// Unknown effect.
    UnknownEffect(String),

    /// Unknown operation.
    UnknownOperation { effect: String, operation: String },

    /// Invalid handler.
    InvalidHandler(String),
}

impl fmt::Display for EffectError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            EffectError::Mismatch { expected, found } => {
                write!(
                    f,
                    "effect mismatch: expected {{{}}}, found {{{}}}",
                    expected, found
                )
            }
            EffectError::Unhandled(effect) => {
                write!(f, "unhandled effect: {}", effect)
            }
            EffectError::UnknownEffect(name) => {
                write!(f, "unknown effect: {}", name)
            }
            EffectError::UnknownOperation { effect, operation } => {
                write!(
                    f,
                    "unknown operation '{}' in effect '{}'",
                    operation, effect
                )
            }
            EffectError::InvalidHandler(msg) => {
                write!(f, "invalid handler: {}", msg)
            }
        }
    }
}

impl std::error::Error for EffectError {}

/// Built-in effect definitions.
pub fn builtin_effects() -> Vec<EffectDef> {
    let ctx = EffectContext::new();
    ctx.effects.into_values().collect()
}

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

    #[test]
    fn test_effect_display() {
        assert_eq!(format!("{}", Effect::io()), "IO");
        assert_eq!(format!("{}", Effect::async_effect()), "Async");

        let error = Effect::error(Ty::str());
        assert_eq!(format!("{}", error), "Error<str>");
    }

    #[test]
    fn test_effect_row() {
        let empty = EffectRow::empty();
        assert!(empty.is_empty());
        assert!(empty.is_closed());
        assert_eq!(format!("{}", empty), "Pure");

        let io_row = EffectRow::closed(vec![Effect::io()]);
        assert!(!io_row.is_empty());
        assert!(io_row.has_io());
        assert_eq!(format!("{}", io_row), "IO");

        let multi = EffectRow::closed(vec![Effect::io(), Effect::async_effect()]);
        assert!(multi.has_io());
        assert!(multi.has_async());
    }

    #[test]
    fn test_effect_row_merge() {
        let r1 = EffectRow::closed(vec![Effect::io()]);
        let r2 = EffectRow::closed(vec![Effect::async_effect()]);

        let merged = r1.merge(&r2);
        assert!(merged.has_io());
        assert!(merged.has_async());
        assert!(merged.is_closed());
    }

    #[test]
    fn test_open_effect_row() {
        let var = EffectVarId::fresh();
        let open = EffectRow::open(vec![Effect::io()], var);

        assert!(open.is_open());
        assert!(open.has_io());
    }

    #[test]
    fn test_effect_unification() {
        let mut ctx = EffectContext::new();

        // Unify two identical closed rows
        let r1 = EffectRow::closed(vec![Effect::io()]);
        let r2 = EffectRow::closed(vec![Effect::io()]);
        assert!(ctx.unify_rows(&r1, &r2).is_ok());

        // Unify different closed rows should fail
        let mut ctx2 = EffectContext::new();
        let r3 = EffectRow::closed(vec![Effect::io()]);
        let r4 = EffectRow::closed(vec![Effect::async_effect()]);
        assert!(ctx2.unify_rows(&r3, &r4).is_err());
    }

    #[test]
    fn test_effectful_fn() {
        let pure_fn = EffectfulFn::pure(vec![Ty::int(super::super::IntTy::I32)], Ty::bool());
        assert!(pure_fn.is_pure());

        let io_fn = EffectfulFn::new(
            vec![Ty::str()],
            Ty::unit(),
            EffectRow::closed(vec![Effect::io()]),
        );
        assert!(!io_fn.is_pure());
        assert!(io_fn.effects.has_io());
    }
}