1
2
Fork
You've already forked tinypp
0
No description
  • Gleam 100%
2025年11月12日 15:05:28 +01:00
examples Extract examples 2025年11月12日 00:22:23 +01:00
src Write docs 2025年11月12日 12:42:22 +01:00
test Add test 2025年11月12日 11:51:38 +01:00
.gitignore Initial commit 2025年11月11日 23:07:15 +01:00
gleam.toml Add info for publshing 2025年11月12日 12:51:46 +01:00
manifest.toml Initial commit 2025年11月11日 23:07:15 +01:00
README.md Link to blog article 2025年11月12日 15:05:28 +01:00

tinypp

Package Version Hex Docs

tinypp is a tiny Gleam package to do probabilistic programming (hence the name). It is inspired by the probabilistic programming language Church after reading this fun paper.

It allows you to program probability distributions with discrete support, making heavy use of Gleam's use-syntax. The main functions are sample, condition and query:

  • use x <- sample(distribution): State that x is supposed to follow distribution.
  • use <- condition(predicate): State that predicate should hold.
  • query(quantity): State that you are interested in the distribution of quantity.

See the example below on how put these together. Read the blog article for some behind the scenes details.

gleam add tinypp
importgleam/floatimportgleam/intimportgleam/ioimporttinypp.{pmf,normalize,sample,condition,query}importtinypp/distribution.{uniform}pubfnmain()->Nil{// What is the probability that a die shows a value greater than three if we
// know that the value is even?
letdistribution_greater_three={letdie=uniform([1,2,3,4,5,6])usevalue<-sample(die)use<-condition(int.is_even(value))query(value>3)}letp_greater_three=pmf(normalize(distribution_greater_three),True)io.println("P(value > 3 | value is even) = "<>float.to_string(p_greater_three))}

In the examples folder, you can find more elaborate examples, including Bayesian linear regression.

Development

gleam test # Run the tests