Write simple
fact for the statement using PROLOG.
A Prolog program consists of a number of clauses. Each clause is either
a fact or a rule. After a Prolog program is loaded (or consulted)
in a Prolog interpreter, users can submit goals or queries, and the Prolog intepreter will
give results (answers) according to the facts and rules.
A fact must start with
a predicate (which is an atom) and end with a fullstop. The predicate may be
followed by one or more arguments which are enclosed by parentheses. The
arguments can be atoms (in this case, these atoms are treated as constants),
numbers, variables or lists. Arguments are separated by commas.
If we consider the arguments in a fact to be objects, then the predicate of the fact
describes a property of the objects.
In a Prolog program, a presence of a fact indicates a statement that is
true. An absence of a fact indicates a statement that is not true. See the
following example:
Program 2: A
Prolog program with facts only |
sunny. |
Goals/Queries and their results (in red): |
?- sunny. /* The response is yes because the
fact "sunny." is present. */ yes |
?- rainy. /* There is an error because there
is no predicate "rainy". */ Erroneous result. |
?- father(john, mary). yes |
?- mother(susan, mary). /* This cannot be deduced. */ no |
?- father(john, susan). /* This cannot be deduced. */ no |
A rule can be viewed
as an extension of a fact with added conditions that also have to be satisfied
for it to be true. It consists of two parts. The first part is similar to a
fact (a predicate with arguments). The second part consists of other clauses
(facts or rules which are separated by commas) which must all be true for the
rule itself to be true. These two parts are separated by ":-". You may interpret this operator
as "if" in English.
See the following example:
Program 3: A
program describes the relationships of the members in a family |
father(jack, susan).
/*
Fact 1 */ |
Take Rule 3 as an example. It means that "granfather(X, Y)" is true if
both "father(X, Z)" and "parent(Z, X)" are true. The comma between the two conditions can be considered
as a logical-AND operator.
You may see that both Rules 1 and 2 start with "parent(X, Y)". When will
"parent(X, Y)" be true? The answer is any one of these two rules is true. This
means that "parent(X, Y)" is true when "father(X, Y)" is true, or "mother(X, Y)" is true.
As you can see from Rules 3 to 5, predicates that only appear in rules
but not facts (in this case, parent) can also form conditions of other
rules.
Comments
Post a Comment