APPENDIX A LOGIC PROGRAMMING WITH PROLOG AND BNF Syntax for Prolog

0
31

APPENDIX A LOGIC PROGRAMMING WITH PROLOG

To program in Prolog, one defines a database of facts about the given information and conditional clauses or rules about how additional information can be deduced from the facts. A query sets the Prolog interpreter into action to try to infer a solution using the database of clauses.To program in Prolog, one defines a database of facts about the given infor-
mation and conditional clauses or rules about how additional information can be deduced from the facts. A query sets the Prolog interpreter into action to try to infer a solution using the database of clauses. Prolog is a relatively small programming language as evidenced by a BNF specification of the core part of Prolog given in Figure A.2. The language
contains a large set of predefined predicates and notational variations such as infix symbols that are not defined in this specification. In addition, Prolog allows a special syntax for lists that will be introduced later

<program> ::= <clause list> <query> | <query>
<clause list> ::= <clause> | <clause list> <clause>
<clause> ::= <predicate> . | <predicate> :- <predicate list> .
<predicate list> ::= <predicate> | <predicate list> , <predicate>
<predicate> ::= <atom> | <atom> ( <term list> )
<term list> ::= <term> | <term list> , <term>
<term> ::= <numeral> | <atom> | <variable> | <structure>
<structure> ::= <atom> ( <term list> )
<query> ::= ?- <predicate list> .
<atom> ::= <small atom> | ‘ <string> ‘
<small atom> ::= <lowercase letter> | <small atom> <character>
<variable> ::= <uppercase letter> | <variable> <character>
<lowercase letter> ::= a | b | c | d | … | x | y | z
<uppercase letter> ::= A | B | C | D | … | X | Y | Z | _
<numeral> ::= <digit> | <numeral> <digit>
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<character> ::= <lowercase letter> | <uppercase letter>
| <digit> | <special>
<special> ::= + | – | * | / | \ | ^ | ~ | : | . | ? | @ | # | $ | &
<string> ::= <character> | <string> <character>

The simple example in this section serves as an introduction to Prolog programming for the beginner. Remember that a Prolog program consists of a collection of facts and rules defined to constrain the logic interpreter in such a way that when we submit a query, the resulting answers solve the problems at hand. Facts, rules, and queries can all be entered interactively, but usually a Prolog programmer creates a file containing the facts and rules, and then after “consulting” this file, enters only the queries interactively. See the documentation for instructions on consulting a file with a particular implementation of Prolog. We develop the example incrementally, adding facts and rules to the database in several stages. User queries will be shown in boldface followed by the response from the Prolog interpreter. Comments start with the symbol % and continue to the end of the line.

Some facts: parent(chester,irvin).
parent(chester,clarence).
parent(chester,mildred).
parent(irvin,ron).
parent(irvin,ken).
parent(clarence,shirley).
parent(clarence,sharon).
parent(clarence,charlie).
parent(mildred,mary).

Some queries:
?- parent(chester,mildred).
yes
?- parent(X,ron).
X = irvin
yes
?- parent(irvin,X).
X = ron;
X = ken;% The user-typed semicolon asks the system for
no % more solutions.
?- parent(X,Y).
X =chester
Y = irvin % System will list all of the parent pairs, one at a time,
yes% if semicolons are entered.
Additional facts:
male(chester). female(mildred).
male(irvin).    female(shirley).

male(clarence). female(sharon).
male(ron). female(mary).
male(ken).
male(charlie).

Additional queries:
?- parent(clarence,X), male(X).
X = charlie
yes
?- male(X), parent(X,ken).
X = irvin
yes
?- parent(X,ken), female(X).
no

Prolog obeys the “closed world assumption” that presumes that any predi-
cate that cannot be proved must be false.
?- parent(X,Y), parent(Y,sharon).
X = chester
Y = clarence
yes

These queries suggest definitions of several family relationships.
Some rules: father(X,Y) :- parent(X,Y), male(X).
grandparent(X,Y) :- parent(X,Z), parent(Z,Y).
paternalgrandfather(X,Y) :- father(X,Z), father(Z,Y).
sibling(X,Y) :- parent(Z,X), parent(Z,Y).
The scope of a variable in Prolog is solely the clause in which it occurs.
Additional queries:
?- paternalgrandfather(X,ken).
X = chester
yes
?- paternalgrandfather(chester,X).
X = ron;
X = ken;
X = shirley;% Note the reversal of the roles of input and output.
X = sharon;
X = charlie;
no

?- sibling(ken,X).
X = ron;
X = ken;
no

The inference engine concludes that ken is a sibling of ken since parent(irvin,ken)
and parent(irvin,ken) both hold. To avoid this consequence, the description of
sibling needs to be more carefully constructed.

CEVAP VER