Dog license

A rule that depends on counting, and an honest note about the count.

dog-license.js · output · proof · check · try it in the playground


The question

Imagine a town rule: anyone who owns more than four dogs needs a dog license. The threshold is made up for this example, not taken from any real town’s rules.

Alice owns five dogs, Bob owns two. To apply the rule we must count each person’s dogs, and counting means being sure we saw every dog.

Who needs a dog license?


What we tell eyemind

fact(owns('alice', 'dog1'))
fact(owns('alice', 'dog2'))
fact(owns('alice', 'dog3'))
fact(owns('alice', 'dog4'))
fact(owns('alice', 'dog5'))
fact(owns('bob', 'dog6'))
fact(owns('bob', 'dog7'))
fact(owner('alice'))
fact(owner('bob'))
// The length of a list, counted recursively.
fact(length([], 0))
implied_by(length([_, ...Xs], N), [length(Xs, Before), is(N, add(Before, 1))])
implies(
  [owner(Owner), findall(Dog, owns(Owner, Dog), Dogs), length(Dogs, N)],
  dog_count(Owner, N),
)
implies([dog_count(Owner, N), gt(N, 4)], requires(Owner, 'dog_license'))

What eyemind concludes

// Conclusions: the facts the program derives, in the order derived.
dog_count('alice', 5)
dog_count('bob', 2)
requires('alice', 'dog_license')

The program has no query, so eyemind lists every fact it derived. Alice has 5 dogs and needs a license. Bob has 2 and does not: since 2 is not more than 4, no license conclusion is drawn for him.


The proof, line by line

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

// Conclusions: the facts the program derives, in the order derived.
dog_count('alice', 5)
dog_count('bob', 2)
requires('alice', 'dog_license')

// Steps: each derived fact and what it rests on: the clause or control that
// justifies it, the bindings and the goals it uses.
step(dog_count('alice', 5), clause(12), {Owner: 'alice', Dog: A, Dogs: ['dog1', 'dog2', 'dog3', 'dog4', 'dog5'], N: 5}, [owner('alice'), findall(A, owns('alice', A), ['dog1', 'dog2', 'dog3', 'dog4', 'dog5']), length(['dog1', 'dog2', 'dog3', 'dog4', 'dog5'], 5)])
step(dog_count('bob', 2), clause(12), {Owner: 'bob', Dog: A, Dogs: ['dog6', 'dog7'], N: 2}, [owner('bob'), findall(A, owns('bob', A), ['dog6', 'dog7']), length(['dog6', 'dog7'], 2)])
step(requires('alice', 'dog_license'), clause(13), {Owner: 'alice', N: 5}, [dog_count('alice', 5), gt(5, 4)])

// Certificate: the derived facts and every answer of each call the questions make.
derived(dog_count(B, C), [dog_count('alice', 5), dog_count('bob', 2)])
derived(requires(B, C), [requires('alice', 'dog_license')])
called(dog_count(A, B), [dog_count('alice', 5), dog_count('bob', 2)])
called(length(['dog1', 'dog2', 'dog3', 'dog4', 'dog5'], C), [length(['dog1', 'dog2', 'dog3', 'dog4', 'dog5'], 5)])
called(length(['dog2', 'dog3', 'dog4', 'dog5'], D), [length(['dog2', 'dog3', 'dog4', 'dog5'], 4)])
called(length(['dog3', 'dog4', 'dog5'], E), [length(['dog3', 'dog4', 'dog5'], 3)])
called(length(['dog4', 'dog5'], F), [length(['dog4', 'dog5'], 2)])
called(length(['dog5'], G), [length(['dog5'], 1)])
called(length([], H), [length([], 0)])
called(length(['dog6', 'dog7'], C), [length(['dog6', 'dog7'], 2)])
called(length(['dog7'], I), [length(['dog7'], 1)])
called(requires(J, 'dog_license'), [requires('alice', 'dog_license')])

It has three parts:

Read the steps as sentences:

The owners, the collections, the lengths and the comparison have no steps of their own. Owners are stated facts. Each length call is answered by its table, and the additions of 1 in between are computed by the checker. The collection is the interesting part: “Alice’s dogs are exactly dog1 to dog5” includes “there are no other dogs”, which no list of steps could show. Here none is needed: owns is defined only by facts, so the checker scans those facts itself and finds exactly the dogs in the list, in the same order.


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 answers each question from its call table and checks each table by applying the program one level, computing the additions itself. It requires each listing to be closed under the forward rules: every count and license the rules give must be listed. And it replays each listed fact’s step from its rule, re-running the findall over the owns facts and the test 5 > 4. A step, table or listing the check does not use fails it. This is the complete report:

// Checked answers: these follow from the input program and are every answer of its questions.
dog_count('alice', 5)
dog_count('bob', 2)
requires('alice', 'dog_license')

verdict('checked')

Neither collection is taken on trust: the checker reads the facts and confirms that Alice’s dogs are exactly dog1 to dog5, and Bob’s exactly dog6 and dog7. “Complete” means complete with respect to this program: add a dog and the list changes. The verdict is checked: the conclusions follow from the program, and they are every answer of the original questions.


Try it

eyemind examples/dog-license.js
eyemind --check-proof examples/proof/dog-license.js examples/dog-license.js

The second command checks the saved proof and prints the report above, ending with verdict('checked').

Or open it in the playground. Add fact(owns('bob', 'dog8')), fact(owns('bob', 'dog9')) and fact(owns('bob', 'dog10')), and run again: Bob’s count becomes 5 and he needs a license too. Or lower the threshold by changing gt(N, 4) to gt(N, 1): Bob’s two dogs are then enough.


Takeaway

Counting is a claim about everything you know. eyemind does the count, shows it, and tells you plainly which part rests on “that was the full list”.