tensorlogic-compiler 0.1.0

Compiler for transforming logic expressions into tensor computation graphs
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
//! Compilation cache for TensorLogic expressions.
//!
//! Provides two complementary caching mechanisms:
//!
//! 1. **[`CompilationCache`]** — A thread-safe, key-based cache that stores compiled
//!    `EinsumGraph` instances keyed by a composite hash of expression structure,
//!    compilation configuration, and domain information. Designed for concurrent use.
//!
//! 2. **[`LruCompilationCache`]** — A single-threaded LRU cache keyed by
//!    [`ExprFingerprint`] (a structural content-address of an expression). Evicts the
//!    least-recently-used entry when capacity is exceeded. Designed for use inside a
//!    [`CachingCompiler`] wrapper.
//!
//! # Choosing the right cache
//!
//! | Scenario | Recommended type |
//! |----------|-----------------|
//! | Single-threaded compilation loop | [`LruCompilationCache`] / [`CachingCompiler`] |
//! | Multi-threaded compilation (shared) | [`CompilationCache`] |
//! | Batch compilation of related exprs | [`CachingCompiler::compile_batch`] |
//!
//! # Example — LRU cache via `CachingCompiler`
//!
//! ```rust
//! use tensorlogic_compiler::cache::{CachingCompiler, CacheStats};
//! use tensorlogic_compiler::compile_to_einsum;
//! use tensorlogic_ir::{TLExpr, Term};
//!
//! let mut compiler = CachingCompiler::new(64, |expr| {
//!     compile_to_einsum(expr).map_err(|e| e.to_string())
//! });
//!
//! let expr = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);
//!
//! let _g1 = compiler.compile(&expr).expect("first compile");
//! let _g2 = compiler.compile(&expr).expect("second compile (cache hit)");
//!
//! assert_eq!(compiler.cache_stats().hits, 1);
//! assert_eq!(compiler.cache_stats().misses, 1);
//! ```

use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, Mutex};

use anyhow::Result;
use tensorlogic_ir::{EinsumGraph, TLExpr};

use crate::config::CompilationConfig;
use crate::CompilerContext;

// ──────────────────────────────────────────────────────────────────────────────
// ExprFingerprint
// ──────────────────────────────────────────────────────────────────────────────

/// A compact fingerprint of a `TLExpr` structure (not values).
///
/// Two expressions with identical structure produce the same fingerprint.
/// Used as a content-addressable cache key in [`LruCompilationCache`] and
/// [`CachingCompiler`].
///
/// The fingerprint is derived from the `Debug` representation of the expression,
/// which is deterministic for the same expression tree.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ExprFingerprint {
    /// Serialised structural representation.
    pub(crate) data: String,
}

impl ExprFingerprint {
    /// Compute a fingerprint from an arbitrary string representation.
    ///
    /// In practice this is called with `format!("{:?}", expr)` so that the
    /// fingerprint captures the full recursive structure of the expression.
    pub fn compute(expr_repr: &str) -> Self {
        ExprFingerprint {
            data: expr_repr.to_string(),
        }
    }
}

impl std::fmt::Display for ExprFingerprint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let preview_len = self.data.len().min(32);
        write!(f, "fp:{}", &self.data[..preview_len])
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// CachedResult  (public, used by LruCompilationCache / CachingCompiler)
// ──────────────────────────────────────────────────────────────────────────────

/// A cached compilation result stored in an [`LruCompilationCache`].
#[derive(Debug, Clone)]
pub struct CachedResult {
    /// The compiled graph.
    pub graph: EinsumGraph,
    /// Number of times this entry was accessed (read) via [`LruCompilationCache::get`].
    pub hit_count: u64,
    /// Approximate memory used by the graph (estimated as `nodes.len() * 256` bytes).
    pub memory_bytes: usize,
}

// ──────────────────────────────────────────────────────────────────────────────
// CacheStats  (shared by both cache types)
// ──────────────────────────────────────────────────────────────────────────────

/// Aggregate statistics for any compilation cache.
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    /// Number of successful cache lookups.
    pub hits: u64,
    /// Number of cache lookups that resulted in a fresh compilation.
    pub misses: u64,
    /// Number of entries that were evicted to make room for new entries.
    pub evictions: u64,
    /// Current number of entries (updated after each insert/evict/clear).
    pub current_entries: usize,
    /// Approximate total memory occupied by all cached graphs (bytes).
    pub total_memory_bytes: usize,
}

impl CacheStats {
    /// Cache hit rate in the range `[0.0, 1.0]`.
    ///
    /// Returns `0.0` when no lookups have been performed yet.
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            self.hits as f64 / total as f64
        }
    }

    /// Total number of cache lookups (hits + misses).
    pub fn total_lookups(&self) -> u64 {
        self.hits + self.misses
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// LruCompilationCache
// ──────────────────────────────────────────────────────────────────────────────

/// LRU compilation cache with configurable capacity.
///
/// Stores compiled `EinsumGraph` instances keyed by [`ExprFingerprint`].
/// When capacity is exceeded the least-recently-used entry is evicted.
///
/// This cache is **not** thread-safe — wrap it in `Arc<Mutex<_>>` or use
/// [`CompilationCache`] if you need concurrent access.
///
/// # Example
///
/// ```rust
/// use tensorlogic_compiler::cache::{LruCompilationCache, ExprFingerprint};
/// use tensorlogic_ir::EinsumGraph;
///
/// let mut cache = LruCompilationCache::new(4);
/// let fp = ExprFingerprint::compute("pred(x)");
/// cache.insert(fp.clone(), EinsumGraph::new());
/// assert!(cache.get(&fp).is_some());
/// ```
pub struct LruCompilationCache {
    /// Maximum number of entries.
    capacity: usize,
    /// The cache storage.
    entries: HashMap<ExprFingerprint, CachedResult>,
    /// LRU order: oldest at the **front**, newest at the **back**.
    lru_order: std::collections::VecDeque<ExprFingerprint>,
    /// Accumulated statistics.
    stats: CacheStats,
}

impl LruCompilationCache {
    /// Create a new LRU cache with the given capacity (minimum 1).
    pub fn new(capacity: usize) -> Self {
        LruCompilationCache {
            capacity: capacity.max(1),
            entries: HashMap::new(),
            lru_order: std::collections::VecDeque::new(),
            stats: CacheStats::default(),
        }
    }

    /// Insert a compiled result for the given fingerprint.
    ///
    /// If the fingerprint already exists the stored graph is updated and the
    /// entry is promoted to the most-recently-used position.
    ///
    /// If the cache is at capacity the least-recently-used entry is evicted
    /// before the new entry is inserted.
    pub fn insert(&mut self, fp: ExprFingerprint, graph: EinsumGraph) {
        // Estimate memory: proportional to node count.
        let memory_bytes = graph.nodes.len() * 256;

        if self.entries.contains_key(&fp) {
            // Update the existing entry in-place.
            if let Some(entry) = self.entries.get_mut(&fp) {
                self.stats.total_memory_bytes = self
                    .stats
                    .total_memory_bytes
                    .saturating_sub(entry.memory_bytes);
                entry.graph = graph;
                entry.memory_bytes = memory_bytes;
                self.stats.total_memory_bytes += memory_bytes;
            }
            // Promote to most-recently-used.
            if let Some(pos) = self.lru_order.iter().position(|x| x == &fp) {
                self.lru_order.remove(pos);
            }
            self.lru_order.push_back(fp);
        } else {
            // Evict the LRU entry when at capacity.
            if self.entries.len() >= self.capacity {
                if let Some(oldest) = self.lru_order.pop_front() {
                    if let Some(evicted) = self.entries.remove(&oldest) {
                        self.stats.total_memory_bytes = self
                            .stats
                            .total_memory_bytes
                            .saturating_sub(evicted.memory_bytes);
                    }
                    self.stats.evictions += 1;
                }
            }
            self.stats.total_memory_bytes += memory_bytes;
            self.lru_order.push_back(fp.clone());
            self.entries.insert(
                fp,
                CachedResult {
                    graph,
                    hit_count: 0,
                    memory_bytes,
                },
            );
        }
        self.stats.current_entries = self.entries.len();
    }

    /// Look up a fingerprint.
    ///
    /// On a hit the entry is promoted to the most-recently-used position,
    /// its `hit_count` is incremented, and a reference to it is returned.
    /// On a miss `None` is returned.
    pub fn get(&mut self, fp: &ExprFingerprint) -> Option<&CachedResult> {
        if self.entries.contains_key(fp) {
            // Promote to most-recently-used.
            if let Some(pos) = self.lru_order.iter().position(|x| x == fp) {
                self.lru_order.remove(pos);
            }
            self.lru_order.push_back(fp.clone());
            // Increment hit counter.
            if let Some(entry) = self.entries.get_mut(fp) {
                entry.hit_count += 1;
            }
            self.stats.hits += 1;
            self.entries.get(fp)
        } else {
            self.stats.misses += 1;
            None
        }
    }

    /// Check if a fingerprint is present **without** updating LRU order or stats.
    pub fn contains(&self, fp: &ExprFingerprint) -> bool {
        self.entries.contains_key(fp)
    }

    /// Remove a specific entry by fingerprint.
    ///
    /// Returns `true` if the entry existed and was removed, `false` otherwise.
    pub fn invalidate(&mut self, fp: &ExprFingerprint) -> bool {
        if let Some(evicted) = self.entries.remove(fp) {
            self.stats.total_memory_bytes = self
                .stats
                .total_memory_bytes
                .saturating_sub(evicted.memory_bytes);
            if let Some(pos) = self.lru_order.iter().position(|x| x == fp) {
                self.lru_order.remove(pos);
            }
            self.stats.current_entries = self.entries.len();
            true
        } else {
            false
        }
    }

    /// Clear all cached entries, resetting memory accounting.
    ///
    /// Statistics counters (hits, misses, evictions) are **not** reset.
    pub fn clear(&mut self) {
        self.entries.clear();
        self.lru_order.clear();
        self.stats.current_entries = 0;
        self.stats.total_memory_bytes = 0;
    }

    /// Reference to the current statistics snapshot.
    pub fn stats(&self) -> &CacheStats {
        &self.stats
    }

    /// Number of cached entries.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns `true` when the cache contains no entries.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// The maximum number of entries this cache can hold before eviction.
    pub fn capacity(&self) -> usize {
        self.capacity
    }
}

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

// ──────────────────────────────────────────────────────────────────────────────
// CachingCompiler
// ──────────────────────────────────────────────────────────────────────────────

/// A compiler wrapper that caches results keyed by expression fingerprint.
///
/// Uses structural fingerprinting of [`TLExpr`] to detect identical expressions.
/// Falls back to fresh compilation on a cache miss and stores the result for
/// subsequent calls.
///
/// # Example
///
/// ```rust
/// use tensorlogic_compiler::cache::CachingCompiler;
/// use tensorlogic_compiler::compile_to_einsum;
/// use tensorlogic_ir::{TLExpr, Term};
///
/// let mut cc = CachingCompiler::new(32, |expr| {
///     compile_to_einsum(expr).map_err(|e| e.to_string())
/// });
///
/// let e = TLExpr::pred("p", vec![Term::var("x")]);
/// let g1 = cc.compile(&e).unwrap();
/// let g2 = cc.compile(&e).unwrap(); // cache hit
///
/// assert_eq!(cc.cache_stats().hits, 1);
/// assert_eq!(g1, g2);
/// ```
/// Type alias for the compile function stored in a [`CachingCompiler`].
type CompileFn =
    Box<dyn Fn(&TLExpr) -> std::result::Result<EinsumGraph, String> + Send + Sync + 'static>;

pub struct CachingCompiler {
    cache: LruCompilationCache,
    compile_fn: CompileFn,
}

impl CachingCompiler {
    /// Create a `CachingCompiler` with a custom compile function and cache capacity.
    ///
    /// # Arguments
    ///
    /// * `capacity` – Maximum number of entries held in the LRU cache.
    /// * `compile_fn` – A closure (or function) that compiles a [`TLExpr`] into an
    ///   [`EinsumGraph`], returning `Err(String)` on failure.
    pub fn new<F>(capacity: usize, compile_fn: F) -> Self
    where
        F: Fn(&TLExpr) -> std::result::Result<EinsumGraph, String> + Send + Sync + 'static,
    {
        CachingCompiler {
            cache: LruCompilationCache::new(capacity),
            compile_fn: Box::new(compile_fn),
        }
    }

    /// Compile an expression, returning the cached result when available.
    ///
    /// # Errors
    ///
    /// Propagates any error produced by the underlying compile function on a cache miss.
    pub fn compile(&mut self, expr: &TLExpr) -> std::result::Result<EinsumGraph, String> {
        let fp = Self::fingerprint(expr);

        if let Some(cached) = self.cache.get(&fp) {
            return Ok(cached.graph.clone());
        }

        let result = (self.compile_fn)(expr)?;
        self.cache.insert(fp, result.clone());
        Ok(result)
    }

    /// Compile multiple expressions in order, sharing the cache across all of them.
    ///
    /// Returns one `Result` per input expression in the same order.
    pub fn compile_batch(
        &mut self,
        exprs: &[TLExpr],
    ) -> Vec<std::result::Result<EinsumGraph, String>> {
        exprs.iter().map(|e| self.compile(e)).collect()
    }

    /// Returns a reference to the current cache statistics.
    pub fn cache_stats(&self) -> &CacheStats {
        self.cache.stats()
    }

    /// Invalidate the cached result for a specific expression.
    ///
    /// Returns `true` if an entry was present and removed.
    pub fn invalidate(&mut self, expr: &TLExpr) -> bool {
        let fp = Self::fingerprint(expr);
        self.cache.invalidate(&fp)
    }

    /// Compute a structural [`ExprFingerprint`] for an expression.
    ///
    /// Two structurally identical expressions will produce equal fingerprints.
    pub fn fingerprint(expr: &TLExpr) -> ExprFingerprint {
        ExprFingerprint::compute(&Self::structural_repr(expr))
    }

    /// Produce a deterministic string representation of an expression's structure.
    ///
    /// This uses the `Debug` implementation of [`TLExpr`] which is deterministic
    /// for the same expression tree. Future enhancements may switch to a custom
    /// canonical serialisation if `Debug` output format changes.
    fn structural_repr(expr: &TLExpr) -> String {
        // `Debug` for TLExpr is stable within a single build and deterministic
        // for identical expression trees, making it a reliable fingerprint source.
        format!("{:?}", expr)
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Legacy thread-safe CompilationCache  (original implementation, retained)
// ──────────────────────────────────────────────────────────────────────────────

/// A hash key for the thread-safe compilation cache.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct CacheKey {
    expr_hash: u64,
    config_hash: u64,
    domain_hash: u64,
}

impl CacheKey {
    fn new(expr: &TLExpr, config: &CompilationConfig, ctx: &CompilerContext) -> Self {
        use std::collections::hash_map::DefaultHasher;

        let mut expr_hasher = DefaultHasher::new();
        format!("{:?}", expr).hash(&mut expr_hasher);
        let expr_hash = expr_hasher.finish();

        let mut config_hasher = DefaultHasher::new();
        format!("{:?}", config).hash(&mut config_hasher);
        let config_hash = config_hasher.finish();

        let mut domain_hasher = DefaultHasher::new();
        for (name, domain) in &ctx.domains {
            name.hash(&mut domain_hasher);
            domain.cardinality.hash(&mut domain_hasher);
        }
        let domain_hash = domain_hasher.finish();

        CacheKey {
            expr_hash,
            config_hash,
            domain_hash,
        }
    }
}

/// Internal cached result for the thread-safe cache.
#[derive(Clone)]
struct ThreadSafeCachedResult {
    graph: EinsumGraph,
    hit_count: usize,
}

/// Thread-safe compilation cache for storing and retrieving compiled expressions.
///
/// Stores compiled `EinsumGraph` instances keyed by a composite hash that includes
/// the expression structure, compilation configuration, and domain information.
/// This cache **is** thread-safe and can be shared across compilation threads.
///
/// When capacity is exceeded the cache evicts the least-frequently-used entry
/// (lowest `hit_count`). For strict LRU eviction use [`LruCompilationCache`] or
/// [`CachingCompiler`] instead.
///
/// # Example
///
/// ```rust
/// use tensorlogic_compiler::{CompilationCache, compile_to_einsum_with_context, CompilerContext};
/// use tensorlogic_ir::{TLExpr, Term};
///
/// let cache = CompilationCache::new(100);
/// let mut ctx = CompilerContext::new();
/// ctx.add_domain("Person", 100);
///
/// let expr = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);
///
/// // First compilation: miss (not in cache)
/// let graph1 = cache.get_or_compile(&expr, &mut ctx, |expr, ctx| {
///     compile_to_einsum_with_context(expr, ctx)
/// }).expect("compile");
///
/// // Second compilation: hit (cached)
/// let graph2 = cache.get_or_compile(&expr, &mut ctx, |expr, ctx| {
///     compile_to_einsum_with_context(expr, ctx)
/// }).expect("compile");
///
/// assert_eq!(graph1, graph2);
/// assert_eq!(cache.stats().hits, 1);
/// ```
pub struct CompilationCache {
    cache: Arc<Mutex<HashMap<CacheKey, ThreadSafeCachedResult>>>,
    max_size: usize,
    stats: Arc<Mutex<CacheStats>>,
}

impl CompilationCache {
    /// Create a new compilation cache with the specified maximum size.
    ///
    /// # Arguments
    ///
    /// * `max_size` – Maximum number of entries to cache.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_compiler::CompilationCache;
    ///
    /// let cache = CompilationCache::new(100);
    /// assert_eq!(cache.max_size(), 100);
    /// ```
    pub fn new(max_size: usize) -> Self {
        Self {
            cache: Arc::new(Mutex::new(HashMap::new())),
            max_size,
            stats: Arc::new(Mutex::new(CacheStats::default())),
        }
    }

    /// Create a cache with the default size of 1 000 entries.
    pub fn default_size() -> Self {
        Self::new(1000)
    }

    /// Maximum number of entries the cache can hold.
    pub fn max_size(&self) -> usize {
        self.max_size
    }

    /// Get or compile an expression.
    ///
    /// On a cache hit the stored result is returned immediately.
    /// On a miss `compile_fn` is called and the result is stored before returning.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_compiler::{CompilationCache, compile_to_einsum_with_context, CompilerContext};
    /// use tensorlogic_ir::{TLExpr, Term};
    ///
    /// let cache = CompilationCache::new(100);
    /// let mut ctx = CompilerContext::new();
    /// ctx.add_domain("Person", 100);
    ///
    /// let expr = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);
    ///
    /// let graph = cache.get_or_compile(&expr, &mut ctx, |expr, ctx| {
    ///     compile_to_einsum_with_context(expr, ctx)
    /// }).expect("compile");
    /// ```
    pub fn get_or_compile<F>(
        &self,
        expr: &TLExpr,
        ctx: &mut CompilerContext,
        compile_fn: F,
    ) -> Result<EinsumGraph>
    where
        F: FnOnce(&TLExpr, &mut CompilerContext) -> Result<EinsumGraph>,
    {
        let key = CacheKey::new(expr, &ctx.config, ctx);

        // Try cache first.
        {
            let mut cache = self
                .cache
                .lock()
                .map_err(|e| anyhow::anyhow!("cache lock poisoned: {}", e))?;
            if let Some(cached) = cache.get_mut(&key) {
                cached.hit_count += 1;
                let mut stats = self
                    .stats
                    .lock()
                    .map_err(|e| anyhow::anyhow!("stats lock poisoned: {}", e))?;
                stats.hits += 1;
                return Ok(cached.graph.clone());
            }
        }

        // Cache miss — compile.
        {
            let mut stats = self
                .stats
                .lock()
                .map_err(|e| anyhow::anyhow!("stats lock poisoned: {}", e))?;
            stats.misses += 1;
        }

        let graph = compile_fn(expr, ctx)?;

        // Store result (evict if necessary).
        {
            let mut cache = self
                .cache
                .lock()
                .map_err(|e| anyhow::anyhow!("cache lock poisoned: {}", e))?;

            if cache.len() >= self.max_size {
                // Evict least-frequently used entry.
                let min_key = cache
                    .iter()
                    .min_by_key(|(_, v)| v.hit_count)
                    .map(|(k, _)| k.clone());

                if let Some(key_to_evict) = min_key {
                    cache.remove(&key_to_evict);
                    let mut stats = self
                        .stats
                        .lock()
                        .map_err(|e| anyhow::anyhow!("stats lock poisoned: {}", e))?;
                    stats.evictions += 1;
                }
            }

            cache.insert(
                key,
                ThreadSafeCachedResult {
                    graph: graph.clone(),
                    hit_count: 0,
                },
            );

            let mut stats = self
                .stats
                .lock()
                .map_err(|e| anyhow::anyhow!("stats lock poisoned: {}", e))?;
            stats.current_entries = cache.len();
        }

        Ok(graph)
    }

    /// Current cache statistics snapshot.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_compiler::CompilationCache;
    ///
    /// let cache = CompilationCache::new(100);
    /// let stats = cache.stats();
    /// assert_eq!(stats.hits, 0);
    /// ```
    pub fn stats(&self) -> CacheStats {
        self.stats.lock().map(|g| g.clone()).unwrap_or_default()
    }

    /// Clear all cached entries.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_compiler::CompilationCache;
    ///
    /// let cache = CompilationCache::new(100);
    /// cache.clear();
    /// assert_eq!(cache.stats().current_entries, 0);
    /// ```
    pub fn clear(&self) {
        if let Ok(mut cache) = self.cache.lock() {
            cache.clear();
        }
        if let Ok(mut stats) = self.stats.lock() {
            stats.current_entries = 0;
            stats.total_memory_bytes = 0;
        }
    }

    /// Current number of entries in the cache.
    pub fn len(&self) -> usize {
        self.cache.lock().map(|g| g.len()).unwrap_or(0)
    }

    /// Returns `true` when the cache is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl Default for CompilationCache {
    fn default() -> Self {
        Self::default_size()
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Tests
// ──────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compile_to_einsum_with_context;
    use tensorlogic_ir::Term;

    // ── helpers ────────────────────────────────────────────────────────────────

    fn make_graph(node_count: usize) -> EinsumGraph {
        use tensorlogic_ir::EinsumNode;
        let mut g = EinsumGraph::new();
        for i in 0..node_count {
            let a = g.add_tensor(format!("t{}", i));
            let b = g.add_tensor(format!("u{}", i));
            let c = g.add_tensor(format!("v{}", i));
            g.add_node(EinsumNode::einsum("i,i->i", vec![a, b], vec![c]))
                .ok();
        }
        g
    }

    fn simple_fp(s: &str) -> ExprFingerprint {
        ExprFingerprint::compute(s)
    }

    // ── LruCompilationCache tests ──────────────────────────────────────────────

    /// insert then get returns Some
    #[test]
    fn test_cache_basic_insert_get() {
        let mut cache = LruCompilationCache::new(8);
        let fp = simple_fp("pred(x)");
        cache.insert(fp.clone(), EinsumGraph::new());
        assert!(
            cache.get(&fp).is_some(),
            "entry should be present after insert"
        );
    }

    /// get on empty cache returns None
    #[test]
    fn test_cache_miss() {
        let mut cache = LruCompilationCache::new(8);
        let fp = simple_fp("pred(x)");
        assert!(cache.get(&fp).is_none(), "empty cache must return None");
    }

    /// hit_count increments on each successful get
    #[test]
    fn test_cache_hit_increments_hit_count() {
        let mut cache = LruCompilationCache::new(8);
        let fp = simple_fp("pred(x)");
        cache.insert(fp.clone(), EinsumGraph::new());

        cache.get(&fp);
        cache.get(&fp);

        // hit_count inside the entry should reflect two reads.
        assert!(cache.contains(&fp), "entry must still exist after reads");
        // Obtain hit_count via a final get.
        let entry = cache.get(&fp).expect("entry must be present");
        // Three gets were performed (two above + this one) → hit_count == 3.
        assert_eq!(entry.hit_count, 3, "hit_count should be 3 after three gets");
    }

    /// 1 hit + 1 miss → hit_rate == 0.5
    #[test]
    fn test_cache_stats_hit_rate() {
        let mut cache = LruCompilationCache::new(8);
        let fp = simple_fp("pred(x)");
        cache.insert(fp.clone(), EinsumGraph::new());

        cache.get(&fp); // hit
        cache.get(&simple_fp("missing")); // miss

        let stats = cache.stats();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
        assert!(
            (stats.hit_rate() - 0.5).abs() < f64::EPSILON,
            "hit rate must be 0.5"
        );
    }

    /// capacity=2, three inserts → oldest evicted
    #[test]
    fn test_cache_lru_eviction() {
        let mut cache = LruCompilationCache::new(2);
        let fp1 = simple_fp("a");
        let fp2 = simple_fp("b");
        let fp3 = simple_fp("c");

        cache.insert(fp1.clone(), EinsumGraph::new());
        cache.insert(fp2.clone(), EinsumGraph::new());
        cache.insert(fp3.clone(), EinsumGraph::new()); // should evict fp1

        assert!(
            !cache.contains(&fp1),
            "oldest entry (fp1) must have been evicted"
        );
        assert!(cache.contains(&fp2), "fp2 must still be present");
        assert!(cache.contains(&fp3), "fp3 must be present");
        assert_eq!(cache.len(), 2);
    }

    /// Access the oldest entry so it becomes newest; the next eviction removes the other one
    #[test]
    fn test_cache_lru_access_updates_order() {
        let mut cache = LruCompilationCache::new(2);
        let fp1 = simple_fp("a");
        let fp2 = simple_fp("b");
        let fp3 = simple_fp("c");

        cache.insert(fp1.clone(), EinsumGraph::new());
        cache.insert(fp2.clone(), EinsumGraph::new());

        // Access fp1 → it becomes MRU; fp2 is now LRU.
        cache.get(&fp1);

        // Insert fp3 → fp2 should be evicted (LRU), not fp1.
        cache.insert(fp3.clone(), EinsumGraph::new());

        assert!(cache.contains(&fp1), "fp1 was accessed so it must survive");
        assert!(
            !cache.contains(&fp2),
            "fp2 is LRU after fp1 was accessed; it must be evicted"
        );
        assert!(cache.contains(&fp3), "fp3 must be present");
    }

    /// invalidate removes an entry
    #[test]
    fn test_cache_invalidate() {
        let mut cache = LruCompilationCache::new(8);
        let fp = simple_fp("pred(x)");
        cache.insert(fp.clone(), EinsumGraph::new());

        let removed = cache.invalidate(&fp);
        assert!(removed, "invalidate must return true when entry existed");
        assert!(
            !cache.contains(&fp),
            "entry must be gone after invalidation"
        );
    }

    /// clear empties the cache
    #[test]
    fn test_cache_clear() {
        let mut cache = LruCompilationCache::new(8);
        cache.insert(simple_fp("a"), EinsumGraph::new());
        cache.insert(simple_fp("b"), EinsumGraph::new());

        cache.clear();

        assert!(cache.is_empty(), "cache must be empty after clear");
        assert_eq!(cache.len(), 0);
        assert_eq!(cache.stats().total_memory_bytes, 0);
    }

    /// len / is_empty reflect the actual entry count
    #[test]
    fn test_cache_len_and_is_empty() {
        let mut cache = LruCompilationCache::new(8);
        assert!(cache.is_empty());
        assert_eq!(cache.len(), 0);

        cache.insert(simple_fp("x"), EinsumGraph::new());
        assert!(!cache.is_empty());
        assert_eq!(cache.len(), 1);
    }

    /// capacity() returns the configured value
    #[test]
    fn test_cache_capacity() {
        let cache = LruCompilationCache::new(42);
        assert_eq!(cache.capacity(), 42);
    }

    /// evictions counter is updated correctly
    #[test]
    fn test_cache_eviction_stat() {
        let mut cache = LruCompilationCache::new(2);
        cache.insert(simple_fp("a"), EinsumGraph::new());
        cache.insert(simple_fp("b"), EinsumGraph::new());
        cache.insert(simple_fp("c"), EinsumGraph::new()); // one eviction
        cache.insert(simple_fp("d"), EinsumGraph::new()); // second eviction

        assert_eq!(
            cache.stats().evictions,
            2,
            "two evictions must have occurred"
        );
    }

    /// total_memory_bytes is positive after inserting a non-empty graph
    #[test]
    fn test_cache_memory_estimate() {
        let mut cache = LruCompilationCache::new(8);
        // Graph with 4 nodes → 4 * 256 = 1024 bytes estimated.
        let graph = make_graph(4);
        cache.insert(simple_fp("g"), graph);

        assert!(
            cache.stats().total_memory_bytes > 0,
            "memory estimate must be > 0 for a non-empty graph"
        );
    }

    // ── ExprFingerprint tests ──────────────────────────────────────────────────

    /// Same expression structure → same fingerprint
    #[test]
    fn test_fingerprint_same_for_same_expr() {
        let expr = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);
        let fp1 = CachingCompiler::fingerprint(&expr);
        let fp2 = CachingCompiler::fingerprint(&expr);
        assert_eq!(
            fp1, fp2,
            "identical expressions must produce identical fingerprints"
        );
    }

    /// Display format starts with "fp:"
    #[test]
    fn test_fingerprint_display() {
        let fp = ExprFingerprint::compute("pred(x, y)");
        let display = format!("{}", fp);
        assert!(display.starts_with("fp:"), "Display must start with 'fp:'");
    }

    // ── CachingCompiler tests ─────────────────────────────────────────────────

    fn make_caching_compiler(capacity: usize) -> CachingCompiler {
        CachingCompiler::new(capacity, |expr| {
            let mut ctx = CompilerContext::new();
            compile_to_einsum_with_context(expr, &mut ctx).map_err(|e| e.to_string())
        })
    }

    /// Second compile of the same expression should use the cache
    #[test]
    fn test_caching_compiler_cache_hit() {
        let mut cc = make_caching_compiler(32);
        let expr = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);

        cc.compile(&expr).expect("first compile");
        cc.compile(&expr).expect("second compile");

        assert_eq!(
            cc.cache_stats().hits,
            1,
            "second compile must be a cache hit"
        );
    }

    /// First compile counts as a miss
    #[test]
    fn test_caching_compiler_cache_miss_count() {
        let mut cc = make_caching_compiler(32);
        let expr = TLExpr::pred("likes", vec![Term::var("a"), Term::var("b")]);

        cc.compile(&expr).expect("compile");

        assert_eq!(
            cc.cache_stats().misses,
            1,
            "first compile must be a cache miss"
        );
        assert_eq!(cc.cache_stats().hits, 0);
    }

    /// compile_batch processes all expressions
    #[test]
    fn test_caching_compiler_batch() {
        let mut cc = make_caching_compiler(32);
        let exprs = vec![
            TLExpr::pred("p", vec![Term::var("x")]),
            TLExpr::pred("q", vec![Term::var("y")]),
            TLExpr::pred("r", vec![Term::var("z")]),
        ];

        let results = cc.compile_batch(&exprs);
        assert_eq!(results.len(), 3, "batch must return one result per input");
        for (i, r) in results.iter().enumerate() {
            assert!(r.is_ok(), "result[{}] must be Ok", i);
        }
    }

    /// invalidate clears the entry for a specific expression
    #[test]
    fn test_caching_compiler_invalidate() {
        let mut cc = make_caching_compiler(32);
        let expr = TLExpr::pred("p", vec![Term::var("x")]);

        cc.compile(&expr).expect("compile");
        let removed = cc.invalidate(&expr);
        assert!(removed, "invalidate must return true when entry existed");

        // Re-compiling should be a miss again.
        cc.compile(&expr).expect("re-compile");
        assert_eq!(
            cc.cache_stats().misses,
            2,
            "re-compile after invalidation must be another miss"
        );
    }

    // ── Default / misc tests ──────────────────────────────────────────────────

    /// Default LRU cache capacity is 256
    #[test]
    fn test_cache_default_capacity() {
        let cache = LruCompilationCache::default();
        assert_eq!(cache.capacity(), 256, "default capacity must be 256");
    }

    /// ExprFingerprint implements Hash and can be used as a HashMap key
    #[test]
    fn test_expr_fingerprint_hash() {
        let mut map: HashMap<ExprFingerprint, u32> = HashMap::new();
        let fp = ExprFingerprint::compute("some_expr");
        map.insert(fp.clone(), 42);
        assert_eq!(
            map.get(&fp),
            Some(&42),
            "fingerprint must work as HashMap key"
        );
    }

    // ── Legacy CompilationCache tests ─────────────────────────────────────────

    #[test]
    fn test_ts_cache_new() {
        let cache = CompilationCache::new(100);
        assert_eq!(cache.max_size(), 100);
        assert_eq!(cache.len(), 0);
        assert!(cache.is_empty());
    }

    #[test]
    fn test_ts_cache_hit() {
        let cache = CompilationCache::new(100);
        let mut ctx = CompilerContext::new();
        ctx.add_domain("Person", 100);

        let expr = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);

        let graph1 = cache
            .get_or_compile(&expr, &mut ctx, compile_to_einsum_with_context)
            .expect("compile");

        let stats = cache.stats();
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.hits, 0);

        let graph2 = cache
            .get_or_compile(&expr, &mut ctx, compile_to_einsum_with_context)
            .expect("compile");

        let stats = cache.stats();
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.hits, 1);
        assert!(
            (stats.hit_rate() - 0.5).abs() < f64::EPSILON,
            "hit rate must be 0.5"
        );

        assert_eq!(graph1, graph2);
    }

    #[test]
    fn test_ts_cache_different_expressions() {
        let cache = CompilationCache::new(100);
        let mut ctx = CompilerContext::new();
        ctx.add_domain("Person", 100);

        let expr1 = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);
        let expr2 = TLExpr::pred("likes", vec![Term::var("x"), Term::var("y")]);

        let _ = cache
            .get_or_compile(&expr1, &mut ctx, compile_to_einsum_with_context)
            .expect("compile");
        let _ = cache
            .get_or_compile(&expr2, &mut ctx, compile_to_einsum_with_context)
            .expect("compile");

        let stats = cache.stats();
        assert_eq!(stats.misses, 2);
        assert_eq!(stats.hits, 0);
        assert_eq!(cache.len(), 2);
    }

    #[test]
    fn test_ts_cache_eviction() {
        let cache = CompilationCache::new(2);
        let mut ctx = CompilerContext::new();
        ctx.add_domain("Person", 100);

        let _ = cache.get_or_compile(
            &TLExpr::pred("p1", vec![Term::var("x")]),
            &mut ctx,
            compile_to_einsum_with_context,
        );
        let _ = cache.get_or_compile(
            &TLExpr::pred("p2", vec![Term::var("x")]),
            &mut ctx,
            compile_to_einsum_with_context,
        );
        let _ = cache.get_or_compile(
            &TLExpr::pred("p3", vec![Term::var("x")]),
            &mut ctx,
            compile_to_einsum_with_context,
        );

        let stats = cache.stats();
        assert_eq!(stats.evictions, 1);
        assert_eq!(cache.len(), 2);
    }

    #[test]
    fn test_ts_cache_clear() {
        let cache = CompilationCache::new(100);
        let mut ctx = CompilerContext::new();
        ctx.add_domain("Person", 100);

        let _ = cache.get_or_compile(
            &TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]),
            &mut ctx,
            compile_to_einsum_with_context,
        );

        assert_eq!(cache.len(), 1);
        cache.clear();
        assert_eq!(cache.len(), 0);
        assert!(cache.is_empty());
    }

    #[test]
    fn test_ts_cache_stats() {
        let cache = CompilationCache::new(100);
        let stats = cache.stats();

        assert_eq!(stats.hits, 0);
        assert_eq!(stats.misses, 0);
        assert_eq!(stats.evictions, 0);
        assert_eq!(stats.current_entries, 0);
        assert_eq!(stats.hit_rate(), 0.0);
        assert_eq!(stats.total_lookups(), 0);
    }
}