logicaffeine-compile 0.10.0

LOGOS compilation pipeline - codegen and interpreter
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
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
use std::collections::{HashMap, HashSet};

use crate::analysis::DiscoveryPass;
use crate::arena::Arena;
use crate::arena_ctx::AstContext;
use crate::ast::stmt::{ClosureBody, Expr, Literal, Pattern, Stmt};
use crate::drs::WorldState;
use crate::error::ParseError;
use crate::intern::{Interner, Symbol};
use crate::lexer::Lexer;
use crate::parser::Parser;

use crate::analysis::callgraph::CallGraph;

/// Effect classification for expressions, statements, and functions.
///
/// Effects form a lattice: Pure ⊂ Read ⊂ Write ⊂ IO ⊂ Unknown.
/// Each EffectSet tracks the precise set of variables read/written,
/// plus boolean flags for allocation, IO, security checks, divergence,
/// and unknown (escape blocks).
#[derive(Debug, Clone, Default)]
pub struct EffectSet {
    pub reads: HashSet<Symbol>,
    pub writes: HashSet<Symbol>,
    pub allocates: bool,
    pub io: bool,
    pub security_check: bool,
    pub diverges: bool,
    pub unknown: bool,
    /// Nondeterministic choice — `Select`, `Try*` (depends on instantaneous buffer
    /// state), and cooperative `StopTask`. The outcome is not a function of the inputs,
    /// so a function carrying one must never be specialized on a static argument.
    pub nondet: bool,
    /// Spawns or communicates across a concurrency boundary — `LaunchTask`, `CreatePipe`,
    /// `Spawn`, and the networking statements. Opaque to the partial evaluator.
    pub concurrent: bool,
}

impl EffectSet {
    pub fn pure() -> Self {
        Self::default()
    }

    pub fn read(sym: Symbol) -> Self {
        let mut s = Self::default();
        s.reads.insert(sym);
        s
    }

    pub fn write(sym: Symbol) -> Self {
        let mut s = Self::default();
        s.writes.insert(sym);
        s
    }

    pub fn io() -> Self {
        let mut s = Self::default();
        s.io = true;
        s
    }

    pub fn alloc() -> Self {
        let mut s = Self::default();
        s.allocates = true;
        s
    }

    pub fn unknown() -> Self {
        let mut s = Self::default();
        s.unknown = true;
        s
    }

    pub fn nondet() -> Self {
        let mut s = Self::default();
        s.nondet = true;
        s
    }

    pub fn concurrent() -> Self {
        let mut s = Self::default();
        s.concurrent = true;
        s
    }

    pub fn is_pure(&self) -> bool {
        self.reads.is_empty()
            && self.writes.is_empty()
            && !self.allocates
            && !self.io
            && !self.security_check
            && !self.diverges
            && !self.unknown
            && !self.nondet
            && !self.concurrent
    }

    /// Join two EffectSets (union of all effects).
    pub fn join(&mut self, other: &EffectSet) {
        self.reads.extend(&other.reads);
        self.writes.extend(&other.writes);
        self.allocates |= other.allocates;
        self.io |= other.io;
        self.security_check |= other.security_check;
        self.diverges |= other.diverges;
        self.unknown |= other.unknown;
        self.nondet |= other.nondet;
        self.concurrent |= other.concurrent;
    }
}

/// Environment tracking effects for variables and functions.
///
/// For each variable binding (`Let x be <expr>`), tracks the EffectSet
/// of the bound expression. For each function, tracks the aggregate
/// EffectSet of its body.
pub struct EffectEnv {
    /// Binding-level effects: var_sym → effect of the bound expression
    bindings: HashMap<Symbol, EffectSet>,
    /// Statement-level aggregate effects (joined across all statements)
    aggregate: EffectSet,
    /// Function-level effects: fn_sym → aggregate effect of body
    functions: HashMap<Symbol, EffectSet>,
    /// Interner for name resolution in test queries
    interner: Interner,
}

