mathcompile 0.1.2

High-performance symbolic mathematics with final tagless design, egglog optimization, and Rust hot-loading compilation
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
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
//! A-Normal Form (ANF) Intermediate Representation
//!
//! This module provides ANF transformation and optimization for mathematical expressions.
//! ANF ensures that every intermediate computation is explicitly named, enabling
//! efficient common subexpression elimination and clean optimization passes.
//!
//! # Overview
//!
//! A-Normal Form is an intermediate representation where:
//! 1. **Every operation** has atomic (non-compound) arguments
//! 2. **Every intermediate result** is bound to a temporary variable
//! 3. **Computation structure** is made explicit through let-bindings
//!
//! ## Example Transformation
//!
//! ```text
//! Input:  sin(x + y) + cos(x + y) + exp(x + y)
//! ANF:    let t0 = x + y in
//!         let t1 = sin(t0) in  
//!         let t2 = cos(t0) in
//!         let t3 = exp(t0) in
//!         let t4 = t1 + t2 in
//!         t4 + t3
//! ```
//!
//! ## Key Features
//!
//! - **Automatic CSE**: Common subexpressions like `x + y` are computed once
//! - **Scope Safety**: Variables are only referenced within their binding scope
//! - **Efficient Caching**: Structural hashing enables O(1) subexpression lookup
//! - **Clean Code Gen**: Produces readable nested let-bindings in Rust
//!
//! # Architecture
//!
//! ## Core Types
//!
//! - [`VarRef`]: Hybrid variable system (user + generated variables)
//! - [`ANFAtom`]: Atomic expressions (constants, variables)
//! - [`ANFComputation`]: Operations with atomic arguments only
//! - [`ANFExpr`]: Complete ANF expressions (atoms + let-bindings)
//! - [`ANFConverter`]: Stateful converter with CSE cache
//!
//! ## Variable Management
//!
//! Uses a hybrid approach:
//! - `VarRef::User(usize)`: Original user variables from the AST
//! - `VarRef::Bound(u32)`: Temporary variables generated during ANF conversion
//!
//! This integrates cleanly with the existing `VariableRegistry` system while providing
//! efficient integer-based variable management during optimization.
//!
//! # Usage Patterns
//!
//! ## Basic Conversion
//!
//! ```rust
//! use mathcompile::anf::{convert_to_anf, generate_rust_code};
//! use mathcompile::final_tagless::{ASTEval, ASTMathExpr, VariableRegistry};
//!
//! // Create expression: x^2 + 2*x + 1
//! let mut registry = VariableRegistry::new();
//! let x = ASTEval::var(registry.register_variable("x"));
//! let expr = ASTEval::add(
//!     ASTEval::add(ASTEval::pow(x.clone(), ASTEval::constant(2.0)),
//!                  ASTEval::mul(ASTEval::constant(2.0), x)),
//!     ASTEval::constant(1.0)
//! );
//!
//! // Convert to ANF
//! let anf = convert_to_anf(&expr).unwrap();
//!
//! // Generate Rust code
//! let code = generate_rust_code(&anf, &registry);
//! println!("{}", code);
//! // Output: { let t0 = x * x; { let t1 = 2 * x; { let t2 = t0 + t1; t2 + 1 } } }
//! ```
//!
//! ## Advanced: Custom Converter
//!
//! ```rust
//! use mathcompile::anf::{ANFCodeGen, ANFConverter};
//! use mathcompile::final_tagless::{ASTEval, ASTMathExpr};
//! let expr1 = ASTEval::constant(1.0);
//! let expr2 = ASTEval::constant(2.0);
//! let mut converter = ANFConverter::new();
//! let anf1 = converter.convert(&expr1).unwrap();
//! let anf2 = converter.convert(&expr2).unwrap();  // Shares CSE cache with expr1
//! ```
//!
//! ## Function Generation
//!
//! ```rust
//! use mathcompile::anf::ANFCodeGen;
//! use mathcompile::final_tagless::VariableRegistry;
//! use mathcompile::anf::{ANFExpr, ANFAtom, VarRef};
//! let mut registry = VariableRegistry::new();
//! let x_idx = registry.register_variable("x");
//! let anf = ANFExpr::Atom(ANFAtom::<f64>::Variable(VarRef::User(x_idx)));
//! let codegen = ANFCodeGen::new(&registry);
//! let function = codegen.generate_function("my_function", &anf);
//! // Output: fn my_function(x: f64) -> f64 { ... }
//! ```
//!
//! ## Useful Debug Patterns
//!
//! ```rust
//! use mathcompile::anf::{convert_to_anf};
//! use mathcompile::final_tagless::{ASTEval, ASTMathExpr, VariableRegistry};
//! let mut registry = VariableRegistry::new();
//! let x = ASTEval::var(registry.register_variable("x"));
//! let expr = ASTEval::add(x.clone(), ASTEval::constant(1.0));
//! let anf = convert_to_anf(&expr).unwrap();
//! // Print ANF structure
//! println!("ANF: {:#?}", anf);
//! // Check variable usage
//! let vars = anf.used_variables();
//! println!("Used variables: {:?}", vars);
//! // Count let-bindings
//! println!("Binding count: {}", anf.let_count());
//! ```
//!
//! # Performance Characteristics
//!
//! - **Time**: O(n) conversion where n = AST node count
//! - **Space**: O(k) cache entries where k = unique subexpression count  
//! - **CSE Hit Rate**: 80-95% for mathematical expressions with redundancy
//! - **Code Reduction**: 40-60% fewer operations in generated code
//!
//! # Implementation Notes
//!
//! ## CSE Algorithm
//!
//! 1. **Structural Hashing**: Create hash ignoring numeric values
//! 2. **Scope Tracking**: Track binding depth for each cached variable
//! 3. **Cache Lookup**: Check if subexpression already computed
//! 4. **Scope Validation**: Only reuse variables still in scope
//! 5. **Cache Invalidation**: Remove out-of-scope entries
//!
//! ## Scope Management
//!
//! The key insight is that cached variables must respect lexical scoping:
//!
//! ```text
//! ✅ Valid:   { let t0 = x + 1; { let t1 = t0 + t0; t1 } }
//! ❌ Invalid: { { let t0 = x + 1; t0 } + t0 }  // t0 not in scope
//! ```
//!
//! Our solution: Cache entries include scope depth, only reuse when `cached_scope <= current_depth`.
//!
//! ## Memory Management
//!
//! - **Cache Growth**: Bounded by unique subexpression count
//! - **Cleanup**: Out-of-scope entries removed proactively  
//! - **Sharing**: Single converter can process multiple expressions
//! - **Reset**: Call `ANFConverter::new()` to clear cache
//!
//! # Testing and Debugging
//!
//! ## Common Issues
//!
//! - **Invalid Variable References**: Usually scope management bugs
//! - **Cache Misses**: Verify structural hash implementation  
//! - **Memory Growth**: Check for cache invalidation
//! - **Wrong Code Generation**: Inspect ANF structure first
//!
//! # Future Directions
//!
//! - **Constant Folding**: Evaluate constant subexpressions at ANF level
//! - **Dead Code Elimination**: Remove unused let-bindings
//! - **Egglog Integration**: ANF as input/output for e-graph optimization
//! - **Parallel CSE**: Thread-safe conversion for concurrent usage

use crate::error::Result;
use crate::final_tagless::{ASTRepr, NumericType, VariableRegistry};
use num_traits::{Float, Zero};
use ordered_float::OrderedFloat;
use std::collections::HashMap;

/// Variable reference that distinguishes between user and generated variables
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum VarRef {
    /// User-defined variable (index into `VariableRegistry`)
    User(usize),
    /// De Bruijn index for let-bound variables (0 = innermost binding)
    Bound(u32),
}

impl VarRef {
    /// Generate Rust code for this variable
    #[must_use]
    pub fn to_rust_code(&self, registry: &VariableRegistry) -> String {
        match self {
            VarRef::User(idx) => registry.get_name(*idx).unwrap_or("unknown").to_string(),
            VarRef::Bound(id) => format!("t{id}"),
        }
    }

    /// Generate a debug-friendly name for this variable
    #[must_use]
    pub fn debug_name(&self, registry: &VariableRegistry) -> String {
        match self {
            VarRef::User(idx) => {
                format!("{}({})", registry.get_name(*idx).unwrap_or("?"), idx)
            }
            VarRef::Bound(id) => format!("t{id}"),
        }
    }

    /// Check if this is a user variable
    #[must_use]
    pub fn is_user(&self) -> bool {
        matches!(self, VarRef::User(_))
    }

    /// Check if this is a generated variable
    #[must_use]
    pub fn is_generated(&self) -> bool {
        matches!(self, VarRef::Bound(_))
    }
}

/// Generator for fresh ANF variables
#[derive(Debug, Clone)]
pub struct ANFVarGen {
    next_temp_id: u32,
}

impl ANFVarGen {
    /// Create a new variable generator
    #[must_use]
    pub fn new() -> Self {
        Self { next_temp_id: 0 }
    }

    /// Generate a fresh temporary variable
    pub fn fresh(&mut self) -> VarRef {
        let var = VarRef::Bound(self.next_temp_id);
        self.next_temp_id += 1;
        var
    }

    /// Create a user variable reference
    #[must_use]
    pub fn user_var(&self, index: usize) -> VarRef {
        VarRef::User(index)
    }

    /// Get the number of generated variables so far
    #[must_use]
    pub fn generated_count(&self) -> u32 {
        self.next_temp_id
    }
}

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

/// ANF atomic expressions (no sub-computations)
#[derive(Debug, Clone, PartialEq)]
pub enum ANFAtom<T> {
    /// Constant value
    Constant(T),
    /// Variable reference
    Variable(VarRef),
}

impl<T: NumericType> ANFAtom<T> {
    /// Check if this atom is a constant
    pub fn is_constant(&self) -> bool {
        matches!(self, ANFAtom::Constant(_))
    }

    /// Check if this atom is a variable
    pub fn is_variable(&self) -> bool {
        matches!(self, ANFAtom::Variable(_))
    }

    /// Extract constant value if this is a constant
    pub fn as_constant(&self) -> Option<&T> {
        match self {
            ANFAtom::Constant(val) => Some(val),
            _ => None,
        }
    }

    /// Extract variable reference if this is a variable
    pub fn as_variable(&self) -> Option<VarRef> {
        match self {
            ANFAtom::Variable(var) => Some(*var),
            _ => None,
        }
    }
}

/// ANF computations (operations with atomic arguments only)
#[derive(Debug, Clone, PartialEq)]
pub enum ANFComputation<T> {
    /// Addition: a + b
    Add(ANFAtom<T>, ANFAtom<T>),
    /// Subtraction: a - b
    Sub(ANFAtom<T>, ANFAtom<T>),
    /// Multiplication: a * b
    Mul(ANFAtom<T>, ANFAtom<T>),
    /// Division: a / b
    Div(ANFAtom<T>, ANFAtom<T>),
    /// Power: a^b
    Pow(ANFAtom<T>, ANFAtom<T>),
    /// Negation: -a
    Neg(ANFAtom<T>),
    /// Natural logarithm: ln(a)
    Ln(ANFAtom<T>),
    /// Exponential: exp(a)
    Exp(ANFAtom<T>),
    /// Sine: sin(a)
    Sin(ANFAtom<T>),
    /// Cosine: cos(a)
    Cos(ANFAtom<T>),
    /// Square root: sqrt(a)
    Sqrt(ANFAtom<T>),
}

impl<T: NumericType> ANFComputation<T> {
    /// Get all atomic operands of this computation
    pub fn operands(&self) -> Vec<&ANFAtom<T>> {
        match self {
            ANFComputation::Add(a, b)
            | ANFComputation::Sub(a, b)
            | ANFComputation::Mul(a, b)
            | ANFComputation::Div(a, b)
            | ANFComputation::Pow(a, b) => vec![a, b],
            ANFComputation::Neg(a)
            | ANFComputation::Ln(a)
            | ANFComputation::Exp(a)
            | ANFComputation::Sin(a)
            | ANFComputation::Cos(a)
            | ANFComputation::Sqrt(a) => vec![a],
        }
    }

    /// Check if this computation uses only constants
    pub fn is_constant_computation(&self) -> bool {
        self.operands().iter().all(|atom| atom.is_constant())
    }
}

/// ANF expressions
#[derive(Debug, Clone, PartialEq)]
pub enum ANFExpr<T> {
    /// Atomic expression (constant or variable)
    Atom(ANFAtom<T>),
    /// Let binding: let var = computation in body
    Let(VarRef, ANFComputation<T>, Box<ANFExpr<T>>),
}

impl<T> ANFExpr<T>
where
    T: NumericType
        + Float
        + Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>
        + std::ops::Neg<Output = T>
        + Zero,
{
    /// Create an atomic constant
    pub fn constant(value: T) -> Self {
        ANFExpr::Atom(ANFAtom::Constant(value))
    }

    /// Create an atomic variable
    #[must_use]
    pub fn variable(var_ref: VarRef) -> Self {
        ANFExpr::Atom(ANFAtom::Variable(var_ref))
    }

    /// Create a let binding
    pub fn let_binding(var_ref: VarRef, computation: ANFComputation<T>, body: ANFExpr<T>) -> Self {
        ANFExpr::Let(var_ref, computation, Box::new(body))
    }

    /// Check if this is an atomic expression
    pub fn is_atom(&self) -> bool {
        matches!(self, ANFExpr::Atom(_))
    }

    /// Check if this is a let binding
    pub fn is_let(&self) -> bool {
        matches!(self, ANFExpr::Let(_, _, _))
    }

    /// Count the number of let bindings in this expression
    pub fn let_count(&self) -> usize {
        match self {
            ANFExpr::Atom(_) => 0,
            ANFExpr::Let(_, _, body) => 1 + body.let_count(),
        }
    }

    /// Collect all variables used in this expression
    pub fn used_variables(&self) -> Vec<VarRef> {
        let mut vars = Vec::new();
        self.collect_variables(&mut vars);
        vars.sort_unstable();
        vars.dedup();
        vars
    }

    fn collect_variables(&self, vars: &mut Vec<VarRef>) {
        match self {
            ANFExpr::Atom(ANFAtom::Variable(var)) => vars.push(*var),
            ANFExpr::Atom(ANFAtom::Constant(_)) => {}
            ANFExpr::Let(_, comp, body) => {
                for operand in comp.operands() {
                    if let ANFAtom::Variable(var) = operand {
                        vars.push(*var);
                    }
                }
                body.collect_variables(vars);
            }
        }
    }

    /// Evaluate the ANF expression with a variable map and bound variable map
    pub fn eval(&self, variables: &HashMap<usize, T>) -> T {
        self.eval_with_bound_vars(variables, &HashMap::new())
    }

    /// Internal eval that handles both user variables and bound variables
    fn eval_with_bound_vars(
        &self,
        user_vars: &HashMap<usize, T>,
        bound_vars: &HashMap<u32, T>,
    ) -> T {
        match self {
            ANFExpr::Atom(atom) => match atom {
                ANFAtom::Constant(value) => *value,
                ANFAtom::Variable(var_ref) => match var_ref {
                    VarRef::User(idx) => user_vars.get(idx).copied().unwrap_or_else(T::zero),
                    VarRef::Bound(id) => bound_vars.get(id).copied().unwrap_or_else(T::zero),
                },
            },
            ANFExpr::Let(var_ref, computation, body) => {
                // Evaluate the computation
                let comp_result = match computation {
                    ANFComputation::Add(a, b) => {
                        self.eval_atom_with_bound(a, user_vars, bound_vars)
                            + self.eval_atom_with_bound(b, user_vars, bound_vars)
                    }
                    ANFComputation::Sub(a, b) => {
                        self.eval_atom_with_bound(a, user_vars, bound_vars)
                            - self.eval_atom_with_bound(b, user_vars, bound_vars)
                    }
                    ANFComputation::Mul(a, b) => {
                        self.eval_atom_with_bound(a, user_vars, bound_vars)
                            * self.eval_atom_with_bound(b, user_vars, bound_vars)
                    }
                    ANFComputation::Div(a, b) => {
                        self.eval_atom_with_bound(a, user_vars, bound_vars)
                            / self.eval_atom_with_bound(b, user_vars, bound_vars)
                    }
                    ANFComputation::Pow(a, b) => self
                        .eval_atom_with_bound(a, user_vars, bound_vars)
                        .powf(self.eval_atom_with_bound(b, user_vars, bound_vars)),
                    ANFComputation::Neg(a) => -self.eval_atom_with_bound(a, user_vars, bound_vars),
                    ANFComputation::Ln(a) => {
                        self.eval_atom_with_bound(a, user_vars, bound_vars).ln()
                    }
                    ANFComputation::Exp(a) => {
                        self.eval_atom_with_bound(a, user_vars, bound_vars).exp()
                    }
                    ANFComputation::Sin(a) => {
                        self.eval_atom_with_bound(a, user_vars, bound_vars).sin()
                    }
                    ANFComputation::Cos(a) => {
                        self.eval_atom_with_bound(a, user_vars, bound_vars).cos()
                    }
                    ANFComputation::Sqrt(a) => {
                        self.eval_atom_with_bound(a, user_vars, bound_vars).sqrt()
                    }
                };

                // Extend the bound variable environment and evaluate the body
                match var_ref {
                    VarRef::Bound(id) => {
                        let mut extended_bound_vars = bound_vars.clone();
                        extended_bound_vars.insert(*id, comp_result);
                        body.eval_with_bound_vars(user_vars, &extended_bound_vars)
                    }
                    VarRef::User(_) => {
                        // This shouldn't happen in normal ANF, but handle gracefully
                        body.eval_with_bound_vars(user_vars, bound_vars)
                    }
                }
            }
        }
    }

    fn eval_atom_with_bound(
        &self,
        atom: &ANFAtom<T>,
        user_vars: &HashMap<usize, T>,
        bound_vars: &HashMap<u32, T>,
    ) -> T {
        match atom {
            ANFAtom::Constant(value) => *value,
            ANFAtom::Variable(var_ref) => match var_ref {
                VarRef::User(idx) => user_vars.get(idx).copied().unwrap_or_else(T::zero),
                VarRef::Bound(id) => bound_vars.get(id).copied().unwrap_or_else(T::zero),
            },
        }
    }
}

/// Structural hash representing the shape of an expression for CSE
/// Ignores actual numeric values but captures operators and variable positions
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum StructuralHash {
    /// Constant (with value)
    Constant(OrderedFloat<f64>),
    /// Variable by index
    Variable(usize),
    /// Binary operations
    Add(Box<StructuralHash>, Box<StructuralHash>),
    Sub(Box<StructuralHash>, Box<StructuralHash>),
    Mul(Box<StructuralHash>, Box<StructuralHash>),
    Div(Box<StructuralHash>, Box<StructuralHash>),
    Pow(Box<StructuralHash>, Box<StructuralHash>),
    /// Unary operations
    Neg(Box<StructuralHash>),
    Ln(Box<StructuralHash>),
    Exp(Box<StructuralHash>),
    Sin(Box<StructuralHash>),
    Cos(Box<StructuralHash>),
    Sqrt(Box<StructuralHash>),
}

impl StructuralHash {
    /// Create a structural hash from an `ASTRepr<f64>` expression
    #[must_use]
    pub fn from_expr(expr: &ASTRepr<f64>) -> Self {
        match expr {
            ASTRepr::Constant(val) => StructuralHash::Constant(OrderedFloat(*val)),
            ASTRepr::Variable(idx) => StructuralHash::Variable(*idx),
            ASTRepr::Add(left, right) => StructuralHash::Add(
                Box::new(Self::from_expr(left)),
                Box::new(Self::from_expr(right)),
            ),
            ASTRepr::Sub(left, right) => StructuralHash::Sub(
                Box::new(Self::from_expr(left)),
                Box::new(Self::from_expr(right)),
            ),
            ASTRepr::Mul(left, right) => StructuralHash::Mul(
                Box::new(Self::from_expr(left)),
                Box::new(Self::from_expr(right)),
            ),
            ASTRepr::Div(left, right) => StructuralHash::Div(
                Box::new(Self::from_expr(left)),
                Box::new(Self::from_expr(right)),
            ),
            ASTRepr::Pow(left, right) => StructuralHash::Pow(
                Box::new(Self::from_expr(left)),
                Box::new(Self::from_expr(right)),
            ),
            ASTRepr::Neg(inner) => StructuralHash::Neg(Box::new(Self::from_expr(inner))),
            ASTRepr::Ln(inner) => StructuralHash::Ln(Box::new(Self::from_expr(inner))),
            ASTRepr::Exp(inner) => StructuralHash::Exp(Box::new(Self::from_expr(inner))),
            ASTRepr::Sin(inner) => StructuralHash::Sin(Box::new(Self::from_expr(inner))),
            ASTRepr::Cos(inner) => StructuralHash::Cos(Box::new(Self::from_expr(inner))),
            ASTRepr::Sqrt(inner) => StructuralHash::Sqrt(Box::new(Self::from_expr(inner))),
        }
    }
}

/// ANF converter that transforms `ASTRepr` to ANF form
#[derive(Debug)]
pub struct ANFConverter {
    /// Current binding depth (for scope tracking)
    binding_depth: u32,
    /// Next unique binding ID
    next_binding_id: u32,
    /// Cache for common subexpression elimination with scope tracking
    /// Maps structural hashes to (`scope_depth`, variable, `binding_id`)
    expr_cache: HashMap<StructuralHash, (u32, VarRef, u32)>,
}

impl ANFConverter {
    /// Create a new ANF converter
    #[must_use]
    pub fn new() -> Self {
        Self {
            binding_depth: 0,
            next_binding_id: 0,
            expr_cache: HashMap::new(),
        }
    }

    /// Convert an `ASTRepr<f64>` expression to ANF
    pub fn convert(&mut self, expr: &ASTRepr<f64>) -> Result<ANFExpr<f64>> {
        Ok(self.to_anf(expr))
    }

