Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

parameter passing - Example of how pass by value-result works in Prolog

I'm new to Prolog and I am aware of how pass by value-result works. However, I noticed that there's a lack of learning materials for Prolog and would like to see an example of how it works in this language.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The answer is: it doesn't.

In standard terms, you always and only "pass by reference". The objects you pass-by-reference are the terms (these are directed, possibly cyclic graphs, generally trees), and they are globally visible (possibly modulo optimizations for strings, atoms, numbers etc).

So when you call this goal:

?- member(X,[1,2,3]).

the predicate member/2 receives a reference to an "empty cell" in a global storage area, above named by the variable name X (and it will try to fill it with something useful), and a reference to a structure, here, written [1,2,3] probably in the global storage area.

On return, the value for X, filled by member/2, is printed:

?- member(X,[1,2,3]).
X = 1 ;
...

As for return values, in standard terms, a called predicate (expressed by an atomic goal like p(X,Y)) can either return true (signifying that "the predicate succeeded" / "the goal was proved" / "the goal can be labeled logically TRUE") or false (signifying that "the proof of the predicate failed" / "there is no proof for this goal" / "there is no reason to label this goal logically TRUE"). Alternatively it can throw an exception.

Note that there is no way in Prolog to express "the goal can be labeled logically FALSE" (even though Prolog may print arrogantly false in response to a query), although there is "negation as failure", but that's for another question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...