Skip to main content

typedlogic

Project description

from typedlogic.registry import get_solver

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 (but can also be used independently of this)
  • Benefit from strong typing and mypy validation
  • Integration with multiple FOL solvers and logic programming engines
  • Interconversion between multiple syntaxes for FOL, Rules, and logical models
  • 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

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.1.2.tar.gz (71.7 kB view details)

Uploaded Source

Built Distribution

typedlogic-0.1.2-py3-none-any.whl (97.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typedlogic-0.1.2.tar.gz
  • Upload date:
  • Size: 71.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for typedlogic-0.1.2.tar.gz
Algorithm Hash digest
SHA256 fc652adad41c7c92994dbc1051d4e829493543548e0a0d4d32b2187fdf3905b5
MD5 1eba4e30df584cfe23a74fdf228b523a
BLAKE2b-256 45eb21fc4a9d34df370ef3c5bf1472cf74f9b006e673db4d0ff1de88677ef159

See more details on using hashes here.

File details

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

File metadata

  • Download URL: typedlogic-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 97.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for typedlogic-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c71be316f46d4eb1c348f60df4f2f61fb9e000025debbcf980bffc166905cc0d
MD5 1d0376d0f65917c0a8d62261315d9fda
BLAKE2b-256 710b559f27abdf2cb5e25fdaf3a2196bb18c1afc435b6e688a19746f2b1566fe

See more details on using hashes here.

Supported by

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