Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:44:46+00:00 2026-06-10T21:44:46+00:00

In the process of learning Prolog I’m making simple program which given some thing

  • 0

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 ?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:44:47+00:00Added an answer on June 10, 2026 at 9:44 pm

    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_options custom 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:

    % 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).
    
    below(X, Y) :-
        above(Y, X).
    
    print_custom(Y, Pred) :-
        (   call(Pred, X, Y)
         -> true
          ; X = empty),
        format('~w = ~w,~n', [Pred, X]).
    
    
    position(Thing) :-
        maplist( print_custom(Thing),
                 [left, right, above, below] ).
    

    Test:

    ?- position(clock).
    left = pencil,
    right = butterfly,
    above = empty,
    below = empty,
    true.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am in the process of learning backbone.js and have written some very simple
Im the process of learning C++. I got stuck debugging some of my code
I'm in the process of learning Javascript and I'm trying to create a simple
I'm in the process of learning Interfaces. I've been reading through some books/articles and
I am in the process of learning MySQL and querying, and right now working
I'm in the process of learning Prolog. I have this recursive predicate: add(0,Y,Y). add(succ(X),Y,succ(Z))
I'm in the process of learning LINQ and would like some help the following
As a part of learning process, I am writing simple IM app, using Remoting
I'm in the process of learning Javascript and I'm trying to create a simple
I'm in the process of learning Clojure and I can't understand some language design

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.