|
| 1 | +-- SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +-- |
| 4 | +-- Port of playground/test/probability_test.ts to Idris2, estate-rollout 5b/11. |
| 5 | +-- The probability layer mixes pure deterministic ops (expectation reduction, |
| 6 | +-- conditional dispatch) with PRNG-driven Monte Carlo. We port the pure parts |
| 7 | +-- directly (weighted-choice over [0,1) inputs, analytic expectation, |
| 8 | +-- conditional dispatch via Tri) and keep content-validation for the parts |
| 9 | +-- that need the Deno mulberry32 PRNG. |
| 10 | + |
| 11 | +module PlaygroundProbabilityTest |
| 12 | + |
| 13 | +import Test.Spec |
| 14 | +import PlaygroundTernaryTest |
| 15 | +import Data.String |
| 16 | +import System.File |
| 17 | + |
| 18 | +%default covering |
| 19 | + |
| 20 | +-- Inline weighted-branch model ----------------------------------------------- |
| 21 | + |
| 22 | +-- A branch carries a weight and a lazy value. Mirrors playground/src/probability.ts. |
| 23 | +public export |
| 24 | +record Branch a where |
| 25 | + constructor MkBranch |
| 26 | + weight : Double |
| 27 | + value : Lazy a |
| 28 | + |
| 29 | +-- Sum of weights. |
| 30 | +totalWeight : List (Branch a) -> Double |
| 31 | +totalWeight = foldr (\b, acc => weight b + acc) 0.0 |
| 32 | + |
| 33 | +-- Pick a branch given a draw in [0,1). Returns Nothing when weights are |
| 34 | +-- non-positive or the input list is empty (we model the runtime error |
| 35 | +-- defensively so tests can assert on it). |
| 36 | +public export |
| 37 | +betWeighted : List (Branch a) -> Double -> Maybe a |
| 38 | +betWeighted [] _ = Nothing |
| 39 | +betWeighted branches@(b0 :: _) draw = |
| 40 | + let tot = totalWeight branches in |
| 41 | + if tot <= 0.0 |
| 42 | + then Nothing |
| 43 | + else Just (pick branches (draw * tot) (Force (value b0))) |
| 44 | + where |
| 45 | + pick : List (Branch a) -> Double -> a -> a |
| 46 | + pick [] _ fallback = fallback |
| 47 | + pick (b :: bs) r fallback = |
| 48 | + let r' = r - weight b in |
| 49 | + if r' <= 0.0 |
| 50 | + then Force (value b) |
| 51 | + else pick bs r' fallback |
| 52 | + |
| 53 | +-- Predicate-driven Tri dispatch: total under U. Mirrors the Deno |
| 54 | +-- betConditional, which is just a Tri-flavoured wrapper over bet(). |
| 55 | +public export |
| 56 | +betConditional : Tri -> Lazy a -> Lazy a -> Lazy a -> a |
| 57 | +betConditional T onT _ _ = onT |
| 58 | +betConditional U _ onU _ = onU |
| 59 | +betConditional F _ _ onF = onF |
| 60 | + |
| 61 | +-- Analytic expectation of a numeric weighted bet (no sampling needed). |
| 62 | +public export |
| 63 | +analyticEV : List (Branch Double) -> Double |
| 64 | +analyticEV bs = |
| 65 | + let tot = totalWeight bs in |
| 66 | + if tot <= 0.0 |
| 67 | + then 0.0 |
| 68 | + else foldr (\b, acc => weight b * Force (value b) + acc) 0.0 bs / tot |
| 69 | + |
| 70 | +-- File helpers for content validation ---------------------------------------- |
| 71 | + |
| 72 | +readFileToString : String -> IO String |
| 73 | +readFileToString path = do |
| 74 | + Right contents <- readFile path |
| 75 | + | Left _ => pure "" |
| 76 | + pure contents |
| 77 | + |
| 78 | +fileExists : String -> IO Bool |
| 79 | +fileExists path = do |
| 80 | + Right _ <- readFile path |
| 81 | + | Left _ => pure False |
| 82 | + pure True |
| 83 | + |
| 84 | +-- Sample weighted payout used in the Deno expectation test. |
| 85 | +samplePayout : List (Branch Double) |
| 86 | +samplePayout = |
| 87 | + [ MkBranch 1.0 100.0 |
| 88 | + , MkBranch 2.0 10.0 |
| 89 | + , MkBranch 7.0 0.0 |
| 90 | + ] |
| 91 | + |
| 92 | +-- Tests ---------------------------------------------------------------------- |
| 93 | + |
| 94 | +public export |
| 95 | +allSuites : List TestCase |
| 96 | +allSuites = |
| 97 | + [ test "analyticEV computes the documented EV = 12 for the sample payout" $ |
| 98 | + assertEq (analyticEV samplePayout) 12.0 |
| 99 | + |
| 100 | + , test "betWeighted with draw=0.0 picks the first branch" $ do |
| 101 | + let bs : List (Branch String) = |
| 102 | + [ MkBranch 0.6 "T", MkBranch 0.3 "U", MkBranch 0.1 "F" ] |
| 103 | + assertEq (betWeighted bs 0.0) (Just "T") |
| 104 | + |
| 105 | + , test "betWeighted with draw=0.5 picks branch with cumulative weight" $ do |
| 106 | + -- weights 0.6/0.3/0.1; draw 0.5 -> target 0.5 -> consume 0.6 first -> T |
| 107 | + let bs : List (Branch String) = |
| 108 | + [ MkBranch 0.6 "T", MkBranch 0.3 "U", MkBranch 0.1 "F" ] |
| 109 | + assertEq (betWeighted bs 0.5) (Just "T") |
| 110 | + |
| 111 | + , test "betWeighted with draw 0.75 picks the middle branch" $ do |
| 112 | + -- target 0.75; 0.6 used by T (-0.15 leftover -> would early-return) |
| 113 | + -- Actually 0.75*1.0 = 0.75; 0.75 - 0.6 = 0.15 > 0, then 0.15 - 0.3 < 0 -> U |
| 114 | + let bs : List (Branch String) = |
| 115 | + [ MkBranch 0.6 "T", MkBranch 0.3 "U", MkBranch 0.1 "F" ] |
| 116 | + assertEq (betWeighted bs 0.75) (Just "U") |
| 117 | + |
| 118 | + , test "betWeighted with draw 0.95 picks the tail branch" $ do |
| 119 | + let bs : List (Branch String) = |
| 120 | + [ MkBranch 0.6 "T", MkBranch 0.3 "U", MkBranch 0.1 "F" ] |
| 121 | + assertEq (betWeighted bs 0.95) (Just "F") |
| 122 | + |
| 123 | + , test "betWeighted rejects empty branch list" $ do |
| 124 | + let bs : List (Branch String) = [] |
| 125 | + assertEq (betWeighted bs 0.5) Nothing |
| 126 | + |
| 127 | + , test "betWeighted rejects non-positive total weight" $ do |
| 128 | + let bs : List (Branch String) = [ MkBranch 0.0 "x" ] |
| 129 | + assertEq (betWeighted bs 0.5) Nothing |
| 130 | + |
| 131 | + , test "betConditional defers to the uncertain branch on Unknown" $ |
| 132 | + assertEq (betConditional U "yes" "maybe" "no") "maybe" |
| 133 | + |
| 134 | + , test "betConditional commits on True" $ |
| 135 | + assertEq (betConditional T "yes" "maybe" "no") "yes" |
| 136 | + |
| 137 | + , test "betConditional declines on False" $ |
| 138 | + assertEq (betConditional F "yes" "maybe" "no") "no" |
| 139 | + |
| 140 | + -- Content validation for the parts of probability.ts that rely on Deno's |
| 141 | + -- mulberry32 PRNG: we cannot port the bit-twiddling RNG identically into |
| 142 | + -- Idris2, so we cross-check that the TS source still exposes the public API |
| 143 | + -- the tests above mirror. |
| 144 | + , test "Structure: probability.ts exports rng/betWeighted/betConditional/expectation" $ do |
| 145 | + content <- readFileToString "src/probability.ts" |
| 146 | + allPass |
| 147 | + [ assertTrue "export rng" (isInfixOf "export function rng" content) |
| 148 | + , assertTrue "export betWeighted" (isInfixOf "export function betWeighted" content) |
| 149 | + , assertTrue "export betConditional" (isInfixOf "export function betConditional" content) |
| 150 | + , assertTrue "export expectation" (isInfixOf "export function expectation" content) |
| 151 | + ] |
| 152 | + |
| 153 | + , test "Structure: probability.ts uses mulberry32 PRNG (deterministic, seedable)" $ do |
| 154 | + content <- readFileToString "src/probability.ts" |
| 155 | + allPass |
| 156 | + [ assertTrue "mulberry32 mention" (isInfixOf "mulberry32" content) |
| 157 | + , assertTrue "0x6d2b79f5 constant" (isInfixOf "0x6d2b79f5" content) |
| 158 | + , assertTrue "Math.imul" (isInfixOf "Math.imul" content) |
| 159 | + ] |
| 160 | + ] |
0 commit comments