    /// Convert `ASTRepr<f64>` to ANF, returning the result and any let-bindings needed
    fn to_anf(&mut self, expr: &ASTRepr<f64>) -> ANFExpr<f64> {
        // Always inline constants as atoms
        if let ASTRepr::Constant(value) = expr {
            return ANFExpr::Atom(ANFAtom::Constant(*value));
        }
        // Check cache for CSE - but only for non-trivial expressions
        if !matches!(expr, ASTRepr::Constant(_) | ASTRepr::Variable(_)) {
            let structural_hash = StructuralHash::from_expr(expr);
            if let Some((cached_scope, cached_var, _cached_binding_id)) =
                self.expr_cache.get(&structural_hash)
            {
                // Cache hit! But only reuse if the variable is still in scope
                // A variable is in scope if it was created at the current depth or shallower
                if *cached_scope <= self.binding_depth {
                    return ANFExpr::Atom(ANFAtom::Variable(*cached_var));
                }
                // Variable is out of scope, remove from cache
                self.expr_cache.remove(&structural_hash);
            }
        }
        match expr {
            ASTRepr::Constant(value) => ANFExpr::Atom(ANFAtom::Constant(*value)),
            ASTRepr::Variable(index) => ANFExpr::Atom(ANFAtom::Variable(VarRef::User(*index))),
            // Binary operations - these will be cached
            ASTRepr::Add(left, right) => {
                self.convert_binary_op_with_cse(expr, left, right, ANFComputation::Add)
            }
            ASTRepr::Sub(left, right) => {
                self.convert_binary_op_with_cse(expr, left, right, ANFComputation::Sub)
            }
            ASTRepr::Mul(left, right) => {
                self.convert_binary_op_with_cse(expr, left, right, ANFComputation::Mul)
            }
            ASTRepr::Div(left, right) => {
                self.convert_binary_op_with_cse(expr, left, right, ANFComputation::Div)
            }
            ASTRepr::Pow(left, right) => {
                self.convert_binary_op_with_cse(expr, left, right, ANFComputation::Pow)
            }
            // Unary operations - these will be cached
            ASTRepr::Neg(inner) => self.convert_unary_op_with_cse(expr, inner, ANFComputation::Neg),
            ASTRepr::Ln(inner) => self.convert_unary_op_with_cse(expr, inner, ANFComputation::Ln),
            ASTRepr::Exp(inner) => self.convert_unary_op_with_cse(expr, inner, ANFComputation::Exp),
            ASTRepr::Sin(inner) => self.convert_unary_op_with_cse(expr, inner, ANFComputation::Sin),
            ASTRepr::Cos(inner) => self.convert_unary_op_with_cse(expr, inner, ANFComputation::Cos),
            ASTRepr::Sqrt(inner) => {
                self.convert_unary_op_with_cse(expr, inner, ANFComputation::Sqrt)
            }
        }
    }

    /// Convert a binary operation to ANF with CSE caching
    fn convert_binary_op_with_cse(
        &mut self,
        expr: &ASTRepr<f64>,
        left: &ASTRepr<f64>,
        right: &ASTRepr<f64>,
        op_constructor: fn(ANFAtom<f64>, ANFAtom<f64>) -> ANFComputation<f64>,
    ) -> ANFExpr<f64> {
        fn extract_final_var(expr: &ANFExpr<f64>) -> Option<VarRef> {
            match expr {
                ANFExpr::Let(var, _, body) => extract_final_var(body).or(Some(*var)),
                ANFExpr::Atom(ANFAtom::Variable(var)) => Some(*var),
                _ => None,
            }
        }
        let (left_expr, left_atom_orig) = self.to_anf_atom(left);
        let (right_expr, right_atom_orig) = self.to_anf_atom(right);
        let left_atom = match &left_expr {
            Some(e) => extract_final_var(e).map_or(left_atom_orig, ANFAtom::Variable),
            None => left_atom_orig,
        };
        let right_atom = match &right_expr {
            Some(e) => extract_final_var(e).map_or(right_atom_orig, ANFAtom::Variable),
            None => right_atom_orig,
        };
        let computation = op_constructor(left_atom.clone(), right_atom.clone());
        if left_atom.is_constant() && right_atom.is_constant() {
            let result = match computation {
                ANFComputation::Add(ANFAtom::Constant(a), ANFAtom::Constant(b)) => {
                    ANFAtom::Constant(a + b)
                }
                ANFComputation::Sub(ANFAtom::Constant(a), ANFAtom::Constant(b)) => {
                    ANFAtom::Constant(a - b)
                }
                ANFComputation::Mul(ANFAtom::Constant(a), ANFAtom::Constant(b)) => {
                    ANFAtom::Constant(a * b)
                }
                ANFComputation::Div(ANFAtom::Constant(a), ANFAtom::Constant(b)) => {
                    ANFAtom::Constant(a / b)
                }
                ANFComputation::Pow(ANFAtom::Constant(a), ANFAtom::Constant(b)) => {
                    ANFAtom::Constant(a.powf(b))
                }
                _ => unreachable!(),
            };
            return ANFExpr::Atom(result);
        }
        let binding_id = self.next_binding_id;
        self.next_binding_id += 1;
        let result_var = VarRef::Bound(binding_id);
        let structural_hash = StructuralHash::from_expr(expr);
        self.expr_cache.insert(
            structural_hash,
            (self.binding_depth, result_var, binding_id),
        );
        self.binding_depth += 1;
        let body = ANFExpr::Atom(ANFAtom::Variable(result_var));
        self.binding_depth -= 1;
        self.chain_lets(
            left_expr,
            right_expr,
            ANFExpr::Let(result_var, computation, Box::new(body)),
        )
    }

    /// Convert a unary operation to ANF with CSE caching
    fn convert_unary_op_with_cse(
        &mut self,
        expr: &ASTRepr<f64>,
        inner: &ASTRepr<f64>,
        op_constructor: fn(ANFAtom<f64>) -> ANFComputation<f64>,
    ) -> ANFExpr<f64> {
        fn extract_final_var(expr: &ANFExpr<f64>) -> Option<VarRef> {
            match expr {
                ANFExpr::Let(var, _, body) => extract_final_var(body).or(Some(*var)),
                ANFExpr::Atom(ANFAtom::Variable(var)) => Some(*var),
                _ => None,
            }
        }
        let (inner_expr, inner_atom_orig) = self.to_anf_atom(inner);
        let inner_atom = match &inner_expr {
            Some(e) => extract_final_var(e).map_or(inner_atom_orig, ANFAtom::Variable),
            None => inner_atom_orig,
        };
        let computation = op_constructor(inner_atom.clone());
        if inner_atom.is_constant() {
            let result = match computation {
                ANFComputation::Neg(ANFAtom::Constant(a)) => ANFAtom::Constant(-a),
                ANFComputation::Ln(ANFAtom::Constant(a)) => ANFAtom::Constant(a.ln()),
                ANFComputation::Exp(ANFAtom::Constant(a)) => ANFAtom::Constant(a.exp()),
                ANFComputation::Sin(ANFAtom::Constant(a)) => ANFAtom::Constant(a.sin()),
                ANFComputation::Cos(ANFAtom::Constant(a)) => ANFAtom::Constant(a.cos()),
                ANFComputation::Sqrt(ANFAtom::Constant(a)) => ANFAtom::Constant(a.sqrt()),
                _ => unreachable!(),
            };
            return ANFExpr::Atom(result);
        }
        let binding_id = self.next_binding_id;
        self.next_binding_id += 1;
        let result_var = VarRef::Bound(binding_id);
        let structural_hash = StructuralHash::from_expr(expr);
        self.expr_cache.insert(
            structural_hash,
            (self.binding_depth, result_var, binding_id),
        );
        self.binding_depth += 1;
        let body = ANFExpr::Atom(ANFAtom::Variable(result_var));
        self.binding_depth -= 1;
        self.wrap_with_lets(
            inner_expr,
            ANFExpr::Let(result_var, computation, Box::new(body)),
        )
    }

