@@ -4,60 +4,71 @@ Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
44-->
55# betlang
66
7- > Probabilistic programming language
8-
9- ** Status:** 📝 Placeholder - contributions welcome
7+ ** Tagline:** A symbolic probabilistic metalanguage — a * probabilistic computer algebra system* , not a betting language.
108
119## Overview
1210
13- betlang is a probabilistic programming language built on Racket, enabling explicit modeling of uncertainty through sampling and inference.
14-
15- ## Core Invariant
16-
17- ** All uncertainty must be explicitly modeled—no hidden randomness. **
11+ BetLang is built on one idea: ** computation is structured choice under uncertainty. **
12+ Its core primitive is the ternary form ` (bet A B C) ` — a probabilistic, ** lazy ** choice
13+ between three branches (only the selected branch is evaluated). This makes BetLang closer
14+ to a * probabilistic CAS * than a simulation DSL: it is symbolic-first, compositional, and
15+ treats uncertainty as a first-class ** typed ** object rather than as noise to average away.
1816
19- ## Key Features
17+ ## Philosophy
2018
21- - First-class probability distributions
22- - Bayesian inference primitives
23- - Conditioning and observation
24- - Sampling and expectation
19+ - ** Ternary over binary ** — real-world decisions are rarely yes/no; the musical ternary
20+ form (A–B–A) gives compositional structure, and Dutch-book coherence needs ≥3 outcomes.
21+ - ** Uncertainty as structure ** — not noise, but a first-class object the type system tracks.
22+ - ** Symbolic first ** — computation stays inspectable and composable.
2523
26- ## Example
24+ ## Core Primitive
2725
2826``` racket
2927#lang betlang
3028
31- ; Define a biased coin
32- (define coin (bernoulli 0.7))
29+ (bet 'win 'draw 'lose) ; uniform ternary choice (lazy)
30+ (bet/weighted '(common 7) '(uncommon 2) '(rare 1)) ; non-uniform
31+ (bet/conditional pred A B C) ; predicate-driven
32+ (bet (expensive) (cheap-approx) (fallback)) ; only one branch is evaluated
33+ ```
3334
34- ; Model with observation
35- (define (model)
36- (let ([heads (sample coin)])
37- (observe (= heads #t)) ; condition on heads
38- heads))
35+ ## Type System
3936
40- ; Run inference
41- (define posterior (infer model 1000))
42- ```
37+ BetLang's checker (` bet-check ` , Rust) is Hindley–Milner with two domain-specific pillars:
4338
44- ## Use Cases
39+ - ** Echo types** — * structured loss* as a typed object. When a ` bet ` collapses three
40+ branches to one (or a ` sample ` marginalises a draw), ` Echo T ` retains a proof-relevant
41+ residue of what was lost; ` Echo T ` is deliberately ** distinct from ` T ` ** (no implicit
42+ forgetting). The operations form a functor + comonad surface (` echo ` , ` echo_output ` ,
43+ ` echo_map ` , ` echo_duplicate ` , ` echo_to_residue ` , ` sample_echo ` ), the ungraded ghost
44+ shadow of the graded comonad proved in [ ` echo-types ` ] ( https://github.com/hyperpolymath/echo-types ) (Agda).
45+ - ** The uncertainty number tower** — 14 uncertainty-aware number systems (Gaussian,
46+ interval/affine, fuzzy, Bayesian, VaR/CVaR, surreal, p-adic, imprecise, Dempster–Shafer,
47+ …). These * are* the type system, not add-ons.
4548
46- - Bayesian modeling
47- - Machine learning prototyping
48- - Statistical inference
49- - Uncertainty quantification
50- - A/B testing analysis
49+ ## Formal Verification
5150
52- ## Getting Started
51+ The core calculus is mechanised in ** Lean 4** (` proofs/BetLang.lean ` , CI-checked):
52+ ** Progress** , ** Preservation** , and the ** distribution monad laws** — with an
53+ ** axiom-free core** (the ` substTop_preserves_typing ` axiom was discharged) and ** zero
54+ ` sorry ` ** . This is BetLang's differentiator versus untyped numeric/CAS tools (R, Octave,
55+ Scilab, Mathematica, Maple): * typed, proof-anchored uncertainty* .
5356
54- ``` bash
55- cd betlang
56- racket main.rkt
57- ```
57+ ## Architecture
58+
59+ Four layers with separated responsibilities: ** Racket** (authoritative semantics) ·
60+ ** Julia** (compute kernel / number tower) · ** Lean 4** (proofs) · ** Rust** (compiler
61+ tooling — ` bet-core ` , ` bet-check ` , ` bet-parse ` ).
62+
63+ ## Status
64+
65+ Active. Racket frontend authoritative; Lean proofs machine-checked (axiom-free core);
66+ Rust checker handles Echo formers ** and** the typed echo operations; Julia backend in
67+ active development.
5868
5969## See Also
6070
61- - [[ Duet Language]] - AI-assisted verification
62- - [[ Ensemble Language]] - AI integration
63- - [[ NEUROSYM.scm]] - Neuro-symbolic patterns
71+ - BetLang repository wiki (in-repo ` wiki/ ` ): Home · Type-System · Echo-Types · Formal-Verification · Number-Tower
72+ - [[ Ephapax]] — linear/affine once-only semantics (sibling language)
73+ - [[ AffineScript]] — affine-typed primary application language
74+ - Upstream: [ ` echo-types ` ] ( https://github.com/hyperpolymath/echo-types ) (Agda, source of truth) · [ ` EchoTypes.jl ` ] ( https://github.com/hyperpolymath/EchoTypes.jl ) (executable shadow)
0 commit comments