1 / 14

Prolog Fundamentals

Prolog Fundamentals. Review Last Lecture. A Prolog program consists of a database of facts and rules, and queries (questions). Fact: ... . Rule: ... :- ... . Query: ?- ... . Variables: must begin with an upper case letter or underscore. Nora, _nora.

ban
Download Presentation

Prolog Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prolog Fundamentals

  2. Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). Fact: ... . Rule: ... :- ... . Query: ?- ... . Variables: must begin with an upper case letter or underscore. Nora, _nora. Constants (atoms): begin with lowercase letter, or enclosed in single quotes. uncle_ali, nora, year2007, . numbers3, 6, 2957, 8.34, ... complex termsAn atom (the functor) is followed by a comma separated sequence of Prolog terms enclosed in parenthesis (the arguments). likes(lama,apples), likes(amy,X). 2

  3. Outline Predicate Definitions Matching Backtracking Recursion 3

  4. Predicate Definitions Both facts and rules are predicate definitions. Predicate:is the name given to the word occurring before the bracket in a fact or rule: parent(ali,samir). By defining a predicate you are specifying which information needs to be known for the property denoted by the predicate to be true. Predicate name 4

  5. Prolog Matching (unification) Two atoms match if they are the same atom. Ex.: nora = nora, but nora ≠ ’Nora’. A variable matches any other Prolog term. The variable gets instantiated with the other term. Ex.: X = teacher(jacky) Ex.: X = Y Two complex terms match if they have the same functorand the same number of arguments and if all pairs of parallel arguments match. Ex.: like(sara,basma) = like(sara,X) Ex.: like(sara,basma) ≠ like(X,X) 5

  6. Prolog Execution When Prolog tries to answer a query, it does so by trying to match the goal to the head of the rule. This might result in some variables getting bound. Example: KB:cleaner(nancey).Query: cleaner (mary). cleaner (shona). nurse(sister_jacky). ?- cleaner(X). X = nancey ; X = mary ; X = shona ; No • Prolog checks for facts that match the query. (There are three.) • Prolog starts from the top of the knowledge base and, therefore, finds cleaner(nancey) first. • Typing ; forces Prolog to check whether there are other possibilities. 6

  7. Example likes(huda, X) :- fresh(X), sweet(X). fresh(Y) :- juicy(Y). juicy(orange). sweet(orange). ?- likes(huda, orange). MATCHES likes(huda, orange) to head of rule 1, with X=orange. Sets fresh(orange), sweet(orange) as new goals. Tries to prove fresh(orange). MATCHES head of rule 2. Y=orange. Tries to prove juicy(orange). MATCHES a fact. So proved. Tries to prove sweet(orange). MATCHES a fact, So proved. 7

  8. Proof tree. Likes (huda, orange) Using rule 1 sweet(orange) fresh(orange) Using rule 2 True fact juicy(orange) True fact likes(huda, X) :- fresh(X), sweet(X). fresh(Y) :- juicy(Y). juicy(orange). sweet(orange). ?- likes(huda, orange). 8

  9. Backtracking Prolog goes through facts/rules top to bottom looking for facts or rule heads which match the goal. If a rule fails as can’t prove body, Prolog will try next rule/fact matching current goal. If can’t find ANY way to prove current goal, Prolog will retrythe previous goal, to see if it can be solved another way. Example: facts only teacher(saleh). teacher(nora). teacher('Aunt Laura'). father( saleh, jaber). teacher(X). X = saleh ; X = nora ; X = 'Aunt Laura' ; No Note: Here we use “;” to ask it to look for other solutions, which forces backtracking. 9

  10. Example using facts & rules (1) likes(huda, X) :- fresh(X), sweet(X). fresh(apple). fresh(orange). sweet(orange). ?- likes(huda, What). Matches head of rule with X=What (binding two variables to same value). Tries to satisfy fresh(What). fresh(apple) succeeds. Tries to satisfy sweet(apple). This fails. So backtracks and retries fresh(What). Succeeds with What=orange etc. 10

  11. Example using facts & rules (2) female(lara). female(hana). female(sara). female(dia). haschildren(sara). haschildren(dia). mother(X) :-female(X), haschildren(X). mother(aisha). • ?- mother(X). • Matches with head of first rule. • Tries to satisfy female(X). • Matches female(lara). • Tries to satisfy haschildren(lara). • FAILS, so GOES BACK to try • female(X) again. • Maches femal(sara). • Tries haschildrens(sara). • Succeeds, so mother(X) succeeds/ • X = sara ; • Going back and trying later femalfacts: • X = dia; • And trying later “mother” fact: • X = aisha. 11

  12. Recursion in any language is a function that can call itself until the goal has been succeed. In Prolog, when a predicate contain a goal that refers to itself. Example. parent(noha,jaber). parent(jaber,salim). parent(salim,reda). ancestor(X,Y) :- parent(X,Y). /* If X is a parent of Y, then X is an ancestor of Y */ ancestor(X,Z) :- parent(X,Y), ancestor(Y,Z). /* if Y is an ancestor of Z and X is a parent of Y, then X is an ancestor of Z */ recursion 12

  13. Recursion (Cont.) KB: Query: ?- ancestor(jaber,reda). • parent(noha,jaber). • parent(jaber,salim). • parent(salim,reda). • ancestor(X,Y) :- parent(X,Y). • ancestor(X,Z) :- parent(X,Y), ancestor(Y,Z). 1.Prolog will first try the goal parent(jaber,reda). and it will fail. 2. then try the second clause of ancestor. The new query is parent(jaber,Y). 3.Prolog will find Y=salim and try ancestor(salim,reda) wich will conduct to check parent(salim,reda). This is succesfull. As a result the goal ancestor(salim,reda) succeeds. Then ancestor(jaber,reda) can succeed. 13

  14. Summary Matching: Prolog tries to prove goals by matching them with rules/facts. Tries to find variable bindings making expressions identical. SEARCHING the whole activity of the Prolog system is to search through various options to find a combination that succeeds. Main search tools are backtracking and recursion Backtracking Prolog goes through facts/rules from top to bottom to try to find matching rules or facts. when the system fails during its search, it returns to previous choices to see if making a different choice would allow success. Recursion when a predicate contain a goal that refers to itself. 14

More Related