Skip to main content

Deterministic logic layer for AI agents — catch logical contradictions in system prompts, rules, and agent reasoning

Project description

boolean-algebra-engine-python

A deterministic boolean algebra engine — evaluates expressions, generates truth tables, synthesises minimal forms, and verifies logical consistency.

Forked from boolean-algebra-java — original Java implementation written during placement season.


What it does

Forward: expression → full truth table, exhaustive 2^n evaluation, exact.

Inverse: truth table → minimal boolean expression via Quine-McCluskey.

Verification: satisfiability, contradiction, tautology, equivalence, pairwise conflict detection across rule sets.


Operators

Symbol Operation Precedence
! NOT 4 (highest)
. AND 3
^ XOR 2
+ OR 1 (lowest)

Variables: uppercase AZ. Parentheses override precedence.


Architecture

┌──────────────────────────────────────────────────────┐
│                   Interface Layer                     │
│   CLI/REPL    MCP Server    REST API    Streamlit UI  │
└───────────────────────┬──────────────────────────────┘
                        │
┌───────────────────────┴──────────────────────────────┐
│                     NL Layer                          │
│   plain English → expression → plain English          │
│   Anthropic · OpenAI · Ollama · OpenAI-compat         │
└───────────────────────┬──────────────────────────────┘
                        │
┌───────────────────────┴──────────────────────────────┐
│                   Core Engine                         │
│   parser (shunting-yard) → evaluator (prefix stack)   │
│                         → synthesizer (Quine-McCluskey)│
└───────────────────────┬──────────────────────────────┘
                        │
┌───────────────────────┴──────────────────────────────┐
│             Acceleration Layer (planned)               │
│         numpy · CUDA · Redis                          │
└──────────────────────────────────────────────────────┘

core/ has zero external dependencies. Every layer above is a thin wrapper. Independently deployable, independently testable.


Project structure

core/
  models.py        TruthTable, TruthTableRow, PerformanceMetrics
  parser.py        Shunting-yard — infix → prefix, validation, variable extraction
  evaluator.py     Prefix stack evaluator — exhaustive 2^n row enumeration
  synthesizer.py   Quine-McCluskey — truth table → minimal SOP expression

mcp_server/
  server.py        5 tools for agent integration (evaluate, simplify,
                   equivalent, satisfiable, check_prompt_logic)

api/
  routes.py        FastAPI — 7 endpoints, Redis cache, optional auth

nl/
  nl.py            Provider abstraction — Anthropic, OpenAI, Ollama, OpenAI-compat

cli/
  main.py          typer + rich — REPL and one-shot, all output formats

ui/
  app.py           Streamlit — Expression, Rule Auditor, Plain English modes

tests/             90 tests — unit, integration, edge cases, round-trips

benchmark.py       LLM hallucination benchmark — engine as oracle
visualisations.ipynb  Colab notebook — complexity vs variables, conflict graph

Quickstart

git clone https://github.com/Shrivastava-Aditya/boolean-algebra-engine-python
cd boolean-algebra-engine-python
pip install -e ".[dev]"
python3 -m pytest tests/

Core usage

from core.evaluator import evaluate
from core.synthesizer import synthesize

table, _ = evaluate("A.(B+C)")
print(table.variables)    # ['A', 'B', 'C']
print(table.minterms)     # [5, 6, 7]
print(table.satisfiable)  # True

minimal, _ = synthesize(table)
print(minimal)            # A.C+A.B

check_prompt_logic

from mcp_server.server import check_prompt_logic

result = check_prompt_logic([
    "A.B",   # approve: good credit AND income verified
    "C",     # approve: collateral exists
    "!A",    # reject:  bad credit
    "!B.!C", # reject:  no income AND no collateral
])
print(result["summary"])
# {'total': 4, 'contradictions': 0, 'conflicting_pairs': 3}

Benchmark

Measures LLM hallucination rate on boolean logic. Engine is the oracle — ground truth by exhaustive enumeration, no human labelers.

ollama pull tinyllama
python3 benchmark.py

First result: tinyllama (1B) · 3 variables · 10 cases · 40% hallucination rate.

See FAILURES.md for real-world severity analysis of each failure.


Branches

Branch What
master Project — engine, interfaces, tests
product-readme Product brief — what it proves, what it's for
benchmark Benchmark methodology, multi-model results (in progress)

Related

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

boolean_algebra_engine-0.1.2.tar.gz (39.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

boolean_algebra_engine-0.1.2-py3-none-any.whl (37.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: boolean_algebra_engine-0.1.2.tar.gz
  • Upload date:
  • Size: 39.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for boolean_algebra_engine-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4e87b524145e7966d950fbbf2dcae233cece6db7bf9c406258d771b2c9be1e9d
MD5 4c572d0bb608939e25a9f8863af2ba9b
BLAKE2b-256 2037b0cbd9e341d7a603cf3d26fecf789a7e7cdc88826edaed2ee320b6b5778b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for boolean_algebra_engine-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fc270e6381ee38afd58bb7f16e710cf4f7e2e8864706b558ff0deb81d9c9bd03
MD5 a3ce744a2603add8c4c57279022aa436
BLAKE2b-256 a1770592bf9f222f7d6ecb43427977a89a8d9cf8b15bcaf984f7ad33a91d9702

See more details on using hashes here.

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