Inventory Invoice

Adding up a shopping bill, and being honest about what “all the lines” means.

inventory.js · output · proof · check · try it in the playground


The question

You buy three kinds of fruit:

All prices are in the same currency. Each line of the invoice is quantity times price, and the invoice adds up all the lines. Easy for a person; the interesting part is how a computer shows its work, including the step “I added up all the lines”.

What is the total on the invoice?


What we tell eyemind

// item(Name, Quantity, UnitPrice)
fact(item('apple', 3, 2))
fact(item('pear', 4, 3))
fact(item('plum', 2, 5))
implies([item(Name, Quantity, Price), is(Total, mul(Quantity, Price))], line_total(Name, Total))
// sum(Numbers, Total): Total is the sum of the list Numbers.
fact(sum([], 0))
implied_by(sum([X, ...Xs], Total), [sum(Xs, Rest), is(Total, add(X, Rest))])
implies([findall_sorted(Amount, line_total(_, Amount), Amounts), sum(Amounts, Total)], invoice(Total))
query(invoice(Total))

The collection must see every line total, so eyemind runs the findall_sorted rule only after all line totals have been derived: it puts that rule in a later stratum, a later stage of reasoning.


What eyemind concludes

// Answers: each instance of a query() that holds.
invoice(28)

The line totals are 3 × 2 = 6, 4 × 3 = 12 and 2 × 5 = 10, and 6 + 12 + 10 = 28.


The proof, line by line

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

// Answers: each instance of a query() that holds.
invoice(28)

// Steps: each derived fact and what it rests on: the clause or control that
// justifies it, the bindings and the goals it uses.
step(invoice(28), clause(7), {Amount: A, _0: B, Amounts: [6, 10, 12], Total: 28}, [findall_sorted(A, line_total(B, A), [6, 10, 12]), sum([6, 10, 12], 28)])
step(line_total('apple', 6), clause(4), {Name: 'apple', Quantity: 3, Price: 2, Total: 6}, [item('apple', 3, 2), is(6, mul(3, 2))])
step(line_total('pear', 12), clause(4), {Name: 'pear', Quantity: 4, Price: 3, Total: 12}, [item('pear', 4, 3), is(12, mul(4, 3))])
step(line_total('plum', 10), clause(4), {Name: 'plum', Quantity: 2, Price: 5, Total: 10}, [item('plum', 2, 5), is(10, mul(2, 5))])

// Certificate: the derived facts and every answer of each call the questions make.
derived(invoice(C), [invoice(28)])
derived(line_total(C, D), [line_total('apple', 6), line_total('pear', 12), line_total('plum', 10)])
called(invoice(A), [invoice(28)])
called(line_total(_1, C), [line_total('apple', 6), line_total('pear', 12), line_total('plum', 10)])
called(sum([6, 10, 12], D), [sum([6, 10, 12], 28)])
called(sum([10, 12], E), [sum([10, 12], 22)])
called(sum([12], F), [sum([12], 12)])
called(sum([], G), [sum([], 0)])

It has three parts:

Read the lines as sentences:

  1. The invoice is 28 — by rule 7, because the line totals, collected and sorted, are [6, 10, 12] and their sum is 28.
  2. The apple line totals 6 — by rule 4, because the apple item has quantity 3 and price 2, and 3 × 2 = 6.
  3. The pear line totals 12 — by rule 4, because 4 × 3 = 12.
  4. The plum line totals 10 — by rule 4, because 2 × 5 = 10.
  5. The derived line totals are exactly 6, 12 and 10, and the derived invoice is exactly 28: the two listings.
  6. The query has the one answer invoice(28); the collection’s call has the three line totals; and the sums of [6, 10, 12], [10, 12], [12] and [] are 28, 22, 12 and 0: the call tables.

The multiplications and additions have no steps: the checker computes them itself. Neither does the sum: it is a backward rule over stated facts, so its call tables are checked directly against clauses 5 and 6.

The collection is the interesting part. Each number in [6, 10, 12] is a line total, but “there are no others” is something eyemind knows only because it searched everything it had derived. A proof cannot point at a missing line, so the listing states every line total the forward rule derived, and the call table states every answer of the collection’s call. The listing proves which line totals were derived, not in what order. A plain findall lists its items in the order they were found, so over derived facts it could not be certified. findall_sorted sorts the totals, so its list depends only on which totals there are, and the checker can settle it.


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.
invoice(28)

verdict('checked')

The list [6, 10, 12] is all the line totals with respect to this program: add another item and the list would change. The verdict is checked: the invoice of 28 follows from the program, and it is the only answer of the query.


Try it

eyemind examples/inventory.js            # the answers
eyemind --proof examples/inventory.js    # answers with their proof
eyemind --goal 'line_total(Name, Total)' examples/inventory.js

The last command asks for the line totals, which the query does not show: line_total('apple', 6), line_total('pear', 12) and line_total('plum', 10).

Or open it in the playground. Add fact(item('fig', 5, 1)) and run again: the invoice becomes invoice(33), because the collection now gathers four line totals.


Takeaway

A total is only right if no line was left out. eyemind writes down which line totals it derived, and because the collection is sorted, the checker can confirm from that record that the list holds every one of them.