Skip to main content

Metaprogramming infrastructure

Project description

Effectful is an algebraic effect system for Python, intended for use in the implementation of probabilistic programming languages. It is a core component of the ChiRho causal modeling language.

Installation

Install From Source

git clone git@github.com:BasisResearch/effectful.git
cd effectful
git checkout master
pip install -e .[pyro]

Install With Optional PyTorch/Pyro Support

effectful has optional support for:

  • PyTorch (tensors with named dimensions)

  • Pyro (wrappers for Pyro effects)

  • Jax (tensors with named dimensions)

  • Numpyro (operations for Numpyro distributions)

To enable PyTorch support:

pip install effectful[torch]

Pyro support (which includes PyTorch support):

pip install effectful[pyro]

Jax support:

pip install effectful[jax]

Numpyro support (which includes Jax support):

pip install effectful[numpyro]

Getting Started

Here’s an example demonstrating how effectful can be used to implement a simple DSL that performs arithmetic on terms with free variables.

import functools

from effectful.ops.types import Term
from effectful.ops.syntax import defdata, defop
from effectful.ops.semantics import handler, evaluate, coproduct, fwd

add = defdata.dispatch(int).__add__

def beta_add(x: int, y: int) -> int:
    match x, y:
        case int(), int():
            return x + y
        case _:
            return fwd()

def commute_add(x: int, y: int) -> int:
    match x, y:
        case Term(), int():
            return y + x
        case _:
            return fwd()

def assoc_add(x: int, y: int) -> int:
    match x, y:
        case _, Term(op, (a, b)) if op == add:
            return (x + a) + b
        case _:
            return fwd()

beta_rules = {add: beta_add}
commute_rules = {add: commute_add}
assoc_rules = {add: assoc_add}

eager_mixed = functools.reduce(coproduct, (beta_rules, commute_rules, assoc_rules))

We can represent free variables as operations with no arguments, generated using defop:

>>> x = defop(int, name="x")
>>> y = defop(int, name="y")

If we evaluate an expression containing free variables, we get a term:

>>> e = 1 + 1 + (x() + 1) + (5 + y())
>>> print(e)
add(2, add(add(x(), 1), add(5, y())))

We can make the evaluation strategy smarter by taking advantage of the commutativity and associativity of addition, as expressed by the commute_add and assoc_add handlers.

>>> with handler(eager_mixed):
>>>     print(evaluate(e))
add(8, add(x(), y()))

Learn More

More examples and API documentation can be found in the docs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

effectful-0.2.1.tar.gz (94.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

effectful-0.2.1-py3-none-any.whl (61.8 kB view details)

Uploaded Python 3

File details

Details for the file effectful-0.2.1.tar.gz.

File metadata

  • Download URL: effectful-0.2.1.tar.gz
  • Upload date:
  • Size: 94.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for effectful-0.2.1.tar.gz
Algorithm Hash digest
SHA256 bb64bde23c521e4645225333acbb09b723b2f3f2e7c226612b3ae903e694a40c
MD5 0ffe76692f822ab5bce080efa468d844
BLAKE2b-256 5abaef6a8f60e34b6082480dde21d5d8ab3e1a1694af489691bf4ccdcbfb7fcd

See more details on using hashes here.

File details

Details for the file effectful-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: effectful-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 61.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for effectful-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2f81931bb1aee57a3a68151d64d60ff004420a64686fdc9fff71e7020a856905
MD5 c54de54362b2968f5b28f3e0c7b5b411
BLAKE2b-256 894d8fb17487473d8adc1d75c25238bc799c2fa31b5cb1e731844ba5fd15d27d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page