concept

Knowledge representation and chaining

Finite positive ground rule reasoning: forward chaining saturates facts; backward chaining searches alternative proofs with branch-local cycle protection. A failed query is not explicit negation. Teaching interfaces, not Python built-ins.

Formal Syntax & Structure

artificial-intelligence syntaxartificial-intelligence
forward_chain(facts, rules) → sorted closure; backward_chain(facts, rules, goal) → bool

Working Example

Example Snippetartificial-intelligence
def backward_chain(facts, rules, goal):    known = set(facts)
    def prove(atom, active):        if atom in known:            return True        if atom in active:            return False        branch = active | {atom}        for premises, head in rules:            if head == atom and all(prove(p, branch) for p in premises):                return True        return False
    return prove(goal, set())
facts = ["low_battery", "failed_start", "manual_available"]rules = [(["check_battery", "manual_available"], "review_manual"),         (["low_battery", "failed_start"], "check_battery")]print(backward_chain(facts, rules, "review_manual"))print(backward_chain(facts, rules, "replace_battery"))cycle = [(["b"], "a"), (["a"], "b"), (["seed"], "b")]print(backward_chain(["seed"], cycle, "a"))print(backward_chain([], cycle, "a"))