Skip to main content

Boolean algebra engine — evaluate expressions, generate truth tables, synthesize minimal forms

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.0.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.0-py3-none-any.whl (37.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: boolean_algebra_engine-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 34bff831131d0b4177ade2940a447b65c37e00400750aa6d10a45d54151a2300
MD5 01fc683bbe29f8c6ec3309bfa8d49d75
BLAKE2b-256 713cd893bece3ab3f44b11dda22745d446001a6694f6d2f71a522e6f75a3f675

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for boolean_algebra_engine-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24a36c3a09409fab7b3fe1f593248852181a96fafac35ae9de90d2adf2fcfe70
MD5 c3d31d116da157673396d1cef29cdc69
BLAKE2b-256 fb65095c7c822487bf27d99bdab966e9592fe6c299ef5d917170841f339d6ded

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