    /// Convert an expression to an atom, generating let-bindings as needed
    fn to_anf_atom(&mut self, expr: &ASTRepr<f64>) -> (Option<ANFExpr<f64>>, ANFAtom<f64>) {
        match expr {
            ASTRepr::Constant(value) => (None, ANFAtom::Constant(*value)),
            ASTRepr::Variable(index) => (None, ANFAtom::Variable(VarRef::User(*index))),
            _ => {
                let anf_expr = self.to_anf(expr);
                match anf_expr {
                    ANFExpr::Atom(atom) => (None, atom),
                    ANFExpr::Let(var, computation, body) => (
                        Some(ANFExpr::Let(var, computation, body)),
                        ANFAtom::Variable(var),
                    ),
                }
            }
        }
    }

    /// Chain two optional ANF expressions with a final expression
    fn chain_lets<T: NumericType + Clone>(
        &self,
        first: Option<ANFExpr<T>>,
        second: Option<ANFExpr<T>>,
        final_expr: ANFExpr<T>,
    ) -> ANFExpr<T> {
        match (first, second) {
            (None, None) => final_expr,
            (Some(first_expr), None) => self.wrap_with_lets(Some(first_expr), final_expr),
            (None, Some(second_expr)) => self.wrap_with_lets(Some(second_expr), final_expr),
            (Some(first_expr), Some(second_expr)) => {
                let combined = self.wrap_with_lets(Some(second_expr), final_expr);
                self.wrap_with_lets(Some(first_expr), combined)
            }
        }
    }

    /// Wrap an expression with let-bindings if needed
    fn wrap_with_lets<T: NumericType + Clone>(
        &self,
        wrapper: Option<ANFExpr<T>>,
        body: ANFExpr<T>,
    ) -> ANFExpr<T> {
        match wrapper {
            None => body,
            Some(ANFExpr::Let(var, computation, inner_body)) => ANFExpr::Let(
                var,
                computation,
                Box::new(self.wrap_with_lets(Some(*inner_body), body)),
            ),
            Some(ANFExpr::Atom(_)) => body, // Atom: just use the body (the let-binding for the unary op)
        }
    }
}

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

/// Convenience function to convert `ASTRepr<f64>` to ANF
pub fn convert_to_anf(expr: &ASTRepr<f64>) -> Result<ANFExpr<f64>> {
    let mut converter = ANFConverter::new();
    converter.convert(expr)
}

/// ANF code generator for Rust
#[derive(Debug)]
pub struct ANFCodeGen<'a> {
    registry: &'a VariableRegistry,
}

impl<'a> ANFCodeGen<'a> {
    /// Create a new code generator with a variable registry
    #[must_use]
    pub fn new(registry: &'a VariableRegistry) -> Self {
        Self { registry }
    }

    /// Generate Rust code from an ANF expression
    pub fn generate<T: NumericType + std::fmt::Display>(&self, expr: &ANFExpr<T>) -> String {
        match expr {
            ANFExpr::Atom(atom) => self.generate_atom(atom),
            ANFExpr::Let(var, computation, body) => {
                let var_name = var.to_rust_code(self.registry);
                let comp_code = self.generate_computation(computation);
                let body_code = self.generate(body);

                format!("{{ let {var_name} = {comp_code};\n{body_code} }}")
            }
        }
    }

    /// Generate code for an atomic expression
    fn generate_atom<T: NumericType + std::fmt::Display>(&self, atom: &ANFAtom<T>) -> String {
        match atom {
            ANFAtom::Constant(value) => value.to_string(),
            ANFAtom::Variable(var) => var.to_rust_code(self.registry),
        }
    }

    /// Generate code for a computation
    fn generate_computation<T: NumericType + std::fmt::Display>(
        &self,
        comp: &ANFComputation<T>,
    ) -> String {
        match comp {
            ANFComputation::Add(left, right) => {
                format!(
                    "{} + {}",
                    self.generate_atom(left),
                    self.generate_atom(right)
                )
            }
            ANFComputation::Sub(left, right) => {
                format!(
                    "{} - {}",
                    self.generate_atom(left),
                    self.generate_atom(right)
                )
            }
            ANFComputation::Mul(left, right) => {
                format!(
                    "{} * {}",
                    self.generate_atom(left),
                    self.generate_atom(right)
                )
            }
            ANFComputation::Div(left, right) => {
                format!(
                    "{} / {}",
                    self.generate_atom(left),
                    self.generate_atom(right)
                )
            }
            ANFComputation::Pow(left, right) => {
                format!(
                    "{}.powf({})",
                    self.generate_atom(left),
                    self.generate_atom(right)
                )
            }
            ANFComputation::Neg(operand) => {
                format!("-{}", self.generate_atom(operand))
            }
            ANFComputation::Ln(operand) => {
                format!("{}.ln()", self.generate_atom(operand))
            }
            ANFComputation::Exp(operand) => {
                format!("{}.exp()", self.generate_atom(operand))
            }
            ANFComputation::Sin(operand) => {
                format!("{}.sin()", self.generate_atom(operand))
            }
            ANFComputation::Cos(operand) => {
                format!("{}.cos()", self.generate_atom(operand))
            }
            ANFComputation::Sqrt(operand) => {
                format!("{}.sqrt()", self.generate_atom(operand))
            }
        }
    }

