Skip to content

Repository files navigation

Zar

An experimental probabilistic programming language (PPL)

Prereqs

  • GHC 8.6.4

Optional

To use the Z3 backend (disabled at the moment), build and install: https://github.com/Z3Prover/z3/tree/z3-4.8.1.

Quickstart

stack build && stack exec zar-exe programs/fair_coin.zar

Example

As an example of a Zar program, consider the following loop that simulates a fair coin from a biased one:

#File: programs/fair_coin.zar
main:
p <- 1/3 #p can be any Rational in (0, 1)
x <- false
y <- false
while x = y:
x <~ bernoulli(p)
y <~ bernoulli(p)
return x

One can run this program by doing

> stack exec zar-exe programs/fair_coin.zar 

from the root of the repository.

Zar's surface syntax makes a distinction between a functional expression language and a conventional imperative probabilistic command language. For example, the following expressions over basic datatypes like lists are currently definable:

func head (l : [int]) -> int :
destruct(l, (0-1), \x:int. \xs:[int]. x)
func tail (l : [int]) -> [int] :
destruct(l, []:int, \x:int. \xs:[int]. xs)
func concat (l1 : [int], l2 : [int]) -> [int] :
destruct(l1, l2, \x:int. \xs:[int]. x :: concat(xs, l2))
func reverse (l : [int]) -> [int] :
destruct(l, []:[int], \x:int. \xs:[int]. concat(reverse(xs), [x]:[int]))
func range (n : int) -> [int] :
if n <= 0 then [] : [int] else concat(range(n-1), [n-1]:[int])
...

and can be used in the context of probabilistic commands such as:

x <~ uniform(range(10))

As an alternative frontend, Zar can be used like an embedded DSL in Haskell (cf. programs/Controller.hs for an example).

Directory Structure

app/

FileWhat it does
Main.hsThe main entry point. Contains some code for generating random trees as well as reading and parsing programs

src/

FileWhat it does
Datatypes.hsGeneric set-up for open recursion style data types
Tree.hsThe tree functor, the type of well-founded trees and operations on them
ListTree.hsList representation of trees and operations on them, e.g., converting to and from the standard representation
Nat.hsOpen recursion natural numbers
Cotree.hsPotentially infinite trees as the greatest fixed point of the tree functor
Sample.hsState-monad sampling of Cotrees
Inference.hsApproximate inference via sampling
Sexp.hsTypeclass for serializing to s-expression format (e.g., in order to visualize trees using https://bagnalla.github.io/sexp-trees/)
Symtab.hsSymbol tables
Util.hsMiscellaneous utility functions including debug print
Lang.hsAbstract syntax (using GADTs and existential packages in the state); interpretation of commands as tree transformers
Distributions.hsPrimitive distributions (Uniform, Bernoulli, etc.)
Untyped.hsUntyped ASTs (typechecked and elaborated to the GADT representation)
Token.hsParser-related stuff
Parser.hsMegaparsec parser
Tycheck.hsTypechecking / elaboration from untyped to GADT

programs/

FileWhat it does
test.zarSimple example program
bernoulli.zarTests the built-in Bernoulli distribution
fair_coin.kySimulates a fair coin using an unfair one
flips.kyA stochastic domination example from Justin Hsu's thesis
tricky_coin.kyTricky coin Bayesian inference

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages