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 3692242
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:14:23+00:00 2026-05-19T04:14:23+00:00

I’m in the process of learning Prolog. I have this recursive predicate: add(0,Y,Y). add(succ(X),Y,succ(Z))

  • 0

I’m in the process of learning Prolog.
I have this recursive predicate:

add(0,Y,Y).
add(succ(X),Y,succ(Z)) :-
add(X,Y,Z).

Alright, so experienced Prolog programmers will probably understand what this predicate does. At first sight it doesn’t look too hard to understand; when given two numerals as the first and second argument, it will return the result of adding them up as its third argument. Sounds simple? Indeed.

But I have a major problem comprehending how this actually works. I’ve re-read the description of this predicate many times but yet I am not able to understand how it actually works. And that’s why I’m asking for help.

By querying this:
add(succ(succ(succ(0))), succ(succ(0)), R).
Prolog will instantiate and thus return R as succ(succ(succ(succ(succ(0))))). Fine. This must be correct. However I have no clue why it is so.

What I have problems understanding is how and why Prolog strips off the outermost succ functor from the original query. Again, as it is recursive, it passes on the results as arguments onto itself, and thus stripping off the outermost succ functor until the goal can be unified. I do understand how the succ(Z) and the argument R works, but I am seemingly unable to comprehend how it actually strips off the outermost succ functor, as mentioned. For me, it seems like it’s adding a succ instead of stripping it, because of succ(X) in the predicate definition.

Any help is greatly appreciated. Thanks in advance!

  • 1 1 Answer
  • 1 View
  • 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-05-19T04:14:24+00:00Added an answer on May 19, 2026 at 4:14 am

    Prolog sees the line add(succ(X),Y,succ(Z)) ... and thinks “oh, it can be matched to the query add(succ(succ(succ(0))), succ(succ(0)), R)., because I can set (unify) X=succ(succ(0)), Y=succ(succ(0)), succ(Z)=R !” The important condition for Prolog, the one that determines it can do it is that succ(succ(succ(0))) can be matched to succ(X), the rest is just simple, because Y and R can be in this first step set arbitrarily (they are not unified yet). And succ(succ(succ(0))) can be matched to succ(X) just because succ(A) can be matched to succ(B) any time A can be matched to B – you see it is not very hard for computer to decide it.

    The next step is that Prolog looks at the rest of the line (:- add(X,Y,Z)) and generate query add(succ(succ(0)), succ(succ(0)), Z). Remember that succ(Z)=R and it won’t change until (if) the computation path is rejected. The next query will be add(succ(0), succ(succ(0)), Z'), where succ(Z’)=Z. Then add(0, succ(succ(0)), Z''), where succ(Z”)=Z’.

    Then can Prolog use the first rule, determine that Z''=Y=succ(succ(0)). Then there is nothing left to do, so it just write to the output that R=succ(Z)=succ(succ(Z'))=succ(succ(succ(Y)))=succ(succ(succ(succ(succ(0))))).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.