Presentation is loading. Please wait.

Presentation is loading. Please wait.

CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.

Similar presentations


Presentation on theme: "CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog."— Presentation transcript:

1 CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog

2 PROLOG ->PROgramming in LOGic Prolog is a logic language that is suitable for symbolic or non-numeric computation. Prolog is frequently used in Artificial Intelligence where manipulation of symbols and inference about them is common. Prolog consists of a series of rules and facts. A program is run by presenting some query and seeing if it can be proved against the set of known rules and facts.

3 Topics in Prolog: ◦ Simple facts ◦ Facts with arguments ◦ Variables and unification ◦ Rules ◦ Search ◦ Recursion ◦ Lists

4 Topics in Prolog: ◦ Simple facts ◦ Facts with arguments ◦ Variables and unification ◦ Rules ◦ Search ◦ Recursion ◦ Lists

5 Represent a statement that is true Can consist of a particular item or a relationship between items Must begin with a lowercase letter! Examples of simple facts: raining./*it is raining*/ mary_has_a_coat./*mary has a coat*/ jackLikesDogs./*jack likes dogs*/

6 Represent more complicated facts Consist of a relation and the items it refers to Facts can have an arbitrary number of arguments (>0) Relation names must begin with a lowercase letter! Examples of facts with arguments: hungry( paul )./*paul is hungry*/ likes( john, mary )./*john likes mary*/ plays( john, football )./*john plays football*/

7 Variables are used to query the database Variable names must begin with an uppercase letter! For example: the letter ‘X’ is a variable the word ‘Who’ is a variable and the string ‘Abcd’ is a variable

8 To query database must replace unknown data in our query with a variable For example: Assuming the facts stated earlier are in our database If we want to find out who john likes, the query: ?-likes( john, X ). returns X = mary The process of matching items with variables is known as unification

9 So far we have looked at how to represent facts and how to query a database of facts Rules allow us to make conditional statements i.e. “the main statement is true if its sub- statement(s) are true” Rules are written in the form: main_statement( ) :- sub_statement( ).

10 Consider the following statement: “All footballers are well paid” In other words: “If a person is a footballer then they are well paid” or“A person is well paid if they are a footballer” This statement can be written in Prolog as a rule: well_paid( X ) :- footballer( X ).

11 More complex conditional statements can also be written in Prolog using rules Consider the following statement: “A house is energy efficient if it is well insulated and has double glazed windows” This can be written in Prolog as the following rule: energy_efficient( X ) :- house( X ), well_insulated( X ), double_glazed_windows( X ).

12 Consider the following statement: “A car has good fuel economy if it is a hybrid or it is a diesel” Statements with a disjunction can be written in Prolog as two separate rules: good_fuel_economy( X ) :- car( X ), hybrid( X ). good_fuel_economy( X ) :- car( X ), diesel( X ).

13 Introduces backtracking in Prolog Consider the following database of facts: eats( jack, curry ). eats( jack, pizza ). eats( jack, tuna ). Earlier we queried a database of facts to find out a single item of information ?- eats( jack, What ).

14 This query will return:What = curry Suppose we wish to answer the question: “What are all the things that jack eats?” ?- eats( jack, What ).returnsWhat = curry Can continue to ask Prolog for more results using ; What = pizza;What = tuna;No

15 Backtracking in rules Consider the following database: hold_party( X ) :- birthday( X ), happy( X ). birthday( tom ). birthday( jack ). birthday( helen ). happy( mary ). happy( helen ).

16 If we want to use the information contained in this database to decide who to hold a party for we can submit the following query: ?- hold_party( Who ). Prolog begins by finding a clause for ‘birthday’ and then binds Who to the argument of clause Prolog will then attempt to find a clause for ‘happy’ that matches the argument bound to Who If a match is not found, the search fails and Prolog backtracks to attempt to find a new birthday clause

17 hold_party( X ) :- birthday( X ), happy( X ). birthday( tom ). birthday( jack ). birthday( helen ). happy( mary ). happy( helen ). 1.Who binds to tom, search for happy( tom ), fail 2.Who binds to jack, search for happy( jack ), fail 3.Who binds to helen, search for happy( helen ), pass


Download ppt "CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog."

Similar presentations


Ads by Google