Docsity

OOPS - Logical Programming, Study notes of Object Oriented Programming

Toggle sidebar
bg1
LOGICAL PROGRAMMING
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.
• All Prolog statements are constructed from terms.
• A Prolog term is a constant, a variable, or a structure.
• A constant is either an atom or an integer.
• A variable is any string of letters, digits, and underscores that begins with an
uppercase letter. Variables are not bound to types by declarations.
• The last kind of term is called a structure. Structures represent the atomic
propositions of predicate calculus, and their general form is the same:
Functor(parameter list)
The functor is any atom and is used to identify the structure. The parameter list can be
any list of atoms, variable, or other structures.
Fact Statements
Prolog has two basic statement forms; these correspond to the headless and
headed Horn clauses of predicate calculus.
• The simplest form of headless Horn clause in Prolog is a single structure, which is
interpreted as an unconditional assertion, or fact.
• Logically, facts are simply propositions that are assumed to be true.
The following examples illustrate the kinds of facts one can have in a Prolog
program. Notice that every Prolog statement is terminated by a period.
Female(shelley).
Male(bill).
Female(mary).
Male(jake).
Father(bill,jake).
pf3
pf4
pf5

Partial preview of the text

Download OOPS - Logical Programming and more Study notes Object Oriented Programming in PDF only on Docsity!

LOGICAL PROGRAMMING

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.

  • All Prolog statements are constructed from terms.
  • A Prolog term is a constant, a variable, or a structure.
  • A constant is either an atom or an integer.
  • A variable is any string of letters, digits, and underscores that begins with an uppercase letter. Variables are not bound to types by declarations.
  • The last kind of term is called a structure. Structures represent the atomic propositions of predicate calculus, and their general form is the same: Functor(parameter list) The functor is any atom and is used to identify the structure. The parameter list can be any list of atoms, variable, or other structures.

Fact Statements

Prolog has two basic statement forms; these correspond to the headless and headed Horn clauses of predicate calculus.

  • The simplest form of headless Horn clause in Prolog is a single structure, which is interpreted as an unconditional assertion, or fact.
  • Logically, facts are simply propositions that are assumed to be true. The following examples illustrate the kinds of facts one can have in a Prolog program. Notice that every Prolog statement is terminated by a period.

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.

  • Conjunctions contain multiple terms that are separated by logical AND operations. One could consider the commas to be And operators. Female (shelley), child(shelley).
  • (^) The general form of the Prolog headed horn clause statement is Consequence_1 :- antecedent_expression. It is read as follows: “consequence_1 can be concluded if the antecedent expression is true or can be made to be true by some instantiation of its variables”. For example, Ancestor(mary, shelley) :- mother(mary, shelley). States that is mary is the mother of shelley, then mary is an ancestor of shelley. Headed Horn clauses are called rules because they state rules of implication between propositions.

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

  • Prolog has a built-in structure named trace that displays the instantiations of values to variables at each step during the attempt to satisfy a given goal.
  • Trace is used to understand and debug the attempt to satisfy a given goal. Trace is used to understand and debug Prolog programs.
  • To understand trace, it is best to introduce a different model of the execution of Prolog programs, called the tracing model.
  • The tracing model describes Prolog execution in terms of four events.
    • (1) call, which occurs at the beginning of an attempt to satisfy a goal
    • (2) exit, which occurs when a goal has been satisfied,
    • (3) redo, which occurs when backtrack causes an attempt to re satisfy a goal,
    • (4) fail, which occurs when a goal fails. The following is a trace of the computation of the value for Chevy_Distance:

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

  • The basic data structure supported is the list, which is similar to the list structure used by LISP.
  • Lists are sequences of any number of elements, where the elements can be atoms, atomic propositions, or any other terms, including other lists. Prolog uses a conventional syntax to specify lists. The list elements are separated by commas, and the entire list is delimited by square brackets as in [apple, prune, grape, kumquat] The notation [] is used to denote the empty list. Instead of having explicit functions for constructing and dismantling the lists, Prolog simply uses a special notation.

(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]