impl EffectEnv {
    /// Analyze already-parsed statements and return the EffectEnv.
    pub fn from_stmts(stmts: &[Stmt<'_>], interner: &Interner) -> Self {
        let env = analyze_program(stmts, interner);
        Self {
            bindings: env.bindings,
            aggregate: env.aggregate,
            functions: env.functions,
            interner: interner.clone(),
        }
    }

    /// Analyze a complete LOGOS source and return the EffectEnv.
    pub fn analyze_source(source: &str) -> Result<Self, ParseError> {
        let mut interner = Interner::new();
        let mut lexer = Lexer::new(source, &mut interner);
        let tokens = lexer.tokenize();

        let type_registry = {
            let mut discovery = DiscoveryPass::new(&tokens, &mut interner);
            let result = discovery.run_full();
            result.types
        };

        let mut world_state = WorldState::new();
        let expr_arena = Arena::new();
        let term_arena = Arena::new();
        let np_arena = Arena::new();
        let sym_arena = Arena::new();
        let role_arena = Arena::new();
        let pp_arena = Arena::new();
        let stmt_arena = Arena::new();
        let imperative_expr_arena = Arena::new();
        let type_expr_arena = Arena::new();

        let ast_ctx = AstContext::with_types(
            &expr_arena,
            &term_arena,
            &np_arena,
            &sym_arena,
            &role_arena,
            &pp_arena,
            &stmt_arena,
            &imperative_expr_arena,
            &type_expr_arena,
        );

        let mut parser = Parser::new(tokens, &mut world_state, &mut interner, ast_ctx, type_registry);
        let stmts = parser.parse_program()?;

        let env = analyze_program(&stmts, &interner);

        Ok(Self {
            bindings: env.bindings,
            aggregate: env.aggregate,
            functions: env.functions,
            interner,
        })
    }

    fn resolve_name(&self, name: &str) -> Option<Symbol> {
        // Try to find the symbol in the interner
        self.interner.lookup(name)
    }

    pub fn is_binding_pure(&self, var_name: &str) -> bool {
        if let Some(sym) = self.resolve_name(var_name) {
            self.bindings.get(&sym).map(|e| e.is_pure()).unwrap_or(true)
        } else {
            true
        }
    }

    pub fn binding_reads(&self, var_name: &str, read_var: &str) -> bool {
        let var_sym = match self.resolve_name(var_name) {
            Some(s) => s,
            None => return false,
        };
        let read_sym = match self.resolve_name(read_var) {
            Some(s) => s,
            None => return false,
        };
        self.bindings
            .get(&var_sym)
            .map(|e| e.reads.contains(&read_sym))
            .unwrap_or(false)
    }

    pub fn binding_allocates(&self, var_name: &str) -> bool {
        if let Some(sym) = self.resolve_name(var_name) {
            self.bindings.get(&sym).map(|e| e.allocates).unwrap_or(false)
        } else {
            false
        }
    }

    pub fn has_write_to(&self, var_name: &str) -> bool {
        if let Some(sym) = self.resolve_name(var_name) {
            self.aggregate.writes.contains(&sym)
        } else {
            false
        }
    }

    pub fn has_io(&self) -> bool {
        self.aggregate.io
    }

    pub fn has_unknown(&self) -> bool {
        self.aggregate.unknown
    }

    pub fn has_security_check(&self) -> bool {
        self.aggregate.security_check
    }

    pub fn may_diverge(&self) -> bool {
        self.aggregate.diverges
    }

    pub fn function_is_pure(&self, fn_name: &str) -> bool {
        if let Some(sym) = self.resolve_name(fn_name) {
            self.functions.get(&sym).map(|e| e.is_pure()).unwrap_or(true)
        } else {
            true
        }
    }

    pub fn function_has_io(&self, fn_name: &str) -> bool {
        if let Some(sym) = self.resolve_name(fn_name) {
            self.functions.get(&sym).map(|e| e.io).unwrap_or(false)
        } else {
            false
        }
    }

    /// Whether the partial evaluator may specialize a call to `fn_name` on a static
    /// argument. Reads, writes, allocation, and divergence are ordinary PE targets and
    /// stay safe. IO already blocks specialization (the historical gate). Nondeterminism
    /// and concurrency are the new exclusions: a concurrency / networking statement carries
    /// expression arguments (a timeout, spawn args, a branch body) that reference the
    /// static parameter, and the specializer's substitution does not enter those
    /// statements — folding across one would drop the parameter and leave a dangling
    /// reference. A `Check` (security_check) is deliberately *not* excluded: it is preserved
    /// verbatim and never references a foldable parameter, so specializing across it is
    /// sound (see `pe_effect_env_check_is_not_io`). Escape blocks (`unknown`) are handled by
    /// the caller's separate `body_has_escape` guard.
    pub fn function_is_specialization_safe(&self, fn_name: &str) -> bool {
        if let Some(sym) = self.resolve_name(fn_name) {
            match self.functions.get(&sym) {
                Some(e) => !e.io && !e.nondet && !e.concurrent,
                None => false,
            }
        } else {
            false
        }
    }
}

// =============================================================================
// Internal analysis
// =============================================================================

struct AnalysisResult {
    bindings: HashMap<Symbol, EffectSet>,
    aggregate: EffectSet,
    functions: HashMap<Symbol, EffectSet>,
}

fn analyze_program(stmts: &[Stmt<'_>], interner: &Interner) -> AnalysisResult {
    let callgraph = CallGraph::build(stmts, interner);

    // Phase 1: Analyze function bodies
    let mut functions: HashMap<Symbol, EffectSet> = HashMap::new();

    // Collect all known function names first
    let mut known_fns: HashSet<Symbol> = HashSet::new();
    for stmt in stmts {
        if let Stmt::FunctionDef { name, .. } = stmt {
            known_fns.insert(*name);
        }
    }

    // Initialize all functions with their direct effects
    for stmt in stmts {
        if let Stmt::FunctionDef { name, body, is_native, params, .. } = stmt {
            if *is_native {
                // Native functions: classify by name
                let effects = classify_native_function(*name, interner);
                functions.insert(*name, effects);
            } else {
                let mut effects = analyze_block_effects_with(body, &known_fns);
                // Remove parameter reads — reading a parameter is not a side effect
                for (param_sym, _) in params {
                    effects.reads.remove(param_sym);
                }
                functions.insert(*name, effects);
            }
        }
    }

    // Fixed-point: propagate effects through call graph
    let mut changed = true;
    while changed {
        changed = false;
        for scc in &callgraph.sccs {
            // For each SCC, join effects of all callees
            for &fn_sym in scc {
                if callgraph.native_fns.contains(&fn_sym) {
                    continue;
                }
                let callees = callgraph.edges.get(&fn_sym).cloned().unwrap_or_default();
                for callee in &callees {
                    if let Some(callee_effects) = functions.get(callee).cloned() {
                        if let Some(fn_effects) = functions.get_mut(&fn_sym) {
                            let before = format!("{:?}", fn_effects);
                            fn_effects.join(&callee_effects);
                            let after = format!("{:?}", fn_effects);
                            if before != after {
                                changed = true;
                            }
                        }
                    }
                }
            }

            // Recursive SCCs always diverge
            if scc.len() > 1 {
                for &fn_sym in scc {
                    if let Some(fn_effects) = functions.get_mut(&fn_sym) {
                        if !fn_effects.diverges {
                            fn_effects.diverges = true;
                            changed = true;
                        }
                    }
                }
            } else if scc.len() == 1 {
                let fn_sym = scc[0];
                if callgraph.edges.get(&fn_sym).map(|e| e.contains(&fn_sym)).unwrap_or(false) {
                    if let Some(fn_effects) = functions.get_mut(&fn_sym) {
                        if !fn_effects.diverges {
                            fn_effects.diverges = true;
                            changed = true;
                        }
                    }
                }
            }
        }
    }

    // Phase 2: Analyze main body and bindings
    let mut bindings: HashMap<Symbol, EffectSet> = HashMap::new();
    let mut aggregate = EffectSet::default();

    for stmt in stmts {
        let effects = analyze_stmt_effects(stmt, &functions);
        aggregate.join(&effects);

        // Track per-binding effects
        if let Stmt::Let { var, value, .. } = stmt {
            let expr_effects = analyze_expr_effects(value);
            bindings.insert(*var, expr_effects);
        }
    }

    AnalysisResult {
        bindings,
        aggregate,
        functions,
    }
}

fn analyze_block_effects(stmts: &[Stmt<'_>]) -> EffectSet {
    analyze_block_effects_with(stmts, &HashSet::new())
}

fn analyze_block_effects_with(stmts: &[Stmt<'_>], known_fns: &HashSet<Symbol>) -> EffectSet {
    let mut result = EffectSet::default();
    for stmt in stmts {
        let effects = analyze_stmt_effects_initial(stmt, known_fns);
        result.join(&effects);
    }
    result
}

/// Analyze statement effects during initial function body analysis.
/// known_fns tracks which functions are defined in the program so calls to them
/// don't get marked as unknown (the fixed-point iteration handles transitive effects).
fn analyze_stmt_effects_initial(stmt: &Stmt<'_>, known_fns: &HashSet<Symbol>) -> EffectSet {
    analyze_stmt_effects_core(stmt, None, known_fns)
}

fn analyze_stmt_effects(stmt: &Stmt<'_>, functions: &HashMap<Symbol, EffectSet>) -> EffectSet {
    analyze_stmt_effects_core(stmt, Some(functions), &HashSet::new())
}

fn analyze_stmt_effects_core(
    stmt: &Stmt<'_>,
    functions: Option<&HashMap<Symbol, EffectSet>>,
    known_fns: &HashSet<Symbol>,
) -> EffectSet {
    match stmt {
        Stmt::Let { var, value, .. } => {
            let mut effects = analyze_expr_effects_core(value, known_fns);
            effects.writes.insert(*var);
            effects
        }
        Stmt::Set { target, value } => {
            let mut effects = analyze_expr_effects_core(value, known_fns);
            effects.writes.insert(*target);
            effects
        }
        Stmt::Call { function, args } => {
            let mut effects = EffectSet::default();
            for arg in args {
                effects.join(&analyze_expr_effects_core(arg, known_fns));
            }
            if let Some(fns) = functions {
                if let Some(fn_effects) = fns.get(function) {
                    effects.join(fn_effects);
                } else if !known_fns.contains(function) {
                    effects.unknown = true;
                }
            } else if !known_fns.contains(function) {
                effects.unknown = true;
            }
            effects
        }
        Stmt::If { cond, then_block, else_block } => {
            let mut effects = analyze_expr_effects_core(cond, known_fns);
            let then_effects = analyze_block_effects_with(then_block, known_fns);
            effects.join(&then_effects);
            if let Some(else_b) = else_block {
                let else_effects = analyze_block_effects_with(else_b, known_fns);
                effects.join(&else_effects);
            }
            effects
        }
        Stmt::While { cond, body, .. } => {
            let mut effects = analyze_expr_effects_core(cond, known_fns);
            let body_effects = analyze_block_effects_with(body, known_fns);
            effects.join(&body_effects);
            effects.diverges = true;
            effects
        }
        Stmt::Repeat { iterable, body, .. } => {
            let mut effects = analyze_expr_effects_core(iterable, known_fns);
            let body_effects = analyze_block_effects_with(body, known_fns);
            effects.join(&body_effects);
            effects
        }
        Stmt::Return { value } => {
            if let Some(v) = value {
                analyze_expr_effects_core(v, known_fns)
            } else {
                EffectSet::pure()
            }
        }
        Stmt::Show { object, .. } => {
            let mut effects = analyze_expr_effects_core(object, known_fns);
            effects.io = true;
            effects
        }
        Stmt::Push { value, collection } => {
            let mut effects = analyze_expr_effects_core(value, known_fns);
            effects.join(&analyze_expr_effects_core(collection, known_fns));
            if let Expr::Identifier(sym) = collection {
                effects.writes.insert(*sym);
            }
            effects
        }
        Stmt::Pop { collection, into } => {
            let mut effects = analyze_expr_effects_core(collection, known_fns);
            if let Expr::Identifier(sym) = collection {
                effects.writes.insert(*sym);
            }
            if let Some(into_sym) = into {
                effects.writes.insert(*into_sym);
            }
            effects
        }
        Stmt::Add { value, collection } | Stmt::Remove { value, collection } => {
            let mut effects = analyze_expr_effects_core(value, known_fns);
            effects.join(&analyze_expr_effects_core(collection, known_fns));
            if let Expr::Identifier(sym) = collection {
                effects.writes.insert(*sym);
            }
            effects
        }
        Stmt::SetIndex { collection, index, value } => {
            let mut effects = analyze_expr_effects_core(value, known_fns);
            effects.join(&analyze_expr_effects_core(index, known_fns));
            if let Expr::Identifier(sym) = collection {
                effects.writes.insert(*sym);
            }
            effects
        }
        Stmt::SetField { object, value, .. } => {
            let mut effects = analyze_expr_effects_core(value, known_fns);
            if let Expr::Identifier(sym) = object {
                effects.writes.insert(*sym);
            }
            effects
        }
        Stmt::Give { object, recipient } => {
            let mut effects = analyze_expr_effects_core(object, known_fns);
            effects.join(&analyze_expr_effects_core(recipient, known_fns));
            effects
        }
        Stmt::Escape { .. } => EffectSet::unknown(),
        Stmt::Check { .. } => {
            let mut effects = EffectSet::default();
            effects.security_check = true;
            effects
        }
        Stmt::RuntimeAssert { condition, .. } => analyze_expr_effects_core(condition, known_fns),
        Stmt::FunctionDef { .. } | Stmt::StructDef { .. } => EffectSet::pure(),
        Stmt::Inspect { target, arms, .. } => {
            let mut effects = analyze_expr_effects_core(target, known_fns);
            for arm in arms {
                let arm_effects = analyze_block_effects_with(arm.body, known_fns);
                effects.join(&arm_effects);
            }
            effects
        }
        Stmt::Zone { body, .. } => analyze_block_effects_with(body, known_fns),
        Stmt::Concurrent { tasks } | Stmt::Parallel { tasks } => {
            let mut effects = analyze_block_effects_with(tasks, known_fns);
            effects.concurrent = true;
            effects
        }
        Stmt::WriteFile { content, path } => {
            let mut effects = analyze_expr_effects_core(content, known_fns);
            effects.join(&analyze_expr_effects_core(path, known_fns));
            effects.io = true;
            effects
        }
        Stmt::SendMessage { message, destination, .. } => {
            let mut effects = analyze_expr_effects_core(message, known_fns);
            effects.join(&analyze_expr_effects_core(destination, known_fns));
            effects.io = true;
            effects
        }
        Stmt::StreamMessage { values, destination } => {
            let mut effects = analyze_expr_effects_core(values, known_fns);
            effects.join(&analyze_expr_effects_core(destination, known_fns));
            effects.io = true;
            effects
        }
        Stmt::IncreaseCrdt { object, amount, .. } | Stmt::DecreaseCrdt { object, amount, .. } => {
            let mut effects = analyze_expr_effects_core(object, known_fns);
            effects.join(&analyze_expr_effects_core(amount, known_fns));
            if let Expr::Identifier(sym) = object {
                effects.writes.insert(*sym);
            }
            effects
        }
        Stmt::MergeCrdt { source, target } => {
            let mut effects = analyze_expr_effects_core(source, known_fns);
            effects.join(&analyze_expr_effects_core(target, known_fns));
            if let Expr::Identifier(sym) = target {
                effects.writes.insert(*sym);
            }
            effects
        }
        Stmt::Sleep { milliseconds } => {
            let mut effects = analyze_expr_effects_core(milliseconds, known_fns);
            effects.io = true;
            effects
        }
        Stmt::SendPipe { value, pipe } => {
            let mut effects = analyze_expr_effects_core(value, known_fns);
            effects.join(&analyze_expr_effects_core(pipe, known_fns));
            effects.io = true;
            effects.diverges = true;
            effects.concurrent = true;
            effects
        }
        Stmt::TrySendPipe { value, pipe, result } => {
            let mut effects = analyze_expr_effects_core(value, known_fns);
            effects.join(&analyze_expr_effects_core(pipe, known_fns));
            effects.io = true;
            effects.concurrent = true;
            // Success depends on instantaneous buffer state — nondeterministic.
            effects.nondet = true;
            if let Some(result_sym) = result {
                effects.writes.insert(*result_sym);
            }
            effects
        }
        Stmt::ReceivePipe { pipe, var, .. } => {
            let mut effects = analyze_expr_effects_core(pipe, known_fns);
            effects.writes.insert(*var);
            effects.io = true;
            effects.diverges = true;
            effects.concurrent = true;
            effects
        }
        Stmt::ReadFrom { var, .. } => {
            let mut effects = EffectSet::default();
            effects.writes.insert(*var);
            effects.io = true;
            effects
        }
        // ----- Go-like concurrency: opaque effectful boundaries -----
        Stmt::Select { branches } => {
            // A Select is a nondeterministic choice over its ready branches. Its outcome
            // is not a function of the inputs, and each branch body is part of the effect.
            let mut effects = EffectSet::nondet();
            for branch in branches {
                match branch {
                    crate::ast::stmt::SelectBranch::Receive { var, pipe, body } => {
                        effects.join(&analyze_expr_effects_core(pipe, known_fns));
                        effects.writes.insert(*var);
                        effects.join(&analyze_block_effects_with(body, known_fns));
                    }
                    crate::ast::stmt::SelectBranch::Timeout { milliseconds, body } => {
                        effects.join(&analyze_expr_effects_core(milliseconds, known_fns));
                        effects.join(&analyze_block_effects_with(body, known_fns));
                    }
                }
            }
            effects.concurrent = true;
            effects
        }
        Stmt::LaunchTask { args, .. } => {
            let mut effects = EffectSet::concurrent();
            for arg in args {
                effects.join(&analyze_expr_effects_core(arg, known_fns));
            }
            effects
        }
        Stmt::LaunchTaskWithHandle { handle, args, .. } => {
            let mut effects = EffectSet::concurrent();
            effects.writes.insert(*handle);
            for arg in args {
                effects.join(&analyze_expr_effects_core(arg, known_fns));
            }
            effects
        }
        Stmt::CreatePipe { var, .. } => {
            let mut effects = EffectSet::concurrent();
            effects.allocates = true;
            effects.writes.insert(*var);
            effects
        }
        Stmt::TryReceivePipe { var, pipe } => {
            // Non-blocking receive: returns value-or-Nothing depending on instantaneous
            // buffer state — nondeterministic, and it binds `var`.
            let mut effects = analyze_expr_effects_core(pipe, known_fns);
            effects.writes.insert(*var);
            effects.io = true;
            effects.nondet = true;
            effects.concurrent = true;
            effects
        }
        Stmt::StopTask { handle } => {
            // Cooperative cancellation — scheduling-dependent.
            let mut effects = analyze_expr_effects_core(handle, known_fns);
            effects.nondet = true;
            effects.concurrent = true;
            effects
        }
        Stmt::Spawn { name, .. } => {
            let mut effects = EffectSet::concurrent();
            effects.writes.insert(*name);
            effects
        }
        // ----- Networking over the relay: I/O boundaries -----
        Stmt::AwaitMessage { source, into, view: _, stream: _ } => {
            let mut effects = analyze_expr_effects_core(source, known_fns);
            effects.writes.insert(*into);
            effects.io = true;
            effects.concurrent = true;
            effects
        }
        Stmt::Listen { address, secure } | Stmt::ConnectTo { address, secure } => {
            let mut effects = analyze_expr_effects_core(address, known_fns);
            if let Some(bind) = secure {
                // The pad path is evaluated too; fold in its (read-only) effects for completeness.
                let pad_fx = analyze_expr_effects_core(bind.pad, known_fns);
                effects.io |= pad_fx.io;
                effects.concurrent |= pad_fx.concurrent;
                effects.writes.extend(pad_fx.writes);
            }
            effects.io = true;
            effects.concurrent = true;
            effects
        }
        Stmt::LetPeerAgent { var, address } => {
            let mut effects = analyze_expr_effects_core(address, known_fns);
            effects.writes.insert(*var);
            effects.io = true;
            effects.concurrent = true;
            effects
        }
        Stmt::Sync { var, topic } => {
            // Subscribe + auto-publish-on-mutation + auto-merge-on-receive: reads and
            // writes the synced CRDT and talks to the relay.
            let mut effects = analyze_expr_effects_core(topic, known_fns);
            effects.reads.insert(*var);
            effects.writes.insert(*var);
            effects.io = true;
            effects.concurrent = true;
            effects
        }
        Stmt::Mount { var, path } => {
            let mut effects = analyze_expr_effects_core(path, known_fns);
            effects.writes.insert(*var);
            effects.io = true;
            effects
        }
        // ----- CRDT structural mutations not covered above -----
        Stmt::AppendToSequence { sequence, value } => {
            let mut effects = analyze_expr_effects_core(sequence, known_fns);
            effects.join(&analyze_expr_effects_core(value, known_fns));
            if let Expr::Identifier(sym) = sequence {
                effects.writes.insert(*sym);
            }
            effects
        }
        Stmt::ResolveConflict { object, value, .. } => {
            let mut effects = analyze_expr_effects_core(object, known_fns);
            effects.join(&analyze_expr_effects_core(value, known_fns));
            if let Expr::Identifier(sym) = object {
                effects.writes.insert(*sym);
            }
            effects
        }
        _ => EffectSet::default(),
    }
}

fn analyze_expr_effects(expr: &Expr<'_>) -> EffectSet {
    analyze_expr_effects_core(expr, &HashSet::new())
}

fn analyze_expr_effects_core(expr: &Expr<'_>, known_fns: &HashSet<Symbol>) -> EffectSet {
    match expr {
        Expr::Literal(_) | Expr::OptionNone => EffectSet::pure(),
        Expr::Identifier(sym) => EffectSet::read(*sym),
        Expr::BinaryOp { left, right, .. } => {
            let mut effects = analyze_expr_effects_core(left, known_fns);
            effects.join(&analyze_expr_effects_core(right, known_fns));
            effects
        }
        Expr::Not { operand } => analyze_expr_effects_core(operand, known_fns),
        Expr::Call { function, args, .. } => {
            let mut effects = EffectSet::default();
            for arg in args {
                effects.join(&analyze_expr_effects_core(arg, known_fns));
            }
            // Only mark as unknown if the callee is not a known function
            if !known_fns.contains(function) {
                effects.unknown = true;
            }
            effects
        }
        Expr::CallExpr { callee, args } => {
            let mut effects = analyze_expr_effects_core(callee, known_fns);
            for arg in args {
                effects.join(&analyze_expr_effects_core(arg, known_fns));
            }
            effects.unknown = true;
            effects
        }
        Expr::Index { collection, index } => {
            let mut effects = analyze_expr_effects_core(collection, known_fns);
            effects.join(&analyze_expr_effects_core(index, known_fns));
            effects
        }
        Expr::Slice { collection, start, end } => {
            let mut effects = analyze_expr_effects_core(collection, known_fns);
            effects.join(&analyze_expr_effects_core(start, known_fns));
            effects.join(&analyze_expr_effects_core(end, known_fns));
            effects
        }
        Expr::Length { collection } => analyze_expr_effects_core(collection, known_fns),
        Expr::Contains { collection, value } => {
            let mut effects = analyze_expr_effects_core(collection, known_fns);
            effects.join(&analyze_expr_effects_core(value, known_fns));
            effects
        }
        Expr::Union { left, right } | Expr::Intersection { left, right }
        | Expr::Range { start: left, end: right } => {
            let mut effects = analyze_expr_effects_core(left, known_fns);
            effects.join(&analyze_expr_effects_core(right, known_fns));
            effects
        }
        Expr::Copy { expr } => {
            let mut effects = analyze_expr_effects_core(expr, known_fns);
            effects.allocates = true;
            effects
        }
        Expr::Give { value } => analyze_expr_effects_core(value, known_fns),
        Expr::FieldAccess { object, .. } => analyze_expr_effects_core(object, known_fns),
        Expr::OptionSome { value } => analyze_expr_effects_core(value, known_fns),
        Expr::ManifestOf { zone } => analyze_expr_effects_core(zone, known_fns),
        Expr::ChunkAt { index, zone } => {
            let mut effects = analyze_expr_effects_core(index, known_fns);
            effects.join(&analyze_expr_effects_core(zone, known_fns));
            effects
        }
        Expr::New { init_fields, .. } => {
            let mut effects = EffectSet::default();
            effects.allocates = true;
            for (_, val) in init_fields {
                effects.join(&analyze_expr_effects_core(val, known_fns));
            }
            effects
        }
        Expr::NewVariant { fields, .. } => {
            let mut effects = EffectSet::default();
            for (_, val) in fields {
                effects.join(&analyze_expr_effects_core(val, known_fns));
            }
            effects
        }
        Expr::WithCapacity { value, capacity } => {
            let mut effects = analyze_expr_effects_core(value, known_fns);
            effects.join(&analyze_expr_effects_core(capacity, known_fns));
            effects.allocates = true;
            effects
        }
        Expr::List(elems) | Expr::Tuple(elems) => {
            let mut effects = EffectSet::default();
            for elem in elems {
                effects.join(&analyze_expr_effects_core(elem, known_fns));
            }
            effects
        }
        Expr::Closure { .. } => EffectSet::pure(),
        Expr::Escape { .. } => EffectSet::unknown(),
        Expr::InterpolatedString(parts) => {
            let mut effects = EffectSet::default();
            for part in parts {
                if let crate::ast::stmt::StringPart::Expr { value, .. } = part {
                    effects.join(&analyze_expr_effects_core(value, known_fns));
                }
            }
            effects
        }
    }
}

/// Classify a native function's effects by name.
fn classify_native_function(sym: Symbol, interner: &Interner) -> EffectSet {
    let name = interner.resolve(sym);
    match name {
        // Pure functions
        "parseInt" | "parseFloat" | "decimal" | "complex" | "modular" | "quantity" | "convert" | "money"
        | "uuid" | "uuid_nil" | "uuid_max" | "uuid_v3" | "uuid_v5" | "uuid_version"
        | "uuid_dns" | "uuid_url" | "uuid_oid" | "uuid_x500"
        | "md5" | "sha1" | "text_bytes" | "uuid_bytes" | "uuid_from_bytes"
        | "parse_timestamp" | "format_timestamp" | "year_of" | "month_of" | "day_of" | "weekday_of"
        | "hour_of" | "minute_of" | "second_of" | "week_of" | "quarter_of" | "date_of" | "time_of"
        | "seconds_between" | "months_between" | "years_between" | "add_seconds" | "in_zone"
        | "local_instant" | "abs" | "min" | "max" | "sqrt" | "floor"
        | "ceil" | "round" | "pow" | "log" | "sin" | "cos" | "tan"
        | "toString" | "toInt" | "toFloat" | "trim" | "uppercase" | "lowercase"
        | "split" | "join" | "replace" | "startsWith" | "endsWith"
        | "substring" | "charAt" | "indexOf" | "lastIndexOf" | "repeat" => {
            EffectSet::pure()
        }
        // IO functions
        "args" | "show" | "readLine" | "sleep" | "readFile" | "writeFile"
        | "print" | "println" | "eprintln" | "exit" => {
            EffectSet::io()
        }
        // Alloc functions
        "newSeq" | "newMap" | "newSet" | "copy" | "clone" | "mapOf" | "setOf" | "repeatSeq" => {
            EffectSet::alloc()
        }
        // Default: IO (conservative for unknown natives)
        _ => EffectSet::io(),
    }
}

#[cfg(test)]
mod concurrency_effect_tests {
    //! A concurrency / networking construct is an effectful, often nondeterministic
    //! boundary. The partial evaluator's specialization gate (`partial_eval.rs`) keys
    //! off these effects; if any of them are misclassified as Pure, a function holding
    //! one can be specialized and a static parameter flowing into it dropped, leaving a
    //! dangling free variable. These tests pin the classification at the source.

    use super::EffectEnv;

    #[test]
    fn effects_select_is_not_pure() {
        // A `Select` is a nondeterministic choice — never Pure. The classifier must also
        // see *into* the branch bodies (the `Show` here is an effect of the function).
        let src = "\
## To sel (ch: Int):
    Await the first of:
        Receive x from ch:
            Show x.
        After 1 seconds:
            Show 0.

## Main
    Let ch be a Pipe of Int.
    Launch a task to sel with ch.
";
        let env = EffectEnv::analyze_source(src).expect("parse");
        assert!(
            !env.function_is_pure("sel"),
            "a function whose body is a Select must not be classified Pure"
        );
    }

    #[test]
    fn effects_launch_task_not_pure() {
        let src = "\
## To helper (ch: Int):
    Receive y from ch.
    Show y.

## To launcher (ch: Int):
    Launch a task to helper with ch.

## Main
    Let ch be a Pipe of Int.
    Launch a task to launcher with ch.
";
        let env = EffectEnv::analyze_source(src).expect("parse");
        assert!(
            !env.function_is_pure("launcher"),
            "a function that launches a task is concurrent, not Pure"
        );
    }

    #[test]
    fn effects_try_receive_nondet() {
        let src = "\
## To tryer (ch: Int):
    Try to receive v from ch.

## Main
    Let ch be a Pipe of Int.
    Launch a task to tryer with ch.
";
        let env = EffectEnv::analyze_source(src).expect("parse");
        assert!(
            !env.function_is_pure("tryer"),
            "a non-blocking receive depends on instantaneous buffer state — not Pure"
        );
    }

    #[test]
    fn effects_mount_writes_var() {
        let src = "\
## Main
    Mount counter at \"data/counter.journal\".
    Show counter.
";
        let env = EffectEnv::analyze_source(src).expect("parse");
        assert!(
            env.has_write_to("counter"),
            "Mount binds its variable — the write must be recorded"
        );
    }
}