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