Skip to main content

A logic-based Truth Maintenance System and pattern-directed reasoning engine in Python, after Forbus & de Kleer, Building Problem Solvers.

Project description

ltms

A logic-based Truth Maintenance System (LTMS) and a pattern-directed reasoning engine in pure Python, after Forbus & de Kleer, Building Problem Solvers (MIT Press, 1993).

The LTMS maintains belief over a set of propositional clauses using Boolean Constraint Propagation (unit propagation), records well-founded support for every derived value, performs dependency-directed backtracking on contradictions, and can explain why anything is believed. A small forward-chaining rule engine sits on top of it.

Documentation site: https://pisanuw.github.io/ltms/ (built from docs/).

New to this? Start with the study companion — a chapter-by-chapter walk-through of the concepts, examples, and exercise solutions. See also PLAN.md for the build plan and STUDY-NOTES.md for the technical digest.

Why

There is no faithful Python LTMS in the wild — JTMS/ATMS have a few toy ports, but the clausal-BCP LTMS with dependency-directed backtracking is the least-ported truth maintenance system outside Lisp/Racket. This is a clean, tested, idiomatic-Python implementation.

What's here

Layer Module What it gives you
Terms + unification ltms.terms, ltms.unify s-expression terms, occurs-checked unification
TRE ltms.tre pattern-directed forward chaining (no belief revision)
JTMS + JTRE ltms.jtms, ltms.jtre justification-based belief, IN/OUT, two-phase retraction
LTMS core ltms.core, ltms.normalize clausal Boolean Constraint Propagation, assumptions, nogoods, CNF
LTRE ltms.ltre reasoning engine: assert!/assume!/retract!, belief-conditioned rules
Facilities ltms.indirect, ltms.cwa, ltms.dds indirect proof, closed-world assumptions, dependency-directed search
Completeness ltms.cltms prime implicates / complete() (optional logical completeness)
Watched literals ltms.watched WatchedLTMS, the SAT-style 2-watched-literals BCP engine
Explanation ltms.explain why_node, explain_node (well-founded proofs)
File DSL ltms.dsl read world models from .kb files, separate from Python

Usage

from ltms import LTRE

e = LTRE()
e.assert_(("or", ("p",), ("q",)))   # p v q
e.assert_(("not", ("p",)))          # ~p
e.is_true(("q",))                   # True  (unit propagation)

e.assume(("rain",), "guess")        # retractable assumption
e.assert_(("implies", ("rain",), ("wet",)))
e.is_true(("wet",))                 # True
e.retract(("rain",), "guess")
e.is_unknown(("wet",))              # True  (belief revised)

World models in files

The world model can live in a .kb data file, separate from Python:

# examples/kb/belief_revision.kb
rain         -> wet ground
sprinkler on -> wet ground
assume rain
expect wet ground true
from ltms.dsl import load_kb_file
load_kb_file("examples/kb/belief_revision.kb")   # runs it; `expect` lines self-check

See examples/ for runnable TRE, LTRE, and dependency-directed-search demos, and exercises/ for worked solutions to the book's chapter exercises (paraphrased statements + original answers/code).

Install (development)

python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"      # add ".[dev,sat]" for the PySAT differential tests
pytest

Notes

BCP uses the book's incremental pvs/sats counters (sound but, by design, not logically complete). A watched-literals rewrite and the completeness extension (CLTMS, prime implicates) are tracked as future work in PLAN.md.

Provenance & licensing

Original code in this repository is MIT licensed (see LICENSE).

This is an independent reimplementation of the algorithms in Building Problem Solvers; it does not copy the original Common Lisp source. The book and its reference code are available from the Northwestern Qualitative Reasoning Group at https://www.qrg.northwestern.edu/BPS/readme.html, where the full-text PDF is offered for free download "thanks to the gracious permission of MIT Press" (MIT Press retains print rights). The book PDF and other reference material are kept local and are not redistributed here. See NOTICE for full attribution.

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

ltms-0.1.0.tar.gz (357.6 kB view details)

Uploaded Source

Built Distribution

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

ltms-0.1.0-py3-none-any.whl (41.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ltms-0.1.0.tar.gz
  • Upload date:
  • Size: 357.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for ltms-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f9f7b7e0250bb06e525cc52e64d99c63bde0b47b964df633e102fe2b93825198
MD5 fa6f48dc16096efc8db5c8695a31bc33
BLAKE2b-256 3d07e43dd0c08b1e21296e3a83479760e6e9eb9d0a12f9a4ae406d4f44fcb0b0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ltms-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 41.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for ltms-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df54b3771f4eada59143a65d221d8e1ee388c38458c6f15a64a3501ca59be5c5
MD5 cb06132fdbb3620360008705ac5f336e
BLAKE2b-256 6998543cb7b33d3d063aa806b0b43634e87f915d4b056486da0fe2fbc8ae4336

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