Skip to main content

ASPish - Answer Set Programming for Python

ASPish is a library that aims to provide some parts of ASP (Answer Set Programming) in a Python-friendly way. All the heavy lifting is done by clingo, a mature implementation of ASP. This library merely provides an interface that allows for a more streamlined usage from Python than the official Python bindings.

ASP can be approximately viewed as the combination of query language that extends datalog, and a satisfiability solver. Both are integrated into a single coherent language which makes ASP particularly convenient when dealing with relational data.

This project is in early stages and is not stable.

Basic Usage

from aspish import Solver, function_, var

# declarations
edge = function_('edge', ('x', 'y'))
path = function_('path', ('x', 'y'))

solver = Solver()
X, Y, Z = map(var, 'XYZ')

# add facts
solver.add(
    edge(1, 2),
    edge(2, 3)
)

# add rules
solver.add(
    path(X, Y) << edge(X, Y),
    path(X, Y) << (
        edge(X, Z),
        path(Z, Y)
    )
)

# run
solver.solve()
solver.get(path)

# returns
[path(x=1, y=2), path(x=2, y=3), path(x=1, y=3)]

Integrating with external data

Usually the input data will not be entered directly. Instead, we pull facts from a source. Similarly, output data will be converted into a different format. Conversion into and out of aspish facts is not complicated when restricting to atomic attributes only, i.e. when not using nesting like f(f(1), g(h(2))). Then an aspish function is equivalent to a table or dataframe with no nulls and no duplicates.

Here's a minimal example how we can get valid data out of a pyspark DataFrame:

from pyspark.sql import DataFrame
from aspish import signature
from aspish.language import Function

def df2asp(df: DataFrame, func: type[Function]) -> list[Function]:
    sig = signature(func)
    clean = (
        df
        .select(*sig)
        dropna()
        .distinct()
    )
    return [func(**row.asDIct()) for row in clean.collect()]

And here's how to convert a collection of functions back to pyspark. We use the fact that all functions created with apish.function_ are simple dataclasses which can be handled by pandas:

from typing import Iterable
import pandas as pd
from pyspark.sql import SparkSession, DataFrame
from aspish.language import Function


def asp2df(data: Iterable[Function], spark: SparkSession) -> DataFrame:
    return spark.createDataFrame(pd.DataFrame(data))

Release files for aspish 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aspish 0.9.0
File Size Uploaded
aspish-0.9.0.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aspish 0.9.0
File Interpreter ABI Platform
aspish-0.9.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.4 kB

Release files / aspish-0.9.0.tar.gz

Download URL aspish-0.9.0.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a7ace655d19ea37ec94bed9722f660dcb16a67931ac5d08e27769f05fa8fbea8
BLAKE2b-256 checksum
How to use checksums
b38e83e0e4e1c35e3d7032a7fb22a5df5cfeb6a86cffed90fa9e3fbe26169103
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.14.6 Darwin/24.6.0

Release files / aspish-0.9.0-py3-none-any.whl

Download URL aspish-0.9.0-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1b7f21629538c0fdbe00501632c737b0777ff6acc1fdbe76a7fc1f19bdf7c5fd
BLAKE2b-256 checksum
How to use checksums
487d49f4bbf2fce49f87ecdf0359a824c28594c59b7472d4a121eb8f3303c6d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.14.6 Darwin/24.6.0

Release history Release notifications | RSS feed

This release

0.9.0 This release

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page