|
| 1 | +<!-- |
| 2 | +SPDX-License-Identifier: CC-BY-SA-4.0 |
| 3 | +SPDX-FileCopyrightText: 2025-2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 4 | +--> |
| 5 | + |
| 6 | +[](https://github.com/sponsors/hyperpolymath) |
| 7 | + |
| 8 | +# What Is This? |
| 9 | + |
| 10 | +Betlangiser analyses deterministic code, identifies values that should |
| 11 | +be probabilistic, wraps them in **Betlang distributions**, and generates |
| 12 | +**uncertainty-propagating code**. Turn `price` `=` `100` into `price` |
| 13 | +`=` `Normal(100,` `5)` with automatic propagation through arithmetic and |
| 14 | +control flow — without rewriting your codebase. |
| 15 | + |
| 16 | +Betlang is a ternary probabilistic programming language where every |
| 17 | +boolean becomes **true / false / unknown**, enabling reasoning under |
| 18 | +genuine uncertainty rather than forcing premature binary decisions. |
| 19 | + |
| 20 | +# How It Works |
| 21 | + |
| 22 | +1. You write a `betlangiser.toml` manifest declaring which values are |
| 23 | + uncertain |
| 24 | + |
| 25 | +2. Betlangiser **analyses your deterministic source** to find numeric |
| 26 | + values, boolean conditions, and decision points |
| 27 | + |
| 28 | +3. The **Idris2 ABI layer** proves that distribution compositions are |
| 29 | + mathematically correct (Kolmogorov axioms, support bounds, parameter |
| 30 | + validity) |
| 31 | + |
| 32 | +4. The **Zig FFI bridge** provides zero-overhead C-ABI sampling and |
| 33 | + combination |
| 34 | + |
| 35 | +5. The **codegen engine** emits Betlang wrappers with ternary bet |
| 36 | + semantics |
| 37 | + |
| 38 | +6. You get **probability distributions**, not point estimates |
| 39 | + |
| 40 | +# Key Value |
| 41 | + |
| 42 | +- **Retrofit uncertainty** — add probabilistic modelling to existing |
| 43 | + code without a rewrite |
| 44 | + |
| 45 | +- **Ternary logic** — every boolean becomes true/false/unknown, |
| 46 | + propagating uncertainty through conditionals and loops |
| 47 | + |
| 48 | +- **Distribution types** — Normal, Uniform, Beta, Bernoulli, and custom |
| 49 | + distributions as first-class values |
| 50 | + |
| 51 | +- **Proven correctness** — Idris2 dependent types prove distribution |
| 52 | + composition obeys Kolmogorov axioms at compile time |
| 53 | + |
| 54 | +- **14 number systems** — from exact rationals to fuzzy intervals, |
| 55 | + matched to precision requirements |
| 56 | + |
| 57 | +- **Automatic propagation** — uncertainty flows through arithmetic, |
| 58 | + comparisons, and control flow without manual instrumentation |
| 59 | + |
| 60 | +# Use Cases |
| 61 | + |
| 62 | +- **Financial modelling** — model price uncertainty, risk distributions, |
| 63 | + portfolio Monte Carlo |
| 64 | + |
| 65 | +- **Sensor fusion** — combine noisy readings with known error |
| 66 | + distributions |
| 67 | + |
| 68 | +- **Risk assessment** — propagate uncertainty through decision trees |
| 69 | + |
| 70 | +- **Monte Carlo pipelines** — generate full simulation harnesses from |
| 71 | + deterministic code |
| 72 | + |
| 73 | +- **Scientific computing** — add measurement uncertainty to numerical |
| 74 | + models |
| 75 | + |
| 76 | +# Architecture |
| 77 | + |
| 78 | +Follows the hyperpolymath **-iser pattern**: |
| 79 | + |
| 80 | + betlangiser.toml (manifest) |
| 81 | + -> Deterministic source analysis |
| 82 | + -> Idris2 ABI (proves distribution correctness) |
| 83 | + -> Zig FFI (C-ABI sampling bridge) |
| 84 | + -> Betlang codegen (uncertainty-propagating wrappers) |
| 85 | + |
| 86 | +## Idris2 ABI Layer |
| 87 | + |
| 88 | +- `Types.idr` — Distribution, TernaryBool, ProbabilityValue, |
| 89 | + ConfidenceInterval, SamplingStrategy |
| 90 | + |
| 91 | +- `Layout.idr` — Distribution struct memory layout, sample buffer layout |
| 92 | + |
| 93 | +- `Foreign.idr` — Distribution creation, sampling, combination, ternary |
| 94 | + logic FFI declarations |
| 95 | + |
| 96 | +## Zig FFI Bridge |
| 97 | + |
| 98 | +- `main.zig` — Distribution allocation, sampling engine, combination |
| 99 | + operators, ternary logic evaluation |
| 100 | + |
| 101 | +- `build.zig` — Shared/static library build, cross-compilation |
| 102 | + |
| 103 | +- `test/integration_test.zig` — ABI compliance tests |
| 104 | + |
| 105 | +Part of the [-iser family](https://github.com/hyperpolymath/iseriser). |
| 106 | + |
| 107 | +# CLI Commands |
| 108 | + |
| 109 | +```bash |
| 110 | +# Initialise a new manifest |
| 111 | +betlangiser init |
| 112 | + |
| 113 | +# Validate manifest |
| 114 | +betlangiser validate -m betlangiser.toml |
| 115 | + |
| 116 | +# Generate Betlang wrappers and FFI bridge |
| 117 | +betlangiser generate -m betlangiser.toml -o generated/betlangiser |
| 118 | + |
| 119 | +# Build generated artifacts |
| 120 | +betlangiser build -m betlangiser.toml --release |
| 121 | + |
| 122 | +# Run the workload |
| 123 | +betlangiser run -m betlangiser.toml |
| 124 | + |
| 125 | +# Show manifest info |
| 126 | +betlangiser info -m betlangiser.toml |
| 127 | +``` |
| 128 | + |
| 129 | +# Example Manifest |
| 130 | + |
| 131 | +```toml |
| 132 | +[workload] |
| 133 | +name = "pricing-model" |
| 134 | +description = "Add uncertainty to deterministic pricing" |
| 135 | + |
| 136 | +[sources] |
| 137 | +paths = ["src/pricing.rs"] |
| 138 | + |
| 139 | +[distributions] |
| 140 | +# Wrap a deterministic value in a normal distribution |
| 141 | +[[distributions.wrap]] |
| 142 | +target = "base_price" |
| 143 | +distribution = "Normal" |
| 144 | +params = { mean = 100.0, stddev = 5.0 } |
| 145 | + |
| 146 | +[[distributions.wrap]] |
| 147 | +target = "demand_factor" |
| 148 | +distribution = "Uniform" |
| 149 | +params = { low = 0.8, high = 1.2 } |
| 150 | + |
| 151 | +[[distributions.wrap]] |
| 152 | +target = "is_peak_season" |
| 153 | +distribution = "Bernoulli" |
| 154 | +params = { p = 0.3 } |
| 155 | + |
| 156 | +[propagation] |
| 157 | +strategy = "monte-carlo" |
| 158 | +samples = 10000 |
| 159 | +confidence = 0.95 |
| 160 | + |
| 161 | +[output] |
| 162 | +format = "betlang" |
| 163 | +ternary-logic = true |
| 164 | +``` |
| 165 | + |
| 166 | +# Building |
| 167 | + |
| 168 | +```bash |
| 169 | +cargo build --release |
| 170 | +cargo test |
| 171 | +``` |
| 172 | + |
| 173 | +# Status |
| 174 | + |
| 175 | +**Pre-alpha.** Architecture defined, CLI scaffolded, ABI definitions in |
| 176 | +progress. Codegen engine pending. |
| 177 | + |
| 178 | +# License |
| 179 | + |
| 180 | +SPDX-License-Identifier: CC-BY-SA-4.0 |
0 commit comments