I am learning Prolog and I have stumbled upon this piece of code and I cannot make sense of it:


Code:
append([],Ys,Ys).

append([X|Xs],Ys,[X|Zs]):-
            append(Xs, Ys, Zs).


In the example, the instruction given is:

Code:
append([1,2],[3,4],Ys).


and the output is Ys = [1,2,3,4]

And I don't understand how that happens.

I understand that the values on Xs get removed one by one until it equals [], what I don't understand is
1-Why is Zs being changed; if the fact is append([],Ys,Ys), Zs should be equal to [3,4] so why changing it in any way if it already obeys the fact?
2-How do [1,2] get in the beginning of Zs??

I used trace and the result of that is even more confuse, because Zs is always values like _G5166, _G5112, etc...
What are those values? Are those memory addresses?

And why is it that, even if I don't use write(), the console displays Ys = ...? And why Ys and not Xs or Zs?
First consider the result of:

Code:

append([1,2],[3,4],[1,2,3,4]).

What does this tell you?






Your primary problem here is a misunderstanding of the nature of Prolog as a language: you don't issue instructions to Prolog. You provide it with facts and then ask questions. Your final interaction is asking "what value of Ys will satisfy the definitions of the append() relationship I previously gave". For most early Prolog programs you shouldn't be concerned about the "how" at all. The "why" is the important part, and the "why" in this case goes back to understanding in what circumstances append(X,Y,Z) returns true.
EDIT:

Wait I finally get it.

So, each time the append function is called recursively it sends only the tail of the first element, that's why it is getting smaller.

The head of the first element is send as the head of the third, so when I use trace and it shows things like _G12345, it is in reality something like [head of the first element| value we want].

When the first element is finally [], it makes the 3rd element equal to the third, so it obeys the fact.

Then, as each recursive call is exited, all the values sent as head of the third element come together.

Yeah, it's confuse but I think I get it. Thanks!
I think you're still a bit confused by how Prolog works, as demonstrated by the fact that you're talking about "functions" and "recursive calls" Smile

Prolog doesn't have functions, it has queries, facts and rules. And there's really nothing to be "called". A program in Prolog is really just a list of facts and rules (and optionally some comments, which are ignored as with any other programming language). You load ("consult") the program, and the Prolog interpreter now has its own little universe made of the rules and facts contained in the program.
You can then ask Prolog things about that universe, and it will try to give you an answer (sometimes it can't, because the programs do not define the "universe" properly or because your "question" is not well made, or doesn't make sense in that "universe"). These "questions" you make are the queries.

Prolog works with unification and SLD resolution and other concepts that are too complex for me to explain here, but the general idea is that it "combines" the queries with the facts and rules such that it finds a substitution for the variables in your query (variables start with a uppercase letter) for constants (which start in lowercase). There can be more than one answer to the query, and Prolog can give them all if you ask it. For example, given the very simple program, which says that Prolog and Java are programming languages:

Code:
progLanguage(prolog).
progLanguage(java).

(note that prolog and java start with lowercase letters: they are constants)
and the query:

Code:
?- progLanguage(X).

(X is a variable: starts with uppercase)
Prolog will give the first reply, which consists of substituting X for the constant prolog:

Code:
X = prolog

If you press space, it will attempt to give another reply:

Code:
X = java.
(the dot means there are no more answers)
You can also assert that in the current little universe made by the facts contained in the program, java is a programming language, and that PHP is not a programming language, and neither is bananas:

Code:
?- progLanguage(java).
true.

?- progLanguage(php).
false.

?- progLanguage(bananas).
false.


Note that in no case progLanguage was "called", and there is no such thing as a "return value". There are replies to the query, nothing more.

If you are just getting started with Prolog I'd say it's best if you avoid lists for a start. I don't find lists in Prolog to be very easy to understand; I believe it's best if you start by trying to understand how rules and facts work, by writing your own "little universes", or as they call it, knowledge bases.
I also don't find SWI-Prolog's debugging tools very easy to use, at least for a beginner, but it's probably the best thing available. Unfortunately, there don't seem to be many good introductory materials on the internet (there's http://lpn.swi-prolog.org/ , does anyone know of anything else?).

Prolog really is different from other programming languages, and that's to be expected, since it operates on a different paradigm (logic programming instead of the more usual imperative programming), but that's exactly what makes it better suited to certain tasks. It forces you to think in a different way when programming in it and learning it is definitely a good exercise for the brain, even if you never end up using it in practice for anything serious.
Alright, thanks!

I have been using SWISH http://swish.swi-prolog.org/ btw.

I have been reading further and I think I can understand how those list-related rules work better now.

Prolog is quite addicting, and yes, it is indeed a good exercise for the brain.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement