• No se han encontrado resultados

15.1 – BREVE COMENTARIO SOBRE LAS ENTREVISTAS.

In document Aceptando la tartamudez (página 63-91)

PROLOG stands for programming for logic. It is a general purpose programming language, but is particularly suitable for reasoning in computational intelligence programming.

3.3.1.1 A sample Prolog program Consider the following Prolog program.

father(john, tom). % john is tom's father. father(tom, mary).

father(tom, dave). grandfather(larry, kim).

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

(Note: "%" denotes the rest of the line contains comments.)

This program allows us to submit queries such as to find who is who's father or who is who's grandfather. In addition, it also allows us who is who's child or grandchild by taking the advantage of unification (see Section 3.3.1.8).

3.3.1.2 Structure of a Prolog statement

In general, a Prolog program implements connectives in first order predicate logic: "and" is implemented as (,), "or" is implemented as (;), "only if" is implemented a (← or :-), and the built-in predicate "not" denote ¬. A Prolog statement (called Horn clause) is of the form

A :- B1, B2, ..., Bn.

Head body

Note that there is at most one symbol in left hand side (LHS), which is referred to as the positive symbol. The reason of calling it the positive symbol can be understood by examining the following relationship between the conversion of the expressions:

A :- B1, B2, ..., Bn

≡ A ← B1, B2, ..., Bn

≡ B1, B2, ... Bn→ A

≡¬ (B1 ∧ B2 ∧ ... Bn) ∨ A

It is important to note that there are two readings for a Prolog statement:

Declarative reading: A if B1 and... and Bn.

For example, consider the following:

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

This statement defines the concept of grandfather: X is the grandfather of Y if there is a person Z so that X is Z's father and Z is Y's father. This is the declarative reading. The same statement also has a procedural reading: In order to make X the grandfather of Y, X should first become the father of a person Z, and Z should become the father of Y. Which reading is more appropriate? In this particular example, probably the declarative one, but in some other cases, procedural reading may be more appropriate.

An important note should be given here that Horn clause calculus is equivalent to the full FOPL for proofs by refutation. Another note is that in Prolog terminology, a fact can be considered as a rule without body (such as "father(john, tom)" in the above example). As a further note, we point out that the following two rules:

a:-b. a:-c. are equal to one rule: a :-b; c.

This is because by using distributive law and de Morgan's law, we have (b → a) ∧ (c → a)

= (¬b ∨ a) ∧ (¬c ∨ a) = (¬b ∧¬ c) ∨ a = ¬ (b ∨ c) ∨ a = (b ∨ c) → a

3.3.1.3 Remarks on structure of a Prolog program

We now give some remarks on how a Prolog program is structured. For more detail on Prolog language and its use in computational intelligence, please refer to references [Clocksin and Mellish 1987; Covinton, Nute and Vellino 1988 Shoham 1994, Sterling and Shapiro 1994, Deransart, Ed-Dbali and Cervoni 1996].

(a) All rules are true in the "knowledge base" at the same time, so they form a conjunction.

(b) Predicates with the same predicate name are grouped together. For example, the two statements on "grandfather" can be considered as a procedure "father." Earlier we introduced the perspective of viewing a predicate as denoting a relationship. What is being discussed here denotes another perspective.

(c) Different order of clauses (rules) or different order of predicates may affect the behavior of the program (this is due to implementation-related considerations, not from logic).

(d) Predicates with the same predicate symbol may have a different number of arguments (but predicates with a different number of arguments will

not unify). For example, it is legal to write p(X,Y) and p(a,Z,b), but they will not unify.

(e) The head of a predicate with the same predicate name in different rules may have different names for arguments (but with same arity). For example, in one rule we may have p(X,Y) :- … while in another rule we may have p(Z,[]) :-… and in a third one we may have p(_,W). (Note the underscore represents an unnamed variable in Prolog.)

(f) Prolog answers queries by unification. In unification, positions of arguments are important, while names of variables are not. Section 3.3.1.8 provides a little more detail on unification.

(g) Quantifiers in Prolog: You can think of variables appearing only in body ("local") as being existentially quantified, and parameter variables as universally quantified. For example, consider the following rule:

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

both variables X and Z are universally quantified, while Z is local to the body. The corresponding FOPL statement is:

X ∀Y grandfather(X,Y) ←∃Z father(X,Z), father(Z,Y).

(h) Remark on "global" variables: There are no global variables in the sense of conventional languages. "Global" information is passed around through arguments.

3.3.1.4 Two kinds of queries (retrieval and confirmation)

In an information system such as a knowledge base system, a query is a statement requesting the retrieval of a specific piece of information. In Prolog, we can retrieve a stored fact or a fact which can be derived from the existing rules and facts. There are two ways to submit queries for retrieval:

?- grandfather(john,Y).

%find John's (one or all) grandchildren. ?- grandfather(X, mary). %find Mary's grandfather. ?- grandfather(john, mary). % confirm or disconfirm. Consider the following simple example:

takes(george, cs101). %George takes CS101 course. takes(george, math201).

takes(george, mis201). takes(sue, cs101). takes(sue, math202). takes(kim,cs101).

friends (X,Y) :- takes(X,Z), takes(Y,Z).

%X and Y are friends if they take the same course. The following two queries illustrate two different types of query, namely, confirmation or retrieval:

?- takes(george, cs101). %confirmation type

?- takes(george, X). %retrieval type, X will be substituted by a % constant if such substitution exists. There are some basic things we should know about Prolog:

(a) For a retrieval type query, if a user enter a semicolon(;) after an answer is retrieved, that means the user is looking for other answers. By this way, a user can ask for all answers.

(b) There are two cases when the system returns a "yes": success for a confirmation type query; more answers for retrieval type query.

(c) There are also two cases when the system returns a "no": fail (for confirmation type queries) or no more answers (for retrieval type queries). 3.3.1.5 Closed world assumption

Closed world assumption (CWA) refers to the assumption that nothing else exists outside the closed world of the knowledge base. It is closely related to another notion, negation as failure. Prolog answers queries using this assumption. For example, if we submit a query "likes(tom, wine)," the answer would be no, because it is not in the knowledge base nor can it be derived. The search was limited to a small world.

3.3.1.6 Answering query through depth first search

In either case of query answering, Prolog tries to prove a goal (in case of retrieval type, a guess may be made first; order of predicates may make a difference.) A depth-first search tree (DFS) with necessary backtracks will be constructed dynamically. This is an And/Or tree because some nodes (the "and" nodes) denote conditions which must be satisfied together while other nodes (the "or" nodes) denote conditions which must be satisfied separately. As a concrete example, consider the following knowledge base in Prolog: q(a). %(1) r(c). %(2) s(b). %(3) p(X) :- q(X),write('r4'), nl. %(4) p(X) :- write('r5'), nl, q(X). %(5) p(X) :- s(X), r(Y). %(6)

Consider the query "p(b)." We want to determine the output produced by this program, and draw depth first search trees to explain the results obtained. Since this is confirmation type, at the vary beginning, X is bound to b. Rule (4) is first tried, and since it is not successful, rule (5) is then tired, which failed again. Finally Rule 6 is used and the query is eventually confirmed. Note that both node (5) and node (6) "are" and nodes. Note also that each time after a dead-end is reached, a new subtree is constructed.

Figure 3. 3 A Prolog search tree p(b)

(4) | (6) (5)

c(Y) failure write('r5') nl failure (2) (3) (looking for (looking for

3.3.1.7 Relationship with resolution proof

It is the time to give an important remark on the semantics of Prolog, particularly the relationship between Prolog and resolution proof: The set of Horn clause expressions is a subset of the resolution clause space in logic programming and the resolution theorem prover is acting as a Prolog interpreter. The role of resolution theorem prover described here is not quite accurate, but we will not pursue this further, since our main interest is in applied aspects of Prolog. A little more detailed discussion on this issue can be found in [Luger and Stubblefield 1998].

3.3.1.8 Unification through recursion

Recursion plays an important role in Prolog; in fact, looping is performed by recursion. Recursion is natural for a language for reasoning. Just think about the retrieving all of a person’s ancestors. Using recursion is much appropriate than using iteration, and one major reason is that usually we don't know how many generations away from the considered person there are. In general, recursion refers to what a program module (for example a procedure or a function) calls itself. In Prolog, recursion occurs when a predicate refers to itself (namely, the same predicate symbol appears in both the head and the body). In fact, even the search process is recursion-based. Just like in a conventional programming language, recursion requires a general case and a base case.

The list data structure is a good example of learning recursion-based search in Prolog. An example of Prolog list is [a, b, c]. A list can be viewed as consisting of a head and a tail: the head of a list is simply the first element of the list (caution: there is no relationship between the head of a list and the head of a Prolog rule!), while the tail is the rest of the entire list (so it is still a list!). We can write a list using the notation of [H|T] (here H stands for the head while T stands for the tail) is not same as [H, T]). Note this is different from writing it as [H,T]. For example, [a, b, c] can be unified with [H|T], here H = a, T = [b, c]

We can now study how recursion is done on Prolog list. Consider the following predicate which checks the membership of a list (Note member is actually a built-in predicate):

member(X,[X|T]). %base case member(X,[Y|T]) :- member(X,T). %general case

These two predicates actually form a procedure. Its meaning can be explained as:

If X is identical with the head of a list Then X is a member of this list

Else we have to check whether X is a member of the tail of the list. Here are some sample queries:

?- member(a,[a,b,c]). %confirmation ?- member(X,[a,b,c]). %retrieval

Now consider another example: length([], 0).

58 Predicate logic

length([H|T], N) :- length(T, M), N is M + 1.

The meaning of this program can be explained as follows: To find the length N of a list L do

if L is empty then let N = 0

else find the length M of the tail of L, then add 1 to M giving N.

As one more example, consider the algorithm for merging two sorted lists (used in merge sort as well as in many external sorting methods. You may compare a recursive program in C or Pascal.

merge([],L2,L2). merge(L1,[],L1). merge([H1|T1], [H2|T2], [H1|Rslt]) :- H1<H2,!, merge(T1, [H2|T2], Result). merge([H1|T1], [H2|T2], [H2|Rslt]) :- merge([H1|T1], T2, Result).

3.3.1.9 More remarks on unification

Continuing our previous example, how would Prolog answer the query of p(X)? Even though this is a retrieval type of query, Prolog still uses pretty much the same way as illustrated in the above. One important difference is that instead of trying to confirm p(b) directly, the search engine has to start from a guess. Rule (4) will be tried first as before, but the variable X in p(X) will not be unified with any constant until fact (1) is used to satisfy Rule 4. Unlike the previous query, this time executing rule (4) results in success. Another important remark about retrieval type of query is that if the user is interested in additional answers, then the construction of search tree continues. Different from the case of confirmation type of query, a new subtree could be constructed not because of the failure, but because of looking for more answers. The reader is advised to complete the search tree for answering query p(X).

3.3.1.10 Using built-in predicates

The following are some built-in predicates in Prolog:

not: a predicate introduce as a logical connective;

cut (written as !): a goal with no arguments; it always succeeds and prevents backtracking;

fail: a predicate introduced due to some language consideration,

nl: new line.

For other built-in predicates in Prolog, please consult a Prolog book, such as [Clocksin and Mellish 1987, Covington et al. 1988].

In document Aceptando la tartamudez (página 63-91)

Documento similar