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
- Consult the main docs for more information
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
230fb3f0099f3e3ee335788e4776a0f5f8ba2cc1ed200cf528f8ffeb56b2de14
|
|
| MD5 |
54bdc3fd36e7a117d59562e4c5808ffe
|
|
| BLAKE2b-256 |
2a9655b4e6ebddd8d96f8bc7857fa2d94e4df99b277e0fd77249fe2bdc661f9e
|
Provenance
The following attestation bundles were made for typedlogic-0.2.2.tar.gz:
Publisher:
pypi-publish.yml on py-typedlogic/py-typedlogic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typedlogic-0.2.2.tar.gz -
Subject digest:
230fb3f0099f3e3ee335788e4776a0f5f8ba2cc1ed200cf528f8ffeb56b2de14 - Sigstore transparency entry: 1551892760
- Sigstore integration time:
-
Permalink:
py-typedlogic/py-typedlogic@e21c6af52ff329878ced2b2d4024901774140fb6 -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/py-typedlogic
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@e21c6af52ff329878ced2b2d4024901774140fb6 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
320558930d2e5dfa50d19246c500e79b1eea9d25fbf69baed38c455a4e8b8beb
|
|
| MD5 |
f653decb09ec06b962977dad0211bf35
|
|
| BLAKE2b-256 |
fe038a580d1a5a08d1ebe6771edd0de462ebd090f45ad2cfbfa2e39018829ce3
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typedlogic-0.2.2-py3-none-any.whl -
Subject digest:
320558930d2e5dfa50d19246c500e79b1eea9d25fbf69baed38c455a4e8b8beb - Sigstore transparency entry: 1551892775
- Sigstore integration time:
-
Permalink:
py-typedlogic/py-typedlogic@e21c6af52ff329878ced2b2d4024901774140fb6 -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/py-typedlogic
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@e21c6af52ff329878ced2b2d4024901774140fb6 -
Trigger Event:
release
-
Statement type: