



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




The Basic elements of PROLOG Terms Prolog programs consist of collections of statements. There are only a few kinds of statements in Prolog, but they can be complex.
Fact Statements
Prolog has two basic statement forms; these correspond to the headless and headed Horn clauses of predicate calculus.
Female(shelley). Male(bill). Female(mary). Male(jake). Father(bill,jake).
Father(bill, shelley). Mother(mary, jake). Mother(mother, shelley).
These simple structures state certain facts about jake, shelley, bill and mary. For example, the first states that shelley is a female. The last four connect their two parameters with a relationship that is named in the functor atom;
Rule Statements The other basic form of Prolog statements for constructing the database corresponds to a headed Horn clause. This form can be related to a known theorem in mathematics from which a conclusion can be drawn if the set of given conditions is satisfied.
Goal Statements The theorem is in the form of a proposition that we want the system to either prove or disprove. In Prolog, these propositions are called goals, or queries. The syntactic form of Prolog goal statements is identical to that of headless Horn clauses. For example, we could have
can be coded as facts, and the relationship between speed, time, and distance can be written as rule, as in the following: speed(ford, 100). speed(chevvy, 105). speed(dodge, 95). time(ford, 20). time(dodge, 24). distance(X, Y) :- speed(X, speed), time(X, Time), Y is Speed * Time.
Now queries can request the distance traveled by a particular car. For example, the query Distance(chevy, Chevy_Distance). instantiates Chevy_Distance with the value 2205. The first two clauses in the right side of the distance computation statement simply instantiate the variables Speed and Time with the corresponding values of the given automobile functor. After satisfying the goal, Prolog also displays the name Chevy_Distance and its value. Trace
Trace. Distance(chevy, Chevy_Distance). (1) 1 Call: distance (chevy, _0)? (2) 2 Call: speed(chevy, _5)? (2) 2 Exit: speed(chevy, 105) (3) 2 Call: time(chevy, _6)? (3) 2 Exit: time(chevy, 21) (4) 2 Call: _0 is 10521?* (4) 2 Exit: 2205 is 105* (1) 1 Exit: distance(chevy, 2205)
Chevy_Distance =
Symbols in the trace that begin with the underscore character (_) are internal variables used to store instantiated values. The first column of the trace indicates the subgoal whose match is currently being attempted. For example, in the trace above, the first line with the indication (3) is an attempt to instantiate the temporary variable _6 with a time value for chevy, where time is the second term in the right side of the statement that describes the computation of distance. The second column indicates the call depth of the matching process. The third column indicates the current action.
List Structures
(3) 3 Exit: reverse([c],[c]) (6) 3 Call: append([c],[b],_65636)? (7) 4 Call: append([],[b],_25)? (7) 4 Exit: append([],[b],[b]) (6) 3 Exit: append([c],[b],[c,b]) (2) 2 Exit: reverse([b,c],[c,b]) (8) 2 Call: append([c,b],[a],_6)? (9) 3 Call: append([b],[a],_32)? (10) 4 Call: append([],[a],_39)? (10) 4 Exit: append([],[a],[a]) (9) 3 Exit: append([b],[a],[b,a]) (8) 2 Exit: append([c,b],[a],[c,b,a]) (1) 1 Exit: reverse([a,b,c],[c,b,a])
Q=[c,b,a]