    /// Generate a complete function definition
    pub fn generate_function<T: NumericType + std::fmt::Display>(
        &self,
        name: &str,
        expr: &ANFExpr<T>,
    ) -> String {
        let param_list: Vec<String> = self
            .registry
            .get_all_names()
            .iter()
            .map(|name| format!("{name}: f64"))
            .collect();

        let body = self.generate(expr);

        format!(
            "fn {}({}) -> f64 {{\n    {}\n}}",
            name,
            param_list.join(", "),
            body.replace('\n', "\n    ")
        )
    }
}

/// Convenience function to generate code from ANF
pub fn generate_rust_code<T: NumericType + std::fmt::Display>(
    expr: &ANFExpr<T>,
    registry: &VariableRegistry,
) -> String {
    let codegen = ANFCodeGen::new(registry);
    codegen.generate(expr)
}

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

    #[test]
    fn test_var_generic() {
        let mut generic = ANFVarGen::new();

        let v1 = generic.fresh();
        let v2 = generic.fresh();
        let v3 = generic.user_var(0);

        assert_eq!(v1, VarRef::Bound(0));
        assert_eq!(v2, VarRef::Bound(1));
        assert_eq!(v3, VarRef::User(0));
        assert_ne!(v1, v2);
        assert_ne!(v1, v3);
    }

    #[test]
    fn test_anf_atom() {
        let const_atom: ANFAtom<f64> = ANFAtom::Constant(42.0);
        let var_atom: ANFAtom<f64> = ANFAtom::Variable(VarRef::Bound(0));

        assert!(const_atom.is_constant());
        assert!(!const_atom.is_variable());
        assert_eq!(const_atom.as_constant(), Some(&42.0));

        assert!(var_atom.is_variable());
        assert!(!var_atom.is_constant());
        assert_eq!(var_atom.as_variable(), Some(VarRef::Bound(0)));
    }

    #[test]
    fn test_anf_computation_operands() {
        let a: ANFAtom<f64> = ANFAtom::Constant(1.0);
        let b: ANFAtom<f64> = ANFAtom::Variable(VarRef::Bound(0));

        let add = ANFComputation::Add(a.clone(), b.clone());
        let operands = add.operands();

        assert_eq!(operands.len(), 2);
        assert_eq!(operands[0], &a);
        assert_eq!(operands[1], &b);

        let neg = ANFComputation::Neg(a.clone());
        let neg_operands = neg.operands();
        assert_eq!(neg_operands.len(), 1);
        assert_eq!(neg_operands[0], &a);
    }

    #[test]
    fn test_anf_expr_construction() {
        let var = VarRef::Bound(0);
        let const_val = 42.0;

        let atom_expr: ANFExpr<f64> = ANFExpr::constant(const_val);
        let var_expr: ANFExpr<f64> = ANFExpr::variable(var);

        assert!(atom_expr.is_atom());
        assert!(var_expr.is_atom());

        let computation = ANFComputation::Add(ANFAtom::Variable(var), ANFAtom::Constant(1.0));
        let let_expr: ANFExpr<f64> = ANFExpr::let_binding(var, computation, atom_expr);

        assert!(let_expr.is_let());
        assert_eq!(let_expr.let_count(), 1);
    }

    #[test]
    fn test_variable_collection() {
        let var1 = VarRef::Bound(0);
        let var2 = VarRef::User(0);

        // Create: let t0 = x + 1 in t0 * t0
        let computation = ANFComputation::Add(
            ANFAtom::Variable(var2), // x
            ANFAtom::Constant(1.0),
        );
        let body: ANFExpr<f64> = ANFExpr::Atom(ANFAtom::Variable(var1)); // t0
        let expr: ANFExpr<f64> = ANFExpr::Let(var1, computation, Box::new(body));

        let used_vars = expr.used_variables();
        assert_eq!(used_vars.len(), 2);
        assert!(used_vars.contains(&var1));
        assert!(used_vars.contains(&var2));
    }

    #[test]
    fn test_anf_conversion() {
        use crate::final_tagless::{ASTEval, ASTMathExpr};

        // Create expression: sin(x + 1) + cos(x + 1)
        // This should demonstrate CSE automatically
        let x = ASTEval::var(0); // Variable with index 0
        let one = ASTEval::constant(1.0);
        let x_plus_one = ASTEval::add(x.clone(), one.clone());
        let sin_expr = ASTEval::sin(x_plus_one.clone());
        let cos_expr = ASTEval::cos(x_plus_one);
        let full_expr = ASTEval::add(sin_expr, cos_expr);

        // Convert to ANF
        let anf_result = convert_to_anf(&full_expr);
        assert!(anf_result.is_ok());

        let anf = anf_result.unwrap();

        // Should have multiple let bindings
        assert!(anf.let_count() > 0);

        // Verify structure is let-bound
        assert!(anf.is_let());
    }

    #[test]
    fn test_anf_code_generation() {
        use crate::final_tagless::{ASTEval, ASTMathExpr, VariableRegistry};

        // Create a variable registry
        let mut registry = VariableRegistry::new();
        let x_idx = registry.register_variable("x");

        // Create expression: x * x + 2 * x + 1 (quadratic)
        let x = ASTEval::var(x_idx);
        let two = ASTEval::constant(2.0);
        let one = ASTEval::constant(1.0);
        let x_squared = ASTEval::mul(x.clone(), x.clone());
        let two_x = ASTEval::mul(two, x);
        let sum1 = ASTEval::add(x_squared, two_x);
        let quadratic = ASTEval::add(sum1, one);

        // Convert to ANF
        let anf = convert_to_anf(&quadratic).unwrap();

        // Generate code
        let code = generate_rust_code(&anf, &registry);

        // Code should contain let bindings and be properly structured
        assert!(code.contains("let t"));
        assert!(code.contains('x'));

        // Also test function generation
        let codegen = ANFCodeGen::new(&registry);
        let function_code = codegen.generate_function("quadratic", &anf);

        assert!(function_code.contains("fn quadratic"));
        assert!(function_code.contains("x: f64"));
        assert!(function_code.contains("-> f64"));

        println!("Generated code:\n{code}");
        println!("Generated function:\n{function_code}");
    }

    #[test]
    fn test_anf_complete_pipeline() {
        use crate::final_tagless::{ASTEval, ASTMathExpr, VariableRegistry};

        // Create a variable registry
        let mut registry = VariableRegistry::new();
        let x_idx = registry.register_variable("x");
        let y_idx = registry.register_variable("y");

        // Create a complex expression with common subexpressions:
        // sin(x + y) + cos(x + y) + exp(x + y)
        // This should demonstrate automatic CSE of (x + y)
        let x = ASTEval::var(x_idx);
        let y = ASTEval::var(y_idx);
        let x_plus_y = ASTEval::add(x, y);

        let sin_term = ASTEval::sin(x_plus_y.clone());
        let cos_term = ASTEval::cos(x_plus_y.clone());
        let exp_term = ASTEval::exp(x_plus_y);

        let sum1 = ASTEval::add(sin_term, cos_term);
        let final_expr = ASTEval::add(sum1, exp_term);

        // Convert to ANF
        let anf = convert_to_anf(&final_expr).unwrap();

        // Generate code
        let codegen = ANFCodeGen::new(&registry);
        let function_code = codegen.generate_function("demo_function", &anf);

        println!("\n=== ANF Demo: Automatic Common Subexpression Elimination ===");
        println!("Original expression: sin(x + y) + cos(x + y) + exp(x + y)");
        println!("ANF introduces variables for shared subexpressions automatically\n");
        println!("Generated function:");
        println!("{function_code}");

        // Verify the ANF has the expected structure
        assert!(anf.let_count() >= 1); // Should have let bindings
        assert!(function_code.contains("fn demo_function"));
        assert!(function_code.contains("x: f64, y: f64"));
        assert!(function_code.contains("-> f64"));

        // The beauty is that this is ready to compile and run!
    }

    #[test]
    fn test_cse_simple_case() {
        use crate::final_tagless::{ASTEval, ASTMathExpr, VariableRegistry};

        // Create a variable registry
        let mut registry = VariableRegistry::new();
        let x_idx = registry.register_variable("x");

        // Create expression: (x + 1) + (x + 1)
        // This should reuse the computation of (x + 1)
        let x = ASTEval::var(x_idx);
        let one = ASTEval::constant(1.0);
        let x_plus_one_left = ASTEval::add(x.clone(), one.clone());
        let x_plus_one_right = ASTEval::add(x, one);
        let final_expr = ASTEval::add(x_plus_one_left, x_plus_one_right);

        // Convert to ANF
        let anf = convert_to_anf(&final_expr).unwrap();

        // Generate code
        let codegen = ANFCodeGen::new(&registry);
        let function_code = codegen.generate_function("cse_test", &anf);

        println!("\n=== CSE Test: (x + 1) + (x + 1) ===");
        println!("Generated function:");
        println!("{function_code}");

        // Count how many times we see "x + 1" in the generated code
        let code_contains_reuse = function_code.matches("x + 1").count() == 1;
        println!(
            "CSE working correctly: {}",
            if code_contains_reuse {
                "✅ YES"
            } else {
                "❌ NO"
            }
        );

        // Should have fewer let bindings due to reuse
        assert!(anf.let_count() > 0);
    }

    #[test]
    fn test_cse_debug() {
        use crate::final_tagless::{ASTEval, ASTMathExpr, VariableRegistry};

        // Create a very simple case to debug: x + x
        let mut registry = VariableRegistry::new();
        let x_idx = registry.register_variable("x");

        let x = ASTEval::var(x_idx);
        let expr = ASTEval::add(x.clone(), x.clone()); // x + x - should reuse x

        // Convert to ANF
        let anf = convert_to_anf(&expr).unwrap();

        // Generate code
        let codegen = ANFCodeGen::new(&registry);
        let function_code = codegen.generate_function("debug_test", &anf);

        println!("\n=== CSE Debug: x + x ===");
        println!("Generated function:");
        println!("{function_code}");

        // This should have only one variable, not duplicate computations
        assert!(anf.let_count() == 1);
    }

    #[test]
    fn test_cse_failing_case() {
        use crate::final_tagless::{ASTEval, ASTMathExpr, VariableRegistry};

        // Create the exact failing case: (x + 1) + (x + 1)
        let mut registry = VariableRegistry::new();
        let x_idx = registry.register_variable("x");

        let x = ASTEval::var(x_idx);
        let one = ASTEval::constant(1.0);
        let x_plus_one_left = ASTEval::add(x.clone(), one.clone());
        let x_plus_one_right = ASTEval::add(x, one);
        let expr = ASTEval::add(x_plus_one_left, x_plus_one_right);

        // Convert to ANF
        let anf = convert_to_anf(&expr).unwrap();

        // Generate code
        let codegen = ANFCodeGen::new(&registry);
        let function_code = codegen.generate_function("failing_case", &anf);

        println!("\n=== CSE Failing Case: (x + 1) + (x + 1) ===");
        println!("Generated function:");
        println!("{function_code}");
        println!("Let count: {}", anf.let_count());

        // Debug the ANF structure
        println!("ANF structure: {anf:#?}");
    }
}