Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

symprog

Symbolic programming in Python: a fast prototyping toolkit for working with weighted Boolean formulas and discrete (probabilistic) programs.

⚠️ Work in progress. symprog is under active development. APIs may change between releases, and some features are experimental. Feedback, bug reports and contributions are very welcome. Please open an issue.

symprog lets you write small symbolic programs (Boolean constraints, probabilistic logic programs, weighted formulas) directly inside Python. You can then reason about them on the fly: count models, compute exact probabilities, find the most likely assignment, take gradients, or look at the underlying symbolic equations.

It is designed with two uses in mind:

  • Rapid prototyping. Sketch a new modeling idea involving weighed Boolean formulas or discrete programming in a few lines of Python. Then switch the reasoning backend (SDDs, BDDs, SAT, SMT) and the numerical backend (PyTorch, symbolic algebra) without rewriting your program.
  • Teaching. The library stays small and readable. Each piece (languages, solvers, semirings, knowledge compilers) matches a concept from logic, probabilistic reasoning and automated reasoning, and the notebooks walk through them step by step.

Installation

symprog requires Python 3.10 or newer.

pip install symprog

To get the latest development version from GitHub:

pip install "git+https://github.com/TuLLI-AI/symprog.git"

Optional extras:

pip install "symprog[console]"    # interactive Streamlit console
pip install "symprog[notebooks]"  # Jupyter and graphviz for the tutorials
pip install "symprog[jax]"        # experimental JAX backend
pip install "symprog[klay]"       # klay tensorizaiton backend (https://github.com/ML-KULeuven/klay)

For development, clone the repository and install it in editable mode:

git clone https://github.com/TuLLI-AI/symprog.git
cd symprog
pip install -e ".[dev,notebooks,console]"
pytest

Quick tour

Languages

symprog can embed small probabilistic programming languages inline in Python, with on-the-fly inference and differentiable computation.

Below is a small program in probabilistic logic. It has a set of constraints over weighted variables:

from symprog import Parser

@Parser(language="constraint")
def prog():
    ## variable weights
    a: 0.9; b: 0.7; c: 0.65

    ## Boolean constraints
    (not a) or b
    c or b
    (not b) or c

    ## conditioning
    condition: a

You can then query the exact probability that the formula is satisfied:

prog.inference(mode="Prob").inference

The next example uses a small probabilistic logic programming language, inspired by ProbLog:

from symprog import Parser

@Parser(language="plp")
def inh():
    ## probabilistic facts
    c1: 0.5; c2: 0.5

    ## weighted clauses
    e1[0.3] :- c1
    e1[0.2, False] :- c2

    e2[0.4] :- c2
    e2[0.2, False] :- c1

    e :- e1
    e :- e2

    ## evidence or conditions
    evidence(c1, True)
    evidence(c2, True)

    ## queries
    query(e)

The same kinds of inference work here. For more examples see notebooks/languages.ipynb.

Interactive console

To try programs interactively, launch the console from a clone of the repository (it requires the console extra):

streamlit run console.py

The console comes with example programs in both languages (graph coloring, fault diagnosis, Monty Hall, a hidden Markov model and more). Besides running, sampling and exact inference, it can show:

  • the program's models, with a brute-force check of the solver's results;
  • the compiled circuit, with the value of each node in a chosen semiring, and the files to download it;
  • each program as inline Python code, ready to copy.

Solvers

Inference is driven by Solvers. A Solver gives you one simple interface to several reasoning tools, such as pysdd, z3 and python-sat, and to tensor libraries such as PyTorch. With these it performs exact inference over (weighted) Boolean formulas.

from symprog import Solver

solver = Solver()

FORMULAS = [
    "Implies(a, b)",
    "And(Or(a, b), Implies(a, c))"
]

## add propositional formulas and variable weights
solver.add(FORMULAS)
solver.add_weights({"a": 0.9, "b": 0.64, "c": 0.34})

## probabilistic inference (weighted model counting)
print(solver.query())
# => SolverOut(inference_type=Prob, inference=0.2598400115966797)

Solvers support many kinds of inference through an extensible algebraic (semiring) interface. See notebooks/solver_tutorial.ipynb for details:

## Viterbi-style inference (most likely assignment of variables)
solver.query(mode="Viterbi").inference
solver.query(mode="ViterbiAssignment").variables
# => {'c': True, 'b': True, 'a': True}
solver.query(mode="LogViterbi").inference

## semantic entropy (entropy of interpretations given the formulas)
solver.query(mode="Entropy").inference

## probability and log probability (more numerically stable)
solver.query(mode="Prob").inference
solver.query(mode="LogProb").inference

## fuzzy inference
solver.query(mode="Fuzzy").inference

## satisfiability testing (SAT)
solver.query(mode="SAT").inference

Conditioning on evidence:

solver.query(mode="ViterbiAssignment", evidence="~a").variables
# => {'c': False, 'a': False, 'b': True}

Gradients with respect to the weights:

