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
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file typedlogic-0.1.3.tar.gz
.
File metadata
- Download URL: typedlogic-0.1.3.tar.gz
- Upload date:
- Size: 86.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7625b915aa03da7bedbaae60c320d6cf8425118320c50841c40098ed4fd46f28 |
|
MD5 | 791c2fde5e6179e1ae6a77dbbd6ff2a3 |
|
BLAKE2b-256 | d48aae1d2b0fd9aec0aa3a28a3be5a9a106c65af5d4a38f8f43d0f8be10eae16 |
Provenance
The following attestation bundles were made for typedlogic-0.1.3.tar.gz
:
Publisher:
pypi-publish.yml
on py-typedlogic/py-typedlogic
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
typedlogic-0.1.3.tar.gz
- Subject digest:
7625b915aa03da7bedbaae60c320d6cf8425118320c50841c40098ed4fd46f28
- Sigstore transparency entry: 148287569
- Sigstore integration time:
- Predicate type:
File details
Details for the file typedlogic-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: typedlogic-0.1.3-py3-none-any.whl
- Upload date:
- Size: 118.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9207db18a0314f152f129e391ffdea3cc597ec6afc789b295dcbab6f30e01b8f |
|
MD5 | eec5360a859f90a01aec7476ddfc9abb |
|
BLAKE2b-256 | 2e258d97a183492ee4395782334a0937433587266c9eb2bf6902ac12abe9fb6c |
Provenance
The following attestation bundles were made for typedlogic-0.1.3-py3-none-any.whl
:
Publisher:
pypi-publish.yml
on py-typedlogic/py-typedlogic
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
typedlogic-0.1.3-py3-none-any.whl
- Subject digest:
9207db18a0314f152f129e391ffdea3cc597ec6afc789b295dcbab6f30e01b8f
- Sigstore transparency entry: 148287570
- Sigstore integration time:
- Predicate type: