Skip to main content

py-typedlogic: Pythonic logic for your data models.

Define logical predicates directly in Python as Pydantic, dataclasses, SQLModel, or plain python objects:

# links.py
from pydantic import BaseModel
from typedlogic import FactMixin, Term

ID = str

class Link(BaseModel, FactMixin):
    """A link between two entities"""
    source: ID
    target: ID

class Path(BaseModel, FactMixin):
    """An N-hop path between two entities, consisting of one or more links"""
    source: ID
    target: ID
    hops: int

This data model has two classes, Link and Path. These also correspond to predicate signatures in a logical theory.

You can use this to create objects (ground terms, in logic terms) using normal Python code:

links = []
for source, target in [('CA', 'OR'), ('OR', 'WA')]:
   links.append(link)

Define logical constraints or rules using Python syntax:

from typedlogic.decorators import axiom

@axiom
def path_from_link(x: ID, y: ID):
    """If there is a link from x to y, there is a path from x to y"""
    if Link(source=x, target=y):
        assert Path(source=x, target=y, hops=1)

@axiom
def transitivity(x: ID, y: ID, z: ID, d1: int, d2: int):
    """Transitivity of paths, plus hop counting"""
    if Path(source=x, target=y, hops=d1) and Path(source=y, target=z, hops=d2):
        assert Path(source=x, target=z, hops=d1+d2)

Use a solver to infer new facts:

from typedlogic.registry import get_solver
from links import Link

solver = get_solver("clingo")
solver.load(links) 
links = [Link(source='CA', target='OR'), Link(source='OR', target='WA')]
for link in links:
    solver.add(link)
model = solver.model()
for fact in model.iter_retrieve("Path"):
    print(fact)

prints:

Path(source='CA', target='OR')
Path(source='OR', target='WA')
Path(source='CA', target='WA')

Key Features

  • Write logical axioms and rules using Python syntax
  • Interconvert between different logical formats and formalisms
  • Benefit from strong typing and mypy validation
  • Integration with multiple FOL solvers and logic programming engines
  • Integration with OWL-DL
  • Integration with Python libraries like Pydantic
  • Command Line and Python interfaces

Installation

Install TypedLogic using pip:

pip install "typedlogic"

Next Steps

Contributing

Testing with External Dependencies

Some tests require external executables (Prover9, Souffle) that may not be available in all environments. The test suite is designed to automatically skip tests that require unavailable dependencies.

When running in CI environments:

  • Tests for external solvers are automatically skipped if the dependency is not available
  • This allows CI to pass while still testing all available functionality

To install optional dependencies for testing:

  • Prover9: Install from your package manager or from source
  • Souffle: Install from your package manager or from source

Run tests with:

uv run pytest

Download files

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

Source Distribution

typedlogic-0.2.3.tar.gz (922.5 kB view details)

Uploaded Source

Built Distribution

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

typedlogic-0.2.3-py3-none-any.whl (200.3 kB view details)

Uploaded Python 3

File details

Details for the file typedlogic-0.2.3.tar.gz.

File metadata

  • Download URL: typedlogic-0.2.3.tar.gz
  • Upload date:
  • Size: 922.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for typedlogic-0.2.3.tar.gz
Algorithm Hash digest
SHA256 f0f65606f99815766ec3e5a2def2fb5eafde96c1a8285247d996f11527020a63
MD5 ef7c653cf43f78bf46a2e2bd4073f55c
BLAKE2b-256 d08376ad1bb551717c47b26b662484c2edeb01513c935acbb95a6865a3811241

See more details on using hashes here.

Provenance

The following attestation bundles were made for typedlogic-0.2.3.tar.gz:

Publisher: pypi-publish.yml on py-typedlogic/py-typedlogic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file typedlogic-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: typedlogic-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 200.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for typedlogic-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4ca7aec80dfa9c41801791b5ef3c9b54efcc07f957d783cf1b21611deca3e9db
MD5 f09f33f6d048919db3c625c23033b6cd
BLAKE2b-256 bcb64b68b7e00aa8c3724bdbd01b779a84b8cbf11d170a4fc1e8bf462707176a

See more details on using hashes here.

Provenance

The following attestation bundles were made for typedlogic-0.2.3-py3-none-any.whl:

Publisher: pypi-publish.yml on py-typedlogic/py-typedlogic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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