solver.gradient(mode="Viterbi", key="weights")
# => tensor([0.2176, 0.3060, 0.5760])

Model enumeration:

for model in solver.models():
    print(model)
# {'a': True, 'c': True, 'b': True}
# {'a': False, 'c': False, 'b': True}
# {'a': False, 'c': True, 'b': True}

Backend support

Not every backend supports every feature yet:

Backend SAT / model counting / model enumeration Weighted inference (Prob, Viterbi, …) Gradients
sdd solver (default) ✅ ✅ ✅
bdd, z3, sympy, pysat solvers ✅ ❌ not yet implemented n/a
pytorch weights (default) ✅ ✅
python weights ✅ ❌
sympy / symengine symbolic weights ✅ ✅ (symbolic)
jax weights (experimental, pip install "symprog[jax]") ✅ ✅

With the JAX backend, Solver.as_function returns inference as a pure function of the weight vector (ordered as in solver.weight_names). You can then transform it with jax.grad, jax.jit and jax.vmap, for example to compute batched gradients for many weight settings at once:

import jax, jax.numpy as jnp

solver.add_weights({"a": 0.9, "b": 0.64, "c": 0.34}, container_type="jax")
f = solver.as_function(mode="LogProb", evidence="c")
batched_grads = jax.jit(jax.vmap(jax.grad(f)))(jnp.array([[0.9, 0.64, 0.34],
                                                          [0.2, 0.50, 0.70]]))

The Product inference mode (product t-norm with probabilistic sum) is also experimental. It is not a true semiring, so its results depend on the structure of the compiled circuit.

Tensorization (solver.tensorize), which compiles an inference into a PyTorch module for fast batched evaluation, is experimental as well. It has two backends: fx (default), which supports all inference modes, evidence, queries and conditionals, and klay, which uses KLay to evaluate circuits layer by layer and supports Prob, LogProb, Viterbi, LogViterbi and Fuzzy, without evidence or conditioning (pip install "symprog[klay]", Python ≥ 3.11).

Experimental features emit a symprog.ExperimentalWarning, which can be silenced with:

import warnings, symprog
warnings.filterwarnings("ignore", category=symprog.ExperimentalWarning)

Symbolic equations

Solvers also connect to computer algebra systems (SymPy, SymEngine). You can produce and manipulate the exact symbolic equations behind any of the inferences above:

## the symbolic polynomial behind the entropy computation
solver.query(mode="Entropy", symbolic=True).inference
# => log(a*b*c + b*(1 - a)) - (a*b*c*log(a) + a*(b*c*log(b) + b*c*log(c)) + ...

## its symbolic gradient vector
solver.gradient(mode="Entropy", symbolic=True, key="weights")

Tutorials

The notebooks/ directory contains step-by-step tutorials, in suggested reading order:

Notebook Topic
solver_tutorial.ipynb Start here: formulas, weighted model counting, conditioning, and the algebraic (semiring) view of inference
languages.ipynb Writing inline constraint programs and probabilistic logic programs (ProbLog-style)
symbolic_and_gradients.ipynb Symbolic expressions, numerical/symbolic/Boolean gradients, and learning weights
tensorization_and_jax.ipynb Fast batched inference: compiling to PyTorch modules, and JAX (jit, vmap, grad)
solver_interfaces.ipynb The solver backends (SDD, BDD, z3, sympy, PySAT)
weight_interface.ipynb The weight backends (PyTorch, Python, symbolic, JAX)

Contributing

symprog is meant to grow with its users. Bug reports, examples, new languages and new solver backends are all welcome. Please open an issue to discuss larger changes before submitting a pull request.

License

symprog is released under the Apache License 2.0.

Metadata

Release files for symprog 0.1.0a2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for symprog 0.1.0a2
File Size Uploaded Provenance Actions
symprog-0.1.0a2.tar.gz 119.5 kB Without provenance Details for symprog-0.1.0a2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for symprog 0.1.0a2
File Interpreter ABI Platform Actions
symprog-0.1.0a2-py3-none-any.whl Python 3 none any Details for symprog-0.1.0a2-py3-none-any.whl

Total release size: 229.3 kB

Release files / symprog-0.1.0a2.tar.gz

Download URL symprog-0.1.0a2.tar.gz
Size 119.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9ad325fe6d6f093dd194135918e499bcbb921913d8d8b951141ecf03142e002f
BLAKE2b-256 checksum
How to use checksums
58543c4c42c17bb89e85d1c0cb659c068a239f4a5a46f8000357c975fee9d73e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / symprog-0.1.0a2-py3-none-any.whl

Download URL symprog-0.1.0a2-py3-none-any.whl
Size 109.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd80e6a2b291fc3b8b9aeda0150a2104f3f7ec8d7d1d196153cacfded57da74a
BLAKE2b-256 checksum
How to use checksums
356dbf71d880bfcf5239b5275c16e08021eaa3ad14838a26d49099e3cb991a83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for symprog 0.1.0a2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page