fugue-ppl 0.2.0

Monadic PPL with numerically stable inference and comprehensive diagnostics.
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
//! # Model Execution Integration Tests
//!
//! This module contains integration tests for end-to-end model execution flows.
//! These tests validate that models can be defined, executed with different handlers,
//! and produce expected results using **only the public API**.
//!
//! ## Test Categories
//!
//! ### 1. Basic Model Execution (`test_basic_*`)
//! - Simple model creation and execution with `PriorHandler`
//! - Verify that `runtime::handler::run()` works correctly
//! - Test that traces contain expected addresses and values
//! - Validate log weight accumulation
//!
//! ### 2. Handler Compatibility (`test_handler_*`)
//! - Test all handler types: `PriorHandler`, `ReplayHandler`, `SafeReplayHandler`,
//!   `ScoreGivenTrace`, `SafeScoreGivenTrace`
//! - Verify handlers produce consistent results for same models
//! - Test handler-specific behaviors (replay consistency, safe fallbacks)
//!
//! ### 3. Model Composition (`test_composition_*`)
//! - Test `bind`, `map`, `and_then` operations
//! - Test `zip` for combining models
//! - Test `sequence_vec` and `traverse_vec` for collections
//! - Verify composed models execute correctly end-to-end
//!
//! ### 4. Mixed Type Support (`test_mixed_types_*`)
//! - Models with `f64`, `bool`, `u64`, `usize` values
//! - Type-safe trace access for different value types
//! - Integration between continuous and discrete distributions
//!
//! ### 5. Factor and Guard Integration (`test_factor_guard_*`)
//! - Models with `factor()` statements affecting log weights
//! - Models with `guard()` conditions
//! - Integration of factors and guards with observations
//!
//! ### 6. Distribution Coverage (`test_distribution_*`)
//! - End-to-end execution with all distribution types:
//!   - Continuous: `Normal`, `Uniform`, `Exponential`, `Beta`, `Gamma`, `LogNormal`
//!   - Discrete: `Bernoulli`, `Poisson`, `Binomial`, `Categorical`
//! - Verify each distribution works in models and produces valid traces
//!
//! ### 7. Macro Integration (`test_macro_*`)
//! - `prob!` macro for model definition
//! - `addr!` macro for address creation
//! - `plate!` and `scoped_addr!` for structured addressing
//!
//! ## Implementation Guidelines
//!
//! - **Public API Only**: Use `fugue::*` imports, avoid `crate::` paths
//! - **Handler Creation**: Use struct literal syntax like examples:
//!   ```rust
//!   let handler = runtime::interpreters::PriorHandler {
//!       rng: &mut rng,
//!       trace: runtime::trace::Trace::default(),
//!   };
//!   ```
//! - **Address Creation**: Use `addr!("name")` macro, not `Address::new()`
//! - **Model Execution**: Use `runtime::handler::run(handler, model)`
//! - **Trace Access**: Use `trace.get_f64(&addr!("name"))` etc., returns `Option<T>`
//! - **Type Safety**: Test both successful access and type mismatches
//!
//! ## Expected Test Structure
//!
//! Each test should follow this pattern:
//! 1. Set up RNG with fixed seed for reproducibility
//! 2. Define model using public API
//! 3. Create appropriate handler
//! 4. Execute with `runtime::handler::run()`
//! 5. Assert on results and trace properties
//! 6. Test edge cases and error conditions

use fugue::*;
use rand::{rngs::StdRng, SeedableRng};

#[test]
fn test_basic_prior_sampling() {
    let mut rng = StdRng::seed_from_u64(42);

    // Define a simple model that samples from a normal distribution
    let model = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap());

    // Create a PriorHandler
    let handler = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    // Execute the model
    let (value, trace) = runtime::handler::run(handler, model);

    // Check that the trace contains the expected address
    let x_value = trace.get_f64(&addr!("x"));
    assert!(x_value.is_some());

    // The sampled value should equal the trace value
    assert_eq!(value, x_value.unwrap());

    // Check that the total log weight is finite (should be 0.0 for pure prior sampling)
    let log_weight = trace.total_log_weight();
    assert!(log_weight.is_finite());
}

#[test]
fn test_model_with_observation_and_factor() {
    let mut rng = StdRng::seed_from_u64(42);

    // Define a model with sample, observe, and factor
    let model = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap())
        .bind(|x| observe(addr!("y"), Normal::new(x, 1.0).unwrap(), 0.5))
        .bind(|_| factor(-1.0));

    // Create a PriorHandler
    let handler = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    // Execute the model
    let ((), trace) = runtime::handler::run(handler, model);

    // Check that the trace contains the expected address
    let x_value = trace.get_f64(&addr!("x"));
    assert!(x_value.is_some());

    // Check that all components of the log weight are finite
    assert!(trace.log_prior.is_finite());
    assert!(trace.log_likelihood.is_finite());
    assert!(trace.log_factors.is_finite());

    // The factor should contribute exactly -1.0
    assert!((trace.log_factors + 1.0).abs() < 1e-12);

    // Total log weight should be finite
    let log_weight = trace.total_log_weight();
    assert!(log_weight.is_finite());
}

#[test]
fn test_replay_and_score_handlers() {
    let mut rng = StdRng::seed_from_u64(42);

    // Define a simple model
    let model = || {
        sample(addr!("x"), Normal::new(0.0, 1.0).unwrap())
            .bind(|x| observe(addr!("y"), Normal::new(x, 1.0).unwrap(), 0.5).map(move |_| x))
    };

    // First, run with PriorHandler to get a trace
    let prior_handler = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let (original_value, original_trace) = runtime::handler::run(prior_handler, model());

    // Now replay with ReplayHandler - should get same result
    let replay_handler = runtime::interpreters::ReplayHandler {
        rng: &mut rng,
        base: original_trace.clone(),
        trace: runtime::trace::Trace::default(),
    };

    let (replayed_value, replayed_trace) = runtime::handler::run(replay_handler, model());

    // Replayed value should match original
    assert_eq!(original_value, replayed_value);

    // Both traces should have the same x value
    let original_x = original_trace.get_f64(&addr!("x")).unwrap();
    let replayed_x = replayed_trace.get_f64(&addr!("x")).unwrap();
    assert_eq!(original_x, replayed_x);

    // Now test ScoreGivenTrace handler
    let score_handler = runtime::interpreters::ScoreGivenTrace {
        base: original_trace.clone(),
        trace: runtime::trace::Trace::default(),
    };

    let (scored_value, scored_trace) = runtime::handler::run(score_handler, model());

    // Scored value should match original (since it's deterministic replay)
    assert_eq!(scored_value, original_value);

    // Scored trace should have the same total log weight structure
    assert!(scored_trace.total_log_weight().is_finite());
    assert_eq!(scored_trace.get_f64(&addr!("x")).unwrap(), original_x);
}

#[test]
fn test_model_composition() {
    let mut rng = StdRng::seed_from_u64(42);

    // Test bind and map operations
    let model1 = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap())
        .bind(|x| sample(addr!("y"), Normal::new(x, 0.5).unwrap()))
        .map(|y| y * 2.0);

    let handler1 = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let (result1, trace1) = runtime::handler::run(handler1, model1);

    // Check that both addresses are in the trace
    let _x_val = trace1.get_f64(&addr!("x")).unwrap();
    let y_val = trace1.get_f64(&addr!("y")).unwrap();

    // Result should be y * 2.0
    assert_eq!(result1, y_val * 2.0);

    // Test zip operation
    let model_a = sample(addr!("a"), Normal::new(0.0, 1.0).unwrap());
    let model_b = sample(addr!("b"), Normal::new(1.0, 1.0).unwrap());
    let zipped_model = zip(model_a, model_b);

    let handler2 = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let ((a_result, b_result), trace2) = runtime::handler::run(handler2, zipped_model);

    // Check that both addresses are in the trace
    let a_val = trace2.get_f64(&addr!("a")).unwrap();
    let b_val = trace2.get_f64(&addr!("b")).unwrap();

    // Results should match trace values
    assert_eq!(a_result, a_val);
    assert_eq!(b_result, b_val);

    // Test sequence_vec
    let models = vec![
        sample(addr!("seq_0"), Normal::new(0.0, 1.0).unwrap()),
        sample(addr!("seq_1"), Normal::new(1.0, 1.0).unwrap()),
        sample(addr!("seq_2"), Normal::new(2.0, 1.0).unwrap()),
    ];
    let sequence_model = sequence_vec(models);

    let handler3 = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let (seq_results, trace3) = runtime::handler::run(handler3, sequence_model);

    // Check that all sequence addresses are in the trace
    assert_eq!(seq_results.len(), 3);
    assert_eq!(seq_results[0], trace3.get_f64(&addr!("seq_0")).unwrap());
    assert_eq!(seq_results[1], trace3.get_f64(&addr!("seq_1")).unwrap());
    assert_eq!(seq_results[2], trace3.get_f64(&addr!("seq_2")).unwrap());
}

#[test]
fn test_mixed_types() {
    let mut rng = StdRng::seed_from_u64(42);

    // Define a model with multiple value types
    let model = sample(addr!("f64_val"), Normal::new(0.0, 1.0).unwrap())
        .bind(|_| sample(addr!("bool_val"), Bernoulli::new(0.6).unwrap()))
        .bind(|_| sample(addr!("u64_val"), Poisson::new(3.0).unwrap()))
        .bind(|_| {
            sample(
                addr!("usize_val"),
                Categorical::new(vec![0.3, 0.4, 0.3]).unwrap(),
            )
        })
        .map(|usize_val| (usize_val, "mixed_types_result"));

    let handler = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let ((usize_result, string_result), trace) = runtime::handler::run(handler, model);

    // Test type-safe trace access for different value types
    let f64_val = trace.get_f64(&addr!("f64_val"));
    assert!(f64_val.is_some());

    let bool_val = trace.get_bool(&addr!("bool_val"));
    assert!(bool_val.is_some());

    let u64_val = trace.get_u64(&addr!("u64_val"));
    assert!(u64_val.is_some());

    let usize_val = trace.get_usize(&addr!("usize_val"));
    assert!(usize_val.is_some());

    // The returned usize should match the trace value
    assert_eq!(usize_result, usize_val.unwrap());
    assert_eq!(string_result, "mixed_types_result");

    // Test type mismatches return None (not panicking)
    assert!(trace.get_f64(&addr!("bool_val")).is_none());
    assert!(trace.get_bool(&addr!("f64_val")).is_none());
    assert!(trace.get_u64(&addr!("usize_val")).is_none());
    assert!(trace.get_usize(&addr!("u64_val")).is_none());

    // Test result variants that return errors instead of panicking
    assert!(trace.get_f64_result(&addr!("bool_val")).is_err());
    assert!(trace.get_bool_result(&addr!("f64_val")).is_err());
    assert!(trace.get_u64_result(&addr!("usize_val")).is_err());
    assert!(trace.get_usize_result(&addr!("u64_val")).is_err());

    // Test missing addresses
    assert!(trace.get_f64(&addr!("missing")).is_none());
    assert!(trace.get_f64_result(&addr!("missing")).is_err());
}

#[test]
fn test_macro_integration() {
    let mut rng = StdRng::seed_from_u64(42);

    // Test using macros in model definition
    let model = prob!(
        let x <- sample(addr!("x"), Normal::new(0.0, 1.0).unwrap());
        let y <- sample(addr!("y"), Normal::new(x, 0.5).unwrap());
        observe(addr!("obs"), Normal::new(x, 0.5).unwrap(), 0.3);
        factor(-0.5);
        pure(x + y)
    );

    let handler = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let (result, trace) = runtime::handler::run(handler, model);

    // Check that addresses are created correctly by macros
    let x_val = trace.get_f64(&addr!("x")).unwrap();
    let y_val = trace.get_f64(&addr!("y")).unwrap();

    // Result should be x + y
    assert_eq!(result, x_val + y_val);

    // Check that factor was applied
    assert!((trace.log_factors + 0.5).abs() < 1e-12);

    // Test scoped_addr macro with plate
    let plate_model = plate! { i in 0..3 =>
        sample(scoped_addr!("plate", "item", "{}", i), Normal::new(i as f64, 1.0).unwrap())
    };

    let handler2 = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let (plate_results, trace2) = runtime::handler::run(handler2, plate_model);

    // Check that all plate addresses are created correctly
    assert_eq!(plate_results.len(), 3);
    for (i, &expected_val) in plate_results.iter().enumerate().take(3) {
        let addr = scoped_addr!("plate", "item", "{}", i);
        let val = trace2.get_f64(&addr);
        assert!(val.is_some());
        assert_eq!(expected_val, val.unwrap());
    }
}

#[test]
fn test_factor_guard_integration() {
    let mut rng = StdRng::seed_from_u64(42);

    // Test guard() conditions
    let guard_model = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap())
        .bind(|x| guard(x > -2.0 && x < 2.0)) // Should usually pass for standard normal
        .bind(|_| factor(-0.5))
        .bind(|_| pure("guard_passed"));

    let handler = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let (result, trace) = runtime::handler::run(handler, guard_model);

    // If we get here, the guard passed
    assert_eq!(result, "guard_passed");

    // Check that both factor and guard affected the trace
    assert!(trace.log_factors.is_finite());
    assert!((trace.log_factors + 0.5).abs() < 1e-12);

    // The sampled x should be within the guard bounds
    let x_val = trace.get_f64(&addr!("x")).unwrap();
    assert!(x_val > -2.0 && x_val < 2.0);

    // Test complex model with factors, guards, and observations
    let complex_model = sample(addr!("mu"), Normal::new(0.0, 2.0).unwrap()).bind(|mu| {
        guard(mu.abs() < 5.0) // Reasonable bound
            .bind(move |_| {
                sample(addr!("sigma"), Exponential::new(1.0).unwrap()).bind(move |sigma| {
                    guard(sigma > 0.1 && sigma < 10.0)  // Reasonable sigma bounds
                       .bind(move |_| observe(addr!("y"), Normal::new(mu, sigma).unwrap(), 1.5))
                       .bind(move |_| factor(mu * 0.1))  // Small preference for positive mu
                       .map(move |_| (mu, sigma))
                })
            })
    });

    let handler2 = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let ((mu_result, sigma_result), trace2) = runtime::handler::run(handler2, complex_model);

    // All guards should have passed
    assert!(mu_result.abs() < 5.0);
    assert!(sigma_result > 0.1 && sigma_result < 10.0);

    // Check trace components
    assert!(trace2.log_prior.is_finite());
    assert!(trace2.log_likelihood.is_finite());
    assert!(trace2.log_factors.is_finite());
    assert!(trace2.total_log_weight().is_finite());

    // Factor should be mu * 0.1
    assert!((trace2.log_factors - mu_result * 0.1).abs() < 1e-12);
}

#[test]
fn test_distribution_coverage() {
    let mut rng = StdRng::seed_from_u64(42);

    // Test all continuous distributions in models
    let continuous_model = sample(addr!("normal"), Normal::new(0.0, 1.0).unwrap())
        .bind(|_| sample(addr!("uniform"), Uniform::new(0.0, 1.0).unwrap()))
        .bind(|_| sample(addr!("exponential"), Exponential::new(1.0).unwrap()))
        .bind(|_| sample(addr!("beta"), Beta::new(2.0, 3.0).unwrap()))
        .bind(|_| sample(addr!("gamma"), Gamma::new(2.0, 1.0).unwrap()))
        .bind(|_| sample(addr!("lognormal"), LogNormal::new(0.0, 1.0).unwrap()));

    let handler1 = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let (lognormal_val, trace1) = runtime::handler::run(handler1, continuous_model);

    // Check that all continuous distributions produced finite values
    assert!(trace1.get_f64(&addr!("normal")).unwrap().is_finite());
    assert!(trace1.get_f64(&addr!("uniform")).unwrap().is_finite());
    assert!(trace1.get_f64(&addr!("exponential")).unwrap().is_finite());
    assert!(trace1.get_f64(&addr!("beta")).unwrap().is_finite());
    assert!(trace1.get_f64(&addr!("gamma")).unwrap().is_finite());
    assert!(trace1.get_f64(&addr!("lognormal")).unwrap().is_finite());
    assert_eq!(lognormal_val, trace1.get_f64(&addr!("lognormal")).unwrap());

    // Test all discrete distributions in models
    let discrete_model = sample(addr!("bernoulli"), Bernoulli::new(0.7).unwrap())
        .bind(|_| sample(addr!("poisson"), Poisson::new(3.0).unwrap()))
        .bind(|_| sample(addr!("binomial"), Binomial::new(10, 0.4).unwrap()))
        .bind(|_| {
            sample(
                addr!("categorical"),
                Categorical::new(vec![0.2, 0.3, 0.5]).unwrap(),
            )
        });

    let handler2 = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let (categorical_val, trace2) = runtime::handler::run(handler2, discrete_model);

    // Check that all discrete distributions produced valid values
    let bernoulli_val = trace2.get_bool(&addr!("bernoulli")).unwrap();
    let poisson_val = trace2.get_u64(&addr!("poisson")).unwrap();
    let binomial_val = trace2.get_u64(&addr!("binomial")).unwrap();
    let categorical_result = trace2.get_usize(&addr!("categorical")).unwrap();

    let _ = bernoulli_val; // Just checking it's a valid bool
    let _ = poisson_val; // poisson_val is u64, comparison with 0 is always true
    assert!((0..=10).contains(&binomial_val));
    assert!(categorical_result < 3); // Should be 0, 1, or 2
    assert_eq!(categorical_val, categorical_result);

    // Test mixed continuous and discrete in one model with observations
    let mixed_model = sample(addr!("mu"), Normal::new(0.0, 1.0).unwrap()).bind(|mu| {
        sample(addr!("success"), Bernoulli::new(0.6).unwrap()).bind(move |success| {
            if success {
                observe(addr!("obs"), Normal::new(mu, 0.5).unwrap(), 0.8)
                    .map(move |_| (mu, success))
            } else {
                observe(addr!("obs"), Normal::new(mu, 0.5).unwrap(), -0.3)
                    .map(move |_| (mu, success))
            }
        })
    });

    let handler3 = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let ((mu_mixed, success_mixed), trace3) = runtime::handler::run(handler3, mixed_model);

    // Check integration between continuous and discrete
    assert!(mu_mixed.is_finite());
    let _ = success_mixed; // Just checking it's a valid bool
    assert!(trace3.log_likelihood.is_finite());
    assert_eq!(mu_mixed, trace3.get_f64(&addr!("mu")).unwrap());
    assert_eq!(success_mixed, trace3.get_bool(&addr!("success")).unwrap());
}

#[test]
fn test_handler_compatibility_complete() {
    let mut rng = StdRng::seed_from_u64(42);

    // Define a model that tests all handler capabilities
    let model = || {
        sample(addr!("x"), Normal::new(0.0, 1.0).unwrap()).bind(|x| {
            sample(addr!("y"), Bernoulli::new(0.6).unwrap()).bind(move |y| {
                observe(addr!("obs"), Normal::new(x, 0.5).unwrap(), 0.5)
                    .bind(move |_| factor(if y { 0.1 } else { -0.1 }))
                    .map(move |_| (x, y))
            })
        })
    };

    // 1. Get baseline with PriorHandler
    let prior_handler = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let (baseline_result, baseline_trace) = runtime::handler::run(prior_handler, model());

    // 2. Test ReplayHandler (exact replay)
    let replay_handler = runtime::interpreters::ReplayHandler {
        rng: &mut rng,
        base: baseline_trace.clone(),
        trace: runtime::trace::Trace::default(),
    };

    let (replay_result, replay_trace) = runtime::handler::run(replay_handler, model());

    // ReplayHandler should produce identical results
    assert_eq!(baseline_result, replay_result);
    assert_eq!(
        baseline_trace.get_f64(&addr!("x")),
        replay_trace.get_f64(&addr!("x"))
    );
    assert_eq!(
        baseline_trace.get_bool(&addr!("y")),
        replay_trace.get_bool(&addr!("y"))
    );

    // 3. Test SafeReplayHandler (falls back to sampling for missing addresses)
    let safe_replay_handler = runtime::interpreters::SafeReplayHandler {
        rng: &mut rng,
        base: baseline_trace.clone(),
        trace: runtime::trace::Trace::default(),
        warn_on_mismatch: false,
    };

    let (safe_replay_result, safe_replay_trace) =
        runtime::handler::run(safe_replay_handler, model());

    // SafeReplayHandler should produce same results when all addresses exist
    assert_eq!(baseline_result, safe_replay_result);
    assert_eq!(
        baseline_trace.get_f64(&addr!("x")),
        safe_replay_trace.get_f64(&addr!("x"))
    );
    assert_eq!(
        baseline_trace.get_bool(&addr!("y")),
        safe_replay_trace.get_bool(&addr!("y"))
    );

    // 4. Test ScoreGivenTrace (deterministic scoring)
    let score_handler = runtime::interpreters::ScoreGivenTrace {
        base: baseline_trace.clone(),
        trace: runtime::trace::Trace::default(),
    };

    let (score_result, score_trace) = runtime::handler::run(score_handler, model());

    // ScoreGivenTrace should produce same results
    assert_eq!(baseline_result, score_result);
    assert_eq!(
        baseline_trace.get_f64(&addr!("x")),
        score_trace.get_f64(&addr!("x"))
    );
    assert_eq!(
        baseline_trace.get_bool(&addr!("y")),
        score_trace.get_bool(&addr!("y"))
    );

    // 5. Test SafeScoreGivenTrace (safe scoring with fallbacks)
    let safe_score_handler = runtime::interpreters::SafeScoreGivenTrace {
        base: baseline_trace.clone(),
        trace: runtime::trace::Trace::default(),
        warn_on_error: false,
    };

    let (safe_score_result, safe_score_trace) = runtime::handler::run(safe_score_handler, model());

    // SafeScoreGivenTrace should produce same results when all addresses exist
    assert_eq!(baseline_result, safe_score_result);
    assert_eq!(
        baseline_trace.get_f64(&addr!("x")),
        safe_score_trace.get_f64(&addr!("x"))
    );
    assert_eq!(
        baseline_trace.get_bool(&addr!("y")),
        safe_score_trace.get_bool(&addr!("y"))
    );

    // All handlers should produce finite log weights
    assert!(baseline_trace.total_log_weight().is_finite());
    assert!(replay_trace.total_log_weight().is_finite());
    assert!(safe_replay_trace.total_log_weight().is_finite());
    assert!(score_trace.total_log_weight().is_finite());
    assert!(safe_score_trace.total_log_weight().is_finite());
}

#[test]
fn test_model_composition_complete() {
    let mut rng = StdRng::seed_from_u64(42);

    // Test and_then operation (should be equivalent to bind)
    let and_then_model = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap())
        .and_then(|x| sample(addr!("y"), Normal::new(x, 0.5).unwrap()))
        .map(|y| y * 2.0);

    let handler1 = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let (and_then_result, trace1) = runtime::handler::run(handler1, and_then_model);

    // Check that and_then works like bind
    let _x_val = trace1.get_f64(&addr!("x")).unwrap();
    let y_val = trace1.get_f64(&addr!("y")).unwrap();
    assert_eq!(and_then_result, y_val * 2.0);

    // Test traverse_vec operation
    let data = vec![1.0, 2.0, 3.0];
    let traverse_model = traverse_vec(data.clone(), |x| {
        let idx = (x as usize).saturating_sub(1); // Convert 1.0->0, 2.0->1, 3.0->2
        sample(addr!("traverse", idx), Normal::new(x, 0.5).unwrap())
    });

    let handler2 = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let (traverse_results, trace2) = runtime::handler::run(handler2, traverse_model);

    // Check that traverse_vec processes all elements
    assert_eq!(traverse_results.len(), 3);
    for (i, _original_val) in data.iter().enumerate() {
        let addr = addr!("traverse", i);
        let sampled_val = trace2.get_f64(&addr).unwrap();
        assert_eq!(traverse_results[i], sampled_val);
        // The sampled value should be reasonably close to the mean (original_val)
        // but we can't assert exact equality due to randomness
        assert!(sampled_val.is_finite());
    }

    // Test complex composition with multiple operations
    let complex_composition = sample(addr!("a"), Normal::new(0.0, 1.0).unwrap())
        .bind(|a| sample(addr!("b"), Normal::new(a, 0.5).unwrap()).map(move |b| (a, b)))
        .and_then(|(a, b)| zip(pure(a + b), pure(a - b)))
        .bind(|(sum, diff)| sequence_vec(vec![pure(sum), pure(diff), pure(sum * diff)]))
        .map(|results| results.iter().sum::<f64>());

    let handler3 = runtime::interpreters::PriorHandler {
        rng: &mut rng,
        trace: runtime::trace::Trace::default(),
    };

    let (complex_result, trace3) = runtime::handler::run(handler3, complex_composition);

    // Verify the complex composition worked correctly
    let a_val = trace3.get_f64(&addr!("a")).unwrap();
    let b_val = trace3.get_f64(&addr!("b")).unwrap();
    let sum = a_val + b_val;
    let diff = a_val - b_val;
    let expected_result = sum + diff + (sum * diff);

    assert!((complex_result - expected_result).abs() < 1e-12);
    assert!(complex_result.is_finite());
}