开发者

Program and Data are the Same in Prolog?

I have heard that i开发者_JAVA技巧n Prolog, program and data are the same thing. What does that mean?


Prolog source is just a list of rules. Some rules are just "data" - they are true without further evaluation.

person(james).
father(james, thomas).

"James is a person." "James is the father of thomas."

These rules are the data.

I can run a query against this data. I can ask:

?- person(X).

The answer will be:

X = james.

Or:

?- father(X, thomas).

The answer will be the same.

Other rules need further evaluation.

grandfather(X, Z) :- father(X, Y), father(Y, Z).

This is a simple "program".

Our grandfather program will evaluate to true if we have the right data. For example:

father(james, william).
father(james, tyler).
father(james, thomas).
father(jeff, james).

If I execute the following program:

?- grandfather(jeff, X).

I get:

X = william

I can ask prolog to continue and I will get X = tyler and X = thomas.

The syntax gets more complicated, but the basics are the same. The data and the program are just a set of facts. The art of prolog is making the right rules that drive the computation to a result.


It means that your program is implemented as a bunch of rules, and data is also implemented as a bunch of rules - there's no distinction between a rule that causes some operations to happen (a program), and a rule that just gives back a data value.


This refers to terms being data, but the program also being described in terms.


Just to put on my pedant's hat- the name for this is: homoiconic. There's a lot of it about: machine Code is, also, homoiconic.


 ?- A=write(B), C=(B is 1+2), Prog = (C, A).
A = write(B),
C = (B is 1+2),
Prog = (B is 1+2, write(B)).

 ?- $Prog.
3
true.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