Reasoning about a rule

The rule itself can be evidence.

rule-inspection.js · output · proof · check · playground


The question

Usually a program uses its rules to draw conclusions. Sometimes we want to talk about a rule instead: “which rules does this program contain, and what do they say?” The program here has one ordinary rule: anything that is a is also c.

Can a second rule look that rule up and report it, keeping the link between its two halves?


What we tell eyemind

fact(a('b'))
implies(a(X), c(X))

implies(implies(X, Y), test(X, Y))
query(test(X, Y))

fact(...) states something true: b is a. implies(premise, conclusion) is a forward rule: whenever the premise holds, eyemind concludes the conclusion. Capitalized names such as X are variables, placeholders that stand for any value. So line 2 reads “whatever X is, if X is a then X is c.”

The third line is the unusual one. Its premise is itself implies(X, Y). Inside a premise, implies does not prove anything: it looks up the forward rules written in the program, and sets X to a rule’s premise and Y to its conclusion. This is called rule inspection. (implied_by(Head, Body) does the same for backward rules.)

query(test(X, Y)) asks eyemind to report every test(...) it can establish.


What eyemind concludes

// Answers: each instance of a query() that holds.
test(a(A), c(A))

The first line is a comment saying what follows. The answer is the rule from line 2 written as data: its premise a(A) and its conclusion c(A). The same A appears in both halves, so the answer still says “the same thing” on both sides: the link between premise and conclusion is kept. The A is a fresh variable, a copy of the rule’s own X.

Forward reasoning also derives c('b') from the first two lines, but the query only asks for test(...). Inspection does not need the fact a('b'): it reads the rule, not the facts.


Which rules can be inspected

The inspecting rule could in principle find itself, which would make no sense. To avoid that, eyemind only offers a fixed list of ordinary rules written in the program. Facts, queries, constraints, rules that eyemind generates internally, and rules that themselves inspect rules are left out. So here the only rule the third line can find is line 2. Each time a rule is inspected its variables are copied fresh, so two inspections never get tangled up.


The proof, line by line

eyemind --proof writes down how it reached each answer. This is the complete proof:

// Answers: each instance of a query() that holds.
test(a(A), c(A))

// Steps: each derived fact and what it rests on: the clause or control that
// justifies it, the bindings and the goals it uses.
step(test(a(B), c(B)), clause(3), {X: a(B), Y: c(B)}, [implies(a(B), c(B))])

// Certificate: the derived facts and every answer of each call the questions make.
derived(test(C, D), [test(a(B), c(B))])
called(test(A, B), [test(a(C), c(C))])

It has three parts:

Read the step as a sentence: test(a(B), c(B)) holds — by clause 3, with X being a(B) and Y being c(B), because the program contains the rule implies(a(B), c(B)). That use needs no step of its own: it is not a fact or a calculation but a quotation of clause 2 of the program itself, which the checker reads from the program.


Checked: what the report says

A proof is only worth something if someone else can check it. eyemind -c generates the proof and checks it against the program; --check-proof checks a saved proof. The checker never reasons by itself: it reads the clauses from the program and confirms the proof against them in one procedure:

This is the complete report:

// Checked answers: these follow from the input program and are every answer of its questions.
test(a(A), c(A))

verdict('checked')

The verdict is checked: the reported rule really is in the program, the answer follows from it, and it is every answer of the query.


Try it

eyemind examples/rule-inspection.js
eyemind --proof examples/rule-inspection.js
eyemind -c examples/rule-inspection.js
eyemind --check-proof examples/proof/rule-inspection.js examples/rule-inspection.js

Replace c(X) with d(X) in the ordinary rule: the answer becomes test(a(A), d(A)), following the rule as written. Delete the fact a('b'): the answer stays test(a(A), c(A)), because inspection reads the rule, not the facts. Add a second rule implies(b(X), d(X)): the query then reports both rules, test(a(A), c(A)) and test(b(B), d(B)).


Takeaway

Rules are not only instructions; they can also be facts to look at. When a rule reports on another rule, the proof points to the exact rule it read, and the checker confirms the report matches it.