Skip to main content

typedlogic

Project description

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

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

typedlogic-0.2.2.tar.gz (846.8 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.2-py3-none-any.whl (158.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typedlogic-0.2.2.tar.gz
  • Upload date:
  • Size: 846.8 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.2.tar.gz
Algorithm Hash digest
SHA256 230fb3f0099f3e3ee335788e4776a0f5f8ba2cc1ed200cf528f8ffeb56b2de14
MD5 54bdc3fd36e7a117d59562e4c5808ffe
BLAKE2b-256 2a9655b4e6ebddd8d96f8bc7857fa2d94e4df99b277e0fd77249fe2bdc661f9e

See more details on using hashes here.

Provenance

The following attestation bundles were made for typedlogic-0.2.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: typedlogic-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 158.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 320558930d2e5dfa50d19246c500e79b1eea9d25fbf69baed38c455a4e8b8beb
MD5 f653decb09ec06b962977dad0211bf35
BLAKE2b-256 fe038a580d1a5a08d1ebe6771edd0de462ebd090f45ad2cfbfa2e39018829ce3

See more details on using hashes here.

Provenance

The following attestation bundles were made for typedlogic-0.2.2-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