In the process of learning Prolog I’m making simple program which given some thing is able to tell what is on the left / right / above / below of that thing.
Code is this:
% left_(LeftThing, ToThing)
left_(bicycle, camera).
left_(pencil, clock).
left_(clock, butterfly).
left_(butterfly, fish).
% above_(AboveThing, ToThing)
above_(bicycle, pencil).
above_(camera, butterfly).
right(X, Y) :-
left_(Y, X);
X = (empty).
below(X, Y) :-
above_(Y, X);
X = (empty).
left(X, Y) :-
left_(X, Y);
X = (empty).
above(X, Y) :-
above_(X, Y);
X = (empty).
position(Thing, Left, Right, Above, Below) :-
left(Left, Thing),
right(Right, Thing),
above(Above, Thing),
below(Below, Thing), !.
Now when I ask for :
?- position(clock, Left, Right, Above, Below).
I get an answer:
Left = pencil, Right = butterfly, Above = Below, Below = empty.
While this is perfectly correct answer, I would like Prolog to make full substitutions and give an output in standard way:
Left = pencil, Right = butterfly, Above = empty, Below = empty.
Is there a way to force Prolog doing such substitutions in output without cluttering a lot with program facts and rules ? (err… maybe there is some results formatting option in SWI-Prolog configuration. If not – how to achieve this with minimal code changes ?)
Prolog returns a most general unifier as a solution when it finds one. Here it is a perfectly standard one and I didn’t find any flag to change this behavior (I might have missed one though, maybe a
toplevel_print_optionscustom predicate or something).That being said, you could roll your own print predicate if you want a prettyprint. It’d allow you to clean your database btw, because here you represent extra stuff from the original problem only to be able to return
empty, which isn’t good from a conception point of view. My take would be:Test: