Two ways out of Paris, and three ways of asking about them.
alternatives.py · output · proof · check · try it in the playground
From Paris you can take a train to Brussels, or a bus to Lille.
Everyday questions about choices come in different shapes:
This example asks all three, and shows the reasoning behind each answer.
Two facts, and two rules that both define a route:
fact(train('paris', 'brussels'))
fact(bus('paris', 'lille'))
implied_by(route(From, To), train(From, To))
implied_by(route(From, To), bus(From, To))
Two rules with the same name are alternatives: a route is a train
connection, or a bus connection. The implied_by means “work this out when
someone asks”.
query(route('paris', To))
query(train('paris', 'brussels') | bus('paris', 'lille'))
query(once(route('paris', To)))
To reachable from Paris.|, which means or: is either connection there?once: find the first route and commit to
it, without looking for more.A line of the form query(Question) tells peye to answer that question.
route('paris', 'brussels')
route('paris', 'lille')
train('paris', 'brussels') | bus('paris', 'lille')
once(route('paris', 'brussels'))
once picked Brussels, because the train rule comes first, and stopped there.A separate checker reads the proof against the program. The report says:
once) checked by confirming the step they rest on.Verdict: checked.
python -m peye examples/alternatives.py # the answers
python -m peye --proof examples/alternatives.py # answers with their proof
Or open it in the playground.
Add fact(train('paris', 'amsterdam')) after the bus line and run again: a third
route, route('paris', 'amsterdam'), appears, but once still answers Brussels,
because that train fact still comes first.
“All of them”, “at least one” and “just the first” are different questions. peye lets you say which one you mean, and its proof shows exactly which option answered.